~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/join.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 * @{
28
28
 */
29
29
 
30
 
#include <config.h>
 
30
#include "config.h"
31
31
 
32
32
#include <float.h>
33
33
#include <math.h>
34
34
 
35
 
#include <drizzled/item/cache.h>
36
 
#include <drizzled/item/cmpfunc.h>
37
 
#include <drizzled/item/copy_string.h>
38
 
#include <drizzled/item/uint.h>
39
 
#include <drizzled/cached_item.h>
40
 
#include <drizzled/sql_base.h>
41
 
#include <drizzled/sql_select.h> /* include join.h */
42
 
#include <drizzled/lock.h>
43
 
#include <drizzled/nested_join.h>
44
 
#include <drizzled/join.h>
45
 
#include <drizzled/join_cache.h>
46
 
#include <drizzled/show.h>
47
 
#include <drizzled/field/blob.h>
48
 
#include <drizzled/optimizer/position.h>
49
 
#include <drizzled/optimizer/sargable_param.h>
50
 
#include <drizzled/optimizer/key_use.h>
51
 
#include <drizzled/optimizer/range.h>
52
 
#include <drizzled/optimizer/sum.h>
53
 
#include <drizzled/optimizer/explain_plan.h>
54
 
#include <drizzled/optimizer/access_method_factory.h>
55
 
#include <drizzled/optimizer/access_method.h>
56
 
#include <drizzled/records.h>
57
 
#include <drizzled/probes.h>
58
 
#include <drizzled/internal/my_bit.h>
59
 
#include <drizzled/internal/my_sys.h>
60
 
#include <drizzled/internal/iocache.h>
61
 
#include <drizzled/plugin/storage_engine.h>
62
 
#include <drizzled/session.h>
63
 
#include <drizzled/select_result.h>
 
35
#include "drizzled/item/cache.h"
 
36
#include "drizzled/item/cmpfunc.h"
 
37
#include "drizzled/item/copy_string.h"
 
38
#include "drizzled/item/uint.h"
 
39
#include "drizzled/cached_item.h"
 
40
#include "drizzled/sql_base.h"
 
41
#include "drizzled/sql_select.h" /* include join.h */
 
42
#include "drizzled/lock.h"
 
43
#include "drizzled/nested_join.h"
 
44
#include "drizzled/join.h"
 
45
#include "drizzled/join_cache.h"
 
46
#include "drizzled/show.h"
 
47
#include "drizzled/field/blob.h"
 
48
#include "drizzled/optimizer/position.h"
 
49
#include "drizzled/optimizer/sargable_param.h"
 
50
#include "drizzled/optimizer/key_use.h"
 
51
#include "drizzled/optimizer/range.h"
 
52
#include "drizzled/optimizer/sum.h"
 
53
#include "drizzled/optimizer/explain_plan.h"
 
54
#include "drizzled/optimizer/access_method_factory.h"
 
55
#include "drizzled/optimizer/access_method.h"
 
56
#include "drizzled/records.h"
 
57
#include "drizzled/probes.h"
 
58
#include "drizzled/internal/my_bit.h"
 
59
#include "drizzled/internal/my_sys.h"
 
60
#include "drizzled/internal/iocache.h"
 
61
#include "drizzled/plugin/storage_engine.h"
64
62
 
65
63
#include <drizzled/debug.h>
66
64
 
310
308
 
311
309
bool Join::is_top_level_join() const
312
310
{
313
 
  return (unit == &session->getLex()->unit && (unit->fake_select_lex == 0 ||
 
311
  return (unit == &session->lex->unit && (unit->fake_select_lex == 0 ||
314
312
                                          select_lex == unit->fake_select_lex));
315
313
}
316
314
 
351
349
  join_list= &select_lex->top_join_list;
352
350
  union_part= unit_arg->is_union();
353
351
 
354
 
  session->getLex()->current_select->is_item_list_lookup= 1;
 
352
  session->lex->current_select->is_item_list_lookup= 1;
355
353
  /*
356
354
    If we have already executed SELECT, then it have not sense to prevent
357
355
    its table from update (see unique_table())
392
390
 
393
391
  if (having)
394
392
  {
395
 
    nesting_map save_allow_sum_func= session->getLex()->allow_sum_func;
 
393
    nesting_map save_allow_sum_func= session->lex->allow_sum_func;
396
394
    session->setWhere("having clause");
397
 
    session->getLex()->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
395
    session->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
398
396
    select_lex->having_fix_field= 1;
399
397
    bool having_fix_rc= (!having->fixed &&
400
398
       (having->fix_fields(session, &having) ||
402
400
    select_lex->having_fix_field= 0;
403
401
    if (having_fix_rc || session->is_error())
404
402
      return(-1);
405
 
    session->getLex()->allow_sum_func= save_allow_sum_func;
 
403
    session->lex->allow_sum_func= save_allow_sum_func;
406
404
  }
407
405
 
408
406
  {
452
450
            in_subs  &&                                                   // 1
453
451
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
454
452
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
455
 
            session->getLex()->sql_command == SQLCOM_SELECT)                       // *
 
453
            session->lex->sql_command == SQLCOM_SELECT)                       // *
456
454
        {
457
455
          if (in_subs->is_top_level_item() &&                             // 4
458
456
              !in_subs->is_correlated &&                                  // 5
625
623
    select_limit= HA_POS_ERROR;
626
624
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
627
625
  // Ignore errors of execution if option IGNORE present
628
 
  if (session->getLex()->ignore)
629
 
    session->getLex()->current_select->no_error= 1;
 
626
  if (session->lex->ignore)
 
627
    session->lex->current_select->no_error= 1;
630
628
 
631
629
#ifdef HAVE_REF_TO_FIELDS     // Not done yet
632
630
  /* Add HAVING to WHERE if possible */
726
724
        conjunctions.
727
725
        Preserve conditions for EXPLAIN.
728
726
      */
729
 
      if (conds && !(session->getLex()->describe & DESCRIBE_EXTENDED))
 
727
      if (conds && !(session->lex->describe & DESCRIBE_EXTENDED))
730
728
      {
731
729
        COND *table_independent_conds= make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
732
730
        conds= table_independent_conds;
761
759
      !(select_options & SELECT_DESCRIBE) &&
762
760
      (!conds ||
763
761
       !(conds->used_tables() & RAND_TABLE_BIT) ||
764
 
       select_lex->master_unit() == &session->getLex()->unit)) // upper level SELECT
 
762
       select_lex->master_unit() == &session->lex->unit)) // upper level SELECT
765
763
  {
766
764
    zero_result_cause= "no matching row in const table";
767
765
    goto setup_subq_exit;
821
819
 
822
820
  if (conds &&!outer_join && const_table_map != found_const_table_map &&
823
821
      (select_options & SELECT_DESCRIBE) &&
824
 
      select_lex->master_unit() == &session->getLex()->unit) // upper level SELECT
 
822
      select_lex->master_unit() == &session->lex->unit) // upper level SELECT
825
823
  {
826
824
    conds=new Item_int((int64_t) 0,1);  // Always false
827
825
  }
1175
1173
    */
1176
1174
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
1177
1175
                             !tmp_group &&
1178
 
                             !session->getLex()->current_select->with_sum_func) ?
 
