1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems, Inc.
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_KEY_FIELD_H
21
#define DRIZZLED_OPTIMIZER_KEY_FIELD_H
23
#include <drizzled/sql_select.h>
33
* Class used when finding key fields
46
null_rejecting(false),
50
KeyField(Field *in_field,
55
bool in_null_rejecting,
61
optimize(in_optimize),
63
null_rejecting(in_null_rejecting),
64
cond_guard(in_cond_guard)
72
void setField(Field *in_field)
82
void setValue(Item *in_val)
92
void setLevel(uint32_t in_level)
97
uint32_t getOptimizeFlags()
102
void setOptimizeFlags(uint32_t in_opt)
107
bool isEqualityCondition() const
112
void setEqualityConditionUsed(bool in_val)
117
bool rejectNullValues() const
119
return null_rejecting;
122
void setRejectNullValues(bool in_val)
124
null_rejecting= in_val;
127
bool *getConditionalGuard()
132
void setConditionalGuard(bool *in_cond_guard)
134
cond_guard= in_cond_guard;
140
Item *val; /**< May be empty if diff constant */
142
uint32_t optimize; /**< KEY_OPTIMIZE_* */
145
If true, the condition this class represents will not be satisfied
149
bool *cond_guard; /**< @see KeyUse::cond_guard */
153
void add_key_fields(Join *join,
154
KeyField **key_fields,
157
table_map usable_tables,
158
std::vector<SargableParam> &sargables);
160
void add_key_part(DYNAMIC_ARRAY *keyuse_array, KeyField *key_field);
163
* Add to KeyField array all 'ref' access candidates within nested join.
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.
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
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).
179
* Example for #1 (outer brackets pair denotes nested join this function is
182
* ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
186
* ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
188
* In examples 1-2 for condition cond, we can add 'ref' access candidates to
192
* ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
194
* Here we can add 'ref' access candidates for t1 and t2, but not for t3.
196
void add_key_fields_for_nj(Join *join,
197
TableList *nested_join_table,
200
std::vector<SargableParam> &sargables);
203
* Merge new key definitions to old ones, remove those not used in both.
205
* This is called for OR between different levels.
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:.
212
* SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL
215
* KeyField::null_rejecting is processed as follows: @n
216
* result has null_rejecting=true if it is set for both ORed references.
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
222
* The result of this is that we're missing some 'ref' accesses.
223
* OptimizerTeam: Fix this
225
KeyField *merge_key_fields(KeyField *start,
226
KeyField *new_fields,
231
* Add a possible key to array of possible keys if it's usable as a key
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
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.
247
* *key_fields is incremented if we stored a key in the array
249
void add_key_field(KeyField **key_fields,
256
table_map usable_tables,
257
std::vector<SargableParam> &sargables);
260
* Add possible keys to array of possible keys originated from a simple
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
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.
278
* *key_fields is incremented if we stored a key in the array
280
void add_key_equal_fields(KeyField **key_fields,
283
Item_field *field_item,
287
table_map usable_tables,
288
std::vector<SargableParam> &sargables);
290
} /* end namespace optimizer */
292
} /* end namespace drizzled */
294
#endif /* DRIZZLED_OPTIMIZER_KEY_FIELD_H */