~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/opt_range.cc

  • Committer: Monty Taylor
  • Date: 2009-05-08 19:27:21 UTC
  • mto: This revision was merged to the branch mainline in revision 1009.
  • Revision ID: mordred@inaugust.com-20090508192721-glbsg850k7wqp1rd
Further reversion of P.

Show diffs side-by-side

added added

removed removed

Lines of Context:
114
114
#include "drizzled/temporal.h" /* Needed in get_mm_leaf() for timestamp -> datetime comparisons */
115
115
 
116
116
#include <string>
117
 
#include <bitset>
118
117
 
119
118
using namespace std;
120
119
 
699
698
  bool quick;                           // Don't calulate possible keys
700
699
 
701
700
  uint32_t fields_bitmap_size;
702
 
  bitset<MAX_FIELDS> needed_fields; /* bitmask of fields needed by the query */
703
 
  bitset<MAX_FIELDS> tmp_covered_fields;
 
701
  MY_BITMAP needed_fields;    /* bitmask of fields needed by the query */
 
702
  MY_BITMAP tmp_covered_fields;
704
703
 
705
704
  key_map *needed_reg;        /* ptr to SQL_SELECT::needed_reg */
706
705
 
1091
1090
{}
1092
1091
 
1093
1092
QUICK_RANGE_SELECT::QUICK_RANGE_SELECT(Session *session, Table *table, uint32_t key_nr,
1094
 
                                       bool no_alloc, MEM_ROOT *parent_alloc)
 
1093
                                       bool no_alloc, MEM_ROOT *parent_alloc,
 
1094
                                       bool *create_error)
1095
1095
  :free_file(0),cur_range(NULL),last_range(0),dont_free(0)
1096
1096
{
 
1097
  my_bitmap_map *bitmap;
 
1098
 
1097
1099
  in_ror_merged_scan= 0;
1098
1100
  sorted= 0;
1099
1101
  index= key_nr;
1118
1120
  save_read_set= head->read_set;
1119
1121
  save_write_set= head->write_set;
1120
1122
 
 
1123
  /* Allocate a bitmap for used columns (Q: why not on MEM_ROOT?) */
 
1124
  if (!(bitmap= (my_bitmap_map*) malloc(head->s->column_bitmap_size)))
 
1125
  {
 
1126
    column_bitmap.bitmap= 0;
 
1127
    *create_error= 1;
 
1128
  }
 
1129
  else
 
1130
    bitmap_init(&column_bitmap, bitmap, head->s->fields, false);
1121
1131
  return;
1122
1132
}
1123
1133
 
1159
1169
    }
1160
1170
    delete_dynamic(&ranges); /* ranges are allocated in alloc */
1161
1171
    free_root(&alloc,MYF(0));
 
1172
    free((char*) column_bitmap.bitmap);
1162
1173
  }
1163
1174
  head->column_bitmaps_set(save_read_set, save_write_set);
1164
1175
  if (mrr_buf_desc)
1344
1355
  }
1345
1356
  head->prepare_for_position();
1346
1357
  head->file= org_file;
1347
 
  column_bitmap= *(head->read_set);
 
1358
  bitmap_copy(&column_bitmap, head->read_set);
1348
1359
  head->column_bitmaps_set(&column_bitmap, &column_bitmap);
1349
1360
 
1350
1361
  return 0;
2085
2096
static int fill_used_fields_bitmap(PARAM *param)
2086
2097
{
2087
2098
  Table *table= param->table;
 
2099
  my_bitmap_map *tmp;
2088
2100
  uint32_t pk;
 
2101
  param->tmp_covered_fields.bitmap= 0;
2089
2102
  param->fields_bitmap_size= table->s->column_bitmap_size;
 
2103
  if (!(tmp= (my_bitmap_map*) alloc_root(param->mem_root,
 
2104
                                  param->fields_bitmap_size)) ||
 
2105
      bitmap_init(&param->needed_fields, tmp, table->s->fields, false))
 
2106
    return 1;
2090
2107
 
2091
 
  param->needed_fields = *(table->read_set);
2092
 
  param->needed_fields |= *(table->write_set);
 
2108
  bitmap_copy(&param->needed_fields, table->read_set);
 
2109
  bitmap_union(&param->needed_fields, table->write_set);
2093
2110
 
2094
2111
  pk= param->table->s->primary_key;
2095
2112
  if (pk != MAX_KEY && param->table->file->primary_key_is_clustered())
2099
2116
    KEY_PART_INFO *key_part_end= key_part +
2100
2117
                                 param->table->key_info[pk].key_parts;
2101
2118
    for (;key_part != key_part_end; ++key_part)
2102
 
      param->needed_fields.reset(key_part->fieldnr-1);
 
2119
      bitmap_clear_bit(&param->needed_fields, key_part->fieldnr-1);
2103
2120
  }
