~clint-fewbar/drizzle/regex-policy-cache-limiter

« back to all changes in this revision

Viewing changes to drizzled/optimizer/quick_group_min_max_select.cc

  • Committer: Clint Byrum
  • Date: 2012-03-15 18:05:43 UTC
  • mfrom: (2224.1.302 workspace)
  • Revision ID: clint@ubuntu.com-20120315180543-9jxxm4q10k3np2ws
merging with latest trunk

Show diffs side-by-side

added added

removed removed

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