~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: LinuxJedi
  • Date: 2010-08-12 17:36:08 UTC
  • mto: (1735.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1736.
  • Revision ID: linuxjedi@linuxjedi-laptop-20100812173608-ccr246iaa8gv3s97
Remove module pointer from registry when module doesn't load to avoid a double-free on shutdown.

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->getNestedJoin())
 
459
      fix_list_after_tbl_changes(new_parent, &table->getNestedJoin()->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
665
673
        else if (use->getKeypart() != 0)                // First found must be 0
666
674
          continue;
667
675
 
668
 
#ifdef HAVE_VALGRIND
 
676
#ifdef HAVE_purify
669
677
        /* Valgrind complains about overlapped memcpy when save_pos==use. */
670
678
        if (save_pos != use)
671
679
#endif
748
756
{
749
757
  List<Item_field> indexed_fields;
750
758
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
751
 
  Order      *cur_group;
 
759
  order_st      *cur_group;
752
760
  Item_field *cur_item;
753
761
  key_map possible_keys(0);
754
762
 
1208
1216
  return tmp;
1209
1217
}
1210
1218
 
 
1219
/*
 
1220
  Check if given expression uses only table fields covered by the given index
 
1221
 
 
1222
  SYNOPSIS
 
1223
    uses_index_fields_only()
 
1224
      item           Expression to check
 
1225
      tbl            The table having the index
 
1226
      keyno          The index number
 
1227
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
1228
 
 
1229
  DESCRIPTION
 
1230
    Check if given expression only uses fields covered by index #keyno in the
 
1231
    table tbl. The expression can use any fields in any other tables.
 
1232
 
 
1233
    The expression is guaranteed not to be AND or OR - those constructs are
 
1234
    handled outside of this function.
 
1235
 
 
1236
  RETURN
 
1237
    true   Yes
 
1238
    false  No
 
1239
*/
 
1240
static bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno, bool other_tbls_ok)
 
1241
{
 
1242
  if (item->const_item())
 
1243
    return true;
 
1244
 
 
1245
  /*
 
1246
    Don't push down the triggered conditions. Nested outer joins execution
 
1247
    code may need to evaluate a condition several times (both triggered and
 
1248
    untriggered), and there is no way to put thi
 
1249
    TODO: Consider cloning the triggered condition and using the copies for:
 
1250
      1. push the first copy down, to have most restrictive index condition
 
1251
         possible
 
1252
      2. Put the second copy into tab->select_cond.
 
1253
  */
 
1254
  if (item->type() == Item::FUNC_ITEM &&
 
1255
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
 
1256
    return false;
 
1257
 
 
1258
  if (!(item->used_tables() & tbl->map))
 
1259
    return other_tbls_ok;
 
1260
 
 
1261
  Item::Type item_type= item->type();
 
1262
  switch (item_type) {
 
1263
  case Item::FUNC_ITEM:
 
1264
    {
 
1265
      /* This is a function, apply condition recursively to arguments */
 
1266
      Item_func *item_func= (Item_func*)item;
 
1267
      Item **child;
 
1268
      Item **item_end= (item_func->arguments()) + item_func->argument_count();
 
1269
      for (child= item_func->arguments(); child != item_end; child++)
 
1270
      {
 
1271
        if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
 
1272
          return false;
 
1273
      }
 
1274
      return true;
 
1275
    }
 
1276
  case Item::COND_ITEM:
 
1277
    {
 
1278
      /* This is a function, apply condition recursively to arguments */
 
1279
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
 
1280
      Item *list_item;
 
1281
      while ((list_item=li++))
 
1282
      {
 
1283
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
 
1284
          return false;
 
1285
      }
 
1286
      return true;
 
1287
    }
 
1288
  case Item::FIELD_ITEM:
 
1289
    {
 
1290
      Item_field *item_field= (Item_field*)item;
 
1291
      if (item_field->field->getTable() != tbl)
 
1292
        return true;
 
1293
      return item_field->field->part_of_key.test(keyno);
 
1294
    }
 
1295
  case Item::REF_ITEM:
 
1296
    return uses_index_fields_only(item->real_item(), tbl, keyno,
 
1297
                                  other_tbls_ok);
 
1298
  default:
 
1299
    return false; /* Play it safe, don't push unknown non-const items */
 
1300
  }
 
1301
}
 
1302
 
1211
1303
#define ICP_COND_USES_INDEX_ONLY 10
1212
1304
 
 
1305
/*
 
1306
  Get a part of the condition that can be checked using only index fields
 
1307
 
 
1308
  SYNOPSIS
 
1309
    make_cond_for_index()
 
1310
      cond           The source condition
 
1311
      table          The table that is partially available
 
1312
      keyno          The index in the above table. Only fields covered by the index
 
1313
                     are available
 
1314
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
 
1315
 
 
1316
  DESCRIPTION
 
1317
    Get a part of the condition that can be checked when for the given table
 
1318
    we have values only of fields covered by some index. The condition may
 
1319
    refer to other tables, it is assumed that we have values of all of their
 
1320
    fields.
 
1321
 
 
1322
    Example:
 
1323
      make_cond_for_index(
 
1324
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
 
1325
          t2, keyno(t2.key1))
 
1326
      will return
 
1327
        "cond(t1.field) AND cond(t2.key2)"
 
1328
 
 
1329
  RETURN
 
1330
    Index condition, or NULL if no condition could be inferred.
 
1331
*/
 
1332
static Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno, bool other_tbls_ok)
 