2104
2121
  return 0;
2105
2122
}
2722
2739
  SEL_ARG   *sel_arg;
2723
2740
 
2724
2741
  /* Fields used in the query and covered by this ROR scan. */
2725
 
  bitset<MAX_FIELDS> covered_fields;
 
2742
  MY_BITMAP covered_fields;
2726
2743
  uint32_t      used_fields_covered; /* # of set bits in covered_fields */
2727
2744
  int       key_rec_length; /* length of key record (including rowid) */
2728
2745
 
2773
2790
                                                param->fields_bitmap_size)))
2774
2791
    return NULL;
2775
2792
 
2776
 
  ror_scan->covered_fields.reset();
 
2793
  if (bitmap_init(&ror_scan->covered_fields, bitmap_buf,
 
2794
                  param->table->s->fields, false))
 
2795
    return NULL;
 
2796
  bitmap_clear_all(&ror_scan->covered_fields);
2777
2797
 
2778
2798
  KEY_PART_INFO *key_part= param->table->key_info[keynr].key_part;
2779
2799
  KEY_PART_INFO *key_part_end= key_part +
2780
2800
                               param->table->key_info[keynr].key_parts;
2781
2801
  for (;key_part != key_part_end; ++key_part)
2782
2802
  {
2783
 
    if (param->needed_fields.test(key_part->fieldnr-1))
2784
 
      ror_scan->covered_fields.set(key_part->fieldnr-1);
 
2803
    if (bitmap_is_set(&param->needed_fields, key_part->fieldnr-1))
 
2804
      bitmap_set_bit(&ror_scan->covered_fields, key_part->fieldnr-1);
2785
2805
  }
2786
2806
  double rows= rows2double(param->table->quick_rows[ror_scan->keynr]);
2787
2807
  ror_scan->index_read_cost=
