~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Brian Aker
  • Date: 2011-02-08 02:06:04 UTC
  • mto: (2152.2.1 alter-table)
  • mto: This revision was merged to the branch mainline in revision 2154.
  • Revision ID: brian@tangent.org-20110208020604-p23mwd9yvamuwbwu
Merge in column varchar build.

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);
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++))
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
 
 
3371
int join_read_system(JoinTable *tab)
 
3372
{
 
3373
  Table *table= tab->table;
 
3374
  int error;
 
3375
  if (table->status & STATUS_GARBAGE)           // If first read
 
3376
  {
 
3377
    if ((error=table->cursor->read_first_row(table->getInsertRecord(),
 
3378
                                           table->getShare()->getPrimaryKey())))
 
3379
    {
 
3380
      if (error != HA_ERR_END_OF_FILE)
 
3381
        return table->report_error(error);
 
3382
      tab->table->mark_as_null_row();
 
3383
      table->emptyRecord();                     // Make empty record
 
3384
      return -1;
 
3385
    }
 
3386
    table->storeRecord();
 
3387
  }
 
3388
  else if (!table->status)                      // Only happens with left join
 
3389
    table->restoreRecord();                     // restore old record
 
3390
  table->null_row=0;
 
3391
  return table->status ? -1 : 0;
 
3392
}
 
3393
 
3374
3394
/**
3375
3395
  Read a (constant) table when there is at most one matching row.
3376
3396
 
3449
3469
    }
3450
3470
  }
3451
3471
 
3452
 
  /* @todo Why don't we do "Late NULLs Filtering" here? */
 
3472
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
3453
3473
  if (cmp_buffer_with_ref(tab) ||
3454
3474
      (table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW)))
