~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/optimizer/quick_group_min_max_select.h

  • Committer: Padraig O'Sullivan
  • Date: 2009-12-10 19:16:07 UTC
  • mto: (1241.2.6 build)
  • mto: This revision was merged to the branch mainline in revision 1243.
  • Revision ID: osullivan.padraig@gmail.com-20091210191607-xbxnoxs3nmpkc1n0
Split the QUICK_GROUP_MIN_MAX_SELECT class out into its own header and implementation files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems
 
5
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 */
 
19
 
 
20
#ifndef DRIZZLED_OPTIMIZER_QUICK_GROUP_MIN_MAX_SELECT_H
 
21
#define DRIZZLED_OPTIMIZER_QUICK_GROUP_MIN_MAX_SELECT_H
 
22
 
 
23
#include "drizzled/optimizer/range.h"
 
24
 
 
25
namespace drizzled
 
26
{
 
27
 
 
28
namespace optimizer
 
29
{
 
30
 
 
31
/**
 
32
  Index scan for GROUP-BY queries with MIN/MAX aggregate functions.
 
33
 
 
34
  This class provides a specialized index access method for GROUP-BY queries
 
35
  of the forms:
 
36
 
 
37
       SELECT A_1,...,A_k, [B_1,...,B_m], [MIN(C)], [MAX(C)]
 
38
         FROM T
 
39
        WHERE [RNG(A_1,...,A_p ; where p <= k)]
 
40
         [AND EQ(B_1,...,B_m)]
 
41
         [AND PC(C)]
 
42
         [AND PA(A_i1,...,A_iq)]
 
43
       GROUP BY A_1,...,A_k;
 
44
 
 
45
    or
 
46
 
 
47
       SELECT DISTINCT A_i1,...,A_ik
 
48
         FROM T
 
49
        WHERE [RNG(A_1,...,A_p ; where p <= k)]
 
50
         [AND PA(A_i1,...,A_iq)];
 
51
 
 
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.
 
55
 
 
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
 
58
  get_next().
 
59
 
 
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.
 
62
*/
 
63
class QUICK_GROUP_MIN_MAX_SELECT : public QuickSelectInterface
 
64
{
 
65
private:
 
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;
 
89
public:
 
90
  /*
 
91
    The following two members are public to allow easy access from
 
92
    TRP_GROUP_MIN_MAX::make_quick()
 
93
  */
 
94
  MEM_ROOT alloc; /**< Memory pool for this and quick_prefix_select data. */
 
95
  QuickRangeSelect *quick_prefix_select; /**< For retrieval of group prefixes. */
 
96
private:
 
97
  int next_prefix();
 
98
  int next_min_in_range();
 
99
  int next_max_in_range();
 
100
  int next_min();
 
101
  int next_max();
 
102
  void update_min_result();
 
103
  void update_max_result();
 
104
public:
 
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
 
111
                             *parent_alloc);
 
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();
 
117
  int init();
 
118
  int reset();
 
119
  int get_next();
 
120
 
 
121
  bool reverse_sorted() const
 
122
  {
 
123
    return false;
 
124
  }
 
125
 
 
126
  bool unique_key_range() const
 
127
  {
 
128
    return false;
 
129
  }
 
130
 
 
131
  int get_type() const
 
132
  {
 
133
    return QS_TYPE_GROUP_MIN_MAX;
 
134
  }
 
135
 
 
136
  void add_keys_and_lengths(String *key_names, String *used_lengths);
 
137
};
 
138
 
 
139
 
 
140
} /* namespace optimizer */
 
141
 
 
142
} /* namespace drizzled */
 
143
 
 
144
#endif /* DRIZZLED_OPTIMIZER_QUICK_GROUP_MIN_MAX_SELECT_H */