~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Andrew Hutchings
  • Date: 2011-02-02 12:51:57 UTC
  • mto: (2157.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2158.
  • Revision ID: andrew@linuxjedi.co.uk-20110202125157-r6thh7ox4g9l90ec
Make transaction_message_threshold a read-only global variable instead of a per-session variable
Make replication_dictionary column lengths use transation_message_threshold

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
  @defgroup Query_Optimizer  Query Optimizer
23
23
  @{
24
24
*/
25
 
#include <config.h>
 
25
#include "config.h"
26
26
 
27
27
#include <string>
28
28
#include <iostream>
29
29
#include <algorithm>
30
30
#include <vector>
31
31
 
32
 
#include <drizzled/sql_select.h> /* include join.h */
33
 
 
34
 
#include <drizzled/error.h>
35
 
#include <drizzled/gettext.h>
36
 
#include <drizzled/util/test.h>
37
 
#include <drizzled/name_resolution_context_state.h>
38
 
#include <drizzled/nested_join.h>
39
 
#include <drizzled/probes.h>
40
 
#include <drizzled/show.h>
41
 
#include <drizzled/item/cache.h>
42
 
#include <drizzled/item/cmpfunc.h>
43
 
#include <drizzled/item/copy_string.h>
44
 
#include <drizzled/item/uint.h>
45
 
#include <drizzled/cached_item.h>
46
 
#include <drizzled/sql_base.h>
47
 
#include <drizzled/field/blob.h>
48
 
#include <drizzled/check_stack_overrun.h>
49
 
#include <drizzled/lock.h>
50
 
#include <drizzled/item/outer_ref.h>
51
 
#include <drizzled/index_hint.h>
52
 
#include <drizzled/records.h>
53
 
#include <drizzled/internal/iocache.h>
54
 
#include <drizzled/drizzled.h>
55
 
#include <drizzled/plugin/storage_engine.h>
56
 
 
57
 
#include <drizzled/sql_union.h>
58
 
#include <drizzled/optimizer/key_field.h>
59
 
#include <drizzled/optimizer/position.h>
60
 
#include <drizzled/optimizer/sargable_param.h>
61
 
#include <drizzled/optimizer/key_use.h>
62
 
#include <drizzled/optimizer/range.h>
63
 
#include <drizzled/optimizer/quick_range_select.h>
64
 
#include <drizzled/optimizer/quick_ror_intersect_select.h>
65
 
 
66
 
#include <drizzled/filesort.h>
67
 
#include <drizzled/sql_lex.h>
68
 
#include <drizzled/session.h>
69
 
#include <drizzled/sort_field.h>
70
 
#include <drizzled/select_result.h>
 
32
#include "drizzled/sql_select.h" /* include join.h */
 
33
 
 
34
#include "drizzled/error.h"
 
35
#include "drizzled/gettext.h"
 
36
#include "drizzled/util/test.h"
 
37
#include "drizzled/name_resolution_context_state.h"
 
38
#include "drizzled/nested_join.h"
 
39
#include "drizzled/probes.h"
 
40
#include "drizzled/show.h"
 
41
#include "drizzled/item/cache.h"
 
42
#include "drizzled/item/cmpfunc.h"
 
43
#include "drizzled/item/copy_string.h"
 
44
#include "drizzled/item/uint.h"
 
45
#include "drizzled/cached_item.h"
 
46
#include "drizzled/sql_base.h"
 
47
#include "drizzled/field/blob.h"
 
48
#include "drizzled/check_stack_overrun.h"
 
49
#include "drizzled/lock.h"
 
50
#include "drizzled/item/outer_ref.h"
 
51
#include "drizzled/index_hint.h"
 
52
#include "drizzled/records.h"
 
53
#include "drizzled/internal/iocache.h"
 
54
#include "drizzled/drizzled.h"
 
55
 
 
56
#include "drizzled/sql_union.h"
 
57
#include "drizzled/optimizer/key_field.h"
 
58
#include "drizzled/optimizer/position.h"
 
59
#include "drizzled/optimizer/sargable_param.h"
 
60
#include "drizzled/optimizer/key_use.h"
 
61
#include "drizzled/optimizer/range.h"
 
62
#include "drizzled/optimizer/quick_range_select.h"
 
63
#include "drizzled/optimizer/quick_ror_intersect_select.h"
 
64
 
 
65
#include "drizzled/filesort.h"
71
66
 
72
67
using namespace std;
73
68
 
212
207
  bool res= false;
213
208
  bool direct_ref= false;
214
209
 
215
 
  List<Item_outer_ref>::iterator ref_it(select->inner_refs_list.begin());
 
210
  List_iterator<Item_outer_ref> ref_it(select->inner_refs_list);
216
211
  while ((ref= ref_it++))
217
212
  {
218
213
    Item *item= ref->outer_ref;
219
214
    Item **item_ref= ref->ref;
220
215
    Item_ref *new_ref;
221
216
    /*
222
 
      @todo this field item already might be present in the select list.
 
217
      TODO: this field item already might be present in the select list.
223
218
      In this case instead of adding new field item we could use an
224
219
      existing one. The change will lead to less operations for copying fields,
225
220
      smaller temporary tables and less data passed through filesort.
425
420
    goto err; // 1
426
421
  }
427
422
 
428
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
423
  if (session->lex->describe & DESCRIBE_EXTENDED)
429
424
  {
430
425
    join->conds_history= join->conds;
431
426
    join->having_history= (join->having?join->having:join->tmp_having);
436
431
 
437
432
  join->exec();
438
433
 
439
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
434
  if (session->lex->describe & DESCRIBE_EXTENDED)
440
435
  {
441
436
    select_lex->where= join->conds_history;
442
437
    select_lex->having= join->having_history;
575
570
    substitutions.
576
571
  */
577
572
  sz= sizeof(optimizer::KeyField) *
578
 
      (((session->getLex()->current_select->cond_count+1)*2 +
579
 
        session->getLex()->current_select->between_count)*m+1);
580
 
  if (! (key_fields= (optimizer::KeyField*) session->getMemRoot()->allocate(sz)))
 
573
      (((session->lex->current_select->cond_count+1)*2 +
 
574
        session->lex->current_select->between_count)*m+1);
 
575
  if (! (key_fields= (optimizer::KeyField*) session->alloc(sz)))
581
576
    return true;
582
577
  and_level= 0;
583
578
  field= end= key_fields;
618
613
 
619
614
  /* Process ON conditions for the nested joins */
620
615
  {
621
 
    List<TableList>::iterator li(join_tab->join->join_list->begin());
 
616
    List_iterator<TableList> li(*join_tab->join->join_list);
622
617
    TableList *table;
623
618
    while ((table= li++))
624
619
    {
752
747
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
753
748
{
754
749
  List<Item_field> indexed_fields;
755
 
  List<Item_field>::iterator indexed_fields_it(indexed_fields.begin());
 
750
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
756
751
  Order      *cur_group;
757
752
  Item_field *cur_item;
758
753
  key_map possible_keys(0);
766
761
  else if (join->select_distinct)
767
762
  { /* Collect all query fields referenced in the SELECT clause. */
768
763
    List<Item> &select_items= join->fields_list;
769
 
    List<Item>::iterator select_items_it(select_items.begin());
 
764
    List_iterator<Item> select_items_it(select_items);
770
765
    Item *item;
771
766
    while ((item= select_items_it++))
772
767
      item->walk(&Item::collect_item_field_processor, 0,
1009
1004
  j->ref.key_length=length;
1010
1005
  j->ref.key=(int) key;
1011
1006
  if (!(j->ref.key_buff= (unsigned char*) session->calloc(ALIGN_SIZE(length)*2)) ||
1012
 
      !(j->ref.key_copy= (StoredKey**) session->getMemRoot()->allocate((sizeof(StoredKey*) *
 
1007
      !(j->ref.key_copy= (StoredKey**) session->alloc((sizeof(StoredKey*) *
1013
1008
               (keyparts+1)))) ||
1014
 
      !(j->ref.items=    (Item**) session->getMemRoot()->allocate(sizeof(Item*)*keyparts)) ||
1015
 
      !(j->ref.cond_guards= (bool**) session->getMemRoot()->allocate(sizeof(uint*)*keyparts)))
 
1009
      !(j->ref.items=    (Item**) session->alloc(sizeof(Item*)*keyparts)) ||
 
1010
      !(j->ref.cond_guards= (bool**) session->alloc(sizeof(uint*)*keyparts)))
1016
1011
  {
1017
1012
    return(true);
1018
1013
  }
1221
1216
*/
1222
1217
void JoinTable::cleanup()
1223
1218
{
1224
 
  safe_delete(select);
1225
 
  safe_delete(quick);
1226
 
 
 
1219
  delete select;
 
1220
  select= 0;
 
1221
  delete quick;
 
1222
  quick= 0;
1227
1223
  if (cache.buff)
1228
1224
  {
1229
1225
    size_t size= cache.end - cache.buff;
1352
1348
  bool in_upper_level= false;
1353
1349
  while (cond_equal)
1354
1350
  {
1355
 
    List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
1351
    List_iterator_fast<Item_equal> li(cond_equal->current_level);
1356
1352
    while ((item= li++))
1357
1353
    {
1358
1354
      if (item->contains(field))
1517
1513
        /* Merge two multiple equalities forming a new one */
1518
1514
        left_item_equal->merge(right_item_equal);
1519
1515
        /* Remove the merged multiple equality from the list */
1520
 
        List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
1516
        List_iterator<Item_equal> li(cond_equal->current_level);
1521
1517
        while ((li++) != right_item_equal) {};
1522
1518
        li.remove();
1523
1519
      }
1657
1653
                                       (Item_row *) right_item,
1658
1654
                                       cond_equal, eq_list);
1659
1655
      if (!is_converted)
1660
 
        session->getLex()->current_select->cond_count++;
 
1656
        session->lex->current_select->cond_count++;
1661
1657
    }
1662
1658
    else
1663
1659
    {
1664
1660
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
1665
 
      session->getLex()->current_select->cond_count++;
 
1661
      session->lex->current_select->cond_count++;
1666
1662
    }
1667
1663
 
1668
1664
    if (!is_converted)
1718
1714
    if (left_item->type() == Item::ROW_ITEM &&
1719
1715
        right_item->type() == Item::ROW_ITEM)
1720
1716
    {
1721
 
      session->getLex()->current_select->cond_count--;
 
1717
      session->lex->current_select->cond_count--;
1722
1718
      return check_row_equality(session,
1723
1719
                                (Item_row *) left_item,
1724
1720
                                (Item_row *) right_item,
1749
1745
    just an argument of a comparison predicate.
1750
1746
    The function also determines the maximum number of members in
1751
1747
    equality lists of each Item_cond_and object assigning it to
1752
 
    session->getLex()->current_select->max_equal_elems.
 
1748
    session->lex->current_select->max_equal_elems.
1753
1749
 
1754
1750
  @note
1755
1751
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
1806
1802
      Item_func::COND_AND_FUNC;
1807
1803
    List<Item> *args= ((Item_cond*) cond)->argument_list();
1808
1804
 
1809
 
    List<Item>::iterator li(args->begin());
 
1805
    List_iterator<Item> li(*args);
1810
1806
    Item *item;
1811
1807
 
1812
1808
    if (and_level)
1828
1824
          li.remove();
1829
1825
      }
1830
1826
 
1831
 
      List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
1827
      List_iterator_fast<Item_equal> it(cond_equal.current_level);
1832
1828
      while ((item_equal= it++))
1833
1829
      {
1834
1830
        item_equal->fix_length_and_dec();
1835
1831
        item_equal->update_used_tables();
1836
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
 
1832
        set_if_bigger(session->lex->current_select->max_equal_elems,
1837
1833
                      item_equal->members());
1838
1834
      }
1839
1835
 
1844
1840
       Make replacement of equality predicates for lower levels
1845
1841
       of the condition expression.
1846
1842
    */
1847
 
    li= args->begin();
 
1843
    li.rewind();
1848
1844
    while ((item= li++))
1849
1845
    {
1850
1846
      Item *new_item;
1892
1888
        }
1893
1889
        else
1894
1890
          item_equal= (Item_equal *) eq_list.pop();
1895
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
 
1891
        set_if_bigger(session->lex->current_select->max_equal_elems,
1896
1892
                      item_equal->members());
1897
1893
        return item_equal;
1898
1894
      }
1905
1901
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
1906
1902
        and_cond->quick_fix_field();
1907
1903
        List<Item> *args= and_cond->argument_list();
1908
 
        List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
1904
        List_iterator_fast<Item_equal> it(cond_equal.current_level);
1909
1905
        while ((item_equal= it++))
1910
1906
        {
1911
1907
          item_equal->fix_length_and_dec();
1912
1908
          item_equal->update_used_tables();
1913
 
          set_if_bigger(session->getLex()->current_select->max_equal_elems,
 
1909
          set_if_bigger(session->lex->current_select->max_equal_elems,
1914
1910
                        item_equal->members());
1915
1911
        }
1916
1912
        and_cond->cond_equal= cond_equal;
2031
2027
  if (join_list)
2032
2028
  {
2033
2029
    TableList *table;
2034
 
    List<TableList>::iterator li(join_list->begin());
 
2030
    List_iterator<TableList> li(*join_list);
2035
2031
 
2036
2032
    while ((table= li++))
2037
2033
    {
2246
2242
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
2247
2243
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
2248
2244
 
2249
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
2245
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
2250
2246
      while ((item_equal= it++))
2251
2247
      {
2252
2248
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2253
2249
      }
2254
2250
    }
2255
2251
 
2256
 
    List<Item>::iterator li(cond_list->begin());
 
2252
    List_iterator<Item> li(*cond_list);
2257
2253
    Item *item;
2258
2254
    while ((item= li++))
2259
2255
    {
2269
2265
 
2270
2266
    if (and_level)
2271
2267
    {
2272
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
2268
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
2273
2269
      while ((item_equal= it++))
2274
2270
      {
2275
2271
        cond= eliminate_item_equal(cond, cond_equal->upper_levels, item_equal);
2311
2307
  @param cond       condition whose multiple equalities are to be checked
2312
2308
  @param table      constant table that has been read
2313
2309
*/
2314
 
void update_const_equal_items(COND *cond, JoinTable *tab)
 
2310
static void update_const_equal_items(COND *cond, JoinTable *tab)
2315
2311
{
2316
2312
  if (!(cond->used_tables() & tab->table->map))
2317
2313
    return;
2319
2315
  if (cond->type() == Item::COND_ITEM)
2320
2316
  {
2321
2317
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
2322
 
    List<Item>::iterator li(cond_list->begin());
 
2318
    List_iterator_fast<Item> li(*cond_list);
2323
2319
    Item *item;
2324
2320
    while ((item= li++))
2325
2321
      update_const_equal_items(item, tab);
2377
2373
  if (cond->type() == Item::COND_ITEM)
2378
2374
  {
2379
2375
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2380
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
2376
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2381
2377
    Item *item;
2382
2378
    while ((item=li++))
2383
2379
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
2457
2453
  if (conds->type() == Item::COND_ITEM)
2458
2454
  {
2459
2455
    Item_cond *cnd= (Item_cond*) conds;
2460
 
    List<Item>::iterator li(cnd->argument_list()->begin());
 
2456
    List_iterator<Item> li(*(cnd->argument_list()));
2461
2457
    Item *item;
2462
2458
    while ((item= li++))
2463
2459
    {
2481
2477
  if (cond->type() == Item::COND_ITEM)
2482
2478
  {
2483
2479
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2484
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
2480
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2485
2481
    Item *item;
2486
2482
    list<COND_CMP> save;
2487
2483
    while ((item=li++))
2610
2606
         position:
2611
2607
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
2612
2608
             joins) we've opened but didn't close.
2613
 
          2. {each NestedJoin class not simplified away}->counter - number
 
2609
          2. {each nested_join_st structure not simplified away}->counter - number
2614
2610
             of this nested join's children that have already been added to to
2615
2611
             the partial join order.
2616
2612
  @endverbatim
2715
2711
  {
2716
2712
    bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
2717
2713
 
2718
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
2714
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2719
2715
    Item::cond_result tmp_cond_value;
2720
2716
    bool should_fix_fields= false;
2721
2717
 
2769
2765
    {                                           
2770
2766
      /* Argument list contains only one element, so reduce it so a single item, then remove list */
2771
2767
      item= ((Item_cond*) cond)->argument_list()->head();
2772
 
      ((Item_cond*) cond)->argument_list()->clear();
 
2768
      ((Item_cond*) cond)->argument_list()->empty();
2773
2769
      return item;
2774
2770
    }
2775
2771
  }
2847
2843
  }
2848
2844
  else if (cond->const_item() && !cond->is_expensive())
2849
2845
  /*
2850
 
    @todo
 
2846
    TODO:
2851
2847
    Excluding all expensive functions is too restritive we should exclude only
2852
2848
    materialized IN subquery predicates because they can't yet be evaluated
2853
2849
    here (they need additional initialization that is done later on).
2919
2915
  {
2920
2916
    bool and_level= (((Item_cond*) cond)->functype()
2921
2917
                     == Item_func::COND_AND_FUNC);
2922
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
2918
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2923
2919
    Item *item;
2924
2920
    while ((item=li++))
2925
2921
    {
3371
3367
  return 0;
3372
3368
}
3373
3369
 
 
3370
int join_read_const_table(JoinTable *tab, optimizer::Position *pos)
 
3371
{
 
3372
  int error;
 
3373
  Table *table=tab->table;
 
3374
  table->const_table=1;
 
3375
  table->null_row=0;
 
3376
  table->status=STATUS_NO_RECORD;
 
3377
 
 
3378
  if (tab->type == AM_SYSTEM)
 
3379
  {
 
3380
    if ((error=join_read_system(tab)))
 
3381
    {                                           // Info for DESCRIBE
 
3382
      tab->info="const row not found";
 
3383
      /* Mark for EXPLAIN that the row was not found */
 
3384
      pos->setFanout(0.0);
 
3385
      pos->clearRefDependMap();
 
3386
      if (! table->maybe_null || error > 0)
 
3387
        return(error);
 
3388
    }
 
3389
  }
 
3390
  else
 
3391
  {
 
3392
    if (! table->key_read && 
 
3393
        table->covering_keys.test(tab->ref.key) && 
 
3394
        ! table->no_keyread &&
 
3395
        (int) table->reginfo.lock_type <= (int) TL_READ_WITH_SHARED_LOCKS)
 
3396
    {
 
3397
      table->key_read=1;
 
3398
      table->cursor->extra(HA_EXTRA_KEYREAD);
 
3399
      tab->index= tab->ref.key;
 
3400
    }
 
3401
    error=join_read_const(tab);
 
3402
    if (table->key_read)
 
3403
    {
 
3404
      table->key_read=0;
 
3405
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
3406
    }
 
3407
    if (error)
 
3408
    {
 
3409
      tab->info="unique row not found";
 
3410
      /* Mark for EXPLAIN that the row was not found */
 
3411
      pos->setFanout(0.0);
 
3412
      pos->clearRefDependMap();
 
3413
      if (!table->maybe_null || error > 0)
 
3414
        return(error);
 
3415
    }
 
3416
  }
 
3417
  if (*tab->on_expr_ref && !table->null_row)
 
3418
  {
 
3419
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
 
3420
      table->mark_as_null_row();
 
3421
  }
 
3422
  if (!table->null_row)
 
3423
    table->maybe_null=0;
 
3424
 
 
3425
  /* Check appearance of new constant items in Item_equal objects */
 
3426
  Join *join= tab->join;
 
3427
  if (join->conds)
 
3428
    update_const_equal_items(join->conds, tab);
 
3429
  TableList *tbl;
 
3430
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
 
3431
  {
 
3432
    TableList *embedded;
 
3433
    TableList *embedding= tbl;
 
3434
    do
 
3435
    {
 
3436
      embedded= embedding;
 
3437
      if (embedded->on_expr)
 
3438
         update_const_equal_items(embedded->on_expr, tab);
 
3439
      embedding= embedded->getEmbedding();
 
3440
    }
 
3441
    while (embedding &&
 
3442
           embedding->getNestedJoin()->join_list.head() == embedded);
 
3443
  }
 
3444
 
 
3445
  return(0);
 
3446
}
 
3447
 
 
3448
int join_read_system(JoinTable *tab)
 
3449
{
 
3450
  Table *table= tab->table;
 
3451
  int error;
 
3452
  if (table->status & STATUS_GARBAGE)           // If first read
 
3453
  {
 
3454
    if ((error=table->cursor->read_first_row(table->getInsertRecord(),
 
3455
                                           table->getShare()->getPrimaryKey())))
 
3456
    {
 
3457
      if (error != HA_ERR_END_OF_FILE)
 
3458
        return table->report_error(error);
 
3459
      tab->table->mark_as_null_row();
 
3460
      table->emptyRecord();                     // Make empty record
 
3461
      return -1;
 
3462
    }
 
3463
    table->storeRecord();
 
3464
  }
 
3465
  else if (!table->status)                      // Only happens with left join
 
3466
    table->restoreRecord();                     // restore old record
 
3467
  table->null_row=0;
 
3468
  return table->status ? -1 : 0;
 
3469
}
 
3470
 
3374
3471
/**
3375
3472
  Read a (constant) table when there is at most one matching row.
3376
3473
 
3449
3546
    }
3450
3547
  }
3451
3548
 
3452
 
  /* @todo Why don't we do "Late NULLs Filtering" here? */
 
3549
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
3453
3550
  if (cmp_buffer_with_ref(tab) ||
3454
3551
      (table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW)))
3455
3552
  {
3640
3737
 
3641
3738
int test_if_quick_select(JoinTable *tab)
3642
3739
{
3643
 
  safe_delete(tab->select->quick);
3644
 
 
 
3740
  delete tab->select->quick;
 
3741
  tab->select->quick= 0;
3645
3742
  return tab->select->test_quick_select(tab->join->session, tab->keys,
3646
3743
                                        (table_map) 0, HA_POS_ERROR, 0, false);
3647
3744
}
3819
3916
        {
3820
3917
          if (!join->first_record)
3821
3918
          {
3822
 
                  List<Item>::iterator it(join->fields->begin());
 
3919
                  List_iterator_fast<Item> it(*join->fields);
3823
3920
                  Item *item;
3824
3921
            /* No matching rows for group function */
3825
3922
            join->clear();
4058
4155
      Item_cond_and *new_cond=new Item_cond_and;
4059
4156
      if (!new_cond)
4060
4157
        return (COND*) 0;
4061
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
4158
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4062
4159
      Item *item;
4063
4160
      while ((item=li++))
4064
4161
      {
4088
4185
      Item_cond_or *new_cond=new Item_cond_or;
4089
4186
      if (!new_cond)
4090
4187
        return (COND*) 0;
4091
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
4188
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4092
4189
      Item *item;
4093
4190
      while ((item=li++))
4094
4191
      {
4440
4537
{
4441
4538
  List<Item> *fields= (List<Item> *) data;
4442
4539
  bool part_found= 0;
4443
 
  List<Item>::iterator li(fields->begin());
 
4540
  List_iterator<Item> li(*fields);
4444
4541
  Item *item;
4445
4542
 
4446
4543
  while ((item= li++))
4525
4622
    save_quick= select->quick;
4526
4623
    /*
4527
4624
      assume results are not ordered when index merge is used
4528
 
      @todo sergeyp: Results of all index merge selects actually are ordered
 
4625
      TODO: sergeyp: Results of all index merge selects actually are ordered
4529
4626
      by clustered PK values.
4530
4627
    */
4531
4628
 
4800
4897
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
4801
4898
          if (select && select->quick)
4802
4899
          {
4803
 
            safe_delete(select->quick);
 
4900
            delete select->quick;
 
4901
            select->quick= 0;
4804
4902
          }
4805
4903
          if (table->covering_keys.test(best_key))
4806
4904
          {
4873
4971
          tab->limit= 0;
4874
4972
          return 0; // Reverse sort not supported
4875
4973
        }
4876
 
        select->quick= tmp;
 
4974
        select->quick=tmp;
4877
4975
      }
4878
4976
    }
4879
4977
    else if (tab->type != AM_NEXT &&
5569
5667
    Item *item;
5570
5668
    Item_field *field;
5571
5669
    int cur_pos_in_select_list= 0;
5572
 
    List<Item>::iterator li(fields.begin());
5573
 
    List<Item_field>::iterator naf_it(session->getLex()->current_select->non_agg_fields.begin());
 
5670
    List_iterator<Item> li(fields);
 
5671
    List_iterator<Item_field> naf_it(session->lex->current_select->non_agg_fields);
5574
5672
 
5575
5673
    field= naf_it++;
5576
5674
    while (field && (item=li++))
5596
5694
            if ((*ord->item)->eq((Item*)field, 0))
5597
5695
              goto next_field;
5598
5696
          /*
5599
 
            @todo change ER_WRONG_FIELD_WITH_GROUP to more detailed ER_NON_GROUPING_FIELD_USED
 
5697
            TODO: change ER_WRONG_FIELD_WITH_GROUP to more detailed
 
5698
            ER_NON_GROUPING_FIELD_USED
5600
5699
          */
5601
5700
          my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), field->full_name());
5602
5701
          return 1;
5625
5724
                                List<Item> &,
5626
5725
                                bool *all_order_by_fields_used)
5627
5726
{
5628
 
  List<Item>::iterator li(fields.begin());
 
5727
  List_iterator<Item> li(fields);
5629
5728
  Item *item;
5630
5729
  Order *order,*group,**prev;
5631
5730
 
5638
5737
  {
5639
5738
    if (order->in_field_list)
5640
5739
    {
5641
 
      Order *ord=(Order*) session->getMemRoot()->duplicate((char*) order,sizeof(Order));
 
5740
      Order *ord=(Order*) session->memdup((char*) order,sizeof(Order));
5642
5741
      if (!ord)
5643
5742
        return 0;
5644
5743
      *prev=ord;
5649
5748
      *all_order_by_fields_used= 0;
5650
5749
  }
5651
5750
 
5652
 
  li= fields.begin();
 
5751
  li.rewind();
5653
5752
  while ((item=li++))
5654
5753
  {
5655
5754
    if (!item->const_item() && !item->with_sum_func && !item->marker)
5689
5788
*/
5690
5789
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
5691
5790
{
5692
 
  List<Item>::iterator li(fields.begin());
 
5791
  List_iterator<Item> li(fields);
5693
5792
  Item *field;
5694
5793
 
5695
5794
  param->field_count=param->sum_func_count=param->func_count=
5746
5845
*/
5747
5846
int test_if_item_cache_changed(List<Cached_item> &list)
5748
5847
{
5749
 
  List<Cached_item>::iterator li(list.begin());
 
5848
  List_iterator<Cached_item> li(list);
5750
5849
  int idx= -1,i;
5751
5850
  Cached_item *buff;
5752
5851
 
5795
5894
                       List<Item> &all_fields)
5796
5895
{
5797
5896
  Item *pos;
5798
 
  List<Item>::iterator li(all_fields.begin());
 
5897
  List_iterator_fast<Item> li(all_fields);
5799
5898
  CopyField *copy= NULL;
5800
 
  res_selected_fields.clear();
5801
 
  res_all_fields.clear();
5802
 
  List<Item>::iterator itr(res_all_fields.begin());
 
5899
  res_selected_fields.empty();
 
5900
  res_all_fields.empty();
 
5901
  List_iterator_fast<Item> itr(res_all_fields);
5803
5902
  List<Item> extra_funcs;
5804
5903
  uint32_t i, border= all_fields.elements - elements;
5805
5904
 
5807
5906
      !(copy=param->copy_field= new CopyField[param->field_count]))
5808
5907
    goto err2;
5809
5908
 
5810
 
  param->copy_funcs.clear();
 
5909
  param->copy_funcs.empty();
5811
5910
  for (i= 0; (pos= li++); i++)
5812
5911
  {
5813
5912
    Field *field;
5876
5975
             !real_pos->with_sum_func)
5877
5976
    {                                           // Save for send fields
5878
5977
      pos= real_pos;
5879
 
      /* 
5880
 
        @todo In most cases this result will be sent to the user.
 
5978
      /* TODO:
 
5979
        In most cases this result will be sent to the user.
5881
5980
        This should be changed to use copy_int or copy_real depending
5882
5981
        on how the value is to be used: In some cases this may be an
5883
5982
        argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
5931
6030
  for (; ptr != end; ptr++)
5932
6031
    (*ptr->do_copy)(ptr);
5933
6032
 
5934
 
  List<Item>::iterator it(param->copy_funcs.begin());
 
6033
  List_iterator_fast<Item> it(param->copy_funcs);
5935
6034
  Item_copy_string *item;
5936
6035
  while ((item = (Item_copy_string*) it++))
5937
6036
    item->copy();
5960
6059
                                                uint32_t elements,
5961
6060
                              List<Item> &all_fields)
5962
6061
{
5963
 
  List<Item>::iterator it(all_fields.begin());
 
6062
  List_iterator_fast<Item> it(all_fields);
5964
6063
  Item *item_field,*item;
5965
6064
 
5966
 
  res_selected_fields.clear();
5967
 
  res_all_fields.clear();
 
6065
  res_selected_fields.empty();
 
6066
  res_all_fields.empty();
5968
6067
 
5969
6068
  uint32_t i, border= all_fields.elements - elements;
5970
6069
  for (i= 0; (item= it++); i++)
6009
6108
      item_field;
6010
6109
  }
6011
6110
 
6012
 
  List<Item>::iterator itr(res_all_fields.begin());
 
6111
  List_iterator_fast<Item> itr(res_all_fields);
6013
6112
  for (i= 0; i < border; i++)
6014
6113
    itr++;
6015
6114
  itr.sublist(res_selected_fields, elements);
6039
6138
                               uint32_t elements,
6040
6139
                                                 List<Item> &all_fields)
6041
6140
{
6042
 
  List<Item>::iterator it(all_fields.begin());
 
6141
  List_iterator_fast<Item> it(all_fields);
6043
6142
  Item *item, *new_item;
6044
 
  res_selected_fields.clear();
6045
 
  res_all_fields.clear();
 
6143
  res_selected_fields.empty();
 
6144
  res_all_fields.empty();
6046
6145
 
6047
6146
  uint32_t i, border= all_fields.elements - elements;
6048
6147
  for (i= 0; (item= it++); i++)
6052
6151
      new_item;
6053
6152
  }
6054
6153
 
6055
 
  List<Item>::iterator itr(res_all_fields.begin());
 
6154
  List_iterator_fast<Item> itr(res_all_fields);
6056
6155
  for (i= 0; i < border; i++)
6057
6156
    itr++;
6058
6157
  itr.sublist(res_selected_fields, elements);
6144
6243
    /*
6145
6244
      Need to check the THD error state because Item::val_xxx() don't
6146
6245
      return error code, but can generate errors
6147
 
      @todo change it for a real status check when Item::val_xxx()
 
6246
      TODO: change it for a real status check when Item::val_xxx()
6148
6247
      are extended to return status code.
6149
6248
    */
6150
6249
    if (session->is_error())
6202
6301
  @param changed        out:  returns 1 if item contains a replaced field item
6203
6302
 
6204
6303
  @todo
6205
 
    - @todo Some functions are not null-preserving. For those functions
 
6304
    - TODO: Some functions are not null-preserving. For those functions
6206
6305
    updating of the maybe_null attribute is an overkill.
6207
6306
 
6208
6307
  @retval
6214
6313
{
6215
6314
  if (expr->arg_count)
6216
6315
  {
6217
 
    Name_resolution_context *context= &session->getLex()->current_select->context;
 
6316
    Name_resolution_context *context= &session->lex->current_select->context;
6218
6317
    Item **arg,**arg_end;
6219
6318
    bool arg_changed= false;
6220
6319
    for (arg= expr->arguments(),
6292
6391
                List<TableList> *tables, enum_query_type)
6293
6392
{
6294
6393
  /* List is reversed => we should reverse it before using */
6295
 
  List<TableList>::iterator ti(tables->begin());
6296
 
  TableList **table= (TableList **)session->getMemRoot()->allocate(sizeof(TableList*) *
 
6394
  List_iterator_fast<TableList> ti(*tables);
 
6395
  TableList **table= (TableList **)session->alloc(sizeof(TableList*) *
6297
6396
                                                tables->elements);
6298
6397
  if (table == 0)
6299
6398
    return;  // out of memory
6329
6428
 
6330
6429
  //Item List
6331
6430
  bool first= 1;
6332
 
  List<Item>::iterator it(item_list.begin());
 
6431
  List_iterator_fast<Item> it(item_list);
6333
6432
  Item *item;
6334
6433
  while ((item= it++))
6335
6434
  {
6342
6441
 
6343
6442
  /*
6344
6443
    from clause
6345
 
    @todo support USING/FORCE/IGNORE index
 
6444
    TODO: support USING/FORCE/IGNORE index
6346
6445
  */
6347
6446
  if (table_list.elements)
6348
6447
  {