3455
3475
  {
3640
3660
 
3641
3661
int test_if_quick_select(JoinTable *tab)
3642
3662
{
3643
 
  safe_delete(tab->select->quick);
3644
 
 
 
3663
  delete tab->select->quick;
 
3664
  tab->select->quick= 0;
3645
3665
  return tab->select->test_quick_select(tab->join->session, tab->keys,
3646
3666
                                        (table_map) 0, HA_POS_ERROR, 0, false);
3647
3667
}
3819
3839
        {
3820
3840
          if (!join->first_record)
3821
3841
          {
3822
 
                  List<Item>::iterator it(join->fields->begin());
 
3842
                  List_iterator_fast<Item> it(*join->fields);
3823
3843
                  Item *item;
3824
3844
            /* No matching rows for group function */
3825
3845
            join->clear();
4058
4078
      Item_cond_and *new_cond=new Item_cond_and;
4059
4079
      if (!new_cond)
4060
4080
        return (COND*) 0;
4061
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
4081
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4062
4082
      Item *item;
4063
4083
      while ((item=li++))
4064
4084
      {
4088
4108
      Item_cond_or *new_cond=new Item_cond_or;
4089
4109
      if (!new_cond)
4090
4110
        return (COND*) 0;
4091
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
4111
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4092
4112
      Item *item;
4093
4113
      while ((item=li++))
4094
4114
      {
4440
4460
{
4441
4461
  List<Item> *fields= (List<Item> *) data;
4442
4462
  bool part_found= 0;
4443
 
  List<Item>::iterator li(fields->begin());
 
4463
  List_iterator<Item> li(*fields);
4444
4464
  Item *item;
4445
4465
 
4446
4466
  while ((item= li++))
4525
4545
    save_quick= select->quick;
4526
4546
    /*
4527
4547
      assume results are not ordered when index merge is used
4528
 
      @todo sergeyp: Results of all index merge selects actually are ordered
 
4548
      TODO: sergeyp: Results of all index merge selects actually are ordered
4529
4549
      by clustered PK values.
4530
4550
    */
4531
4551
 
4800
4820
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
4801
4821
          if (select && select->quick)
4802
4822
          {
4803
 
            safe_delete(select->quick);
 
4823
            delete select->quick;
 
4824
            select->quick= 0;
4804
4825
          }
4805
4826
          if (table->covering_keys.test(best_key))
4806
4827
          {
4873
4894
          tab->limit= 0;
4874
4895
          return 0; // Reverse sort not supported
4875
4896
        }
4876
 
        select->quick= tmp;
 
4897
        select->quick=tmp;
4877
4898
      }
4878
4899
    }
4879
4900
    else if (tab->type != AM_NEXT &&
5569
5590
    Item *item;
5570
5591
    Item_field *field;
5571
5592
    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());
 
5593
    List_iterator<Item> li(fields);
 
5594
    List_iterator<Item_field> naf_it(session->lex->current_select->non_agg_fields);
5574
5595
 
5575
5596
    field= naf_it++;
5576
5597
    while (field && (item=li++))
5596
5617
            if ((*ord->item)->eq((Item*)field, 0))
5597
5618
              goto next_field;
5598
5619
          /*
5599
 
            @todo change ER_WRONG_FIELD_WITH_GROUP to more detailed ER_NON_GROUPING_FIELD_USED
 
5620
            TODO: change ER_WRONG_FIELD_WITH_GROUP to more detailed
 
5621
            ER_NON_GROUPING_FIELD_USED
5600
5622
          */
5601
5623
          my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), field->full_name());
5602
5624
          return 1;
5625
5647
                                List<Item> &,
5626
5648
                                bool *all_order_by_fields_used)
5627
5649
{
5628
 
  List<Item>::iterator li(fields.begin());
 
5650
  List_iterator<Item> li(fields);
5629
5651
  Item *item;
5630
5652
  Order *order,*group,**prev;
5631
5653
 
5638
5660
  {
5639
5661
    if (order->in_field_list)
5640
5662
    {
5641
 
      Order *ord=(Order*) session->getMemRoot()->duplicate((char*) order,sizeof(Order));
 
5663
      Order *ord=(Order*) session->memdup((char*) order,sizeof(Order));
5642
5664
      if (!ord)
5643
5665
        return 0;
5644
5666
      *prev=ord;
5649
5671
      *all_order_by_fields_used= 0;
5650
5672
  }
5651
5673
 
5652
 
  li= fields.begin();
 
5674
  li.rewind();
5653
5675
  while ((item=li++))
5654
5676
  {
5655
5677
    if (!item->const_item() && !item->with_sum_func && !item->marker)
5689
5711
*/
5690
5712
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
5691
5713
{
5692
 
  List<Item>::iterator li(fields.begin());
 
5714
  List_iterator<Item> li(fields);
5693
5715
  Item *field;
5694
5716
 
5695
5717
  param->field_count=param->sum_func_count=param->func_count=
5746
5768
*/
5747
5769
int test_if_item_cache_changed(List<Cached_item> &list)
5748
5770
{
5749
 
  List<Cached_item>::iterator li(list.begin());
 
5771
  List_iterator<Cached_item> li(list);
5750
5772
  int idx= -1,i;
5751
5773
  Cached_item *buff;
5752
5774
 
5795
5817
                       List<Item> &all_fields)
5796
5818
{
5797
5819
  Item *pos;
5798
 
  List<Item>::iterator li(all_fields.begin());
 
5820
  List_iterator_fast<Item> li(all_fields);
5799
5821
  CopyField *copy= NULL;
5800
 
  res_selected_fields.clear();
5801
 
  res_all_fields.clear();
5802
 
  List<Item>::iterator itr(res_all_fields.begin());
 
5822
  res_selected_fields.empty();
 
5823
  res_all_fields.empty();
 
5824
  List_iterator_fast<Item> itr(res_all_fields);
5803
5825
  List<Item> extra_funcs;
5804
5826
  uint32_t i, border= all_fields.elements - elements;
5805
5827
 
5807
5829
      !(copy=param->copy_field= new CopyField[param->field_count]))
5808
5830
    goto err2;
5809
5831
 
5810
 
  param->copy_funcs.clear();
 
5832
  param->copy_funcs.empty();
5811
5833
  for (i= 0; (pos= li++); i++)
5812
5834
  {
5813
5835
    Field *field;
5876
5898
             !real_pos->with_sum_func)
5877
5899
    {                                           // Save for send fields
5878
5900
      pos= real_pos;
5879
 
      /* 
5880
 
        @todo In most cases this result will be sent to the user.
 
5901
      /* TODO:
 
5902
        In most cases this result will be sent to the user.
5881
5903
        This should be changed to use copy_int or copy_real depending
5882
5904
        on how the value is to be used: In some cases this may be an
5883
5905
        argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
5931
5953
  for (; ptr != end; ptr++)
5932
5954
    (*ptr->do_copy)(ptr);
5933
5955
 
5934
 
  List<Item>::iterator it(param->copy_funcs.begin());
 
5956
  List_iterator_fast<Item> it(param->copy_funcs);
5935
5957
  Item_copy_string *item;
5936
5958
  while ((item = (Item_copy_string*) it++))
5937
5959
    item->copy();
5960
5982
                                                uint32_t elements,
5961
5983
                              List<Item> &all_fields)
5962
5984
{
5963
 
  List<Item>::iterator it(all_fields.begin());
 
5985
  List_iterator_fast<Item> it(all_fields);
5964
5986
  Item *item_field,*item;
5965
5987
 
5966
 
  res_selected_fields.clear();
5967
 
  res_all_fields.clear();
 
5988
  res_selected_fields.empty();
 
5989
  res_all_fields.empty();
5968
5990
 
5969
5991
  uint32_t i, border= all_fields.elements - elements;
5970
5992
  for (i= 0; (item= it++); i++)
6009
6031
      item_field;
6010
6032
  }
6011
6033
 
6012
 
  List<Item>::iterator itr(res_all_fields.begin());
 
6034
  List_iterator_fast<Item> itr(res_all_fields);
6013
6035
  for (i= 0; i < border; i++)
6014
6036
    itr++;
6015
6037
  itr.sublist(res_selected_fields, elements);
6039
6061
                               uint32_t elements,
6040
6062
                                                 List<Item> &all_fields)
6041
6063
{
6042
 
  List<Item>::iterator it(all_fields.begin());
 
6064
  List_iterator_fast<Item> it(all_fields);
6043
6065
  Item *item, *new_item;
6044
 
  res_selected_fields.clear();
6045
 
  res_all_fields.clear();
 
6066
  res_selected_fields.empty();
 
6067
  res_all_fields.empty();
6046
6068
 
6047
6069
  uint32_t i, border= all_fields.elements - elements;
6048
6070
  for (i= 0; (item= it++); i++)
6052
6074
      new_item;
6053
6075
  }
6054
6076
 
6055
 
  List<Item>::iterator itr(res_all_fields.begin());
 
6077
  List_iterator_fast<Item> itr(res_all_fields);
6056
6078
  for (i= 0; i < border; i++)
6057
6079
    itr++;
6058
6080
  itr.sublist(res_selected_fields, elements);
6144
6166
    /*
6145
6167
      Need to check the THD error state because Item::val_xxx() don't
6146
6168
      return error code, but can generate errors
6147
 
      @todo change it for a real status check when Item::val_xxx()
 
6169
      TODO: change it for a real status check when Item::val_xxx()
6148
6170
      are extended to return status code.
6149
6171
    */
6150
6172
    if (session->is_error())
6202
6224
  @param changed        out:  returns 1 if item contains a replaced field item
6203
6225
 
6204
6226
  @todo
6205
 
    - @todo Some functions are not null-preserving. For those functions
 
6227
    - TODO: Some functions are not null-preserving. For those functions
6206
6228
    updating of the maybe_null attribute is an overkill.
6207
6229
 
6208
6230
  @retval
6214
6236
{
6215
6237
  if (expr->arg_count)
6216
6238
  {
6217
 
    Name_resolution_context *context= &session->getLex()->current_select->context;
 
6239
    Name_resolution_context *context= &session->lex->current_select->context;
6218
6240
    Item **arg,**arg_end;
6219
6241
    bool arg_changed= false;
6220
6242
    for (arg= expr->arguments(),
6292
6314
                List<TableList> *tables, enum_query_type)
6293
6315
{
6294
6316
  /* 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*) *
 
6317
  List_iterator_fast<TableList> ti(*tables);
 
6318
  TableList **table= (TableList **)session->alloc(sizeof(TableList*) *
6297
6319
                                                tables->elements);
6298
6320
  if (table == 0)
6299
6321
    return;  // out of memory
6329
6351
 
6330
6352
  //Item List
6331
6353
  bool first= 1;
6332
 
  List<Item>::iterator it(item_list.begin());
 
6354
  List_iterator_fast<Item> it(item_list);
6333
6355
  Item *item;
6334
6356
  while ((item= it++))
6335
6357
  {
6342
6364
 
6343
6365
  /*
6344
6366
    from clause
6345
 
    @todo support USING/FORCE/IGNORE index
 
6367
    TODO: support USING/FORCE/IGNORE index
6346
6368
  */
6347
6369
  if (table_list.elements)
6348
6370
  {