~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Paweł Blokus
  • Date: 2010-06-09 20:36:51 UTC
  • mto: This revision was merged to the branch mainline in revision 1620.
  • Revision ID: pawel@pawel-desktop-20100609203651-mbq5x34bt9m3kv0o
minor style fixes

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
51
51
#include "drizzled/index_hint.h"
52
52
#include "drizzled/records.h"
53
53
#include "drizzled/internal/iocache.h"
54
 
#include "drizzled/drizzled.h"
55
54
 
56
55
#include "drizzled/sql_union.h"
57
56
#include "drizzled/optimizer/key_field.h"
62
61
#include "drizzled/optimizer/quick_range_select.h"
63
62
#include "drizzled/optimizer/quick_ror_intersect_select.h"
64
63
 
65
 
#include "drizzled/filesort.h"
66
 
 
67
64
using namespace std;
68
65
 
69
66
namespace drizzled
117
114
{
118
115
  bool res;
119
116
  register Select_Lex *select_lex= &lex->select_lex;
120
 
  DRIZZLE_SELECT_START(session->getQueryString()->c_str());
 
117
  DRIZZLE_SELECT_START(session->query.c_str());
121
118
 
122
119
  if (select_lex->master_unit()->is_union() ||
123
120
      select_lex->master_unit()->fake_select_lex)
135
132
      every PS/SP execution new, we will not need reset this flag if
136
133
      setup_tables_done_option changed for next rexecution
137
134
    */
138
 
    res= mysql_select(session,
139
 
                      &select_lex->ref_pointer_array,
 
135
    res= mysql_select(session, &select_lex->ref_pointer_array,
140
136
                      (TableList*) select_lex->table_list.first,
141
 
                      select_lex->with_wild,
142
 
                      select_lex->item_list,
 
137
                      select_lex->with_wild, select_lex->item_list,
143
138
                      select_lex->where,
144
139
                      select_lex->order_list.elements +
145
140
                      select_lex->group_list.elements,
146
 
                      (Order*) select_lex->order_list.first,
147
 
                      (Order*) select_lex->group_list.first,
 
141
                      (order_st*) select_lex->order_list.first,
 
142
                      (order_st*) select_lex->group_list.first,
148
143
                      select_lex->having,
149
144
                      select_lex->options | session->options |
150
145
                      setup_tables_done_option,
357
352
                  List<Item> &fields,
358
353
                  COND *conds, 
359
354
                  uint32_t og_num,  
360
 
                  Order *order,
361
 
                  Order *group,
 
355
                  order_st *order, 
 
356
                  order_st *group,
362
357
                  Item *having, 
363
358
                  uint64_t select_options,
364
359
                  select_result *result, 
452
447
  return (cond? (new Item_cond_and(cond, item)) : item);
453
448
}
454
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->nested_join)
 
459
      fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
 
460
  }
 
461
}
 
462
 
455
463
/*****************************************************************************
456
464
  Create JoinTableS, make a guess about the table types,
457
465
  Approximate how many records will be used in each table
547
555
                         Select_Lex *select_lex,
548
556
                         vector<optimizer::SargableParam> &sargables)
549
557
{
550
 
  uint  and_level,found_eq_constant;
 
558
  uint  and_level,i,found_eq_constant;
551
559
  optimizer::KeyField *key_fields, *end, *field;
552
560
  uint32_t sz;
553
561
  uint32_t m= max(select_lex->max_equal_elems,(uint32_t)1);
570
578
    substitutions.
571
579
  */
572
580
  sz= sizeof(optimizer::KeyField) *
573
 
      (((session->lex->current_select->cond_count+1)*2 +
 
581
      (((session->lex->current_select->cond_count+1) +
574
582
        session->lex->current_select->between_count)*m+1);
575
583
  if (! (key_fields= (optimizer::KeyField*) session->alloc(sz)))
576
584
    return true;
590
598
      if (field->getValue()->type() == Item::NULL_ITEM &&
591
599
          ! field->getField()->real_maybe_null())
592
600
      {
593
 
        field->getField()->getTable()->reginfo.not_exists_optimize= 1;
 
601
        field->getField()->table->reginfo.not_exists_optimize= 1;
594
602
      }
595
603
    }
596
604
  }
597
 
  for (uint32_t i= 0; i < tables; i++)
 
605
  for (i= 0; i < tables; i++)
598
606
  {
599
607
    /*
600
608
      Block the creation of keys for inner tables of outer joins.
617
625
    TableList *table;
618
626
    while ((table= li++))
619
627
    {
620
 
      if (table->getNestedJoin())
 
628
      if (table->nested_join)
621
629
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
622
630
                              sargables);
623
631
    }
649
657
    use= save_pos= dynamic_element(keyuse, 0, optimizer::KeyUse*);
650
658
    prev= &key_end;
651
659
    found_eq_constant= 0;
 
660
    for (i= 0; i < keyuse->elements-1; i++, use++)
652
661
    {
653
 
      uint32_t i;
654
 
 
655
 
      for (i= 0; i < keyuse->elements-1; i++, use++)
 
662
      if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
 
663
        use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
 
664
      if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
656
665
      {
657
 
        if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
658
 
          use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
659
 
        if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
660
 
        {
661
 
          if (prev->getKeypart() + 1 < use->getKeypart() || 
662
 
              ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
663
 
            continue;                           /* remove */
664
 
        }
665
 
        else if (use->getKeypart() != 0)                // First found must be 0
666
 
          continue;
 
666
        if (prev->getKeypart() + 1 < use->getKeypart() || 
 
667
            ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
 
668
          continue;                             /* remove */
 
669
      }
 
670
      else if (use->getKeypart() != 0)          // First found must be 0
 
671
        continue;
667
672
 
668
 
#ifdef HAVE_VALGRIND
669
 
        /* Valgrind complains about overlapped memcpy when save_pos==use. */
670
 
        if (save_pos != use)
 
673
#ifdef HAVE_purify
 
674
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
 
675
      if (save_pos != use)
671
676
#endif
672
 
          *save_pos= *use;
673
 
        prev=use;
674
 
        found_eq_constant= ! use->getUsedTables();
675
 
        /* Save ptr to first use */
676
 
        if (! use->getTable()->reginfo.join_tab->keyuse)
677
 
          use->getTable()->reginfo.join_tab->keyuse= save_pos;
