-
Notifications
You must be signed in to change notification settings - Fork 240
/
sql.y
3159 lines (2947 loc) · 57.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--
}
// 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
partitions Partitions
colName *ColName
tableExprs TableExprs
tableExpr TableExpr
joinCondition JoinCondition
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
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
indexOption *IndexOption
indexOptions []*IndexOption
indexColumn *IndexColumn
indexColumns []*IndexColumn
partDefs []*PartitionDefinition
partDef *PartitionDefinition
partSpec *PartitionSpec
vindexParam VindexParam
vindexParams []VindexParam
showFilter *ShowFilter
}
%token LEX_ERROR
%left <bytes> UNION
%token <bytes> SELECT STREAM INSERT UPDATE DELETE FROM WHERE GROUP HAVING ORDER BY LIMIT OFFSET FOR
%token <bytes> ALL DISTINCT AS EXISTS ASC DESC INTO DUPLICATE KEY DEFAULT SET LOCK KEYS
%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 USING
%token <empty> '(' ',' ')'
%token <bytes> ID HEX STRING INTEGRAL FLOAT HEXNUM VALUE_ARG LIST_ARG COMMENT COMMENT_KEYWORD BIT_LITERAL
%token <bytes> NULL TRUE FALSE
// 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 UNDERSCORE_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
%token <bytes> SCHEMA TABLE INDEX VIEW TO IGNORE IF UNIQUE PRIMARY COLUMN CONSTRAINT SPATIAL FULLTEXT FOREIGN KEY_BLOCK_SIZE
%token <bytes> SHOW DESCRIBE EXPLAIN DATE ESCAPE REPAIR OPTIMIZE TRUNCATE
%token <bytes> MAXVALUE PARTITION REORGANIZE LESS THAN PROCEDURE TRIGGER
%token <bytes> VINDEX VINDEXES
%token <bytes> STATUS VARIABLES
// Transaction Tokens
%token <bytes> BEGIN START TRANSACTION COMMIT ROLLBACK
// 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
%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
// Supported SHOW tokens
%token <bytes> DATABASES TABLES VITESS_KEYSPACES VITESS_SHARDS VITESS_TABLETS VSCHEMA_TABLES EXTENDED FULL PROCESSLIST
// SET tokens
%token <bytes> NAMES CHARSET GLOBAL SESSION ISOLATION LEVEL READ WRITE ONLY REPEATABLE COMMITTED UNCOMMITTED SERIALIZABLE
// 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> SUBSTR SUBSTRING
%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
%type <statement> command
%type <selStmt> select_statement base_select union_lhs union_rhs
%type <statement> stream_statement insert_statement update_statement delete_statement set_statement
%type <statement> create_statement alter_statement rename_statement drop_statement truncate_statement
%type <ddl> create_table_prefix
%type <statement> analyze_statement show_statement use_statement other_statement
%type <statement> begin_statement commit_statement rollback_statement
%type <bytes2> comment_opt comment_list
%type <str> union_op insert_or_replace
%type <str> distinct_opt straight_join_opt cache_opt match_option 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 on_expression_opt
%type <tableNames> table_name_list
%type <str> inner_join outer_join straight_join natural_join
%type <tableName> table_name into_table_name
%type <aliasedTableName> aliased_table_name
%type <indexHints> index_hint_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 column_list
%type <partitions> opt_partition_clause partition_list
%type <updateExprs> on_dup_opt
%type <updateExprs> update_list
%type <setExprs> set_list transaction_chars
%type <bytes> charset_or_character_set
%type <updateExpr> update_expression
%type <setExpr> set_expression transaction_char isolation_level
%type <bytes> for_from
%type <str> ignore_opt default_opt
%type <str> extended_opt full_opt from_database_opt tables_or_processlist
%type <showFilter> like_or_where_opt
%type <byt> exists_opt
%type <empty> not_exists_opt non_add_drop_or_rename_operation to_opt index_opt constraint_opt
%type <bytes> reserved_keyword non_reserved_keyword
%type <colIdent> sql_id reserved_sql_id col_alias as_ci_opt using_opt
%type <expr> charset_value
%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 show_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
%type <optVal> length_opt column_default_opt column_comment_opt on_update_opt
%type <str> charset_opt collate_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 <strs> enum_values
%type <columnDefinition> column_definition
%type <indexDefinition> index_definition
%type <str> index_or_key
%type <str> equal_opt
%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 <indexOption> index_option
%type <indexOptions> index_option_list
%type <partDefs> partition_definitions
%type <partDef> partition_definition
%type <partSpec> partition_operation
%type <vindexParam> vindex_param
%type <vindexParams> vindex_param_list vindex_params_opt
%type <colIdent> vindex_type vindex_type_opt
%type <bytes> alter_object_type
%start any_command
%%
any_command:
command semicolon_opt
{
setParseTree(yylex, $1)
}
semicolon_opt:
/*empty*/ {}
| ';' {}
command:
select_statement
{
$$ = $1
}
| stream_statement
| insert_statement
| update_statement
| delete_statement
| set_statement
| create_statement
| alter_statement
| rename_statement
| drop_statement
| truncate_statement
| analyze_statement
| show_statement
| use_statement
| begin_statement
| commit_statement
| rollback_statement
| other_statement
select_statement:
base_select order_by_opt limit_opt lock_opt
{
sel := $1.(*Select)
sel.OrderBy = $2
sel.Limit = $3
sel.Lock = $4
$$ = sel
}
| union_lhs union_op union_rhs order_by_opt limit_opt lock_opt
{
$$ = &Union{Type: $2, Left: $1, Right: $3, OrderBy: $4, Limit: $5, Lock: $6}
}
| SELECT comment_opt cache_opt NEXT num_val for_from table_name
{
$$ = &Select{Comments: Comments($2), Cache: $3, SelectExprs: SelectExprs{Nextval{Expr: $5}}, From: TableExprs{&AliasedTableExpr{Expr: $7}}}
}
stream_statement:
STREAM comment_opt select_expression FROM table_name
{
$$ = &Stream{Comments: Comments($2), SelectExpr: $3, Table: $5}
}
// base_select is an unparenthesized SELECT with no order by clause or beyond.
base_select:
SELECT comment_opt cache_opt distinct_opt straight_join_opt select_expression_list from_opt where_expression_opt group_by_opt having_opt
{
$$ = &Select{Comments: Comments($2), Cache: $3, Distinct: $4, Hints: $5, SelectExprs: $6, From: $7, Where: NewWhere(WhereStr, $8), GroupBy: GroupBy($9), Having: NewWhere(HavingStr, $10)}
}
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 opt_partition_clause insert_data on_dup_opt
{
// insert_data returns a *Insert pre-filled with Columns & Values
ins := $6
ins.Action = $1
ins.Comments = $2
ins.Ignore = $3
ins.Table = $4
ins.Partitions = $5
ins.OnDup = OnDup($7)
$$ = ins
}
| insert_or_replace comment_opt ignore_opt into_table_name opt_partition_clause SET update_list on_dup_opt
{
cols := make(Columns, 0, len($7))
vals := make(ValTuple, 0, len($8))
for _, updateList := range $7 {
cols = append(cols, updateList.Name.Name)
vals = append(vals, updateList.Expr)
}
$$ = &Insert{Action: $1, Comments: Comments($2), Ignore: $3, Table: $4, Partitions: $5, Columns: cols, Rows: Values{vals}, OnDup: OnDup($8)}
}
insert_or_replace:
INSERT
{
$$ = InsertStr
}
| 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 opt_partition_clause where_expression_opt order_by_opt limit_opt
{
$$ = &Delete{Comments: Comments($2), TableExprs: TableExprs{&AliasedTableExpr{Expr:$4}}, Partitions: $5, Where: NewWhere(WhereStr, $6), OrderBy: $7, Limit: $8}
}
| DELETE comment_opt FROM table_name_list USING table_references where_expression_opt
{
$$ = &Delete{Comments: Comments($2), Targets: $4, TableExprs: $6, Where: NewWhere(WhereStr, $7)}
}
| DELETE comment_opt table_name_list from_or_using table_references where_expression_opt
{
$$ = &Delete{Comments: Comments($2), Targets: $3, TableExprs: $5, Where: NewWhere(WhereStr, $6)}
}
from_or_using:
FROM {}
| USING {}
table_name_list:
table_name
{
$$ = TableNames{$1}
}
| table_name_list ',' table_name
{
$$ = append($$, $3)
}
opt_partition_clause:
{
$$ = nil
}
| PARTITION openb partition_list closeb
{
$$ = $3
}
set_statement:
SET comment_opt set_list
{
$$ = &Set{Comments: Comments($2), Exprs: $3}
}
| SET comment_opt set_session_or_global set_list
{
$$ = &Set{Comments: Comments($2), Scope: $3, Exprs: $4}
}
| SET comment_opt set_session_or_global TRANSACTION transaction_chars
{
$$ = &Set{Comments: Comments($2), Scope: $3, Exprs: $5}
}
| SET comment_opt TRANSACTION transaction_chars
{
$$ = &Set{Comments: Comments($2), Exprs: $4}
}
transaction_chars:
transaction_char
{
$$ = SetExprs{$1}
}
| transaction_chars ',' transaction_char
{
$$ = append($$, $3)
}
transaction_char:
ISOLATION LEVEL isolation_level
{
$$ = $3
}
| READ WRITE
{
$$ = &SetExpr{Name: NewColIdent("tx_read_only"), Expr: NewIntVal([]byte("0"))}
}
| READ ONLY
{
$$ = &SetExpr{Name: NewColIdent("tx_read_only"), Expr: NewIntVal([]byte("1"))}
}
isolation_level:
REPEATABLE READ
{
$$ = &SetExpr{Name: NewColIdent("tx_isolation"), Expr: NewStrVal([]byte("repeatable read"))}
}
| READ COMMITTED
{
$$ = &SetExpr{Name: NewColIdent("tx_isolation"), Expr: NewStrVal([]byte("read committed"))}
}
| READ UNCOMMITTED
{
$$ = &SetExpr{Name: NewColIdent("tx_isolation"), Expr: NewStrVal([]byte("read uncommitted"))}
}
| SERIALIZABLE
{
$$ = &SetExpr{Name: NewColIdent("tx_isolation"), Expr: NewStrVal([]byte("serializable"))}
}
set_session_or_global:
SESSION
{
$$ = SessionStr
}
| GLOBAL
{
$$ = GlobalStr
}
create_statement:
create_table_prefix table_spec
{
$1.TableSpec = $2
$$ = $1
}
| CREATE constraint_opt INDEX ID using_opt ON table_name ddl_force_eof
{
// Change this to an alter statement
$$ = &DDL{Action: AlterStr, Table: $7, NewName:$7}
}
| CREATE VIEW table_name ddl_force_eof
{
$$ = &DDL{Action: CreateStr, NewName: $3.ToViewName()}
}
| CREATE OR REPLACE VIEW table_name ddl_force_eof
{
$$ = &DDL{Action: CreateStr, NewName: $5.ToViewName()}
}
| CREATE VINDEX sql_id vindex_type_opt vindex_params_opt
{
$$ = &DDL{Action: CreateVindexStr, VindexSpec: &VindexSpec{
Name: $3,
Type: $4,
Params: $5,
}}
}
| CREATE DATABASE not_exists_opt ID ddl_force_eof
{
$$ = &DBDDL{Action: CreateStr, DBName: string($4)}
}
| CREATE SCHEMA not_exists_opt ID ddl_force_eof
{
$$ = &DBDDL{Action: CreateStr, DBName: string($4)}
}
vindex_type_opt:
{
$$ = NewColIdent("")
}
| USING vindex_type
{
$$ = $2
}
vindex_type:
ID
{
$$ = NewColIdent(string($1))
}
vindex_params_opt:
{
var v []VindexParam
$$ = v
}
| WITH vindex_param_list
{
$$ = $2
}
vindex_param_list:
vindex_param
{
$$ = make([]VindexParam, 0, 4)
$$ = append($$, $1)
}
| vindex_param_list ',' vindex_param
{
$$ = append($$, $3)
}
vindex_param:
reserved_sql_id '=' table_opt_value
{
$$ = VindexParam{Key: $1, Val: $3}
}
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:
ID column_type null_opt column_default_opt on_update_opt auto_increment_opt column_key_opt column_comment_opt
{
$2.NotNull = $3
$2.Default = $4
$2.OnUpdate = $5
$2.Autoincrement = $6
$2.KeyOpt = $7
$2.Comment = $8
$$ = &ColumnDefinition{Name: NewColIdent(string($1)), Type: $2}
}
column_type:
numeric_type unsigned_opt zero_fill_opt
{
$$ = $1
$$.Unsigned = $2
$$.Zerofill = $3
}
| char_type
| time_type
| spatial_type
numeric_type:
int_type length_opt
{
$$ = $1
$$.Length = $2
}
| decimal_type
{
$$ = $1
}
int_type:
BIT
{
$$ = ColumnType{Type: string($1)}
}
| 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 charset_opt collate_opt
{
$$ = ColumnType{Type: string($1), Length: $2, Charset: $3, Collate: $4}
}
| VARCHAR length_opt charset_opt collate_opt
{
$$ = ColumnType{Type: string($1), Length: $2, Charset: $3, Collate: $4}
}
| BINARY length_opt
{
$$ = ColumnType{Type: string($1), Length: $2}
}
| VARBINARY length_opt
{
$$ = ColumnType{Type: string($1), Length: $2}
}
| TEXT charset_opt collate_opt
{
$$ = ColumnType{Type: string($1), Charset: $2, Collate: $3}
}
| TINYTEXT charset_opt collate_opt
{
$$ = ColumnType{Type: string($1), Charset: $2, Collate: $3}
}
| MEDIUMTEXT charset_opt collate_opt
{
$$ = ColumnType{Type: string($1), Charset: $2, Collate: $3}
}
| LONGTEXT charset_opt collate_opt
{
$$ = ColumnType{Type: string($1), Charset: $2, Collate: $3}
}
| BLOB
{
$$ = ColumnType{Type: string($1)}
}
| TINYBLOB
{
$$ = ColumnType{Type: string($1)}
}
| MEDIUMBLOB
{
$$ = ColumnType{Type: string($1)}
}
| LONGBLOB
{
$$ = ColumnType{Type: string($1)}
}
| JSON
{
$$ = ColumnType{Type: string($1)}
}
| ENUM '(' enum_values ')' charset_opt collate_opt
{
$$ = ColumnType{Type: string($1), EnumValues: $3, Charset: $5, Collate: $6}
}
// need set_values / SetValues ?
| SET '(' enum_values ')' charset_opt collate_opt
{
$$ = ColumnType{Type: string($1), EnumValues: $3, Charset: $5, Collate: $6}
}
spatial_type:
GEOMETRY
{
$$ = ColumnType{Type: string($1)}
}
| POINT
{
$$ = ColumnType{Type: string($1)}
}
| LINESTRING
{
$$ = ColumnType{Type: string($1)}
}
| POLYGON
{
$$ = ColumnType{Type: string($1)}
}
| GEOMETRYCOLLECTION
{
$$ = ColumnType{Type: string($1)}
}
| MULTIPOINT
{
$$ = ColumnType{Type: string($1)}
}
| MULTILINESTRING
{
$$ = ColumnType{Type: string($1)}
}
| MULTIPOLYGON
{
$$ = ColumnType{Type: string($1)}
}
enum_values:
STRING
{
$$ = make([]string, 0, 4)
$$ = append($$, "'" + string($1) + "'")
}
| enum_values ',' STRING
{
$$ = append($1, "'" + string($3) + "'")
}
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)
}
| DEFAULT BIT_LITERAL
{
$$ = NewBitVal($2)
}
on_update_opt:
{
$$ = nil
}
| ON UPDATE CURRENT_TIMESTAMP
{
$$ = NewValArg($3)
}
auto_increment_opt:
{
$$ = BoolVal(false)
}
| AUTO_INCREMENT
{
$$ = BoolVal(true)
}
charset_opt:
{
$$ = ""
}
| CHARACTER SET ID
{
$$ = string($3)
}
| CHARACTER SET BINARY
{
$$ = string($3)
}
collate_opt:
{
$$ = ""
}
| COLLATE ID
{