1333
{
 
1334
  if (!cond)
 
1335
    return NULL;
 
1336
  if (cond->type() == Item::COND_ITEM)
 
1337
  {
 
1338
    uint32_t n_marked= 0;
 
1339
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
1340
    {
 
1341
      Item_cond_and *new_cond=new Item_cond_and;
 
1342
      if (!new_cond)
 
1343
        return (COND*) 0;
 
1344
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
1345
      Item *item;
 
1346
      while ((item=li++))
 
1347
      {
 
1348
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
1349
        if (fix)
 
1350
          new_cond->argument_list()->push_back(fix);
 
1351
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
1352
      }
 
1353
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
1354
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
1355
      switch (new_cond->argument_list()->elements) {
 
1356
      case 0:
 
1357
        return (COND*) 0;
 
1358
      case 1:
 
1359
        return new_cond->argument_list()->head();
 
1360
      default:
 
1361
        new_cond->quick_fix_field();
 
1362
        return new_cond;
 
1363
      }
 
1364
    }
 
1365
    else /* It's OR */
 
1366
    {
 
1367
      Item_cond_or *new_cond=new Item_cond_or;
 
1368
      if (!new_cond)
 
1369
        return (COND*) 0;
 
1370
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
1371
      Item *item;
 
1372
      while ((item=li++))
 
1373
      {
 
1374
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
1375
        if (!fix)
 
1376
          return (COND*) 0;
 
1377
        new_cond->argument_list()->push_back(fix);
 
1378
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
 
1379
      }
 
1380
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
 
1381
        cond->marker= ICP_COND_USES_INDEX_ONLY;
 
1382
      new_cond->quick_fix_field();
 
1383
      new_cond->top_level_item();
 
1384
      return new_cond;
 
1385
    }
 
1386
  }
 
1387
 
 
1388
  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
 
1389
    return (COND*) 0;
 
1390
  cond->marker= ICP_COND_USES_INDEX_ONLY;
 
1391
  return cond;
 
1392
}
 
1393
 
 
1394
 
 
1395
static Item *make_cond_remainder(Item *cond, bool exclude_index)
 
1396
{
 
1397
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
 
1398
    return 0; /* Already checked */
 
1399
 
 
1400
  if (cond->type() == Item::COND_ITEM)
 
1401
  {
 
1402
    table_map tbl_map= 0;
 
1403
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
 
1404
    {
 
1405
      /* Create new top level AND item */
 
1406
      Item_cond_and *new_cond=new Item_cond_and;
 
1407
      if (!new_cond)
 
1408
        return (COND*) 0;
 
1409
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
1410
      Item *item;
 
1411
      while ((item=li++))
 
1412
      {
 
1413
        Item *fix= make_cond_remainder(item, exclude_index);
 
1414
        if (fix)
 
1415
        {
 
1416
          new_cond->argument_list()->push_back(fix);
 
1417
          tbl_map |= fix->used_tables();
 
1418
        }
 
1419
      }
 
1420
      switch (new_cond->argument_list()->elements) {
 
1421
      case 0:
 
1422
        return (COND*) 0;
 
1423
      case 1:
 
1424
        return new_cond->argument_list()->head();
 
1425
      default:
 
1426
        new_cond->quick_fix_field();
 
1427
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
1428
        return new_cond;
 
1429
      }
 
1430
    }
 
1431
    else /* It's OR */
 
1432
    {
 
1433
      Item_cond_or *new_cond=new Item_cond_or;
 
1434
      if (!new_cond)
 
1435
        return (COND*) 0;
 
1436
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
 
1437
      Item *item;
 
1438
      while ((item=li++))
 
1439
      {
 
1440
        Item *fix= make_cond_remainder(item, false);
 
1441
        if (!fix)
 
1442
          return (COND*) 0;
 
1443
        new_cond->argument_list()->push_back(fix);
 
1444
        tbl_map |= fix->used_tables();
 
1445
      }
 
1446
      new_cond->quick_fix_field();
 
1447
      ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
 
1448
      new_cond->top_level_item();
 
1449
      return new_cond;
 
1450
    }
 
1451
  }
 
1452
  return cond;
 
1453
}
1213
1454
 
