~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/optimizer/range.cc

  • Committer: Paul McCullagh
  • Date: 2010-05-26 10:17:56 UTC
  • mto: (1567.1.3 new-staging)
  • mto: This revision was merged to the branch mainline in revision 1568.
  • Revision ID: paul.mccullagh@primebase.org-20100526101756-3qdwb9id3qhog0z1
Adjust for namespace changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
109
109
#include <vector>
110
110
#include <algorithm>
111
111
 
112
 
#include <boost/dynamic_bitset.hpp>
113
 
 
114
112
#include "drizzled/sql_base.h"
115
113
#include "drizzled/sql_select.h"
116
114
#include "drizzled/error.h"
206
204
  cost->zero();
207
205
  if (table->cursor->primary_key_is_clustered())
208
206
  {
209
 
    cost->setIOCount(table->cursor->read_time(table->getShare()->getPrimaryKey(),
 
207
    cost->setIOCount(table->cursor->read_time(table->getShare()->primary_key,
210
208
                                             static_cast<uint32_t>(nrows),
211
209
                                             nrows));
212
210
  }
230
228
  }
231
229
}
232
230
 
 
231
struct st_ror_scan_info;
 
232
 
233
233
static optimizer::SEL_TREE * get_mm_parts(optimizer::RangeParameter *param,
234
234
                               COND *cond_func,
235
235
                               Field *field,
248
248
 
249
249
static bool is_key_scan_ror(optimizer::Parameter *param, uint32_t keynr, uint8_t nparts);
250
250
 
251
 
static ha_rows check_quick_select(Session *session,
252
 
                                  optimizer::Parameter *param,
 
251
static ha_rows check_quick_select(optimizer::Parameter *param,
253
252
                                  uint32_t idx,
254
253
                                  bool index_only,
255
254
                                  optimizer::SEL_ARG *tree,
258
257
                                  uint32_t *bufsize,
259
258
                                  optimizer::CostVector *cost);
260
259
 
261
 
static optimizer::RangeReadPlan *get_key_scans_params(Session *session,
262
 
                                                      optimizer::Parameter *param,
 
260
static optimizer::RangeReadPlan *get_key_scans_params(optimizer::Parameter *param,
263
261
                                                      optimizer::SEL_TREE *tree,
264
262
                                                      bool index_read_must_be_used,
265
263
                                                      bool update_tbl_stats,
277
275
                                                                 double read_time);
278
276
 
279
277
static
280
 
optimizer::TableReadPlan *get_best_disjunct_quick(Session *session,
281
 
                                                  optimizer::Parameter *param,
 
278
optimizer::TableReadPlan *get_best_disjunct_quick(optimizer::Parameter *param,
282
279
                                                  optimizer::SEL_IMERGE *imerge,
283
280
                                                  double read_time);
284
281
 
385
382
 
386
383
void optimizer::SqlSelect::cleanup()
387
384
{
388
 
  if (quick)
389
 
  {
390
 
    delete quick;
391
 
    quick= NULL;
392
 
  }
393
 
 
 
385
  delete quick;
 
386
  quick= 0;
394
387
  if (free_cond)
395
388
  {
396
389
    free_cond= 0;
397
390
    delete cond;
398
391
    cond= 0;
399
392
  }
400
 
  file->close_cached_file();
 
393
  close_cached_file(file);
401
394
}
402
395
 
403
396
 
464
457
    MAX_KEY if no such index was found.
465
458
*/
466
459
 
467
 
uint32_t optimizer::get_index_for_order(Table *table, Order *order, ha_rows limit)
 
460
uint32_t optimizer::get_index_for_order(Table *table, order_st *order, ha_rows limit)
468
461
{
469
462
  uint32_t idx;
470
463
  uint32_t match_key= MAX_KEY, match_key_len= MAX_KEY_LENGTH + 1;
471
 
  Order *ord;
 
464
  order_st *ord;
472
465
 
473
466
  for (ord= order; ord; ord= ord->next)
474
467
    if (!ord->asc)
475
468
      return MAX_KEY;
476
469
 
477
 
  for (idx= 0; idx < table->getShare()->sizeKeys(); idx++)
 
470
  for (idx= 0; idx < table->getShare()->keys; idx++)
478
471
  {
479
472
    if (!(table->keys_in_use_for_query.test(idx)))
480
473
      continue;
543
536
static int fill_used_fields_bitmap(optimizer::Parameter *param)
544
537
{
545
538
  Table *table= param->table;
 
539
  my_bitmap_map *tmp;
546
540
  uint32_t pk;
547
 
  param->tmp_covered_fields.clear();
548
 
  param->needed_fields.resize(table->getShare()->sizeFields());
549
 
  param->needed_fields.reset();
550
 
 
551
 
  param->needed_fields|= *table->read_set;
552
 
  param->needed_fields|= *table->write_set;
553
 
 
554
 
  pk= param->table->getShare()->getPrimaryKey();
 
541
  param->tmp_covered_fields.setBitmap(0);
 
542
  param->fields_bitmap_size= table->getShare()->column_bitmap_size;
 
543
  if (!(tmp= (my_bitmap_map*) param->mem_root->alloc_root(param->fields_bitmap_size)) ||
 
544
      param->needed_fields.init(tmp, table->getShare()->fields))
 
545
  {
 
546
    return 1;
 
547
  }
 
548
 
 
549
  param->needed_fields= *table->read_set;
 
550
  bitmap_union(&param->needed_fields, table->write_set);
 
551
 
 
552
  pk= param->table->getShare()->primary_key;
555
553
  if (pk != MAX_KEY && param->table->cursor->primary_key_is_clustered())
556
554
  {
557
555
    /* The table uses clustered PK and it is not internally generated */
559
557
    KeyPartInfo *key_part_end= key_part +
560
558
                                 param->table->key_info[pk].key_parts;
561
559
    for (;key_part != key_part_end; ++key_part)
562
 
      param->needed_fields.reset(key_part->fieldnr-1);
 
560
      param->needed_fields.clearBit(key_part->fieldnr-1);
563
561
  }
564
562
  return 0;
565
563
}
640
638
{
641
639
  uint32_t idx;
642
640
  double scan_time;
643
 
  if (quick)
644
 
  {
645
 
    delete quick;
646
 
    quick= NULL;
647
 
  }
 
641
  delete quick;
 
642
  quick=0;
648
643
  needed_reg.reset();
649
644
  quick_keys.reset();
650
645
  if (keys_to_use.none())
706
701
      This is used in get_mm_parts function.
707
702
    */
708
703
    key_info= head->key_info;
709
 
    for (idx=0 ; idx < head->getShare()->sizeKeys() ; idx++, key_info++)
 
704
    for (idx=0 ; idx < head->getShare()->keys ; idx++, key_info++)
710
705
    {
711
706
      KeyPartInfo *key_part_info;
712
707
      if (! keys_to_use.test(idx))
796
791
        bool can_build_covering= false;
797
792
 
798
793
        /* Get best 'range' plan and prepare data for making other plans */
799
 
        if ((range_trp= get_key_scans_params(session, &param, tree, false, true,
 
794
        if ((range_trp= get_key_scans_params(&param, tree, false, true,
800
795
                                             best_read_time)))
801
796
        {
802
797
          best_trp= range_trp;
839
834
        List_iterator_fast<optimizer::SEL_IMERGE> it(tree->merges);
840
835
        while ((imerge= it++))
841
836
        {
842
 
          new_conj_trp= get_best_disjunct_quick(session, &param, imerge, best_read_time);
 
837
          new_conj_trp= get_best_disjunct_quick(&param, imerge, best_read_time);
843
838
          if (new_conj_trp)
844
839
            set_if_smaller(param.table->quick_condition_rows,
845
840
                           new_conj_trp->records);
860
855
      records= best_trp->records;
861
856
      if (! (quick= best_trp->make_quick(&param, true)) || quick->init())
862
857
      {
863
 
        /* quick can already be free here */
864
 
        if (quick)
865
 
        {
866
 
          delete quick;
867
 
          quick= NULL;
868
 
        }
 
858
        delete quick;
 
859
        quick= NULL;
869
860
      }
870
861
    }
871
862
 
886
877
  Get best plan for a optimizer::SEL_IMERGE disjunctive expression.
887
878
  SYNOPSIS
888
879
    get_best_disjunct_quick()
889
 
      session
890
880
      param     Parameter from check_quick_select function
891
881
      imerge    Expression to use
892
882
      read_time Don't create scans with cost > read_time
949
939
*/
950
940
 
951
941
static
952
 
optimizer::TableReadPlan *get_best_disjunct_quick(Session *session,
953
 
                                                  optimizer::Parameter *param,
 
942
optimizer::TableReadPlan *get_best_disjunct_quick(optimizer::Parameter *param,
954
943
                                                  optimizer::SEL_IMERGE *imerge,
955
944
                                                  double read_time)
956
945
{
988
977
       ptree != imerge->trees_next;
989
978
       ptree++, cur_child++)
990
979
  {
991
 
    if (!(*cur_child= get_key_scans_params(session, param, *ptree, true, false, read_time)))
 
980
    if (!(*cur_child= get_key_scans_params(param, *ptree, true, false, read_time)))
992
981
    {
993
982
      /*
994
983
        One of index scans in this index_merge is more expensive than entire
1006
995
    all_scans_rors &= (*cur_child)->is_ror;
1007
996
    if (pk_is_clustered &&
1008
997
        param->real_keynr[(*cur_child)->key_idx] ==
1009
 
        param->table->getShare()->getPrimaryKey())
 
998
        param->table->getShare()->primary_key)
1010
999
    {
1011
1000
      cpk_scan= cur_child;
1012
1001
      cpk_scan_records= (*cur_child)->records;
1185
1174
}
1186
1175
 
1187
1176
 
 
1177
typedef struct st_ror_scan_info
 
1178
{
 
1179
  uint32_t      idx;      /* # of used key in param->keys */
 
1180
  uint32_t      keynr;    /* # of used key in table */
 
1181
  ha_rows   records;  /* estimate of # records this scan will return */
 
1182
 
 
1183
  /* Set of intervals over key fields that will be used for row retrieval. */
 
1184
  optimizer::SEL_ARG   *sel_arg;
 
1185
 
 
1186
  /* Fields used in the query and covered by this ROR scan. */
 
1187
  MyBitmap covered_fields;
 
1188
  uint32_t      used_fields_covered; /* # of set bits in covered_fields */
 
1189
  int       key_rec_length; /* length of key record (including rowid) */
 
1190
 
 
1191
  /*
 
1192
    Cost of reading all index records with values in sel_arg intervals set
 
1193
    (assuming there is no need to access full table records)
 
1194
  */
 
1195
  double    index_read_cost;
 
1196
  uint32_t      first_uncovered_field; /* first unused bit in covered_fields */
 
1197
  uint32_t      key_components; /* # of parts in the key */
 
1198
} ROR_SCAN_INFO;
 
1199
 
1188
1200
 
1189
1201
/*
1190
 
  Create optimizer::RorScanInfo* structure with a single ROR scan on index idx using
 
1202
  Create ROR_SCAN_INFO* structure with a single ROR scan on index idx using
1191
1203
  sel_arg set of intervals.
1192
1204
 
1193
1205
  SYNOPSIS
1202
1214
*/
1203
1215
 
1204
1216
static
1205
 
optimizer::RorScanInfo *make_ror_scan(const optimizer::Parameter *param, int idx, optimizer::SEL_ARG *sel_arg)
 
1217
ROR_SCAN_INFO *make_ror_scan(const optimizer::Parameter *param, int idx, optimizer::SEL_ARG *sel_arg)
1206
1218
{
1207
 
  optimizer::RorScanInfo *ror_scan= NULL;
 
1219
  ROR_SCAN_INFO *ror_scan;
 
1220
  my_bitmap_map *bitmap_buf;
1208
1221
 
1209
1222
  uint32_t keynr;
1210
1223
 
1211
 
  if (!(ror_scan= (optimizer::RorScanInfo*)param->mem_root->alloc_root(sizeof(optimizer::RorScanInfo))))
 
1224
  if (!(ror_scan= (ROR_SCAN_INFO*)param->mem_root->alloc_root(sizeof(ROR_SCAN_INFO))))
1212
1225
    return NULL;
1213
1226
 
1214
1227
  ror_scan->idx= idx;
1218
1231
  ror_scan->sel_arg= sel_arg;
1219
1232
  ror_scan->records= param->table->quick_rows[keynr];
1220
1233
 
1221
 
  ror_scan->covered_fields_size= param->table->getShare()->sizeFields();
1222
 
  boost::dynamic_bitset<> tmp_bitset(param->table->getShare()->sizeFields());
1223
 
  tmp_bitset.reset();
 
1234
  if (!(bitmap_buf= (my_bitmap_map*) param->mem_root->alloc_root(param->fields_bitmap_size)))
 
1235
  {
 
1236
    return NULL;
 
1237
  }
 
1238
 
 
1239
  if (ror_scan->covered_fields.init(bitmap_buf, param->table->getShare()->fields))
 
1240
  {
 
1241
    return NULL;
 
1242
  }
 
1243
  ror_scan->covered_fields.clearAll();
1224
1244
 
1225
1245
  KeyPartInfo *key_part= param->table->key_info[keynr].key_part;
1226
1246
  KeyPartInfo *key_part_end= key_part +
1227
1247
                               param->table->key_info[keynr].key_parts;
1228
 
  for (; key_part != key_part_end; ++key_part)
 
1248
  for (;key_part != key_part_end; ++key_part)
1229
1249
  {
1230
 
    if (param->needed_fields.test(key_part->fieldnr-1))
1231
 
      tmp_bitset.set(key_part->fieldnr-1);
 
1250
    if (param->needed_fields.isBitSet(key_part->fieldnr-1))
 
1251
      ror_scan->covered_fields.setBit(key_part->fieldnr-1);
1232
1252
  }
1233
1253
  double rows= rows2double(param->table->quick_rows[ror_scan->keynr]);
1234
1254
  ror_scan->index_read_cost=
1235
1255
    param->table->cursor->index_only_read_time(ror_scan->keynr, rows);
1236
 
  ror_scan->covered_fields= tmp_bitset.to_ulong();
1237
 
  return ror_scan;
 
1256
  return(ror_scan);
1238
1257
}
1239
1258
 
1240
1259
 
1241
1260
/*
1242
 
  Compare two optimizer::RorScanInfo** by  E(#records_matched) * key_record_length.
 
1261
  Compare two ROR_SCAN_INFO** by  E(#records_matched) * key_record_length.
1243
1262
  SYNOPSIS
1244
1263
    cmp_ror_scan_info()
1245
1264
      a ptr to first compared value
1251
1270
    1 a > b
1252
1271
*/
1253
1272
 
1254
 
static int cmp_ror_scan_info(optimizer::RorScanInfo** a, optimizer::RorScanInfo** b)
 
1273
static int cmp_ror_scan_info(ROR_SCAN_INFO** a, ROR_SCAN_INFO** b)
1255
1274
{
1256
1275
  double val1= rows2double((*a)->records) * (*a)->key_rec_length;
1257
1276
  double val2= rows2double((*b)->records) * (*b)->key_rec_length;
1260
1279
 
1261
1280
 
1262
1281
/*
1263
 
  Compare two optimizer::RorScanInfo** by
 
1282
  Compare two ROR_SCAN_INFO** by
1264
1283
   (#covered fields in F desc,
1265
1284
    #components asc,
1266
1285
    number of first not covered component asc)
1276
1295
    1 a > b
1277
1296
*/
1278
1297
 
1279
 
static int cmp_ror_scan_info_covering(optimizer::RorScanInfo** a, optimizer::RorScanInfo** b)
 
1298
static int cmp_ror_scan_info_covering(ROR_SCAN_INFO** a, ROR_SCAN_INFO** b)
1280
1299
{
1281
1300
  if ((*a)->used_fields_covered > (*b)->used_fields_covered)
1282
1301
    return -1;
1294
1313
}
1295
1314
 
1296
1315
/* Auxiliary structure for incremental ROR-intersection creation */
1297
 
typedef struct st_ror_intersect_info
 
1316
typedef struct
1298
1317
{
1299
 
  st_ror_intersect_info()
1300
 
    :
1301
 
      param(NULL),
1302
 
      covered_fields(),
1303
 
      out_rows(0.0),
1304
 
      is_covering(false),
1305
 
      index_records(0),
1306
 
      index_scan_costs(0.0),
1307
 
      total_cost(0.0)
1308
 
  {}
1309
 
 
1310
 
  st_ror_intersect_info(const optimizer::Parameter *in_param)
1311
 
    :
1312
 
      param(in_param),
1313
 
      covered_fields(in_param->table->getShare()->sizeFields()),
1314
 
      out_rows(in_param->table->cursor->stats.records),
1315
 
      is_covering(false),
1316
 
      index_records(0),
1317
 
      index_scan_costs(0.0),
1318
 
      total_cost(0.0)
1319
 
  {
1320
 
    covered_fields.reset();
1321
 
  }
1322
 
 
1323
1318
  const optimizer::Parameter *param;
1324
 
  boost::dynamic_bitset<> covered_fields; /* union of fields covered by all scans */
 
1319
  MyBitmap covered_fields; /* union of fields covered by all scans */
1325
1320
  /*
1326
1321
    Fraction of table records that satisfies conditions of all scans.
1327
1322
    This is the number of full records that will be retrieved if a
1337
1332
} ROR_INTERSECT_INFO;
1338
1333
 
1339
1334
 
 
1335
/*
 
1336
  Allocate a ROR_INTERSECT_INFO and initialize it to contain zero scans.
 
1337
 
 
1338
  SYNOPSIS
 
1339
    ror_intersect_init()
 
1340
      param         Parameter from test_quick_select
 
1341
 
 
1342
  RETURN
 
1343
    allocated structure
 
1344
    NULL on error
 
1345
*/
 
1346
 
 
1347
static
 
1348
ROR_INTERSECT_INFO* ror_intersect_init(const optimizer::Parameter *param)
 
1349
{
 
1350
  ROR_INTERSECT_INFO *info;
 
1351
  my_bitmap_map* buf;
 
1352
  if (!(info= (ROR_INTERSECT_INFO*)param->mem_root->alloc_root(sizeof(ROR_INTERSECT_INFO))))
 
1353
    return NULL;
 
1354
  info->param= param;
 
1355
  if (!(buf= (my_bitmap_map*) param->mem_root->alloc_root(param->fields_bitmap_size)))
 
1356
    return NULL;
 
1357
  if (info->covered_fields.init(buf, param->table->getShare()->fields))
 
1358
    return NULL;
 
1359
  info->is_covering= false;
 
1360
  info->index_scan_costs= 0.0;
 
1361
  info->index_records= 0;
 
1362
  info->out_rows= (double) param->table->cursor->stats.records;
 
1363
  info->covered_fields.clearAll();
 
1364
  return info;
 
1365
}
 
1366
 
1340
1367
static void ror_intersect_cpy(ROR_INTERSECT_INFO *dst,
1341
1368
                              const ROR_INTERSECT_INFO *src)
1342
1369
{
1441
1468
*/
1442
1469
 
1443
1470
static double ror_scan_selectivity(const ROR_INTERSECT_INFO *info,
1444
 
                                   const optimizer::RorScanInfo *scan)
 
1471
                                   const ROR_SCAN_INFO *scan)
1445
1472
{
1446
1473
  double selectivity_mult= 1.0;
1447
1474
  KeyPartInfo *key_part= info->param->table->key_info[scan->keynr].key_part;
1451
1478
  optimizer::SEL_ARG *tuple_arg= NULL;
1452
1479
  key_part_map keypart_map= 0;
1453
1480
  bool cur_covered;
1454
 
  bool prev_covered= test(info->covered_fields.test(key_part->fieldnr-1));
 
1481
  bool prev_covered= test(info->covered_fields.isBitSet(key_part->fieldnr-1));
1455
1482
  key_range min_range;
1456
1483
  key_range max_range;
1457
1484
  min_range.key= key_val;
1464
1491
       sel_arg= sel_arg->next_key_part)
1465
1492
  {
1466
1493
    cur_covered=
1467
 
      test(info->covered_fields.test(key_part[sel_arg->part].fieldnr-1));
 
1494
      test(info->covered_fields.isBitSet(key_part[sel_arg->part].fieldnr-1));
1468
1495
    if (cur_covered != prev_covered)
1469
1496
    {
1470
1497
      /* create (part1val, ..., part{n-1}val) tuple. */
1549
1576
*/
1550
1577
 
1551
1578
static bool ror_intersect_add(ROR_INTERSECT_INFO *info,
1552
 
                              optimizer::RorScanInfo* ror_scan, bool is_cpk_scan)
 
1579
                              ROR_SCAN_INFO* ror_scan, bool is_cpk_scan)
1553
1580
{
1554
1581
  double selectivity_mult= 1.0;
1555
1582
 
1576
1603
  {
1577
1604
    info->index_records += info->param->table->quick_rows[ror_scan->keynr];
1578
1605
    info->index_scan_costs += ror_scan->index_read_cost;
1579
 
    boost::dynamic_bitset<> tmp_bitset= ror_scan->bitsToBitset();
1580
 
    info->covered_fields|= tmp_bitset;
1581
 
    if (! info->is_covering && info->param->needed_fields.is_subset_of(info->covered_fields))
 
1606
    bitmap_union(&info->covered_fields, &ror_scan->covered_fields);
 
1607
    if (!info->is_covering && bitmap_is_subset(&info->param->needed_fields,
 
1608
                                               &info->covered_fields))
1582
1609
    {
1583
1610
      info->is_covering= true;
1584
1611
    }
1585
1612
  }
1586
1613
 
1587
1614
  info->total_cost= info->index_scan_costs;
1588
 
  if (! info->is_covering)
 
1615
  if (!info->is_covering)
1589
1616
  {
1590
1617
    optimizer::CostVector sweep_cost;
1591
1618
    Join *join= info->param->session->lex->select_lex.join;
1636
1663
                                                            optimizer::SEL_TREE *tree,
1637
1664
                                                            double read_time)
1638
1665
{
1639
 
  optimizer::RorScanInfo **ror_scan_mark;
1640
 
  optimizer::RorScanInfo **ror_scans_end= tree->ror_scans_end;
 
1666
  ROR_SCAN_INFO **ror_scan_mark;
 
1667
  ROR_SCAN_INFO **ror_scans_end= tree->ror_scans_end;
1641
1668
 
1642
 
  for (optimizer::RorScanInfo **scan= tree->ror_scans; scan != ror_scans_end; ++scan)
 
1669
  for (ROR_SCAN_INFO **scan= tree->ror_scans; scan != ror_scans_end; ++scan)
1643
1670
    (*scan)->key_components=
1644
1671
      param->table->key_info[(*scan)->keynr].key_parts;
1645
1672
 
1651
1678
  /*I=set of all covering indexes */
1652
1679
  ror_scan_mark= tree->ror_scans;
1653
1680
 
1654
 
  boost::dynamic_bitset<> *covered_fields= &param->tmp_covered_fields;
1655
 
  if (covered_fields->empty())
 
1681
  MyBitmap *covered_fields= &param->tmp_covered_fields;
 
1682
  if (! covered_fields->getBitmap())
1656
1683
  {
1657
 
    covered_fields->resize(param->table->getShare()->sizeFields());
 
1684
    my_bitmap_map *tmp_bitmap= (my_bitmap_map*)param->mem_root->alloc_root(param->fields_bitmap_size);
 
1685
    covered_fields->setBitmap(tmp_bitmap);
1658
1686
  }
1659
 
  covered_fields->reset();
 
1687
  if (! covered_fields->getBitmap() ||
 
1688
      covered_fields->init(covered_fields->getBitmap(),
 
1689
                           param->table->getShare()->fields))
 
1690
    return 0;
 
1691
  covered_fields->clearAll();
1660
1692
 
1661
1693
  double total_cost= 0.0f;
1662
1694
  ha_rows records=0;
1670
1702
        number of first not covered component
1671
1703
      Calculate and save these values for each of remaining scans.
1672
1704
    */
1673
 
    for (optimizer::RorScanInfo **scan= ror_scan_mark; scan != ror_scans_end; ++scan)
 
1705
    for (ROR_SCAN_INFO **scan= ror_scan_mark; scan != ror_scans_end; ++scan)
1674
1706
    {
1675
 
      /* subtract one bitset from the other */
1676
 
      (*scan)->subtractBitset(*covered_fields);
 
1707
      bitmap_subtract(&(*scan)->covered_fields, covered_fields);
1677
1708
      (*scan)->used_fields_covered=
1678
 
        (*scan)->getBitCount();
1679
 
      (*scan)->first_uncovered_field= (*scan)->findFirstNotSet();
 
1709
        (*scan)->covered_fields.getBitsSet();
 
1710
      (*scan)->first_uncovered_field=
 
1711
        (*scan)->covered_fields.getFirst();
1680
1712
    }
1681
1713
 
1682
1714
    internal::my_qsort(ror_scan_mark, ror_scans_end-ror_scan_mark,
1683
 
                       sizeof(optimizer::RorScanInfo*),
 
1715
                       sizeof(ROR_SCAN_INFO*),
1684
1716
                       (qsort_cmp)cmp_ror_scan_info_covering);
1685
1717
 
1686
1718
    /* I=I-first(I) */
1689
1721
    if (total_cost > read_time)
1690
1722
      return NULL;
1691
1723
    /* F=F-covered by first(I) */
1692
 
    boost::dynamic_bitset<> tmp_bitset= (*ror_scan_mark)->bitsToBitset();
1693
 
    *covered_fields|= tmp_bitset;
1694
 
    all_covered= param->needed_fields.is_subset_of(*covered_fields);
1695
 
  } while ((++ror_scan_mark < ror_scans_end) && ! all_covered);
 
1724
    bitmap_union(covered_fields, &(*ror_scan_mark)->covered_fields);
 
1725
    all_covered= bitmap_is_subset(&param->needed_fields, covered_fields);
 
1726
  } while ((++ror_scan_mark < ror_scans_end) && !all_covered);
1696
1727
 
1697
1728
  if (!all_covered || (ror_scan_mark - tree->ror_scans) == 1)
1698
1729
    return NULL;
1716
1747
  }
1717
1748
 
1718
1749
  uint32_t best_num= (ror_scan_mark - tree->ror_scans);
1719
 
  if (!(trp->first_scan= (optimizer::RorScanInfo**)param->mem_root->alloc_root(sizeof(optimizer::RorScanInfo*)* best_num)))
 
1750
  if (!(trp->first_scan= (ROR_SCAN_INFO**)param->mem_root->alloc_root(sizeof(ROR_SCAN_INFO*)* best_num)))
1720
1751
    return NULL;
1721
 
  memcpy(trp->first_scan, tree->ror_scans, best_num*sizeof(optimizer::RorScanInfo*));
 
1752
  memcpy(trp->first_scan, tree->ror_scans, best_num*sizeof(ROR_SCAN_INFO*));
1722
1753
  trp->last_scan=  trp->first_scan + best_num;
1723
1754
  trp->is_covering= true;
1724
1755
  trp->read_cost= total_cost;
1807
1838
    return NULL;
1808
1839
 
1809
1840
  /*
1810
 
    Step1: Collect ROR-able SEL_ARGs and create optimizer::RorScanInfo for each of
 
1841
    Step1: Collect ROR-able SEL_ARGs and create ROR_SCAN_INFO for each of
1811
1842
    them. Also find and save clustered PK scan if there is one.
1812
1843
  */
1813
 
  optimizer::RorScanInfo **cur_ror_scan= NULL;
1814
 
  optimizer::RorScanInfo *cpk_scan= NULL;
 
1844
  ROR_SCAN_INFO **cur_ror_scan= NULL;
 
1845
  ROR_SCAN_INFO *cpk_scan= NULL;
1815
1846
  uint32_t cpk_no= 0;
1816
1847
  bool cpk_scan_used= false;
1817
1848
 
1818
 
  if (! (tree->ror_scans= (optimizer::RorScanInfo**)param->mem_root->alloc_root(sizeof(optimizer::RorScanInfo*)* param->keys)))
 
1849
  if (! (tree->ror_scans= (ROR_SCAN_INFO**)param->mem_root->alloc_root(sizeof(ROR_SCAN_INFO*)* param->keys)))
1819
1850
  {
1820
1851
    return NULL;
1821
1852
  }
1822
1853
  cpk_no= ((param->table->cursor->primary_key_is_clustered()) ?
1823
 
           param->table->getShare()->getPrimaryKey() : MAX_KEY);
 
1854
           param->table->getShare()->primary_key : MAX_KEY);
1824
1855
 
1825
1856
  for (idx= 0, cur_ror_scan= tree->ror_scans; idx < param->keys; idx++)
1826
1857
  {
1827
 
    optimizer::RorScanInfo *scan;
 
1858
    ROR_SCAN_INFO *scan;
1828
1859
    if (! tree->ror_scans_map.test(idx))
1829
1860
      continue;
1830
 
    if (! (scan= make_ror_scan(param, idx, tree->keys[idx])))
 
1861
    if (!(scan= make_ror_scan(param, idx, tree->keys[idx])))
1831
1862
      return NULL;
1832
1863
    if (param->real_keynr[idx] == cpk_no)
1833
1864
    {
1841
1872
  tree->ror_scans_end= cur_ror_scan;
1842
1873
  /*
1843
1874
    Ok, [ror_scans, ror_scans_end) is array of ptrs to initialized
1844
 
    optimizer::RorScanInfo's.
 
1875
    ROR_SCAN_INFO's.
1845
1876
    Step 2: Get best ROR-intersection using an approximate algorithm.
1846
1877
  */
1847
 
  internal::my_qsort(tree->ror_scans, tree->n_ror_scans, sizeof(optimizer::RorScanInfo*),
 
1878
  internal::my_qsort(tree->ror_scans, tree->n_ror_scans, sizeof(ROR_SCAN_INFO*),
1848
1879
                     (qsort_cmp)cmp_ror_scan_info);
1849
1880
 
1850
 
  optimizer::RorScanInfo **intersect_scans= NULL; /* ROR scans used in index intersection */
1851
 
  optimizer::RorScanInfo **intersect_scans_end= NULL;
1852
 
  if (! (intersect_scans= (optimizer::RorScanInfo**)param->mem_root->alloc_root(sizeof(optimizer::RorScanInfo*) * tree->n_ror_scans)))
 
1881
  ROR_SCAN_INFO **intersect_scans= NULL; /* ROR scans used in index intersection */
 
1882
  ROR_SCAN_INFO **intersect_scans_end= NULL;
 
1883
  if (! (intersect_scans= (ROR_SCAN_INFO**)param->mem_root->alloc_root(sizeof(ROR_SCAN_INFO*) * tree->n_ror_scans)))
1853
1884
    return NULL;
1854
1885
  intersect_scans_end= intersect_scans;
1855
1886
 
1856
1887
  /* Create and incrementally update ROR intersection. */
1857
 
  ROR_INTERSECT_INFO intersect(param);
1858
 
  ROR_INTERSECT_INFO intersect_best(param);
 
1888
  ROR_INTERSECT_INFO *intersect= NULL;
 
1889
  ROR_INTERSECT_INFO *intersect_best= NULL;
 
1890
  if (! (intersect= ror_intersect_init(param)) ||
 
1891
      ! (intersect_best= ror_intersect_init(param)))
 
1892
    return NULL;
1859
1893
 
1860
1894
  /* [intersect_scans,intersect_scans_best) will hold the best intersection */
1861
 
  optimizer::RorScanInfo **intersect_scans_best= NULL;
 
1895
  ROR_SCAN_INFO **intersect_scans_best= NULL;
1862
1896
  cur_ror_scan= tree->ror_scans;
1863
1897
  intersect_scans_best= intersect_scans;
1864
 
  while (cur_ror_scan != tree->ror_scans_end && ! intersect.is_covering)
 
1898
  while (cur_ror_scan != tree->ror_scans_end && !intersect->is_covering)
1865
1899
  {
1866
1900
    /* S= S + first(R);  R= R - first(R); */
1867
 
    if (! ror_intersect_add(&intersect, *cur_ror_scan, false))
 
1901
    if (!ror_intersect_add(intersect, *cur_ror_scan, false))
1868
1902
    {
1869
1903
      cur_ror_scan++;
1870
1904
      continue;
1872
1906
 
1873
1907
    *(intersect_scans_end++)= *(cur_ror_scan++);
1874
1908
 
1875
 
    if (intersect.total_cost < min_cost)
 
1909
    if (intersect->total_cost < min_cost)
1876
1910
    {
1877
1911
      /* Local minimum found, save it */
1878
 
      ror_intersect_cpy(&intersect_best, &intersect);
 
1912
      ror_intersect_cpy(intersect_best, intersect);
1879
1913
      intersect_scans_best= intersect_scans_end;
1880
 
      min_cost = intersect.total_cost;
 
1914
      min_cost = intersect->total_cost;
1881
1915
    }
1882
1916
  }
1883
1917
 
1886
1920
    return NULL;
1887
1921
  }
1888
1922
 
1889
 
  *are_all_covering= intersect.is_covering;
 
1923
  *are_all_covering= intersect->is_covering;
1890
1924
  uint32_t best_num= intersect_scans_best - intersect_scans;
1891
 
  ror_intersect_cpy(&intersect, &intersect_best);
 
1925
  ror_intersect_cpy(intersect, intersect_best);
1892
1926
 
1893
1927
  /*
1894
1928
    Ok, found the best ROR-intersection of non-CPK key scans.
1895
1929
    Check if we should add a CPK scan. If the obtained ROR-intersection is
1896
1930
    covering, it doesn't make sense to add CPK scan.
1897
1931
  */
1898
 
  if (cpk_scan && ! intersect.is_covering)
 
1932
  if (cpk_scan && !intersect->is_covering)
1899
1933
  {
1900
 
    if (ror_intersect_add(&intersect, cpk_scan, true) &&
1901
 
        (intersect.total_cost < min_cost))
 
1934
    if (ror_intersect_add(intersect, cpk_scan, true) &&
 
1935
        (intersect->total_cost < min_cost))
1902
1936
    {
1903
1937
      cpk_scan_used= true;
1904
1938
      intersect_best= intersect; //just set pointer here
1913
1947
      return trp;
1914
1948
 
1915
1949
    if (! (trp->first_scan=
1916
 
           (optimizer::RorScanInfo**)param->mem_root->alloc_root(sizeof(optimizer::RorScanInfo*)*best_num)))
 
1950
           (ROR_SCAN_INFO**)param->mem_root->alloc_root(sizeof(ROR_SCAN_INFO*)*best_num)))
1917
1951
      return NULL;
1918
 
    memcpy(trp->first_scan, intersect_scans, best_num*sizeof(optimizer::RorScanInfo*));
 
1952
    memcpy(trp->first_scan, intersect_scans, best_num*sizeof(ROR_SCAN_INFO*));
1919
1953
    trp->last_scan=  trp->first_scan + best_num;
1920
 
    trp->is_covering= intersect_best.is_covering;
1921
 
    trp->read_cost= intersect_best.total_cost;
 
1954
    trp->is_covering= intersect_best->is_covering;
 
1955
    trp->read_cost= intersect_best->total_cost;
1922
1956
    /* Prevent divisons by zero */
1923
 
    ha_rows best_rows = double2rows(intersect_best.out_rows);
 
1957
    ha_rows best_rows = double2rows(intersect_best->out_rows);
1924
1958
    if (! best_rows)
1925
1959
      best_rows= 1;
1926
1960
    set_if_smaller(param->table->quick_condition_rows, best_rows);
1927
1961
    trp->records= best_rows;
1928
 
    trp->index_scan_costs= intersect_best.index_scan_costs;
 
1962
    trp->index_scan_costs= intersect_best->index_scan_costs;
1929
1963
    trp->cpk_scan= cpk_scan_used? cpk_scan: NULL;
1930
1964
  }
1931
1965
  return trp;
1937
1971
 
1938
1972
  SYNOPSIS
1939
1973
    get_key_scans_params()
1940
 
      session
1941
1974
      param                    Parameters from test_quick_select
1942
1975
      tree                     Make range select for this optimizer::SEL_TREE
1943
1976
      index_read_must_be_used  true <=> assume 'index only' option will be set
1959
1992
    NULL if no plan found or error occurred
1960
1993
*/
1961
1994
 
1962
 
static optimizer::RangeReadPlan *get_key_scans_params(Session *session,
1963
 
                                                      optimizer::Parameter *param,
 
1995
static optimizer::RangeReadPlan *get_key_scans_params(optimizer::Parameter *param,
1964
1996
                                                      optimizer::SEL_TREE *tree,
1965
1997
                                                      bool index_read_must_be_used,
1966
1998
                                                      bool update_tbl_stats,
1997
2029
      bool read_index_only= index_read_must_be_used ||
1998
2030
                            param->table->covering_keys.test(keynr);
1999
2031
 
2000
 
      found_records= check_quick_select(session, param, idx, read_index_only, *key,
 
2032
      found_records= check_quick_select(param, idx, read_index_only, *key,
2001
2033
                                        update_tbl_stats, &mrr_flags,
2002
2034
                                        &buf_size, &cost);
2003
2035
      found_read_time= cost.total_cost();
2528
2560
  field->setWriteSet();
2529
2561
 
2530
2562
  Item_result cmp_type= field->cmp_type();
2531
 
  if (!((ref_tables | field->getTable()->map) & param_comp))
 
2563
  if (!((ref_tables | field->table->map) & param_comp))
2532
2564
    ftree= get_func_mm_tree(param, cond_func, field, value, cmp_type, inv);
2533
2565
  Item_equal *item_equal= field_item->item_equal;
2534
2566
  if (item_equal)
2542
2574
 
2543
2575
      if (field->eq(f))
2544
2576
        continue;
2545
 
      if (!((ref_tables | f->getTable()->map) & param_comp))
 
2577
      if (!((ref_tables | f->table->map) & param_comp))
2546
2578
      {
2547
2579
        tree= get_func_mm_tree(param, cond_func, f, value, cmp_type, inv);
2548
2580
        ftree= !ftree ? tree : tree_and(param, ftree, tree);
2600
2632
    }
2601
2633
    return(tree);
2602
2634
  }
2603
 
  /* Here when simple cond
2604
 
     There are limits on what kinds of const items we can evaluate, grep for
2605
 
     DontEvaluateMaterializedSubqueryTooEarly.
2606
 
  */
2607
 
  if (cond->const_item()  && !cond->is_expensive())
 
2635
  /* Here when simple cond */
 
2636
  if (cond->const_item())
2608
2637
  {
2609
2638
    /*
2610
2639
      During the cond->val_int() evaluation we can come across a subselect
2696
2725
      field->setWriteSet();
2697
2726
 
2698
2727
      Item_result cmp_type= field->cmp_type();
2699
 
      if (!((ref_tables | field->getTable()->map) & param_comp))
 
2728
      if (!((ref_tables | field->table->map) & param_comp))
2700
2729
      {
2701
2730
        tree= get_mm_parts(param, cond, field, Item_func::EQ_FUNC,
2702
2731
                           value,cmp_type);
2735
2764
                   Item_func::Functype type,
2736
2765
                   Item *value, Item_result)
2737
2766
{
2738
 
  if (field->getTable() != param->table)
 
2767
  if (field->table != param->table)
2739
2768
    return 0;
2740
2769
 
2741
2770
  KEY_PART *key_part = param->key_parts;
2805
2834
  param->session->mem_root= param->old_root;
2806
2835
  if (!value)                                   // IS NULL or IS NOT NULL
2807
2836
  {
2808
 
    if (field->getTable()->maybe_null)          // Can't use a key on this
 
2837
    if (field->table->maybe_null)               // Can't use a key on this
2809
2838
      goto end;
2810
2839
    if (!maybe_null)                            // Not null field
2811
2840
    {
3122
3151
    tree= &optimizer::null_element;                        // cmp with NULL is never true
3123
3152
    goto end;
3124
3153
  }
3125
 
 
3126
 
  /*
3127
 
    Any predicate except "<=>"(null-safe equality operator) involving NULL as a
3128
 
    constant is always FALSE
3129
 
    Put IMPOSSIBLE Tree(null_element) here.
3130
 
  */  
3131
 
  if (type != Item_func::EQUAL_FUNC && field->is_real_null())
3132
 
  {
3133
 
    tree= &optimizer::null_element;
3134
 
    goto end;
3135
 
  }
3136
 
 
3137
3154
  str= (unsigned char*) alloc->alloc_root(key_part->store_length+1);
3138
3155
  if (!str)
3139
3156
    goto end;
3734
3751
  /* Ok got a tuple */
3735
3752
  RANGE_SEQ_ENTRY *cur= &seq->stack[seq->i];
3736
3753
 
3737
 
  range->ptr= (char*)(size_t)(key_tree->part);
 
3754
  range->ptr= (char*)(int)(key_tree->part);
3738
3755
  {
3739
3756
    range->range_flag= cur->min_key_flag | cur->max_key_flag;
3740
3757
 
3810
3827
*/
3811
3828
 
3812
3829
static
3813
 
ha_rows check_quick_select(Session *session,
3814
 
                           optimizer::Parameter *param,
 
3830
ha_rows check_quick_select(optimizer::Parameter *param,
3815
3831
                           uint32_t idx,
3816
3832
                           bool index_only,
3817
3833
                           optimizer::SEL_ARG *tree,
3852
3868
  bool pk_is_clustered= cursor->primary_key_is_clustered();
3853
3869
  if (index_only &&
3854
3870
      (param->table->index_flags(keynr) & HA_KEYREAD_ONLY) &&
3855
 
      !(pk_is_clustered && keynr == param->table->getShare()->getPrimaryKey()))
 
3871
      !(pk_is_clustered && keynr == param->table->getShare()->primary_key))
3856
3872
     *mrr_flags |= HA_MRR_INDEX_ONLY;
3857
3873
 
3858
 
  if (session->lex->sql_command != SQLCOM_SELECT)
 
3874
  if (current_session->lex->sql_command != SQLCOM_SELECT)
3859
3875
    *mrr_flags |= HA_MRR_USE_DEFAULT_IMPL;
3860
3876
 
3861
3877
  *bufsize= param->session->variables.read_rnd_buff_size;
3887
3903
  else
3888
3904
  {
3889
3905
    /* Clustered PK scan is always a ROR scan (TODO: same as above) */
3890
 
    if (param->table->getShare()->getPrimaryKey() == keynr && pk_is_clustered)
 
3906
    if (param->table->getShare()->primary_key == keynr && pk_is_clustered)
3891
3907
      param->is_ror_scan= true;
3892
3908
  }
3893
3909
 
3944
3960
  {
3945
3961
    uint16_t fieldnr= param->table->key_info[keynr].
3946
3962
                    key_part[kp - table_key->key_part].fieldnr - 1;
3947
 
    if (param->table->getField(fieldnr)->key_length() != kp->length)
 
3963
    if (param->table->field[fieldnr]->key_length() != kp->length)
3948
3964
      return false;
3949
3965
  }
3950
3966
 
3952
3968
    return true;
3953
3969
 
3954
3970
  key_part= table_key->key_part + nparts;
3955
 
  pk_number= param->table->getShare()->getPrimaryKey();
 
3971
  pk_number= param->table->getShare()->primary_key;
3956
3972
  if (!param->table->cursor->primary_key_is_clustered() || pk_number == MAX_KEY)
3957
3973
    return false;
3958
3974
 
3978
3994
                            uint32_t mrr_buf_size,
3979
3995
                            memory::Root *parent_alloc)
3980
3996
{
3981
 
  optimizer::QuickRangeSelect *quick= new optimizer::QuickRangeSelect(param->session,
3982
 
                                                                      param->table,
3983
 
                                                                      param->real_keynr[idx],
3984
 
                                                                      test(parent_alloc),
3985
 
                                                                      NULL);
 
3997
  optimizer::QuickRangeSelect *quick= NULL;
 
3998
  bool create_err= false;
 
3999
 
 
4000
  quick= new optimizer::QuickRangeSelect(param->session,
 
4001
                                         param->table,
 
4002
                                         param->real_keynr[idx],
 
4003
                                         test(parent_alloc),
 
4004
                                         NULL,
 
4005
                                         &create_err);
3986
4006
 
3987
4007
  if (quick)
3988
4008
  {
3989
 
          if (get_quick_keys(param,
 
4009
    if (create_err ||
 
4010
              get_quick_keys(param,
3990
4011
                       quick,
3991
4012
                       param->key[idx],
3992
4013
                       key_tree,
4207
4228
}
4208
4229
 
4209
4230
 
4210
 
bool optimizer::QuickSelectInterface::is_keys_used(const boost::dynamic_bitset<>& fields)
 
4231
bool optimizer::QuickSelectInterface::is_keys_used(const MyBitmap *fields)
4211
4232
{
4212
4233
  return is_key_used(head, index, fields);
4213
4234
}
4237
4258
                                                                 table_reference_st *ref,
4238
4259
                                                                 ha_rows records)
4239
4260
{
4240
 
  memory::Root *old_root= NULL;
4241
 
  memory::Root *alloc= NULL;
 
4261
  memory::Root *old_root, *alloc;
 
4262
  optimizer::QuickRangeSelect *quick= NULL;
4242
4263
  KeyInfo *key_info = &table->key_info[ref->key];
4243
4264
  KEY_PART *key_part;
4244
4265
  optimizer::QuickRange *range= NULL;
4245
4266
  uint32_t part;
 
4267
  bool create_err= false;
4246
4268
  optimizer::CostVector cost;
4247
4269
 
4248
4270
  old_root= session->mem_root;
4249
4271
  /* The following call may change session->mem_root */
4250
 
  optimizer::QuickRangeSelect *quick= new optimizer::QuickRangeSelect(session, table, ref->key, 0, 0);
 
4272
  quick= new optimizer::QuickRangeSelect(session, table, ref->key, 0, 0, &create_err);
4251
4273
  /* save mem_root set by QuickRangeSelect constructor */
4252
4274
  alloc= session->mem_root;
4253
4275
  /*
4256
4278
  */
4257
4279
  session->mem_root= old_root;
4258
4280
 
4259
 
  if (! quick)
 
4281
  if (!quick || create_err)
4260
4282
    return 0;                   /* no ranges found */
4261
4283
  if (quick->init())
4262
4284
    goto err;
4578
4600
  uint32_t key_infix_len= 0;          /* Length of key_infix. */
4579
4601
  optimizer::GroupMinMaxReadPlan *read_plan= NULL; /* The eventually constructed TRP. */
4580
4602
  uint32_t key_part_nr;
4581
 
  Order *tmp_group= NULL;
 
4603
  order_st *tmp_group= NULL;
4582
4604
  Item *item= NULL;
4583
4605
  Item_field *item_field= NULL;
4584
4606
 
4591
4613
       (! join->select_distinct)) ||
4592
4614
      (join->select_lex->olap == ROLLUP_TYPE)) /* Check (B3) for ROLLUP */
4593
4615
    return NULL;
4594
 
  if (table->getShare()->sizeKeys() == 0)        /* There are no indexes to use. */
 
4616
  if (table->getShare()->keys == 0)        /* There are no indexes to use. */
4595
4617
    return NULL;
4596
4618
 
4597
4619
  /* Analyze the query in more detail. */
4651
4673
    first one. Here we set the variables: group_prefix_len and index_info.
4652
4674
  */
4653
4675
  KeyInfo *cur_index_info= table->key_info;
4654
 
  KeyInfo *cur_index_info_end= cur_index_info + table->getShare()->sizeKeys();
 
4676
  KeyInfo *cur_index_info_end= cur_index_info + table->getShare()->keys;
4655
4677
  KeyPartInfo *cur_part= NULL;
4656
4678
  KeyPartInfo *end_part= NULL; /* Last part for loops. */
4657
4679
  /* Last index part. */
4676
4698
  uint32_t cur_key_infix_len= 0;
4677
4699
  unsigned char cur_key_infix[MAX_KEY_LENGTH];
4678
4700
  uint32_t cur_used_key_parts= 0;
4679
 
  uint32_t pk= param->table->getShare()->getPrimaryKey();
 
4701
  uint32_t pk= param->table->getShare()->primary_key;
4680
4702
 
4681
4703
  for (uint32_t cur_index= 0;
4682
4704
       cur_index_info != cur_index_info_end;
4699
4721
        (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)))
4700
4722
    {
4701
4723
      /* For each table field */
4702
 
      for (uint32_t i= 0; i < table->getShare()->sizeFields(); i++)
 
4724
      for (uint32_t i= 0; i < table->getShare()->fields; i++)
4703
4725
      {
4704
 
        Field *cur_field= table->getField(i);
 
4726
        Field *cur_field= table->field[i];
4705
4727
        /*
4706
4728
          If the field is used in the current query ensure that it's
4707
4729
          part of 'cur_index'
4913
4935
      optimizer::CostVector dummy_cost;
4914
4936
      uint32_t mrr_flags= HA_MRR_USE_DEFAULT_IMPL;
4915
4937
      uint32_t mrr_bufsize= 0;
4916
 
      cur_quick_prefix_records= check_quick_select(session,
4917
 
                                                   param,
 
4938
      cur_quick_prefix_records= check_quick_select(param,
4918
4939
                                                   cur_param_idx,
4919
4940
                                                   false /*don't care*/,
4920
4941
                                                   cur_index_tree,
5567
5588
}
5568
5589
 
5569
5590
 
5570
 
uint32_t optimizer::RorScanInfo::findFirstNotSet() const
5571
 
{
5572
 
  boost::dynamic_bitset<> map= bitsToBitset();
5573
 
  for (boost::dynamic_bitset<>::size_type i= 0; i < map.size(); i++)
5574
 
  {
5575
 
    if (! map.test(i))
5576
 
    {
5577
 
      return i;
5578
 
    }
5579
 
  }
5580
 
  return map.size();
5581
 
}
5582
 
 
5583
 
 
5584
 
size_t optimizer::RorScanInfo::getBitCount() const
5585
 
{
5586
 
  boost::dynamic_bitset<> tmp_bitset= bitsToBitset();
5587
 
  return tmp_bitset.count();
5588
 
}
5589
 
 
5590
 
 
5591
 
void optimizer::RorScanInfo::subtractBitset(const boost::dynamic_bitset<>& in_bitset)
5592
 
{
5593
 
  boost::dynamic_bitset<> tmp_bitset= bitsToBitset();
5594
 
  tmp_bitset-= in_bitset;
5595
 
  covered_fields= tmp_bitset.to_ulong();
5596
 
}
5597
 
 
5598
 
 
5599
 
boost::dynamic_bitset<> optimizer::RorScanInfo::bitsToBitset() const
5600
 
{
5601
 
  string res;
5602
 
  uint64_t conv= covered_fields;
5603
 
  while (conv)
5604
 
  {
5605
 
    res.push_back((conv & 1) + '0');
5606
 
    conv>>= 1;
5607
 
  }
5608
 
  if (! res.empty())
5609
 
  {
5610
 
    std::reverse(res.begin(), res.end());
5611
 
  }
5612
 
  string final(covered_fields_size - res.length(), '0');
5613
 
  final.append(res);
5614
 
  return (boost::dynamic_bitset<>(final));
5615
 
}
5616
 
 
5617
 
 
5618
5591
} /* namespace drizzled */