~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Brian Aker
  • Date: 2010-11-22 00:16:44 UTC
  • mto: (1945.2.1 quick)
  • mto: This revision was merged to the branch mainline in revision 1947.
  • Revision ID: brian@tangent.org-20101122001644-pi6jv0d65e82xn38
Merge in lock refactor, this just encapsulates.

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
16
/**
17
17
  @file
49
49
#include "drizzled/lock.h"
50
50
#include "drizzled/item/outer_ref.h"
51
51
#include "drizzled/index_hint.h"
52
 
#include "drizzled/memory/multi_malloc.h"
53
52
#include "drizzled/records.h"
54
53
#include "drizzled/internal/iocache.h"
 
54
#include "drizzled/drizzled.h"
55
55
 
56
56
#include "drizzled/sql_union.h"
57
57
#include "drizzled/optimizer/key_field.h"
62
62
#include "drizzled/optimizer/quick_range_select.h"
63
63
#include "drizzled/optimizer/quick_ror_intersect_select.h"
64
64
 
 
65
#include "drizzled/filesort.h"
 
66
 
65
67
using namespace std;
66
68
 
67
69
namespace drizzled
115
117
{
116
118
  bool res;
117
119
  register Select_Lex *select_lex= &lex->select_lex;
118
 
  DRIZZLE_SELECT_START(session->query.c_str());
 
120
  DRIZZLE_SELECT_START(session->getQueryString()->c_str());
119
121
 
120
122
  if (select_lex->master_unit()->is_union() ||
121
123
      select_lex->master_unit()->fake_select_lex)
 
124
  {
122
125
    res= drizzle_union(session, lex, result, &lex->unit,
123
126
                       setup_tables_done_option);
 
127
  }
124
128
  else
125
129
  {
126
130
    Select_Lex_Unit *unit= &lex->unit;
137
141
                      select_lex->where,
138
142
                      select_lex->order_list.elements +
139
143
                      select_lex->group_list.elements,
140
 
                      (order_st*) select_lex->order_list.first,
141
 
                      (order_st*) select_lex->group_list.first,
 
144
                      (Order*) select_lex->order_list.first,
 
145
                      (Order*) select_lex->group_list.first,
142
146
                      select_lex->having,
143
147
                      select_lex->options | session->options |
144
148
                      setup_tables_done_option,
351
355
                  List<Item> &fields,
352
356
                  COND *conds, 
353
357
                  uint32_t og_num,  
354
 
                  order_st *order, 
355
 
                  order_st *group,
 
358
                  Order *order,
 
359
                  Order *group,
356
360
                  Item *having, 
357
361
                  uint64_t select_options,
358
362
                  select_result *result, 
363
367
  bool free_join= 1;
364
368
 
365
369
  select_lex->context.resolve_in_select_list= true;
366
 
  JOIN *join;
 
370
  Join *join;
367
371
  if (select_lex->join != 0)
368
372
  {
369
373
    join= select_lex->join;
396
400
  }
397
401
  else
398
402
  {
399
 
    if (!(join= new JOIN(session, fields, select_options, result)))
 
403
    if (!(join= new Join(session, fields, select_options, result)))
400
404
      return(true);
401
405
    session->set_proc_info("init");
402
406
    session->used_tables=0;                         // Updated by setup_fields
446
450
  return (cond? (new Item_cond_and(cond, item)) : item);
447
451
}
448
452
 
449
 
static void fix_list_after_tbl_changes(Select_Lex *new_parent, List<TableList> *tlist)
450
 
{
451
 
  List_iterator<TableList> it(*tlist);
452
 
  TableList *table;
453
 
  while ((table= it++))
454
 
  {
455
 
    if (table->on_expr)
456
 
      table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
457
 
    if (table->nested_join)
458
 
      fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
459
 
  }
460
 
}
461
 
 
462
453
/*****************************************************************************
463
454
  Create JoinTableS, make a guess about the table types,
464
455
  Approximate how many records will be used in each table
554
545
                         Select_Lex *select_lex,
555
546
                         vector<optimizer::SargableParam> &sargables)
556
547
{
557
 
  uint  and_level,i,found_eq_constant;
 
548
  uint  and_level,found_eq_constant;
558
549
  optimizer::KeyField *key_fields, *end, *field;
559
550
  uint32_t sz;
560
551
  uint32_t m= max(select_lex->max_equal_elems,(uint32_t)1);
577
568
    substitutions.
578
569
  */
579
570
  sz= sizeof(optimizer::KeyField) *
580
 
      (((session->lex->current_select->cond_count+1) +
 
571
      (((session->lex->current_select->cond_count+1)*2 +
581
572
        session->lex->current_select->between_count)*m+1);
582
573
  if (! (key_fields= (optimizer::KeyField*) session->alloc(sz)))
583
574
    return true;
597
588
      if (field->getValue()->type() == Item::NULL_ITEM &&
598
589
          ! field->getField()->real_maybe_null())
599
590
      {
600
 
        field->getField()->table->reginfo.not_exists_optimize= 1;
 
591
        field->getField()->getTable()->reginfo.not_exists_optimize= 1;
601
592
      }
602
593
    }
603
594
  }
604
 
  for (i= 0; i < tables; i++)
 
595
  for (uint32_t i= 0; i < tables; i++)
605
596
  {
606
597
    /*
607
598
      Block the creation of keys for inner tables of outer joins.
624
615
    TableList *table;
625
616
    while ((table= li++))
626
617
    {
627
 
      if (table->nested_join)
 
618
      if (table->getNestedJoin())
628
619
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
629
620
                              sargables);
630
621
    }
656
647
    use= save_pos= dynamic_element(keyuse, 0, optimizer::KeyUse*);
657
648
    prev= &key_end;
658
649
    found_eq_constant= 0;
659
 
    for (i= 0; i < keyuse->elements-1; i++, use++)
660
650
    {
661
 
      if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
662
 
        use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
663
 
      if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
 
651
      uint32_t i;
 
652
 
 
653
      for (i= 0; i < keyuse->elements-1; i++, use++)
664
654
      {
665
 
        if (prev->getKeypart() + 1 < use->getKeypart() || 
666
 
            ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
667
 
          continue;                             /* remove */
 
655
        if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
 
656
          use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
 
657
        if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
 
658
        {
 
659
          if (prev->getKeypart() + 1 < use->getKeypart() || 
 
660
              ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
 
661
            continue;                           /* remove */
 
662
        }
 
663
        else if (use->getKeypart() != 0)                // First found must be 0
 
664
          continue;
 
665
 
 
666
#ifdef HAVE_VALGRIND
 
667
        /* Valgrind complains about overlapped memcpy when save_pos==use. */
 
668
        if (save_pos != use)
 
669
#endif
 
670
          *save_pos= *use;
 
671
        prev=use;
 
672
        found_eq_constant= ! use->getUsedTables();
 
673
        /* Save ptr to first use */
 
674
        if (! use->getTable()->reginfo.join_tab->keyuse)
 
675
          use->getTable()->reginfo.join_tab->keyuse= save_pos;
 
676
        use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
 
677
        save_pos++;
668
678
      }
669
 
      else if (use->getKeypart() != 0)          // First found must be 0
670
 
        continue;
671
 
 
672
 
#ifdef HAVE_purify
673
 
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
674
 
      if (save_pos != use)
675
 
#endif
676
 
        *save_pos= *use;
677
 
      prev=use;
678
 
      found_eq_constant= ! use->getUsedTables();
679
 
      /* Save ptr to first use */
680
 
      if (! use->getTable()->reginfo.join_tab->keyuse)
681
 
        use->getTable()->reginfo.join_tab->keyuse= save_pos;
682
 
      use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
683
 
      save_pos++;
 
679
      i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
 
680
      set_dynamic(keyuse, (unsigned char*) &key_end, i);
 
681
      keyuse->elements= i;
684
682
    }
685
 
    i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
686
 
    set_dynamic(keyuse, (unsigned char*) &key_end, i);
687
 
    keyuse->elements= i;
688
683
  }
689
684
  return false;
690
685
}
692
687
/**
693
688
  Update some values in keyuse for faster choose_plan() loop.
694
689
*/
695
 
void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
 
