1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008-2009 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#ifndef DRIZZLED_OPTIMIZER_QUICK_GROUP_MIN_MAX_SELECT_H
21
#define DRIZZLED_OPTIMIZER_QUICK_GROUP_MIN_MAX_SELECT_H
23
#include "drizzled/optimizer/range.h"
32
Index scan for GROUP-BY queries with MIN/MAX aggregate functions.
34
This class provides a specialized index access method for GROUP-BY queries
37
SELECT A_1,...,A_k, [B_1,...,B_m], [MIN(C)], [MAX(C)]
39
WHERE [RNG(A_1,...,A_p ; where p <= k)]
42
[AND PA(A_i1,...,A_iq)]
47
SELECT DISTINCT A_i1,...,A_ik
49
WHERE [RNG(A_1,...,A_p ; where p <= k)]
50
[AND PA(A_i1,...,A_iq)];
52
where all selected fields are parts of the same index.
53
The class of queries that can be processed by this quick select is fully
54
specified in the description of get_best_trp_group_min_max() in optimizer/range.cc.
56
The get_next() method directly produces result tuples, thus obviating the
57
need to call end_send_group() because all grouping is already done inside
60
Since one of the requirements is that all select fields are part of the same
61
index, this class produces only index keys, and not complete records.
63
class QUICK_GROUP_MIN_MAX_SELECT : public QuickSelectInterface
66
Cursor *cursor; /**< The Cursor used to get data. */
67
JOIN *join; /**< Descriptor of the current query */
68
KEY *index_info; /**< The index chosen for data access */
69
unsigned char *record; /**< Buffer where the next record is returned. */
70
unsigned char *tmp_record; /**< Temporary storage for next_min(), next_max(). */
71
unsigned char *group_prefix; /**< Key prefix consisting of the GROUP fields. */
72
uint32_t group_prefix_len; /**< Length of the group prefix. */
73
uint32_t group_key_parts; /**< A number of keyparts in the group prefix */
74
unsigned char *last_prefix; /**< Prefix of the last group for detecting EOF. */
75
bool have_min; /**< Specify whether we are computing */
76
bool have_max; /**< a MIN, a MAX, or both. */
77
bool seen_first_key; /**< Denotes whether the first key was retrieved.*/
78
KEY_PART_INFO *min_max_arg_part; /** The keypart of the only argument field of all MIN/MAX functions. */
79
uint32_t min_max_arg_len; /**< The length of the MIN/MAX argument field */
80
unsigned char *key_infix; /**< Infix of constants from equality predicates. */
81
uint32_t key_infix_len;
82
DYNAMIC_ARRAY min_max_ranges; /**< Array of range ptrs for the MIN/MAX field. */
83
uint32_t real_prefix_len; /**< Length of key prefix extended with key_infix. */
84
uint32_t real_key_parts; /**< A number of keyparts in the above value. */
85
List<Item_sum> *min_functions;
86
List<Item_sum> *max_functions;
87
List_iterator<Item_sum> *min_functions_it;
88
List_iterator<Item_sum> *max_functions_it;
91
The following two members are public to allow easy access from
92
TRP_GROUP_MIN_MAX::make_quick()
94
MEM_ROOT alloc; /**< Memory pool for this and quick_prefix_select data. */
95
QuickRangeSelect *quick_prefix_select; /**< For retrieval of group prefixes. */
98
int next_min_in_range();
99
int next_max_in_range();
102
void update_min_result();
103
void update_max_result();
105
QUICK_GROUP_MIN_MAX_SELECT(Table *table, JOIN *join, bool have_min,
106
bool have_max, KEY_PART_INFO *min_max_arg_part,
107
uint32_t group_prefix_len, uint32_t group_key_parts,
108
uint32_t used_key_parts, KEY *index_info, uint
109
use_index, double read_cost, ha_rows records, uint
110
key_infix_len, unsigned char *key_infix, MEM_ROOT
112
~QUICK_GROUP_MIN_MAX_SELECT();
113
bool add_range(SEL_ARG *sel_range);
114
void update_key_stat();
115
void adjust_prefix_ranges();
116
bool alloc_buffers();
121
bool reverse_sorted() const
126
bool unique_key_range() const
133
return QS_TYPE_GROUP_MIN_MAX;
136
void add_keys_and_lengths(String *key_names, String *used_lengths);
140
} /* namespace optimizer */
142
} /* namespace drizzled */
144
#endif /* DRIZZLED_OPTIMIZER_QUICK_GROUP_MIN_MAX_SELECT_H */