~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:07:39 UTC
  • mto: This revision was merged to the branch mainline in revision 1009.
  • Revision ID: mordred@inaugust.com-20090508190739-rwas5y9xjg1a92p6
Reverted a crap-ton of padraig's work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
619
619
  SEL_TREE(enum Type type_arg) :type(type_arg) {}
620
620
  SEL_TREE() :type(KEY)
621
621
  {
622
 
    keys_map.reset();
 
622
    keys_map.clear_all();
623
623
    memset(keys, 0, sizeof(keys));
624
624
  }
625
625
  /*
633
633
 
634
634
  /*
635
635
    Possible ways to read rows using index_merge. The list is non-empty only
636
 
    if type==KEY. Currently can be non empty only if keys_map.none().
 
636
    if type==KEY. Currently can be non empty only if keys_map.is_clear_all().
637
637
  */
638
638
  List<SEL_IMERGE> merges;
639
639
 
1044
1044
 
1045
1045
SQL_SELECT::SQL_SELECT() :quick(0),cond(0),free_cond(0)
1046
1046
{
1047
 
  quick_keys.reset(); 
1048
 
  needed_reg.reset();
 
1047
  quick_keys.clear_all(); needed_reg.clear_all();
1049
1048
  my_b_clear(&file);
1050
1049
}
1051
1050
 
1074
1073
                             ha_rows limit)
1075
1074
{
1076
1075
  key_map tmp;
1077
 
  tmp.set();
 
1076
  tmp.set_all();
1078
1077
  return test_quick_select(session, tmp, 0, limit,
1079
1078
                           force_quick_range, false) < 0;
1080
1079
}
1826
1825
 
1827
1826
  for (idx= 0; idx < table->s->keys; idx++)
