~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Stewart Smith
  • Date: 2010-08-12 16:48:46 UTC
  • mto: This revision was merged to the branch mainline in revision 1707.
  • Revision ID: stewart@flamingspork.com-20100812164846-s9bhy47g60bvqs41
bug lp:611379 Equivalent queries with Impossible where return different results

The following two equivalent queries return different results in maria 5.2 and 5.3 (and identical results in mysql 5.5.5) :

SELECT SUM( DISTINCT table1 .`pk` ) FROM B table1 STRAIGHT_JOIN ( BB table2 JOIN CC ON table2 .`col_varchar_key` ) ON table2 .`pk` ;

SELECT * FROM ( SELECT SUM( DISTINCT table1 .`pk` ) FROM B table1 STRAIGHT_JOIN ( BB table2 JOIN CC ON table2 .`col_varchar_key` ) ON table2 .`pk` );

MariaDB returns 0 on the second query and NULL on the first, whereas MySQL returns NULL on both. In MariaDB, both EXPLAIN plans agree that "Impossible WHERE noticed after reading const tables"



We have some slightly different output in drizzle:

main.bug_lp611379 [ fail ]
drizzletest: At line 9: query 'explain select * from (select sum(distinct t1.a) from t1,t2 where t1.a=t2.a)
as t' failed: 1048: Column 'sum(distinct t1.a)' cannot be null

but the fix gets us the correct query results, although with slightly different execution plans.



This fix is directly ported from MariaDB.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/**
17
17
  @file
18
18
 
19
19
  @brief
20
 
  select_query and join optimization
 
20
  mysql_select and join optimization
21
21
 
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
 
 
55
#include "drizzled/sql_union.h"
 
56
#include "drizzled/optimizer/key_field.h"
 
57
#include "drizzled/optimizer/position.h"
 
58
#include "drizzled/optimizer/sargable_param.h"
 
59
#include "drizzled/optimizer/key_use.h"
 
60
#include "drizzled/optimizer/range.h"
 
61
#include "drizzled/optimizer/quick_range_select.h"
 
62
#include "drizzled/optimizer/quick_ror_intersect_select.h"
71
63
 
72
64
using namespace std;
73
65
 
83
75
static Item* part_of_refkey(Table *form,Field *field);
84
76
static bool cmp_buffer_with_ref(JoinTable *tab);
85
77
static void change_cond_ref_to_const(Session *session,
86
 
                                     list<COND_CMP>& save_list,
 
78
                                     vector<COND_CMP>& save_list,
87
79
                                     Item *and_father,
88
80
                                     Item *cond,
89
81
                                     Item *field,
122
114
{
123
115
  bool res;
124
116
  register Select_Lex *select_lex= &lex->select_lex;
125
 
  DRIZZLE_SELECT_START(session->getQueryString()->c_str());
 
117
  DRIZZLE_SELECT_START(session->query.c_str());
126
118
 
127
119
  if (select_lex->master_unit()->is_union() ||
128
120
      select_lex->master_unit()->fake_select_lex)
136
128
    unit->set_limit(unit->global_parameters);
137
129
    session->session_marker= 0;
138
130
    /*
139
 
      'options' of select_query will be set in JOIN, as far as JOIN for
 
131
      'options' of mysql_select will be set in JOIN, as far as JOIN for
140
132
      every PS/SP execution new, we will not need reset this flag if
141
133
      setup_tables_done_option changed for next rexecution
142
134
    */
