~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/optimizer/key_field.h

  • Committer: Jay Pipes
  • Date: 2009-08-20 18:28:11 UTC
  • mfrom: (1108.6.52 optimizer)
  • mto: This revision was merged to the branch mainline in revision 1120.
  • Revision ID: jpipes@serialcoder-20090820182811-akpzlv3042dyyui7
Merge Padraig's optimizer refactoring around pulling structs into real classes...

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
  Field *field;
 
52
  Item *val; /**< May be empty if diff constant */
 
53
  uint32_t level;
 
54
  uint32_t optimize; /**< KEY_OPTIMIZE_* */
 
55
  bool eq_func;
 
56
  /**
 
57
    If true, the condition this class represents will not be satisfied
 
58
    when val IS NULL.
 
59
  */
 
60
  bool null_rejecting;
 
61
  bool *cond_guard; /**< @see KeyUse::cond_guard */
 
62
};
 
63
 
 
64
void add_key_fields(JOIN *join, 
 
65
                    KeyField **key_fields,
 
66
                    uint32_t *and_level,
 
67
                    COND *cond,
 
68
                    table_map usable_tables,
 
69
                    std::vector<SargableParam> &sargables);
 
70
 
 
71
void add_key_part(DYNAMIC_ARRAY *keyuse_array, KeyField *key_field);
 
72
 
 
73
/**
 
74
 * Add to KeyField array all 'ref' access candidates within nested join.
 
75
 *
 
76
 * This function populates KeyField array with entries generated from the
 
77
 * ON condition of the given nested join, and does the same for nested joins
 
78
 * contained within this nested join.
 
79
 *
 
80
 * @param[in]      nested_join_table   Nested join pseudo-table to process
 
81
 * @param[in,out]  end                 End of the key field array
 
82
 * @param[in,out]  and_level           And-level
 
83
 * @param[in,out]  sargables           std::vector of found sargable candidates
 
84
 *
 
85
 *
 
86
 * @note
 
87
 *   We can add accesses to the tables that are direct children of this nested
 
88
 *   join (1), and are not inner tables w.r.t their neighbours (2).
 
89
 *
 
90
 *   Example for #1 (outer brackets pair denotes nested join this function is
 
91
 *   invoked for):
 
92
 *   @code
 
93
 *    ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
 
94
 *   @endcode
 
95
 *   Example for #2:
 
96
 *   @code
 
97
 *    ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
 
98
 *   @endcode
 
99
 *   In examples 1-2 for condition cond, we can add 'ref' access candidates to
 
100
 *   t1 only.
 
101
 *   Example #3:
 
102
 *   @code
 
103
 *    ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
 
104
 *   @endcode
 
105
 *   Here we can add 'ref' access candidates for t1 and t2, but not for t3.
 
106
 */
 
107
void add_key_fields_for_nj(JOIN *join,
 
108
                           TableList *nested_join_table,
 
109
                           KeyField **end,
 
110
                           uint32_t *and_level,
 
111
                           std::vector<SargableParam> &sargables);
 
112
 
 
113
/**
 
114
 * Merge new key definitions to old ones, remove those not used in both.
 
115
 *
 
116
 * This is called for OR between different levels.
 
117
 *
 
118
 * To be able to do 'ref_or_null' we merge a comparison of a column
 
119
 * and 'column IS NULL' to one test.  This is useful for sub select queries
 
120
 * that are internally transformed to something like:.
 
121
 *
 
122
 * @code
 
123
 * SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL
 
124
 * @endcode
 
125
 *
 
126
 * KeyField::null_rejecting is processed as follows: @n
 
127
 * result has null_rejecting=true if it is set for both ORed references.
 
128
 * for example:
 
129
 * -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
 
130
 * -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
 
131
 *
 
132
 * @todo
 
133
 *   The result of this is that we're missing some 'ref' accesses.
 
134
 *   OptimizerTeam: Fix this
 
135
 */
 
136
KeyField *merge_key_fields(KeyField *start,
 
137
                            KeyField *new_fields,
 
138
                            KeyField *end, 
 
139
                            uint32_t and_level);
 
140
 
 
141
/**
 
142
 * Add a possible key to array of possible keys if it's usable as a key
 
143
 *
 
144
 * @param key_fields      Pointer to add key, if usable
 
145
 * @param and_level       And level, to be stored in KeyField
 
146
 * @param cond            Condition predicate
 
147
 * @param field           Field used in comparision
 
148
 * @param eq_func         True if we used =, <=> or IS NULL
 
149
 * @param value           Value used for comparison with field
 
150
 * @param usable_tables   Tables which can be used for key optimization
 
151
 * @param sargables       IN/OUT std::vector of found sargable candidates
 
152
 *
 
153
 * @note
 
154
 *  If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
 
155
 *  table, we store this to be able to do not exists optimization later.
 
156
 *
 
157
 * @returns
 
158
 *  *key_fields is incremented if we stored a key in the array
 
159
 */
 
160
void add_key_field(KeyField **key_fields,
 
161
                   uint32_t and_level,
 
162
                   Item_func *cond,
 
163
                   Field *field,
 
164
                   bool eq_func,
 
165
                   Item **value,
 
166
                   uint32_t num_values,
 
167
                   table_map usable_tables,
 
168
                   std::vector<SargableParam> &sargables);
 
169
 
 
170
/**
 
171
 * Add possible keys to array of possible keys originated from a simple
 
172
 * predicate.
 
173
 *
 
174
 * @param  key_fields     Pointer to add key, if usable
 
175
 * @param  and_level      And level, to be stored in KeyField
 
176
 * @param  cond           Condition predicate
 
177
 * @param  field          Field used in comparision
 
178
 * @param  eq_func        True if we used =, <=> or IS NULL
 
179
 * @param  value          Value used for comparison with field
 
180
 *                        Is NULL for BETWEEN and IN
 
181
 * @param  usable_tables  Tables which can be used for key optimization
 
182
 * @param  sargables      IN/OUT std::vector of found sargable candidates
 
183
 *
 
184
 * @note
 
185
 *  If field items f1 and f2 belong to the same multiple equality and
 
186
 *  a key is added for f1, the the same key is added for f2.
 
187
 *
 
188
 * @returns
 
189
 *  *key_fields is incremented if we stored a key in the array
 
190
*/
 
191
void add_key_equal_fields(KeyField **key_fields,
 
192
                          uint32_t and_level,
 
193
                          Item_func *cond,
 
194
                          Item_field *field_item,
 
195
                          bool eq_func,
 
196
                          Item **val,
 
197
                          uint32_t num_values,
 
198
                          table_map usable_tables,
 
199
                          std::vector<SargableParam> &sargables);
 
200
 
 
201
} /* end namespace optimizer */
 
202
 
 
203
} /* end namespace drizzled */
 
204
 
 
205
#endif /* DRIZZLED_OPTIMIZER_KEY_FIELD_H */