forked from xwb1989/sqlparser
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sql.y
2061 lines (1911 loc) · 36.3 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.
Copyright 2018 The CovenantSQL Authors.
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--
}
// forceEOF forces the lexer to end prematurely. Not all SQL statements
// are supported by the Parser, thus calling forceEOF will make the lexer
// return EOF early.
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
joinCondition JoinCondition
tableName TableName
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
setExprs SetExprs
updateExpr *UpdateExpr
setExpr *SetExpr
colIdent ColIdent
tableIdent TableIdent
convertType *ConvertType
aliasedTableName *AliasedTableExpr
TableSpec *TableSpec
columnType ColumnType
colKeyOpt ColumnKeyOption
optVal *SQLVal
LengthScaleOption LengthScaleOption
columnDefinition *ColumnDefinition
indexDefinition *IndexDefinition
indexInfo *IndexInfo
indexColumn *IndexColumn
indexColumns []*IndexColumn
}
%token LEX_ERROR
%left <bytes> UNION
%token <bytes> SELECT INSERT UPDATE DELETE FROM WHERE GROUP HAVING ORDER BY LIMIT OFFSET
%token <bytes> ALL DISTINCT AS EXISTS ASC DESC INTO KEY DEFAULT SET
%token <bytes> VALUES LAST_INSERT_ID
%left <bytes> JOIN LEFT RIGHT INNER OUTER CROSS NATURAL
%left <bytes> ON USING
%token <empty> '(' ',' ')'
%token <bytes> ID HEX STRING INTEGRAL FLOAT HEXNUM VALUE_ARG POS_ARG LIST_ARG COMMENT
%token <bytes> NULL TRUE FALSE
%token <bytes> FULL COLUMNS
// 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 IS LIKE REGEXP MATCH IN NULL_SAFE_NOTEQUAL
%left <bytes> '|'
%left <bytes> '&'
%left <bytes> SHIFT_LEFT SHIFT_RIGHT
%left <bytes> '+' '-'
%left <bytes> '*' '/' DIV '%' MOD
%left <bytes> '^'
%right <bytes> '~' UNARY
%right <bytes> INTERVAL
%nonassoc <bytes> '.'
// DDL Tokens
%token <bytes> CREATE ALTER DROP RENAME ADD
%token <bytes> TABLE VIRTUAL INDEX TO IGNORE IF UNIQUE PRIMARY COLUMN CONSTRAINT FOREIGN
%token <bytes> SHOW DESCRIBE DATE ESCAPE EXPLAIN
// Type Tokens
%token <bytes> 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 NCHAR
%token <bytes> TEXT TINYTEXT MEDIUMTEXT LONGTEXT
%token <bytes> BLOB TINYBLOB MEDIUMBLOB LONGBLOB
// Type Modifiers
%token <bytes> AUTO_INCREMENT SIGNED UNSIGNED ZEROFILL
// Supported SHOW tokens
%token <bytes> TABLES
// Functions
%token <bytes> CURRENT_TIMESTAMP CURRENT_DATE CURRENT_TIME
%token <bytes> REPLACE
%token <bytes> CAST
%token <bytes> GROUP_CONCAT SEPARATOR
// MySQL reserved words that are unused by this grammar will map to this token.
%token <bytes> UNUSED
%type <statement> command
%type <selStmt> select_statement base_select union_lhs union_rhs
%type <statement> insert_statement update_statement delete_statement
%type <statement> create_statement alter_statement drop_statement
%type <ddl> create_table_prefix
%type <statement> show_statement other_statement
%type <bytes2> comment_opt comment_list
%type <str> union_op insert_or_replace
%type <str> distinct_opt separator_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 <joinCondition> join_condition join_condition_opt
%type <str> inner_join outer_join natural_join
%type <tableName> table_name into_table_name
%type <aliasedTableName> aliased_table_name
%type <expr> where_expression_opt
%type <expr> condition
%type <boolVal> boolean_value
%type <str> compare
%type <ins> insert_data
%type <expr> value value_expression
%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 column_name_not_string
%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 <columns> ins_column_list column_list
%type <updateExprs> update_list
%type <updateExpr> update_expression
%type <str> ignore_opt
%type <byt> exists_opt
%type <empty> not_exists_opt constraint_opt
%type <bytes> reserved_keyword non_reserved_keyword
%type <colIdent> sql_id reserved_sql_id col_alias as_ci_opt
%type <tableIdent> table_id reserved_table_id table_alias as_opt_id
%type <empty> as_opt
%type <empty> ddl_force_eof
%type <convertType> convert_type
%type <columnType> column_type
%type <columnType> int_type decimal_type numeric_type time_type char_type
%type <optVal> length_opt column_default_opt
%type <boolVal> unsigned_opt zero_fill_opt
%type <LengthScaleOption> float_length_opt decimal_length_opt
%type <boolVal> null_opt auto_increment_opt
%type <colKeyOpt> column_key_opt
%type <columnDefinition> column_definition
%type <indexDefinition> index_definition
%type <str> index_or_key
%type <TableSpec> table_spec table_column_list
%type <str> table_option_list table_option table_opt_value
%type <indexInfo> index_info
%type <indexColumn> index_column
%type <indexColumns> index_column_list
%type <bytes> alter_object_type
%type <bytes> full_opt
%start any_command
%%
any_command:
command semicolon_opt
{
setParseTree(yylex, $1)
}
semicolon_opt:
/*empty*/ {}
| ';' {}
command:
select_statement
{
$$ = $1
}
| insert_statement
| update_statement
| delete_statement
| create_statement
| alter_statement
| drop_statement
| show_statement
| other_statement
select_statement:
base_select order_by_opt limit_opt
{
sel := $1.(*Select)
sel.OrderBy = $2
sel.Limit = $3
$$ = sel
}
| union_lhs union_op union_rhs order_by_opt limit_opt
{
$$ = &Union{Type: $2, Left: $1, Right: $3, OrderBy: $4, Limit: $5}
}
// base_select is an unparenthesized SELECT with no order by clause or beyond.
base_select:
SELECT comment_opt distinct_opt select_expression_list from_opt where_expression_opt group_by_opt having_opt
{
$$ = &Select{Comments: Comments($2), Distinct: $3, SelectExprs: $4, From: $5, Where: NewWhere(WhereStr, $6), GroupBy: GroupBy($7), Having: NewWhere(HavingStr, $8)}
}
| SELECT comment_opt distinct_opt select_expression_list
{
$$ = &Select{Comments: Comments($2), Distinct: $3, SelectExprs: $4}
}
union_lhs:
select_statement
{
$$ = $1
}
| openb select_statement closeb
{
$$ = &ParenSelect{Select: $2}
}
union_rhs:
base_select
{
$$ = $1
}
| openb select_statement closeb
{
$$ = &ParenSelect{Select: $2}
}
insert_statement:
insert_or_replace comment_opt ignore_opt into_table_name insert_data
{
// insert_data returns a *Insert pre-filled with Columns & Values
ins := $5
ins.Action = $1
ins.Comments = $2
ins.Ignore = $3
ins.Table = $4
$$ = ins
}
| insert_or_replace comment_opt ignore_opt into_table_name SET update_list
{
cols := make(Columns, 0, len($6))
vals := make(ValTuple, 0, len($6))
for _, updateList := range $6 {
cols = append(cols, updateList.Name.Name)
vals = append(vals, updateList.Expr)
}
$$ = &Insert{Action: $1, Comments: Comments($2), Ignore: $3, Table: $4, Columns: cols, Rows: Values{vals}}
}
insert_or_replace:
INSERT
{
$$ = InsertStr
}
| REPLACE
{
$$ = ReplaceStr
}
| INSERT OR REPLACE
{
$$ = ReplaceStr
}
update_statement:
UPDATE comment_opt table_references SET update_list where_expression_opt order_by_opt limit_opt
{
$$ = &Update{Comments: Comments($2), TableExprs: $3, Exprs: $5, Where: NewWhere(WhereStr, $6), OrderBy: $7, Limit: $8}
}
delete_statement:
DELETE comment_opt FROM table_name where_expression_opt order_by_opt limit_opt
{
$$ = &Delete{Comments: Comments($2), TableExprs: TableExprs{&AliasedTableExpr{Expr:$4}}, Where: NewWhere(WhereStr, $5), OrderBy: $6, Limit: $7}
}
create_statement:
create_table_prefix table_spec
{
$1.TableSpec = $2
$$ = $1
}
| CREATE VIRTUAL TABLE not_exists_opt table_name USING table_name ddl_force_eof
{
$$ = &DDL{Action: CreateVirtualTableStr, Table: $5, NewName: $7}
}
| CREATE constraint_opt INDEX not_exists_opt table_name ON table_name ddl_force_eof
{
// Change this to an alter statement
$$ = &DDL{Action: CreateIndexStr, Table: $7, NewName:$7}
}
create_table_prefix:
CREATE TABLE not_exists_opt table_name
{
$$ = &DDL{Action: CreateStr, NewName: $4}
setDDL(yylex, $$)
}
table_spec:
'(' table_column_list ')' table_option_list
{
$$ = $2
$$.Options = $4
}
table_column_list:
column_definition
{
$$ = &TableSpec{}
$$.AddColumn($1)
}
| table_column_list ',' column_definition
{
$$.AddColumn($3)
}
| table_column_list ',' index_definition
{
$$.AddIndex($3)
}
column_definition:
col_alias column_type null_opt column_default_opt auto_increment_opt column_key_opt
{
$2.NotNull = $3
$2.Default = $4
$2.Autoincrement = $5
$2.KeyOpt = $6
$$ = &ColumnDefinition{Name: $1, Type: $2}
}
column_type:
numeric_type unsigned_opt zero_fill_opt
{
$$ = $1
$$.Unsigned = $2
$$.Zerofill = $3
}
| char_type
| time_type
numeric_type:
int_type length_opt
{
$$ = $1
$$.Length = $2
}
| decimal_type
{
$$ = $1
}
int_type:
TINYINT
{
$$ = ColumnType{Type: string($1)}
}
| SMALLINT
{
$$ = ColumnType{Type: string($1)}
}
| MEDIUMINT
{
$$ = ColumnType{Type: string($1)}
}
| INT
{
$$ = ColumnType{Type: string($1)}
}
| INTEGER
{
$$ = ColumnType{Type: string($1)}
}
| BIGINT
{
$$ = ColumnType{Type: string($1)}
}
decimal_type:
REAL float_length_opt
{
$$ = ColumnType{Type: string($1)}
$$.Length = $2.Length
$$.Scale = $2.Scale
}
| DOUBLE float_length_opt
{
$$ = ColumnType{Type: string($1)}
$$.Length = $2.Length
$$.Scale = $2.Scale
}
| FLOAT_TYPE float_length_opt
{
$$ = ColumnType{Type: string($1)}
$$.Length = $2.Length
$$.Scale = $2.Scale
}
| DECIMAL decimal_length_opt
{
$$ = ColumnType{Type: string($1)}
$$.Length = $2.Length
$$.Scale = $2.Scale
}
| NUMERIC decimal_length_opt
{
$$ = ColumnType{Type: string($1)}
$$.Length = $2.Length
$$.Scale = $2.Scale
}
time_type:
DATE
{
$$ = ColumnType{Type: string($1)}
}
| TIME length_opt
{
$$ = ColumnType{Type: string($1), Length: $2}
}
| TIMESTAMP length_opt
{
$$ = ColumnType{Type: string($1), Length: $2}
}
| DATETIME length_opt
{
$$ = ColumnType{Type: string($1), Length: $2}
}
| YEAR
{
$$ = ColumnType{Type: string($1)}
}
char_type:
CHAR length_opt
{
$$ = ColumnType{Type: string($1), Length: $2}
}
| VARCHAR length_opt
{
$$ = ColumnType{Type: string($1), Length: $2}
}
| TEXT
{
$$ = ColumnType{Type: string($1)}
}
| TINYTEXT
{
$$ = ColumnType{Type: string($1)}
}
| MEDIUMTEXT
{
$$ = ColumnType{Type: string($1)}
}
| LONGTEXT
{
$$ = ColumnType{Type: string($1)}
}
| BLOB
{
$$ = ColumnType{Type: string($1)}
}
| TINYBLOB
{
$$ = ColumnType{Type: string($1)}
}
| MEDIUMBLOB
{
$$ = ColumnType{Type: string($1)}
}
| LONGBLOB
{
$$ = ColumnType{Type: string($1)}
}
length_opt:
{
$$ = nil
}
| '(' INTEGRAL ')'
{
$$ = NewIntVal($2)
}
float_length_opt:
{
$$ = LengthScaleOption{}
}
| '(' INTEGRAL ',' INTEGRAL ')'
{
$$ = LengthScaleOption{
Length: NewIntVal($2),
Scale: NewIntVal($4),
}
}
decimal_length_opt:
{
$$ = LengthScaleOption{}
}
| '(' INTEGRAL ')'
{
$$ = LengthScaleOption{
Length: NewIntVal($2),
}
}
| '(' INTEGRAL ',' INTEGRAL ')'
{
$$ = LengthScaleOption{
Length: NewIntVal($2),
Scale: NewIntVal($4),
}
}
unsigned_opt:
{
$$ = BoolVal(false)
}
| UNSIGNED
{
$$ = BoolVal(true)
}
zero_fill_opt:
{
$$ = BoolVal(false)
}
| ZEROFILL
{
$$ = BoolVal(true)
}
// Null opt returns false to mean NULL (i.e. the default) and true for NOT NULL
null_opt:
{
$$ = BoolVal(false)
}
| NULL
{
$$ = BoolVal(false)
}
| NOT NULL
{
$$ = BoolVal(true)
}
column_default_opt:
{
$$ = nil
}
| DEFAULT STRING
{
$$ = NewStrVal($2)
}
| DEFAULT INTEGRAL
{
$$ = NewIntVal($2)
}
| DEFAULT FLOAT
{
$$ = NewFloatVal($2)
}
| DEFAULT NULL
{
$$ = NewValArg($2)
}
| DEFAULT CURRENT_TIMESTAMP
{
$$ = NewValArg($2)
}
auto_increment_opt:
{
$$ = BoolVal(false)
}
| AUTO_INCREMENT
{
$$ = BoolVal(true)
}
column_key_opt:
{
$$ = colKeyNone
}
| PRIMARY KEY
{
$$ = colKeyPrimary
}
| KEY
{
$$ = colKey
}
| UNIQUE KEY
{
$$ = colKeyUniqueKey
}
| UNIQUE
{
$$ = colKeyUnique
}
index_definition:
index_info '(' index_column_list ')'
{
$$ = &IndexDefinition{Info: $1, Columns: $3}
}
index_info:
PRIMARY KEY
{
$$ = &IndexInfo{Type: string($1) + " " + string($2), Name: NewColIdent("PRIMARY"), Primary: true, Unique: true}
}
| UNIQUE index_or_key ID
{
$$ = &IndexInfo{Type: string($1) + " " + string($2), Name: NewColIdent(string($3)), Unique: true}
}
| UNIQUE ID
{
$$ = &IndexInfo{Type: string($1), Name: NewColIdent(string($2)), Unique: true}
}
| index_or_key ID
{
$$ = &IndexInfo{Type: string($1), Name: NewColIdent(string($2)), Unique: false}
}
index_or_key:
INDEX
{
$$ = string($1)
}
| KEY
{
$$ = string($1)
}
index_column_list:
index_column
{
$$ = []*IndexColumn{$1}
}
| index_column_list ',' index_column
{
$$ = append($$, $3)
}
index_column:
sql_id length_opt
{
$$ = &IndexColumn{Column: $1, Length: $2}
}
table_option_list:
{
$$ = ""
}
| table_option
{
$$ = " " + string($1)
}
| table_option_list ',' table_option
{
$$ = string($1) + ", " + string($3)
}
// rather than explicitly parsing the various keywords for table options,
// just accept any number of keywords, IDs, strings, numbers, and '='
table_option:
table_opt_value
{
$$ = $1
}
| table_option table_opt_value
{
$$ = $1 + " " + $2
}
| table_option '=' table_opt_value
{
$$ = $1 + "=" + $3
}
table_opt_value:
reserved_sql_id
{
$$ = $1.String()
}
| STRING
{
$$ = "'" + string($1) + "'"
}
| INTEGRAL
{
$$ = string($1)
}
alter_statement:
ALTER TABLE table_name ADD alter_object_type column_definition
{
$$ = &DDL{Action: AlterStr, Table: $3, NewName: $3}
}
| ALTER TABLE table_name RENAME TO table_name
{
// Change this to a rename statement
$$ = &DDL{Action: RenameStr, Table: $3, NewName: $6}
}
| ALTER TABLE table_name RENAME alter_object_type column_name TO column_name
{
// Rename an index can just be an alter
$$ = &DDL{Action: AlterStr, Table: $3, NewName: $3}
}
alter_object_type:
{} | COLUMN
drop_statement:
DROP TABLE exists_opt table_name
{
var exists bool
if $3 != 0 {
exists = true
}
$$ = &DDL{Action: DropStr, Table: $4, IfExists: exists}
}
| DROP INDEX exists_opt table_name
{
var exists bool
if $3 != 0 {
exists = true
}
$$ = &DDL{Action: DropIndexStr, Table: $4, IfExists: exists}
}
show_statement:
SHOW CREATE TABLE table_name
{
$$ = &Show{Type: string($3), ShowCreate: true, OnTable: $4}
}
| SHOW INDEX FROM TABLE table_name
{
$$ = &Show{Type: string($2), OnTable: $5}
}
| SHOW TABLE table_name
{
$$ = &Show{Type: string($2), OnTable: $3}
}
| SHOW full_opt TABLES
{
$$ = &Show{Type: string($3)}
}
| SHOW full_opt COLUMNS FROM table_name
{
$$ = &Show{Type: "table", OnTable: $5}
}
full_opt:
{
$$ = nil
}
| FULL
{
$$ = nil
}
other_statement:
DESC table_name
{
$$ = &Show{Type: "table", OnTable: $2}
}
| DESCRIBE table_name
{
$$ = &Show{Type: "table", OnTable: $2}
}
| EXPLAIN force_eof
{
$$ = &Explain{}
}
comment_opt:
{
setAllowComments(yylex, true)
}
comment_list
{
$$ = $2
setAllowComments(yylex, false)
}
comment_list:
{
$$ = nil
}
| comment_list COMMENT
{
$$ = append($1, $2)
}
union_op:
UNION
{
$$ = UnionStr
}
| UNION ALL
{
$$ = UnionAllStr
}
distinct_opt:
{
$$ = ""
}
| DISTINCT
{
$$ = DistinctStr
}
select_expression_list_opt:
{
$$ = nil
}
| select_expression_list
{
$$ = $1
}
select_expression_list:
select_expression
{
$$ = SelectExprs{$1}
}
| select_expression_list ',' select_expression
{
$$ = append($$, $3)
}
select_expression:
'*'
{
$$ = &StarExpr{}
}
| expression as_ci_opt
{
$$ = &AliasedExpr{Expr: $1, As: $2}
}
| table_id '.' '*'
{
$$ = &StarExpr{TableName: TableName{Name: $1}}
}
| table_id '.' reserved_table_id '.' '*'
{
$$ = &StarExpr{TableName: TableName{Qualifier: $1, Name: $3}}
}
as_ci_opt:
{
$$ = ColIdent{}
}
| col_alias
{
$$ = $1
}
| AS col_alias
{
$$ = $2
}
col_alias:
sql_id
| STRING
{
$$ = NewColIdent(string($1))
}
from_opt:
FROM table_references
{
$$ = $2
}
table_references:
table_reference
{
$$ = TableExprs{$1}
}
| table_references ',' table_reference
{
$$ = append($$, $3)
}
table_reference:
table_factor
| join_table
table_factor:
aliased_table_name
{
$$ = $1
}
| subquery
{
$$ = &AliasedTableExpr{Expr:$1}
}
| subquery as_opt table_id
{
$$ = &AliasedTableExpr{Expr:$1, As: $3}
}
| openb table_references closeb
{
$$ = &ParenTableExpr{Exprs: $2}
}
aliased_table_name:
table_name as_opt_id
{
$$ = &AliasedTableExpr{Expr:$1, As: $2}
}
column_list:
col_alias
{
$$ = Columns{$1}
}
| column_list ',' col_alias