~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/optimizer/key_field.h

MergedĀ inĀ trunk.

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) 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_KEY_FIELD_H
 
21
#define DRIZZLED_OPTIMIZER_KEY_FIELD_H
 
22
 
 
23
#include <drizzled/server_includes.h>
 
24
#include <drizzled/sql_select.h>
 
25
 
 
26
#include <vector>
 
27
 
 
28
namespace drizzled
 
29
{
 
30
namespace optimizer
 
31
{
 
32
 
 
33
/**
 
34
 * Class used when finding key fields
 
35
 */
 
36
class KeyField
 
37
{
 
38
public:
 
39
 
 
40
  KeyField()
 
41
    :
 
42
      field(NULL),
 
43
      val(NULL),
 
44
      level(0),
 
45
      optimize(0),
 
46
      eq_func(false),
 
47
      null_rejecting(false),
 
48
      cond_guard(NULL)
 
49
  {}
 
50
 
 
51
  KeyField(Field *in_field,
 
52
           Item *in_val,
 
53
           uint32_t in_level,
 
54
           uint32_t in_optimize,
 
55
           bool in_eq_func,
 
56
           bool in_null_rejecting,
 
57
           bool *in_cond_guard)
 
58
    :
 
59
      field(in_field),
 
60
      val(in_val),
 
61
      level(in_level),
 
62
      optimize(in_optimize),
 
63
      eq_func(in_eq_func),
 
64
      null_rejecting(in_null_rejecting),
 
65
      cond_guard(in_cond_guard)
 
66
  {}
 
67
 
 
68
  Field *getField()
 
69
  {
 
70
    return field;
 
71
  }
 
72
 
 
73
  void setField(Field *in_field)
 
74
  {
 
75
    field= in_field;
 
76
  }
 
77
 
 
78
  Item *getValue()
 
79
  {
 
80
    return val;
 
81
  }
 
82
 
 
83
  void setValue(Item *in_val)
 
84
  {
 
85
    val= in_val;
 
86
  }
 
87
 
 
88
  uint32_t getLevel()
 
89
  {
 
90
    return level;
 
91
  }
 
92
 
 
93
  void setLevel(uint32_t in_level)
 
94
  {
 
95
    level= in_level;
 
96
  }
 
97
 
 
98
  uint32_t getOptimizeFlags()
 
99
  {
 
100
    return optimize;
 
101
  }
 
102
 
 
103
  void setOptimizeFlags(uint32_t in_opt)
 
104
  {
 
105
    optimize= in_opt;
 
106
  }
 
107
 
 
108
  bool isEqualityCondition() const
 
109
  {
 
110
    return eq_func;
 
111
  }
 
112
 
 
113
  void setEqualityConditionUsed(bool in_val)
 
114
  {
 
115
    eq_func= in_val;
 
116
  }
 
117
 
 
118
  bool rejectNullValues() const
 
119
  {
 
120
    return null_rejecting;
 
121
  }
 
122
 
 
123
  void setRejectNullValues(bool in_val)
 
124
  {
 
125
    null_rejecting= in_val;
 
126
  }
 
127
 
 
128
  bool *getConditionalGuard()
 
129
  {
 
130
    return cond_guard;
 
131
  }
 
132
 
 
133
  void setConditionalGuard(bool *in_cond_guard)
 
134
  {
 
135
    cond_guard= in_cond_guard;
 
136
  }
 
137
 
 
138
private:
 
139
 
 
140
  Field *field;
 
141
  Item *val; /**< May be empty if diff constant */
 
142
  uint32_t level;
 
143
  uint32_t optimize; /**< KEY_OPTIMIZE_* */
 
144
  bool eq_func;
 
145
  /**
 
146
    If true, the condition this class represents will not be satisfied
 
147
    when val IS NULL.
 
148
  */
 
149
  bool null_rejecting;
 
150
  bool *cond_guard; /**< @see KeyUse::cond_guard */
 
151
 
 
152
};
 
153
 
 
154
void add_key_fields(JOIN *join, 
 
155
                    KeyField **key_fields,
 
156
                    uint32_t *and_level,
 
157
                    COND *cond,
 
158
                    table_map usable_tables,
 
159
                    std::vector<SargableParam> &sargables);
 
160
 
 
161
void add_key_part(DYNAMIC_ARRAY *keyuse_array, KeyField *key_field);
 
162
 
 
163
/**
 
164
 * Add to KeyField array all 'ref' access candidates within nested join.
 
165
 *
 
166
 * This function populates KeyField array with entries generated from the
 
167
 * ON condition of the given nested join, and does the same for nested joins
 
168
 * contained within this nested join.
 
169
 *
 
170
 * @param[in]      nested_join_table   Nested join pseudo-table to process
 
171
 * @param[in,out]  end                 End of the key field array
 
172
 * @param[in,out]  and_level           And-level
 
173
 * @param[in,out]  sargables           std::vector of found sargable candidates
 
174
 *
 
175
 *
 
176
 * @note
 
177
 *   We can add accesses to the tables that are direct children of this nested
 
178
 *   join (1), and are not inner tables w.r.t their neighbours (2).
 
179
 *
 
180
 *   Example for #1 (outer brackets pair denotes nested join this function is
 
181
 *   invoked for):
 
182
 *   @code
 
183
 *    ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
 
184
 *   @endcode
 
185
 *   Example for #2:
 
186
 *   @code
 
187
 *    ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
 
188
 *   @endcode
 
189
 *   In examples 1-2 for condition cond, we can add 'ref' access candidates to
 
190
 *   t1 only.
 
191
 *   Example #3:
 
192
 *   @code
 
193
 *    ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
 
194
 *   @endcode
 
195
 *   Here we can add 'ref' access candidates for t1 and t2, but not for t3.
 
196
 */
 
197
void add_key_fields_for_nj(JOIN *join,
 
198
                           TableList *nested_join_table,
 
199
                           KeyField **end,
 
200
                           uint32_t *and_level,
 
201
                           std::vector<SargableParam> &sargables);
 
202
 
 
203
/**
 
204
 * Merge new key definitions to old ones, remove those not used in both.
 
205
 *
 
206
 * This is called for OR between different levels.
 
207
 *
 
208
 * To be able to do 'ref_or_null' we merge a comparison of a column
 
209
 * and 'column IS NULL' to one test.  This is useful for sub select queries
 
210
 * that are internally transformed to something like:.
 
211
 *
 
212
 * @code
 
213
 * SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL
 
214
 * @endcode
 
215
 *
 
216
 * KeyField::null_rejecting is processed as follows: @n
 
217
 * result has null_rejecting=true if it is set for both ORed references.
 
218
 * for example:
 
219
 * -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
 
220
 * -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
 
221
 *
 
222
 * @todo
 
223
 *   The result of this is that we're missing some 'ref' accesses.
 
224
 *   OptimizerTeam: Fix this
 
225
 */
 
226
KeyField *merge_key_fields(KeyField *start,
 
227
                            KeyField *new_fields,
 
228
                            KeyField *end, 
 
229
                            uint32_t and_level);
 
230
 
 
231
/**
 
232
 * Add a possible key to array of possible keys if it's usable as a key
 
233
 *
 
234
 * @param key_fields      Pointer to add key, if usable
 
235
 * @param and_level       And level, to be stored in KeyField
 
236
 * @param cond            Condition predicate
 
237
 * @param field           Field used in comparision
 
238
 * @param eq_func         True if we used =, <=> or IS NULL
 
239
 * @param value           Value used for comparison with field
 
240
 * @param usable_tables   Tables which can be used for key optimization
 
241
 * @param sargables       IN/OUT std::vector of found sargable candidates
 
242
 *
 
243
 * @note
 
244
 *  If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
 
245
 *  table, we store this to be able to do not exists optimization later.
 
246
 *
 
247
 * @returns
 
248
 *  *key_fields is incremented if we stored a key in the array
 
249
 */
 
250
void add_key_field(KeyField **key_fields,
 
251
                   uint32_t and_level,
 
252
                   Item_func *cond,
 
253
                   Field *field,
 
254
                   bool eq_func,
 
255
                   Item **value,
 
256
                   uint32_t num_values,
 
257
                   table_map usable_tables,
 
258
                   std::vector<SargableParam> &sargables);
 
259
 
 
260
/**
 
261
 * Add possible keys to array of possible keys originated from a simple
 
262
 * predicate.
 
263
 *
 
264
 * @param  key_fields     Pointer to add key, if usable
 
265
 * @param  and_level      And level, to be stored in KeyField
 
266
 * @param  cond           Condition predicate
 
267
 * @param  field          Field used in comparision
 
268
 * @param  eq_func        True if we used =, <=> or IS NULL
 
269
 * @param  value          Value used for comparison with field
 
270
 *                        Is NULL for BETWEEN and IN
 
271
 * @param  usable_tables  Tables which can be used for key optimization
 
272
 * @param  sargables      IN/OUT std::vector of found sargable candidates
 
273
 *
 
274
 * @note
 
275
 *  If field items f1 and f2 belong to the same multiple equality and
 
276
 *  a key is added for f1, the the same key is added for f2.
 
277
 *
 
278
 * @returns
 
279
 *  *key_fields is incremented if we stored a key in the array
 
280
*/
 
281
void add_key_equal_fields(KeyField **key_fields,
 
282
                          uint32_t and_level,
 
283
                          Item_func *cond,
 
284
                          Item_field *field_item,
 
285
                          bool eq_func,
 
286
                          Item **val,
 
287
                          uint32_t num_values,
 
288
                          table_map usable_tables,
 
289
                          std::vector<SargableParam> &sargables);
 
290
 
 
291
} /* end namespace optimizer */
 
292
 
 
293
} /* end namespace drizzled */
 
294
 
 
295
#endif /* DRIZZLED_OPTIMIZER_KEY_FIELD_H */