~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/optimizer/quick_group_min_max_select.cc

merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
using namespace std;
38
38
 
39
39
namespace drizzled {
 
40
namespace optimizer {
40
41
 
41
 
optimizer::QuickGroupMinMaxSelect::
42
 
QuickGroupMinMaxSelect(Table *table,
 
42
QuickGroupMinMaxSelect::QuickGroupMinMaxSelect(Table *table,
43
43
                       Join *join_arg,
44
44
                       bool have_min_arg,
45
45
                       bool have_max_arg,
88
88
  assert(! parent_alloc);
89
89
  if (! parent_alloc)
90
90
  {
91
 
    memory::init_sql_alloc(&alloc, join->session->variables.range_alloc_block_size, 0);
 
91
    alloc.init(join->session->variables.range_alloc_block_size);
92
92
    join->session->mem_root= &alloc;
93
93
  }
94
94
  else
96
96
}
97
97
 
98
98
 
99
 
int optimizer::QuickGroupMinMaxSelect::init()
 
99
int QuickGroupMinMaxSelect::init()
100
100
{
101
101
  if (group_prefix) /* Already initialized. */
102
102
    return 0;
103
103
 
104
 
  if (! (last_prefix= (unsigned char*) alloc.alloc_root(group_prefix_len)))
105
 
      return 1;
 
104
  last_prefix= alloc.alloc(group_prefix_len);
106
105
  /*
107
106
    We may use group_prefix to store keys with all select fields, so allocate
108
107
    enough space for it.
109
108
  */
110
 
  if (! (group_prefix= (unsigned char*) alloc.alloc_root(real_prefix_len + min_max_arg_len)))
111
 
    return 1;
 
109
  group_prefix= alloc.alloc(real_prefix_len + min_max_arg_len);
112
110
 
113
111
  if (key_infix_len > 0)
114
112
  {
116
114
      The memory location pointed to by key_infix will be deleted soon, so
117
115
      allocate a new buffer and copy the key_infix into it.
118
116
    */
119
 
    unsigned char *tmp_key_infix= (unsigned char*) alloc.alloc_root(key_infix_len);
120
 
    if (! tmp_key_infix)
121
 
      return 1;
 
117
    unsigned char *tmp_key_infix= alloc.alloc(key_infix_len);
122
118
    memcpy(tmp_key_infix, this->key_infix, key_infix_len);
123
119
    this->key_infix= tmp_key_infix;
124
120
  }
125
121
 
126
122
  if (min_max_arg_part)
127
123
  {
128
 
    if (have_min)
129
 
    {
130
 
      if (! (min_functions= new List<Item_sum>))
131
 
        return 1;
132
 
    }
133
 
    else
134
 
      min_functions= NULL;
135
 
    if (have_max)
136
 
    {
137
 
      if (! (max_functions= new List<Item_sum>))
138
 
        return 1;
139
 
    }
140
 
    else
141
 
      max_functions= NULL;
142
 
 
143
 
    Item_sum *min_max_item= NULL;
 
124
    min_functions= have_min ? new List<Item_sum> : NULL;
 
125
    max_functions= have_max ? new List<Item_sum> : NULL;
144
126
    Item_sum **func_ptr= join->sum_funcs;
145
 
    while ((min_max_item= *(func_ptr++)))
 
127
    while (Item_sum* min_max_item= *(func_ptr++))
146
128
    {
147
129
      if (have_min && (min_max_item->sum_func() == Item_sum::MIN_FUNC))
148
130
        min_functions->push_back(min_max_item);
151
133
    }
152
134
 
153
135
    if (have_min)
154
 
    {
155
 
      if (! (min_functions_it= new List<Item_sum>::iterator(min_functions->begin())))
156
 
        return 1;
157
 
    }
158
 
 
 
136
      min_functions_it= new List<Item_sum>::iterator(min_functions->begin());
159
137
    if (have_max)
160
 
    {
161
 
      if (! (max_functions_it= new List<Item_sum>::iterator(max_functions->begin())))
162
 
        return 1;
163
 
    }
 
138
      max_functions_it= new List<Item_sum>::iterator(max_functions->begin());
164
139
  }
165
 
 
166
140
  return 0;
167
141
}
168
142
 
169
 
 
170
 
optimizer::QuickGroupMinMaxSelect::~QuickGroupMinMaxSelect()
 
143
QuickGroupMinMaxSelect::~QuickGroupMinMaxSelect()
171
144
{
172
145
  if (cursor->inited != Cursor::NONE)
173
146
  {
187
160
}
188
161
 
189
162
 
190
 
bool optimizer::QuickGroupMinMaxSelect::add_range(optimizer::SEL_ARG *sel_range)
 
163
bool QuickGroupMinMaxSelect::add_range(SEL_ARG *sel_range)
191
164
{
192
 
  optimizer::QuickRange *range= NULL;
 
165
  QuickRange *range= NULL;
193
166
  uint32_t range_flag= sel_range->min_flag | sel_range->max_flag;
194
167
 
195
168
  /* Skip (-inf,+inf) ranges, e.g. (x < 5 or x > 4). */
206
179
                    min_max_arg_len) == 0)
207
180
      range_flag|= EQ_RANGE;  /* equality condition */
208
181
  }
209
 