143
 
    res= select_query(session,
144
 
                      &select_lex->ref_pointer_array,
 
135
    res= mysql_select(session, &select_lex->ref_pointer_array,
145
136
                      (TableList*) select_lex->table_list.first,
146
 
                      select_lex->with_wild,
147
 
                      select_lex->item_list,
 
137
                      select_lex->with_wild, select_lex->item_list,
148
138
                      select_lex->where,
149
139
                      select_lex->order_list.elements +
150
140
                      select_lex->group_list.elements,
151
 
                      (Order*) select_lex->order_list.first,
152
 
                      (Order*) select_lex->group_list.first,
 
141
                      (order_st*) select_lex->order_list.first,
 
142
                      (order_st*) select_lex->group_list.first,
153
143
                      select_lex->having,
154
144
                      select_lex->options | session->options |
155
145
                      setup_tables_done_option,
212
202
  bool res= false;
213
203
  bool direct_ref= false;
214
204
 
215
 
  List<Item_outer_ref>::iterator ref_it(select->inner_refs_list.begin());
 
205
  List_iterator<Item_outer_ref> ref_it(select->inner_refs_list);
216
206
  while ((ref= ref_it++))
217
207
  {
218
208
    Item *item= ref->outer_ref;
219
209
    Item **item_ref= ref->ref;
220
210
    Item_ref *new_ref;
221
211
    /*
222
 
      @todo this field item already might be present in the select list.
 
212
      TODO: this field item already might be present in the select list.
223
213
      In this case instead of adding new field item we could use an
224
214
      existing one. The change will lead to less operations for copying fields,
225
215
      smaller temporary tables and less data passed through filesort.
275
265
 
276
266
/*****************************************************************************
277
267
  Check fields, find best join, do the select and output fields.
278
 
  select_query assumes that all tables are already opened
 
268
  mysql_select assumes that all tables are already opened
279
269
*****************************************************************************/
280
270
 
281
271
/*
355
345
  @retval
356
346
    true   an error
357
347
*/
358
 
bool select_query(Session *session,
 
348
bool mysql_select(Session *session,
359
349
                  Item ***rref_pointer_array,
360
350
                  TableList *tables, 
361
351
                  uint32_t wild_num, 
362
352
                  List<Item> &fields,
363
353
                  COND *conds, 
364
354
                  uint32_t og_num,  
365
 
                  Order *order,
366
 
                  Order *group,
 
355
                  order_st *order, 
 
356
                  order_st *group,
367
357
                  Item *having, 
368
358
                  uint64_t select_options,
369
359
                  select_result *result, 
425
415
    goto err; // 1
426
416
  }
427
417
 
428
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
418
  if (session->lex->describe & DESCRIBE_EXTENDED)
429
419
  {
430
420
    join->conds_history= join->conds;
431
421
    join->having_history= (join->having?join->having:join->tmp_having);
436
426
 
437
427
  join->exec();
438
428
 
439
 
  if (session->getLex()->describe & DESCRIBE_EXTENDED)
 
429
  if (session->lex->describe & DESCRIBE_EXTENDED)
440
430
  {
441
431
    select_lex->where= join->conds_history;
442
432
    select_lex->having= join->having_history;
457
447
  return (cond? (new Item_cond_and(cond, item)) : item);
458
448
}
459
449
 
 
450
static void fix_list_after_tbl_changes(Select_Lex *new_parent, List<TableList> *tlist)
 
451
{
 
452
  List_iterator<TableList> it(*tlist);
 
453
  TableList *table;
 
454
  while ((table= it++))
 
455
  {
 
456
    if (table->on_expr)
 
457
      table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
 
458
    if (table->getNestedJoin())
 
459
      fix_list_after_tbl_changes(new_parent, &table->getNestedJoin()->join_list);
 
460
  }
 
461
}
 
462
 
460
463
/*****************************************************************************
461
464
  Create JoinTableS, make a guess about the table types,
462
465
  Approximate how many records will be used in each table
575
578
    substitutions.
576
579
  */
577
580
  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)))
 
581
      (((session->lex->current_select->cond_count+1)*2 +
 
582
        session->lex->current_select->between_count)*m+1);
 
583
  if (! (key_fields= (optimizer::KeyField*) session->alloc(sz)))
581
584
    return true;
582
585
  and_level= 0;
583
586
  field= end= key_fields;
618
621
 
619
622
  /* Process ON conditions for the nested joins */
620
623
  {
621
 
    List<TableList>::iterator li(join_tab->join->join_list->begin());
 
624
    List_iterator<TableList> li(*join_tab->join->join_list);
622
625
    TableList *table;
623
626
    while ((table= li++))
624
627
    {
670
673
        else if (use->getKeypart() != 0)                // First found must be 0
671
674
          continue;
672
675
 
673
 
#ifdef HAVE_VALGRIND
 
676
#ifdef HAVE_purify
674
677
        /* Valgrind complains about overlapped memcpy when save_pos==use. */
675
678
        if (save_pos != use)
676
679
#endif
752
755
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
753
756
{
754
757
  List<Item_field> indexed_fields;
755
 
  List<Item_field>::iterator indexed_fields_it(indexed_fields.begin());
756
 
  Order      *cur_group;
 
758
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
 
759
  order_st      *cur_group;
757
760
  Item_field *cur_item;
758
761
  key_map possible_keys(0);
759
762
 
766
769
  else if (join->select_distinct)
767
770
  { /* Collect all query fields referenced in the SELECT clause. */
768
771
    List<Item> &select_items= join->fields_list;
769
 
    List<Item>::iterator select_items_it(select_items.begin());
 
772
    List_iterator<Item> select_items_it(select_items);
770
773
    Item *item;
771
774
    while ((item= select_items_it++))
772
775
      item->walk(&Item::collect_item_field_processor, 0,
936
939
 
937
940
  /*
938
941
    we should restore old value of count_cuted_fields because
939
 
    store_val_in_field can be called from insert_query
 
942
    store_val_in_field can be called from mysql_insert
940
943
    with select_insert, which make count_cuted_fields= 1
941
944
   */
942
945
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
1009
1012
  j->ref.key_length=length;
1010
1013
  j->ref.key=(int) key;
1011
1014
  if (!(j->ref.key_buff= (unsigned char*) session->calloc(ALIGN_SIZE(length)*2)) ||
1012
 
      !(j->ref.key_copy= (StoredKey**) session->getMemRoot()->allocate((sizeof(StoredKey*) *
 
1015
      !(j->ref.key_copy= (StoredKey**) session->alloc((sizeof(StoredKey*) *
1013
1016
               (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)))
 
1017
      !(j->ref.items=    (Item**) session->alloc(sizeof(Item*)*keyparts)) ||
 
1018
      !(j->ref.cond_guards= (bool**) session->alloc(sizeof(uint*)*keyparts)))
1016
1019
  {
1017
1020
    return(true);
1018
1021
  }
1213
1216
  return tmp;
1214
1217
}
1215
1218
 
 
1219
/*
 
1220
  Check if given expression uses only table fields covered by the given index
 
1221
 
 
1222
  SYNOPSIS
 
1223
    uses_index_fields_only()
 
1224
      item           Expression to check
 
1225
      tbl            The table having the index
 
1226
      keyno          The index number
 
1227
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
1228
 
 
1229
  DESCRIPTION
 
1230
    Check if given expression only uses fields covered by index #keyno in the
 
1231
    table tbl. The expression can use any fields in any other tables.
 
1232
 
 
1233
    The expression is guaranteed not to be AND or OR - those constructs are
 
1234
    handled outside of this function.
 
1235
 
 
1236
  RETURN
 
1237
    true   Yes
 
1238
    false  No
 
1239
*/
 
1240
static bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno, bool other_tbls_ok)
 
1241
{
 
1242
  if (item->const_item())
 
1243
    return true;
 
1244
 
 
1245
  /*
 
1246
    Don't push down the triggered conditions. Nested outer joins execution
 
1247
    code may need to evaluate a condition several times (both triggered and
 
1248
    untriggered), and there is no way to put thi
 
1249
    TODO: Consider cloning the triggered condition and using the copies for:
 
1250
      1. push the first copy down, to have most restrictive index condition
 
1251
         possible
 
1252
      2. Put the second copy into tab->select_cond.
 
1253
  */
 
1254
  if (item->type() == Item::FUNC_ITEM &&
 
1255
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
 
1256
    return false;
 
1257
 
 
1258
  if (!(item->used_tables() & tbl->map))
 
1259
    return other_tbls_ok;
 
1260
 
 
1261
  Item::Type item_type= item->type();
 
1262
  switch (item_type) {
 
1263
  case Item::FUNC_ITEM:
 
1264
    {
 
1265
      /* This is a function, apply condition recursively to arguments */
 
1266
      Item_func *item_func= (Item_func*)item;
 
1267
      Item **child;
 
1268
      Item **item_end= (item_func->arguments()) + item_func->argument_count();
 
1269
      for (child= item_func->arguments(); child != item_end; child++)
 
1270
      {
 
1271
        if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
 
1272
          return false;
 
1273
      }
 
1274
      return true;
 
1275
    }
 
1276
  case Item::COND_ITEM:
 
1277
    {
 
1278
      /* This is a function, apply condition recursively to arguments */
 
1279
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
 
1280
      Item *list_item;
 
1281
      while ((list_item=li++))
 
1282
      {
 
1283
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
 
1284
          return false;
 
1285
      }
 
1286
      return true;
 
1287
    }
 
1288
  case Item::FIELD_ITEM:
 
1289
    {
 
1290
      Item_field *item_field= (Item_field*)item;
 
1291
      if (item_field->field->getTable() != tbl)
 
1292
        return true;
 
1293
      return item_field->field->part_of_key.test(keyno);
 
1294
    }
 
1295
  case Item::REF_ITEM:
 
1296
    return uses_index_fields_only(item->real_item(), tbl, keyno,
 
1297
                                  other_tbls_ok);
 
1298
  default:
 
1299
    return false; /* Play it safe, don't push unknown non-const items */
 
1300
  }
 
1301
}
 
1302
 
1216
1303
#define ICP_COND_USES_INDEX_ONLY 10
1217
1304
 
 
1305
/*
 
1306
  Get a part of the condition that can be checked using only index fields
 
1307
 
 
1308
  SYNOPSIS
 
1309
    make_cond_for_index()
 
1310
      cond           The source condition
 
1311
      table          The table that is partially available
 
1312
      keyno          The index in the above table. Only fields covered by the index
 
1313
                     are available
 
1314
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
1315
 
 
1316
  DESCRIPTION
 
1317
    Get a part of the condition that can be checked when for the given table
 
1318
    we have values only of fields covered by some index. The condition may
 
1319
    refer to other tables, it is assumed that we have values of all of their
 
1320
    fields.
 
1321
 
 
1322
    Example:
 
1323
      make_cond_for_index(
 
1324
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
 
1325
          t2, keyno(t2.key1))
 
1326
      will return
 
1327
        "cond(t1.field) AND cond(t2.key2)"
 
1328
 
 
1329
  RETURN
 
1330
    Index condition, or NULL if no condition could be inferred.
 
1331
*/
 
1332
static Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno, bool other_tbls_ok)
 
1333
{
 
1334
  if (!cond)
 
1335
    return NULL;
 
1336
  if (cond->type() == Item::COND_ITEM)
 
1337
  {
 
1338
    uint32_t n_marked= 0;
 
1339
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
1340
    {
 
1341
      Item_cond_and *new_cond=new Item_cond_and;
 
1342
      if (!new_cond)
 
1343
        return (COND*) 0;
 
1344
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
1345
      Item *item;
 
1346
      while ((item=li++))
 
1347
      {
 
1348
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
1349
        if (fix)
 
1350
          new_cond->argument_list()->push_back(fix);
 
1351
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
1352
      }
 
1353
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
1354
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
1355
      switch (new_cond->argument_list()->elements) {
 
1356
      case 0:
 
1357
        return (COND*) 0;
 
1358
      case 1:
 
1359
        return new_cond->argument_list()->head();
 
1360
      default:
 
1361
        new_cond->quick_fix_field();
 
1362
        return new_cond;
 
1363
      }
 
1364
    }
 
1365
    else /* It's OR */
 
1366
    {
 
1367
      Item_cond_or *new_cond=new Item_cond_or;
 
1368
      if (!new_cond)
 
1369
        return (COND*) 0;
 
1370
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
1371
      Item *item;
 
1372
      while ((item=li++))
 
1373
      {
 
1374
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
1375
        if (!fix)
 
1376
          return (COND*) 0;
 
1377
        new_cond->argument_list()->push_back(fix);
 
1378
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
1379
      }
 
1380
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
1381
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
1382
      new_cond->quick_fix_field();
 
1383
      new_cond->top_level_item();
 
1384
      return new_cond;
 
1385
    }
 
1386
  }
 
1387
 
 
1388
  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
 
1389
    return (COND*) 0;
 
1390
  cond->marker= ICP_COND_USES_INDEX_ONLY;
 
1391
  return cond;
 
1392
}
 
1393
 
 
1394
 
 
1395
static Item *make_cond_remainder(Item *cond, bool exclude_index)
 
1396
{
 
1397
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
 
1398
    return 0; /* Already checked */
 
1399
 
 
1400
  if (cond->type() == Item::COND_ITEM)
 
1401
  {
 
1402
    table_map tbl_map= 0;
 
1403
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
1404
    {
 
1405
      /* Create new top level AND item */
 
1406
      Item_cond_and *new_cond=new Item_cond_and;
 
1407
      if (!new_cond)
 
1408
        return (COND*) 0;
 
1409
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
1410
      Item *item;
 
1411
      while ((item=li++))
 
1412
      {
 
1413
        Item *fix= make_cond_remainder(item, exclude_index);
 
1414
        if (fix)
 
1415
        {
 
1416
          new_cond->argument_list()->push_back(fix);
 
1417
          tbl_map |= fix->used_tables();
 
1418
        }
 
1419
      }
 
1420
      switch (new_cond->argument_list()->elements) {
 
1421
      case 0:
 
1422
        return (COND*) 0;
 
1423
      case 1:
 
1424
        return new_cond->argument_list()->head();
 
1425
      default:
 
1426
        new_cond->quick_fix_field();
 
1427
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
1428
        return new_cond;
 
1429
      }
 
1430
    }
 
1431
    else /* It's OR */
 
1432
    {
 
1433
      Item_cond_or *new_cond=new Item_cond_or;
 
1434
      if (!new_cond)
 
1435
        return (COND*) 0;
 
1436
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
1437
      Item *item;
 
1438
      while ((item=li++))
 
1439
      {
 
1440
        Item *fix= make_cond_remainder(item, false);
 
1441
        if (!fix)
 
1442
          return (COND*) 0;
 
1443
        new_cond->argument_list()->push_back(fix);
 
1444
        tbl_map |= fix->used_tables();
 
1445
      }
 
1446
      new_cond->quick_fix_field();
 
1447
      ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
1448
      new_cond->top_level_item();
 
1449
      return new_cond;
 
1450
    }
 
1451
  }
 
1452
  return cond;
 
1453
}
1218
1454
 
1219
1455
/**
1220
1456
  cleanup JoinTable.
1221
1457
*/
1222
1458
void JoinTable::cleanup()
1223
1459
{
1224
 
  safe_delete(select);
1225
 
  safe_delete(quick);
1226
 
 
 
1460
  delete select;
 
1461
  select= 0;
 
1462
  delete quick;
 
1463
  quick= 0;
1227
1464
  if (cache.buff)
1228
 
  {
1229
 
    size_t size= cache.end - cache.buff;
1230
 
    global_join_buffer.sub(size);
1231
1465
    free(cache.buff);
1232
 
  }
1233
1466
  cache.buff= 0;
1234
1467
  limit= 0;
1235
1468
  if (table)
1249
1482
  read_record.end_read_record();
1250
1483
}
1251
1484
 
1252
 
bool only_eq_ref_tables(Join *join,Order *order,table_map tables)
 
1485
bool only_eq_ref_tables(Join *join,order_st *order,table_map tables)
1253
1486
{
1254
1487
  for (JoinTable **tab=join->map2table ; tables ; tab++, tables>>=1)
1255
1488
  {
1278
1511
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
1279
1512
  @endcode
1280
1513
*/
1281
 
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
 
1514
bool eq_ref_table(Join *join, order_st *start_order, JoinTable *tab)
1282
1515
{
1283
1516
  if (tab->cached_eq_ref_table)                 // If cached
1284
1517
    return tab->eq_ref_table;
1297
1530
  {
1298
1531
    if (! (*ref_item)->const_item())
1299
1532
    {                                           // Not a const ref
1300
 
      Order *order;
 
1533
      order_st *order;
1301
1534
      for (order=start_order ; order ; order=order->next)
1302
1535
      {
1303
1536
        if ((*ref_item)->eq(order->item[0],0))
1352
1585
  bool in_upper_level= false;
1353
1586
  while (cond_equal)
1354
1587
  {
1355
 
    List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
1588
    List_iterator_fast<Item_equal> li(cond_equal->current_level);
1356
1589
    while ((item= li++))
1357
1590
    {
1358
1591
      if (item->contains(field))
1517
1750
        /* Merge two multiple equalities forming a new one */
1518
1751
        left_item_equal->merge(right_item_equal);
1519
1752
        /* Remove the merged multiple equality from the list */
1520
 
        List<Item_equal>::iterator li(cond_equal->current_level.begin());
 
1753
        List_iterator<Item_equal> li(cond_equal->current_level);
1521
1754
        while ((li++) != right_item_equal) {};
1522
1755
        li.remove();
1523
1756
      }
1657
1890
                                       (Item_row *) right_item,
1658
1891
                                       cond_equal, eq_list);
1659
1892
      if (!is_converted)
1660
 
        session->getLex()->current_select->cond_count++;
 
1893
        session->lex->current_select->cond_count++;
1661
1894
    }
1662
1895
    else
1663
1896
    {
1664
1897
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
1665
 
      session->getLex()->current_select->cond_count++;
 
1898
      session->lex->current_select->cond_count++;
1666
1899
    }
1667
1900
 
1668
1901
    if (!is_converted)
1718
1951
    if (left_item->type() == Item::ROW_ITEM &&
1719
1952
        right_item->type() == Item::ROW_ITEM)
1720
1953
    {
1721
 
      session->getLex()->current_select->cond_count--;
 
1954
      session->lex->current_select->cond_count--;
1722
1955
      return check_row_equality(session,
1723
1956
                                (Item_row *) left_item,
1724
1957
                                (Item_row *) right_item,
1749
1982
    just an argument of a comparison predicate.
1750
1983
    The function also determines the maximum number of members in
1751
1984
    equality lists of each Item_cond_and object assigning it to
1752
 
    session->getLex()->current_select->max_equal_elems.
 
1985
    session->lex->current_select->max_equal_elems.
1753
1986
 
1754
1987
  @note
1755
1988
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
1806
2039
      Item_func::COND_AND_FUNC;
1807
2040
    List<Item> *args= ((Item_cond*) cond)->argument_list();
1808
2041
 
1809
 
    List<Item>::iterator li(args->begin());
 
2042
    List_iterator<Item> li(*args);
1810
2043
    Item *item;
1811
2044
 
1812
2045
    if (and_level)
1828
2061
          li.remove();
1829
2062
      }
1830
2063
 
1831
 
      List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
2064
      List_iterator_fast<Item_equal> it(cond_equal.current_level);
1832
2065
      while ((item_equal= it++))
1833
2066
      {
1834
2067
        item_equal->fix_length_and_dec();
1835
2068
        item_equal->update_used_tables();
1836
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
 
2069
        set_if_bigger(session->lex->current_select->max_equal_elems,
1837
2070
                      item_equal->members());
1838
2071
      }
1839
2072
 
1844
2077
       Make replacement of equality predicates for lower levels
1845
2078
       of the condition expression.
1846
2079
    */
1847
 
    li= args->begin();
 
2080
    li.rewind();
1848
2081
    while ((item= li++))
1849
2082
    {
1850
2083
      Item *new_item;
1892
2125
        }
1893
2126
        else
1894
2127
          item_equal= (Item_equal *) eq_list.pop();
1895
 
        set_if_bigger(session->getLex()->current_select->max_equal_elems,
 
2128
        set_if_bigger(session->lex->current_select->max_equal_elems,
1896
2129
                      item_equal->members());
1897
2130
        return item_equal;
1898
2131
      }
1905
2138
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
1906
2139
        and_cond->quick_fix_field();
1907
2140
        List<Item> *args= and_cond->argument_list();
1908
 
        List<Item_equal>::iterator it(cond_equal.current_level.begin());
 
2141
        List_iterator_fast<Item_equal> it(cond_equal.current_level);
1909
2142
        while ((item_equal= it++))
1910
2143
        {
1911
2144
          item_equal->fix_length_and_dec();
1912
2145
          item_equal->update_used_tables();
1913
 
          set_if_bigger(session->getLex()->current_select->max_equal_elems,
 
2146
          set_if_bigger(session->lex->current_select->max_equal_elems,
1914
2147
                        item_equal->members());
1915
2148
        }
1916
2149
        and_cond->cond_equal= cond_equal;
2031
2264
  if (join_list)
2032
2265
  {
2033
2266
    TableList *table;
2034
 
    List<TableList>::iterator li(join_list->begin());
 
2267
    List_iterator<TableList> li(*join_list);
2035
2268
 
2036
2269
    while ((table= li++))
2037
2270
    {
2246
2479
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
2247
2480
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
2248
2481
 
2249
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
2482
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
2250
2483
      while ((item_equal= it++))
2251
2484
      {
2252
2485
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2253
2486
      }
2254
2487
    }
2255
2488
 
2256
 
    List<Item>::iterator li(cond_list->begin());
 
2489
    List_iterator<Item> li(*cond_list);
2257
2490
    Item *item;
2258
2491
    while ((item= li++))
2259
2492
    {
2269
2502
 
2270
2503
    if (and_level)
2271
2504
    {
2272
 
      List<Item_equal>::iterator it(cond_equal->current_level.begin());
 
2505
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
2273
2506
      while ((item_equal= it++))
2274
2507
      {
2275
2508
        cond= eliminate_item_equal(cond, cond_equal->upper_levels, item_equal);
2311
2544
  @param cond       condition whose multiple equalities are to be checked
2312
2545
  @param table      constant table that has been read
2313
2546
*/
2314
 
void update_const_equal_items(COND *cond, JoinTable *tab)
 
2547
static void update_const_equal_items(COND *cond, JoinTable *tab)
2315
2548
{
2316
2549
  if (!(cond->used_tables() & tab->table->map))
2317
2550
    return;
2319
2552
  if (cond->type() == Item::COND_ITEM)
2320
2553
  {
2321
2554
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
2322
 
    List<Item>::iterator li(cond_list->begin());
 
2555
    List_iterator_fast<Item> li(*cond_list);
2323
2556
    Item *item;
2324
2557
    while ((item= li++))
2325
2558
      update_const_equal_items(item, tab);
2368
2601
  and_level
2369
2602
*/
2370
2603
static void change_cond_ref_to_const(Session *session,
2371
 
                                     list<COND_CMP>& save_list,
 
2604
                                     vector<COND_CMP>& save_list,
2372
2605
                                     Item *and_father,
2373
2606
                                     Item *cond,
2374
2607
                                     Item *field,
2377
2610
  if (cond->type() == Item::COND_ITEM)
2378
2611
  {
2379
2612
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2380
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
2613
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2381
2614
    Item *item;
2382
2615
    while ((item=li++))
2383
2616
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
2384
 
 
2385
2617
    return;
2386
2618
  }
2387
2619
  if (cond->eq_cmp_result() == Item::COND_OK)
2400
2632
       left_item->collation.collation == value->collation.collation))
2401
2633
  {
2402
2634
    Item *tmp=value->clone_item();
 
2635
    tmp->collation.set(right_item->collation);
 
2636
 
2403
2637
    if (tmp)
2404
2638
    {
2405
 
      tmp->collation.set(right_item->collation);
2406
2639
      session->change_item_tree(args + 1, tmp);
2407
2640
      func->update_used_tables();
2408
2641
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2422
2655
            right_item->collation.collation == value->collation.collation))
2423
2656
  {
2424
2657
    Item *tmp= value->clone_item();
 
2658
    tmp->collation.set(left_item->collation);
 
2659
 
2425
2660
    if (tmp)
2426
2661
    {
2427
 
      tmp->collation.set(left_item->collation);
2428
2662
      session->change_item_tree(args, tmp);
2429
2663
      value= tmp;
2430
2664
      func->update_used_tables();
2457
2691
  if (conds->type() == Item::COND_ITEM)
2458
2692
  {
2459
2693
    Item_cond *cnd= (Item_cond*) conds;
2460
 
    List<Item>::iterator li(cnd->argument_list()->begin());
 
2694
    List_iterator<Item> li(*(cnd->argument_list()));
2461
2695
    Item *item;
2462
2696
    while ((item= li++))
2463
2697
    {
2474
2708
}
2475
2709
 
2476
2710
static void propagate_cond_constants(Session *session, 
2477
 
                                     list<COND_CMP>& save_list, 
 
2711
                                     vector<COND_CMP>& save_list, 
2478
2712
                                     COND *and_father, 
2479
2713
                                     COND *cond)
2480
2714
{
2481
2715
  if (cond->type() == Item::COND_ITEM)
2482
2716
  {
2483
2717
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
2484
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
2718
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2485
2719
    Item *item;
2486
 
    list<COND_CMP> save;
 
2720
    vector<COND_CMP> save;
2487
2721
    while ((item=li++))
2488
2722
    {
2489
2723
      propagate_cond_constants(session, save, and_level ? cond : item, item);
2491
2725
    if (and_level)
2492
2726
    {
2493
2727
      // Handle other found items
2494
 
      for (list<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
 
2728
      for (vector<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
2495
2729
      {
2496
 
        Item **args= iter->second->arguments();
2497
 
        if (not args[0]->const_item())
 
2730
        Item **args= iter->cmp_func->arguments();
 
2731
        if (!args[0]->const_item())
2498
2732
        {
2499
 
          change_cond_ref_to_const(session, save, iter->first,
2500
 
                                   iter->first, args[0], args[1] );
 
2733
          change_cond_ref_to_const( session, save, iter->and_level,
 
2734
                                    iter->and_level, args[0], args[1] );
2501
2735
        }
2502
2736
      }
2503
2737
    }
2610
2844
         position:
2611
2845
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
2612
2846
             joins) we've opened but didn't close.
2613
 
          2. {each NestedJoin class not simplified away}->counter - number
 
2847
          2. {each nested_join_st structure not simplified away}->counter - number
2614
2848
             of this nested join's children that have already been added to to
2615
2849
             the partial join order.
2616
2850
  @endverbatim
2688
2922
                             &join->cond_equal);
2689
2923
 
2690
2924
    /* change field = field to field = const for each found field = const */
2691
 
    list<COND_CMP> temp;
 
2925
    vector<COND_CMP> temp;
2692
2926
    propagate_cond_constants(session, temp, conds, conds);
2693
2927
    /*
2694
2928
      Remove all instances of item == item
2715
2949
  {
2716
2950
    bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
2717
2951
 
2718
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
2952
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2719
2953
    Item::cond_result tmp_cond_value;
2720
2954
    bool should_fix_fields= false;
2721
2955
 
2769
3003
    {                                           
2770
3004
      /* Argument list contains only one element, so reduce it so a single item, then remove list */
2771
3005
      item= ((Item_cond*) cond)->argument_list()->head();
2772
 
      ((Item_cond*) cond)->argument_list()->clear();
 
3006
      ((Item_cond*) cond)->argument_list()->empty();
2773
3007
      return item;
2774
3008
    }
2775
3009
  }
2847
3081
  }
2848
3082
  else if (cond->const_item() && !cond->is_expensive())
2849
3083
  /*
2850
 
    @todo
 
3084
    TODO:
2851
3085
    Excluding all expensive functions is too restritive we should exclude only
2852
3086
    materialized IN subquery predicates because they can't yet be evaluated
2853
3087
    here (they need additional initialization that is done later on).
2919
3153
  {
2920
3154
    bool and_level= (((Item_cond*) cond)->functype()
2921
3155
                     == Item_func::COND_AND_FUNC);
2922
 
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
3156
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
2923
3157
    Item *item;
2924
3158
    while ((item=li++))
2925
3159
    {
3057
3291
    table->emptyRecord();
3058
3292
    if (table->group && join->tmp_table_param.sum_func_count &&
3059
3293
        table->getShare()->sizeKeys() && !table->cursor->inited)
3060
 
    {
3061
 
      int tmp_error;
3062
 
      tmp_error= table->cursor->startIndexScan(0, 0);
3063
 
      if (tmp_error != 0)
3064
 
      {
3065
 
        table->print_error(tmp_error, MYF(0));
3066
 
        return -1;
3067
 
      }
3068
 
    }
 
3294
      table->cursor->startIndexScan(0, 0);
3069
3295
  }
3070
3296
  /* Set up select_end */
3071
3297
  Next_select_func end_select= setup_end_select_func(join);
3162
3388
      rc= sub_select(join,join_tab,end_of_records);
3163
3389
    return rc;
3164
3390
  }
3165
 
  if (join->session->getKilled())               // If aborted by user
 
3391
  if (join->session->killed)            // If aborted by user
3166
3392
  {
3167
3393
    join->session->send_kill_message();
3168
3394
    return NESTED_LOOP_KILLED;
3371
3597
  return 0;
3372
3598
}
3373
3599
 
 
3600
int join_read_const_table(JoinTable *tab, optimizer::Position *pos)
 
3601
{
 
3602
  int error;
 
3603
  Table *table=tab->table;
 
3604
  table->const_table=1;
 
3605
  table->null_row=0;
 
3606
  table->status=STATUS_NO_RECORD;
 
3607
 
 
3608
  if (tab->type == AM_SYSTEM)
 
3609
  {
 
3610
    if ((error=join_read_system(tab)))
 
3611
    {                                           // Info for DESCRIBE
 
3612
      tab->info="const row not found";
 
3613
      /* Mark for EXPLAIN that the row was not found */
 
3614
      pos->setFanout(0.0);
 
3615
      pos->clearRefDependMap();
 
3616
      if (! table->maybe_null || error > 0)
 
3617
        return(error);
 
3618
    }
 
3619
  }
 
3620
  else
 
3621
  {
 
3622
    if (! table->key_read && 
 
3623
        table->covering_keys.test(tab->ref.key) && 
 
3624
        ! table->no_keyread &&
 
3625
        (int) table->reginfo.lock_type <= (int) TL_READ_WITH_SHARED_LOCKS)
 
3626
    {
 
3627
      table->key_read=1;
 
3628
      table->cursor->extra(HA_EXTRA_KEYREAD);
 
3629
      tab->index= tab->ref.key;
 
3630
    }
 
3631
    error=join_read_const(tab);
 
3632
    if (table->key_read)
 
3633
    {
 
3634
      table->key_read=0;
 
3635
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
3636
    }
 
3637
    if (error)
 
3638
    {
 
3639
      tab->info="unique row not found";
 
3640
      /* Mark for EXPLAIN that the row was not found */
 
3641
      pos->setFanout(0.0);
 
3642
      pos->clearRefDependMap();
 
3643
      if (!table->maybe_null || error > 0)
 
3644
        return(error);
 
3645
    }
 
3646
  }
 
3647
  if (*tab->on_expr_ref && !table->null_row)
 
3648
  {
 
3649
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
 
3650
      table->mark_as_null_row();
 
3651
  }
 
3652
  if (!table->null_row)
 
3653
    table->maybe_null=0;
 
3654
 
 
3655
  /* Check appearance of new constant items in Item_equal objects */
 
3656
  Join *join= tab->join;
 
3657
  if (join->conds)
 
3658
    update_const_equal_items(join->conds, tab);
 
3659
  TableList *tbl;
 
3660
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
 
3661
  {
 
3662
    TableList *embedded;
 
3663
    TableList *embedding= tbl;
 
3664
    do
 
3665
    {
 
3666
      embedded= embedding;
 
3667
      if (embedded->on_expr)
 
3668
         update_const_equal_items(embedded->on_expr, tab);
 
3669
      embedding= embedded->getEmbedding();
 
3670
    }
 
3671
    while (embedding &&
 
3672
           embedding->getNestedJoin()->join_list.head() == embedded);
 
3673
  }
 
3674
 
 
3675
  return(0);
 
3676
}
 
3677
 
 
3678
int join_read_system(JoinTable *tab)
 
3679
{
 
3680
  Table *table= tab->table;
 
3681
  int error;
 
3682
  if (table->status & STATUS_GARBAGE)           // If first read
 
3683
  {
 
3684
    if ((error=table->cursor->read_first_row(table->getInsertRecord(),
 
3685
                                           table->getShare()->getPrimaryKey())))
 
3686
    {
 
3687
      if (error != HA_ERR_END_OF_FILE)
 
3688
        return table->report_error(error);
 
3689
      tab->table->mark_as_null_row();
 
3690
      table->emptyRecord();                     // Make empty record
 
3691
      return -1;
 
3692
    }
 
3693
    table->storeRecord();
 
3694
  }
 
3695
  else if (!table->status)                      // Only happens with left join
 
3696
    table->restoreRecord();                     // restore old record
 
3697
  table->null_row=0;
 
3698
  return table->status ? -1 : 0;
 
3699
}
 
3700
 
3374
3701
/**
3375
3702
  Read a (constant) table when there is at most one matching row.
3376
3703
 
3442
3769
 
3443
3770
  if (!table->cursor->inited)
3444
3771
  {
3445
 
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3446
 
    if (error != 0)
3447
 
    {
3448
 
      table->print_error(error, MYF(0));
3449
 
    }
 
3772
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3450
3773
  }
3451
3774
 
3452
 
  /* @todo Why don't we do "Late NULLs Filtering" here? */
 
3775
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
3453
3776
  if (cmp_buffer_with_ref(tab) ||
3454
3777
      (table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW)))
3455
3778
  {
3494
3817
 
3495
3818
  /* Initialize the index first */
3496
3819
  if (!table->cursor->inited)
3497
 
  {
3498
 
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3499
 
    if (error != 0)
3500
 
      return table->report_error(error);
3501
 
  }
 
3820
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3502
3821
 
3503
3822
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3504
3823
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
3532
3851
  Table *table= tab->table;
3533
3852
 
3534
3853
  if (!table->cursor->inited)
3535
 
  {
3536
 
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3537
 
    if (error != 0)
3538
 
      return table->report_error(error);
3539
 
  }
 
3854
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3540
3855
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3541
3856
    return -1;
3542
3857
  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
3640
3955
 
3641
3956
int test_if_quick_select(JoinTable *tab)
3642
3957
{
3643
 
  safe_delete(tab->select->quick);
3644
 
 
 
3958
  delete tab->select->quick;
 
3959
  tab->select->quick= 0;
3645
3960
  return tab->select->test_quick_select(tab->join->session, tab->keys,
3646
3961
                                        (table_map) 0, HA_POS_ERROR, 0, false);
3647
3962
}
3651
3966
  if (tab->select && tab->select->quick && tab->select->quick->reset())
3652
3967
    return 1;
3653
3968
 
3654
 
  if (tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true))
3655
 
    return 1;
 
3969
  tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true);
3656
3970
 
3657
3971
  return (*tab->read_record.read_record)(&tab->read_record);
3658
3972
}
3685
3999
  }
3686
4000
 
3687
4001
  if (!table->cursor->inited)
3688
 
  {
3689
 
    error= table->cursor->startIndexScan(tab->index, tab->sorted);
3690
 
    if (error != 0)
3691
 
    {
3692
 
      table->report_error(error);
3693
 
      return -1;
3694
 
    }
3695
 
  }
 
4002
    table->cursor->startIndexScan(tab->index, tab->sorted);
3696
4003
  if ((error=tab->table->cursor->index_first(tab->table->getInsertRecord())))
3697
4004
  {
3698
4005
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3751
4058
  tab->read_record.index=tab->index;
3752
4059
  tab->read_record.record=table->getInsertRecord();
3753
4060
  if (!table->cursor->inited)
3754
 
  {
3755
 
    error= table->cursor->startIndexScan(tab->index, 1);
3756
 
    if (error != 0)
3757
 
      return table->report_error(error);
3758
 
  }
 
4061
    table->cursor->startIndexScan(tab->index, 1);
3759
4062
  if ((error= tab->table->cursor->index_last(tab->table->getInsertRecord())))
3760
4063
    return table->report_error(error);
3761
4064
 
3819
4122
        {
3820
4123
          if (!join->first_record)
3821
4124
          {
3822
 
                  List<Item>::iterator it(join->fields->begin());
 
4125
                  List_iterator_fast<Item> it(*join->fields);
3823
4126
                  Item *item;
3824
4127
            /* No matching rows for group function */
3825
4128
            join->clear();
3835
4138
              error=join->result->send_data(*join->fields) ? 1 : 0;
3836
4139
            join->send_records++;
3837
4140
          }
3838
 
          if (join->rollup.getState() != Rollup::STATE_NONE && error <= 0)
 
4141
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
3839
4142
          {
3840
4143
            if (join->rollup_send_data((uint32_t) (idx+1)))
3841
4144
              error= 1;
3896
4199
  Table *table=join->tmp_table;
3897
4200
  int     idx= -1;
3898
4201
 
3899
 
  if (join->session->getKilled())
 
4202
  if (join->session->killed)
3900
4203
  {                                             // Aborted by user
3901
4204
    join->session->send_kill_message();
3902
4205
    return NESTED_LOOP_KILLED;
3925
4228
            return NESTED_LOOP_ERROR;
3926
4229
          }
3927
4230
        }
3928
 
        if (join->rollup.getState() != Rollup::STATE_NONE)
 
4231
        if (join->rollup.state != ROLLUP::STATE_NONE)
3929
4232
        {
3930
4233
          if (join->rollup_write_data((uint32_t) (idx+1), table))
3931
4234
            return NESTED_LOOP_ERROR;
3944
4247
    if (idx < (int) join->send_group_parts)
3945
4248
    {
3946
4249
      copy_fields(&join->tmp_table_param);
3947
 
      if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
3948
 
        return NESTED_LOOP_ERROR;
 
4250
      copy_funcs(join->tmp_table_param.items_to_copy);
3949
4251
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
3950
4252
        return NESTED_LOOP_ERROR;
3951
4253
      return NESTED_LOOP_OK;
3962
4264
  outer join table.
3963
4265
  We can't remove tests that are made against columns which are stored
3964
4266
  in sorted order.
 
4267
*****************************************************************************/
 
4268
 
 
4269
/**
3965
4270
  @return
3966
 
    1 if right_item used is a removable reference key on left_item
3967
 
    0 otherwise.
3968
 
****************************************************************************/
 
4271
    1 if right_item is used removable reference key on left_item
 
4272
*/
3969
4273
bool test_if_ref(Item_field *left_item,Item *right_item)
3970
4274
{
3971
4275
  Field *field=left_item->field;
4058
4362
      Item_cond_and *new_cond=new Item_cond_and;
4059
4363
      if (!new_cond)
4060
4364
        return (COND*) 0;
4061
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
4365
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4062
4366
      Item *item;
4063
4367
      while ((item=li++))
4064
4368
      {
4088
4392
      Item_cond_or *new_cond=new Item_cond_or;
4089
4393
      if (!new_cond)
4090
4394
        return (COND*) 0;
4091
 
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
 
4395
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4092
4396
      Item *item;
4093
4397
      while ((item=li++))
4094
4398
      {
4166
4470
    }
4167
4471
 
4168
4472
    for (part=0 ; part < ref_parts ; part++,key_part++)
4169
 
    {
4170
4473
      if (field->eq(key_part->field) &&
4171
 
          !(key_part->key_part_flag & HA_PART_KEY_SEG) &&
4172
 
          //If field can be NULL, we should not remove this predicate, as
4173
 
          //it may lead to non-rejection of NULL values. 
4174
 
          !(field->real_maybe_null()))
4175
 
      {
 
4474
          !(key_part->key_part_flag & HA_PART_KEY_SEG))
4176
4475
        return table->reginfo.join_tab->ref.items[part];
4177
 
      }
4178
 
    }
4179
4476
  }
4180
4477
  return (Item*) 0;
4181
4478
}
4200
4497
  @retval
4201
4498
    -1   Reverse key can be used
4202
4499
*/
4203
 
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
 
4500
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
4204
4501
{
4205
4502
  KeyPartInfo *key_part= NULL;
4206
4503
  KeyPartInfo *key_part_end= NULL;
4306
4603
    - MAX_KEY                   If we can't use other key
4307
4604
    - the number of found key   Otherwise
4308
4605
*/
4309
 
static uint32_t test_if_subkey(Order *order,
 
4606
static uint32_t test_if_subkey(order_st *order,
4310
4607
                               Table *table,
4311
4608
                               uint32_t ref,
4312
4609
                               uint32_t ref_key_parts,
4408
4705
*/
4409
4706
bool find_field_in_order_list (Field *field, void *data)
4410
4707
{
4411
 
  Order *group= (Order *) data;
 
4708
  order_st *group= (order_st *) data;
4412
4709
  bool part_found= 0;
4413
 
  for (Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
4710
  for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4414
4711
  {
4415
4712
    Item *item= (*tmp_group->item)->real_item();
4416
4713
    if (item->type() == Item::FIELD_ITEM &&
4440
4737
{
4441
4738
  List<Item> *fields= (List<Item> *) data;
4442
4739
  bool part_found= 0;
4443
 
  List<Item>::iterator li(fields->begin());
 
4740
  List_iterator<Item> li(*fields);
4444
4741
  Item *item;
4445
4742
 
4446
4743
  while ((item= li++))
4480
4777
  @retval
4481
4778
    1    We can use an index.
4482
4779
*/
4483
 
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
 
4780
bool test_if_skip_sort_order(JoinTable *tab, order_st *order, ha_rows select_limit, bool no_changes, const key_map *map)
4484
4781
{
4485
4782
  int32_t ref_key;
4486
4783
  uint32_t ref_key_parts;
4497
4794
  */
4498
4795
  usable_keys= *map;
4499
4796
 
4500
 
  for (Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
4797
  for (order_st *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4501
4798
  {
4502
4799
    Item *item= (*tmp_order->item)->real_item();
4503
4800
    if (item->type() != Item::FIELD_ITEM)
4525
4822
    save_quick= select->quick;
4526
4823
    /*
4527
4824
      assume results are not ordered when index merge is used
4528
 
      @todo sergeyp: Results of all index merge selects actually are ordered
 
4825
      TODO: sergeyp: Results of all index merge selects actually are ordered
4529
4826
      by clustered PK values.
4530
4827
    */
4531
4828
 
4800
5097
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
4801
5098
          if (select && select->quick)
4802
5099
          {
4803
 
            safe_delete(select->quick);
 
5100
            delete select->quick;
 
5101
            select->quick= 0;
4804
5102
          }
4805
5103
          if (table->covering_keys.test(best_key))
4806
5104
          {
4873
5171
          tab->limit= 0;
4874
5172
          return 0; // Reverse sort not supported
4875
5173
        }
4876
 
        select->quick= tmp;
 
5174
        select->quick=tmp;
4877
5175
      }
4878
5176
    }
4879
5177
    else if (tab->type != AM_NEXT &&
4922
5220
    -1          Some fatal error
4923
5221
    1           No records
4924
5222
*/
4925
 
int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
 
5223
int create_sort_index(Session *session, Join *join, order_st *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
4926
5224
{
4927
5225
  uint32_t length= 0;
4928
5226
  ha_rows examined_rows;
4949
5247
                              is_order_by ?  &table->keys_in_use_for_order_by :
4950
5248
                              &table->keys_in_use_for_group_by))
4951
5249
    return(0);
4952
 
  for (Order *ord= join->order; ord; ord= ord->next)
 
5250
  for (order_st *ord= join->order; ord; ord= ord->next)
4953
5251
    length++;
4954
 
  if (!(join->sortorder= make_unireg_sortorder(order, &length, join->sortorder)))
4955
 
  {
4956
 
    return(-1);
4957
 
  }
 
5252
  if (!(join->sortorder=
 
5253
        make_unireg_sortorder(order, &length, join->sortorder)))
 
5254
    goto err;
4958
5255
 
4959
5256
  table->sort.io_cache= new internal::IO_CACHE;
4960
5257
  table->status=0;                              // May be wrong if quick_select
4989
5286
                                                                 &tab->ref,
4990
5287
                                                                 tab->found_records))))
4991
5288
      {
4992
 
        return(-1);
 
5289
        goto err;
4993
5290
      }
4994
5291
    }
4995
5292
  }
4996
5293
 
4997
5294
  if (table->getShare()->getType())
4998
5295
    table->cursor->info(HA_STATUS_VARIABLE);    // Get record count
4999
 
 
5000
 
  FileSort filesort(*session);
5001
 
  table->sort.found_records=filesort.run(table,join->sortorder, length,
5002
 
                                         select, filesort_limit, 0,
5003
 
                                         examined_rows);
 
5296
  table->sort.found_records=filesort(session, table,join->sortorder, length,
 
5297
                                     select, filesort_limit, 0,
 
5298
                                     &examined_rows);
5004
5299
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
5005
5300
  if (select)
5006
5301
  {
5018
5313
    table->key_read=0;
5019
5314
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
5020
5315
  }
5021
 
 
5022
5316
  return(table->sort.found_records == HA_POS_ERROR);
 
5317
err:
 
5318
  return(-1);
5023
5319
}
5024
5320
 
5025
5321
int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having)
5033
5329
  org_record=(char*) (record=table->getInsertRecord())+offset;
5034
5330
  new_record=(char*) table->getUpdateRecord()+offset;
5035
5331
 
5036
 
  if ((error= cursor->startTableScan(1)))
5037
 
    goto err;
5038
 
 
 
5332
  cursor->startTableScan(1);
5039
5333
  error=cursor->rnd_next(record);
5040
5334
  for (;;)
5041
5335
  {
5042
 
    if (session->getKilled())
 
5336
    if (session->killed)
5043
5337
    {
5044
5338
      session->send_kill_message();
5045
5339
      error=0;
5152
5446
    return(1);
5153
5447
  }
5154
5448
 
5155
 
  if ((error= cursor->startTableScan(1)))
5156
 
    goto err;
5157
 
 
 
5449
  cursor->startTableScan(1);
5158
5450
  key_pos= &key_buffer[0];
5159
5451
  for (;;)
5160
5452
  {
5161
5453
    unsigned char *org_key_pos;
5162
 
    if (session->getKilled())
 
5454
    if (session->killed)
5163
5455
    {
5164
5456
      session->send_kill_message();
5165
5457
      error=0;
5213
5505
  return(1);
5214
5506
}
5215
5507
 
5216
 
SortField *make_unireg_sortorder(Order *order, uint32_t *length, SortField *sortorder)
 
5508
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length, SORT_FIELD *sortorder)
5217
5509
{
5218
5510
  uint32_t count;
5219
 
  SortField *sort,*pos;
 
5511
  SORT_FIELD *sort,*pos;
5220
5512
 
5221
5513
  count=0;
5222
 
  for (Order *tmp = order; tmp; tmp=tmp->next)
 
5514
  for (order_st *tmp = order; tmp; tmp=tmp->next)
5223
5515
    count++;
5224
5516
  if (!sortorder)
5225
 
    sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) *
 
5517
    sortorder= (SORT_FIELD*) memory::sql_alloc(sizeof(SORT_FIELD) *
5226
5518
                                       (max(count, *length) + 1));
5227
5519
  pos= sort= sortorder;
5228
5520
 
5344
5636
static bool find_order_in_list(Session *session, 
5345
5637
                               Item **ref_pointer_array, 
5346
5638
                               TableList *tables,
5347
 
                               Order *order,
 
5639
                               order_st *order,
5348
5640
                               List<Item> &fields,
5349
5641
                               List<Item> &all_fields,
5350
5642
                               bool is_group_field)
5366
5658
    if (!count || count > fields.elements)
5367
5659
    {
5368
5660
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
5369
 
               order_item->full_name(), session->where());
 
5661
               order_item->full_name(), session->where);
5370
5662
      return true;
5371
5663
    }
5372
5664
    order->item= ref_pointer_array + count - 1;
5443
5735
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
5444
5736
                          ER(ER_NON_UNIQ_ERROR),
5445
5737
                          ((Item_ident*) order_item)->field_name,
5446
 
                          session->where());
 
5738
                          session->where);
5447
5739
    }
5448
5740
  }
5449
5741
 
5483
5775
                TableList *tables,
5484
5776
                            List<Item> &fields,
5485
5777
                List<Item> &all_fields,
5486
 
                Order *order)
 
5778
                order_st *order)
5487
5779
{
5488
 
  session->setWhere("order clause");
 
5780
  session->where="order clause";
5489
5781
  for (; order; order=order->next)
5490
5782
  {
5491
5783
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields,
5525
5817
                TableList *tables,
5526
5818
                      List<Item> &fields,
5527
5819
                List<Item> &all_fields,
5528
 
                Order *order,
 
5820
                order_st *order,
5529
5821
                      bool *hidden_group_fields)
5530
5822
{
5531
5823
  *hidden_group_fields=0;
5532
 
  Order *ord;
 
5824
  order_st *ord;
5533
5825
 
5534
5826
  if (!order)
5535
5827
    return 0;                           /* Everything is ok */
5536
5828
 
5537
5829
  uint32_t org_fields=all_fields.elements;
5538
5830
 
5539
 
  session->setWhere("group statement");
 
5831
  session->where="group statement";
5540
5832
  for (ord= order; ord; ord= ord->next)
5541
5833
  {
5542
5834
    if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
5569
5861
    Item *item;
5570
5862
    Item_field *field;
5571
5863
    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());
 
5864
    List_iterator<Item> li(fields);
 
5865
    List_iterator<Item_field> naf_it(session->lex->current_select->non_agg_fields);
5574
5866
 
5575
5867
    field= naf_it++;
5576
5868
    while (field && (item=li++))
5596
5888
            if ((*ord->item)->eq((Item*)field, 0))
5597
5889
              goto next_field;
5598
5890
          /*
5599
 
            @todo change ER_WRONG_FIELD_WITH_GROUP to more detailed ER_NON_GROUPING_FIELD_USED
 
5891
            TODO: change ER_WRONG_FIELD_WITH_GROUP to more detailed
 
5892
            ER_NON_GROUPING_FIELD_USED
5600
5893
          */
5601
5894
          my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), field->full_name());
5602
5895
          return 1;
5618
5911
  Try to use the fields in the order given by 'order' to allow one to
5619
5912
  optimize away 'order by'.
5620
5913
*/
5621
 
Order *create_distinct_group(Session *session,
 
5914
order_st *create_distinct_group(Session *session,
5622
5915
                                Item **ref_pointer_array,
5623
 
                                Order *order_list,
 
5916
                                order_st *order_list,
5624
5917
                                List<Item> &fields,
5625
5918
                                List<Item> &,
5626
5919
                                bool *all_order_by_fields_used)
5627
5920
{
5628
 
  List<Item>::iterator li(fields.begin());
 
5921
  List_iterator<Item> li(fields);
5629
5922
  Item *item;
5630
 
  Order *order,*group,**prev;
 
5923
  order_st *order,*group,**prev;
5631
5924
 
5632
5925
  *all_order_by_fields_used= 1;
5633
5926
  while ((item=li++))
5638
5931
  {
5639
5932
    if (order->in_field_list)
5640
5933
    {
5641
 
      Order *ord=(Order*) session->getMemRoot()->duplicate((char*) order,sizeof(Order));
 
5934
      order_st *ord=(order_st*) session->memdup((char*) order,sizeof(order_st));
5642
5935
      if (!ord)
5643
5936
        return 0;
5644
5937
      *prev=ord;
5649
5942
      *all_order_by_fields_used= 0;
5650
5943
  }
5651
5944
 
5652
 
  li= fields.begin();
 
5945
  li.rewind();
5653
5946
  while ((item=li++))
5654
5947
  {
5655
5948
    if (!item->const_item() && !item->with_sum_func && !item->marker)
5658
5951
        Don't put duplicate columns from the SELECT list into the
5659
5952
        GROUP BY list.
5660
5953
      */
5661
 
      Order *ord_iter;
 
5954
      order_st *ord_iter;
5662
5955
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
5663
5956
        if ((*ord_iter->item)->eq(item, 1))
5664
5957
          goto next_item;
5665
5958
 
5666
 
      Order *ord=(Order*) session->calloc(sizeof(Order));
 
5959
      order_st *ord=(order_st*) session->calloc(sizeof(order_st));
5667
5960
      if (!ord)
5668
5961
        return 0;
5669
5962
 
5689
5982
*/
5690
5983
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
5691
5984
{
5692
 
  List<Item>::iterator li(fields.begin());
 
5985
  List_iterator<Item> li(fields);
5693
5986
  Item *field;
5694
5987
 
5695
5988
  param->field_count=param->sum_func_count=param->func_count=
5746
6039
*/
5747
6040
int test_if_item_cache_changed(List<Cached_item> &list)
5748
6041
{
5749
 
  List<Cached_item>::iterator li(list.begin());
 
6042
  List_iterator<Cached_item> li(list);
5750
6043
  int idx= -1,i;
5751
6044
  Cached_item *buff;
5752
6045
 
5795
6088
                       List<Item> &all_fields)
5796
6089
{
5797
6090
  Item *pos;
5798
 
  List<Item>::iterator li(all_fields.begin());
 
6091
  List_iterator_fast<Item> li(all_fields);
5799
6092
  CopyField *copy= NULL;
5800
 
  res_selected_fields.clear();
5801
 
  res_all_fields.clear();
5802
 
  List<Item>::iterator itr(res_all_fields.begin());
 
6093
  res_selected_fields.empty();
 
6094
  res_all_fields.empty();
 
6095
  List_iterator_fast<Item> itr(res_all_fields);
5803
6096
  List<Item> extra_funcs;
5804
6097
  uint32_t i, border= all_fields.elements - elements;
5805
6098
 
5807
6100
      !(copy=param->copy_field= new CopyField[param->field_count]))
5808
6101
    goto err2;
5809
6102
 
5810
 
  param->copy_funcs.clear();
 
6103
  param->copy_funcs.empty();
5811
6104
  for (i= 0; (pos= li++); i++)
5812
6105
  {
5813
6106
    Field *field;
5862
6155
        {
5863
6156
          copy->set(tmp, item->result_field);
5864
6157
          item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
5865
 
#ifdef HAVE_VALGRIND
 
6158
#ifdef HAVE_purify
5866
6159
          copy->to_ptr[copy->from_length]= 0;
5867
6160
#endif
5868
6161
          copy++;
5876
6169
             !real_pos->with_sum_func)
5877
6170
    {                                           // Save for send fields
5878
6171
      pos= real_pos;
5879
 
      /* 
5880
 
        @todo In most cases this result will be sent to the user.
 
6172
      /* TODO:
 
6173
        In most cases this result will be sent to the user.
5881
6174
        This should be changed to use copy_int or copy_real depending
5882
6175
        on how the value is to be used: In some cases this may be an
5883
6176
        argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
5931
6224
  for (; ptr != end; ptr++)
5932
6225
    (*ptr->do_copy)(ptr);
5933
6226
 
5934
 
  List<Item>::iterator it(param->copy_funcs.begin());
 
6227
  List_iterator_fast<Item> it(param->copy_funcs);
5935
6228
  Item_copy_string *item;
5936
6229
  while ((item = (Item_copy_string*) it++))
5937
6230
    item->copy();
5960
6253
                                                uint32_t elements,
5961
6254
                              List<Item> &all_fields)
5962
6255
{
5963
 
  List<Item>::iterator it(all_fields.begin());
 
6256
  List_iterator_fast<Item> it(all_fields);
5964
6257
  Item *item_field,*item;
5965
6258
 
5966
 
  res_selected_fields.clear();
5967
 
  res_all_fields.clear();
 
6259
  res_selected_fields.empty();
 
6260
  res_all_fields.empty();
5968
6261
 
5969
6262
  uint32_t i, border= all_fields.elements - elements;
5970
6263
  for (i= 0; (item= it++); i++)
6009
6302
      item_field;
6010
6303
  }
6011
6304
 
6012
 
  List<Item>::iterator itr(res_all_fields.begin());
 
6305
  List_iterator_fast<Item> itr(res_all_fields);
6013
6306
  for (i= 0; i < border; i++)
6014
6307
    itr++;
6015
6308
  itr.sublist(res_selected_fields, elements);
6039
6332
                               uint32_t elements,
6040
6333
                                                 List<Item> &all_fields)
6041
6334
{
6042
 
  List<Item>::iterator it(all_fields.begin());
 
6335
  List_iterator_fast<Item> it(all_fields);
6043
6336
  Item *item, *new_item;
6044
 
  res_selected_fields.clear();
6045
 
  res_all_fields.clear();
 
6337
  res_selected_fields.empty();
 
6338
  res_all_fields.empty();
6046
6339
 
6047
6340
  uint32_t i, border= all_fields.elements - elements;
6048
6341
  for (i= 0; (item= it++); i++)
6052
6345
      new_item;
6053
6346
  }
6054
6347
 
6055
 
  List<Item>::iterator itr(res_all_fields.begin());
 
6348
  List_iterator_fast<Item> itr(res_all_fields);
6056
6349
  for (i= 0; i < border; i++)
6057
6350
    itr++;
6058
6351
  itr.sublist(res_selected_fields, elements);
6135
6428
}
6136
6429
 
6137
6430
/** Copy result of functions to record in tmp_table. */
6138
 
bool copy_funcs(Item **func_ptr, const Session *session)
 
6431
void copy_funcs(Item **func_ptr)
6139
6432
{
6140
6433
  Item *func;
6141
6434
  for (; (func = *func_ptr) ; func_ptr++)
6142
 
  {
6143
6435
    func->save_in_result_field(1);
6144
 
    /*
6145
 
      Need to check the THD error state because Item::val_xxx() don't
6146
 
      return error code, but can generate errors
6147
 
      @todo change it for a real status check when Item::val_xxx()
6148
 
      are extended to return status code.
6149
 
    */
6150
 
    if (session->is_error())
6151
 
      return true;
6152
 
  }
6153
 
  return false;
6154
6436
}
6155
6437
 
6156
6438
/**
6202
6484
  @param changed        out:  returns 1 if item contains a replaced field item
6203
6485
 
6204
6486
  @todo
6205
 
    - @todo Some functions are not null-preserving. For those functions
 
6487
    - TODO: Some functions are not null-preserving. For those functions
6206
6488
    updating of the maybe_null attribute is an overkill.
6207
6489
 
6208
6490
  @retval
6210
6492
  @retval
6211
6493
    1   on error
6212
6494
*/
6213
 
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
 
6495
bool change_group_ref(Session *session, Item_func *expr, order_st *group_list, bool *changed)
6214
6496
{
6215
6497
  if (expr->arg_count)
6216
6498
  {
6217
 
    Name_resolution_context *context= &session->getLex()->current_select->context;
 
6499
    Name_resolution_context *context= &session->lex->current_select->context;
6218
6500
    Item **arg,**arg_end;
6219
6501
    bool arg_changed= false;
6220
6502
    for (arg= expr->arguments(),
6224
6506
      Item *item= *arg;
6225
6507
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6226
6508
      {
6227
 
        Order *group_tmp;
 
6509
        order_st *group_tmp;
6228
6510
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6229
6511
        {
6230
6512
          if (item->eq(*group_tmp->item,0))
6292
6574
                List<TableList> *tables, enum_query_type)
6293
6575
{
6294
6576
  /* 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*) *
 
6577
  List_iterator_fast<TableList> ti(*tables);
 
6578
  TableList **table= (TableList **)session->alloc(sizeof(TableList*) *
6297
6579
                                                tables->elements);
6298
6580
  if (table == 0)
6299
6581
    return;  // out of memory
6307
6589
void Select_Lex::print(Session *session, String *str, enum_query_type query_type)
6308
6590
{
6309
6591
  /* QQ: session may not be set for sub queries, but this should be fixed */
6310
 
  if(not session)
6311
 
    session= current_session;
6312
 
 
 
6592
  assert(session);
6313
6593
 
6314
6594
  str->append(STRING_WITH_LEN("select "));
6315
6595
 
6329
6609
 
6330
6610
  //Item List
6331
6611
  bool first= 1;
6332
 
  List<Item>::iterator it(item_list.begin());
 
6612
  List_iterator_fast<Item> it(item_list);
6333
6613
  Item *item;
6334
6614
  while ((item= it++))
6335
6615
  {
6342
6622
 
6343
6623
  /*
6344
6624
    from clause
6345
 
    @todo support USING/FORCE/IGNORE index
 
6625
    TODO: support USING/FORCE/IGNORE index
6346
6626
  */
6347
6627
  if (table_list.elements)
6348
6628
  {
6376
6656
  if (group_list.elements)
6377
6657
  {
6378
6658
    str->append(STRING_WITH_LEN(" group by "));
6379
 
    print_order(str, (Order *) group_list.first, query_type);
 
6659
    print_order(str, (order_st *) group_list.first, query_type);
6380
6660
    switch (olap)
6381
6661
    {
6382
6662
      case CUBE_TYPE:
6407
6687
  if (order_list.elements)
6408
6688
  {
6409
6689
    str->append(STRING_WITH_LEN(" order by "));
6410
 
    print_order(str, (Order *) order_list.first, query_type);
 
6690
    print_order(str, (order_st *) order_list.first, query_type);
6411
6691
  }
6412
6692
 
6413
6693
  // limit