678
 
        use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
679
 
        save_pos++;
680
 
      }
681
 
      i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
682
 
      set_dynamic(keyuse, (unsigned char*) &key_end, i);
683
 
      keyuse->elements= i;
 
677
        *save_pos= *use;
 
678
      prev=use;
 
679
      found_eq_constant= ! use->getUsedTables();
 
680
      /* Save ptr to first use */
 
681
      if (! use->getTable()->reginfo.join_tab->keyuse)
 
682
        use->getTable()->reginfo.join_tab->keyuse= save_pos;
 
683
      use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
 
684
      save_pos++;
684
685
    }
 
686
    i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
 
687
    set_dynamic(keyuse, (unsigned char*) &key_end, i);
 
688
    keyuse->elements= i;
685
689
  }
686
690
  return false;
687
691
}
748
752
{
749
753
  List<Item_field> indexed_fields;
750
754
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
751
 
  Order      *cur_group;
 
755
  order_st      *cur_group;
752
756
  Item_field *cur_item;
753
757
  key_map possible_keys(0);
754
758
 
925
929
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
926
930
{
927
931
  bool error;
928
 
  Table *table= field->getTable();
 
932
  Table *table= field->table;
929
933
  Session *session= table->in_use;
930
934
  ha_rows cuted_fields=session->cuted_fields;
931
935
 
1151
1155
          Item *notnull;
1152
1156
          assert(item->type() == Item::FIELD_ITEM);
1153
1157
          Item_field *not_null_item= (Item_field*)item;
1154
 
          JoinTable *referred_tab= not_null_item->field->getTable()->reginfo.join_tab;
 
1158
          JoinTable *referred_tab= not_null_item->field->table->reginfo.join_tab;
1155
1159
          /*
1156
1160
            For UPDATE queries such as:
1157
1161
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
1208
1212
  return tmp;
1209
1213
}
1210
1214
 
 
1215
/*
 
1216
  Check if given expression uses only table fields covered by the given index
 
1217
 
 
1218
  SYNOPSIS
 
1219
    uses_index_fields_only()
 
1220
      item           Expression to check
 
1221
      tbl            The table having the index
 
1222
      keyno          The index number
 
1223
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
1224
 
 
1225
  DESCRIPTION
 
1226
    Check if given expression only uses fields covered by index #keyno in the
 
1227
    table tbl. The expression can use any fields in any other tables.
 
1228
 
 
1229
    The expression is guaranteed not to be AND or OR - those constructs are
 
1230
    handled outside of this function.
 
1231
 
 
1232
  RETURN
 
1233
    true   Yes
 
1234
    false  No
 
1235
*/
 
1236
static bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno, bool other_tbls_ok)
 
1237
{
 
1238
  if (item->const_item())
 
1239
    return true;
 
1240
 
 
1241
  /*
 
1242
    Don't push down the triggered conditions. Nested outer joins execution
 
1243
    code may need to evaluate a condition several times (both triggered and
 
1244
    untriggered), and there is no way to put thi
 
1245
    TODO: Consider cloning the triggered condition and using the copies for:
 
1246
      1. push the first copy down, to have most restrictive index condition
 
1247
         possible
 
1248
      2. Put the second copy into tab->select_cond.
 
1249
  */
 
1250
  if (item->type() == Item::FUNC_ITEM &&
 
1251
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
 
1252
    return false;
 
1253
 
 
1254
  if (!(item->used_tables() & tbl->map))
 
1255
    return other_tbls_ok;
 
1256
 
 
1257
  Item::Type item_type= item->type();
 
1258
  switch (item_type) {
 
1259
  case Item::FUNC_ITEM:
 
1260
    {
 
1261
      /* This is a function, apply condition recursively to arguments */
 
1262
      Item_func *item_func= (Item_func*)item;
 
1263
      Item **child;
 
1264
      Item **item_end= (item_func->arguments()) + item_func->argument_count();
 
1265
      for (child= item_func->arguments(); child != item_end; child++)
 
1266
      {
 
1267
        if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
 
1268
          return false;
 
1269
      }
 
1270
      return true;
 
1271
    }
 
1272
  case Item::COND_ITEM:
 
1273
    {
 
1274
      /* This is a function, apply condition recursively to arguments */
 
1275
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
 
1276
      Item *list_item;
 
1277
      while ((list_item=li++))
 
1278
      {
 
1279
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
 
1280
          return false;
 
1281
      }
 
1282
      return true;
 
1283
    }
 
1284
  case Item::FIELD_ITEM:
 
1285
    {
 
1286
      Item_field *item_field= (Item_field*)item;
 
1287
      if (item_field->field->table != tbl)
 
1288
        return true;
 
1289
      return item_field->field->part_of_key.test(keyno);
 
1290
    }
 
1291
  case Item::REF_ITEM:
 
1292
    return uses_index_fields_only(item->real_item(), tbl, keyno,
 
1293
                                  other_tbls_ok);
 
1294
  default:
 
1295
    return false; /* Play it safe, don't push unknown non-const items */
 
1296
  }
 
1297
}
 
1298
 
1211
1299
#define ICP_COND_USES_INDEX_ONLY 10
1212
1300
 
 
1301
/*
 
1302
  Get a part of the condition that can be checked using only index fields
 
1303
 
 
1304
  SYNOPSIS
 
1305
    make_cond_for_index()
 
1306
      cond           The source condition
 
1307
      table          The table that is partially available
 
1308
      keyno          The index in the above table. Only fields covered by the index
 
1309
                     are available
 
1310
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
1311
 
 
1312
  DESCRIPTION
 
1313
    Get a part of the condition that can be checked when for the given table
 
1314
    we have values only of fields covered by some index. The condition may
 
1315
    refer to other tables, it is assumed that we have values of all of their
 
1316
    fields.
 
1317
 
 
1318
    Example:
 
1319
      make_cond_for_index(
 
1320
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
 
1321
          t2, keyno(t2.key1))
 
1322
      will return
 
1323
        "cond(t1.field) AND cond(t2.key2)"
 
1324
 
 
1325
  RETURN
 
1326
    Index condition, or NULL if no condition could be inferred.
 
1327
*/
 
1328
static Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno, bool other_tbls_ok)
 
1329
{
 
1330
  if (!cond)
 
1331
    return NULL;
 
1332
  if (cond->type() == Item::COND_ITEM)
 
1333
  {
 
1334
    uint32_t n_marked= 0;
 
1335
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
1336
    {
 
1337
      Item_cond_and *new_cond=new Item_cond_and;
 
1338
      if (!new_cond)
 
1339
        return (COND*) 0;
 
1340
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
1341
      Item *item;
 
1342
      while ((item=li++))
 
1343
      {
 
1344
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
1345
        if (fix)
 
1346
          new_cond->argument_list()->push_back(fix);
 
1347
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
1348
      }
 
1349
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
1350
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
1351
      switch (new_cond->argument_list()->elements) {
 
1352
      case 0:
 
1353
        return (COND*) 0;
 
1354
      case 1:
 
1355
        return new_cond->argument_list()->head();
 
1356
      default:
 
1357
        new_cond->quick_fix_field();
 
1358
        return new_cond;
 
1359
      }
 
1360
    }
 
1361
    else /* It's OR */
 
1362
    {
 
1363
      Item_cond_or *new_cond=new Item_cond_or;
 
1364
      if (!new_cond)
 
1365
        return (COND*) 0;
 
1366
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
1367
      Item *item;
 
1368
      while ((item=li++))
 
1369
      {
 
1370
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
1371
        if (!fix)
 
1372
          return (COND*) 0;
 
1373
        new_cond->argument_list()->push_back(fix);
 
1374
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
1375
      }
 
1376
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
1377
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
1378
      new_cond->quick_fix_field();
 
1379
      new_cond->top_level_item();
 
1380
      return new_cond;
 
1381
    }
 
1382
  }
 
1383
 
 
1384
  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
 
1385
    return (COND*) 0;
 
1386
  cond->marker= ICP_COND_USES_INDEX_ONLY;
 
1387
  return cond;
 
1388
}
 
1389
 
 
1390
 
 
1391
static Item *make_cond_remainder(Item *cond, bool exclude_index)
 
1392
{
 
1393
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
 
1394
    return 0; /* Already checked */
 
1395
 
 
1396
  if (cond->type() == Item::COND_ITEM)
 
1397
  {
 
1398
    table_map tbl_map= 0;
 
1399
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
1400
    {
 
1401
      /* Create new top level AND item */
 
1402
      Item_cond_and *new_cond=new Item_cond_and;
 
1403
      if (!new_cond)
 
1404
        return (COND*) 0;
 
1405
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
1406
      Item *item;
 
1407
      while ((item=li++))
 
1408
      {
 
1409
        Item *fix= make_cond_remainder(item, exclude_index);
 
1410
        if (fix)
 
1411
        {
 
1412
          new_cond->argument_list()->push_back(fix);
 
1413
          tbl_map |= fix->used_tables();
 
1414
        }
 
1415
      }
 
1416
      switch (new_cond->argument_list()->elements) {
 
1417
      case 0:
 
1418
        return (COND*) 0;
 
1419
      case 1:
 
1420
        return new_cond->argument_list()->head();
 
1421
      default:
 
1422
        new_cond->quick_fix_field();
 
1423
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
1424
        return new_cond;
 
1425
      }
 
1426
    }
 
1427
    else /* It's OR */
 
1428
    {
 
1429
      Item_cond_or *new_cond=new Item_cond_or;
 
1430
      if (!new_cond)
 
1431
        return (COND*) 0;
 
1432
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
1433
      Item *item;
 
1434
      while ((item=li++))
 
1435
      {
 
1436
        Item *fix= make_cond_remainder(item, false);
 
1437
        if (!fix)
 
1438
          return (COND*) 0;
 
1439
        new_cond->argument_list()->push_back(fix);
 
1440
        tbl_map |= fix->used_tables();
 
1441
      }
 
1442
      new_cond->quick_fix_field();
 
1443
      ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
1444
      new_cond->top_level_item();
 
1445
      return new_cond;
 
1446
    }
 
1447
  }
 
1448
  return cond;
 
1449
}
1213
1450
 
1214
1451
/**
1215
1452
  cleanup JoinTable.
1221
1458
  delete quick;
1222
1459
  quick= 0;
1223
1460
  if (cache.buff)
1224
 
  {
1225
 
    size_t size= cache.end - cache.buff;
1226
 
    global_join_buffer.sub(size);
1227
1461
    free(cache.buff);
1228
 
  }
1229
1462
  cache.buff= 0;
1230
1463
  limit= 0;
1231
1464
  if (table)
1245
1478
  read_record.end_read_record();
1246
1479
}
1247
1480
 
1248
 
bool only_eq_ref_tables(Join *join,Order *order,table_map tables)
 
1481
bool only_eq_ref_tables(Join *join,order_st *order,table_map tables)
1249
1482
{
1250
1483
  for (JoinTable **tab=join->map2table ; tables ; tab++, tables>>=1)
1251
1484
  {
1274
1507
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
1275
1508
  @endcode
1276
1509
*/
1277
 
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
 
1510
bool eq_ref_table(Join *join, order_st *start_order, JoinTable *tab)
1278
1511
{
1279
1512
  if (tab->cached_eq_ref_table)                 // If cached
1280
1513
    return tab->eq_ref_table;
1293
1526
  {
1294
1527
    if (! (*ref_item)->const_item())
1295
1528
    {                                           // Not a const ref
1296
 
      Order *order;
 
1529
      order_st *order;
1297
1530
      for (order=start_order ; order ; order=order->next)
1298
1531
      {
1299
1532
        if ((*ref_item)->eq(order->item[0],0))
2033
2266
    {
2034
2267
      if (table->on_expr)
2035
2268
      {
2036
 
        List<TableList> *nested_join_list= table->getNestedJoin() ?
2037
 
          &table->getNestedJoin()->join_list : NULL;
 
2269
        List<TableList> *nested_join_list= table->nested_join ?
 
2270
          &table->nested_join->join_list : NULL;
2038
2271
        /*
2039
2272
          We can modify table->on_expr because its old value will
2040
2273
          be restored before re-execution of PS/SP.
2087
2320
  if (outer_ref)
2088
2321
    return cmp;
2089
2322
  JoinTable **idx= (JoinTable **) table_join_idx;
2090
 
  cmp= idx[field2->field->getTable()->tablenr]-idx[field1->field->getTable()->tablenr];
 
2323
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
2091
2324
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
2092
2325
}
2093
2326
 
2334
2567
      while ((item_field= it++))
2335
2568
      {
2336
2569
        Field *field= item_field->field;
2337
 
        JoinTable *stat= field->getTable()->reginfo.join_tab;
 
2570
        JoinTable *stat= field->table->reginfo.join_tab;
2338
2571
        key_map possible_keys= field->key_start;
2339
 
        possible_keys&= field->getTable()->keys_in_use_for_query;
 
2572
        possible_keys&= field->table->keys_in_use_for_query;
2340
2573
        stat[0].const_keys|= possible_keys;
2341
2574
 
2342
2575
        /*
2346
2579
        */
2347
2580
        if (possible_keys.any())
2348
2581
        {
2349
 
          Table *field_tab= field->getTable();
 
2582
          Table *field_tab= field->table;
2350
2583
          optimizer::KeyUse *use;
2351
2584
          for (use= stat->keyuse; use && use->getTable() == field_tab; use++)
2352
2585
            if (possible_keys.test(use->getKey()) &&
2377
2610
    Item *item;
2378
2611
    while ((item=li++))
2379
2612
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
2380
 
 
2381
2613
    return;
2382
2614
  }
2383
2615
  if (cond->eq_cmp_result() == Item::COND_OK)
2396
2628
       left_item->collation.collation == value->collation.collation))
2397
2629
  {
2398
2630
    Item *tmp=value->clone_item();
 
2631
    tmp->collation.set(right_item->collation);
 
2632
 
2399
2633
    if (tmp)
2400
2634
    {
2401
 
      tmp->collation.set(right_item->collation);
2402
2635
      session->change_item_tree(args + 1, tmp);
2403
2636
      func->update_used_tables();
2404
2637
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2418
2651
            right_item->collation.collation == value->collation.collation))
2419
2652
  {
2420
2653
    Item *tmp= value->clone_item();
 
2654
    tmp->collation.set(left_item->collation);
 
2655
 
2421
2656
    if (tmp)
2422
2657
    {
2423
 
      tmp->collation.set(left_item->collation);
2424
2658
      session->change_item_tree(args, tmp);
2425
2659
      value= tmp;
2426
2660
      func->update_used_tables();
2490
2724
      for (vector<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
2491
2725
      {
2492
2726
        Item **args= iter->cmp_func->arguments();
2493
 
        if (not args[0]->const_item())
 
2727
        if (!args[0]->const_item())
2494
2728
        {
2495
 
          change_cond_ref_to_const(session, save_list, iter->and_level,
2496
 
                                   iter->and_level, args[0], args[1] );
 
2729
          change_cond_ref_to_const( session, save, iter->and_level,
 
2730
                                    iter->and_level, args[0], args[1] );
2497
2731
        }
2498
2732
      }
2499
2733
    }
2612
2846
  @endverbatim
2613
2847
 
2614
2848
  @param join       Join being processed
 
2849
  @param last_tab   Last table in current partial join order (this function is
 
2850
                    not called for empty partial join orders)
2615
2851
  @param next_tab   Table we're going to extend the current partial join with
2616
2852
 
2617
2853
  @retval
2620
2856
  @retval
2621
2857
    true   Requested join order extension not allowed.
2622
2858
*/
2623
 
bool check_interleaving_with_nj(JoinTable *next_tab)
 
2859
bool check_interleaving_with_nj(JoinTable *last_tab, JoinTable *next_tab)
2624
2860
{
2625
 
  TableList *next_emb= next_tab->table->pos_in_table_list->getEmbedding();
2626
 
  Join *join= next_tab->join;
 
2861
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
 
2862
  Join *join= last_tab->join;
2627
2863
 
2628
2864
  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
2629
2865
  {
2638
2874
    Do update counters for "pairs of brackets" that we've left (marked as
2639
2875
    X,Y,Z in the above picture)
2640
2876
  */
2641
 
  for (;next_emb; next_emb= next_emb->getEmbedding())
 
2877
  for (;next_emb; next_emb= next_emb->embedding)
2642
2878
  {
2643
 
    next_emb->getNestedJoin()->counter_++;
2644
 
    if (next_emb->getNestedJoin()->counter_ == 1)
 
2879
    next_emb->nested_join->counter_++;
 
2880
    if (next_emb->nested_join->counter_ == 1)
2645
2881
    {
2646
2882
      /*
2647
2883
        next_emb is the first table inside a nested join we've "entered". In
2648
2884
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
2649
2885
        X bracket might have Y pair bracket.
2650
2886
      */
2651
 
      join->cur_embedding_map |= next_emb->getNestedJoin()->nj_map;
 
2887
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
2652
2888
    }
2653
2889
 
2654
 
    if (next_emb->getNestedJoin()->join_list.elements !=
2655
 
        next_emb->getNestedJoin()->counter_)
 
2890
    if (next_emb->nested_join->join_list.elements !=
 
2891
        next_emb->nested_join->counter_)
2656
2892
      break;
2657
2893
 
2658
2894
    /*
2659
2895
      We're currently at Y or Z-bracket as depicted in the above picture.
2660
2896
      Mark that we've left it and continue walking up the brackets hierarchy.
2661
2897
    */
2662
 
    join->cur_embedding_map &= ~next_emb->getNestedJoin()->nj_map;
 
2898
    join->cur_embedding_map &= ~next_emb->nested_join->nj_map;
2663
2899
  }
2664
2900
  return false;
2665
2901
}
2787
3023
    {
2788
3024
      Field *field= ((Item_field*) args[0])->field;
2789
3025
      if (field->flags & AUTO_INCREMENT_FLAG 
2790
 
          && ! field->getTable()->maybe_null 
 
3026
          && ! field->table->maybe_null 
2791
3027
          && session->options & OPTION_AUTO_IS_NULL
2792
3028
          && (
2793
3029
            session->first_successful_insert_id_in_prev_stmt > 0 
2845
3081
  /*
2846
3082
    TODO:
2847
3083
    Excluding all expensive functions is too restritive we should exclude only
2848
 
    materialized IN subquery predicates because they can't yet be evaluated
2849
 
    here (they need additional initialization that is done later on).
2850
 
 
2851
 
    The proper way to exclude the subqueries would be to walk the cond tree and
2852
 
    check for materialized subqueries there.
2853
 
 
 
3084
    materialized IN because it is created later than this phase, and cannot be
 
3085
    evaluated at this point.
 
3086
    The condition should be something as (need to fix member access):
 
3087
      !(cond->type() == Item::FUNC_ITEM &&
 
3088
        ((Item_func*)cond)->func_name() == "<in_optimizer>" &&
 
3089
        ((Item_in_optimizer*)cond)->is_expensive()))
2854
3090
  */
2855
3091
  {
2856
3092
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
3150
3386
      rc= sub_select(join,join_tab,end_of_records);
3151
3387
    return rc;
3152
3388
  }
3153
 
  if (join->session->getKilled())               // If aborted by user
 
3389
  if (join->session->killed)            // If aborted by user
3154
3390
  {
3155
3391
    join->session->send_kill_message();
3156
3392
    return NESTED_LOOP_KILLED;
3351
3587
{
3352
3588
  int error;
3353
3589
  Table *table= tab->table;
3354
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
3590
  if ((error=table->cursor->index_read_map(table->record[0],
3355
3591
                                         tab->ref.key_buff,
3356
3592
                                         make_prev_keypart_map(tab->ref.key_parts),
3357
3593
                                         HA_READ_KEY_EXACT)))
3428
3664
      embedded= embedding;
3429
3665
      if (embedded->on_expr)
3430
3666
         update_const_equal_items(embedded->on_expr, tab);
3431
 
      embedding= embedded->getEmbedding();
 
3667
      embedding= embedded->embedding;
3432
3668
    }
3433
3669
    while (embedding &&
3434
 
           embedding->getNestedJoin()->join_list.head() == embedded);
 
3670
           embedding->nested_join->join_list.head() == embedded);
3435
3671
  }
3436
3672
 
3437
3673
  return(0);
3443
3679
  int error;
3444
3680
  if (table->status & STATUS_GARBAGE)           // If first read
3445
3681
  {
3446
 
    if ((error=table->cursor->read_first_row(table->getInsertRecord(),
3447
 
                                           table->getShare()->getPrimaryKey())))
 
3682
    if ((error=table->cursor->read_first_row(table->record[0],
 
3683
                                           table->getShare()->primary_key)))
3448
3684
    {
3449
3685
      if (error != HA_ERR_END_OF_FILE)
3450
3686
        return table->report_error(error);
3483
3719
      error= HA_ERR_KEY_NOT_FOUND;
3484
3720
    else
3485
3721
    {
3486
 
      error=table->cursor->index_read_idx_map(table->getInsertRecord(),tab->ref.key,
 
3722
      error=table->cursor->index_read_idx_map(table->record[0],tab->ref.key,
3487
3723
                                            (unsigned char*) tab->ref.key_buff,
3488
3724
                                            make_prev_keypart_map(tab->ref.key_parts),
3489
3725
                                            HA_READ_KEY_EXACT);
3543
3779
      table->status=STATUS_NOT_FOUND;
3544
3780
      return -1;
3545
3781
    }
3546
 
    error=table->cursor->index_read_map(table->getInsertRecord(),
 
3782
    error=table->cursor->index_read_map(table->record[0],
3547
3783
                                      tab->ref.key_buff,
3548
3784
                                      make_prev_keypart_map(tab->ref.key_parts),
3549
3785
                                      HA_READ_KEY_EXACT);
3590
3826
 
3591
3827
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3592
3828
    return -1;
3593
 
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
 
3829
  if ((error=table->cursor->index_read_map(table->record[0],
3594
3830
                                         tab->ref.key_buff,
3595
3831
                                         make_prev_keypart_map(tab->ref.key_parts),
3596
3832
                                         HA_READ_KEY_EXACT)))
3616
3852
    table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3617
3853
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3618
3854
    return -1;
3619
 
  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
 
3855
  if ((error=table->cursor->index_read_last_map(table->record[0],
3620
3856
                                              tab->ref.key_buff,
3621
3857
                                              make_prev_keypart_map(tab->ref.key_parts))))
3622
3858
  {
3645
3881
      /* Save index tuple from record to the buffer */
3646
3882
      key_copy(tab->insideout_buf, info->record, key, 0);
3647
3883
 
3648
 
      if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
3884
      if ((error=table->cursor->index_next_same(table->record[0],
3649
3885
                                              tab->ref.key_buff,
3650
3886
                                              tab->ref.key_length)))
3651
3887
      {
3669
3905
  Table *table= info->table;
3670
3906
  JoinTable *tab=table->reginfo.join_tab;
3671
3907
 
3672
 
  if ((error=table->cursor->index_next_same(table->getInsertRecord(),
 
3908
  if ((error=table->cursor->index_next_same(table->record[0],
3673
3909
                                          tab->ref.key_buff,
3674
3910
                                          tab->ref.key_length)))
3675
3911
  {
3688
3924
  Table *table= info->table;
3689
3925
  JoinTable *tab=table->reginfo.join_tab;
3690
3926
 
3691
 
  if ((error=table->cursor->index_prev(table->getInsertRecord())))
 
3927
  if ((error=table->cursor->index_prev(table->record[0])))
3692
3928
    return table->report_error(error);
3693
3929
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
3694
3930
                      tab->ref.key_length))
3747
3983
  tab->read_record.table=table;
3748
3984
  tab->read_record.cursor=table->cursor;
3749
3985
  tab->read_record.index=tab->index;
3750
 
  tab->read_record.record=table->getInsertRecord();
 
3986
  tab->read_record.record=table->record[0];
3751
3987
  if (tab->insideout_match_tab)
3752
3988
  {
3753
3989
    tab->read_record.do_insideout_scan= tab;
3762
3998
 
3763
3999
  if (!table->cursor->inited)
3764
4000
    table->cursor->startIndexScan(tab->index, tab->sorted);
3765
 
  if ((error=tab->table->cursor->index_first(tab->table->getInsertRecord())))
 
4001
  if ((error=tab->table->cursor->index_first(tab->table->record[0])))
3766
4002
  {
3767
4003
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3768
4004
      table->report_error(error);
3818
4054
  tab->read_record.table=table;
3819
4055
  tab->read_record.cursor=table->cursor;
3820
4056
  tab->read_record.index=tab->index;
3821
 
  tab->read_record.record=table->getInsertRecord();
 
4057
  tab->read_record.record=table->record[0];
3822
4058
  if (!table->cursor->inited)
3823
4059
    table->cursor->startIndexScan(tab->index, 1);
3824
 
  if ((error= tab->table->cursor->index_last(tab->table->getInsertRecord())))
 
4060
  if ((error= tab->table->cursor->index_last(tab->table->record[0])))
3825
4061
    return table->report_error(error);
3826
4062
 
3827
4063
  return 0;
3961
4197
  Table *table=join->tmp_table;
3962
4198
  int     idx= -1;
3963
4199
 
3964
 
  if (join->session->getKilled())
 
4200
  if (join->session->killed)
3965
4201
  {                                             // Aborted by user
3966
4202
    join->session->send_kill_message();
3967
4203
    return NESTED_LOOP_KILLED;
3982
4218
        copy_sum_funcs(join->sum_funcs, join->sum_funcs_end[send_group_parts]);
3983
4219
        if (!join->having || join->having->val_int())
3984
4220
        {
3985
 
          int error= table->cursor->insertRecord(table->getInsertRecord());
 
4221
          int error= table->cursor->insertRecord(table->record[0]);
3986
4222
 
3987
4223
          if (error)
3988
4224
          {
4009
4245
    if (idx < (int) join->send_group_parts)
4010
4246
    {
4011
4247
      copy_fields(&join->tmp_table_param);
4012
 
      if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
4013
 
        return NESTED_LOOP_ERROR;
 
4248
      copy_funcs(join->tmp_table_param.items_to_copy);
4014
4249
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
4015
4250
        return NESTED_LOOP_ERROR;
4016
4251
      return NESTED_LOOP_OK;
4027
4262
  outer join table.
4028
4263
  We can't remove tests that are made against columns which are stored
4029
4264
  in sorted order.
 
4265
*****************************************************************************/
 
4266
 
 
4267
/**
4030
4268
  @return
4031
 
    1 if right_item used is a removable reference key on left_item
4032
 
    0 otherwise.
4033
 
****************************************************************************/
 
4269
    1 if right_item is used removable reference key on left_item
 
4270
*/
4034
4271
bool test_if_ref(Item_field *left_item,Item *right_item)
4035
4272
{
4036
4273
  Field *field=left_item->field;
4037
4274
  // No need to change const test. We also have to keep tests on LEFT JOIN
4038
 
  if (not field->getTable()->const_table && !field->getTable()->maybe_null)
 
4275
  if (!field->table->const_table && !field->table->maybe_null)
4039
4276
  {
4040
 
    Item *ref_item=part_of_refkey(field->getTable(),field);
 
4277
    Item *ref_item=part_of_refkey(field->table,field);
4041
4278
    if (ref_item && ref_item->eq(right_item,1))
4042
4279
    {
4043
4280
      right_item= right_item->real_item();
4231
4468
    }
4232
4469
 
4233
4470
    for (part=0 ; part < ref_parts ; part++,key_part++)
4234
 
    {
4235
4471
      if (field->eq(key_part->field) &&
4236
 
          !(key_part->key_part_flag & HA_PART_KEY_SEG) &&
4237
 
          //If field can be NULL, we should not remove this predicate, as
4238
 
          //it may lead to non-rejection of NULL values. 
4239
 
          !(field->real_maybe_null()))
4240
 
      {
 
4472
          !(key_part->key_part_flag & HA_PART_KEY_SEG))
4241
4473
        return table->reginfo.join_tab->ref.items[part];
4242
 
      }
4243
 
    }
4244
4474
  }
4245
4475
  return (Item*) 0;
4246
4476
}
4265
4495
  @retval
4266
4496
    -1   Reverse key can be used
4267
4497
*/
4268
 
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
 
4498
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
4269
4499
{
4270
4500
  KeyPartInfo *key_part= NULL;
4271
4501
  KeyPartInfo *key_part_end= NULL;
4296
4526
      */
4297
4527
      if (!on_primary_key &&
4298
4528
          (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
4299
 
          table->getShare()->hasPrimaryKey())
 
4529
          table->getShare()->primary_key != MAX_KEY)
4300
4530
      {
4301
4531
        on_primary_key= true;
4302
 
        key_part= table->key_info[table->getShare()->getPrimaryKey()].key_part;
4303
 
        key_part_end=key_part+table->key_info[table->getShare()->getPrimaryKey()].key_parts;
4304
 
        const_key_parts=table->const_key_parts[table->getShare()->getPrimaryKey()];
 
4532
        key_part= table->key_info[table->getShare()->primary_key].key_part;
 
4533
        key_part_end=key_part+table->key_info[table->getShare()->primary_key].key_parts;
 
4534
        const_key_parts=table->const_key_parts[table->getShare()->primary_key];
4305
4535
 
4306
4536
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
4307
4537
          key_part++;
4371
4601
    - MAX_KEY                   If we can't use other key
4372
4602
    - the number of found key   Otherwise
4373
4603
*/
4374
 
static uint32_t test_if_subkey(Order *order,
 
4604
static uint32_t test_if_subkey(order_st *order,
4375
4605
                               Table *table,
4376
4606
                               uint32_t ref,
4377
4607
                               uint32_t ref_key_parts,
4435
4665
{
4436
4666
  for (uint32_t keynr= 0; keynr < table->getShare()->sizeKeys(); keynr++)
4437
4667
  {
4438
 
    if (keynr == table->getShare()->getPrimaryKey() ||
 
4668
    if (keynr == table->getShare()->primary_key ||
4439
4669
         (table->key_info[keynr].flags & HA_NOSAME))
4440
4670
    {
4441
4671
      KeyInfo *keyinfo= table->key_info + keynr;
4473
4703
*/
4474
4704
bool find_field_in_order_list (Field *field, void *data)
4475
4705
{
4476
 
  Order *group= (Order *) data;
 
4706
  order_st *group= (order_st *) data;
4477
4707
  bool part_found= 0;
4478
 
  for (Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
4708
  for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4479
4709
  {
4480
4710
    Item *item= (*tmp_group->item)->real_item();
4481
4711
    if (item->type() == Item::FIELD_ITEM &&
4545
4775
  @retval
4546
4776
    1    We can use an index.
4547
4777
*/
4548
 
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
 
4778
bool test_if_skip_sort_order(JoinTable *tab, order_st *order, ha_rows select_limit, bool no_changes, const key_map *map)
4549
4779
{
4550
4780
  int32_t ref_key;
4551
4781
  uint32_t ref_key_parts;
4562
4792
  */
4563
4793
  usable_keys= *map;
4564
4794
 
4565
 
  for (Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
4795
  for (order_st *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4566
4796
  {
4567
4797
    Item *item= (*tmp_order->item)->real_item();
4568
4798
    if (item->type() != Item::FIELD_ITEM)
4740
4970
      if (keys.test(nr) &&
4741
4971
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
4742
4972
      {
4743
 
        bool is_covering= table->covering_keys.test(nr) || (nr == table->getShare()->getPrimaryKey() && table->cursor->primary_key_is_clustered());
 
4973
        bool is_covering= table->covering_keys.test(nr) || (nr == table->getShare()->primary_key && table->cursor->primary_key_is_clustered());
4744
4974
 
4745
4975
        /*
4746
4976
          Don't use an index scan with ORDER BY without limit.
4988
5218
    -1          Some fatal error
4989
5219
    1           No records
4990
5220
*/
4991
 
int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
 
5221
int create_sort_index(Session *session, Join *join, order_st *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
4992
5222
{
4993
5223
  uint32_t length= 0;
4994
5224
  ha_rows examined_rows;
5015
5245
                              is_order_by ?  &table->keys_in_use_for_order_by :
5016
5246
                              &table->keys_in_use_for_group_by))
5017
5247
    return(0);
5018
 
  for (Order *ord= join->order; ord; ord= ord->next)
 
5248
  for (order_st *ord= join->order; ord; ord= ord->next)
5019
5249
    length++;
5020
 
  if (!(join->sortorder= make_unireg_sortorder(order, &length, join->sortorder)))
5021
 
  {
5022
 
    return(-1);
5023
 
  }
 
5250
  if (!(join->sortorder=
 
5251
        make_unireg_sortorder(order, &length, join->sortorder)))
 
5252
    goto err;
5024
5253
 
5025
5254
  table->sort.io_cache= new internal::IO_CACHE;
5026
5255
  table->status=0;                              // May be wrong if quick_select
5055
5284
                                                                 &tab->ref,
5056
5285
                                                                 tab->found_records))))
5057
5286
      {
5058
 
        return(-1);
 
5287
        goto err;
5059
5288
      }
5060
5289
    }
5061
5290
  }
5062
5291
 
5063
 
  if (table->getShare()->getType())
 
5292
  if (table->getShare()->tmp_table)
5064
5293
    table->cursor->info(HA_STATUS_VARIABLE);    // Get record count
5065
 
 
5066
 
  FileSort filesort(*session);
5067
 
  table->sort.found_records=filesort.run(table,join->sortorder, length,
5068
 
                                         select, filesort_limit, 0,
5069
 
                                         examined_rows);
 
5294
  table->sort.found_records=filesort(session, table,join->sortorder, length,
 
5295
                                     select, filesort_limit, 0,
 
5296
                                     &examined_rows);
5070
5297
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
5071
5298
  if (select)
5072
5299
  {
5084
5311
    table->key_read=0;
5085
5312
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
5086
5313
  }
5087
 
 
5088
5314
  return(table->sort.found_records == HA_POS_ERROR);
 
5315
err:
 
5316
  return(-1);
5089
5317
}
5090
5318
 
5091
5319
int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having)
5096
5324
  int error;
5097
5325
  uint32_t reclength= table->getShare()->getRecordLength() - offset;
5098
5326
 
5099
 
  org_record=(char*) (record=table->getInsertRecord())+offset;
5100
 
  new_record=(char*) table->getUpdateRecord()+offset;
 
5327
  org_record=(char*) (record=table->record[0])+offset;
 
5328
  new_record=(char*) table->record[1]+offset;
5101
5329
 
5102
5330
  cursor->startTableScan(1);
5103
5331
  error=cursor->rnd_next(record);
5104
5332
  for (;;)
5105
5333
  {
5106
 
    if (session->getKilled())
 
5334
    if (session->killed)
5107
5335
    {
5108
5336
      session->send_kill_message();
5109
5337
      error=0;
5183
5411
                               uint32_t key_length,
5184
5412
                               Item *having)
5185
5413
{
5186
 
  unsigned char *key_pos, *record=table->getInsertRecord();
 
5414
  unsigned char *key_pos, *record=table->record[0];
5187
5415
  int error;
5188
5416
  Cursor *cursor= table->cursor;
5189
5417
  uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
5221
5449
  for (;;)
5222
5450
  {
5223
5451
    unsigned char *org_key_pos;
5224
 
    if (session->getKilled())
 
5452
    if (session->killed)
5225
5453
    {
5226
5454
      session->send_kill_message();
5227
5455
      error=0;
5275
5503
  return(1);
5276
5504
}
5277
5505
 
5278
 
SortField *make_unireg_sortorder(Order *order, uint32_t *length, SortField *sortorder)
 
5506
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length, SORT_FIELD *sortorder)
5279
5507
{
5280
5508
  uint32_t count;
5281
 
  SortField *sort,*pos;
 
5509
  SORT_FIELD *sort,*pos;
5282
5510
 
5283
5511
  count=0;
5284
 
  for (Order *tmp = order; tmp; tmp=tmp->next)
 
5512
  for (order_st *tmp = order; tmp; tmp=tmp->next)
5285
5513
    count++;
5286
5514
  if (!sortorder)
5287
 
    sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) *
 
5515
    sortorder= (SORT_FIELD*) memory::sql_alloc(sizeof(SORT_FIELD) *
5288
5516
                                       (max(count, *length) + 1));
5289
5517
  pos= sort= sortorder;
5290
5518
 
5406
5634
static bool find_order_in_list(Session *session, 
5407
5635
                               Item **ref_pointer_array, 
5408
5636
                               TableList *tables,
5409
 
                               Order *order,
 
5637
                               order_st *order,
5410
5638
                               List<Item> &fields,
5411
5639
                               List<Item> &all_fields,
5412
5640
                               bool is_group_field)
5438
5666
    return false;
5439
5667
  }
5440
5668
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
5441
 
  select_item= find_item_in_list(session, order_item, fields, &counter,
 
5669
  select_item= find_item_in_list(order_item, fields, &counter,
5442
5670
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
5443
5671
  if (!select_item)
5444
5672
    return true; /* The item is not unique, or some other error occured. */
5545
5773
                TableList *tables,
5546
5774
                            List<Item> &fields,
5547
5775
                List<Item> &all_fields,
5548
 
                Order *order)
 
5776
                order_st *order)
5549
5777
{
5550
5778
  session->where="order clause";
5551
5779
  for (; order; order=order->next)
5587
5815
                TableList *tables,
5588
5816
                      List<Item> &fields,
5589
5817
                List<Item> &all_fields,
5590
 
                Order *order,
 
5818
                order_st *order,
5591
5819
                      bool *hidden_group_fields)
5592
5820
{
5593
5821
  *hidden_group_fields=0;
5594
 
  Order *ord;
 
5822
  order_st *ord;
5595
5823
 
5596
5824
  if (!order)
5597
5825
    return 0;                           /* Everything is ok */
5681
5909
  Try to use the fields in the order given by 'order' to allow one to
5682
5910
  optimize away 'order by'.
5683
5911
*/
5684
 
Order *create_distinct_group(Session *session,
 
5912
order_st *create_distinct_group(Session *session,
5685
5913
                                Item **ref_pointer_array,
5686
 
                                Order *order_list,
 
5914
                                order_st *order_list,
5687
5915
                                List<Item> &fields,
5688
5916
                                List<Item> &,
5689
5917
                                bool *all_order_by_fields_used)
5690
5918
{
5691
5919
  List_iterator<Item> li(fields);
5692
5920
  Item *item;
5693
 
  Order *order,*group,**prev;
 
5921
  order_st *order,*group,**prev;
5694
5922
 
5695
5923
  *all_order_by_fields_used= 1;
5696
5924
  while ((item=li++))
5701
5929
  {
5702
5930
    if (order->in_field_list)
5703
5931
    {
5704
 
      Order *ord=(Order*) session->memdup((char*) order,sizeof(Order));
 
5932
      order_st *ord=(order_st*) session->memdup((char*) order,sizeof(order_st));
5705
5933
      if (!ord)
5706
5934
        return 0;
5707
5935
      *prev=ord;
5721
5949
        Don't put duplicate columns from the SELECT list into the
5722
5950
        GROUP BY list.
5723
5951
      */
5724
 
      Order *ord_iter;
 
5952
      order_st *ord_iter;
5725
5953
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
5726
5954
        if ((*ord_iter->item)->eq(item, 1))
5727
5955
          goto next_item;
5728
5956
 
5729
 
      Order *ord=(Order*) session->calloc(sizeof(Order));
 
5957
      order_st *ord=(order_st*) session->calloc(sizeof(order_st));
5730
5958
      if (!ord)
5731
5959
        return 0;
5732
5960
 
5913
6141
          saved value
5914
6142
        */
5915
6143
        field= item->field;
5916
 
        item->result_field=field->new_field(session->mem_root,field->getTable(), 1);
 
6144
        item->result_field=field->new_field(session->mem_root,field->table, 1);
5917
6145
              /*
5918
6146
                We need to allocate one extra byte for null handling and
5919
6147
                another extra byte to not get warnings from purify in
5925
6153
        {
5926
6154
          copy->set(tmp, item->result_field);
5927
6155
          item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
5928
 
#ifdef HAVE_VALGRIND
 
6156
#ifdef HAVE_purify
5929
6157
          copy->to_ptr[copy->from_length]= 0;
5930
6158
#endif
5931
6159
          copy++;
6046
6274
      }
6047
6275
      else if ((field= item->get_tmp_table_field()))
6048
6276
      {
6049
 
        if (item->type() == Item::SUM_FUNC_ITEM && field->getTable()->group)
 
6277
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
6050
6278
          item_field= ((Item_sum*) item)->result_item(field);
6051
6279
        else
6052
6280
          item_field= (Item*) new Item_field(field);
6198
6426
}
6199
6427
 
6200
6428
/** Copy result of functions to record in tmp_table. */
6201
 
bool copy_funcs(Item **func_ptr, const Session *session)
 
6429
void copy_funcs(Item **func_ptr)
6202
6430
{
6203
6431
  Item *func;
6204
6432
  for (; (func = *func_ptr) ; func_ptr++)
6205
 
  {
6206
6433
    func->save_in_result_field(1);
6207
 
    /*
6208
 
      Need to check the THD error state because Item::val_xxx() don't
6209
 
      return error code, but can generate errors
6210
 
      TODO: change it for a real status check when Item::val_xxx()
6211
 
      are extended to return status code.
6212
 
    */
6213
 
    if (session->is_error())
6214
 
      return true;
6215
 
  }
6216
 
  return false;
6217
6434
}
6218
6435
 
6219
6436
/**
6273
6490
  @retval
6274
6491
    1   on error
6275
6492
*/
6276
 
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
 
6493
bool change_group_ref(Session *session, Item_func *expr, order_st *group_list, bool *changed)
6277
6494
{
6278
6495
  if (expr->arg_count)
6279
6496
  {
6287
6504
      Item *item= *arg;
6288
6505
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6289
6506
      {
6290
 
        Order *group_tmp;
 
6507
        order_st *group_tmp;
6291
6508
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6292
6509
        {
6293
6510
          if (item->eq(*group_tmp->item,0))
6370
6587
void Select_Lex::print(Session *session, String *str, enum_query_type query_type)
6371
6588
{
6372
6589
  /* QQ: session may not be set for sub queries, but this should be fixed */
6373
 
  if(not session)
6374
 
    session= current_session;
6375
 
 
 
6590
  assert(session);
6376
6591
 
6377
6592
  str->append(STRING_WITH_LEN("select "));
6378
6593
 
6439
6654
  if (group_list.elements)
6440
6655
  {
6441
6656
    str->append(STRING_WITH_LEN(" group by "));
6442
 
    print_order(str, (Order *) group_list.first, query_type);
 
6657
    print_order(str, (order_st *) group_list.first, query_type);
6443
6658
    switch (olap)
6444
6659
    {
6445
6660
      case CUBE_TYPE:
6470
6685
  if (order_list.elements)
6471
6686
  {
6472
6687
    str->append(STRING_WITH_LEN(" order by "));
6473
 
    print_order(str, (Order *) order_list.first, query_type);
 
6688
    print_order(str, (order_st *) order_list.first, query_type);
6474
6689
  }
6475
6690
 
6476
6691
  // limit