1214
1455
/**
1215
1456
  cleanup JoinTable.
1221
1462
  delete quick;
1222
1463
  quick= 0;
1223
1464
  if (cache.buff)
1224
 
  {
1225
 
    size_t size= cache.end - cache.buff;
1226
 
    global_join_buffer.sub(size);
1227
1465
    free(cache.buff);
1228
 
  }
1229
1466
  cache.buff= 0;
1230
1467
  limit= 0;
1231
1468
  if (table)
1245
1482
  read_record.end_read_record();
1246
1483
}
1247
1484
 
1248
 
bool only_eq_ref_tables(Join *join,Order *order,table_map tables)
 
1485
bool only_eq_ref_tables(Join *join,order_st *order,table_map tables)
1249
1486
{
1250
1487
  for (JoinTable **tab=join->map2table ; tables ; tab++, tables>>=1)
1251
1488
  {
1274
1511
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
1275
1512
  @endcode
1276
1513
*/
1277
 
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
 
1514
bool eq_ref_table(Join *join, order_st *start_order, JoinTable *tab)
1278
1515
{
1279
1516
  if (tab->cached_eq_ref_table)                 // If cached
1280
1517
    return tab->eq_ref_table;
1293
1530
  {
1294
1531
    if (! (*ref_item)->const_item())
1295
1532
    {                                           // Not a const ref
1296
 
      Order *order;
 
1533
      order_st *order;
1297
1534
      for (order=start_order ; order ; order=order->next)
1298
1535
      {
1299
1536
        if ((*ref_item)->eq(order->item[0],0))
2377
2614
    Item *item;
2378
2615
    while ((item=li++))
2379
2616
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
2380
 
 
2381
2617
    return;
2382
2618
  }
2383
2619
  if (cond->eq_cmp_result() == Item::COND_OK)
2396
2632
       left_item->collation.collation == value->collation.collation))
2397
2633
  {
2398
2634
    Item *tmp=value->clone_item();
 
2635
    tmp->collation.set(right_item->collation);
 
2636
 
2399
2637
    if (tmp)
2400
2638
    {
2401
 
      tmp->collation.set(right_item->collation);
2402
2639
      session->change_item_tree(args + 1, tmp);
2403
2640
      func->update_used_tables();
2404
2641
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
2418
2655
            right_item->collation.collation == value->collation.collation))
2419
2656
  {
2420
2657
    Item *tmp= value->clone_item();
 
2658
    tmp->collation.set(left_item->collation);
 
2659
 
2421
2660
    if (tmp)
2422
2661
    {
2423
 
      tmp->collation.set(left_item->collation);
2424
2662
      session->change_item_tree(args, tmp);
2425
2663
      value= tmp;
2426
2664
      func->update_used_tables();
2490
2728
      for (vector<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
2491
2729
      {
2492
2730
        Item **args= iter->cmp_func->arguments();
2493
 
        if (not args[0]->const_item())
 
2731
        if (!args[0]->const_item())
2494
2732
        {
2495
 
          change_cond_ref_to_const(session, save_list, iter->and_level,
2496
 
                                   iter->and_level, args[0], args[1] );
 
2733
          change_cond_ref_to_const( session, save, iter->and_level,
 
2734
                                    iter->and_level, args[0], args[1] );
2497
2735
        }
2498
2736
      }
2499
2737
    }
3150
3388
      rc= sub_select(join,join_tab,end_of_records);
3151
3389
    return rc;
3152
3390
  }
3153
 
  if (join->session->getKilled())               // If aborted by user
 
3391
  if (join->session->killed)            // If aborted by user
3154
3392
  {
3155
3393
    join->session->send_kill_message();
3156
3394
    return NESTED_LOOP_KILLED;
3961
4199
  Table *table=join->tmp_table;
3962
4200
  int     idx= -1;
3963
4201
 
3964
 
  if (join->session->getKilled())
 
4202
  if (join->session->killed)
3965
4203
  {                                             // Aborted by user
3966
4204
    join->session->send_kill_message();
3967
4205
    return NESTED_LOOP_KILLED;
4009
4247
    if (idx < (int) join->send_group_parts)
4010
4248
    {
4011
4249
      copy_fields(&join->tmp_table_param);
4012
 
      if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
4013
 
        return NESTED_LOOP_ERROR;
 
4250
      copy_funcs(join->tmp_table_param.items_to_copy);
4014
4251
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
4015
4252
        return NESTED_LOOP_ERROR;
4016
4253
      return NESTED_LOOP_OK;
4027
4264
  outer join table.
4028
4265
  We can't remove tests that are made against columns which are stored
4029
4266
  in sorted order.
 
4267
*****************************************************************************/
 
4268
 
 
4269
/**
4030
4270
  @return
4031
 
    1 if right_item used is a removable reference key on left_item
4032
 
    0 otherwise.
4033
 
****************************************************************************/
 
4271
    1 if right_item is used removable reference key on left_item
 
4272
*/
4034
4273
bool test_if_ref(Item_field *left_item,Item *right_item)
4035
4274
{
4036
4275
  Field *field=left_item->field;
4231
4470
    }
4232
4471
 
4233
4472
    for (part=0 ; part < ref_parts ; part++,key_part++)
4234
 
    {
4235
4473
      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
 
      {
 
4474
          !(key_part->key_part_flag & HA_PART_KEY_SEG))
4241
4475
        return table->reginfo.join_tab->ref.items[part];
4242
 
      }
4243
 
    }
4244
4476
  }
4245
4477
  return (Item*) 0;
4246
4478
}
4265
4497
  @retval
4266
4498
    -1   Reverse key can be used
4267
4499
*/
4268
 
static int test_if_order_by_key(Order *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
 
4500
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
4269
4501
{
4270
4502
  KeyPartInfo *key_part= NULL;
4271
4503
  KeyPartInfo *key_part_end= NULL;
4371
4603
    - MAX_KEY                   If we can't use other key
4372
4604
    - the number of found key   Otherwise
4373
4605
*/
4374
 
static uint32_t test_if_subkey(Order *order,
 
4606
static uint32_t test_if_subkey(order_st *order,
4375
4607
                               Table *table,
4376
4608
                               uint32_t ref,
4377
4609
                               uint32_t ref_key_parts,
4473
4705
*/
4474
4706
bool find_field_in_order_list (Field *field, void *data)
4475
4707
{
4476
 
  Order *group= (Order *) data;
 
4708
  order_st *group= (order_st *) data;
4477
4709
  bool part_found= 0;
4478
 
  for (Order *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
4710
  for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
4479
4711
  {
4480
4712
    Item *item= (*tmp_group->item)->real_item();
4481
4713
    if (item->type() == Item::FIELD_ITEM &&
4545
4777
  @retval
4546
4778
    1    We can use an index.
4547
4779
*/
4548
 
bool test_if_skip_sort_order(JoinTable *tab, Order *order, ha_rows select_limit, bool no_changes, const key_map *map)
 
4780
bool test_if_skip_sort_order(JoinTable *tab, order_st *order, ha_rows select_limit, bool no_changes, const key_map *map)
4549
4781
{
4550
4782
  int32_t ref_key;
4551
4783
  uint32_t ref_key_parts;
4562
4794
  */
4563
4795
  usable_keys= *map;
4564
4796
 
4565
 
  for (Order *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
4797
  for (order_st *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
4566
4798
  {
4567
4799
    Item *item= (*tmp_order->item)->real_item();
4568
4800
    if (item->type() != Item::FIELD_ITEM)
4988
5220
    -1          Some fatal error
4989
5221
    1           No records
4990
5222
*/
4991
 
int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
 
5223
int create_sort_index(Session *session, Join *join, order_st *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
4992
5224
{
4993
5225
  uint32_t length= 0;
4994
5226
  ha_rows examined_rows;
5015
5247
                              is_order_by ?  &table->keys_in_use_for_order_by :
5016
5248
                              &table->keys_in_use_for_group_by))
5017
5249
    return(0);
5018
 
  for (Order *ord= join->order; ord; ord= ord->next)
 
5250
  for (order_st *ord= join->order; ord; ord= ord->next)
5019
5251
    length++;
5020
 
  if (!(join->sortorder= make_unireg_sortorder(order, &length, join->sortorder)))
5021
 
  {
5022
 
    return(-1);
5023
 
  }
 
5252
  if (!(join->sortorder=
 
5253
        make_unireg_sortorder(order, &length, join->sortorder)))
 
5254
    goto err;
5024
5255
 
5025
5256
  table->sort.io_cache= new internal::IO_CACHE;
5026
5257
  table->status=0;                              // May be wrong if quick_select
5055
5286
                                                                 &tab->ref,
5056
5287
                                                                 tab->found_records))))
5057
5288
      {
5058
 
        return(-1);
 
5289
        goto err;
5059
5290
      }
5060
5291
    }
5061
5292
  }
5062
5293
 
5063
5294
  if (table->getShare()->getType())
5064
5295
    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);
 
5296
  table->sort.found_records=filesort(session, table,join->sortorder, length,
 
5297
                                     select, filesort_limit, 0,
 
5298
                                     &examined_rows);
5070
5299
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
5071
5300
  if (select)
5072
5301
  {
5084
5313
    table->key_read=0;
5085
5314
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
5086
5315
  }
5087
 
 
5088
5316
  return(table->sort.found_records == HA_POS_ERROR);
 
5317
err:
 
5318
  return(-1);
5089
5319
}
5090
5320
 
5091
5321
int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having)
5103
5333
  error=cursor->rnd_next(record);
5104
5334
  for (;;)
5105
5335
  {
5106
 
    if (session->getKilled())
 
5336
    if (session->killed)
5107
5337
    {
5108
5338
      session->send_kill_message();
5109
5339
      error=0;
5221
5451
  for (;;)
5222
5452
  {
5223
5453
    unsigned char *org_key_pos;
5224
 
    if (session->getKilled())
 
5454
    if (session->killed)
5225
5455
    {
5226
5456
      session->send_kill_message();
5227
5457
      error=0;
5275
5505
  return(1);
5276
5506
}
5277
5507
 
5278
 
SortField *make_unireg_sortorder(Order *order, uint32_t *length, SortField *sortorder)
 
5508
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length, SORT_FIELD *sortorder)
5279
5509
{
5280
5510
  uint32_t count;
5281
 
  SortField *sort,*pos;
 
5511
  SORT_FIELD *sort,*pos;
5282
5512
 
5283
5513
  count=0;
5284
 
  for (Order *tmp = order; tmp; tmp=tmp->next)
 
5514
  for (order_st *tmp = order; tmp; tmp=tmp->next)
5285
5515
    count++;
5286
5516
  if (!sortorder)
5287
 
    sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) *
 
5517
    sortorder= (SORT_FIELD*) memory::sql_alloc(sizeof(SORT_FIELD) *
5288
5518
                                       (max(count, *length) + 1));
5289
5519
  pos= sort= sortorder;
5290
5520
 
5406
5636
static bool find_order_in_list(Session *session, 
5407
5637
                               Item **ref_pointer_array, 
5408
5638
                               TableList *tables,
5409
 
                               Order *order,
 
5639
                               order_st *order,
5410
5640
                               List<Item> &fields,
5411
5641
                               List<Item> &all_fields,
5412
5642
                               bool is_group_field)
5545
5775
                TableList *tables,
5546
5776
                            List<Item> &fields,
5547
5777
                List<Item> &all_fields,
5548
 
                Order *order)
 
5778
                order_st *order)
5549
5779
{
5550
5780
  session->where="order clause";
5551
5781
  for (; order; order=order->next)
5587
5817
                TableList *tables,
5588
5818
                      List<Item> &fields,
5589
5819
                List<Item> &all_fields,
5590
 
                Order *order,
 
5820
                order_st *order,
5591
5821
                      bool *hidden_group_fields)
5592
5822
{
5593
5823
  *hidden_group_fields=0;
5594
 
  Order *ord;
 
5824
  order_st *ord;
5595
5825
 
5596
5826
  if (!order)
5597
5827
    return 0;                           /* Everything is ok */
5681
5911
  Try to use the fields in the order given by 'order' to allow one to
5682
5912
  optimize away 'order by'.
5683
5913
*/
5684
 
Order *create_distinct_group(Session *session,
 
5914
order_st *create_distinct_group(Session *session,
5685
5915
                                Item **ref_pointer_array,
5686
 
                                Order *order_list,
 
5916
                                order_st *order_list,
5687
5917
                                List<Item> &fields,
5688
5918
                                List<Item> &,
5689
5919
                                bool *all_order_by_fields_used)
5690
5920
{
5691
5921
  List_iterator<Item> li(fields);
5692
5922
  Item *item;
5693
 
  Order *order,*group,**prev;
 
5923
  order_st *order,*group,**prev;
5694
5924
 
5695
5925
  *all_order_by_fields_used= 1;
5696
5926
  while ((item=li++))
5701
5931
  {
5702
5932
    if (order->in_field_list)
5703
5933
    {
5704
 
      Order *ord=(Order*) session->memdup((char*) order,sizeof(Order));
 
5934
      order_st *ord=(order_st*) session->memdup((char*) order,sizeof(order_st));
5705
5935
      if (!ord)
5706
5936
        return 0;
5707
5937
      *prev=ord;
5721
5951
        Don't put duplicate columns from the SELECT list into the
5722
5952
        GROUP BY list.
5723
5953
      */
5724
 
      Order *ord_iter;
 
5954
      order_st *ord_iter;
5725
5955
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
5726
5956
        if ((*ord_iter->item)->eq(item, 1))
5727
5957
          goto next_item;
5728
5958
 
5729
 
      Order *ord=(Order*) session->calloc(sizeof(Order));
 
5959
      order_st *ord=(order_st*) session->calloc(sizeof(order_st));
5730
5960
      if (!ord)
5731
5961
        return 0;
5732
5962
 
5925
6155
        {
5926
6156
          copy->set(tmp, item->result_field);
5927
6157
          item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
5928
 
#ifdef HAVE_VALGRIND
 
6158
#ifdef HAVE_purify
5929
6159
          copy->to_ptr[copy->from_length]= 0;
5930
6160
#endif
5931
6161
          copy++;
6198
6428
}
6199
6429
 
6200
6430
/** Copy result of functions to record in tmp_table. */
6201
 
bool copy_funcs(Item **func_ptr, const Session *session)
 
6431
void copy_funcs(Item **func_ptr)
6202
6432
{
6203
6433
  Item *func;
6204
6434
  for (; (func = *func_ptr) ; func_ptr++)
6205
 
  {
6206
6435
    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
6436
}
6218
6437
 
6219
6438
/**
6273
6492
  @retval
6274
6493
    1   on error
6275
6494
*/
6276
 
bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed)
 
6495
bool change_group_ref(Session *session, Item_func *expr, order_st *group_list, bool *changed)
6277
6496
{
6278
6497
  if (expr->arg_count)
6279
6498
  {
6287
6506
      Item *item= *arg;
6288
6507
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
6289
6508
      {
6290
 
        Order *group_tmp;
 
6509
        order_st *group_tmp;
6291
6510
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
6292
6511
        {
6293
6512
          if (item->eq(*group_tmp->item,0))
6370
6589
void Select_Lex::print(Session *session, String *str, enum_query_type query_type)
6371
6590
{
6372
6591
  /* QQ: session may not be set for sub queries, but this should be fixed */
6373
 
  if(not session)
6374
 
    session= current_session;
6375
 
 
 
6592
  assert(session);
6376
6593
 
6377
6594
  str->append(STRING_WITH_LEN("select "));
6378
6595
 
6439
6656
  if (group_list.elements)
6440
6657
  {
6441
6658
    str->append(STRING_WITH_LEN(" group by "));
6442
 
    print_order(str, (Order *) group_list.first, query_type);
 
6659
    print_order(str, (order_st *) group_list.first, query_type);
6443
6660
    switch (olap)
6444
6661
    {
6445
6662
      case CUBE_TYPE:
6470
6687
  if (order_list.elements)
6471
6688
  {
6472
6689
    str->append(STRING_WITH_LEN(" order by "));
6473
 
    print_order(str, (Order *) order_list.first, query_type);
 
6690
    print_order(str, (order_st *) order_list.first, query_type);
6474
6691
  }
6475
6692
 
6476
6693
  // limit