This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
/
sql.y
4618 lines (4201 loc) · 71.9 KB
/
sql.y
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 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
%{
package sqlparser
func setParseTree(yylex interface{}, stmt Statement) {
yylex.(*Tokenizer).ParseTree = stmt
}
func setAllowComments(yylex interface{}, allow bool) {
yylex.(*Tokenizer).AllowComments = allow
}
func setDDL(yylex interface{}, ddl *DDL) {
yylex.(*Tokenizer).partialDDL = ddl
}
func incNesting(yylex interface{}) bool {
yylex.(*Tokenizer).nesting++
if yylex.(*Tokenizer).nesting == 200 {
return true
}
return false
}
func decNesting(yylex interface{}) {
yylex.(*Tokenizer).nesting--
}
func forceEOF(yylex interface{}) {
yylex.(*Tokenizer).ForceEOF = true
}
%}
%union {
empty struct{}
statement Statement
selStmt SelectStatement
ddl *DDL
ins *Insert
byt byte
bytes []byte
bytes2 [][]byte
str string
strs []string
selectExprs SelectExprs
selectExpr SelectExpr
columns Columns
colName *ColName
tableExprs TableExprs
tableExpr TableExpr
tableName TableName
tableNames TableNames
indexHints *IndexHints
expr Expr
exprs Exprs
boolVal BoolVal
colTuple ColTuple
values Values
valTuple ValTuple
subquery *Subquery
whens []*When
when *When
orderBy OrderBy
order *Order
limit *Limit
updateExprs UpdateExprs
updateExpr *UpdateExpr
setExprs SetExprs
setExpr *SetExpr
setVal SetVal
colIdent ColIdent
colIdents []ColIdent
tableIdent TableIdent
convertType *ConvertType
aliasedTableName *AliasedTableExpr
tableSpec *TableSpec
tableOptionListOpt TableOptionListOpt
tableOptionList []*TableOption
tableOption *TableOption
columnType ColumnType
colPrimaryKeyOpt ColumnPrimaryKeyOption
colUniqueKeyOpt ColumnUniqueKeyOption
optVal *SQLVal
lengthScaleOption LengthScaleOption
columnDefinition *ColumnDefinition
indexDefinition *IndexDefinition
indexColumn *IndexColumn
indexColumns []*IndexColumn
indexOptionList []*IndexOption
indexOption *IndexOption
columnOptionListOpt ColumnOptionListOpt
columnOptionList []*ColumnOption
columnOption *ColumnOption
databaseOptionListOpt DatabaseOptionListOpt
databaseOptionList []*DatabaseOption
databaseOption *DatabaseOption
partitionDefinition *PartitionDefinition
partitionDefinitions []*PartitionDefinition
showFilter *ShowFilter
}
%token LEX_ERROR
%left <bytes>
UNION
%token <bytes>
SELECT
INSERT
UPDATE
DELETE
FROM
WHERE
GROUP
HAVING
ORDER
BY
LIMIT
OFFSET
FOR
// INDEX.
%token <bytes>
ALGORITHM
BTREE
FULLTEXT
KEY_BLOCK_SIZE
NGRAM
PARSER
SPATIAL
// Resolve shift/reduce conflict on 'UNIQUE KEY', if we don`t define the precedence, the code
// doesn`t know which way to shift. Such as it can be parsed like 'UNIQUE' and 'KEY'(primary key),
// and also can be parsed like just 'UNIQUE KEY'.
// in mysql sql_yacc.cc, they are: %right UNIQUE_SYM KEY_SYM
// see: https://github.com/percona/percona-server/blob/8.0/sql/sql_yacc.yy#L1258
%right <bytes>
UNIQUE
KEY
%token <bytes>
ALL
DISTINCT
AS
EXISTS
ASC
DESC
INTO
DUPLICATE
DEFAULT
SET
LOCK
FULL
CHECKSUM
%token <bytes>
VALUES
LAST_INSERT_ID
%token <bytes>
NEXT
VALUE
SHARE
MODE
%token <bytes>
SQL_NO_CACHE
SQL_CACHE
%left <bytes>
JOIN
STRAIGHT_JOIN
LEFT
RIGHT
INNER
OUTER
CROSS
NATURAL
USE
FORCE
%left <bytes>
ON
%token <empty>
'('
','
')'
%token <bytes>
ID
HEX
STRING
INTEGRAL
FLOAT
HEXNUM
VALUE_ARG
LIST_ARG
COMMENT
COMMENT_KEYWORD
%token <bytes>
NULL
TRUE
FALSE
OFF
// Precedence dictated by mysql. But the vitess grammar is simplified.
// Some of these operators don't conflict in our situation. Nevertheless,
// it's better to have these listed in the correct order. Also, we don't
// support all operators yet.
%left <bytes>
OR
%left <bytes>
AND
%right <bytes>
NOT
'!'
%left <bytes>
BETWEEN
CASE
WHEN
THEN
ELSE
END
%left <bytes>
'='
'<'
'>'
LE
GE
NE
NULL_SAFE_EQUAL
IS
LIKE
REGEXP
IN
%left <bytes>
'|'
%left <bytes>
'&'
%left <bytes>
SHIFT_LEFT
SHIFT_RIGHT
%left <bytes>
'+'
'-'
%left <bytes>
'*'
'/'
DIV
'%'
MOD
%left <bytes>
'^'
%right <bytes>
'~'
UNARY
%left <bytes>
COLLATE
%right <bytes>
BINARY
%right <bytes>
INTERVAL
%nonassoc <bytes>
'.'
// There is no need to define precedence for the JSON
// operators because the syntax is restricted enough that
// they don't cause conflicts.
%token <empty>
JSON_EXTRACT_OP
JSON_UNQUOTE_EXTRACT_OP
// DDL Tokens
%token <bytes>
CREATE
ALTER
DROP
RENAME
ANALYZE
ADD
MODIFY
%token <bytes>
TABLE
INDEX
VIEW
TO
IGNORE
IF
USING
PRIMARY
COLUMN
%token <bytes>
SHOW
DESCRIBE
EXPLAIN
DATE
ESCAPE
REPAIR
OPTIMIZE
TRUNCATE
// Type Tokens
%token <bytes>
BIT
TINYINT
SMALLINT
MEDIUMINT
INT
INTEGER
BIGINT
INTNUM
%token <bytes>
REAL
DOUBLE
FLOAT_TYPE
DECIMAL
NUMERIC
%token <bytes>
TIME
TIMESTAMP
DATETIME
YEAR
%token <bytes>
CHAR
VARCHAR
BOOL
CHARACTER
VARBINARY
NCHAR
CHARSET
%token <bytes>
TEXT
TINYTEXT
MEDIUMTEXT
LONGTEXT
%token <bytes>
BLOB
TINYBLOB
MEDIUMBLOB
LONGBLOB
JSON
ENUM
%token <bytes>
GEOMETRY
POINT
LINESTRING
POLYGON
GEOMETRYCOLLECTION
MULTIPOINT
MULTILINESTRING
MULTIPOLYGON
// Type Modifiers
%token <bytes>
NULLX
AUTO_INCREMENT
APPROXNUM
SIGNED
UNSIGNED
ZEROFILL
FIXED
DYNAMIC
STORAGE
DISK
MEMORY
COLUMN_FORMAT
AVG_ROW_LENGTH
COMPRESSION
CONNECTION
DATA
DIRECTORY
DELAY_KEY_WRITE
ENCRYPTION
INSERT_METHOD
MAX_ROWS
MIN_ROWS
PACK_KEYS
PASSWORD
ROW_FORMAT
STATS_AUTO_RECALC
STATS_PERSISTENT
STATS_SAMPLE_PAGES
TABLESPACE
// ROW_FORMAT options
%token <bytes>
COMPRESSED
REDUNDANT
COMPACT
TOKUDB_DEFAULT
TOKUDB_FAST
TOKUDB_SMALL
TOKUDB_ZLIB
TOKUDB_QUICKLZ
TOKUDB_LZMA
TOKUDB_SNAPPY
TOKUDB_UNCOMPRESSED
// Supported SHOW tokens
%token <bytes>
DATABASES
TABLES
WARNINGS
VARIABLES
EVENTS
BINLOG
GTID
STATUS
COLUMNS
FIELDS
// Functions
%token <bytes>
CURRENT_TIMESTAMP
DATABASE
CURRENT_DATE
%token <bytes>
CURRENT_TIME
LOCALTIME
LOCALTIMESTAMP
%token <bytes>
UTC_DATE
UTC_TIME
UTC_TIMESTAMP
%token <bytes>
REPLACE
%token <bytes>
CONVERT
CAST
%token <bytes>
GROUP_CONCAT
SEPARATOR
// Match
%token <bytes>
MATCH
AGAINST
BOOLEAN
LANGUAGE
WITH
QUERY
EXPANSION
// MySQL reserved words that are unused by this grammar will map to this token.
%token <bytes>
UNUSED
// RadonDB
%token <empty>
PARTITION
PARTITIONS
HASH
LIST
XA
DISTRIBUTED
%type <statement>
truncate_statement
xa_statement
explain_statement
kill_statement
transaction_statement
radon_statement
%token <bytes>
ENGINES
VERSIONS
PROCESSLIST
QUERYZ
TXNZ
KILL
ENGINE
SINGLE
// Transaction Tokens
%token <bytes>
BEGIN
START
TRANSACTION
COMMIT
ROLLBACK
// SET tokens
%token <bytes>
GLOBAL
LOCAL
SESSION
NAMES
ISOLATION
LEVEL
READ
WRITE
ONLY
REPEATABLE
COMMITTED
UNCOMMITTED
SERIALIZABLE
// Radon Tokens
%token <bytes>
RADON
ATTACH
ATTACHLIST
DETACH
RESHARD
CLEANUP
%type <statement>
command
%type <selStmt>
select_statement
base_select
union_lhs
union_rhs
%type <statement>
insert_statement
update_statement
delete_statement
set_statement
%type <statement>
create_statement
alter_statement
drop_statement
%type <ddl>
create_table_prefix
%type <statement>
analyze_statement
show_statement
use_statement
other_statement
checksum_statement
%type <bytes2>
comment_opt
comment_list
%type <str>
union_op
insert_or_replace
now_sym_with_frac_opt
on_update_opt
%type <str>
distinct_opt
straight_join_opt
cache_opt
match_option
separator_opt
binlog_from_opt
%type <expr>
like_escape_opt
%type <selectExprs>
select_expression_list
select_expression_list_opt
%type <selectExpr>
select_expression
%type <expr>
expression
%type <tableExprs>
from_opt
table_references
%type <tableExpr>
table_reference
table_factor
join_table
%type <str>
inner_join
outer_join
natural_join
%type <tableName>
table_name
into_table_name
database_from_opt
%type <str>
full_opt
columns_or_fields
%type <showFilter>
like_or_where_opt
%type <tableNames>
table_name_list
%type <aliasedTableName>
aliased_table_name
%type <indexHints>
index_hint_list
%type <colIdents>
index_list
%type <expr>
where_expression_opt
%type <expr>
condition
%type <boolVal>
boolean_value
%type <str>
compare
%type <ins>
insert_data
%type <expr>
value
value_expression
num_val
%type <expr>
function_call_keyword
function_call_nonkeyword
function_call_generic
function_call_conflict
%type <str>
is_suffix
%type <colTuple>
col_tuple
%type <exprs>
expression_list
%type <values>
tuple_list
%type <valTuple>
row_tuple
tuple_or_empty
%type <expr>
tuple_expression
%type <subquery>
subquery
%type <colName>
column_name
%type <whens>
when_expression_list
%type <when>
when_expression
%type <expr>
expression_opt
else_expression_opt
%type <exprs>
group_by_opt
%type <expr>
having_opt
%type <orderBy>
order_by_opt
order_list
%type <order>
order
%type <str>
asc_desc_opt
%type <limit>
limit_opt
%type <str>
lock_opt
%type <columns>
ins_column_list
%type <updateExprs>
on_dup_opt
%type <updateExprs>
update_list
%type <updateExpr>
update_expression
%type <setExprs>
set_list
txn_list
%type <setExpr>
set_opt_vals
set_expression
%type <setVal>
set_txn_vals
%type <str>
access_mode
access_mode_opt
isolation_level
isolation_level_opt
isolation_types
%type <expr>
charset_value
%type <bytes>
charset_or_character_set
now_sym
%type <bytes>
for_from
%type <str>
ignore_opt
default_opt
%type <byt>
exists_opt
not_exists_opt
%type <empty>
non_rename_operation
to_opt
index_opt
%type <bytes>
reserved_keyword
non_reserved_keyword
%type <colIdent>
sql_id
reserved_sql_id
col_alias
as_ci_opt
col_id
%type <tableIdent>
table_id
reserved_table_id
table_alias
as_opt_id
%type <empty>
as_opt
%type <empty>
force_eof
ddl_force_eof
%type <str>
charset
%type <str>
set_session_or_global
%type <convertType>
convert_type
%type <columnType>
column_type
%type <columnType>
int_type
decimal_type
numeric_type
time_type
char_type
spatial_type
// table options
%type <optVal>
length_opt
column_default_opt
column_comment_opt
col_collate_opt
table_engine_option
parts_num_opt
table_charset_option
table_type_option
table_auto_opt
table_comment_opt
table_avg_row_length_opt
table_checksum_opt
table_collate_opt
table_compression_opt
table_connection_opt
table_data_directory_opt
table_index_directory_opt
table_delay_key_write_opt
table_encryption_opt
table_insert_method_opt
table_key_block_size_opt
table_max_rows_opt
table_min_rows_opt
table_pack_keys_opt
table_password_opt
table_row_format_opt
table_stats_auto_recalc_opt
table_stats_persistent_opt
table_stats_sample_pages_opt
table_tablespace_opt
%type <str>
charset_opt
collate_opt
column_format_opt
storage_opt
%type <optVal>
id_or_string
%type <str>
opt_charset
opt_equal
opt_default
charset_name_or_default
%type <boolVal>
unsigned_opt
zero_fill_opt
%type <lengthScaleOption>
float_length_opt
decimal_length_opt
%type <boolVal>
null_opt
auto_increment_opt
%type <colPrimaryKeyOpt>
column_primary_key_opt
%type <colUniqueKeyOpt>
column_unique_key_opt
%type <strs>
enum_values
%type <columnDefinition>
column_definition
%type <indexDefinition>
index_definition
%type <str>
index_or_key
%type <tableSpec>
table_spec
table_column_list
%type <tableOptionListOpt>
table_option_list_opt
%type <tableOptionList>
table_option_list
%type <tableOption>
table_option
%type <indexColumn>
index_column
%type <indexColumns>
index_column_list
%type <indexOptionList>
fulltext_key_opts
lock_algorithm_opts
normal_key_opts
spatial_key_opts
%type <indexOption>
all_key_opt
fulltext_key_opt
index_using_opt
lock_algorithm_opt
normal_key_opt
%type <str>
constraint_opt
index_using_str
id_or_default
%type <columnOptionListOpt>
column_option_list_opt
%type <columnOptionList>
column_option_list
%type <columnOption>
column_option
%type <databaseOptionListOpt>
database_option_list_opt
%type <databaseOptionList>
database_option_list
%type <databaseOption>
database_option
%type <partitionDefinition>
partition_definition
%type <partitionDefinitions>
partition_definitions
%start any_command
%%
any_command:
command semicolon_opt
{
setParseTree(yylex, $1)
}
semicolon_opt:
/*empty*/
{}
| ';'
{}