~ubuntu-branches/ubuntu/raring/drizzle/raring

« back to all changes in this revision

Viewing changes to drizzled/join.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2011-03-15 10:41:18 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20110315104118-eaf0hvlytjdl4zrf
Tags: 2011.03.13-0ubuntu1
* New upstream release.
* Added slave plugin.
* Removed archive, blackhole and blitzdb plugins.
* Moved location of libdrizzle headers.
* Removed drizzleadmin manpage patch.
* Add drizzle_safe_write_string to symbols.

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
#include <drizzled/select_result.h>
64
64
#include <drizzled/debug.h>
65
65
#include <drizzled/item/subselect.h>
66
 
 
 
66
#include <drizzled/my_hash.h>
 
67
#include <drizzled/sql_lex.h>
67
68
#include <algorithm>
68
69
 
69
70
using namespace std;
70
71
 
71
 
namespace drizzled
72
 
{
 
72
namespace drizzled {
 
73
 
73
74
extern plugin::StorageEngine *heap_engine;
74
75
 
75
76
/** Declarations of static functions used in this source file. */
310
311
 
311
312
bool Join::is_top_level_join() const
312
313
{
313
 
  return (unit == &session->getLex()->unit && (unit->fake_select_lex == 0 ||
 
314
  return (unit == &session->lex().unit && (unit->fake_select_lex == 0 ||
314
315
                                          select_lex == unit->fake_select_lex));
315
316
}
316
317
 
351
352
  join_list= &select_lex->top_join_list;
352
353
  union_part= unit_arg->is_union();
353
354
 
354
 
  session->getLex()->current_select->is_item_list_lookup= 1;
 
355
  session->lex().current_select->is_item_list_lookup= 1;
355
356
  /*
356
357
    If we have already executed SELECT, then it have not sense to prevent
357
358
    its table from update (see unique_table())
392
393
 
393
394
  if (having)
394
395
  {
395
 
    nesting_map save_allow_sum_func= session->getLex()->allow_sum_func;
 
396
    nesting_map save_allow_sum_func= session->lex().allow_sum_func;
396
397
    session->setWhere("having clause");
397
 
    session->getLex()->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
398
    session->lex().allow_sum_func|= 1 << select_lex_arg->nest_level;
398
399
    select_lex->having_fix_field= 1;
399
400
    bool having_fix_rc= (!having->fixed &&
400
401
       (having->fix_fields(session, &having) ||
402
403
    select_lex->having_fix_field= 0;
403
404
    if (having_fix_rc || session->is_error())
404
405
      return(-1);
405
 
    session->getLex()->allow_sum_func= save_allow_sum_func;
 
406
    session->lex().allow_sum_func= save_allow_sum_func;
406
407
  }
407
408
 
408
409
  {
452
453
            in_subs  &&                                                   // 1
453
454
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
454
455
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
455
 
            session->getLex()->sql_command == SQLCOM_SELECT)                       // *
 
456
            session->lex().sql_command == SQLCOM_SELECT)                       // *
456
457
        {
457
458
          if (in_subs->is_top_level_item() &&                             // 4
458
459
              !in_subs->is_correlated &&                                  // 5
625
626
    select_limit= HA_POS_ERROR;
626
627
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
627
628
  // Ignore errors of execution if option IGNORE present
628
 
  if (session->getLex()->ignore)
629
 
    session->getLex()->current_select->no_error= 1;
 
629
  if (session->lex().ignore)
 
630
    session->lex().current_select->no_error= 1;
630
631
 
631
632
#ifdef HAVE_REF_TO_FIELDS     // Not done yet
632
633
  /* Add HAVING to WHERE if possible */
726
727
        conjunctions.
727
728
        Preserve conditions for EXPLAIN.
728
729
      */
729
 
      if (conds && !(session->getLex()->describe & DESCRIBE_EXTENDED))
 
730
      if (conds && !(session->lex().describe & DESCRIBE_EXTENDED))
730
731
      {
731
732
        COND *table_independent_conds= make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
732
733
        conds= table_independent_conds;
761
762
      !(select_options & SELECT_DESCRIBE) &&
762
763
      (!conds ||
763
764
       !(conds->used_tables() & RAND_TABLE_BIT) ||
764
 
       select_lex->master_unit() == &session->getLex()->unit)) // upper level SELECT
 
765
       select_lex->master_unit() == &session->lex().unit)) // upper level SELECT
765
766
  {
766
767
    zero_result_cause= "no matching row in const table";
767
768
    goto setup_subq_exit;
821
822
 
822
823
  if (conds &&!outer_join && const_table_map != found_const_table_map &&
823
824
      (select_options & SELECT_DESCRIBE) &&
824
 
      select_lex->master_unit() == &session->getLex()->unit) // upper level SELECT
 
825
      select_lex->master_unit() == &session->lex().unit) // upper level SELECT
825
826
  {
826
 
    conds=new Item_int((int64_t) 0,1);  // Always false
 
827
    conds=new Item_int(0, 1);  // Always false
827
828
  }
828
829
 
829
830
  if (make_join_select(this, select, conds))
1174
1175
    */
1175
1176
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
1176
1177
                             !tmp_group &&
1177
 
                             !session->getLex()->current_select->with_sum_func) ?
 
1178
                             !session->lex().current_select->with_sum_func) ?