1176
                             !session->lex->current_select->with_sum_func) ?
1179
1177
                            select_limit : HA_POS_ERROR;
1180
1178
 
1181
1179
    if (!(exec_tmp_table1=
1871
1869
    for a derived table which is always materialized.
1872
1870
    Otherwise we would not be able to print the query  correctly.
1873
1871
  */
1874
 
  if (items0 && (session->getLex()->describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
 
1872
  if (items0 && (session->lex->describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
1875
1873
    set_items_ref_array(items0);
1876
1874
 
1877
1875
  return;
2003
2001
    Optimization: if not EXPLAIN and we are done with the Join,
2004
2002
    free all tables.
2005
2003
  */
2006
 
  bool full= (select_lex->uncacheable.none() && ! session->getLex()->describe);
 
2004
  bool full= (select_lex->uncacheable.none() && ! session->lex->describe);
2007
2005
  bool can_unlock= full;
2008
2006
 
2009
2007
  cleanup(full);
2038
2036
  if (can_unlock && lock && session->lock &&
2039
2037
      !(select_options & SELECT_NO_UNLOCK) &&
2040
2038
      !select_lex->subquery_in_having &&
2041
 
      (select_lex == (session->getLex()->unit.fake_select_lex ?
2042
 
                      session->getLex()->unit.fake_select_lex : &session->getLex()->select_lex)))
 
2039
      (select_lex == (session->lex->unit.fake_select_lex ?
 
2040
                      session->lex->unit.fake_select_lex : &session->lex->select_lex)))
2043
2041
  {
2044
2042
    /*
2045
2043
      TODO: unlock tables even if the join isn't top level select in the
2112
2110
      We can't call delete_elements() on copy_funcs as this will cause
2113
2111
      problems in free_elements() as some of the elements are then deleted.
2114
2112
    */
2115
 
    tmp_table_param.copy_funcs.clear();
 
2113
    tmp_table_param.copy_funcs.empty();
2116
2114
    /*
2117
2115
      If we have tmp_join and 'this' Join is not tmp_join and
2118
2116
      tmp_table_param.copy_field's  of them are equal then we have to remove
2213
2211
                              bool before_group_by, 
2214
2212
                              bool recompute)
2215
2213
{
2216
 
  List<Item>::iterator it(field_list.begin());
 
2214
  List_iterator_fast<Item> it(field_list);
2217
2215
  Item_sum **func;
2218
2216
  Item *item;
2219
2217
 
2281
2279
  {
2282
2280
    rollup.getNullItems()[i]= new (session->mem_root) Item_null_result();
2283
2281
    List<Item> *rollup_fields= &rollup.getFields()[i];
2284
 
    rollup_fields->clear();
 
2282
    rollup_fields->empty();
2285
2283
    rollup.getRefPointerArrays()[i]= ref_array;
2286
2284
    ref_array+= all_fields.elements;
2287
2285
  }
2294
2292
    }
2295
2293
  }
2296
2294
 
2297
 
  List<Item>::iterator it(all_fields.begin());
 
2295
  List_iterator<Item> it(all_fields);
2298
2296
  Item *item;
2299
2297
  while ((item= it++))
2300
2298
  {
2369
2367
*/
2370
2368
bool Join::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields, Item_sum ***func)
2371
2369
{
2372
 
  List<Item>::iterator it(fields_arg.begin());
 
2370
  List_iterator_fast<Item> it(fields_arg);
2373
2371
  Item *first_field= sel_fields.head();
2374
2372
  uint32_t level;
2375
2373
 
2400
2398
    uint32_t pos= send_group_parts - level -1;
2401
2399
    bool real_fields= 0;
2402
2400
    Item *item;
2403
 
    List<Item>::iterator new_it(rollup.getFields()[pos].begin());
 
2401
    List_iterator<Item> new_it(rollup.getFields()[pos]);
2404
2402
    Item **ref_array_start= rollup.getRefPointerArrays()[pos];
2405
2403
    Order *start_group;
2406
2404
 
2414
2412
    for (i= 0, start_group= group_list ;i++ < pos ;start_group= start_group->next)
2415
2413
    {}
2416
2414
 
2417
 
    it= fields_arg.begin();
 
2415
    it.rewind();
2418
2416
    while ((item= it++))
2419
2417
    {
2420
2418
      if (item == first_field)
2549
2547
    {
2550
2548
      int write_error;
2551
2549
      Item *item;
2552
 
      List<Item>::iterator it(rollup.getFields()[i].begin());
 
2550
      List_iterator_fast<Item> it(rollup.getFields()[i]);
2553
2551
      while ((item= it++))
2554
2552
      {
2555
2553
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
3231
3229
            have STRING_RESULT result type, we increase the length
3232
3230
            by 8 as maximum pack length of such fields.
3233
3231
          */
3234
 
          if (field::isDateTime(type))
 
3232
          if (type == DRIZZLE_TYPE_DATE ||
 
3233
              type == DRIZZLE_TYPE_TIME ||
 
3234
              type == DRIZZLE_TYPE_DATETIME ||
 
3235
              type == DRIZZLE_TYPE_MICROTIME ||
 
3236
              type == DRIZZLE_TYPE_TIMESTAMP)
3235
3237
          {
3236
3238
            key_length+= 8;
3237
3239
          }
3527
3529
    i.e. they have subqueries, unions or call stored procedures.
3528
3530
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
3529
3531
  */
3530
 
  if (join->session->getLex()->is_single_level_stmt())
 
3532
  if (join->session->lex->is_single_level_stmt())
3531
3533
    join->session->status_var.last_query_cost= join->best_read;
3532
3534
  return(false);
3533
3535
}
4040
4042
        will ensure that this will be used
4041
4043
      */
4042
4044
      best= tmp;
4043
 
      records= rnd_records;
 
4045
      records= rows2double(rnd_records);
4044
4046
      best_key= 0;
4045
4047
      /* range/index_merge/ALL/index access method are "independent", so: */
4046
4048
      best_ref_depends_map= 0;
4721
4723
      if (join->tables > 1)
4722
4724
        cond->update_used_tables();             // Tablenr may have changed
4723
4725
      if (join->const_tables == join->tables &&
4724
 
          session->getLex()->current_select->master_unit() ==
4725
 
          &session->getLex()->unit)             // not upper level SELECT
 
4726
          session->lex->current_select->master_unit() ==
 
4727
          &session->lex->unit)          // not upper level SELECT
4726
4728
        join->const_table_map|=RAND_TABLE_BIT;
4727
4729
      {                                         // Check const tables
4728
4730
        COND *const_cond=
4791
4793
        tab->ref.key= -1;
4792
4794
        tab->ref.key_parts= 0;          // Don't use ref key.
4793
4795
        cur_pos= join->getPosFromOptimalPlan(i);
4794
 
        cur_pos.setFanout(tab->quick->records);
 
4796
        cur_pos.setFanout(rows2double(tab->quick->records));
4795
4797
        /*
4796
4798
           We will use join cache here : prevent sorting of the first
4797
4799
           table only and sort at the end.
4800
4802
          join->full_join= 1;
4801
4803
      }
4802
4804
 
4803
 
      if (join->full_join and not session->getLex()->current_select->is_cross and not cond)
 
4805
      if (join->full_join and not session->lex->current_select->is_cross and not cond)
4804
4806
      {
4805
4807
        my_error(ER_CARTESIAN_JOIN_ATTEMPTED, MYF(0));
4806
4808
        return 1;
5292
5294
  {
5293
5295
    if (send_row)
5294
5296
    {
5295
 
      List<Item>::iterator it(fields.begin());
 
5297
      List_iterator_fast<Item> it(fields);
5296
5298
      Item *item;
5297
5299
      while ((item= it++))
5298
5300
        item->no_rows_in_result();
5430
5432
  TableList *table;
5431
5433
  NestedJoin *nested_join;
5432
5434
  TableList *prev_table= 0;
5433
 
  List<TableList>::iterator li(join_list->begin());
 
5435
  List_iterator<TableList> li(*join_list);
5434
5436
 
5435
5437
  /*
5436
5438
    Try to simplify join operations from join_list.
5566
5568
    Flatten nested joins that can be flattened.
5567
5569
    no ON expression and not a semi-join => can be flattened.
5568
5570
  */
5569
 
  li= join_list->begin();
 
5571
  li.rewind();
5570
5572
  while ((table= li++))
5571
5573
  {
5572
5574
    nested_join= table->getNestedJoin();
5573
5575
    if (nested_join && !table->on_expr)
5574
5576
    {
5575
5577
      TableList *tbl;
5576
 
      List<TableList>::iterator it(nested_join->join_list.begin());
 
5578
      List_iterator<TableList> it(nested_join->join_list);
5577
5579
      while ((tbl= it++))
5578
5580
      {
5579
5581
        tbl->setEmbedding(table->getEmbedding());
5596
5598
 
5597
5599
  /* Calculate how many saved fields there is in list */
5598
5600
  field_count=0;
5599
 
  List<Item>::iterator it(fields.begin());
 
5601
  List_iterator<Item> it(fields);
5600
5602
  Item *item;
5601
5603
  while ((item=it++))
5602
5604
  {
5650
5652
                               bool *hidden_group_fields)
5651
5653
{
5652
5654
  int res;
5653
 
  nesting_map save_allow_sum_func=session->getLex()->allow_sum_func ;
 
5655
  nesting_map save_allow_sum_func=session->lex->allow_sum_func ;
5654
5656
 
5655
 
  session->getLex()->allow_sum_func&= ~(1 << session->getLex()->current_select->nest_level);
 
5657
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
5656
5658
  res= session->setup_conds(tables, conds);
5657
5659
 
5658
 
  session->getLex()->allow_sum_func|= 1 << session->getLex()->current_select->nest_level;
 
5660
  session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
5659
5661
  res= res || setup_order(session, ref_pointer_array, tables, fields, all_fields,
5660
5662
                          order);
5661
 
  session->getLex()->allow_sum_func&= ~(1 << session->getLex()->current_select->nest_level);
 
5663
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
5662
5664
  res= res || setup_group(session, ref_pointer_array, tables, fields, all_fields,
5663
5665
                          group, hidden_group_fields);
5664
 
  session->getLex()->allow_sum_func= save_allow_sum_func;
 
5666
  session->lex->allow_sum_func= save_allow_sum_func;
5665
5667
  return(res);
5666
5668
}
5667
5669
 
6138
6140
*/
6139
6141
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, uint32_t first_unused)
6140
6142
{
6141
 
  List<TableList>::iterator li(join_list->begin());
 
6143
  List_iterator<TableList> li(*join_list);
6142
6144
  TableList *table;
6143
6145
  while ((table= li++))
6144
6146
  {
6209
6211
*/
6210
6212
static void reset_nj_counters(List<TableList> *join_list)
6211
6213
{
6212
 
  List<TableList>::iterator li(join_list->begin());
 
6214
  List_iterator<TableList> li(*join_list);
6213
6215
  TableList *table;
6214
6216
  while ((table= li++))
6215
6217
  {