2849
2869
typedef struct
2850
2870
{
2851
2871
  const PARAM *param;
2852
 
  bitset<MAX_FIELDS> covered_fields; /* union of fields covered by all scans */
 
2872
  MY_BITMAP covered_fields; /* union of fields covered by all scans */
2853
2873
  /*
2854
2874
    Fraction of table records that satisfies conditions of all scans.
2855
2875
    This is the number of full records that will be retrieved if a
2881
2901
ROR_INTERSECT_INFO* ror_intersect_init(const PARAM *param)
2882
2902
{
2883
2903
  ROR_INTERSECT_INFO *info;
 
2904
  my_bitmap_map* buf;
2884
2905
  if (!(info= (ROR_INTERSECT_INFO*)alloc_root(param->mem_root,
2885
2906
                                              sizeof(ROR_INTERSECT_INFO))))
2886
2907
    return NULL;
2887
2908
  info->param= param;
 
2909
  if (!(buf= (my_bitmap_map*) alloc_root(param->mem_root,
 
2910
                                         param->fields_bitmap_size)))
 
2911
    return NULL;
 
2912
  if (bitmap_init(&info->covered_fields, buf, param->table->s->fields,
 
2913
                  false))
 
2914
    return NULL;
2888
2915
  info->is_covering= false;
2889
2916
  info->index_scan_costs= 0.0;
2890
2917
  info->index_records= 0;
2891
2918
  info->out_rows= (double) param->table->file->stats.records;
2892
 
  info->covered_fields.reset();
 
2919
  bitmap_clear_all(&info->covered_fields);
2893
2920
  return info;
2894
2921
}
2895
2922
 
2896
2923
void ror_intersect_cpy(ROR_INTERSECT_INFO *dst, const ROR_INTERSECT_INFO *src)
2897
2924
{
2898
2925
  dst->param= src->param;
2899
 
  dst->covered_fields= src->covered_fields;
 
2926
  memcpy(dst->covered_fields.bitmap, src->covered_fields.bitmap,
 
2927
         no_bytes_in_map(&src->covered_fields));
2900
2928
  dst->out_rows= src->out_rows;
2901
2929
  dst->is_covering= src->is_covering;
2902
2930
  dst->index_records= src->index_records;
3005
3033
  SEL_ARG *sel_arg, *tuple_arg= NULL;
3006
3034
  key_part_map keypart_map= 0;
3007
3035
  bool cur_covered;
3008
 
  bool prev_covered= test(info->covered_fields.test(key_part->fieldnr-1));
 
3036
  bool prev_covered= test(bitmap_is_set(&info->covered_fields,
 
3037
                                        key_part->fieldnr-1));
3009
3038
  key_range min_range;
3010
3039
  key_range max_range;
3011
3040
  min_range.key= key_val;
3017
3046
  for (sel_arg= scan->sel_arg; sel_arg;
3018
3047
       sel_arg= sel_arg->next_key_part)
3019
3048
  {
3020
 
    cur_covered= test(info->covered_fields.test(key_part[sel_arg->part].fieldnr-1));
 
3049
    cur_covered= test(bitmap_is_set(&info->covered_fields,
 
3050
                                    key_part[sel_arg->part].fieldnr-1));
3021
3051
    if (cur_covered != prev_covered)
3022
3052
    {
3023
3053
      /* create (part1val, ..., part{n-1}val) tuple. */
3129
3159
  {
3130
3160
    info->index_records += info->param->table->quick_rows[ror_scan->keynr];
3131
3161
    info->index_scan_costs += ror_scan->index_read_cost;
3132
 
    info->covered_fields |= ror_scan->covered_fields;
3133
 
    if (!info->is_covering && isBitmapSubset(&info->param->needed_fields,
3134
 
                                             &info->covered_fields))
 
3162
    bitmap_union(&info->covered_fields, &ror_scan->covered_fields);
 
3163
    if (!info->is_covering && bitmap_is_subset(&info->param->needed_fields,
 
3164
                                               &info->covered_fields))
3135
3165
    {
3136
3166
      info->is_covering= true;
3137
3167
    }
3366
3396
  return(trp);
3367
3397
}
3368
3398
 
 
3399
 
3369
3400
/*
3370
3401
  Get best covering ROR-intersection.
3371
3402
  SYNOPSIS
3419
3450
  /*I=set of all covering indexes */
3420
3451
  ror_scan_mark= tree->ror_scans;
3421
3452
 
3422
 
  bitset<MAX_FIELDS> *covered_fields= &param->tmp_covered_fields;
3423
 
  covered_fields->reset();
 
3453
  MY_BITMAP *covered_fields= &param->tmp_covered_fields;
 
3454
  if (!covered_fields->bitmap)
 
3455
    covered_fields->bitmap= (my_bitmap_map*)alloc_root(param->mem_root,
 
3456
                                               param->fields_bitmap_size);
 
3457
  if (!covered_fields->bitmap ||
 
3458
      bitmap_init(covered_fields, covered_fields->bitmap,
 
3459
                  param->table->s->fields, false))
 
3460
    return 0;
 
3461
  bitmap_clear_all(covered_fields);
3424
3462
 
3425
3463
  double total_cost= 0.0f;
3426
3464
  ha_rows records=0;
3439
3477
    */
3440
3478
    for (ROR_SCAN_INFO **scan= ror_scan_mark; scan != ror_scans_end; ++scan)
3441
3479
    {
3442
 
      /* subtract a bitset */
3443
 
      (*scan)->covered_fields &= covered_fields->flip();
3444
 
      covered_fields->flip();
3445
 
      (*scan)->used_fields_covered= (*scan)->covered_fields.count();
3446
 
      (*scan)->first_uncovered_field= getFirstBitPos((*scan)->covered_fields);
 
3480
      bitmap_subtract(&(*scan)->covered_fields, covered_fields);
 
3481
      (*scan)->used_fields_covered=
 
3482
        bitmap_bits_set(&(*scan)->covered_fields);
 
3483
      (*scan)->first_uncovered_field=
 
3484
        bitmap_get_first(&(*scan)->covered_fields);
3447
3485
    }
3448
3486
 
3449
3487
    my_qsort(ror_scan_mark, ror_scans_end-ror_scan_mark, sizeof(ROR_SCAN_INFO*),
3459
3497
    if (total_cost > read_time)
3460
3498
      return NULL;
3461
3499
    /* F=F-covered by first(I) */
3462
 
    *covered_fields |= (*ror_scan_mark)->covered_fields;
3463
 
    all_covered= isBitmapSubset(&param->needed_fields, covered_fields);
 
3500
    bitmap_union(covered_fields, &(*ror_scan_mark)->covered_fields);
 
3501
    all_covered= bitmap_is_subset(&param->needed_fields, covered_fields);
3464
3502
  } while ((++ror_scan_mark < ror_scans_end) && !all_covered);
3465
3503
 
3466
3504
  if (!all_covered || (ror_scan_mark - tree->ror_scans) == 1)
6444
6482
 
6445
6483
  quick=new QUICK_RANGE_SELECT(param->session, param->table,
6446
6484
                               param->real_keynr[idx],
6447
 
                               test(parent_alloc), NULL);
 
6485
                               test(parent_alloc), NULL, &create_err);
