~drizzle-trunk/drizzle/development

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