~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/optimizer/key_field.h

Merged vcol stuff.

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