6448
6486
 
6449
6487
  if (quick)
6450
6488
  {
6633
6671
}
6634
6672
 
6635
6673
 
6636
 
bool QUICK_SELECT_I::is_keys_used(const bitset<MAX_FIELDS> *fields)
 
6674
bool QUICK_SELECT_I::is_keys_used(const MY_BITMAP *fields)
6637
6675
{
6638
6676
  return is_key_used(head, index, fields);
6639
6677
}
6640
6678
 
6641
 
bool QUICK_INDEX_MERGE_SELECT::is_keys_used(const bitset<MAX_FIELDS> *fields)
6642
 
{
6643
 
  QUICK_RANGE_SELECT *quick;
6644
 
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
6645
 
  while ((quick= it++))
6646
 
  {
6647
 
    if (is_key_used(head, quick->index, fields))
6648
 
      return 1;
6649
 
  }
6650
 
  return 0;
6651
 
}
6652
 
 
6653
 
bool QUICK_ROR_INTERSECT_SELECT::is_keys_used(const bitset<MAX_FIELDS> *fields)
6654
 
{
6655
 
  QUICK_RANGE_SELECT *quick;
6656
 
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
6657
 
  while ((quick= it++))
6658
 
  {
6659
 
    if (is_key_used(head, quick->index, fields))
6660
 
      return 1;
6661
 
  }
6662
 
  return 0;
6663
 
}
6664
 
 
6665
 
bool QUICK_ROR_UNION_SELECT::is_keys_used(const bitset<MAX_FIELDS> *fields)
 
6679
bool QUICK_INDEX_MERGE_SELECT::is_keys_used(const MY_BITMAP *fields)
 
6680
{
 
6681
  QUICK_RANGE_SELECT *quick;
 
6682
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
 
6683
  while ((quick= it++))
 
6684
  {
 
6685
    if (is_key_used(head, quick->index, fields))
 
6686
      return 1;
 
6687
  }
 
6688
  return 0;
 
6689
}
 
6690
 
 
6691
bool QUICK_ROR_INTERSECT_SELECT::is_keys_used(const MY_BITMAP *fields)
 
6692
{
 
6693
  QUICK_RANGE_SELECT *quick;
 
6694
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
 
6695
  while ((quick= it++))
 
6696
  {
 
6697
    if (is_key_used(head, quick->index, fields))
 
6698
      return 1;
 
6699
  }
 
6700
  return 0;
 
6701
}
 
6702
 
 
6703
bool QUICK_ROR_UNION_SELECT::is_keys_used(const MY_BITMAP *fields)
6666
6704
{
6667
6705
  QUICK_SELECT_I *quick;
6668
6706
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
6708
6746
 
6709
6747
  old_root= session->mem_root;
6710
6748
  /* The following call may change session->mem_root */
6711
 
  quick= new QUICK_RANGE_SELECT(session, table, ref->key, 0, 0);
 
6749
  quick= new QUICK_RANGE_SELECT(session, table, ref->key, 0, 0, &create_err);
6712
6750
  /* save mem_root set by QUICK_RANGE_SELECT constructor */
6713
6751
  alloc= session->mem_root;
6714
6752
  /*
8063
8101
          If the field is used in the current query ensure that it's
8064
8102
          part of 'cur_index'
8065
8103
        */
8066
 
        if (table->read_set->test(cur_field->field_index) &&
 
8104
        if (bitmap_is_set(table->read_set, cur_field->field_index) &&
8067
8105
            !cur_field->part_of_key_not_clustered.is_set(cur_index))
8068
8106
          goto next_index;                  // Field was not part of key
8069
8107
      }
8235
8273
                (min_max_arg_part && (min_max_arg_part < last_part));
8236
8274
      for (; cur_part != last_part; cur_part++)
8237
8275
      {
8238
 
        if (table->read_set->test(cur_part->field->field_index))
 
8276
        if (bitmap_is_set(table->read_set, cur_part->field->field_index))
8239
8277
          goto next_index;
8240
8278
      }
8241
8279
    }