1828
1827
  {
1829
 
    if (!(table->keys_in_use_for_query.test(idx)))
 
1828
    if (!(table->keys_in_use_for_query.is_set(idx)))
1830
1829
      continue;
1831
1830
    KEY_PART_INFO *keyinfo= table->key_info[idx].key_part;
1832
1831
    uint32_t n_parts=  table->key_info[idx].key_parts;
2181
2180
  double scan_time;
2182
2181
  delete quick;
2183
2182
  quick=0;
2184
 
  needed_reg.reset();
2185
 
  quick_keys.reset();
2186
 
  if (keys_to_use.none())
 
2183
  needed_reg.clear_all();
 
2184
  quick_keys.clear_all();
 
2185
  if (keys_to_use.is_clear_all())
2187
2186
    return 0;
2188
2187
  records= head->file->stats.records;
2189
2188
  if (!records)
2197
2196
  else if (read_time <= 2.0 && !force_quick_range)
2198
2197
    return 0;                           /* No need for quick select */
2199
2198
 
2200
 
  keys_to_use&= head->keys_in_use_for_query;
2201
 
  if (keys_to_use.any())
 
2199
  keys_to_use.intersect(head->keys_in_use_for_query);
 
2200
  if (!keys_to_use.is_clear_all())
2202
2201
  {
2203
2202
    MEM_ROOT alloc;
2204
2203
    SEL_TREE *tree= NULL;
2212
2211
    /* set up parameter that is passed to all functions */
2213
2212
    param.session= session;
2214
2213
    param.baseflag= head->file->ha_table_flags();
2215
 
    param.prev_tables= prev_tables | const_tables;
2216
 
    param.read_tables= read_tables;
 
2214
    param.prev_tables=prev_tables | const_tables;
 
2215
    param.read_tables=read_tables;
2217
2216
    param.current_table= head->map;
2218
2217
    param.table=head;
2219
2218
    param.keys=0;
2247
2246
    for (idx=0 ; idx < head->s->keys ; idx++, key_info++)
2248
2247
    {
2249
2248
      KEY_PART_INFO *key_part_info;
2250
 
      if (! keys_to_use.test(idx))
 
2249
      if (!keys_to_use.is_set(idx))
2251
2250
        continue;
2252
2251
 
2253
2252
      param.key[param.keys]=key_parts;
2271
2270
    param.alloced_sel_args= 0;
2272
2271
 
2273
2272
    /* Calculate cost of full index read for the shortest covering index */
2274
 
    if (!head->covering_keys.none())
 
2273
    if (!head->covering_keys.is_clear_all())
2275
2274
    {
2276
2275
      int key_for_use= head->find_shortest_key(&head->covering_keys);
2277
2276
      double key_read_time=
2756
2755
ROR_SCAN_INFO *make_ror_scan(const PARAM *param, int idx, SEL_ARG *sel_arg)
2757
2756
{
2758
2757
  ROR_SCAN_INFO *ror_scan;
 
2758
  my_bitmap_map *bitmap_buf;
2759
2759
  uint32_t keynr;
2760
2760
 
2761
2761
  if (!(ror_scan= (ROR_SCAN_INFO*)alloc_root(param->mem_root,
2768
2768
                             param->table->file->ref_length);
2769
2769
  ror_scan->sel_arg= sel_arg;
2770
2770
  ror_scan->records= param->table->quick_rows[keynr];
 
2771
 
 
2772
  if (!(bitmap_buf= (my_bitmap_map*) alloc_root(param->mem_root,
 
2773
                                                param->fields_bitmap_size)))
 
2774
    return NULL;
 
2775
 
2771
2776
  ror_scan->covered_fields.reset();
2772
2777
 
2773
2778
  KEY_PART_INFO *key_part= param->table->key_info[keynr].key_part;
3240
3245
  for (idx= 0, cur_ror_scan= tree->ror_scans; idx < param->keys; idx++)
3241
3246
  {
3242
3247
    ROR_SCAN_INFO *scan;
3243
 
    if (! tree->ror_scans_map.test(idx))
 
3248
    if (!tree->ror_scans_map.is_set(idx))
3244
3249
      continue;
3245
3250
    if (!(scan= make_ror_scan(param, idx, tree->keys[idx])))
3246
3251
      return NULL;
3545
3550
    is defined as "not null".
3546
3551
  */
3547
3552
  print_sel_tree(param, tree, &tree->keys_map, "tree scans");
3548
 
  tree->ror_scans_map.reset();
 
3553
  tree->ror_scans_map.clear_all();
3549
3554
  tree->n_ror_scans= 0;
3550
3555
  for (idx= 0,key=tree->keys, end=key+param->keys; key != end; key++,idx++)
3551
3556
  {
3558
3563
      uint32_t keynr= param->real_keynr[idx];
3559
3564
      if ((*key)->type == SEL_ARG::MAYBE_KEY ||
3560
3565
          (*key)->maybe_flag)
3561
 
        param->needed_reg->set(keynr);
 
3566
        param->needed_reg->set_bit(keynr);
3562
3567
 
3563
3568
      bool read_index_only= index_read_must_be_used ||
3564
 
                            param->table->covering_keys.test(keynr);
 
3569
                            param->table->covering_keys.is_set(keynr);
3565
3570
 
3566
3571
      found_records= check_quick_select(param, idx, read_index_only, *key,
3567
3572
                                        update_tbl_stats, &mrr_flags,
3570
3575
      if ((found_records != HA_POS_ERROR) && param->is_ror_scan)
3571
3576
      {
3572
3577
        tree->n_ror_scans++;
3573
 
        tree->ror_scans_map.set(idx);
 
3578
        tree->ror_scans_map.set_bit(idx);
3574
3579
      }
3575
3580
      if (read_time > found_read_time && found_records != HA_POS_ERROR)
3576
3581
      {
3591
3596
                                                    best_mrr_flags)))
3592
3597
    {
3593
3598
      read_plan->records= best_records;
3594
 
      read_plan->is_ror= tree->ror_scans_map.test(idx);
 
3599
      read_plan->is_ror= tree->ror_scans_map.is_set(idx);
3595
3600
      read_plan->read_cost= read_time;
3596
3601
      read_plan->mrr_buf_size= best_buf_size;
3597
3602
    }
4298
4303
      }
4299
4304
      sel_arg->part=(unsigned char) key_part->part;
4300
4305
      tree->keys[key_part->key]=sel_add(tree->keys[key_part->key],sel_arg);
4301
 
      tree->keys_map.set(key_part->key);
 
4306
      tree->keys_map.set_bit(key_part->key);
4302
4307
    }
4303
4308
  }
4304
4309
 
4787
4792
    return(tree1);
4788
4793
  }
4789
4794
  key_map  result_keys;
4790
 
  result_keys.reset();
 
4795
  result_keys.clear_all();
4791
4796
 
4792
4797
  /* Join the trees key per key */
4793
4798
  SEL_ARG **key1,**key2,**end;
4807
4812
        tree1->type= SEL_TREE::IMPOSSIBLE;
4808
4813
        return(tree1);
4809
4814
      }
4810
 
      result_keys.set(key1 - tree1->keys);
 
4815
      result_keys.set_bit(key1 - tree1->keys);
4811
4816
#ifdef EXTRA_DEBUG
4812
4817
        if (*key1 && param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS)
4813
4818
          (*key1)->test_use_count(*key1);
4816
4821
  }
4817
4822
  tree1->keys_map= result_keys;
4818
4823
  /* dispose index_merge if there is a "range" option */
4819
 
  if (result_keys.any())
 
4824
  if (!result_keys.is_clear_all())
4820
4825
  {
4821
4826
    tree1->merges.empty();
4822
4827
    return(tree1);
4838
4843
                           RANGE_OPT_PARAM* param)
4839
4844
{
4840
4845
  key_map common_keys= tree1->keys_map;
4841
 
  common_keys&= tree2->keys_map;
 
4846
  common_keys.intersect(tree2->keys_map);
4842
4847
 
4843
 
  if (common_keys.none())
 
4848
  if (common_keys.is_clear_all())
4844
4849
    return false;
4845
4850
 
4846
4851
  /* trees have a common key, check if they refer to same key part */
4847
4852
  SEL_ARG **key1,**key2;
4848
4853
  for (uint32_t key_no=0; key_no < param->keys; key_no++)
4849
4854
  {
4850
 
    if (common_keys.test(key_no))
 
4855
    if (common_keys.is_set(key_no))
4851
4856
    {
4852
4857
      key1= tree1->keys + key_no;
4853
4858
      key2= tree2->keys + key_no;
4926
4931
      if (tree->keys[i]->part)
4927
4932
      {
4928
4933
        tree->keys[i]= NULL;
4929
 
        tree->keys_map.reset(i);
 
4934
        tree->keys_map.clear_bit(i);
4930
4935
      }
4931
4936
      else
4932
4937
        res= true;
4952
4957
 
4953
4958
  SEL_TREE *result= 0;
4954
4959
  key_map  result_keys;
4955
 
  result_keys.reset();
 
4960
  result_keys.clear_all();
4956
4961
  if (sel_trees_can_be_ored(tree1, tree2, param))
4957
4962
  {
4958
4963
    /* Join the trees key per key */
4964
4969
      if (*key1)
4965
4970
      {
4966
4971
        result=tree1;                           // Added to tree1
4967
 
        result_keys.set(key1 - tree1->keys);
 
4972
        result_keys.set_bit(key1 - tree1->keys);
4968
4973
#ifdef EXTRA_DEBUG
4969
4974
        if (param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS)
4970
4975
          (*key1)->test_use_count(*key1);
6309
6314
    param->table->quick_rows[keynr]=rows;
6310
6315
    if (update_tbl_stats)
6311
6316
    {
6312
 
      param->table->quick_keys.set(keynr);
 
6317
      param->table->quick_keys.set_bit(keynr);
6313
6318
      param->table->quick_key_parts[keynr]=param->max_key_part+1;
6314
6319
      param->table->quick_n_ranges[keynr]= param->range_count;
6315
6320
      param->table->quick_condition_rows=
8041
8046
       cur_index_info++, cur_index++)
8042
8047
  {
8043
8048
    /* Check (B1) - if current index is covering. */
8044
 
    if (!table->covering_keys.test(cur_index))
 
8049
    if (!table->covering_keys.is_set(cur_index))
8045
8050
      goto next_index;
8046
8051
 
8047
8052
    /*
8065
8070
          part of 'cur_index'
8066
8071
        */
8067
8072
        if (table->read_set->test(cur_field->field_index) &&
8068
 
            !cur_field->part_of_key_not_clustered.test(cur_index))
 
8073
            !cur_field->part_of_key_not_clustered.is_set(cur_index))
8069
8074
          goto next_index;                  // Field was not part of key
8070
8075
      }
8071
8076
    }
8109
8114
    else if (join->select_distinct)
8110
8115
    {
8111
8116
      select_items_it.rewind();
8112
 
      cur_used_key_parts.reset();
 
8117
      cur_used_key_parts.clear_all();
8113
8118
      uint32_t max_key_part= 0;
8114
8119
      while ((item= select_items_it++))
8115
8120
      {
8120
8125
          Check if this attribute was already present in the select list.
8121
8126
          If it was present, then its corresponding key part was alredy used.
8122
8127
        */
8123
 
        if (cur_used_key_parts.test(key_part_nr))
 
8128
        if (cur_used_key_parts.is_set(key_part_nr))
8124
8129
          continue;
8125
8130
        if (key_part_nr < 1 || key_part_nr > join->fields_list.elements)
8126
8131
          goto next_index;
8127
8132
        cur_part= cur_index_info->key_part + key_part_nr - 1;
8128
8133
        cur_group_prefix_len+= cur_part->store_length;
8129
 
        cur_used_key_parts.set(key_part_nr);
 
8134
        cur_used_key_parts.set_bit(key_part_nr);
8130
8135
        ++cur_group_key_parts;
8131
8136
        max_key_part= cmax(max_key_part,key_part_nr);
8132
8137
      }
8136
8141
        all_parts have all bits set from 0 to (max_key_part-1).
8137
8142
        cur_parts have bits set for only used keyparts.
8138
8143
      */
8139
 
      key_map all_parts, cur_parts;
8140
 
      for (uint32_t pos= 0; pos < max_key_part; pos++)
8141
 
        all_parts.set(pos);
8142
 
      cur_parts= cur_used_key_parts >> 1;
 
8144
      uint64_t all_parts, cur_parts;
 
8145
      all_parts= (1<<max_key_part) - 1;
 
8146
      cur_parts= cur_used_key_parts.to_uint64_t() >> 1;
8143
8147
      if (all_parts != cur_parts)
8144
8148
        goto next_index;
8145
8149
    }
9841
9845
       key != end ;
9842
9846
       key++,idx++)
9843
9847
  {
9844
 
    if (tree_map->test(idx))
 
9848
    if (tree_map->is_set(idx))
9845
9849
    {
9846
9850
      uint32_t keynr= param->real_keynr[idx];
9847
9851
      if (tmp.length())