1178
1179
                            select_limit : HA_POS_ERROR;
1179
1180
 
1180
1181
    if (!(exec_tmp_table1=
1513
1514
    session->set_proc_info("Copying to tmp table");
1514
1515
    if (! curr_join->sort_and_group && curr_join->const_tables != curr_join->tables)
1515
1516
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
1516
 
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
1517
    if ((tmp_error= do_select(curr_join, NULL, curr_tmp_table)))
1517
1518
    {
1518
1519
      error= tmp_error;
1519
1520
      return;
1612
1613
              exec_tmp_table2= create_tmp_table(session,
1613
1614
                                                &curr_join->tmp_table_param,
1614
1615
                                                *curr_all_fields,
1615
 
                                                (Order*) 0,
 
1616
                                                NULL,
1616
1617
                                                curr_join->select_distinct &&
1617
1618
                                                !curr_join->group_list,
1618
1619
                                                1, curr_join->select_options,
1664
1665
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
1665
1666
      
1666
1667
      if (setup_sum_funcs(curr_join->session, curr_join->sum_funcs) 
1667
 
        || (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
1668
        || (tmp_error= do_select(curr_join, NULL, curr_tmp_table)))
1668
1669
      {
1669
1670
        error= tmp_error;
1670
1671
        return;
1870
1871
    for a derived table which is always materialized.
1871
1872
    Otherwise we would not be able to print the query  correctly.
1872
1873
  */
1873
 
  if (items0 && (session->getLex()->describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
 
1874
  if (items0 && (session->lex().describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
1874
1875
    set_items_ref_array(items0);
1875
1876
 
1876
1877
  return;
2002
2003
    Optimization: if not EXPLAIN and we are done with the Join,
2003
2004
    free all tables.
2004
2005
  */
2005
 
  bool full= (select_lex->uncacheable.none() && ! session->getLex()->describe);
 
2006
  bool full= (select_lex->uncacheable.none() && ! session->lex().describe);
2006
2007
  bool can_unlock= full;
2007
2008
 
2008
2009
  cleanup(full);
2037
2038
  if (can_unlock && lock && session->lock &&
2038
2039
      !(select_options & SELECT_NO_UNLOCK) &&
2039
2040
      !select_lex->subquery_in_having &&
2040
 
      (select_lex == (session->getLex()->unit.fake_select_lex ?
2041
 
                      session->getLex()->unit.fake_select_lex : &session->getLex()->select_lex)))
 
2041
      (select_lex == (session->lex().unit.fake_select_lex ?
 
2042
                      session->lex().unit.fake_select_lex : &session->lex().select_lex)))
2042
2043
  {
2043
2044
    /*
2044
2045
      TODO: unlock tables even if the join isn't top level select in the
2324
2325
          Item* new_item= new Item_func_rollup_const(item);
2325
2326
          if (!new_item)
2326
2327
            return 1;
2327
 
          new_item->fix_fields(session, (Item **) 0);
 
2328
          new_item->fix_fields(session, NULL);
2328
2329
          *it.ref()= new_item;
2329
2330
          for (Order *tmp= group_tmp; tmp; tmp= tmp->next)
2330
2331
          {
3526
3527
    i.e. they have subqueries, unions or call stored procedures.
3527
3528
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
3528
3529
  */
3529
 
  if (join->session->getLex()->is_single_level_stmt())
 
3530
  if (join->session->lex().is_single_level_stmt())
3530
3531
    join->session->status_var.last_query_cost= join->best_read;
3531
3532
  return(false);
3532
3533
}
4720
4721
      if (join->tables > 1)
4721
4722
        cond->update_used_tables();             // Tablenr may have changed
4722
4723
      if (join->const_tables == join->tables &&
4723
 
          session->getLex()->current_select->master_unit() ==
4724
 
          &session->getLex()->unit)             // not upper level SELECT
 
4724
          session->lex().current_select->master_unit() ==
 
4725
          &session->lex().unit)         // not upper level SELECT
4725
4726
        join->const_table_map|=RAND_TABLE_BIT;
4726
4727
      {                                         // Check const tables
4727
4728
        COND *const_cond=
4728
4729
          make_cond_for_table(cond,
4729
4730
              join->const_table_map,
4730
 
              (table_map) 0, 1);
 
4731
              0, 1);
4731
4732
        for (JoinTable *tab= join->join_tab+join->const_tables;
4732
4733
            tab < join->join_tab+join->tables ; tab++)
4733
4734
        {
4799
4800
          join->full_join= 1;
4800
4801
      }
4801
4802
 
4802
 
      if (join->full_join and not session->getLex()->current_select->is_cross and not cond)
 
4803
      if (join->full_join and not session->lex().current_select->is_cross and not cond)
4803
4804
      {
4804
4805
        my_error(ER_CARTESIAN_JOIN_ATTEMPTED, MYF(0));
4805
4806
        return 1;
5649
5650
                               bool *hidden_group_fields)
5650
5651
{
5651
5652
  int res;
5652
 
  nesting_map save_allow_sum_func=session->getLex()->allow_sum_func ;
 
5653
  nesting_map save_allow_sum_func=session->lex().allow_sum_func ;
5653
5654
 
5654
 
  session->getLex()->allow_sum_func&= ~(1 << session->getLex()->current_select->nest_level);
 
5655
  session->lex().allow_sum_func&= ~(1 << session->lex().current_select->nest_level);
5655
5656
  res= session->setup_conds(tables, conds);
5656
5657
 
5657
 
  session->getLex()->allow_sum_func|= 1 << session->getLex()->current_select->nest_level;
 
5658
  session->lex().allow_sum_func|= 1 << session->lex().current_select->nest_level;
5658
5659
  res= res || setup_order(session, ref_pointer_array, tables, fields, all_fields,
5659
5660
                          order);
5660
 
  session->getLex()->allow_sum_func&= ~(1 << session->getLex()->current_select->nest_level);
 
5661
  session->lex().allow_sum_func&= ~(1 << session->lex().current_select->nest_level);
5661
5662
  res= res || setup_group(session, ref_pointer_array, tables, fields, all_fields,
5662
5663
                          group, hidden_group_fields);
5663
 
  session->getLex()->allow_sum_func= save_allow_sum_func;
 
5664
  session->lex().allow_sum_func= save_allow_sum_func;
5664
5665
  return(res);
5665
5666
}
5666
5667
 
5750
5751
      if (!table->cursor->stats.records && !embedding)
5751
5752
      {                                         // Empty table
5752
5753
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
5753
 
        set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
 
5754
        set_position(join, const_count++, s, NULL);
5754
5755
        continue;
5755
5756
      }
5756
5757
      outer_join|= table->map;
5778
5779
              (table->cursor->getEngine()->check_flag(HTON_BIT_STATS_RECORDS_IS_EXACT)) &&
5779
5780
        !join->no_const_tables)
5780
5781
    {
5781
 
      set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
 
5782
      set_position(join, const_count++, s, NULL);
5782
5783
    }
5783
5784
  }
5784
5785
  stat_vector[i]=0;
5902
5903
            table->mark_as_null_row();
5903
5904
            found_const_table_map|= table->map;
5904
5905
            join->const_table_map|= table->map;
5905
 
            set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
 
5906
            set_position(join, const_count++, s, NULL);
5906
5907
            goto more_const_tables_found;
5907
5908
           }
5908
5909
          keyuse++;
5921
5922
          int tmp= 0;
5922
5923
          s->type= AM_SYSTEM;
5923
5924
          join->const_table_map|=table->map;
5924
 
          set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
 
5925
          set_position(join, const_count++, s, NULL);
5925
5926
          partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
5926
5927
          if ((tmp= s->joinReadConstTable(partial_pos)))
5927
5928
          {
6069
6070
          caller to abort with a zero row result.
6070
6071
        */
6071
6072
        join->const_table_map|= s->table->map;
6072
 
        set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
 
6073
        set_position(join, const_count++, s, NULL);
6073
6074
        s->type= AM_CONST;
6074
6075
        if (*s->on_expr_ref)
6075
6076
        {
6175
6176
*/
6176
6177
static Table *get_sort_by_table(Order *a, Order *b,TableList *tables)
6177
6178
{
6178
 
  table_map map= (table_map) 0;
 
6179
  table_map map= 0;
6179
6180
 
6180
6181
  if (!a)
6181
6182
    a= b;                                       // Only one need to be given