1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
/* Copyright (C) 2009 Sun Microsystems
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include "config.h"
#include <string>
#include "drizzled/error.h"
#include "drizzled/table_list.h"
#include "drizzled/item.h"
#include "drizzled/item/field.h"
#include "drizzled/nested_join.h"
#include "drizzled/sql_lex.h"
#include "drizzled/sql_select.h"
#include "drizzled/strfunc.h"
using namespace std;
namespace drizzled
{
class Item;
class Item_field;
uint32_t TableList::create_table_def_key(char *key)
{
return TableShare::createKey(key, db, table_name);
}
bool TableList::set_insert_values(memory::Root *mem_root)
{
if (table)
{
if (!table->insert_values &&
!(table->insert_values= (unsigned char *)alloc_root(mem_root,
table->s->rec_buff_length)))
return true;
}
return false;
}
bool TableList::is_leaf_for_name_resolution()
{
return (is_natural_join || is_join_columns_complete || !nested_join);
}
TableList *TableList::find_underlying_table(Table *table_to_find)
{
/* is this real table and table which we are looking for? */
if (table == table_to_find)
return this;
return NULL;
}
bool TableList::placeholder()
{
return derived || (create && !table->getDBStat()) || !table;
}
/*
* The right-most child of a nested table reference is the first
* element in the list of children because the children are inserted
* in reverse order.
*/
TableList *TableList::last_leaf_for_name_resolution()
{
TableList *cur_table_ref= this;
nested_join_st *cur_nested_join;
if (is_leaf_for_name_resolution())
return this;
assert(nested_join);
for (cur_nested_join= nested_join;
cur_nested_join;
cur_nested_join= cur_table_ref->nested_join)
{
cur_table_ref= cur_nested_join->join_list.head();
/*
If the current nested is a RIGHT JOIN, the operands in
'join_list' are in reverse order, thus the last operand is in the
end of the list.
*/
if ((cur_table_ref->outer_join & JOIN_TYPE_RIGHT))
{
List_iterator_fast<TableList> it(cur_nested_join->join_list);
TableList *next;
cur_table_ref= it++;
while ((next= it++))
cur_table_ref= next;
}
if (cur_table_ref->is_leaf_for_name_resolution())
break;
}
return cur_table_ref;
}
/*
* The left-most child of a nested table reference is the last element
* in the list of children because the children are inserted in
* reverse order.
*/
TableList *TableList::first_leaf_for_name_resolution()
{
TableList *cur_table_ref= NULL;
nested_join_st *cur_nested_join;
if (is_leaf_for_name_resolution())
return this;
assert(nested_join);
for (cur_nested_join= nested_join;
cur_nested_join;
cur_nested_join= cur_table_ref->nested_join)
{
List_iterator_fast<TableList> it(cur_nested_join->join_list);
cur_table_ref= it++;
/*
If the current nested join is a RIGHT JOIN, the operands in
'join_list' are in reverse order, thus the first operand is
already at the front of the list. Otherwise the first operand
is in the end of the list of join operands.
*/
if (!(cur_table_ref->outer_join & JOIN_TYPE_RIGHT))
{
TableList *next;
while ((next= it++))
cur_table_ref= next;
}
if (cur_table_ref->is_leaf_for_name_resolution())
break;
}
return cur_table_ref;
}
Item_subselect *TableList::containing_subselect()
{
return (select_lex ? select_lex->master_unit()->item : 0);
}
bool TableList::process_index_hints(Table *tbl)
{
/* initialize the result variables */
tbl->keys_in_use_for_query= tbl->keys_in_use_for_group_by=
tbl->keys_in_use_for_order_by= tbl->s->keys_in_use;
/* index hint list processing */
if (index_hints)
{
key_map index_join[INDEX_HINT_FORCE + 1];
key_map index_order[INDEX_HINT_FORCE + 1];
key_map index_group[INDEX_HINT_FORCE + 1];
Index_hint *hint;
int type;
bool have_empty_use_join= false, have_empty_use_order= false,
have_empty_use_group= false;
List_iterator <Index_hint> iter(*index_hints);
/* initialize temporary variables used to collect hints of each kind */
for (type= INDEX_HINT_IGNORE; type <= INDEX_HINT_FORCE; type++)
{
index_join[type].reset();
index_order[type].reset();
index_group[type].reset();
}
/* iterate over the hints list */
while ((hint= iter++))
{
uint32_t pos;
/* process empty USE INDEX () */
if (hint->type == INDEX_HINT_USE && !hint->key_name.str)
{
if (hint->clause & INDEX_HINT_MASK_JOIN)
{
index_join[hint->type].reset();
have_empty_use_join= true;
}
if (hint->clause & INDEX_HINT_MASK_ORDER)
{
index_order[hint->type].reset();
have_empty_use_order= true;
}
if (hint->clause & INDEX_HINT_MASK_GROUP)
{
index_group[hint->type].reset();
have_empty_use_group= true;
}
continue;
}
/*
Check if an index with the given name exists and get his offset in
the keys bitmask for the table
*/
if (tbl->s->keynames.type_names == 0 ||
(pos= find_type(&tbl->s->keynames, hint->key_name.str,
hint->key_name.length, 1)) <= 0)
{
my_error(ER_KEY_DOES_NOT_EXITS, MYF(0), hint->key_name.str, alias);
return 1;
}
pos--;
/* add to the appropriate clause mask */
if (hint->clause & INDEX_HINT_MASK_JOIN)
index_join[hint->type].set(pos);
if (hint->clause & INDEX_HINT_MASK_ORDER)
index_order[hint->type].set(pos);
if (hint->clause & INDEX_HINT_MASK_GROUP)
index_group[hint->type].set(pos);
}
/* cannot mix USE INDEX and FORCE INDEX */
if ((index_join[INDEX_HINT_FORCE].any() ||
index_order[INDEX_HINT_FORCE].any() ||
index_group[INDEX_HINT_FORCE].any()) &&
(index_join[INDEX_HINT_USE].any() || have_empty_use_join ||
index_order[INDEX_HINT_USE].any() || have_empty_use_order ||
index_group[INDEX_HINT_USE].any() || have_empty_use_group))
{
my_error(ER_WRONG_USAGE, MYF(0), index_hint_type_name[INDEX_HINT_USE],
index_hint_type_name[INDEX_HINT_FORCE]);
return 1;
}
/* process FORCE INDEX as USE INDEX with a flag */
if (index_join[INDEX_HINT_FORCE].any() ||
index_order[INDEX_HINT_FORCE].any() ||
index_group[INDEX_HINT_FORCE].any())
{
tbl->force_index= true;
index_join[INDEX_HINT_USE]|= index_join[INDEX_HINT_FORCE];
index_order[INDEX_HINT_USE]|= index_order[INDEX_HINT_FORCE];
index_group[INDEX_HINT_USE]|= index_group[INDEX_HINT_FORCE];
}
/* apply USE INDEX */
if (index_join[INDEX_HINT_USE].any() || have_empty_use_join)
tbl->keys_in_use_for_query&= index_join[INDEX_HINT_USE];
if (index_order[INDEX_HINT_USE].any() || have_empty_use_order)
tbl->keys_in_use_for_order_by&= index_order[INDEX_HINT_USE];
if (index_group[INDEX_HINT_USE].any() || have_empty_use_group)
tbl->keys_in_use_for_group_by&= index_group[INDEX_HINT_USE];
/* apply IGNORE INDEX */
key_map_subtract(tbl->keys_in_use_for_query, index_join[INDEX_HINT_IGNORE]);
key_map_subtract(tbl->keys_in_use_for_order_by, index_order[INDEX_HINT_IGNORE]);
key_map_subtract(tbl->keys_in_use_for_group_by, index_group[INDEX_HINT_IGNORE]);
}
/* make sure covering_keys don't include indexes disabled with a hint */
tbl->covering_keys&= tbl->keys_in_use_for_query;
return 0;
}
void TableList::print(Session *session, String *str, enum_query_type query_type)
{
if (nested_join)
{
str->append('(');
print_join(session, str, &nested_join->join_list, query_type);
str->append(')');
}
else
{
const char *cmp_name; // Name to compare with alias
if (derived)
{
// A derived table
str->append('(');
derived->print(str, query_type);
str->append(')');
cmp_name= ""; // Force printing of alias
}
else
{
// A normal table
{
str->append_identifier(db, db_length);
str->append('.');
}
str->append_identifier(table_name, table_name_length);
cmp_name= table_name;
}
if (my_strcasecmp(table_alias_charset, cmp_name, alias))
{
if (alias && alias[0])
{
str->append(' ');
string t_alias(alias);
transform(t_alias.begin(), t_alias.end(),
t_alias.begin(), ::tolower);
str->append_identifier(t_alias.c_str(), t_alias.length());
}
}
if (index_hints)
{
List_iterator<Index_hint> it(*index_hints);
Index_hint *hint;
while ((hint= it++))
{
str->append (STRING_WITH_LEN(" "));
hint->print (session, str);
}
}
}
}
} /* namespace drizzled */
|