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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2010 Brian Aker
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <drizzled/parser.h>
namespace drizzled
{
namespace parser
{
/**
Helper to resolve the SQL:2003 Syntax exception 1) in <in predicate>.
See SQL:2003, Part 2, section 8.4 <in predicate>, Note 184, page 383.
This function returns the proper item for the SQL expression
<code>left [NOT] IN ( expr )</code>
@param session the current thread
@param left the in predicand
@param equal true for IN predicates, false for NOT IN predicates
@param expr first and only expression of the in value list
@return an expression representing the IN predicate.
*/
Item* handle_sql2003_note184_exception(Session *session, Item* left, bool equal, Item *expr)
{
/*
Relevant references for this issue:
- SQL:2003, Part 2, section 8.4 <in predicate>, page 383,
- SQL:2003, Part 2, section 7.2 <row value expression>, page 296,
- SQL:2003, Part 2, section 6.3 <value expression primary>, page 174,
- SQL:2003, Part 2, section 7.15 <subquery>, page 370,
- SQL:2003 Feature F561, "Full value expressions".
The exception in SQL:2003 Note 184 means:
Item_singlerow_subselect, which corresponds to a <scalar subquery>,
should be re-interpreted as an Item_in_subselect, which corresponds
to a <table subquery> when used inside an <in predicate>.
Our reading of Note 184 is reccursive, so that all:
- IN (( <subquery> ))
- IN ((( <subquery> )))
- IN '('^N <subquery> ')'^N
- etc
should be interpreted as a <table subquery>, no matter how deep in the
expression the <subquery> is.
*/
Item *result;
if (expr->type() == Item::SUBSELECT_ITEM)
{
Item_subselect *expr2 = (Item_subselect*) expr;
if (expr2->substype() == Item_subselect::SINGLEROW_SUBS)
{
Item_singlerow_subselect *expr3 = (Item_singlerow_subselect*) expr2;
Select_Lex *subselect;
/*
Implement the mandated change, by altering the semantic tree:
left IN Item_singlerow_subselect(subselect)
is modified to
left IN (subselect)
which is represented as
Item_in_subselect(left, subselect)
*/
subselect= expr3->invalidate_and_restore_select_lex();
result= new (session->mem_root) Item_in_subselect(left, subselect);
if (! equal)
result = negate_expression(session, result);
return(result);
}
}
if (equal)
result= new (session->mem_root) Item_func_eq(left, expr);
else
result= new (session->mem_root) Item_func_ne(left, expr);
return(result);
}
/**
@brief Creates a new Select_Lex for a UNION branch.
Sets up and initializes a Select_Lex structure for a query once the parser
discovers a UNION token. The current Select_Lex is pushed on the stack and
the new Select_Lex becomes the current one..=
@lex The parser state.
@is_union_distinct True if the union preceding the new select statement
uses UNION DISTINCT.
@return <code>false</code> if successful, <code>true</code> if an error was
reported. In the latter case parsing should stop.
*/
bool add_select_to_union_list(Session *session, LEX *lex, bool is_union_distinct)
{
if (lex->result)
{
/* Only the last SELECT can have INTO...... */
my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
return true;
}
if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
{
my_parse_error(session->m_lip);
return true;
}
/* This counter shouldn't be incremented for UNION parts */
lex->nest_level--;
if (new_select(lex, 0))
return true;
init_select(lex);
lex->current_select->linkage=UNION_TYPE;
if (is_union_distinct) /* UNION DISTINCT - remember position */
lex->current_select->master_unit()->union_distinct=
lex->current_select;
return false;
}
/**
@brief Initializes a Select_Lex for a query within parentheses (aka
braces).
@return false if successful, true if an error was reported. In the latter
case parsing should stop.
*/
bool setup_select_in_parentheses(Session *session, LEX *lex)
{
Select_Lex * sel= lex->current_select;
if (sel->set_braces(1))
{
my_parse_error(session->m_lip);
return true;
}
if (sel->linkage == UNION_TYPE &&
!sel->master_unit()->first_select()->braces &&
sel->master_unit()->first_select()->linkage ==
UNION_TYPE)
{
my_parse_error(session->m_lip);
return true;
}
if (sel->linkage == UNION_TYPE &&
sel->olap != UNSPECIFIED_OLAP_TYPE &&
sel->master_unit()->fake_select_lex)
{
my_error(ER_WRONG_USAGE, MYF(0), "CUBE/ROLLUP", "ORDER BY");
return true;
}
/* select in braces, can't contain global parameters */
if (sel->master_unit()->fake_select_lex)
sel->master_unit()->global_parameters=
sel->master_unit()->fake_select_lex;
return false;
}
Item* reserved_keyword_function(Session *session, const std::string &name, List<Item> *item_list)
{
const plugin::Function *udf= plugin::Function::get(name.c_str(), name.length());
Item *item= NULL;
if (udf)
{
item= Create_udf_func::s_singleton.create(session, udf, item_list);
} else {
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "FUNCTION", name.c_str());
}
return item;
}
/**
@brief Push an error message into MySQL error stack with line
and position information.
This function provides semantic action implementers with a way
to push the famous "You have a syntax error near..." error
message into the error stack, which is normally produced only if
a parse error is discovered internally by the Bison generated
parser.
*/
void my_parse_error(Lex_input_stream *lip)
{
assert(lip);
const char *yytext= lip->get_tok_start();
/* Push an error into the error stack */
my_printf_error(ER_PARSE_ERROR, ER(ER_PARSE_ERROR), MYF(0), ER(ER_SYNTAX_ERROR),
(yytext ? yytext : ""),
lip->yylineno);
}
void my_parse_error(const char *message)
{
my_printf_error(ER_PARSE_ERROR_UNKNOWN, ER(ER_PARSE_ERROR_UNKNOWN), MYF(0), message);
}
bool check_reserved_words(LEX_STRING *name)
{
if (!my_strcasecmp(system_charset_info, name->str, "GLOBAL") ||
!my_strcasecmp(system_charset_info, name->str, "LOCAL") ||
!my_strcasecmp(system_charset_info, name->str, "SESSION"))
return true;
return false;
}
/**
@brief Bison callback to report a syntax/OOM error
This function is invoked by the bison-generated parser
when a syntax error, a parse error or an out-of-memory
condition occurs. This function is not invoked when the
parser is requested to abort by semantic action code
by means of YYABORT or YYACCEPT macros. This is why these
macros should not be used (use DRIZZLE_YYABORT/DRIZZLE_YYACCEPT
instead).
The parser will abort immediately after invoking this callback.
This function is not for use in semantic actions and is internal to
the parser, as it performs some pre-return cleanup.
In semantic actions, please use parser::my_parse_error or my_error to
push an error into the error stack and DRIZZLE_YYABORT
to abort from the parser.
*/
void errorOn(const char *s)
{
Session *session= current_session;
/* "parse error" changed into "syntax error" between bison 1.75 and 1.875 */
if (strcmp(s,"parse error") == 0 || strcmp(s,"syntax error") == 0)
{
parser::my_parse_error(session->m_lip);
}
else
{
parser::my_parse_error(s);
}
}
bool buildOrderBy(Session *session)
{
Select_Lex *sel= session->getLex()->current_select;
Select_Lex_Unit *unit= sel-> master_unit();
if (sel->linkage != GLOBAL_OPTIONS_TYPE &&
sel->olap != UNSPECIFIED_OLAP_TYPE &&
(sel->linkage != UNION_TYPE || sel->braces))
{
my_error(ER_WRONG_USAGE, MYF(0),
"CUBE/ROLLUP", "ORDER BY");
return false;
}
if (session->getLex()->sql_command != SQLCOM_ALTER_TABLE && !unit->fake_select_lex)
{
/*
A query of the of the form (SELECT ...) ORDER BY order_list is
executed in the same way as the query
SELECT ... ORDER BY order_list
unless the SELECT construct contains ORDER BY or LIMIT clauses.
Otherwise we create a fake Select_Lex if it has not been created
yet.
*/
Select_Lex *first_sl= unit->first_select();
if (!unit->is_union() &&
(first_sl->order_list.elements ||
first_sl->select_limit) &&
unit->add_fake_select_lex(session->getLex()->session))
{
return false;
}
}
return true;
}
void buildEngineOption(Session *session, const char *key, const LEX_STRING &value)
{
message::Engine::Option *opt= session->getLex()->table()->mutable_engine()->add_options();
opt->set_name(key);
opt->set_state(value.str, value.length);
}
void buildEngineOption(Session *session, const char *key, uint64_t value)
{
drizzled::message::Engine::Option *opt= session->getLex()->table()->mutable_engine()->add_options();
opt->set_name(key);
opt->set_state(boost::lexical_cast<std::string>(value));
}
void buildSchemaOption(Session *session, const char *key, const LEX_STRING &value)
{
statement::CreateSchema *statement= (statement::CreateSchema *)session->getLex()->statement;
message::Engine::Option *opt= statement->schema_message.mutable_engine()->add_options();
opt->set_name(key);
opt->set_state(value.str, value.length);
}
void buildSchemaOption(Session *session, const char *key, uint64_t value)
{
statement::CreateSchema *statement= (statement::CreateSchema *)session->getLex()->statement;
message::Engine::Option *opt= statement->schema_message.mutable_engine()->add_options();
opt->set_name(key);
opt->set_state(boost::lexical_cast<std::string>(value));
}
bool checkFieldIdent(Session *session, const LEX_STRING &schema_name, const LEX_STRING &table_name)
{
TableList *table= reinterpret_cast<TableList*>(session->getLex()->current_select->table_list.first);
if (schema_name.length)
{
if (my_strcasecmp(table_alias_charset, schema_name.str, table->getSchemaName()))
{
my_error(ER_WRONG_DB_NAME, MYF(0), schema_name.str);
return false;
}
}
if (my_strcasecmp(table_alias_charset, table_name.str,
table->getTableName()))
{
my_error(ER_WRONG_TABLE_NAME, MYF(0), table_name.str);
return false;
}
return true;
}
Item *buildIdent(Session *session,
const LEX_STRING &schema_name,
const LEX_STRING &table_name,
const LEX_STRING &field_name)
{
Select_Lex *sel= session->getLex()->current_select;
if (sel->no_table_names_allowed)
{
my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
MYF(0), table_name.str, session->where);
}
Item *item= (sel->parsing_place != IN_HAVING or
sel->get_in_sum_expr() > 0) ?
(Item*) new Item_field(session->getLex()->current_context(), schema_name.str, table_name.str, field_name.str) :
(Item*) new Item_ref(session->getLex()->current_context(), schema_name.str, table_name.str, field_name.str);
return item;
}
} // namespace parser
} // namespace drizzled
|