  range= new optimizer::QuickRange(sel_range->min_value,
 
182
  range= new QuickRange(sel_range->min_value,
210
183
                                   min_max_arg_len,
211
184
                                   make_keypart_map(sel_range->part),
212
185
                                   sel_range->max_value,
220
193
}
221
194
 
222
195
 
223
 
void optimizer::QuickGroupMinMaxSelect::adjust_prefix_ranges()
 
196
void QuickGroupMinMaxSelect::adjust_prefix_ranges()
224
197
{
225
198
  if (quick_prefix_select &&
226
199
      group_prefix_len < quick_prefix_select->max_used_key_length)
227
200
  {
228
201
    DYNAMIC_ARRAY& arr= quick_prefix_select->ranges;
229
202
    for (size_t inx= 0; inx < arr.size(); inx++)
230
 
      reinterpret_cast<optimizer::QuickRange**>(arr.buffer)[inx]->flag &= ~(NEAR_MIN | NEAR_MAX);
 
203
      reinterpret_cast<QuickRange**>(arr.buffer)[inx]->flag &= ~(NEAR_MIN | NEAR_MAX);
231
204
  }
232
205
}
233
206
 
234
207
 
235
 
void optimizer::QuickGroupMinMaxSelect::update_key_stat()
 
208
void QuickGroupMinMaxSelect::update_key_stat()
236
209
{
237
210
  max_used_key_length= real_prefix_len;
238
211
  if (! min_max_ranges.empty())
239
212
  {
240
 
    optimizer::QuickRange *cur_range= NULL;
 
213
    QuickRange *cur_range= NULL;
241
214
    if (have_min)
242
215
    { /* Check if the right-most range has a lower boundary. */
243
216
      cur_range= min_max_ranges.back();
276
249
}
277
250
 
278
251
 
279
 
int optimizer::QuickGroupMinMaxSelect::reset(void)
 
252
int QuickGroupMinMaxSelect::reset(void)
280
253
{
281
254
  int result;
282
255
 
295
268
}
296
269
 
297
270
 
298
 
int optimizer::QuickGroupMinMaxSelect::get_next()
 
271
int QuickGroupMinMaxSelect::get_next()
299
272
{
300
273
  int min_res= 0;
301
274
  int max_res= 0;
375
348
}
376
349
 
377
350
 
378
 
int optimizer::QuickGroupMinMaxSelect::next_min()
 
351
int QuickGroupMinMaxSelect::next_min()
379
352
{
380
353
  int result= 0;
381
354
 
440
413
}
441
414
 
442
415
 
443
 
int optimizer::QuickGroupMinMaxSelect::next_max()
 
416
int QuickGroupMinMaxSelect::next_max()
444
417
{
445
 
  int result= 0;
446
 
 
447
418
  /* Get the last key in the (possibly extended) group. */
448
 
  if (! min_max_ranges.empty())
449
 
    result= next_max_in_range();
450
 
  else
451
 
    result= cursor->index_read_map(record,
452
 
                                   group_prefix,
453
 
                                   make_prev_keypart_map(real_key_parts),
454
 
                                   HA_READ_PREFIX_LAST);
455
 
  return result;
 
419
  return min_max_ranges.empty()
 
420
    ? cursor->index_read_map(record, group_prefix, make_prev_keypart_map(real_key_parts), HA_READ_PREFIX_LAST)
 
421
    : next_max_in_range();
456
422
}
457
423
 
458
424
 
459
 
int optimizer::QuickGroupMinMaxSelect::next_prefix()
 
425
int QuickGroupMinMaxSelect::next_prefix()
460
426
{
461
427
  int result= 0;
462
428
 
502
468
}
503
469
 
504
470
 
505
 
int optimizer::QuickGroupMinMaxSelect::next_min_in_range()
 
471
int QuickGroupMinMaxSelect::next_min_in_range()
506
472
{
507
473
  ha_rkey_function find_flag;
508
474
  key_part_map keypart_map;
509
 
  optimizer::QuickRange *cur_range= NULL;
 
475
  QuickRange *cur_range= NULL;
510
476
  bool found_null= false;
511
477
  int result= HA_ERR_KEY_NOT_FOUND;
512
478
  basic_string<unsigned char> max_key;
515
481
 
516
482
  assert(! min_max_ranges.empty());
517
483
 
518
 
  for (vector<optimizer::QuickRange *>::iterator it= min_max_ranges.begin();
519
 
       it != min_max_ranges.end();
520
 
       ++it)
 
484
  for (vector<QuickRange *>::iterator it= min_max_ranges.begin(); it != min_max_ranges.end(); ++it)
521
485
  { /* Search from the left-most range to the right. */
522
486
    cur_range= *it;
523
487
 
622
586
}
623
587
 
624
588
 
625
 
int optimizer::QuickGroupMinMaxSelect::next_max_in_range()
 
589
int QuickGroupMinMaxSelect::next_max_in_range()
626
590
{
627
591
  ha_rkey_function find_flag;
628
592
  key_part_map keypart_map;
629
 
  optimizer::QuickRange *cur_range= NULL;
 
593
  QuickRange *cur_range= NULL;
630
594
  int result= 0;
631
595
  basic_string<unsigned char> min_key;
632
596
  min_key.reserve(real_prefix_len + min_max_arg_len);
633
597
 
634
598
  assert(! min_max_ranges.empty());
635
599
 
636
 
  for (vector<optimizer::QuickRange *>::reverse_iterator rit= min_max_ranges.rbegin();
 
600
  for (vector<QuickRange *>::reverse_iterator rit= min_max_ranges.rbegin();
637
601
       rit != min_max_ranges.rend();
638
602
       ++rit)
639
603
  { /* Search from the right-most range to the left. */
712
676
}
713
677
 
714
678
 
715
 
void optimizer::QuickGroupMinMaxSelect::update_min_result()
 
679
void QuickGroupMinMaxSelect::update_min_result()
716
680
{
717
681
  *min_functions_it= min_functions->begin();
718
682
  for (Item_sum *min_func; (min_func= (*min_functions_it)++); )
720
684
}
721
685
 
722
686
 
723
 
void optimizer::QuickGroupMinMaxSelect::update_max_result()
 
687
void QuickGroupMinMaxSelect::update_max_result()
724
688
{
725
689
  *max_functions_it= max_functions->begin();
726
690
  for (Item_sum *max_func; (max_func= (*max_functions_it)++); )
728
692
}
729
693
 
730
694
 
731
 
void optimizer::QuickGroupMinMaxSelect::add_keys_and_lengths(string *key_names,
 
695
void QuickGroupMinMaxSelect::add_keys_and_lengths(string *key_names,
732
696
                                                             string *used_lengths)
733
697
{
734
698
  char buf[64];
737
701
  used_lengths->append(buf, length);
738
702
}
739
703
 
 
704
}
740
705
} /* namespace drizzled */