690
void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array)
696
691
{
697
692
  optimizer::KeyUse *end,*keyuse= dynamic_element(keyuse_array, 
698
693
                                                  0, 
747
742
  @return
748
743
    None
749
744
*/
750
 
void add_group_and_distinct_keys(JOIN *join, JoinTable *join_tab)
 
745
void add_group_and_distinct_keys(Join *join, JoinTable *join_tab)
751
746
{
752
747
  List<Item_field> indexed_fields;
753
748
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
754
 
  order_st      *cur_group;
 
749
  Order      *cur_group;
755
750
  Item_field *cur_item;
756
751
  key_map possible_keys(0);
757
752
 
852
847
  Field **f_ptr,*field;
853
848
 
854
849
  null_fields= blobs= fields= rec_length=0;
855
 
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
 
850
  for (f_ptr=join_tab->table->getFields() ; (field= *f_ptr) ; f_ptr++)
856
851
  {
857
852
    if (field->isReadSet())
858
853
    {
883
878
StoredKey *get_store_key(Session *session,
884
879
                         optimizer::KeyUse *keyuse,
885
880
                         table_map used_tables,
886
 
                         KEY_PART_INFO *key_part,
 
881
                         KeyPartInfo *key_part,
887
882
                         unsigned char *key_buff,
888
883
                         uint32_t maybe_null)
889
884
{
928
923
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
929
924
{
930
925
  bool error;
931
 
  Table *table= field->table;
 
926
  Table *table= field->getTable();
932
927
  Session *session= table->in_use;
933
928
  ha_rows cuted_fields=session->cuted_fields;
934
929
 
959
954
    *e1= e2;
960
955
}
961
956
 
962
 
bool create_ref_for_key(JOIN *join, 
 
957
bool create_ref_for_key(Join *join, 
963
958
                        JoinTable *j, 
964
959
                        optimizer::KeyUse *org_keyuse,
965
960
                        table_map used_tables)
970
965
  uint32_t length;
971
966
  uint32_t key;
972
967
  Table *table= NULL;
973
 
  KEY *keyinfo= NULL;
 
968
  KeyInfo *keyinfo= NULL;
974
969
 
975
970
  /*  Use best key from find_best */
976
971
  table= j->table;
1137
1132
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
1138
1133
         appropiate JoinTable members.
1139
1134
*/
1140
 
void add_not_null_conds(JOIN *join)
 
1135
void add_not_null_conds(Join *join)
1141
1136
{
1142
1137
  for (uint32_t i= join->const_tables; i < join->tables; i++)
1143
1138
  {
1154
1149
          Item *notnull;
1155
1150
          assert(item->type() == Item::FIELD_ITEM);
1156
1151
          Item_field *not_null_item= (Item_field*)item;
1157
 
          JoinTable *referred_tab= not_null_item->field->table->reginfo.join_tab;
 
1152
          JoinTable *referred_tab= not_null_item->field->getTable()->reginfo.join_tab;
1158
1153
          /*
1159
1154
            For UPDATE queries such as:
1160
1155
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
1211
1206
  return tmp;
1212
1207
}
1213
1208
 
1214
 
/*
1215
 
  Check if given expression uses only table fields covered by the given index
1216
 
 
1217
 
  SYNOPSIS
1218
 
    uses_index_fields_only()
1219
 
      item           Expression to check
1220
 
      tbl            The table having the index
1221
 
      keyno          The index number
1222
 
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
1223
 
 
1224
 
  DESCRIPTION
1225
 
    Check if given expression only uses fields covered by index #keyno in the
1226
 
    table tbl. The expression can use any fields in any other tables.
1227
 
 
1228
 
    The expression is guaranteed not to be AND or OR - those constructs are
1229
 
    handled outside of this function.
1230
 
 
1231
 
  RETURN
1232
 
    true   Yes
1233
 
    false  No
1234
 
*/
1235
 
static bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno, bool other_tbls_ok)
1236
 
{
1237
 
  if (item->const_item())
1238
 
    return true;
1239
 
 
1240
 
  /*
1241
 
    Don't push down the triggered conditions. Nested outer joins execution
1242
 
    code may need to evaluate a condition several times (both triggered and
1243
 
    untriggered), and there is no way to put thi
1244
 
    TODO: Consider cloning the triggered condition and using the copies for:
1245
 
      1. push the first copy down, to have most restrictive index condition
1246
 
         possible
1247
 
      2. Put the second copy into tab->select_cond.
1248
 
  */
1249
 
  if (item->type() == Item::FUNC_ITEM &&
1250
 
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
1251
 
    return false;
1252
 
 
1253
 
  if (!(item->used_tables() & tbl->map))
1254
 
    return other_tbls_ok;
1255
 
 
1256
 
  Item::Type item_type= item->type();
1257
 
  switch (item_type) {
1258
 
  case Item::FUNC_ITEM:
1259
 
    {
1260
 
      /* This is a function, apply condition recursively to arguments */
1261
 
      Item_func *item_func= (Item_func*)item;
1262
 
      Item **child;
1263
 
      Item **item_end= (item_func->arguments()) + item_func->argument_count();
1264
 
      for (child= item_func->arguments(); child != item_end; child++)
1265
 
      {
1266
 
        if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
1267
 
          return false;
1268
 
      }
1269
 
      return true;
1270
 
    }
1271
 
  case Item::COND_ITEM:
1272
 
    {
1273
 
      /* This is a function, apply condition recursively to arguments */
1274
 
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
1275
 
      Item *list_item;
1276
 
      while ((list_item=li++))
1277
 
      {
1278
 
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
1279
 
          return false;
1280
 
      }
1281
 
      return true;
1282
 
    }
1283
 
  case Item::FIELD_ITEM:
1284
 
    {
1285
 
      Item_field *item_field= (Item_field*)item;
1286
 
      if (item_field->field->table != tbl)
1287
 
        return true;
1288
 
      return item_field->field->part_of_key.test(keyno);
1289
 
    }
1290
 
  case Item::REF_ITEM:
1291
 
    return uses_index_fields_only(item->real_item(), tbl, keyno,
1292
 
                                  other_tbls_ok);
1293
 
  default:
1294
 
    return false; /* Play it safe, don't push unknown non-const items */
1295
 
  }
1296
 
}
1297
 
 
1298
1209
#define ICP_COND_USES_INDEX_ONLY 10
1299
1210
 
1300
 
/*
1301
 
  Get a part of the condition that can be checked using only index fields
1302
 
 
1303
 
  SYNOPSIS
1304
 
    make_cond_for_index()
1305
 
      cond           The source condition
1306
 
      table          The table that is partially available
1307
 
      keyno          The index in the above table. Only fields covered by the index
1308
 
                     are available
1309
 
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
1310
 
 
1311
 
  DESCRIPTION
1312
 
    Get a part of the condition that can be checked when for the given table
1313
 
    we have values only of fields covered by some index. The condition may
1314
 
    refer to other tables, it is assumed that we have values of all of their
1315
 
    fields.
1316
 
 
1317
 
    Example:
1318
 
      make_cond_for_index(
1319
 
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
1320
 
          t2, keyno(t2.key1))
1321
 
      will return
1322
 
        "cond(t1.field) AND cond(t2.key2)"
1323
 
 
1324
 
  RETURN
1325
 
    Index condition, or NULL if no condition could be inferred.
1326
 
*/
1327
 
static Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno, bool other_tbls_ok)
1328
 
{
1329
 
  if (!cond)
1330
 
    return NULL;
1331
 
  if (cond->type() == Item::COND_ITEM)
1332
 
  {
1333
 
    uint32_t n_marked= 0;
1334
 
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
1335
 
    {
1336
 
      Item_cond_and *new_cond=new Item_cond_and;
1337
 
      if (!new_cond)
1338
 
        return (COND*) 0;
1339
 
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1340
 
      Item *item;
1341
 
      while ((item=li++))
1342
 
      {
1343
 
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
1344
 
        if (fix)
1345
 
          new_cond->argument_list()->push_back(fix);
1346
 
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
1347
 
      }
1348
 
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
1349
 
        cond->marker= ICP_COND_USES_INDEX_ONLY;
1350
 
      switch (new_cond->argument_list()->elements) {
1351
 
      case 0:
1352
 
        return (COND*) 0;
1353
 
      case 1:
1354
 
        return new_cond->argument_list()->head();
1355
 
      default:
1356
 
        new_cond->quick_fix_field();
1357
 
        return new_cond;
1358
 
      }
1359
 
    }
1360
 
    else /* It's OR */
1361
 
    {
1362
 
      Item_cond_or *new_cond=new Item_cond_or;
1363
 
      if (!new_cond)
1364
 
        return (COND*) 0;
1365
 
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1366
 
      Item *item;
1367
 
      while ((item=li++))
1368
 
      {
1369
 
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
1370
 
        if (!fix)
1371
 
          return (COND*) 0;
1372
 
        new_cond->argument_list()->push_back(fix);
1373
 
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
1374
 
      }
1375
 
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
1376
 
        cond->marker= ICP_COND_USES_INDEX_ONLY;
1377
 
      new_cond->quick_fix_field();
1378
 
      new_cond->top_level_item();
1379
 
      return new_cond;
1380
 
    }
1381
 
  }
1382
 
 
1383
 
  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
1384
 
    return (COND*) 0;
1385
 
  cond->marker= ICP_COND_USES_INDEX_ONLY;
1386
 
  return cond;
1387
 
}
1388
 
 
1389
 
 
1390
 
static Item *make_cond_remainder(Item *cond, bool exclude_index)
1391
 
{
1392
 
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
1393
 
    return 0; /* Already checked */
1394
 
 
1395
 
  if (cond->type() == Item::COND_ITEM)
1396
 
  {
1397
 
    table_map tbl_map= 0;
1398
 
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
1399
 
    {
1400
 
      /* Create new top level AND item */
1401
 
      Item_cond_and *new_cond=new Item_cond_and;
1402
 
      if (!new_cond)
1403
 
        return (COND*) 0;
1404
 
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1405
 
      Item *item;
1406
 
      while ((item=li++))
1407
 
      {
1408
 
        Item *fix= make_cond_remainder(item, exclude_index);
1409
 
        if (fix)
1410
 
        {
1411
 
          new_cond->argument_list()->push_back(fix);
1412
 
          tbl_map |= fix->used_tables();
1413
 
        }
1414
 
      }
1415
 
      switch (new_cond->argument_list()->elements) {
1416
 
      case 0:
1417
 
        return (COND*) 0;
1418
 
      case 1:
1419
 
        return new_cond->argument_list()->head();
1420
 
      default:
1421
 
        new_cond->quick_fix_field();
1422
 
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
1423
 
        return new_cond;
1424
 
      }
1425
 
    }
1426
 
    else /* It's OR */
1427
 
    {
1428
 
      Item_cond_or *new_cond=new Item_cond_or;
1429
 
      if (!new_cond)
1430
 
        return (COND*) 0;
1431
 
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1432
 
      Item *item;
1433
 
      while ((item=li++))
1434
 
      {
1435
 
        Item *fix= make_cond_remainder(item, false);
1436
 
        if (!fix)
1437
 
          return (COND*) 0;
1438
 
        new_cond->argument_list()->push_back(fix);
1439
 
        tbl_map |= fix->used_tables();
1440
 
      }
1441
 
      new_cond->quick_fix_field();
1442
 
      ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
1443
 
      new_cond->top_level_item();
1444
 
      return new_cond;
1445
 
    }
1446
 
  }
1447
 
  return cond;
1448
 
}
1449
1211
 
1450
1212
/**
1451
1213
  cleanup JoinTable.
1457
1219
  delete quick;
1458
1220
  quick= 0;
1459
1221
  if (cache.buff)
 
1222
  {
 
1223
    size_t size= cache.end - cache.buff;
 
1224
    global_join_buffer.sub(size);
1460
1225
    free(cache.buff);
 
1226
  }
1461
1227
  cache.buff= 0;
1462
1228
  limit= 0;
1463
1229
  if (table)
1474
1240
    */
1475
1241
    table->reginfo.join_tab= 0;
1476
1242
  }
1477
 
  end_read_record(&read_record);
 
1243
  read_record.end_read_record();
1478
1244
}
1479
1245
 
1480
 
bool only_eq_ref_tables(JOIN *join,order_st *order,table_map tables)
 
1246
bool only_eq_ref_tables(Join *join,Order *order,table_map tables)
1481
1247
{
1482
1248
  for (JoinTable **tab=join->map2table ; tables ; tab++, tables>>=1)
1483
1249
  {
1506
1272
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
1507
1273
  @endcode
1508
1274
*/
1509
 
bool eq_ref_table(JOIN *join, order_st *start_order, JoinTable *tab)
 
1275
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
1510
1276
{
1511
1277
  if (tab->cached_eq_ref_table)                 // If cached
1512
1278
    return tab->eq_ref_table;
1525
1291
  {
1526
1292
    if (! (*ref_item)->const_item())
1527
1293
    {                                           // Not a const ref
1528
 
      order_st *order;
 
1294
      Order *order;
1529
1295
      for (order=start_order ; order ; order=order->next)
1530
1296
      {
1531
1297
        if ((*ref_item)->eq(order->item[0],0))
2265
2031
    {
2266
2032
      if (table->on_expr)
2267
2033
      {
2268
 
        List<TableList> *nested_join_list= table->nested_join ?
2269
 
          &table->nested_join->join_list : NULL;
 
2034
        List<TableList> *nested_join_list= table->getNestedJoin() ?
 
2035
          &table->getNestedJoin()->join_list : NULL;
2270
2036
        /*
2271
2037
          We can modify table->on_expr because its old value will
2272
2038
          be restored before re-execution of PS/SP.
2319
2085
  if (outer_ref)
2320
2086
    return cmp;
2321
2087
  JoinTable **idx= (JoinTable **) table_join_idx;
2322
 
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
 
2088
  cmp= idx[field2->field->getTable()->tablenr]-idx[field1->field->getTable()->tablenr];
2323
2089
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
2324
2090
}
2325
2091
 
2566
2332
      while ((item_field= it++))
2567
2333
      {
2568
2334
        Field *field= item_field->field;
2569
 
        JoinTable *stat= field->table->reginfo.join_tab;
 
2335
        JoinTable *stat= field->getTable()->reginfo.join_tab;
2570
2336
        key_map possible_keys= field->key_start;
2571
 
        possible_keys&= field->table->keys_in_use_for_query;
 
2337
        possible_keys&= field->getTable()->keys_in_use_for_query;
2572
2338
        stat[0].const_keys|= possible_keys;
2573
2339
 
2574
2340
        /*
2578
2344
        */
2579
2345
        if (possible_keys.any())
2580
2346
        {
2581
 
          Table *field_tab= field->table;
 
2347
          Table *field_tab= field->getTable();
2582
2348
          optimizer::KeyUse *use;
2583
2349
          for (use= stat->keyuse; use && use->getTable() == field_tab; use++)
2584
2350
            if (possible_keys.test(use->getKey()) &&
2627
2393
       left_item->collation.collation == value->collation.collation))
2628
2394
  {
2629
2395
    Item *tmp=value->clone_item();
2630
 
    tmp->collation.set(right_item->collation);
2631
 
 
2632
2396
    if (tmp)
2633
2397
    {
 
2398
      tmp->collation.set(right_item->collation);
2634
2399
      session->change_item_tree(args + 1, tmp);
2635
2400
      func->update_used_tables();
2636
2401
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2650
2415
            right_item->collation.collation == value->collation.collation))
2651
2416
  {
2652
2417
    Item *tmp= value->clone_item();
2653
 
    tmp->collation.set(left_item->collation);
2654
 
 
2655
2418
    if (tmp)
2656
2419
    {
 
2420
      tmp->collation.set(left_item->collation);
2657
2421
      session->change_item_tree(args, tmp);
2658
2422
      value= tmp;
2659
2423
      func->update_used_tables();
2845
2609
  @endverbatim
2846
2610
 
2847
2611
  @param join       Join being processed
2848
 
  @param last_tab   Last table in current partial join order (this function is
2849
 
                    not called for empty partial join orders)
2850
2612
  @param next_tab   Table we're going to extend the current partial join with
2851
2613
 
2852
2614
  @retval
2855
2617
  @retval
2856
2618
    true   Requested join order extension not allowed.
2857
2619
*/
2858
 
bool check_interleaving_with_nj(JoinTable *last_tab, JoinTable *next_tab)
 
2620
bool check_interleaving_with_nj(JoinTable *next_tab)
2859
2621
{
2860
 
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
2861
 
  JOIN *join= last_tab->join;
 
2622
  TableList *next_emb= next_tab->table->pos_in_table_list->getEmbedding();
 
2623
  Join *join= next_tab->join;
2862
2624
 
2863
2625
  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
2864
2626
  {
2873
2635
    Do update counters for "pairs of brackets" that we've left (marked as
2874
2636
    X,Y,Z in the above picture)
2875
2637
  */
2876
 
  for (;next_emb; next_emb= next_emb->embedding)
 
2638
  for (;next_emb; next_emb= next_emb->getEmbedding())
2877
2639
  {
2878
 
    next_emb->nested_join->counter_++;
2879
 
    if (next_emb->nested_join->counter_ == 1)
 
2640
    next_emb->getNestedJoin()->counter_++;
 
2641
    if (next_emb->getNestedJoin()->counter_ == 1)
2880
2642
    {
2881
2643
      /*
2882
2644
        next_emb is the first table inside a nested join we've "entered". In
2883
2645
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
2884
2646
        X bracket might have Y pair bracket.
2885
2647
      */
2886
 
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
 
2648
      join->cur_embedding_map |= next_emb->getNestedJoin()->nj_map;
2887
2649
    }
2888
2650
 
2889
 
    if (next_emb->nested_join->join_list.elements !=
2890
 
        next_emb->nested_join->counter_)
 
2651
    if (next_emb->getNestedJoin()->join_list.elements !=
 
2652
        next_emb->getNestedJoin()->counter_)
2891
2653
      break;
2892
2654
 
2893
2655
    /*
2894
2656
      We're currently at Y or Z-bracket as depicted in the above picture.
2895
2657
      Mark that we've left it and continue walking up the brackets hierarchy.
2896
2658
    */
2897
 
    join->cur_embedding_map &= ~next_emb->nested_join->nj_map;
 
2659
    join->cur_embedding_map &= ~next_emb->getNestedJoin()->nj_map;
2898
2660
  }
2899
2661
  return false;
2900
2662
}
2901
2663
 
2902
 
COND *optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
 
2664
COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
2903
2665
{
2904
2666
  Session *session= join->session;
2905
2667
 
3022
2784
    {
3023
2785
      Field *field= ((Item_field*) args[0])->field;
3024
2786
      if (field->flags & AUTO_INCREMENT_FLAG 
3025
 
          && ! field->table->maybe_null 
 
2787
          && ! field->getTable()->maybe_null 
3026
2788
          && session->options & OPTION_AUTO_IS_NULL
3027
2789
          && (
3028
2790
            session->first_successful_insert_id_in_prev_stmt > 0 
3080
2842
  /*
3081
2843
    TODO:
3082
2844
    Excluding all expensive functions is too restritive we should exclude only
3083
 
    materialized IN because it is created later than this phase, and cannot be
3084
 
    evaluated at this point.
3085
 
    The condition should be something as (need to fix member access):
3086
 
      !(cond->type() == Item::FUNC_ITEM &&
3087
 
        ((Item_func*)cond)->func_name() == "<in_optimizer>" &&
3088
 
        ((Item_in_optimizer*)cond)->is_expensive()))
 
2845
    materialized IN subquery predicates because they can't yet be evaluated
 
2846
    here (they need additional initialization that is done later on).
 
2847
 
 
2848
    The proper way to exclude the subqueries would be to walk the cond tree and
 
2849
    check for materialized subqueries there.
 
2850
 
3089
2851
  */
3090
2852
  {
3091
2853
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
3208
2970
  @return
3209
2971
    end_select function to use. This function can't fail.
3210
2972
*/
3211
 
Next_select_func setup_end_select_func(JOIN *join)
 
2973
Next_select_func setup_end_select_func(Join *join)
3212
2974
{
3213
2975
  Table *table= join->tmp_table;
3214
2976
  Tmp_Table_Param *tmp_tbl= &join->tmp_table_param;
3220
2982
    if (table->group && tmp_tbl->sum_func_count &&
3221
2983
        !tmp_tbl->precomputed_group_by)
3222
2984
    {
3223
 
      if (table->s->keys)
 
2985
      if (table->getShare()->sizeKeys())
3224
2986
      {
3225
2987
        end_select= end_update;
3226
2988
      }
3231
2993
    }
3232
2994
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
3233
2995
    {
3234
 
      end_select=end_write_group;
 
2996
      end_select= end_write_group;
3235
2997
    }
3236
2998
    else
3237
2999
    {
3238
 
      end_select=end_write;
 
3000
      end_select= end_write;
3239
3001
      if (tmp_tbl->precomputed_group_by)
3240
3002
      {
3241
3003
        /*
3273
3035
  @retval
3274
3036
    -1  if error should be sent
3275
3037
*/
3276
 
int do_select(JOIN *join, List<Item> *fields, Table *table)
 
3038
int do_select(Join *join, List<Item> *fields, Table *table)
3277
3039
{
3278
3040
  int rc= 0;
3279
3041
  enum_nested_loop_state error= NESTED_LOOP_OK;
3287
3049
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
3288
3050
    table->emptyRecord();
3289
3051
    if (table->group && join->tmp_table_param.sum_func_count &&
3290
 
        table->s->keys && !table->cursor->inited)
3291
 
      table->cursor->ha_index_init(0, 0);
 
3052
        table->getShare()->sizeKeys() && !table->cursor->inited)
 
3053
      table->cursor->startIndexScan(0, 0);
3292
3054
  }
3293
3055
  /* Set up select_end */
3294
3056
  Next_select_func end_select= setup_end_select_func(join);
3374
3136
  return(join->session->is_error() ? -1 : rc);
3375
3137
}
3376
3138
 
3377
 
enum_nested_loop_state sub_select_cache(JOIN *join, JoinTable *join_tab, bool end_of_records)
 
3139
enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records)
3378
3140
{
3379
3141
  enum_nested_loop_state rc;
3380
3142
 
3385
3147
      rc= sub_select(join,join_tab,end_of_records);
3386
3148
    return rc;
3387
3149
  }
3388
 
  if (join->session->killed)            // If aborted by user
 
3150
  if (join->session->getKilled())               // If aborted by user
3389
3151
  {
3390
3152
    join->session->send_kill_message();
3391
3153
    return NESTED_LOOP_KILLED;
3392
3154
  }
3393
3155
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
3394
3156
  {
3395
 
    if (! store_record_in_cache(&join_tab->cache))
 
3157
    if (! join_tab->cache.store_record_in_cache())
3396
3158
      return NESTED_LOOP_OK;                     // There is more room in cache
3397
3159
    return flush_cached_records(join,join_tab,false);
3398
3160
  }
3521
3283
  @return
3522
3284
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
3523
3285
*/
3524
 
enum_nested_loop_state sub_select(JOIN *join, JoinTable *join_tab, bool end_of_records)
 
3286
enum_nested_loop_state sub_select(Join *join, JoinTable *join_tab, bool end_of_records)
3525
3287
{
3526
3288
  join_tab->table->null_row=0;
3527
3289
  if (end_of_records)
3529
3291
 
3530
3292
  int error;
3531
3293
  enum_nested_loop_state rc;
3532
 
  READ_RECORD *info= &join_tab->read_record;
 
3294
  ReadRecord *info= &join_tab->read_record;
3533
3295
 
3534
3296
  if (join->resume_nested_loop)
3535
3297
  {
3586
3348
{
3587
3349
  int error;
3588
3350
  Table *table= tab->table;
3589
 
  if ((error=table->cursor->index_read_map(table->record[0],
 
3351
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
3590
3352
                                         tab->ref.key_buff,
3591
3353
                                         make_prev_keypart_map(tab->ref.key_parts),
3592
3354
                                         HA_READ_KEY_EXACT)))
3650
3412
    table->maybe_null=0;
3651
3413
 
3652
3414
  /* Check appearance of new constant items in Item_equal objects */
3653
 
  JOIN *join= tab->join;
 
3415
  Join *join= tab->join;
3654
3416
  if (join->conds)
3655
3417
    update_const_equal_items(join->conds, tab);
3656
3418
  TableList *tbl;
3663
3425
      embedded= embedding;
3664
3426
      if (embedded->on_expr)
3665
3427
         update_const_equal_items(embedded->on_expr, tab);
3666
 
      embedding= embedded->embedding;
 
3428
      embedding= embedded->getEmbedding();
3667
3429
    }
3668
3430
    while (embedding &&
3669
 
           embedding->nested_join->join_list.head() == embedded);
 
3431
           embedding->getNestedJoin()->join_list.head() == embedded);
3670
3432
  }
3671
3433
 
3672
3434
  return(0);
3678
3440
  int error;
3679
3441
  if (table->status & STATUS_GARBAGE)           // If first read
3680
3442
  {
3681
 
    if ((error=table->cursor->read_first_row(table->record[0],
3682
 
                                           table->s->primary_key)))
 
3443
    if ((error=table->cursor->read_first_row(table->getInsertRecord(),
 
3444
                                           table->getShare()->getPrimaryKey())))
3683
3445
    {
3684
3446
      if (error != HA_ERR_END_OF_FILE)
3685
3447
        return table->report_error(error);
3718
3480
      error= HA_ERR_KEY_NOT_FOUND;
3719
3481
    else
3720
3482
    {
3721
 
      error=table->cursor->index_read_idx_map(table->record[0],tab->ref.key,
 
3483
      error=table->cursor->index_read_idx_map(table->getInsertRecord(),tab->ref.key,
3722
3484
                                            (unsigned char*) tab->ref.key_buff,
3723
3485
                                            make_prev_keypart_map(tab->ref.key_parts),
3724
3486
                                            HA_READ_KEY_EXACT);
3766
3528
 
3767
3529
  if (!table->cursor->inited)
3768
3530
  {
3769
 
    table->cursor->ha_index_init(tab->ref.key, tab->sorted);
 
3531
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3770
3532
  }
3771
3533
 
3772
3534
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
3778
3540
      table->status=STATUS_NOT_FOUND;
3779
3541
      return -1;
3780
3542
    }
3781
 
    error=table->cursor->index_read_map(table->record[0],
 
3543
    error=table->cursor->index_read_map(table->getInsertRecord(),
3782
3544
                                      tab->ref.key_buff,
3783
3545
                                      make_prev_keypart_map(tab->ref.key_parts),
3784
3546
                                      HA_READ_KEY_EXACT);
3814
3576
 
3815
3577
  /* Initialize the index first */
3816
3578
  if (!table->cursor->inited)
3817
 
    table->cursor->ha_index_init(tab->ref.key, tab->sorted);
 
3579
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3818
3580
 
3819
3581
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3820
3582
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
3825
3587
 
3826
3588
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3827
3589
    return -1;
3828
 
  if ((error=table->cursor->index_read_map(table->record[0],
 
3590
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
3829
3591
                                         tab->ref.key_buff,
3830
3592
                                         make_prev_keypart_map(tab->ref.key_parts),
3831
3593
                                         HA_READ_KEY_EXACT)))
3848
3610
  Table *table= tab->table;
3849
3611
 
3850
3612
  if (!table->cursor->inited)
3851
 
    table->cursor->ha_index_init(tab->ref.key, tab->sorted);
 
3613
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3852
3614
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3853
3615
    return -1;
3854
 
  if ((error=table->cursor->index_read_last_map(table->record[0],
 
3616
  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
3855
3617
                                              tab->ref.key_buff,
3856
3618
                                              make_prev_keypart_map(tab->ref.key_parts))))
3857
3619
  {
3862
3624
  return 0;
3863
3625
}
3864
3626
 
3865
 
int join_no_more_records(READ_RECORD *)
 
3627
int join_no_more_records(ReadRecord *)
3866
3628
{
3867
3629
  return -1;
3868
3630
}
3869
3631
 
3870
 
int join_read_next_same_diff(READ_RECORD *info)
 
3632
int join_read_next_same_diff(ReadRecord *info)
3871
3633
{
3872
3634
  Table *table= info->table;
3873
3635
  JoinTable *tab=table->reginfo.join_tab;
3874
3636
  if (tab->insideout_match_tab->found_match)
3875
3637
  {
3876
 
    KEY *key= tab->table->key_info + tab->index;
 
3638
    KeyInfo *key= tab->table->key_info + tab->index;
3877
3639
    do
3878
3640
    {
3879
3641
      int error;
3880
3642
      /* Save index tuple from record to the buffer */
3881
3643
      key_copy(tab->insideout_buf, info->record, key, 0);
3882
3644
 
3883
 
      if ((error=table->cursor->index_next_same(table->record[0],
 
3645
      if ((error=table->cursor->index_next_same(table->getInsertRecord(),
3884
3646
                                              tab->ref.key_buff,
3885
3647
                                              tab->ref.key_length)))
3886
3648
      {
3898
3660
    return join_read_next_same(info);
3899
3661
}
3900
3662
 
3901
 
int join_read_next_same(READ_RECORD *info)
 
3663
int join_read_next_same(ReadRecord *info)
3902
3664
{
3903
3665
  int error;
3904
3666
  Table *table= info->table;
3905
3667
  JoinTable *tab=table->reginfo.join_tab;
3906
3668
 
3907
 
  if ((error=table->cursor->index_next_same(table->record[0],
 
3669
  if ((error=table->cursor->index_next_same(table->getInsertRecord(),
3908
3670
                                          tab->ref.key_buff,
3909
3671
                                          tab->ref.key_length)))
3910
3672
  {
3917
3679
  return 0;
3918
3680
}
3919
3681
 
3920
 
int join_read_prev_same(READ_RECORD *info)
 
3682
int join_read_prev_same(ReadRecord *info)
3921
3683
{
3922
3684
  int error;
3923
3685
  Table *table= info->table;
3924
3686
  JoinTable *tab=table->reginfo.join_tab;
3925
3687
 
3926
 
  if ((error=table->cursor->index_prev(table->record[0])))
 
3688
  if ((error=table->cursor->index_prev(table->getInsertRecord())))
3927
3689
    return table->report_error(error);
3928
3690
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
3929
3691
                      tab->ref.key_length))
3941
3703
  return join_init_read_record(tab);
3942
3704
}
3943
3705
 
3944
 
int rr_sequential(READ_RECORD *info);
3945
3706
int init_read_record_seq(JoinTable *tab)
3946
3707
{
3947
 
  tab->read_record.read_record= rr_sequential;
3948
 
  if (tab->read_record.cursor->ha_rnd_init(1))
 
3708
  tab->read_record.init_reard_record_sequential();
 
3709
 
 
3710
  if (tab->read_record.cursor->startTableScan(1))
3949
3711
    return 1;
3950
3712
  return (*tab->read_record.read_record)(&tab->read_record);
3951
3713
}
3962
3724
{
3963
3725
  if (tab->select && tab->select->quick && tab->select->quick->reset())
3964
3726
    return 1;
3965
 
  init_read_record(&tab->read_record, tab->join->session, tab->table,
3966
 
                   tab->select,1,1);
 
3727
 
 
3728
  tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true);
 
3729
 
3967
3730
  return (*tab->read_record.read_record)(&tab->read_record);
3968
3731
}
3969
3732
 
3974
3737
  if (!table->key_read && table->covering_keys.test(tab->index) &&
3975
3738
      !table->no_keyread)
3976
3739
  {
3977
 
    table->key_read=1;
 
3740
    table->key_read= 1;
3978
3741
    table->cursor->extra(HA_EXTRA_KEYREAD);
3979
3742
  }
3980
 
  tab->table->status=0;
 
3743
  tab->table->status= 0;
3981
3744
  tab->read_record.table=table;
3982
3745
  tab->read_record.cursor=table->cursor;
3983
3746
  tab->read_record.index=tab->index;
3984
 
  tab->read_record.record=table->record[0];
 
3747
  tab->read_record.record=table->getInsertRecord();
3985
3748
  if (tab->insideout_match_tab)
3986
3749
  {
3987
3750
    tab->read_record.do_insideout_scan= tab;
3995
3758
  }
3996
3759
 
3997
3760
  if (!table->cursor->inited)
3998
 
    table->cursor->ha_index_init(tab->index, tab->sorted);
3999
 
  if ((error=tab->table->cursor->index_first(tab->table->record[0])))
 
3761
    table->cursor->startIndexScan(tab->index, tab->sorted);
 
3762
  if ((error=tab->table->cursor->index_first(tab->table->getInsertRecord())))
4000
3763
  {
4001
3764
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
4002
3765
      table->report_error(error);
4006
3769
  return 0;
4007
3770
}
4008
3771
 
4009
 
int join_read_next_different(READ_RECORD *info)
 
3772
int join_read_next_different(ReadRecord *info)
4010
3773
{
4011
3774
  JoinTable *tab= info->do_insideout_scan;
4012
3775
  if (tab->insideout_match_tab->found_match)
4013
3776
  {
4014
 
    KEY *key= tab->table->key_info + tab->index;
 
3777
    KeyInfo *key= tab->table->key_info + tab->index;
4015
3778
    do
4016
3779
    {
4017
3780
      int error;
4029
3792
    return join_read_next(info);
4030
3793
}
4031
3794
 
4032
 
int join_read_next(READ_RECORD *info)
 
3795
int join_read_next(ReadRecord *info)
4033
3796
{
4034
3797
  int error;
4035
3798
  if ((error=info->cursor->index_next(info->record)))
4052
3815
  tab->read_record.table=table;
4053
3816
  tab->read_record.cursor=table->cursor;
4054
3817
  tab->read_record.index=tab->index;
4055
 
  tab->read_record.record=table->record[0];
 
3818
  tab->read_record.record=table->getInsertRecord();
4056
3819
  if (!table->cursor->inited)
4057
 
    table->cursor->ha_index_init(tab->index, 1);
4058
 
  if ((error= tab->table->cursor->index_last(tab->table->record[0])))
 
3820
    table->cursor->startIndexScan(tab->index, 1);
 
3821
  if ((error= tab->table->cursor->index_last(tab->table->getInsertRecord())))
4059
3822
    return table->report_error(error);
4060
3823
 
4061
3824
  return 0;
4062
3825
}
4063
3826
 
4064
 
int join_read_prev(READ_RECORD *info)
 
3827
int join_read_prev(ReadRecord *info)
4065
3828
{
4066
3829
  int error;
4067
3830
  if ((error= info->cursor->index_prev(info->record)))
4087
3850
  return safe_index_read(tab);
4088
3851
}
4089
3852
 
4090
 
int join_read_next_same_or_null(READ_RECORD *info)
 
3853
int join_read_next_same_or_null(ReadRecord *info)
4091
3854
{
4092
3855
  int error;
4093
3856
  if ((error= join_read_next_same(info)) >= 0)
4101
3864
  return safe_index_read(tab);                  // then read null keys
4102
3865
}
4103
3866
 
4104
 
enum_nested_loop_state end_send_group(JOIN *join, JoinTable *, bool end_of_records)
 
3867
enum_nested_loop_state end_send_group(Join *join, JoinTable *, bool end_of_records)
4105
3868
{
4106
3869
  int idx= -1;
4107
3870
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
4190
3953
  return(NESTED_LOOP_OK);
4191
3954
}
4192
3955
 
4193
 
enum_nested_loop_state end_write_group(JOIN *join, JoinTable *, bool end_of_records)
 
3956
enum_nested_loop_state end_write_group(Join *join, JoinTable *, bool end_of_records)
4194
3957
{
4195
3958
  Table *table=join->tmp_table;
4196
3959
  int     idx= -1;
4197
3960
 
4198
 
  if (join->session->killed)
 
3961
  if (join->session->getKilled())
4199
3962
  {                                             // Aborted by user
4200
3963
    join->session->send_kill_message();
4201
3964
    return NESTED_LOOP_KILLED;
4216
3979
        copy_sum_funcs(join->sum_funcs, join->sum_funcs_end[send_group_parts]);
4217
3980
        if (!join->having || join->having->val_int())
4218
3981
        {
4219
 
          int error= table->cursor->ha_write_row(table->record[0]);
4220
 
          if (error && create_myisam_from_heap(join->session, table,
4221
 
                                              join->tmp_table_param.start_recinfo,
4222
 
                                                &join->tmp_table_param.recinfo,
4223
 
                                              error, 0))
4224
 
          return NESTED_LOOP_ERROR;
 
3982
          int error= table->cursor->insertRecord(table->getInsertRecord());
 
3983
 
 
3984
          if (error)
 
3985
          {
 
3986
            my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
 
3987
            return NESTED_LOOP_ERROR;
 
3988
          }
4225
3989
        }
4226
3990
        if (join->rollup.state != ROLLUP::STATE_NONE)
4227
3991
        {
4259
4023
  outer join table.
4260
4024
  We can't remove tests that are made against columns which are stored
4261
4025
  in sorted order.
4262
 
*****************************************************************************/
4263
 
 
4264
 
/**
4265
4026
  @return
4266
 
    1 if right_item is used removable reference key on left_item
4267
 
*/
 
4027
    1 if right_item used is a removable reference key on left_item
 
4028
    0 otherwise.
 
4029
****************************************************************************/
4268
4030
bool test_if_ref(Item_field *left_item,Item *right_item)
4269
4031
{
4270
4032
  Field *field=left_item->field;
4271
4033
  // No need to change const test. We also have to keep tests on LEFT JOIN
4272
 
  if (!field->table->const_table && !field->table->maybe_null)
 
4034
  if (not field->getTable()->const_table && !field->getTable()->maybe_null)
4273
4035
  {
4274
 
    Item *ref_item=part_of_refkey(field->table,field);
 
4036
    Item *ref_item=part_of_refkey(field->getTable(),field);
4275
4037
    if (ref_item && ref_item->eq(right_item,1))
4276
4038
    {
4277
4039
      right_item= right_item->real_item();
4454
4216
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
4455
4217
  if (ref_parts)
4456
4218
  {
4457
 
    KEY_PART_INFO *key_part=
 
4219
    KeyPartInfo *key_part=
4458
4220
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
4459
4221
    uint32_t part;
4460
4222
 
4465
4227
    }
4466
4228
 
4467
4229
    for (part=0 ; part < ref_parts ; part++,key_part++)
 
4230
    {
4468
4231
      if (field->eq(key_part->field) &&
4469
 
          !(key_part->key_part_flag & HA_PART_KEY_SEG))
 
4232
          !(key_part->key_part_flag & HA_PART_KEY_SEG) &&
 
4233
          //If field can be NULL, we should not remove this predicate, as
 
4234
          //it may lead to non-rejection of NULL values. 
 
4235
          !(field->real_maybe_null()))
 
4236
      {
4470
4237
        return table->reginfo.join_tab->ref.items[part];
 
4238
      }
 
4239
    }
4471
4240
  }
4472
4241
  return (Item*) 0;
4473
4242
}
4492
4261
  @retval
4493
4262
    -1   Reverse key can be used
4494
4263
*/
4495
 
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
 
4264
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
4496
4265
{
4497
 
  KEY_PART_INFO *key_part= NULL;
4498
 
  KEY_PART_INFO *key_part_end= NULL;
 
4266
  KeyPartInfo *key_part= NULL;
 
4267
  KeyPartInfo *key_part_end= NULL;
4499
4268
  key_part= table->key_info[idx].key_part;
4500
4269
  key_part_end= key_part + table->key_info[idx].key_parts;
4501
4270
  key_part_map const_key_parts=table->const_key_parts[idx];
4523
4292
      */
4524
4293
      if (!on_primary_key &&
4525
4294
          (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
4526
 
          table->s->primary_key != MAX_KEY)
 
4295
          table->getShare()->hasPrimaryKey())
4527
4296
      {
4528
4297
        on_primary_key= true;
4529
 
        key_part= table->key_info[table->s->primary_key].key_part;
4530
 
        key_part_end=key_part+table->key_info[table->s->primary_key].key_parts;
4531
 
        const_key_parts=table->const_key_parts[table->s->primary_key];
 
4298
        key_part= table->key_info[table->getShare()->getPrimaryKey()].key_part;
 
4299
        key_part_end=key_part+table->key_info[table->getShare()->getPrimaryKey()].key_parts;
 
4300
        const_key_parts=table->const_key_parts[table->getShare()->getPrimaryKey()];
4532
4301
 
4533
4302
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
4534
4303
          key_part++;
4577
4346
  @retval
4578
4347
    0   no sub key
4579
4348
*/
4580
 
inline bool is_subkey(KEY_PART_INFO *key_part,
4581
 
                      KEY_PART_INFO *ref_key_part,
4582
 
                      KEY_PART_INFO *ref_key_part_end)
 
4349
inline bool is_subkey(KeyPartInfo *key_part,
 
4350
                      KeyPartInfo *ref_key_part,
 
4351
                      KeyPartInfo *ref_key_part_end)
4583
4352
{
4584
4353
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
4585
4354
    if (! key_part->field->eq(ref_key_part->field))
4598
4367
    - MAX_KEY                   If we can't use other key
4599
4368
    - the number of found key   Otherwise
4600
4369
*/
4601
 
static uint32_t test_if_subkey(order_st *order,
 
4370
static uint32_t test_if_subkey(Order *order,
4602
4371
                               Table *table,
4603
4372
                               uint32_t ref,
4604
4373
                               uint32_t ref_key_parts,
4608
4377
  uint32_t min_length= UINT32_MAX;
4609
4378
  uint32_t best= MAX_KEY;
4610
4379
  uint32_t not_used;
4611
 
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
4612
 
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
 
4380
  KeyPartInfo *ref_key_part= table->key_info[ref].key_part;
 
4381
  KeyPartInfo *ref_key_part_end= ref_key_part + ref_key_parts;
4613
4382
 
4614
 
  for (nr= 0 ; nr < table->s->keys ; nr++)
 
4383
  for (nr= 0 ; nr < table->getShare()->sizeKeys() ; nr++)
4615
4384
  {
4616
4385
    if (usable_keys->test(nr) &&
4617
4386
        table->key_info[nr].key_length < min_length &&
4660
4429
*/
4661
4430
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
4662
4431
{
4663
 
  for (uint32_t keynr= 0; keynr < table->s->keys; keynr++)
 
4432
  for (uint32_t keynr= 0; keynr < table->getShare()->sizeKeys(); keynr++)
4664
4433
  {
4665
 
    if (keynr == table->s->primary_key ||
 
4434
    if (keynr == table->getShare()->getPrimaryKey() ||
4666
4435
         (table->key_info[keynr].flags & HA_NOSAME))
4667
4436
    {
4668
 
      KEY *keyinfo= table->key_info + keynr;
4669
 
      KEY_PART_INFO *key_part= NULL;
4670
 
      KEY_PART_INFO *key_part_end= NULL;
 
4437
      KeyInfo *keyinfo= table->key_info + keynr;
 
4438
      KeyPartInfo *key_part= NULL;
 
4439
      KeyPartInfo *key_part_end= NULL;
4671
4440
 
4672
4441
      for (key_part=keyinfo->key_part,
4673
4442
           key_part_end=key_part+ keyinfo->key_parts;
4700
4469
*/
4701
4470
bool find_field_in_order_list (Field *field, void *data)
4702
4471
{
4703
 
  order_st *group= (order_st *) data;
 
4472
  Order *group= (Order *) data;
4704
4473
  bool part_found= 0;
4705
 
  for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
4474
  for (Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4706
4475
  {
4707
4476
    Item *item= (*tmp_group->item)->real_item();
4708
4477
    if (item->type() == Item::FIELD_ITEM &&
4772
4541
  @retval
4773
4542
    1    We can use an index.
4774
4543
*/
4775
 
bool test_if_skip_sort_order(JoinTable *tab, order_st *order, ha_rows select_limit, bool no_changes, const key_map *map)
 
4544
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
4776
4545
{
4777
4546
  int32_t ref_key;
4778
4547
  uint32_t ref_key_parts;
4789
4558
  */
4790
4559
  usable_keys= *map;
4791
4560
 
4792
 
  for (order_st *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
4561
  for (Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4793
4562
  {
4794
4563
    Item *item= (*tmp_order->item)->real_item();
4795
4564
    if (item->type() != Item::FIELD_ITEM)
4918
4687
    int best_key= -1;
4919
4688
    bool is_best_covering= false;
4920
4689
    double fanout= 1;
4921
 
    JOIN *join= tab->join;
 
4690
    Join *join= tab->join;
4922
4691
    uint32_t tablenr= tab - join->join_tab;
4923
4692
    ha_rows table_records= table->cursor->stats.records;
4924
4693
    bool group= join->group && order == join->group_list;
4961
4730
      fanout*= cur_pos.getFanout(); // fanout is always >= 1
4962
4731
    }
4963
4732
 
4964
 
    for (nr=0; nr < table->s->keys ; nr++)
 
4733
    for (nr=0; nr < table->getShare()->sizeKeys() ; nr++)
4965
4734
    {
4966
4735
      int direction;
4967
4736
      if (keys.test(nr) &&
4968
4737
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
4969
4738
      {
4970
 
        bool is_covering= table->covering_keys.test(nr) || (nr == table->s->primary_key && table->cursor->primary_key_is_clustered());
 
4739
        bool is_covering= table->covering_keys.test(nr) || (nr == table->getShare()->getPrimaryKey() && table->cursor->primary_key_is_clustered());
4971
4740
 
4972
4741
        /*
4973
4742
          Don't use an index scan with ORDER BY without limit.
4984
4753
        {
4985
4754
          double rec_per_key;
4986
4755
          double index_scan_time;
4987
 
          KEY *keyinfo= tab->table->key_info+nr;
 
4756
          KeyInfo *keyinfo= tab->table->key_info+nr;
4988
4757
          if (select_limit == HA_POS_ERROR)
4989
4758
            select_limit= table_records;
4990
4759
          if (group)
5215
4984
    -1          Some fatal error
5216
4985
    1           No records
5217
4986
*/
5218
 
int create_sort_index(Session *session, JOIN *join, order_st *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
 
4987
int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
5219
4988
{
5220
4989
  uint32_t length= 0;
5221
4990
  ha_rows examined_rows;
5242
5011
                              is_order_by ?  &table->keys_in_use_for_order_by :
5243
5012
                              &table->keys_in_use_for_group_by))
5244
5013
    return(0);
5245
 
  for (order_st *ord= join->order; ord; ord= ord->next)
 
5014
  for (Order *ord= join->order; ord; ord= ord->next)
5246
5015
    length++;
5247
 
  if (!(join->sortorder=
5248
 
        make_unireg_sortorder(order, &length, join->sortorder)))
5249
 
    goto err;
 
5016
  if (!(join->sortorder= make_unireg_sortorder(order, &length, join->sortorder)))
 
5017
  {
 
5018
    return(-1);
 
5019
  }
5250
5020
 
5251
5021
  table->sort.io_cache= new internal::IO_CACHE;
5252
 
  memset(table->sort.io_cache, 0, sizeof(internal::IO_CACHE));
5253
5022
  table->status=0;                              // May be wrong if quick_select
5254
5023
 
5255
5024
  // If table has a range, move it to select
5282
5051
                                                                 &tab->ref,
5283
5052
                                                                 tab->found_records))))
5284
5053
      {
5285
 
        goto err;
 
5054
        return(-1);
5286
5055
      }
5287
5056
    }
5288
5057
  }
5289
5058
 
5290
 
  if (table->s->tmp_table)
 
5059
  if (table->getShare()->getType())
5291
5060
    table->cursor->info(HA_STATUS_VARIABLE);    // Get record count
5292
 
  table->sort.found_records=filesort(session, table,join->sortorder, length,
5293
 
                                     select, filesort_limit, 0,
5294
 
                                     &examined_rows);
 
5061
 
 
5062
  FileSort filesort(*session);
 
5063
  table->sort.found_records=filesort.run(table,join->sortorder, length,
 
5064
                                         select, filesort_limit, 0,
 
5065
                                         examined_rows);
5295
5066
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
5296
5067
  if (select)
5297
5068
  {
5309
5080
    table->key_read=0;
5310
5081
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
5311
5082
  }
 
5083
 
5312
5084
  return(table->sort.found_records == HA_POS_ERROR);
5313
 
err:
5314
 
  return(-1);
5315
5085
}
5316
5086
 
5317
5087
int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having)
5320
5090
  char *org_record,*new_record;
5321
5091
  unsigned char *record;
5322
5092
  int error;
5323
 
  uint32_t reclength= table->s->reclength-offset;
5324
 
 
5325
 
  org_record=(char*) (record=table->record[0])+offset;
5326
 
  new_record=(char*) table->record[1]+offset;
5327
 
 
5328
 
  cursor->ha_rnd_init(1);
 
5093
  uint32_t reclength= table->getShare()->getRecordLength() - offset;
 
5094
 
 
5095
  org_record=(char*) (record=table->getInsertRecord())+offset;
 
5096
  new_record=(char*) table->getUpdateRecord()+offset;
 
5097
 
 
5098
  cursor->startTableScan(1);
5329
5099
  error=cursor->rnd_next(record);
5330
5100
  for (;;)
5331
5101
  {
5332
 
    if (session->killed)
 
5102
    if (session->getKilled())
5333
5103
    {
5334
5104
      session->send_kill_message();
5335
5105
      error=0;
5345
5115
    }
5346
5116
    if (having && !having->val_int())
5347
5117
    {
5348
 
      if ((error=cursor->ha_delete_row(record)))
 
5118
      if ((error=cursor->deleteRecord(record)))
5349
5119
        goto err;
5350
5120
      error=cursor->rnd_next(record);
5351
5121
      continue;
5372
5142
      }
5373
5143
      if (table->compare_record(first_field) == 0)
5374
5144
      {
5375
 
        if ((error=cursor->ha_delete_row(record)))
 
5145
        if ((error=cursor->deleteRecord(record)))
5376
5146
          goto err;
5377
5147
      }
5378
5148
      else if (!found)
5383
5153
    }
5384
5154
    if (!found)
5385
5155
      break;                                    // End of cursor
5386
 
    /* Restart search on next row */
5387
 
    error=cursor->restart_rnd_next(record,cursor->ref);
 
5156
    /* Move current position to the next row */
 
5157
    error= cursor->rnd_pos(record, cursor->ref);
5388
5158
  }
5389
5159
 
5390
5160
  cursor->extra(HA_EXTRA_NO_CACHE);
5409
5179
                               uint32_t key_length,
5410
5180
                               Item *having)
5411
5181
{
5412
 
  unsigned char *key_buffer, *key_pos, *record=table->record[0];
 
5182
  unsigned char *key_pos, *record=table->getInsertRecord();
5413
5183
  int error;
5414
5184
  Cursor *cursor= table->cursor;
5415
5185
  uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
5416
 
  uint32_t *field_lengths,*field_length;
 
5186
  uint32_t *field_length;
5417
5187
  HASH hash;
 
5188
  std::vector<unsigned char> key_buffer;
 
5189
  std::vector<uint32_t> field_lengths;
5418
5190
 
5419
 
  if (! memory::multi_malloc(false,
5420
 
                       &key_buffer,
5421
 
                       (uint32_t) ((key_length + extra_length) *
5422
 
                               (long) cursor->stats.records),
5423
 
                       &field_lengths,
5424
 
                       (uint32_t) (field_count*sizeof(*field_lengths)),
5425
 
                       NULL))
5426
 
    return(1);
 
5191
  key_buffer.resize((key_length + extra_length) * (long) cursor->stats.records);
 
5192
  field_lengths.resize(field_count);
5427
5193
 
5428
5194
  {
5429
5195
    Field **ptr;
5430
5196
    uint32_t total_length= 0;
5431
 
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
 
5197
 
 
5198
    for (ptr= first_field, field_length= &field_lengths[0] ; *ptr ; ptr++)
5432
5199
    {
5433
5200
      uint32_t length= (*ptr)->sort_length();
5434
5201
      (*field_length++)= length;
5442
5209
  if (hash_init(&hash, &my_charset_bin, (uint32_t) cursor->stats.records, 0,
5443
5210
                key_length, (hash_get_key) 0, 0, 0))
5444
5211
  {
5445
 
    free((char*) key_buffer);
5446
5212
    return(1);
5447
5213
  }
5448
5214
 
5449
 
  cursor->ha_rnd_init(1);
5450
 
  key_pos=key_buffer;
 
5215
  cursor->startTableScan(1);
 
5216
  key_pos= &key_buffer[0];
5451
5217
  for (;;)
5452
5218
  {
5453
5219
    unsigned char *org_key_pos;
5454
 
    if (session->killed)
 
5220
    if (session->getKilled())
5455
5221
    {
5456
5222
      session->send_kill_message();
5457
5223
      error=0;
5467
5233
    }
5468
5234
    if (having && !having->val_int())
5469
5235
    {
5470
 
      if ((error=cursor->ha_delete_row(record)))
 
5236
      if ((error=cursor->deleteRecord(record)))
5471
5237
        goto err;
5472
5238
      continue;
5473
5239
    }
5474
5240
 
5475
5241
    /* copy fields to key buffer */
5476
5242
    org_key_pos= key_pos;
5477
 
    field_length=field_lengths;
 
5243
    field_length= &field_lengths[0];
5478
5244
    for (Field **ptr= first_field ; *ptr ; ptr++)
5479
5245
    {
5480
5246
      (*ptr)->sort_string(key_pos,*field_length);
5484
5250
    if (hash_search(&hash, org_key_pos, key_length))
5485
5251
    {
5486
5252
      /* Duplicated found ; Remove the row */
5487
 
      if ((error=cursor->ha_delete_row(record)))
 
5253
      if ((error=cursor->deleteRecord(record)))
5488
5254
        goto err;
5489
5255
    }
5490
5256
    else
5491
5257
      (void) my_hash_insert(&hash, org_key_pos);
5492
5258
    key_pos+=extra_length;
5493
5259
  }
5494
 
  free((char*) key_buffer);
5495
5260
  hash_free(&hash);
5496
5261
  cursor->extra(HA_EXTRA_NO_CACHE);
5497
 
  (void) cursor->ha_rnd_end();
 
5262
  (void) cursor->endTableScan();
5498
5263
  return(0);
5499
5264
 
5500
5265
err:
5501
 
  free((char*) key_buffer);
5502
5266
  hash_free(&hash);
5503
5267
  cursor->extra(HA_EXTRA_NO_CACHE);
5504
 
  (void) cursor->ha_rnd_end();
 
5268
  (void) cursor->endTableScan();
5505
5269
  if (error)
5506
5270
    table->print_error(error,MYF(0));
5507
5271
  return(1);
5508
5272
}
5509
5273
 
5510
 
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length, SORT_FIELD *sortorder)
 
5274
SortField *make_unireg_sortorder(Order *order, uint32_t *length, SortField *sortorder)
5511
5275
{
5512
5276
  uint32_t count;
5513
 
  SORT_FIELD *sort,*pos;
 
5277
  SortField *sort,*pos;
5514
5278
 
5515
5279
  count=0;
5516
 
  for (order_st *tmp = order; tmp; tmp=tmp->next)
 
5280
  for (Order *tmp = order; tmp; tmp=tmp->next)
5517
5281
    count++;
5518
5282
  if (!sortorder)
5519
 
    sortorder= (SORT_FIELD*) memory::sql_alloc(sizeof(SORT_FIELD) *
 
5283
    sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) *
5520
5284
                                       (max(count, *length) + 1));
5521
5285
  pos= sort= sortorder;
5522
5286
 
5638
5402
static bool find_order_in_list(Session *session, 
5639
5403
                               Item **ref_pointer_array, 
5640
5404
                               TableList *tables,
5641
 
                               order_st *order,
 
5405
                               Order *order,
5642
5406
                               List<Item> &fields,
5643
5407
                               List<Item> &all_fields,
5644
5408
                               bool is_group_field)
5670
5434
    return false;
5671
5435
  }
5672
5436
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
5673
 
  select_item= find_item_in_list(order_item, fields, &counter,
 
5437
  select_item= find_item_in_list(session, order_item, fields, &counter,
5674
5438
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
5675
5439
  if (!select_item)
5676
5440
    return true; /* The item is not unique, or some other error occured. */
5737
5501
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
5738
5502
                          ER(ER_NON_UNIQ_ERROR),
5739
5503
                          ((Item_ident*) order_item)->field_name,
5740
 
                          current_session->where);
 
5504
                          session->where);
5741
5505
    }
5742
5506
  }
5743
5507
 
5777
5541
                TableList *tables,
5778
5542
                            List<Item> &fields,
5779
5543
                List<Item> &all_fields,
5780
 
                order_st *order)
 
5544
                Order *order)
5781
5545
{
5782
5546
  session->where="order clause";
5783
5547
  for (; order; order=order->next)
5819
5583
                TableList *tables,
5820
5584
                      List<Item> &fields,
5821
5585
                List<Item> &all_fields,
5822
 
                order_st *order,
 
5586
                Order *order,
5823
5587
                      bool *hidden_group_fields)
5824
5588
{
5825
5589
  *hidden_group_fields=0;
5826
 
  order_st *ord;
 
5590
  Order *ord;
5827
5591
 
5828
5592
  if (!order)
5829
5593
    return 0;                           /* Everything is ok */
5913
5677
  Try to use the fields in the order given by 'order' to allow one to
5914
5678
  optimize away 'order by'.
5915
5679
*/
5916
 
order_st *create_distinct_group(Session *session,
 
5680
Order *create_distinct_group(Session *session,
5917
5681
                                Item **ref_pointer_array,
5918
 
                                order_st *order_list,
 
5682
                                Order *order_list,
5919
5683
                                List<Item> &fields,
5920
5684
                                List<Item> &,
5921
5685
                                bool *all_order_by_fields_used)
5922
5686
{
5923
5687
  List_iterator<Item> li(fields);
5924
5688
  Item *item;
5925
 
  order_st *order,*group,**prev;
 
5689
  Order *order,*group,**prev;
5926
5690
 
5927
5691
  *all_order_by_fields_used= 1;
5928
5692
  while ((item=li++))
5933
5697
  {
5934
5698
    if (order->in_field_list)
5935
5699
    {
5936
 
      order_st *ord=(order_st*) session->memdup((char*) order,sizeof(order_st));
 
5700
      Order *ord=(Order*) session->memdup((char*) order,sizeof(Order));
5937
5701
      if (!ord)
5938
5702
        return 0;
5939
5703
      *prev=ord;
5953
5717
        Don't put duplicate columns from the SELECT list into the
5954
5718
        GROUP BY list.
5955
5719
      */
5956
 
      order_st *ord_iter;
 
5720
      Order *ord_iter;
5957
5721
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
5958
5722
        if ((*ord_iter->item)->eq(item, 1))
5959
5723
          goto next_item;
5960
5724
 
5961
 
      order_st *ord=(order_st*) session->calloc(sizeof(order_st));
 
5725
      Order *ord=(Order*) session->calloc(sizeof(Order));
5962
5726
      if (!ord)
5963
5727
        return 0;
5964
5728
 
6145
5909
          saved value
6146
5910
        */
6147
5911
        field= item->field;
6148
 
        item->result_field=field->new_field(session->mem_root,field->table, 1);
 
5912
        item->result_field=field->new_field(session->mem_root,field->getTable(), 1);
6149
5913
              /*
6150
5914
                We need to allocate one extra byte for null handling and
6151
5915
                another extra byte to not get warnings from purify in
6157
5921
        {
6158
5922
          copy->set(tmp, item->result_field);
6159
5923
          item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
6160
 
#ifdef HAVE_purify
 
5924
#ifdef HAVE_VALGRIND
6161
5925
          copy->to_ptr[copy->from_length]= 0;
6162
5926
#endif
6163
5927
          copy++;
6278
6042
      }
6279
6043
      else if ((field= item->get_tmp_table_field()))
6280
6044
      {
6281
 
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
 
6045
        if (item->type() == Item::SUM_FUNC_ITEM && field->getTable()->group)
6282
6046
          item_field= ((Item_sum*) item)->result_item(field);
6283
6047
        else
6284
6048
          item_field= (Item*) new Item_field(field);
6494
6258
  @retval
6495
6259
    1   on error
6496
6260
*/
6497
 
bool change_group_ref(Session *session, Item_func *expr, order_st *group_list, bool *changed)
 
6261
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
6498
6262
{
6499
6263
  if (expr->arg_count)
6500
6264
  {
6508
6272
      Item *item= *arg;
6509
6273
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6510
6274
      {
6511
 
        order_st *group_tmp;
 
6275
        Order *group_tmp;
6512
6276
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6513
6277
        {
6514
6278
          if (item->eq(*group_tmp->item,0))
6591
6355
void Select_Lex::print(Session *session, String *str, enum_query_type query_type)
6592
6356
{
6593
6357
  /* QQ: session may not be set for sub queries, but this should be fixed */
6594
 
  if (!session)
 
6358
  if(not session)
6595
6359
    session= current_session;
6596
6360
 
 
6361
 
6597
6362
  str->append(STRING_WITH_LEN("select "));
6598
6363
 
6599
6364
  /* First add options */
6659
6424
  if (group_list.elements)
6660
6425
  {
6661
6426
    str->append(STRING_WITH_LEN(" group by "));
6662
 
    print_order(str, (order_st *) group_list.first, query_type);
 
6427
    print_order(str, (Order *) group_list.first, query_type);
6663
6428
    switch (olap)
6664
6429
    {
6665
6430
      case CUBE_TYPE:
6690
6455
  if (order_list.elements)
6691
6456
  {
6692
6457
    str->append(STRING_WITH_LEN(" order by "));
6693
 
    print_order(str, (order_st *) order_list.first, query_type);
 
6458
    print_order(str, (Order *) order_list.first, query_type);
6694
6459
  }
6695
6460
 
6696
6461
  // limit