/
sql_yacc.yy
17086 lines (15688 loc) · 517 KB
/
sql_yacc.yy
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2000, 2015, Oracle and/or its affiliates.
Copyright (c) 2010, 2016, MariaDB
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
/* sql_yacc.yy */
/**
@defgroup Parser Parser
@{
*/
%{
#define YYLIP (& thd->m_parser_state->m_lip)
#define YYPS (& thd->m_parser_state->m_yacc)
#define YYCSCL (thd->variables.character_set_client)
#define MYSQL_YACC
#define YYINITDEPTH 100
#define YYMAXDEPTH 3200 /* Because of 64K stack */
#define Lex (thd->lex)
#define Select Lex->current_select
#include <my_global.h>
#include "sql_priv.h"
#include "sql_parse.h" /* comp_*_creator */
#include "sql_table.h" /* primary_key_name */
#include "sql_partition.h" /* mem_alloc_error, partition_info, HASH_PARTITION */
#include "sql_acl.h" /* *_ACL */
#include "sql_class.h" /* Key_part_spec, enum_filetype, Diag_condition_item_name */
#include "slave.h"
#include "lex_symbol.h"
#include "item_create.h"
#include "sp_head.h"
#include "sp_rcontext.h"
#include "sp.h"
#include "sql_show.h"
#include "sql_alter.h" // Sql_cmd_alter_table*
#include "sql_truncate.h" // Sql_cmd_truncate_table
#include "sql_admin.h" // Sql_cmd_analyze/Check..._table
#include "sql_partition_admin.h" // Sql_cmd_alter_table_*_part.
#include "sql_handler.h" // Sql_cmd_handler_*
#include "sql_signal.h"
#include "sql_get_diagnostics.h" // Sql_cmd_get_diagnostics
#include "sql_cte.h"
#include "sql_window.h"
#include "item_windowfunc.h"
#include "event_parse_data.h"
#include "create_options.h"
#include <myisam.h>
#include <myisammrg.h>
#include "keycaches.h"
#include "set_var.h"
#include "rpl_mi.h"
#include "lex_token.h"
/* this is to get the bison compilation windows warnings out */
#ifdef _MSC_VER
/* warning C4065: switch statement contains 'default' but no 'case' labels */
#pragma warning (disable : 4065)
#endif
int yylex(void *yylval, void *yythd);
#define yyoverflow(A,B,C,D,E,F) \
{ \
ulong val= *(F); \
if (my_yyoverflow((B), (D), &val)) \
{ \
yyerror(thd, (char*) (A)); \
return 2; \
} \
else \
{ \
*(F)= (YYSIZE_T)val; \
} \
}
#define MYSQL_YYABORT \
do \
{ \
LEX::cleanup_lex_after_parse_error(thd); \
YYABORT; \
} while (0)
#define MYSQL_YYABORT_UNLESS(A) \
if (!(A)) \
{ \
my_parse_error(thd, ER_SYNTAX_ERROR); \
MYSQL_YYABORT; \
}
#define my_yyabort_error(A) \
do { my_error A; MYSQL_YYABORT; } while(0)
#ifndef DBUG_OFF
#define YYDEBUG 1
#else
#define YYDEBUG 0
#endif
/**
@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.
*/
static void my_parse_error_intern(THD *thd, const char *err_text,
const char *yytext)
{
Lex_input_stream *lip= &thd->m_parser_state->m_lip;
if (!yytext)
{
if (!(yytext= lip->get_tok_start()))
yytext= "";
}
/* Push an error into the error stack */
ErrConvString err(yytext, strlen(yytext),
thd->variables.character_set_client);
my_error(ER_PARSE_ERROR, MYF(0), err_text, err.ptr(), lip->yylineno);
}
static void my_parse_error(THD *thd, uint err_number, const char *yytext=0)
{
return my_parse_error_intern(thd, ER_THD(thd, err_number), yytext);
}
void LEX::parse_error()
{
my_parse_error(thd, ER_SYNTAX_ERROR);
}
static Item* escape(THD *thd)
{
thd->lex->escape_used= false;
const char *esc= thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES ? "" : "\\";
return new (thd->mem_root) Item_string_ascii(thd, esc, MY_TEST(esc[0]));
}
/**
@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 MYSQL_YYABORT/MYSQL_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 my_parse_error or my_error to
push an error into the error stack and MYSQL_YYABORT
to abort from the parser.
*/
void MYSQLerror(THD *thd, const char *s)
{
/*
Restore the original LEX if it was replaced when parsing
a stored procedure. We must ensure that a parsing error
does not leave any side effects in the THD.
*/
LEX::cleanup_lex_after_parse_error(thd);
/* "parse error" changed into "syntax error" between bison 1.75 and 1.875 */
if (strcmp(s,"parse error") == 0 || strcmp(s,"syntax error") == 0)
s= ER_THD(thd, ER_SYNTAX_ERROR);
my_parse_error_intern(thd, s, 0);
}
#ifndef DBUG_OFF
void turn_parser_debug_on()
{
/*
MYSQLdebug is in sql/sql_yacc.cc, in bison generated code.
Turning this option on is **VERY** verbose, and should be
used when investigating a syntax error problem only.
The syntax to run with bison traces is as follows :
- Starting a server manually :
mysqld --debug-dbug="d,parser_debug" ...
- Running a test :
mysql-test-run.pl --mysqld="--debug-dbug=d,parser_debug" ...
The result will be in the process stderr (var/log/master.err)
*/
extern int yydebug;
yydebug= 1;
}
#endif
static bool is_native_function(THD *thd, const LEX_STRING *name)
{
if (find_native_function_builder(thd, *name))
return true;
if (is_lex_native_function(name))
return true;
return false;
}
static sp_head *make_sp_head(THD *thd, sp_name *name,
enum stored_procedure_type type)
{
LEX *lex= thd->lex;
sp_head *sp;
/* Order is important here: new - reset - init */
if ((sp= sp_head::create()))
{
sp->reset_thd_mem_root(thd);
sp->init(lex);
sp->m_type= type;
if (name)
sp->init_sp_name(thd, name);
sp->m_chistics= &lex->sp_chistics;
lex->sphead= sp;
}
bzero(&lex->sp_chistics, sizeof(lex->sp_chistics));
return sp;
}
static bool maybe_start_compound_statement(THD *thd)
{
if (!thd->lex->sphead)
{
if (!make_sp_head(thd, NULL, TYPE_ENUM_PROCEDURE))
return 1;
Lex->sp_chistics.suid= SP_IS_NOT_SUID;
Lex->sphead->set_body_start(thd, YYLIP->get_cpp_ptr());
}
return 0;
}
static bool push_sp_label(THD *thd, LEX_STRING label)
{
sp_pcontext *ctx= thd->lex->spcont;
sp_label *lab= ctx->find_label(label);
if (lab)
{
my_error(ER_SP_LABEL_REDEFINE, MYF(0), label.str);
return 1;
}
else
{
lab= thd->lex->spcont->push_label(thd, label,
thd->lex->sphead->instructions());
lab->type= sp_label::ITERATION;
}
return 0;
}
static bool push_sp_empty_label(THD *thd)
{
if (maybe_start_compound_statement(thd))
return 1;
/* Unlabeled controls get an empty label. */
thd->lex->spcont->push_label(thd, empty_lex_str,
thd->lex->sphead->instructions());
return 0;
}
/**
Helper action for a case expression statement (the expr in 'CASE expr').
This helper is used for 'searched' cases only.
@param lex the parser lex context
@param expr the parsed expression
@return 0 on success
*/
int case_stmt_action_expr(LEX *lex, Item* expr)
{
sp_head *sp= lex->sphead;
sp_pcontext *parsing_ctx= lex->spcont;
int case_expr_id= parsing_ctx->register_case_expr();
sp_instr_set_case_expr *i;
if (parsing_ctx->push_case_expr_id(case_expr_id))
return 1;
i= new (lex->thd->mem_root)
sp_instr_set_case_expr(sp->instructions(), parsing_ctx, case_expr_id, expr,
lex);
sp->add_cont_backpatch(i);
return sp->add_instr(i);
}
/**
Helper action for a case when condition.
This helper is used for both 'simple' and 'searched' cases.
@param lex the parser lex context
@param when the parsed expression for the WHEN clause
@param simple true for simple cases, false for searched cases
*/
int case_stmt_action_when(LEX *lex, Item *when, bool simple)
{
sp_head *sp= lex->sphead;
sp_pcontext *ctx= lex->spcont;
uint ip= sp->instructions();
sp_instr_jump_if_not *i;
Item_case_expr *var;
Item *expr;
THD *thd= lex->thd;
if (simple)
{
var= new (thd->mem_root)
Item_case_expr(thd, ctx->get_current_case_expr_id());
#ifndef DBUG_OFF
if (var)
{
var->m_sp= sp;
}
#endif
expr= new (thd->mem_root) Item_func_eq(thd, var, when);
i= new (thd->mem_root) sp_instr_jump_if_not(ip, ctx, expr, lex);
}
else
i= new (thd->mem_root) sp_instr_jump_if_not(ip, ctx, when, lex);
/*
BACKPATCH: Registering forward jump from
"case_stmt_action_when" to "case_stmt_action_then"
(jump_if_not from instruction 2 to 5, 5 to 8 ... in the example)
*/
return !MY_TEST(i) ||
sp->push_backpatch(thd, i, ctx->push_label(thd, empty_lex_str, 0)) ||
sp->add_cont_backpatch(i) ||
sp->add_instr(i);
}
/**
Helper action for a case then statements.
This helper is used for both 'simple' and 'searched' cases.
@param lex the parser lex context
*/
int case_stmt_action_then(LEX *lex)
{
sp_head *sp= lex->sphead;
sp_pcontext *ctx= lex->spcont;
uint ip= sp->instructions();
sp_instr_jump *i= new (lex->thd->mem_root) sp_instr_jump(ip, ctx);
if (!MY_TEST(i) || sp->add_instr(i))
return 1;
/*
BACKPATCH: Resolving forward jump from
"case_stmt_action_when" to "case_stmt_action_then"
(jump_if_not from instruction 2 to 5, 5 to 8 ... in the example)
*/
sp->backpatch(ctx->pop_label());
/*
BACKPATCH: Registering forward jump from
"case_stmt_action_then" to after END CASE
(jump from instruction 4 to 12, 7 to 12 ... in the example)
*/
return sp->push_backpatch(lex->thd, i, ctx->last_label());
}
static bool
find_sys_var_null_base(THD *thd, struct sys_var_with_base *tmp)
{
tmp->var= find_sys_var(thd, tmp->base_name.str, tmp->base_name.length);
if (tmp->var != NULL)
tmp->base_name= null_lex_str;
return thd->is_error();
}
/**
Helper action for a SET statement.
Used to push a system variable into the assignment list.
@param thd the current thread
@param tmp the system variable with base name
@param var_type the scope of the variable
@param val the value being assigned to the variable
@return TRUE if error, FALSE otherwise.
*/
static bool
set_system_variable(THD *thd, struct sys_var_with_base *tmp,
enum enum_var_type var_type, Item *val)
{
set_var *var;
LEX *lex= thd->lex;
/* No AUTOCOMMIT from a stored function or trigger. */
if (lex->spcont && tmp->var == Sys_autocommit_ptr)
lex->sphead->m_flags|= sp_head::HAS_SET_AUTOCOMMIT_STMT;
if (val && val->type() == Item::FIELD_ITEM &&
((Item_field*)val)->table_name)
{
my_error(ER_WRONG_TYPE_FOR_VAR, MYF(0), tmp->var->name.str);
return TRUE;
}
if (! (var= new (thd->mem_root)
set_var(thd, var_type, tmp->var, &tmp->base_name, val)))
return TRUE;
return lex->var_list.push_back(var, thd->mem_root);
}
/**
Helper action for a SET statement.
Used to push a SP local variable into the assignment list.
@param thd the current thread
@param var_type the SP local variable
@param val the value being assigned to the variable
@return TRUE if error, FALSE otherwise.
*/
static bool
set_local_variable(THD *thd, sp_variable *spv, Item *val)
{
Item *it;
LEX *lex= thd->lex;
sp_instr_set *sp_set;
if (val)
it= val;
else if (spv->default_value)
it= spv->default_value;
else
{
it= new (thd->mem_root) Item_null(thd);
if (it == NULL)
return TRUE;
}
sp_set= new (thd->mem_root)
sp_instr_set(lex->sphead->instructions(), lex->spcont,
spv->offset, it, spv->sql_type(),
lex, TRUE);
return (sp_set == NULL || lex->sphead->add_instr(sp_set));
}
/**
Helper action for a SET statement.
Used to SET a field of NEW row.
@param thd the current thread
@param name the field name
@param val the value being assigned to the row
@return TRUE if error, FALSE otherwise.
*/
static bool
set_trigger_new_row(THD *thd, LEX_STRING *name, Item *val)
{
LEX *lex= thd->lex;
Item_trigger_field *trg_fld;
sp_instr_set_trigger_field *sp_fld;
/* QQ: Shouldn't this be field's default value ? */
if (! val)
val= new (thd->mem_root) Item_null(thd);
DBUG_ASSERT(lex->trg_chistics.action_time == TRG_ACTION_BEFORE &&
(lex->trg_chistics.event == TRG_EVENT_INSERT ||
lex->trg_chistics.event == TRG_EVENT_UPDATE));
trg_fld= new (thd->mem_root)
Item_trigger_field(thd, lex->current_context(),
Item_trigger_field::NEW_ROW,
name->str, UPDATE_ACL, FALSE);
if (trg_fld == NULL)
return TRUE;
sp_fld= new (thd->mem_root)
sp_instr_set_trigger_field(lex->sphead->instructions(),
lex->spcont, trg_fld, val,
lex);
if (sp_fld == NULL)
return TRUE;
/*
Let us add this item to list of all Item_trigger_field
objects in trigger.
*/
lex->trg_table_fields.link_in_list(trg_fld, &trg_fld->next_trg_field);
return lex->sphead->add_instr(sp_fld);
}
/**
Create an object to represent a SP variable in the Item-hierarchy.
@param thd The current thread.
@param name The SP variable name.
@param spvar The SP variable (optional).
@param start_in_q Start position of the SP variable name in the query.
@param end_in_q End position of the SP variable name in the query.
@remark If spvar is not specified, the name is used to search for the
variable in the parse-time context. If the variable does not
exist, a error is set and NULL is returned to the caller.
@return An Item_splocal object representing the SP variable, or NULL on error.
*/
static Item_splocal*
create_item_for_sp_var(THD *thd, LEX_STRING name, sp_variable *spvar,
const char *start_in_q, const char *end_in_q)
{
Item_splocal *item;
LEX *lex= thd->lex;
uint pos_in_q, len_in_q;
sp_pcontext *spc = lex->spcont;
/* If necessary, look for the variable. */
if (spc && !spvar)
spvar= spc->find_variable(name, false);
if (!spvar)
{
my_error(ER_SP_UNDECLARED_VAR, MYF(0), name.str);
return NULL;
}
DBUG_ASSERT(spc && spvar);
/* Position and length of the SP variable name in the query. */
pos_in_q= (uint)(start_in_q - lex->sphead->m_tmp_query);
len_in_q= (uint)(end_in_q - start_in_q);
item= new (thd->mem_root)
Item_splocal(thd, name, spvar->offset, spvar->sql_type(),
pos_in_q, len_in_q);
#ifndef DBUG_OFF
if (item)
item->m_sp= lex->sphead;
#endif
return item;
}
/**
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 thd 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(THD *thd, 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;
DBUG_ENTER("handle_sql2003_note184_exception");
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;
st_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 (thd->mem_root) Item_in_subselect(thd, left, subselect);
if (! equal)
result = negate_expression(thd, result);
DBUG_RETURN(result);
}
}
if (equal)
result= new (thd->mem_root) Item_func_eq(thd, left, expr);
else
result= new (thd->mem_root) Item_func_ne(thd, left, expr);
DBUG_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.
@param lex The parser state.
@param is_union_distinct True if the union preceding the new select
statement uses UNION DISTINCT.
@param is_top_level This should be @c TRUE if the newly created SELECT_LEX
is a non-nested statement.
@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(LEX *lex, bool is_union_distinct,
bool is_top_level)
{
/*
Only the last SELECT can have INTO. Since the grammar won't allow INTO in
a nested SELECT, we make this check only when creating a top-level SELECT.
*/
if (is_top_level && lex->result)
{
my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
return TRUE;
}
if (lex->current_select->order_list.first && !lex->current_select->braces)
{
my_error(ER_WRONG_USAGE, MYF(0), "UNION", "ORDER BY");
return TRUE;
}
if (lex->current_select->explicit_limit && !lex->current_select->braces)
{
my_error(ER_WRONG_USAGE, MYF(0), "UNION", "LIMIT");
return TRUE;
}
if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
{
my_parse_error(lex->thd, ER_SYNTAX_ERROR);
return TRUE;
}
/* This counter shouldn't be incremented for UNION parts */
lex->nest_level--;
if (mysql_new_select(lex, 0))
return TRUE;
mysql_init_select(lex);
lex->current_select->linkage=UNION_TYPE;
lex->current_select->with_all_modifier= !is_union_distinct;
if (is_union_distinct) /* UNION DISTINCT - remember position */
lex->current_select->master_unit()->union_distinct=
lex->current_select;
return FALSE;
}
static bool add_create_index_prepare(LEX *lex, Table_ident *table)
{
lex->sql_command= SQLCOM_CREATE_INDEX;
if (!lex->current_select->add_table_to_list(lex->thd, table, NULL,
TL_OPTION_UPDATING,
TL_READ_NO_INSERT,
MDL_SHARED_UPGRADABLE))
return TRUE;
lex->alter_info.reset();
lex->alter_info.flags= Alter_info::ALTER_ADD_INDEX;
lex->option_list= NULL;
return FALSE;
}
/**
Create a separate LEX for each assignment if in SP.
If we are in SP we want have own LEX for each assignment.
This is mostly because it is hard for several sp_instr_set
and sp_instr_set_trigger instructions share one LEX.
(Well, it is theoretically possible but adds some extra
overhead on preparation for execution stage and IMO less
robust).
QQ: May be we should simply prohibit group assignments in SP?
@see sp_create_assignment_instr
@param thd Thread context
@param no_lookahead True if the parser has no lookahead
*/
static void sp_create_assignment_lex(THD *thd, bool no_lookahead)
{
LEX *lex= thd->lex;
if (lex->sphead)
{
Lex_input_stream *lip= &thd->m_parser_state->m_lip;
LEX *old_lex= lex;
lex->sphead->reset_lex(thd);
lex= thd->lex;
/* Set new LEX as if we at start of set rule. */
lex->sql_command= SQLCOM_SET_OPTION;
mysql_init_select(lex);
lex->var_list.empty();
lex->autocommit= 0;
/* get_ptr() is only correct with no lookahead. */
if (no_lookahead)
lex->sphead->m_tmp_query= lip->get_ptr();
else
lex->sphead->m_tmp_query= lip->get_tok_end();
/* Inherit from outer lex. */
lex->option_type= old_lex->option_type;
}
}
/**
Create a SP instruction for a SET assignment.
@see sp_create_assignment_lex
@param thd Thread context
@param no_lookahead True if the parser has no lookahead
@return false if success, true otherwise.
*/
static bool sp_create_assignment_instr(THD *thd, bool no_lookahead)
{
LEX *lex= thd->lex;
if (lex->sphead)
{
sp_head *sp= lex->sphead;
if (!lex->var_list.is_empty())
{
/*
We have assignment to user or system variable or
option setting, so we should construct sp_instr_stmt
for it.
*/
LEX_STRING qbuff;
sp_instr_stmt *i;
Lex_input_stream *lip= &thd->m_parser_state->m_lip;
if (!(i= new (thd->mem_root)
sp_instr_stmt(sp->instructions(), lex->spcont, lex)))
return true;
/*
Extract the query statement from the tokenizer. The
end is either lip->ptr, if there was no lookahead,
lip->tok_end otherwise.
*/
if (no_lookahead)
qbuff.length= lip->get_ptr() - sp->m_tmp_query;
else
qbuff.length= lip->get_tok_end() - sp->m_tmp_query;
if (!(qbuff.str= (char*) alloc_root(thd->mem_root,
qbuff.length + 5)))
return true;
strmake(strmake(qbuff.str, "SET ", 4), sp->m_tmp_query,
qbuff.length);
qbuff.length+= 4;
i->m_query= qbuff;
if (sp->add_instr(i))
return true;
}
enum_var_type inner_option_type= lex->option_type;
if (lex->sphead->restore_lex(thd))
return true;
/* Copy option_type to outer lex in case it has changed. */
thd->lex->option_type= inner_option_type;
}
return false;
}
static void add_key_to_list(LEX *lex, LEX_STRING *field_name,
enum Key::Keytype type, bool check_exists)
{
Key *key;
MEM_ROOT *mem_root= lex->thd->mem_root;
key= new (mem_root)
Key(type, null_lex_str, HA_KEY_ALG_UNDEF, false,
DDL_options(check_exists ?
DDL_options::OPT_IF_NOT_EXISTS :
DDL_options::OPT_NONE));
key->columns.push_back(new (mem_root) Key_part_spec(*field_name, 0),
mem_root);
lex->alter_info.key_list.push_back(key, mem_root);
}
void LEX::init_last_field(Column_definition *field, const char *field_name,
CHARSET_INFO *cs)
{
last_field= field;
field->field_name= field_name;
/* reset LEX fields that are used in Create_field::set_and_check() */
charset= cs;
}
void LEX::set_last_field_type(const Lex_field_type_st &type)
{
last_field->sql_type= type.field_type();
last_field->charset= charset;
if (type.length())
{
int err;
last_field->length= my_strtoll10(type.length(), NULL, &err);
if (err)
last_field->length= ~0ULL; // safety
}
else
last_field->length= 0;
last_field->decimals= type.dec() ? (uint)atoi(type.dec()) : 0;
}
bool LEX::set_bincmp(CHARSET_INFO *cs, bool bin)
{
/*
if charset is NULL - we're parsing a field declaration.
we cannot call find_bin_collation for a field here, because actual
field charset is determined in get_sql_field_charset() much later.
so we only set a flag.
*/
if (!charset)
{
charset= cs;
last_field->flags|= bin ? BINCMP_FLAG : 0;
return false;
}
charset= bin ? find_bin_collation(cs ? cs : charset)
: cs ? cs : charset;
return charset == NULL;
}
#define bincmp_collation(X,Y) \
do \
{ \
if (Lex->set_bincmp(X,Y)) \
MYSQL_YYABORT; \
} while(0)
Virtual_column_info *add_virtual_expression(THD *thd, Item *expr)
{
Virtual_column_info *v= new (thd->mem_root) Virtual_column_info();
if (!v)
{
mem_alloc_error(sizeof(Virtual_column_info));
return 0;
}
v->expr= expr;
v->utf8= 0; /* connection charset */
return v;
}
%}
%union {
int num;
ulong ulong_num;
ulonglong ulonglong_number;
longlong longlong_number;
/* structs */
LEX_STRING lex_str;
LEX_SYMBOL symbol;
struct sys_var_with_base variable;
struct { int vars, conds, hndlrs, curs; } spblock;
Lex_length_and_dec_st Lex_length_and_dec;
Lex_cast_type_st Lex_cast_type;
Lex_field_type_st Lex_field_type;
Lex_dyncol_type_st Lex_dyncol_type;
/* pointers */
Create_field *create_field;
CHARSET_INFO *charset;
Condition_information_item *cond_info_item;
DYNCALL_CREATE_DEF *dyncol_def;
Diagnostics_information *diag_info;
Item *item;
Item_num *item_num;
Item_param *item_param;
Key_part_spec *key_part;
LEX *lex;
LEX_STRING *lex_str_ptr;
LEX_USER *lex_user;
List<Condition_information_item> *cond_info_list;
List<DYNCALL_CREATE_DEF> *dyncol_def_list;
List<Item> *item_list;
List<Statement_information_item> *stmt_info_list;
List<String> *string_list;
List<LEX_STRING> *lex_str_list;
Statement_information_item *stmt_info_item;
String *string;
TABLE_LIST *table_list;
Table_ident *table;
char *simple_string;
const char *const_simple_string;
chooser_compare_func_creator boolfunc2creator;
class my_var *myvar;
class sp_condition_value *spcondvalue;
class sp_head *sphead;
class sp_label *splabel;
class sp_name *spname;
class sp_variable *spvar;
class With_clause *with_clause;
class Virtual_column_info *virtual_column;
handlerton *db_type;
st_select_lex *select_lex;
struct p_elem_val *p_elem_value;
class Window_frame *window_frame;
class Window_frame_bound *window_frame_bound;
udf_func *udf;
st_trg_execution_order trg_execution_order;
/* enums */
enum Condition_information_item::Name cond_info_item_name;
enum enum_diag_condition_item_name diag_condition_item_name;
enum Diagnostics_information::Which_area diag_area;
enum Field::geometry_type geom_type;
enum enum_fk_option m_fk_option;