Debian Codegraph @

sql.y

raw
1 /*
2 Copyright 2017 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 %{
18 package sqlparser
19
20 func setParseTree(yylex interface{}, stmt Statement) {
21 yylex.(*Tokenizer).ParseTree = stmt
22 }
23
24 func setAllowComments(yylex interface{}, allow bool) {
25 yylex.(*Tokenizer).AllowComments = allow
26 }
27
28 func setDDL(yylex interface{}, ddl *DDL) {
29 yylex.(*Tokenizer).partialDDL = ddl
30 }
31
32 func incNesting(yylex interface{}) bool {
33 yylex.(*Tokenizer).nesting++
34 if yylex.(*Tokenizer).nesting == 200 {
35 return true
36 }
37 return false
38 }
39
40 func decNesting(yylex interface{}) {
41 yylex.(*Tokenizer).nesting--
42 }
43
44 // forceEOF forces the lexer to end prematurely. Not all SQL statements
45 // are supported by the Parser, thus calling forceEOF will make the lexer
46 // return EOF early.
47 func forceEOF(yylex interface{}) {
48 yylex.(*Tokenizer).ForceEOF = true
49 }
50
51 %}
52
53 %union {
54 empty struct{}
55 statement Statement
56 selStmt SelectStatement
57 ddl *DDL
58 ins *Insert
59 byt byte
60 bytes []byte
61 bytes2 [][]byte
62 str string
63 strs []string
64 selectExprs SelectExprs
65 selectExpr SelectExpr
66 columns Columns
67 partitions Partitions
68 colName *ColName
69 tableExprs TableExprs
70 tableExpr TableExpr
71 joinCondition JoinCondition
72 tableName TableName
73 tableNames TableNames
74 indexHints *IndexHints
75 expr Expr
76 exprs Exprs
77 boolVal BoolVal
78 colTuple ColTuple
79 values Values
80 valTuple ValTuple
81 subquery *Subquery
82 whens []*When
83 when *When
84 orderBy OrderBy
85 order *Order
86 limit *Limit
87 updateExprs UpdateExprs
88 setExprs SetExprs
89 updateExpr *UpdateExpr
90 setExpr *SetExpr
91 colIdent ColIdent
92 tableIdent TableIdent
93 convertType *ConvertType
94 aliasedTableName *AliasedTableExpr
95 TableSpec *TableSpec
96 columnType ColumnType
97 colKeyOpt ColumnKeyOption
98 optVal *SQLVal
99 LengthScaleOption LengthScaleOption
100 columnDefinition *ColumnDefinition
101 indexDefinition *IndexDefinition
102 indexInfo *IndexInfo
103 indexOption *IndexOption
104 indexOptions []*IndexOption
105 indexColumn *IndexColumn
106 indexColumns []*IndexColumn
107 partDefs []*PartitionDefinition
108 partDef *PartitionDefinition
109 partSpec *PartitionSpec
110 vindexParam VindexParam
111 vindexParams []VindexParam
112 showFilter *ShowFilter
113 }
114
115 %token LEX_ERROR
116 %left <bytes> UNION
117 %token <bytes> SELECT STREAM INSERT UPDATE DELETE FROM WHERE GROUP HAVING ORDER BY LIMIT OFFSET FOR
118 %token <bytes> ALL DISTINCT AS EXISTS ASC DESC INTO DUPLICATE KEY DEFAULT SET LOCK KEYS
119 %token <bytes> VALUES LAST_INSERT_ID
120 %token <bytes> NEXT VALUE SHARE MODE
121 %token <bytes> SQL_NO_CACHE SQL_CACHE
122 %left <bytes> JOIN STRAIGHT_JOIN LEFT RIGHT INNER OUTER CROSS NATURAL USE FORCE
123 %left <bytes> ON USING
124 %token <empty> '(' ',' ')'
125 %token <bytes> ID HEX STRING INTEGRAL FLOAT HEXNUM VALUE_ARG LIST_ARG COMMENT COMMENT_KEYWORD BIT_LITERAL
126 %token <bytes> NULL TRUE FALSE
127
128 // Precedence dictated by mysql. But the vitess grammar is simplified.
129 // Some of these operators don't conflict in our situation. Nevertheless,
130 // it's better to have these listed in the correct order. Also, we don't
131 // support all operators yet.
132 %left <bytes> OR
133 %left <bytes> AND
134 %right <bytes> NOT '!'
135 %left <bytes> BETWEEN CASE WHEN THEN ELSE END
136 %left <bytes> '=' '<' '>' LE GE NE NULL_SAFE_EQUAL IS LIKE REGEXP IN
137 %left <bytes> '|'
138 %left <bytes> '&'
139 %left <bytes> SHIFT_LEFT SHIFT_RIGHT
140 %left <bytes> '+' '-'
141 %left <bytes> '*' '/' DIV '%' MOD
142 %left <bytes> '^'
143 %right <bytes> '~' UNARY
144 %left <bytes> COLLATE
145 %right <bytes> BINARY UNDERSCORE_BINARY
146 %right <bytes> INTERVAL
147 %nonassoc <bytes> '.'
148
149 // There is no need to define precedence for the JSON
150 // operators because the syntax is restricted enough that
151 // they don't cause conflicts.
152 %token <empty> JSON_EXTRACT_OP JSON_UNQUOTE_EXTRACT_OP
153
154 // DDL Tokens
155 %token <bytes> CREATE ALTER DROP RENAME ANALYZE ADD
156 %token <bytes> SCHEMA TABLE INDEX VIEW TO IGNORE IF UNIQUE PRIMARY COLUMN CONSTRAINT SPATIAL FULLTEXT FOREIGN KEY_BLOCK_SIZE
157 %token <bytes> SHOW DESCRIBE EXPLAIN DATE ESCAPE REPAIR OPTIMIZE TRUNCATE
158 %token <bytes> MAXVALUE PARTITION REORGANIZE LESS THAN PROCEDURE TRIGGER
159 %token <bytes> VINDEX VINDEXES
160 %token <bytes> STATUS VARIABLES
161
162 // Transaction Tokens
163 %token <bytes> BEGIN START TRANSACTION COMMIT ROLLBACK
164
165 // Type Tokens
166 %token <bytes> BIT TINYINT SMALLINT MEDIUMINT INT INTEGER BIGINT INTNUM
167 %token <bytes> REAL DOUBLE FLOAT_TYPE DECIMAL NUMERIC
168 %token <bytes> TIME TIMESTAMP DATETIME YEAR
169 %token <bytes> CHAR VARCHAR BOOL CHARACTER VARBINARY NCHAR
170 %token <bytes> TEXT TINYTEXT MEDIUMTEXT LONGTEXT
171 %token <bytes> BLOB TINYBLOB MEDIUMBLOB LONGBLOB JSON ENUM
172 %token <bytes> GEOMETRY POINT LINESTRING POLYGON GEOMETRYCOLLECTION MULTIPOINT MULTILINESTRING MULTIPOLYGON
173
174 // Type Modifiers
175 %token <bytes> NULLX AUTO_INCREMENT APPROXNUM SIGNED UNSIGNED ZEROFILL
176
177 // Supported SHOW tokens
178 %token <bytes> DATABASES TABLES VITESS_KEYSPACES VITESS_SHARDS VITESS_TABLETS VSCHEMA_TABLES EXTENDED FULL PROCESSLIST
179
180 // SET tokens
181 %token <bytes> NAMES CHARSET GLOBAL SESSION ISOLATION LEVEL READ WRITE ONLY REPEATABLE COMMITTED UNCOMMITTED SERIALIZABLE
182
183 // Functions
184 %token <bytes> CURRENT_TIMESTAMP DATABASE CURRENT_DATE
185 %token <bytes> CURRENT_TIME LOCALTIME LOCALTIMESTAMP
186 %token <bytes> UTC_DATE UTC_TIME UTC_TIMESTAMP
187 %token <bytes> REPLACE
188 %token <bytes> CONVERT CAST
189 %token <bytes> SUBSTR SUBSTRING
190 %token <bytes> GROUP_CONCAT SEPARATOR
191
192 // Match
193 %token <bytes> MATCH AGAINST BOOLEAN LANGUAGE WITH QUERY EXPANSION
194
195 // MySQL reserved words that are unused by this grammar will map to this token.
196 %token <bytes> UNUSED
197
198 %type <statement> command
199 %type <selStmt> select_statement base_select union_lhs union_rhs
200 %type <statement> stream_statement insert_statement update_statement delete_statement set_statement
201 %type <statement> create_statement alter_statement rename_statement drop_statement truncate_statement
202 %type <ddl> create_table_prefix
203 %type <statement> analyze_statement show_statement use_statement other_statement
204 %type <statement> begin_statement commit_statement rollback_statement
205 %type <bytes2> comment_opt comment_list
206 %type <str> union_op insert_or_replace
207 %type <str> distinct_opt straight_join_opt cache_opt match_option separator_opt
208 %type <expr> like_escape_opt
209 %type <selectExprs> select_expression_list select_expression_list_opt
210 %type <selectExpr> select_expression
211 %type <expr> expression
212 %type <tableExprs> from_opt table_references
213 %type <tableExpr> table_reference table_factor join_table
214 %type <joinCondition> join_condition join_condition_opt on_expression_opt
215 %type <tableNames> table_name_list
216 %type <str> inner_join outer_join straight_join natural_join
217 %type <tableName> table_name into_table_name
218 %type <aliasedTableName> aliased_table_name
219 %type <indexHints> index_hint_list
220 %type <expr> where_expression_opt
221 %type <expr> condition
222 %type <boolVal> boolean_value
223 %type <str> compare
224 %type <ins> insert_data
225 %type <expr> value value_expression num_val
226 %type <expr> function_call_keyword function_call_nonkeyword function_call_generic function_call_conflict
227 %type <str> is_suffix
228 %type <colTuple> col_tuple
229 %type <exprs> expression_list
230 %type <values> tuple_list
231 %type <valTuple> row_tuple tuple_or_empty
232 %type <expr> tuple_expression
233 %type <subquery> subquery
234 %type <colName> column_name
235 %type <whens> when_expression_list
236 %type <when> when_expression
237 %type <expr> expression_opt else_expression_opt
238 %type <exprs> group_by_opt
239 %type <expr> having_opt
240 %type <orderBy> order_by_opt order_list
241 %type <order> order
242 %type <str> asc_desc_opt
243 %type <limit> limit_opt
244 %type <str> lock_opt
245 %type <columns> ins_column_list column_list
246 %type <partitions> opt_partition_clause partition_list
247 %type <updateExprs> on_dup_opt
248 %type <updateExprs> update_list
249 %type <setExprs> set_list transaction_chars
250 %type <bytes> charset_or_character_set
251 %type <updateExpr> update_expression
252 %type <setExpr> set_expression transaction_char isolation_level
253 %type <bytes> for_from
254 %type <str> ignore_opt default_opt
255 %type <str> extended_opt full_opt from_database_opt tables_or_processlist
256 %type <showFilter> like_or_where_opt
257 %type <byt> exists_opt
258 %type <empty> not_exists_opt non_add_drop_or_rename_operation to_opt index_opt constraint_opt
259 %type <bytes> reserved_keyword non_reserved_keyword
260 %type <colIdent> sql_id reserved_sql_id col_alias as_ci_opt using_opt
261 %type <expr> charset_value
262 %type <tableIdent> table_id reserved_table_id table_alias as_opt_id
263 %type <empty> as_opt
264 %type <empty> force_eof ddl_force_eof
265 %type <str> charset
266 %type <str> set_session_or_global show_session_or_global
267 %type <convertType> convert_type
268 %type <columnType> column_type
269 %type <columnType> int_type decimal_type numeric_type time_type char_type spatial_type
270 %type <optVal> length_opt column_default_opt column_comment_opt on_update_opt
271 %type <str> charset_opt collate_opt
272 %type <boolVal> unsigned_opt zero_fill_opt
273 %type <LengthScaleOption> float_length_opt decimal_length_opt
274 %type <boolVal> null_opt auto_increment_opt
275 %type <colKeyOpt> column_key_opt
276 %type <strs> enum_values
277 %type <columnDefinition> column_definition
278 %type <indexDefinition> index_definition
279 %type <str> index_or_key
280 %type <str> equal_opt
281 %type <TableSpec> table_spec table_column_list
282 %type <str> table_option_list table_option table_opt_value
283 %type <indexInfo> index_info
284 %type <indexColumn> index_column
285 %type <indexColumns> index_column_list
286 %type <indexOption> index_option
287 %type <indexOptions> index_option_list
288 %type <partDefs> partition_definitions
289 %type <partDef> partition_definition
290 %type <partSpec> partition_operation
291 %type <vindexParam> vindex_param
292 %type <vindexParams> vindex_param_list vindex_params_opt
293 %type <colIdent> vindex_type vindex_type_opt
294 %type <bytes> alter_object_type
295
296 %start any_command
297
298 %%
299
300 any_command:
301 command semicolon_opt
302 {
303 setParseTree(yylex, $1)
304 }
305
306 semicolon_opt:
307 /*empty*/ {}
308 | ';' {}
309
310 command:
311 select_statement
312 {
313 $$ = $1
314 }
315 | stream_statement
316 | insert_statement
317 | update_statement
318 | delete_statement
319 | set_statement
320 | create_statement
321 | alter_statement
322 | rename_statement
323 | drop_statement
324 | truncate_statement
325 | analyze_statement
326 | show_statement
327 | use_statement
328 | begin_statement
329 | commit_statement
330 | rollback_statement
331 | other_statement
332
333 select_statement:
334 base_select order_by_opt limit_opt lock_opt
335 {
336 sel := $1.(*Select)
337 sel.OrderBy = $2
338 sel.Limit = $3
339 sel.Lock = $4
340 $$ = sel
341 }
342 | union_lhs union_op union_rhs order_by_opt limit_opt lock_opt
343 {
344 $$ = &Union{Type: $2, Left: $1, Right: $3, OrderBy: $4, Limit: $5, Lock: $6}
345 }
346 | SELECT comment_opt cache_opt NEXT num_val for_from table_name
347 {
348 $$ = &Select{Comments: Comments($2), Cache: $3, SelectExprs: SelectExprs{Nextval{Expr: $5}}, From: TableExprs{&AliasedTableExpr{Expr: $7}}}
349 }
350
351 stream_statement:
352 STREAM comment_opt select_expression FROM table_name
353 {
354 $$ = &Stream{Comments: Comments($2), SelectExpr: $3, Table: $5}
355 }
356
357 // base_select is an unparenthesized SELECT with no order by clause or beyond.
358 base_select:
359 SELECT comment_opt cache_opt distinct_opt straight_join_opt select_expression_list from_opt where_expression_opt group_by_opt having_opt
360 {
361 $$ = &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)}
362 }
363
364 union_lhs:
365 select_statement
366 {
367 $$ = $1
368 }
369 | openb select_statement closeb
370 {
371 $$ = &ParenSelect{Select: $2}
372 }
373
374 union_rhs:
375 base_select
376 {
377 $$ = $1
378 }
379 | openb select_statement closeb
380 {
381 $$ = &ParenSelect{Select: $2}
382 }
383
384
385 insert_statement:
386 insert_or_replace comment_opt ignore_opt into_table_name opt_partition_clause insert_data on_dup_opt
387 {
388 // insert_data returns a *Insert pre-filled with Columns & Values
389 ins := $6
390 ins.Action = $1
391 ins.Comments = $2
392 ins.Ignore = $3
393 ins.Table = $4
394 ins.Partitions = $5
395 ins.OnDup = OnDup($7)
396 $$ = ins
397 }
398 | insert_or_replace comment_opt ignore_opt into_table_name opt_partition_clause SET update_list on_dup_opt
399 {
400 cols := make(Columns, 0, len($7))
401 vals := make(ValTuple, 0, len($8))
402 for _, updateList := range $7 {
403 cols = append(cols, updateList.Name.Name)
404 vals = append(vals, updateList.Expr)
405 }
406 $$ = &Insert{Action: $1, Comments: Comments($2), Ignore: $3, Table: $4, Partitions: $5, Columns: cols, Rows: Values{vals}, OnDup: OnDup($8)}
407 }
408
409 insert_or_replace:
410 INSERT
411 {
412 $$ = InsertStr
413 }
414 | REPLACE
415 {
416 $$ = ReplaceStr
417 }
418
419 update_statement:
420 UPDATE comment_opt table_references SET update_list where_expression_opt order_by_opt limit_opt
421 {
422 $$ = &Update{Comments: Comments($2), TableExprs: $3, Exprs: $5, Where: NewWhere(WhereStr, $6), OrderBy: $7, Limit: $8}
423 }
424
425 delete_statement:
426 DELETE comment_opt FROM table_name opt_partition_clause where_expression_opt order_by_opt limit_opt
427 {
428 $$ = &Delete{Comments: Comments($2), TableExprs: TableExprs{&AliasedTableExpr{Expr:$4}}, Partitions: $5, Where: NewWhere(WhereStr, $6), OrderBy: $7, Limit: $8}
429 }
430 | DELETE comment_opt FROM table_name_list USING table_references where_expression_opt
431 {
432 $$ = &Delete{Comments: Comments($2), Targets: $4, TableExprs: $6, Where: NewWhere(WhereStr, $7)}
433 }
434 | DELETE comment_opt table_name_list from_or_using table_references where_expression_opt
435 {
436 $$ = &Delete{Comments: Comments($2), Targets: $3, TableExprs: $5, Where: NewWhere(WhereStr, $6)}
437 }
438
439 from_or_using:
440 FROM {}
441 | USING {}
442
443 table_name_list:
444 table_name
445 {
446 $$ = TableNames{$1}
447 }
448 | table_name_list ',' table_name
449 {
450 $$ = append($$, $3)
451 }
452
453 opt_partition_clause:
454 {
455 $$ = nil
456 }
457 | PARTITION openb partition_list closeb
458 {
459 $$ = $3
460 }
461
462 set_statement:
463 SET comment_opt set_list
464 {
465 $$ = &Set{Comments: Comments($2), Exprs: $3}
466 }
467 | SET comment_opt set_session_or_global set_list
468 {
469 $$ = &Set{Comments: Comments($2), Scope: $3, Exprs: $4}
470 }
471 | SET comment_opt set_session_or_global TRANSACTION transaction_chars
472 {
473 $$ = &Set{Comments: Comments($2), Scope: $3, Exprs: $5}
474 }
475 | SET comment_opt TRANSACTION transaction_chars
476 {
477 $$ = &Set{Comments: Comments($2), Exprs: $4}
478 }
479
480 transaction_chars:
481 transaction_char
482 {
483 $$ = SetExprs{$1}
484 }
485 | transaction_chars ',' transaction_char
486 {
487 $$ = append($$, $3)
488 }
489
490 transaction_char:
491 ISOLATION LEVEL isolation_level
492 {
493 $$ = $3
494 }
495 | READ WRITE
496 {
497 $$ = &SetExpr{Name: NewColIdent("tx_read_only"), Expr: NewIntVal([]byte("0"))}
498 }
499 | READ ONLY
500 {
501 $$ = &SetExpr{Name: NewColIdent("tx_read_only"), Expr: NewIntVal([]byte("1"))}
502 }
503
504 isolation_level:
505 REPEATABLE READ
506 {
507 $$ = &SetExpr{Name: NewColIdent("tx_isolation"), Expr: NewStrVal([]byte("repeatable read"))}
508 }
509 | READ COMMITTED
510 {
511 $$ = &SetExpr{Name: NewColIdent("tx_isolation"), Expr: NewStrVal([]byte("read committed"))}
512 }
513 | READ UNCOMMITTED
514 {
515 $$ = &SetExpr{Name: NewColIdent("tx_isolation"), Expr: NewStrVal([]byte("read uncommitted"))}
516 }
517 | SERIALIZABLE
518 {
519 $$ = &SetExpr{Name: NewColIdent("tx_isolation"), Expr: NewStrVal([]byte("serializable"))}
520 }
521
522 set_session_or_global:
523 SESSION
524 {
525 $$ = SessionStr
526 }
527 | GLOBAL
528 {
529 $$ = GlobalStr
530 }
531
532 create_statement:
533 create_table_prefix table_spec
534 {
535 $1.TableSpec = $2
536 $$ = $1
537 }
538 | CREATE constraint_opt INDEX ID using_opt ON table_name ddl_force_eof
539 {
540 // Change this to an alter statement
541 $$ = &DDL{Action: AlterStr, Table: $7, NewName:$7}
542 }
543 | CREATE VIEW table_name ddl_force_eof
544 {
545 $$ = &DDL{Action: CreateStr, NewName: $3.ToViewName()}
546 }
547 | CREATE OR REPLACE VIEW table_name ddl_force_eof
548 {
549 $$ = &DDL{Action: CreateStr, NewName: $5.ToViewName()}
550 }
551 | CREATE VINDEX sql_id vindex_type_opt vindex_params_opt
552 {
553 $$ = &DDL{Action: CreateVindexStr, VindexSpec: &VindexSpec{
554 Name: $3,
555 Type: $4,
556 Params: $5,
557 }}
558 }
559 | CREATE DATABASE not_exists_opt ID ddl_force_eof
560 {
561 $$ = &DBDDL{Action: CreateStr, DBName: string($4)}
562 }
563 | CREATE SCHEMA not_exists_opt ID ddl_force_eof
564 {
565 $$ = &DBDDL{Action: CreateStr, DBName: string($4)}
566 }
567
568 vindex_type_opt:
569 {
570 $$ = NewColIdent("")
571 }
572 | USING vindex_type
573 {
574 $$ = $2
575 }
576
577 vindex_type:
578 ID
579 {
580 $$ = NewColIdent(string($1))
581 }
582
583 vindex_params_opt:
584 {
585 var v []VindexParam
586 $$ = v
587 }
588 | WITH vindex_param_list
589 {
590 $$ = $2
591 }
592
593 vindex_param_list:
594 vindex_param
595 {
596 $$ = make([]VindexParam, 0, 4)
597 $$ = append($$, $1)
598 }
599 | vindex_param_list ',' vindex_param
600 {
601 $$ = append($$, $3)
602 }
603
604 vindex_param:
605 reserved_sql_id '=' table_opt_value
606 {
607 $$ = VindexParam{Key: $1, Val: $3}
608 }
609
610 create_table_prefix:
611 CREATE TABLE not_exists_opt table_name
612 {
613 $$ = &DDL{Action: CreateStr, NewName: $4}
614 setDDL(yylex, $$)
615 }
616
617 table_spec:
618 '(' table_column_list ')' table_option_list
619 {
620 $$ = $2
621 $$.Options = $4
622 }
623
624 table_column_list:
625 column_definition
626 {
627 $$ = &TableSpec{}
628 $$.AddColumn($1)
629 }
630 | table_column_list ',' column_definition
631 {
632 $$.AddColumn($3)
633 }
634 | table_column_list ',' index_definition
635 {
636 $$.AddIndex($3)
637 }
638
639 column_definition:
640 ID column_type null_opt column_default_opt on_update_opt auto_increment_opt column_key_opt column_comment_opt
641 {
642 $2.NotNull = $3
643 $2.Default = $4
644 $2.OnUpdate = $5
645 $2.Autoincrement = $6
646 $2.KeyOpt = $7
647 $2.Comment = $8
648 $$ = &ColumnDefinition{Name: NewColIdent(string($1)), Type: $2}
649 }
650 column_type:
651 numeric_type unsigned_opt zero_fill_opt
652 {
653 $$ = $1
654 $$.Unsigned = $2
655 $$.Zerofill = $3
656 }
657 | char_type
658 | time_type
659 | spatial_type
660
661 numeric_type:
662 int_type length_opt
663 {
664 $$ = $1
665 $$.Length = $2
666 }
667 | decimal_type
668 {
669 $$ = $1
670 }
671
672 int_type:
673 BIT
674 {
675 $$ = ColumnType{Type: string($1)}
676 }
677 | TINYINT
678 {
679 $$ = ColumnType{Type: string($1)}
680 }
681 | SMALLINT
682 {
683 $$ = ColumnType{Type: string($1)}
684 }
685 | MEDIUMINT
686 {
687 $$ = ColumnType{Type: string($1)}
688 }
689 | INT
690 {
691 $$ = ColumnType{Type: string($1)}
692 }
693 | INTEGER
694 {
695 $$ = ColumnType{Type: string($1)}
696 }
697 | BIGINT
698 {
699 $$ = ColumnType{Type: string($1)}
700 }
701
702 decimal_type:
703 REAL float_length_opt
704 {
705 $$ = ColumnType{Type: string($1)}
706 $$.Length = $2.Length
707 $$.Scale = $2.Scale
708 }
709 | DOUBLE float_length_opt
710 {
711 $$ = ColumnType{Type: string($1)}
712 $$.Length = $2.Length
713 $$.Scale = $2.Scale
714 }
715 | FLOAT_TYPE float_length_opt
716 {
717 $$ = ColumnType{Type: string($1)}
718 $$.Length = $2.Length
719 $$.Scale = $2.Scale
720 }
721 | DECIMAL decimal_length_opt
722 {
723 $$ = ColumnType{Type: string($1)}
724 $$.Length = $2.Length
725 $$.Scale = $2.Scale
726 }
727 | NUMERIC decimal_length_opt
728 {
729 $$ = ColumnType{Type: string($1)}
730 $$.Length = $2.Length
731 $$.Scale = $2.Scale
732 }
733
734 time_type:
735 DATE
736 {
737 $$ = ColumnType{Type: string($1)}
738 }
739 | TIME length_opt
740 {
741 $$ = ColumnType{Type: string($1), Length: $2}
742 }
743 | TIMESTAMP length_opt
744 {
745 $$ = ColumnType{Type: string($1), Length: $2}
746 }
747 | DATETIME length_opt
748 {
749 $$ = ColumnType{Type: string($1), Length: $2}
750 }
751 | YEAR
752 {
753 $$ = ColumnType{Type: string($1)}
754 }
755
756 char_type:
757 CHAR length_opt charset_opt collate_opt
758 {
759 $$ = ColumnType{Type: string($1), Length: $2, Charset: $3, Collate: $4}
760 }
761 | VARCHAR length_opt charset_opt collate_opt
762 {
763 $$ = ColumnType{Type: string($1), Length: $2, Charset: $3, Collate: $4}
764 }
765 | BINARY length_opt
766 {
767 $$ = ColumnType{Type: string($1), Length: $2}
768 }
769 | VARBINARY length_opt
770 {
771 $$ = ColumnType{Type: string($1), Length: $2}
772 }
773 | TEXT charset_opt collate_opt
774 {
775 $$ = ColumnType{Type: string($1), Charset: $2, Collate: $3}
776 }
777 | TINYTEXT charset_opt collate_opt
778 {
779 $$ = ColumnType{Type: string($1), Charset: $2, Collate: $3}
780 }
781 | MEDIUMTEXT charset_opt collate_opt
782 {
783 $$ = ColumnType{Type: string($1), Charset: $2, Collate: $3}
784 }
785 | LONGTEXT charset_opt collate_opt
786 {
787 $$ = ColumnType{Type: string($1), Charset: $2, Collate: $3}
788 }
789 | BLOB
790 {
791 $$ = ColumnType{Type: string($1)}
792 }
793 | TINYBLOB
794 {
795 $$ = ColumnType{Type: string($1)}
796 }
797 | MEDIUMBLOB
798 {
799 $$ = ColumnType{Type: string($1)}
800 }
801 | LONGBLOB
802 {
803 $$ = ColumnType{Type: string($1)}
804 }
805 | JSON
806 {
807 $$ = ColumnType{Type: string($1)}
808 }
809 | ENUM '(' enum_values ')' charset_opt collate_opt
810 {
811 $$ = ColumnType{Type: string($1), EnumValues: $3, Charset: $5, Collate: $6}
812 }
813 // need set_values / SetValues ?
814 | SET '(' enum_values ')' charset_opt collate_opt
815 {
816 $$ = ColumnType{Type: string($1), EnumValues: $3, Charset: $5, Collate: $6}
817 }
818
819 spatial_type:
820 GEOMETRY
821 {
822 $$ = ColumnType{Type: string($1)}
823 }
824 | POINT
825 {
826 $$ = ColumnType{Type: string($1)}
827 }
828 | LINESTRING
829 {
830 $$ = ColumnType{Type: string($1)}
831 }
832 | POLYGON
833 {
834 $$ = ColumnType{Type: string($1)}
835 }
836 | GEOMETRYCOLLECTION
837 {
838 $$ = ColumnType{Type: string($1)}
839 }
840 | MULTIPOINT
841 {
842 $$ = ColumnType{Type: string($1)}
843 }
844 | MULTILINESTRING
845 {
846 $$ = ColumnType{Type: string($1)}
847 }
848 | MULTIPOLYGON
849 {
850 $$ = ColumnType{Type: string($1)}
851 }
852
853 enum_values:
854 STRING
855 {
856 $$ = make([]string, 0, 4)
857 $$ = append($$, "'" + string($1) + "'")
858 }
859 | enum_values ',' STRING
860 {
861 $$ = append($1, "'" + string($3) + "'")
862 }
863
864 length_opt:
865 {
866 $$ = nil
867 }
868 | '(' INTEGRAL ')'
869 {
870 $$ = NewIntVal($2)
871 }
872
873 float_length_opt:
874 {
875 $$ = LengthScaleOption{}
876 }
877 | '(' INTEGRAL ',' INTEGRAL ')'
878 {
879 $$ = LengthScaleOption{
880 Length: NewIntVal($2),
881 Scale: NewIntVal($4),
882 }
883 }
884
885 decimal_length_opt:
886 {
887 $$ = LengthScaleOption{}
888 }
889 | '(' INTEGRAL ')'
890 {
891 $$ = LengthScaleOption{
892 Length: NewIntVal($2),
893 }
894 }
895 | '(' INTEGRAL ',' INTEGRAL ')'
896 {
897 $$ = LengthScaleOption{
898 Length: NewIntVal($2),
899 Scale: NewIntVal($4),
900 }
901 }
902
903 unsigned_opt:
904 {
905 $$ = BoolVal(false)
906 }
907 | UNSIGNED
908 {
909 $$ = BoolVal(true)
910 }
911
912 zero_fill_opt:
913 {
914 $$ = BoolVal(false)
915 }
916 | ZEROFILL
917 {
918 $$ = BoolVal(true)
919 }
920
921 // Null opt returns false to mean NULL (i.e. the default) and true for NOT NULL
922 null_opt:
923 {
924 $$ = BoolVal(false)
925 }
926 | NULL
927 {
928 $$ = BoolVal(false)
929 }
930 | NOT NULL
931 {
932 $$ = BoolVal(true)
933 }
934
935 column_default_opt:
936 {
937 $$ = nil
938 }
939 | DEFAULT STRING
940 {
941 $$ = NewStrVal($2)
942 }
943 | DEFAULT INTEGRAL
944 {
945 $$ = NewIntVal($2)
946 }
947 | DEFAULT FLOAT
948 {
949 $$ = NewFloatVal($2)
950 }
951 | DEFAULT NULL
952 {
953 $$ = NewValArg($2)
954 }
955 | DEFAULT CURRENT_TIMESTAMP
956 {
957 $$ = NewValArg($2)
958 }
959 | DEFAULT BIT_LITERAL
960 {
961 $$ = NewBitVal($2)
962 }
963
964 on_update_opt:
965 {
966 $$ = nil
967 }
968 | ON UPDATE CURRENT_TIMESTAMP
969 {
970 $$ = NewValArg($3)
971 }
972
973 auto_increment_opt:
974 {
975 $$ = BoolVal(false)
976 }
977 | AUTO_INCREMENT
978 {
979 $$ = BoolVal(true)
980 }
981
982 charset_opt:
983 {
984 $$ = ""
985 }
986 | CHARACTER SET ID
987 {
988 $$ = string($3)
989 }
990 | CHARACTER SET BINARY
991 {
992 $$ = string($3)
993 }
994
995 collate_opt:
996 {
997 $$ = ""
998 }
999 | COLLATE ID
1000 {
1001 $$ = string($2)
1002 }
1003
1004 column_key_opt:
1005 {
1006 $$ = colKeyNone
1007 }
1008 | PRIMARY KEY
1009 {
1010 $$ = colKeyPrimary
1011 }
1012 | KEY
1013 {
1014 $$ = colKey
1015 }
1016 | UNIQUE KEY
1017 {
1018 $$ = colKeyUniqueKey
1019 }
1020 | UNIQUE
1021 {
1022 $$ = colKeyUnique
1023 }
1024
1025 column_comment_opt:
1026 {
1027 $$ = nil
1028 }
1029 | COMMENT_KEYWORD STRING
1030 {
1031 $$ = NewStrVal($2)
1032 }
1033
1034 index_definition:
1035 index_info '(' index_column_list ')' index_option_list
1036 {
1037 $$ = &IndexDefinition{Info: $1, Columns: $3, Options: $5}
1038 }
1039 | index_info '(' index_column_list ')'
1040 {
1041 $$ = &IndexDefinition{Info: $1, Columns: $3}
1042 }
1043
1044 index_option_list:
1045 index_option
1046 {
1047 $$ = []*IndexOption{$1}
1048 }
1049 | index_option_list index_option
1050 {
1051 $$ = append($$, $2)
1052 }
1053
1054 index_option:
1055 USING ID
1056 {
1057 $$ = &IndexOption{Name: string($1), Using: string($2)}
1058 }
1059 | KEY_BLOCK_SIZE equal_opt INTEGRAL
1060 {
1061 // should not be string
1062 $$ = &IndexOption{Name: string($1), Value: NewIntVal($3)}
1063 }
1064 | COMMENT_KEYWORD STRING
1065 {
1066 $$ = &IndexOption{Name: string($1), Value: NewStrVal($2)}
1067 }
1068
1069 equal_opt:
1070 /* empty */
1071 {
1072 $$ = ""
1073 }
1074 | '='
1075 {
1076 $$ = string($1)
1077 }
1078
1079 index_info:
1080 PRIMARY KEY
1081 {
1082 $$ = &IndexInfo{Type: string($1) + " " + string($2), Name: NewColIdent("PRIMARY"), Primary: true, Unique: true}
1083 }
1084 | SPATIAL index_or_key ID
1085 {
1086 $$ = &IndexInfo{Type: string($1) + " " + string($2), Name: NewColIdent(string($3)), Spatial: true, Unique: false}
1087 }
1088 | UNIQUE index_or_key ID
1089 {
1090 $$ = &IndexInfo{Type: string($1) + " " + string($2), Name: NewColIdent(string($3)), Unique: true}
1091 }
1092 | UNIQUE ID
1093 {
1094 $$ = &IndexInfo{Type: string($1), Name: NewColIdent(string($2)), Unique: true}
1095 }
1096 | index_or_key ID
1097 {
1098 $$ = &IndexInfo{Type: string($1), Name: NewColIdent(string($2)), Unique: false}
1099 }
1100
1101 index_or_key:
1102 INDEX
1103 {
1104 $$ = string($1)
1105 }
1106 | KEY
1107 {
1108 $$ = string($1)
1109 }
1110
1111 index_column_list:
1112 index_column
1113 {
1114 $$ = []*IndexColumn{$1}
1115 }
1116 | index_column_list ',' index_column
1117 {
1118 $$ = append($$, $3)
1119 }
1120
1121 index_column:
1122 sql_id length_opt
1123 {
1124 $$ = &IndexColumn{Column: $1, Length: $2}
1125 }
1126
1127 table_option_list:
1128 {
1129 $$ = ""
1130 }
1131 | table_option
1132 {
1133 $$ = " " + string($1)
1134 }
1135 | table_option_list ',' table_option
1136 {
1137 $$ = string($1) + ", " + string($3)
1138 }
1139
1140 // rather than explicitly parsing the various keywords for table options,
1141 // just accept any number of keywords, IDs, strings, numbers, and '='
1142 table_option:
1143 table_opt_value
1144 {
1145 $$ = $1
1146 }
1147 | table_option table_opt_value
1148 {
1149 $$ = $1 + " " + $2
1150 }
1151 | table_option '=' table_opt_value
1152 {
1153 $$ = $1 + "=" + $3
1154 }
1155
1156 table_opt_value:
1157 reserved_sql_id
1158 {
1159 $$ = $1.String()
1160 }
1161 | STRING
1162 {
1163 $$ = "'" + string($1) + "'"
1164 }
1165 | INTEGRAL
1166 {
1167 $$ = string($1)
1168 }
1169
1170 alter_statement:
1171 ALTER ignore_opt TABLE table_name non_add_drop_or_rename_operation force_eof
1172 {
1173 $$ = &DDL{Action: AlterStr, Table: $4, NewName: $4}
1174 }
1175 | ALTER ignore_opt TABLE table_name ADD alter_object_type force_eof
1176 {
1177 $$ = &DDL{Action: AlterStr, Table: $4, NewName: $4}
1178 }
1179 | ALTER ignore_opt TABLE table_name DROP alter_object_type force_eof
1180 {
1181 $$ = &DDL{Action: AlterStr, Table: $4, NewName: $4}
1182 }
1183 | ALTER ignore_opt TABLE table_name ADD VINDEX sql_id '(' column_list ')' vindex_type_opt vindex_params_opt
1184 {
1185 $$ = &DDL{
1186 Action: AddColVindexStr,
1187 Table: $4,
1188 VindexSpec: &VindexSpec{
1189 Name: $7,
1190 Type: $11,
1191 Params: $12,
1192 },
1193 VindexCols: $9,
1194 }
1195 }
1196 | ALTER ignore_opt TABLE table_name DROP VINDEX sql_id
1197 {
1198 $$ = &DDL{
1199 Action: DropColVindexStr,
1200 Table: $4,
1201 VindexSpec: &VindexSpec{
1202 Name: $7,
1203 },
1204 }
1205 }
1206 | ALTER ignore_opt TABLE table_name RENAME to_opt table_name
1207 {
1208 // Change this to a rename statement
1209 $$ = &DDL{Action: RenameStr, Table: $4, NewName: $7}
1210 }
1211 | ALTER ignore_opt TABLE table_name RENAME index_opt force_eof
1212 {
1213 // Rename an index can just be an alter
1214 $$ = &DDL{Action: AlterStr, Table: $4, NewName: $4}
1215 }
1216 | ALTER VIEW table_name ddl_force_eof
1217 {
1218 $$ = &DDL{Action: AlterStr, Table: $3.ToViewName(), NewName: $3.ToViewName()}
1219 }
1220 | ALTER ignore_opt TABLE table_name partition_operation
1221 {
1222 $$ = &DDL{Action: AlterStr, Table: $4, PartitionSpec: $5}
1223 }
1224
1225 alter_object_type:
1226 COLUMN
1227 | CONSTRAINT
1228 | FOREIGN
1229 | FULLTEXT
1230 | ID
1231 | INDEX
1232 | KEY
1233 | PRIMARY
1234 | SPATIAL
1235 | PARTITION
1236 | UNIQUE
1237
1238 partition_operation:
1239 REORGANIZE PARTITION sql_id INTO openb partition_definitions closeb
1240 {
1241 $$ = &PartitionSpec{Action: ReorganizeStr, Name: $3, Definitions: $6}
1242 }
1243
1244 partition_definitions:
1245 partition_definition
1246 {
1247 $$ = []*PartitionDefinition{$1}
1248 }
1249 | partition_definitions ',' partition_definition
1250 {
1251 $$ = append($1, $3)
1252 }
1253
1254 partition_definition:
1255 PARTITION sql_id VALUES LESS THAN openb value_expression closeb
1256 {
1257 $$ = &PartitionDefinition{Name: $2, Limit: $7}
1258 }
1259 | PARTITION sql_id VALUES LESS THAN openb MAXVALUE closeb
1260 {
1261 $$ = &PartitionDefinition{Name: $2, Maxvalue: true}
1262 }
1263
1264 rename_statement:
1265 RENAME TABLE table_name TO table_name
1266 {
1267 $$ = &DDL{Action: RenameStr, Table: $3, NewName: $5}
1268 }
1269
1270 drop_statement:
1271 DROP TABLE exists_opt table_name
1272 {
1273 var exists bool
1274 if $3 != 0 {
1275 exists = true
1276 }
1277 $$ = &DDL{Action: DropStr, Table: $4, IfExists: exists}
1278 }
1279 | DROP INDEX ID ON table_name ddl_force_eof
1280 {
1281 // Change this to an alter statement
1282 $$ = &DDL{Action: AlterStr, Table: $5, NewName: $5}
1283 }
1284 | DROP VIEW exists_opt table_name ddl_force_eof
1285 {
1286 var exists bool
1287 if $3 != 0 {
1288 exists = true
1289 }
1290 $$ = &DDL{Action: DropStr, Table: $4.ToViewName(), IfExists: exists}
1291 }
1292 | DROP DATABASE exists_opt ID
1293 {
1294 $$ = &DBDDL{Action: DropStr, DBName: string($4)}
1295 }
1296 | DROP SCHEMA exists_opt ID
1297 {
1298 $$ = &DBDDL{Action: DropStr, DBName: string($4)}
1299 }
1300
1301 truncate_statement:
1302 TRUNCATE TABLE table_name
1303 {
1304 $$ = &DDL{Action: TruncateStr, Table: $3}
1305 }
1306 | TRUNCATE table_name
1307 {
1308 $$ = &DDL{Action: TruncateStr, Table: $2}
1309 }
1310 analyze_statement:
1311 ANALYZE TABLE table_name
1312 {
1313 $$ = &DDL{Action: AlterStr, Table: $3, NewName: $3}
1314 }
1315
1316 show_statement:
1317 SHOW BINARY ID ddl_force_eof /* SHOW BINARY LOGS */
1318 {
1319 $$ = &Show{Type: string($2) + " " + string($3)}
1320 }
1321 | SHOW CHARACTER SET ddl_force_eof
1322 {
1323 $$ = &Show{Type: string($2) + " " + string($3)}
1324 }
1325 | SHOW CREATE DATABASE ddl_force_eof
1326 {
1327 $$ = &Show{Type: string($2) + " " + string($3)}
1328 }
1329 /* Rule to handle SHOW CREATE EVENT, SHOW CREATE FUNCTION, etc. */
1330 | SHOW CREATE ID ddl_force_eof
1331 {
1332 $$ = &Show{Type: string($2) + " " + string($3)}
1333 }
1334 | SHOW CREATE PROCEDURE ddl_force_eof
1335 {
1336 $$ = &Show{Type: string($2) + " " + string($3)}
1337 }
1338 | SHOW CREATE TABLE ddl_force_eof
1339 {
1340 $$ = &Show{Type: string($2) + " " + string($3)}
1341 }
1342 | SHOW CREATE TRIGGER ddl_force_eof
1343 {
1344 $$ = &Show{Type: string($2) + " " + string($3)}
1345 }
1346 | SHOW CREATE VIEW ddl_force_eof
1347 {
1348 $$ = &Show{Type: string($2) + " " + string($3)}
1349 }
1350 | SHOW DATABASES ddl_force_eof
1351 {
1352 $$ = &Show{Type: string($2)}
1353 }
1354 | SHOW INDEX ddl_force_eof
1355 {
1356 $$ = &Show{Type: string($2)}
1357 }
1358 | SHOW KEYS ddl_force_eof
1359 {
1360 $$ = &Show{Type: string($2)}
1361 }
1362 | SHOW PROCEDURE ddl_force_eof
1363 {
1364 $$ = &Show{Type: string($2)}
1365 }
1366 | SHOW show_session_or_global STATUS ddl_force_eof
1367 {
1368 $$ = &Show{Scope: $2, Type: string($3)}
1369 }
1370 | SHOW TABLE ddl_force_eof
1371 {
1372 $$ = &Show{Type: string($2)}
1373 }
1374 | SHOW extended_opt full_opt tables_or_processlist from_database_opt like_or_where_opt
1375 {
1376 // this is ugly, but I couldn't find a better way for now
1377 if $4 == "processlist" {
1378 $$ = &Show{Type: $4}
1379 } else {
1380 showTablesOpt := &ShowTablesOpt{Extended: $2, Full:$3, DbName:$5, Filter:$6}
1381 $$ = &Show{Type: $4, ShowTablesOpt: showTablesOpt}
1382 }
1383 }
1384 | SHOW show_session_or_global VARIABLES ddl_force_eof
1385 {
1386 $$ = &Show{Scope: $2, Type: string($3)}
1387 }
1388 | SHOW VINDEXES
1389 {
1390 $$ = &Show{Type: string($2)}
1391 }
1392 | SHOW VINDEXES ON table_name
1393 {
1394 $$ = &Show{Type: string($2), OnTable: $4}
1395 }
1396 | SHOW VITESS_KEYSPACES
1397 {
1398 $$ = &Show{Type: string($2)}
1399 }
1400 | SHOW VITESS_SHARDS
1401 {
1402 $$ = &Show{Type: string($2)}
1403 }
1404 | SHOW VITESS_TABLETS
1405 {
1406 $$ = &Show{Type: string($2)}
1407 }
1408 | SHOW VSCHEMA_TABLES
1409 {
1410 $$ = &Show{Type: string($2)}
1411 }
1412 /*
1413 * Catch-all for show statements without vitess keywords:
1414 *
1415 * SHOW BINARY LOGS
1416 * SHOW INVALID
1417 */
1418 | SHOW ID ddl_force_eof
1419 {
1420 $$ = &Show{Type: string($2)}
1421 }
1422
1423 tables_or_processlist:
1424 TABLES
1425 {
1426 $$ = string($1)
1427 }
1428 | PROCESSLIST
1429 {
1430 $$ = string($1)
1431 }
1432
1433 extended_opt:
1434 /* empty */
1435 {
1436 $$ = ""
1437 }
1438 | EXTENDED
1439 {
1440 $$ = "extended "
1441 }
1442
1443 full_opt:
1444 /* empty */
1445 {
1446 $$ = ""
1447 }
1448 | FULL
1449 {
1450 $$ = "full "
1451 }
1452
1453 from_database_opt:
1454 /* empty */
1455 {
1456 $$ = ""
1457 }
1458 | FROM table_id
1459 {
1460 $$ = $2.v
1461 }
1462 | IN table_id
1463 {
1464 $$ = $2.v
1465 }
1466
1467 like_or_where_opt:
1468 /* empty */
1469 {
1470 $$ = nil
1471 }
1472 | LIKE STRING
1473 {
1474 $$ = &ShowFilter{Like:string($2)}
1475 }
1476 | WHERE expression
1477 {
1478 $$ = &ShowFilter{Filter:$2}
1479 }
1480
1481 show_session_or_global:
1482 /* empty */
1483 {
1484 $$ = ""
1485 }
1486 | SESSION
1487 {
1488 $$ = SessionStr
1489 }
1490 | GLOBAL
1491 {
1492 $$ = GlobalStr
1493 }
1494
1495 use_statement:
1496 USE table_id
1497 {
1498 $$ = &Use{DBName: $2}
1499 }
1500 | USE
1501 {
1502 $$ = &Use{DBName:TableIdent{v:""}}
1503 }
1504
1505 begin_statement:
1506 BEGIN
1507 {
1508 $$ = &Begin{}
1509 }
1510 | START TRANSACTION
1511 {
1512 $$ = &Begin{}
1513 }
1514
1515 commit_statement:
1516 COMMIT
1517 {
1518 $$ = &Commit{}
1519 }
1520
1521 rollback_statement:
1522 ROLLBACK
1523 {
1524 $$ = &Rollback{}
1525 }
1526
1527 other_statement:
1528 DESC force_eof
1529 {
1530 $$ = &OtherRead{}
1531 }
1532 | DESCRIBE force_eof
1533 {
1534 $$ = &OtherRead{}
1535 }
1536 | EXPLAIN force_eof
1537 {
1538 $$ = &OtherRead{}
1539 }
1540 | REPAIR force_eof
1541 {
1542 $$ = &OtherAdmin{}
1543 }
1544 | OPTIMIZE force_eof
1545 {
1546 $$ = &OtherAdmin{}
1547 }
1548
1549 comment_opt:
1550 {
1551 setAllowComments(yylex, true)
1552 }
1553 comment_list
1554 {
1555 $$ = $2
1556 setAllowComments(yylex, false)
1557 }
1558
1559 comment_list:
1560 {
1561 $$ = nil
1562 }
1563 | comment_list COMMENT
1564 {
1565 $$ = append($1, $2)
1566 }
1567
1568 union_op:
1569 UNION
1570 {
1571 $$ = UnionStr
1572 }
1573 | UNION ALL
1574 {
1575 $$ = UnionAllStr
1576 }
1577 | UNION DISTINCT
1578 {
1579 $$ = UnionDistinctStr
1580 }
1581
1582 cache_opt:
1583 {
1584 $$ = ""
1585 }
1586 | SQL_NO_CACHE
1587 {
1588 $$ = SQLNoCacheStr
1589 }
1590 | SQL_CACHE
1591 {
1592 $$ = SQLCacheStr
1593 }
1594
1595 distinct_opt:
1596 {
1597 $$ = ""
1598 }
1599 | DISTINCT
1600 {
1601 $$ = DistinctStr
1602 }
1603
1604 straight_join_opt:
1605 {
1606 $$ = ""
1607 }
1608 | STRAIGHT_JOIN
1609 {
1610 $$ = StraightJoinHint
1611 }
1612
1613 select_expression_list_opt:
1614 {
1615 $$ = nil
1616 }
1617 | select_expression_list
1618 {
1619 $$ = $1
1620 }
1621
1622 select_expression_list:
1623 select_expression
1624 {
1625 $$ = SelectExprs{$1}
1626 }
1627 | select_expression_list ',' select_expression
1628 {
1629 $$ = append($$, $3)
1630 }
1631
1632 select_expression:
1633 '*'
1634 {
1635 $$ = &StarExpr{}
1636 }
1637 | expression as_ci_opt
1638 {
1639 $$ = &AliasedExpr{Expr: $1, As: $2}
1640 }
1641 | table_id '.' '*'
1642 {
1643 $$ = &StarExpr{TableName: TableName{Name: $1}}
1644 }
1645 | table_id '.' reserved_table_id '.' '*'
1646 {
1647 $$ = &StarExpr{TableName: TableName{Qualifier: $1, Name: $3}}
1648 }
1649
1650 as_ci_opt:
1651 {
1652 $$ = ColIdent{}
1653 }
1654 | col_alias
1655 {
1656 $$ = $1
1657 }
1658 | AS col_alias
1659 {
1660 $$ = $2
1661 }
1662
1663 col_alias:
1664 sql_id
1665 | STRING
1666 {
1667 $$ = NewColIdent(string($1))
1668 }
1669
1670 from_opt:
1671 {
1672 $$ = TableExprs{&AliasedTableExpr{Expr:TableName{Name: NewTableIdent("dual")}}}
1673 }
1674 | FROM table_references
1675 {
1676 $$ = $2
1677 }
1678
1679 table_references:
1680 table_reference
1681 {
1682 $$ = TableExprs{$1}
1683 }
1684 | table_references ',' table_reference
1685 {
1686 $$ = append($$, $3)
1687 }
1688
1689 table_reference:
1690 table_factor
1691 | join_table
1692
1693 table_factor:
1694 aliased_table_name
1695 {
1696 $$ = $1
1697 }
1698 | subquery as_opt table_id
1699 {
1700 $$ = &AliasedTableExpr{Expr:$1, As: $3}
1701 }
1702 | openb table_references closeb
1703 {
1704 $$ = &ParenTableExpr{Exprs: $2}
1705 }
1706
1707 aliased_table_name:
1708 table_name as_opt_id index_hint_list
1709 {
1710 $$ = &AliasedTableExpr{Expr:$1, As: $2, Hints: $3}
1711 }
1712 | table_name PARTITION openb partition_list closeb as_opt_id index_hint_list
1713 {
1714 $$ = &AliasedTableExpr{Expr:$1, Partitions: $4, As: $6, Hints: $7}
1715 }
1716
1717 column_list:
1718 sql_id
1719 {
1720 $$ = Columns{$1}
1721 }
1722 | column_list ',' sql_id
1723 {
1724 $$ = append($$, $3)
1725 }
1726
1727 partition_list:
1728 sql_id
1729 {
1730 $$ = Partitions{$1}
1731 }
1732 | partition_list ',' sql_id
1733 {
1734 $$ = append($$, $3)
1735 }
1736
1737 // There is a grammar conflict here:
1738 // 1: INSERT INTO a SELECT * FROM b JOIN c ON b.i = c.i
1739 // 2: INSERT INTO a SELECT * FROM b JOIN c ON DUPLICATE KEY UPDATE a.i = 1
1740 // When yacc encounters the ON clause, it cannot determine which way to
1741 // resolve. The %prec override below makes the parser choose the
1742 // first construct, which automatically makes the second construct a
1743 // syntax error. This is the same behavior as MySQL.
1744 join_table:
1745 table_reference inner_join table_factor join_condition_opt
1746 {
1747 $$ = &JoinTableExpr{LeftExpr: $1, Join: $2, RightExpr: $3, Condition: $4}
1748 }
1749 | table_reference straight_join table_factor on_expression_opt
1750 {
1751 $$ = &JoinTableExpr{LeftExpr: $1, Join: $2, RightExpr: $3, Condition: $4}
1752 }
1753 | table_reference outer_join table_reference join_condition
1754 {
1755 $$ = &JoinTableExpr{LeftExpr: $1, Join: $2, RightExpr: $3, Condition: $4}
1756 }
1757 | table_reference natural_join table_factor
1758 {
1759 $$ = &JoinTableExpr{LeftExpr: $1, Join: $2, RightExpr: $3}
1760 }
1761
1762 join_condition:
1763 ON expression
1764 { $$ = JoinCondition{On: $2} }
1765 | USING '(' column_list ')'
1766 { $$ = JoinCondition{Using: $3} }
1767
1768 join_condition_opt:
1769 %prec JOIN
1770 { $$ = JoinCondition{} }
1771 | join_condition
1772 { $$ = $1 }
1773
1774 on_expression_opt:
1775 %prec JOIN
1776 { $$ = JoinCondition{} }
1777 | ON expression
1778 { $$ = JoinCondition{On: $2} }
1779
1780 as_opt:
1781 { $$ = struct{}{} }
1782 | AS
1783 { $$ = struct{}{} }
1784
1785 as_opt_id:
1786 {
1787 $$ = NewTableIdent("")
1788 }
1789 | table_alias
1790 {
1791 $$ = $1
1792 }
1793 | AS table_alias
1794 {
1795 $$ = $2
1796 }
1797
1798 table_alias:
1799 table_id
1800 | STRING
1801 {
1802 $$ = NewTableIdent(string($1))
1803 }
1804
1805 inner_join:
1806 JOIN
1807 {
1808 $$ = JoinStr
1809 }
1810 | INNER JOIN
1811 {
1812 $$ = JoinStr
1813 }
1814 | CROSS JOIN
1815 {
1816 $$ = JoinStr
1817 }
1818
1819 straight_join:
1820 STRAIGHT_JOIN
1821 {
1822 $$ = StraightJoinStr
1823 }
1824
1825 outer_join:
1826 LEFT JOIN
1827 {
1828 $$ = LeftJoinStr
1829 }
1830 | LEFT OUTER JOIN
1831 {
1832 $$ = LeftJoinStr
1833 }
1834 | RIGHT JOIN
1835 {
1836 $$ = RightJoinStr
1837 }
1838 | RIGHT OUTER JOIN
1839 {
1840 $$ = RightJoinStr
1841 }
1842
1843 natural_join:
1844 NATURAL JOIN
1845 {
1846 $$ = NaturalJoinStr
1847 }
1848 | NATURAL outer_join
1849 {
1850 if $2 == LeftJoinStr {
1851 $$ = NaturalLeftJoinStr
1852 } else {
1853 $$ = NaturalRightJoinStr
1854 }
1855 }
1856
1857 into_table_name:
1858 INTO table_name
1859 {
1860 $$ = $2
1861 }
1862 | table_name
1863 {
1864 $$ = $1
1865 }
1866
1867 table_name:
1868 table_id
1869 {
1870 $$ = TableName{Name: $1}
1871 }
1872 | table_id '.' reserved_table_id
1873 {
1874 $$ = TableName{Qualifier: $1, Name: $3}
1875 }
1876
1877 index_hint_list:
1878 {
1879 $$ = nil
1880 }
1881 | USE INDEX openb column_list closeb
1882 {
1883 $$ = &IndexHints{Type: UseStr, Indexes: $4}
1884 }
1885 | IGNORE INDEX openb column_list closeb
1886 {
1887 $$ = &IndexHints{Type: IgnoreStr, Indexes: $4}
1888 }
1889 | FORCE INDEX openb column_list closeb
1890 {
1891 $$ = &IndexHints{Type: ForceStr, Indexes: $4}
1892 }
1893
1894 where_expression_opt:
1895 {
1896 $$ = nil
1897 }
1898 | WHERE expression
1899 {
1900 $$ = $2
1901 }
1902
1903 expression:
1904 condition
1905 {
1906 $$ = $1
1907 }
1908 | expression AND expression
1909 {
1910 $$ = &AndExpr{Left: $1, Right: $3}
1911 }
1912 | expression OR expression
1913 {
1914 $$ = &OrExpr{Left: $1, Right: $3}
1915 }
1916 | NOT expression
1917 {
1918 $$ = &NotExpr{Expr: $2}
1919 }
1920 | expression IS is_suffix
1921 {
1922 $$ = &IsExpr{Operator: $3, Expr: $1}
1923 }
1924 | value_expression
1925 {
1926 $$ = $1
1927 }
1928 | DEFAULT default_opt
1929 {
1930 $$ = &Default{ColName: $2}
1931 }
1932
1933 default_opt:
1934 /* empty */
1935 {
1936 $$ = ""
1937 }
1938 | openb ID closeb
1939 {
1940 $$ = string($2)
1941 }
1942
1943 boolean_value:
1944 TRUE
1945 {
1946 $$ = BoolVal(true)
1947 }
1948 | FALSE
1949 {
1950 $$ = BoolVal(false)
1951 }
1952
1953 condition:
1954 value_expression compare value_expression
1955 {
1956 $$ = &ComparisonExpr{Left: $1, Operator: $2, Right: $3}
1957 }
1958 | value_expression IN col_tuple
1959 {
1960 $$ = &ComparisonExpr{Left: $1, Operator: InStr, Right: $3}
1961 }
1962 | value_expression NOT IN col_tuple
1963 {
1964 $$ = &ComparisonExpr{Left: $1, Operator: NotInStr, Right: $4}
1965 }
1966 | value_expression LIKE value_expression like_escape_opt
1967 {
1968 $$ = &ComparisonExpr{Left: $1, Operator: LikeStr, Right: $3, Escape: $4}
1969 }
1970 | value_expression NOT LIKE value_expression like_escape_opt
1971 {
1972 $$ = &ComparisonExpr{Left: $1, Operator: NotLikeStr, Right: $4, Escape: $5}
1973 }
1974 | value_expression REGEXP value_expression
1975 {
1976 $$ = &ComparisonExpr{Left: $1, Operator: RegexpStr, Right: $3}
1977 }
1978 | value_expression NOT REGEXP value_expression
1979 {
1980 $$ = &ComparisonExpr{Left: $1, Operator: NotRegexpStr, Right: $4}
1981 }
1982 | value_expression BETWEEN value_expression AND value_expression
1983 {
1984 $$ = &RangeCond{Left: $1, Operator: BetweenStr, From: $3, To: $5}
1985 }
1986 | value_expression NOT BETWEEN value_expression AND value_expression
1987 {
1988 $$ = &RangeCond{Left: $1, Operator: NotBetweenStr, From: $4, To: $6}
1989 }
1990 | EXISTS subquery
1991 {
1992 $$ = &ExistsExpr{Subquery: $2}
1993 }
1994
1995 is_suffix:
1996 NULL
1997 {
1998 $$ = IsNullStr
1999 }
2000 | NOT NULL
2001 {
2002 $$ = IsNotNullStr
2003 }
2004 | TRUE
2005 {
2006 $$ = IsTrueStr
2007 }
2008 | NOT TRUE
2009 {
2010 $$ = IsNotTrueStr
2011 }
2012 | FALSE
2013 {
2014 $$ = IsFalseStr
2015 }
2016 | NOT FALSE
2017 {
2018 $$ = IsNotFalseStr
2019 }
2020
2021 compare:
2022 '='
2023 {
2024 $$ = EqualStr
2025 }
2026 | '<'
2027 {
2028 $$ = LessThanStr
2029 }
2030 | '>'
2031 {
2032 $$ = GreaterThanStr
2033 }
2034 | LE
2035 {
2036 $$ = LessEqualStr
2037 }
2038 | GE
2039 {
2040 $$ = GreaterEqualStr
2041 }
2042 | NE
2043 {
2044 $$ = NotEqualStr
2045 }
2046 | NULL_SAFE_EQUAL
2047 {
2048 $$ = NullSafeEqualStr
2049 }
2050
2051 like_escape_opt:
2052 {
2053 $$ = nil
2054 }
2055 | ESCAPE value_expression
2056 {
2057 $$ = $2
2058 }
2059
2060 col_tuple:
2061 row_tuple
2062 {
2063 $$ = $1
2064 }
2065 | subquery
2066 {
2067 $$ = $1
2068 }
2069 | LIST_ARG
2070 {
2071 $$ = ListArg($1)
2072 }
2073
2074 subquery:
2075 openb select_statement closeb
2076 {
2077 $$ = &Subquery{$2}
2078 }
2079
2080 expression_list:
2081 expression
2082 {
2083 $$ = Exprs{$1}
2084 }
2085 | expression_list ',' expression
2086 {
2087 $$ = append($1, $3)
2088 }
2089
2090 value_expression:
2091 value
2092 {
2093 $$ = $1
2094 }
2095 | boolean_value
2096 {
2097 $$ = $1
2098 }
2099 | column_name
2100 {
2101 $$ = $1
2102 }
2103 | tuple_expression
2104 {
2105 $$ = $1
2106 }
2107 | subquery
2108 {
2109 $$ = $1
2110 }
2111 | value_expression '&' value_expression
2112 {
2113 $$ = &BinaryExpr{Left: $1, Operator: BitAndStr, Right: $3}
2114 }
2115 | value_expression '|' value_expression
2116 {
2117 $$ = &BinaryExpr{Left: $1, Operator: BitOrStr, Right: $3}
2118 }
2119 | value_expression '^' value_expression
2120 {
2121 $$ = &BinaryExpr{Left: $1, Operator: BitXorStr, Right: $3}
2122 }
2123 | value_expression '+' value_expression
2124 {
2125 $$ = &BinaryExpr{Left: $1, Operator: PlusStr, Right: $3}
2126 }
2127 | value_expression '-' value_expression
2128 {
2129 $$ = &BinaryExpr{Left: $1, Operator: MinusStr, Right: $3}
2130 }
2131 | value_expression '*' value_expression
2132 {
2133 $$ = &BinaryExpr{Left: $1, Operator: MultStr, Right: $3}
2134 }
2135 | value_expression '/' value_expression
2136 {
2137 $$ = &BinaryExpr{Left: $1, Operator: DivStr, Right: $3}
2138 }
2139 | value_expression DIV value_expression
2140 {
2141 $$ = &BinaryExpr{Left: $1, Operator: IntDivStr, Right: $3}
2142 }
2143 | value_expression '%' value_expression
2144 {
2145 $$ = &BinaryExpr{Left: $1, Operator: ModStr, Right: $3}
2146 }
2147 | value_expression MOD value_expression
2148 {
2149 $$ = &BinaryExpr{Left: $1, Operator: ModStr, Right: $3}
2150 }
2151 | value_expression SHIFT_LEFT value_expression
2152 {
2153 $$ = &BinaryExpr{Left: $1, Operator: ShiftLeftStr, Right: $3}
2154 }
2155 | value_expression SHIFT_RIGHT value_expression
2156 {
2157 $$ = &BinaryExpr{Left: $1, Operator: ShiftRightStr, Right: $3}
2158 }
2159 | column_name JSON_EXTRACT_OP value
2160 {
2161 $$ = &BinaryExpr{Left: $1, Operator: JSONExtractOp, Right: $3}
2162 }
2163 | column_name JSON_UNQUOTE_EXTRACT_OP value
2164 {
2165 $$ = &BinaryExpr{Left: $1, Operator: JSONUnquoteExtractOp, Right: $3}
2166 }
2167 | value_expression COLLATE charset
2168 {
2169 $$ = &CollateExpr{Expr: $1, Charset: $3}
2170 }
2171 | BINARY value_expression %prec UNARY
2172 {
2173 $$ = &UnaryExpr{Operator: BinaryStr, Expr: $2}
2174 }
2175 | UNDERSCORE_BINARY value_expression %prec UNARY
2176 {
2177 $$ = &UnaryExpr{Operator: UBinaryStr, Expr: $2}
2178 }
2179 | '+' value_expression %prec UNARY
2180 {
2181 if num, ok := $2.(*SQLVal); ok && num.Type == IntVal {
2182 $$ = num
2183 } else {
2184 $$ = &UnaryExpr{Operator: UPlusStr, Expr: $2}
2185 }
2186 }
2187 | '-' value_expression %prec UNARY
2188 {
2189 if num, ok := $2.(*SQLVal); ok && num.Type == IntVal {
2190 // Handle double negative
2191 if num.Val[0] == '-' {
2192 num.Val = num.Val[1:]
2193 $$ = num
2194 } else {
2195 $$ = NewIntVal(append([]byte("-"), num.Val...))
2196 }
2197 } else {
2198 $$ = &UnaryExpr{Operator: UMinusStr, Expr: $2}
2199 }
2200 }
2201 | '~' value_expression
2202 {
2203 $$ = &UnaryExpr{Operator: TildaStr, Expr: $2}
2204 }
2205 | '!' value_expression %prec UNARY
2206 {
2207 $$ = &UnaryExpr{Operator: BangStr, Expr: $2}
2208 }
2209 | INTERVAL value_expression sql_id
2210 {
2211 // This rule prevents the usage of INTERVAL
2212 // as a function. If support is needed for that,
2213 // we'll need to revisit this. The solution
2214 // will be non-trivial because of grammar conflicts.
2215 $$ = &IntervalExpr{Expr: $2, Unit: $3.String()}
2216 }
2217 | function_call_generic
2218 | function_call_keyword
2219 | function_call_nonkeyword
2220 | function_call_conflict
2221
2222 /*
2223 Regular function calls without special token or syntax, guaranteed to not
2224 introduce side effects due to being a simple identifier
2225 */
2226 function_call_generic:
2227 sql_id openb select_expression_list_opt closeb
2228 {
2229 $$ = &FuncExpr{Name: $1, Exprs: $3}
2230 }
2231 | sql_id openb DISTINCT select_expression_list closeb
2232 {
2233 $$ = &FuncExpr{Name: $1, Distinct: true, Exprs: $4}
2234 }
2235 | table_id '.' reserved_sql_id openb select_expression_list_opt closeb
2236 {
2237 $$ = &FuncExpr{Qualifier: $1, Name: $3, Exprs: $5}
2238 }
2239
2240 /*
2241 Function calls using reserved keywords, with dedicated grammar rules
2242 as a result
2243 */
2244 function_call_keyword:
2245 LEFT openb select_expression_list closeb
2246 {
2247 $$ = &FuncExpr{Name: NewColIdent("left"), Exprs: $3}
2248 }
2249 | RIGHT openb select_expression_list closeb
2250 {
2251 $$ = &FuncExpr{Name: NewColIdent("right"), Exprs: $3}
2252 }
2253 | CONVERT openb expression ',' convert_type closeb
2254 {
2255 $$ = &ConvertExpr{Expr: $3, Type: $5}
2256 }
2257 | CAST openb expression AS convert_type closeb
2258 {
2259 $$ = &ConvertExpr{Expr: $3, Type: $5}
2260 }
2261 | CONVERT openb expression USING charset closeb
2262 {
2263 $$ = &ConvertUsingExpr{Expr: $3, Type: $5}
2264 }
2265 | SUBSTR openb column_name ',' value_expression closeb
2266 {
2267 $$ = &SubstrExpr{Name: $3, From: $5, To: nil}
2268 }
2269 | SUBSTR openb column_name ',' value_expression ',' value_expression closeb
2270 {
2271 $$ = &SubstrExpr{Name: $3, From: $5, To: $7}
2272 }
2273 | SUBSTR openb column_name FROM value_expression FOR value_expression closeb
2274 {
2275 $$ = &SubstrExpr{Name: $3, From: $5, To: $7}
2276 }
2277 | SUBSTRING openb column_name ',' value_expression closeb
2278 {
2279 $$ = &SubstrExpr{Name: $3, From: $5, To: nil}
2280 }
2281 | SUBSTRING openb column_name ',' value_expression ',' value_expression closeb
2282 {
2283 $$ = &SubstrExpr{Name: $3, From: $5, To: $7}
2284 }
2285 | SUBSTRING openb column_name FROM value_expression FOR value_expression closeb
2286 {
2287 $$ = &SubstrExpr{Name: $3, From: $5, To: $7}
2288 }
2289 | MATCH openb select_expression_list closeb AGAINST openb value_expression match_option closeb
2290 {
2291 $$ = &MatchExpr{Columns: $3, Expr: $7, Option: $8}
2292 }
2293 | GROUP_CONCAT openb distinct_opt select_expression_list order_by_opt separator_opt closeb
2294 {
2295 $$ = &GroupConcatExpr{Distinct: $3, Exprs: $4, OrderBy: $5, Separator: $6}
2296 }
2297 | CASE expression_opt when_expression_list else_expression_opt END
2298 {
2299 $$ = &CaseExpr{Expr: $2, Whens: $3, Else: $4}
2300 }
2301 | VALUES openb column_name closeb
2302 {
2303 $$ = &ValuesFuncExpr{Name: $3}
2304 }
2305
2306 /*
2307 Function calls using non reserved keywords but with special syntax forms.
2308 Dedicated grammar rules are needed because of the special syntax
2309 */
2310 function_call_nonkeyword:
2311 CURRENT_TIMESTAMP func_datetime_precision_opt
2312 {
2313 $$ = &FuncExpr{Name:NewColIdent("current_timestamp")}
2314 }
2315 | UTC_TIMESTAMP func_datetime_precision_opt
2316 {
2317 $$ = &FuncExpr{Name:NewColIdent("utc_timestamp")}
2318 }
2319 | UTC_TIME func_datetime_precision_opt
2320 {
2321 $$ = &FuncExpr{Name:NewColIdent("utc_time")}
2322 }
2323 | UTC_DATE func_datetime_precision_opt
2324 {
2325 $$ = &FuncExpr{Name:NewColIdent("utc_date")}
2326 }
2327 // now
2328 | LOCALTIME func_datetime_precision_opt
2329 {
2330 $$ = &FuncExpr{Name:NewColIdent("localtime")}
2331 }
2332 // now
2333 | LOCALTIMESTAMP func_datetime_precision_opt
2334 {
2335 $$ = &FuncExpr{Name:NewColIdent("localtimestamp")}
2336 }
2337 // curdate
2338 | CURRENT_DATE func_datetime_precision_opt
2339 {
2340 $$ = &FuncExpr{Name:NewColIdent("current_date")}
2341 }
2342 // curtime
2343 | CURRENT_TIME func_datetime_precision_opt
2344 {
2345 $$ = &FuncExpr{Name:NewColIdent("current_time")}
2346 }
2347
2348 func_datetime_precision_opt:
2349 /* empty */
2350 | openb closeb
2351
2352 /*
2353 Function calls using non reserved keywords with *normal* syntax forms. Because
2354 the names are non-reserved, they need a dedicated rule so as not to conflict
2355 */
2356 function_call_conflict:
2357 IF openb select_expression_list closeb
2358 {
2359 $$ = &FuncExpr{Name: NewColIdent("if"), Exprs: $3}
2360 }
2361 | DATABASE openb select_expression_list_opt closeb
2362 {
2363 $$ = &FuncExpr{Name: NewColIdent("database"), Exprs: $3}
2364 }
2365 | MOD openb select_expression_list closeb
2366 {
2367 $$ = &FuncExpr{Name: NewColIdent("mod"), Exprs: $3}
2368 }
2369 | REPLACE openb select_expression_list closeb
2370 {
2371 $$ = &FuncExpr{Name: NewColIdent("replace"), Exprs: $3}
2372 }
2373
2374 match_option:
2375 /*empty*/
2376 {
2377 $$ = ""
2378 }
2379 | IN BOOLEAN MODE
2380 {
2381 $$ = BooleanModeStr
2382 }
2383 | IN NATURAL LANGUAGE MODE
2384 {
2385 $$ = NaturalLanguageModeStr
2386 }
2387 | IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION
2388 {
2389 $$ = NaturalLanguageModeWithQueryExpansionStr
2390 }
2391 | WITH QUERY EXPANSION
2392 {
2393 $$ = QueryExpansionStr
2394 }
2395
2396 charset:
2397 ID
2398 {
2399 $$ = string($1)
2400 }
2401 | STRING
2402 {
2403 $$ = string($1)
2404 }
2405
2406 convert_type:
2407 BINARY length_opt
2408 {
2409 $$ = &ConvertType{Type: string($1), Length: $2}
2410 }
2411 | CHAR length_opt charset_opt
2412 {
2413 $$ = &ConvertType{Type: string($1), Length: $2, Charset: $3, Operator: CharacterSetStr}
2414 }
2415 | CHAR length_opt ID
2416 {
2417 $$ = &ConvertType{Type: string($1), Length: $2, Charset: string($3)}
2418 }
2419 | DATE
2420 {
2421 $$ = &ConvertType{Type: string($1)}
2422 }
2423 | DATETIME length_opt
2424 {
2425 $$ = &ConvertType{Type: string($1), Length: $2}
2426 }
2427 | DECIMAL decimal_length_opt
2428 {
2429 $$ = &ConvertType{Type: string($1)}
2430 $$.Length = $2.Length
2431 $$.Scale = $2.Scale
2432 }
2433 | JSON
2434 {
2435 $$ = &ConvertType{Type: string($1)}
2436 }
2437 | NCHAR length_opt
2438 {
2439 $$ = &ConvertType{Type: string($1), Length: $2}
2440 }
2441 | SIGNED
2442 {
2443 $$ = &ConvertType{Type: string($1)}
2444 }
2445 | SIGNED INTEGER
2446 {
2447 $$ = &ConvertType{Type: string($1)}
2448 }
2449 | TIME length_opt
2450 {
2451 $$ = &ConvertType{Type: string($1), Length: $2}
2452 }
2453 | UNSIGNED
2454 {
2455 $$ = &ConvertType{Type: string($1)}
2456 }
2457 | UNSIGNED INTEGER
2458 {
2459 $$ = &ConvertType{Type: string($1)}
2460 }
2461
2462 expression_opt:
2463 {
2464 $$ = nil
2465 }
2466 | expression
2467 {
2468 $$ = $1
2469 }
2470
2471 separator_opt:
2472 {
2473 $$ = string("")
2474 }
2475 | SEPARATOR STRING
2476 {
2477 $$ = " separator '"+string($2)+"'"
2478 }
2479
2480 when_expression_list:
2481 when_expression
2482 {
2483 $$ = []*When{$1}
2484 }
2485 | when_expression_list when_expression
2486 {
2487 $$ = append($1, $2)
2488 }
2489
2490 when_expression:
2491 WHEN expression THEN expression
2492 {
2493 $$ = &When{Cond: $2, Val: $4}
2494 }
2495
2496 else_expression_opt:
2497 {
2498 $$ = nil
2499 }
2500 | ELSE expression
2501 {
2502 $$ = $2
2503 }
2504
2505 column_name:
2506 sql_id
2507 {
2508 $$ = &ColName{Name: $1}
2509 }
2510 | table_id '.' reserved_sql_id
2511 {
2512 $$ = &ColName{Qualifier: TableName{Name: $1}, Name: $3}
2513 }
2514 | table_id '.' reserved_table_id '.' reserved_sql_id
2515 {
2516 $$ = &ColName{Qualifier: TableName{Qualifier: $1, Name: $3}, Name: $5}
2517 }
2518
2519 value:
2520 STRING
2521 {
2522 $$ = NewStrVal($1)
2523 }
2524 | HEX
2525 {
2526 $$ = NewHexVal($1)
2527 }
2528 | BIT_LITERAL
2529 {
2530 $$ = NewBitVal($1)
2531 }
2532 | INTEGRAL
2533 {
2534 $$ = NewIntVal($1)
2535 }
2536 | FLOAT
2537 {
2538 $$ = NewFloatVal($1)
2539 }
2540 | HEXNUM
2541 {
2542 $$ = NewHexNum($1)
2543 }
2544 | VALUE_ARG
2545 {
2546 $$ = NewValArg($1)
2547 }
2548 | NULL
2549 {
2550 $$ = &NullVal{}
2551 }
2552
2553 num_val:
2554 sql_id
2555 {
2556 // TODO(sougou): Deprecate this construct.
2557 if $1.Lowered() != "value" {
2558 yylex.Error("expecting value after next")
2559 return 1
2560 }
2561 $$ = NewIntVal([]byte("1"))
2562 }
2563 | INTEGRAL VALUES
2564 {
2565 $$ = NewIntVal($1)
2566 }
2567 | VALUE_ARG VALUES
2568 {
2569 $$ = NewValArg($1)
2570 }
2571
2572 group_by_opt:
2573 {
2574 $$ = nil
2575 }
2576 | GROUP BY expression_list
2577 {
2578 $$ = $3
2579 }
2580
2581 having_opt:
2582 {
2583 $$ = nil
2584 }
2585 | HAVING expression
2586 {
2587 $$ = $2
2588 }
2589
2590 order_by_opt:
2591 {
2592 $$ = nil
2593 }
2594 | ORDER BY order_list
2595 {
2596 $$ = $3
2597 }
2598
2599 order_list:
2600 order
2601 {
2602 $$ = OrderBy{$1}
2603 }
2604 | order_list ',' order
2605 {
2606 $$ = append($1, $3)
2607 }
2608
2609 order:
2610 expression asc_desc_opt
2611 {
2612 $$ = &Order{Expr: $1, Direction: $2}
2613 }
2614
2615 asc_desc_opt:
2616 {
2617 $$ = AscScr
2618 }
2619 | ASC
2620 {
2621 $$ = AscScr
2622 }
2623 | DESC
2624 {
2625 $$ = DescScr
2626 }
2627
2628 limit_opt:
2629 {
2630 $$ = nil
2631 }
2632 | LIMIT expression
2633 {
2634 $$ = &Limit{Rowcount: $2}
2635 }
2636 | LIMIT expression ',' expression
2637 {
2638 $$ = &Limit{Offset: $2, Rowcount: $4}
2639 }
2640 | LIMIT expression OFFSET expression
2641 {
2642 $$ = &Limit{Offset: $4, Rowcount: $2}
2643 }
2644
2645 lock_opt:
2646 {
2647 $$ = ""
2648 }
2649 | FOR UPDATE
2650 {
2651 $$ = ForUpdateStr
2652 }
2653 | LOCK IN SHARE MODE
2654 {
2655 $$ = ShareModeStr
2656 }
2657
2658 // insert_data expands all combinations into a single rule.
2659 // This avoids a shift/reduce conflict while encountering the
2660 // following two possible constructs:
2661 // insert into t1(a, b) (select * from t2)
2662 // insert into t1(select * from t2)
2663 // Because the rules are together, the parser can keep shifting
2664 // the tokens until it disambiguates a as sql_id and select as keyword.
2665 insert_data:
2666 VALUES tuple_list
2667 {
2668 $$ = &Insert{Rows: $2}
2669 }
2670 | select_statement
2671 {
2672 $$ = &Insert{Rows: $1}
2673 }
2674 | openb select_statement closeb
2675 {
2676 // Drop the redundant parenthesis.
2677 $$ = &Insert{Rows: $2}
2678 }
2679 | openb ins_column_list closeb VALUES tuple_list
2680 {
2681 $$ = &Insert{Columns: $2, Rows: $5}
2682 }
2683 | openb ins_column_list closeb select_statement
2684 {
2685 $$ = &Insert{Columns: $2, Rows: $4}
2686 }
2687 | openb ins_column_list closeb openb select_statement closeb
2688 {
2689 // Drop the redundant parenthesis.
2690 $$ = &Insert{Columns: $2, Rows: $5}
2691 }
2692
2693 ins_column_list:
2694 sql_id
2695 {
2696 $$ = Columns{$1}
2697 }
2698 | sql_id '.' sql_id
2699 {
2700 $$ = Columns{$3}
2701 }
2702 | ins_column_list ',' sql_id
2703 {
2704 $$ = append($$, $3)
2705 }
2706 | ins_column_list ',' sql_id '.' sql_id
2707 {
2708 $$ = append($$, $5)
2709 }
2710
2711 on_dup_opt:
2712 {
2713 $$ = nil
2714 }
2715 | ON DUPLICATE KEY UPDATE update_list
2716 {
2717 $$ = $5
2718 }
2719
2720 tuple_list:
2721 tuple_or_empty
2722 {
2723 $$ = Values{$1}
2724 }
2725 | tuple_list ',' tuple_or_empty
2726 {
2727 $$ = append($1, $3)
2728 }
2729
2730 tuple_or_empty:
2731 row_tuple
2732 {
2733 $$ = $1
2734 }
2735 | openb closeb
2736 {
2737 $$ = ValTuple{}
2738 }
2739
2740 row_tuple:
2741 openb expression_list closeb
2742 {
2743 $$ = ValTuple($2)
2744 }
2745
2746 tuple_expression:
2747 row_tuple
2748 {
2749 if len($1) == 1 {
2750 $$ = &ParenExpr{$1[0]}
2751 } else {
2752 $$ = $1
2753 }
2754 }
2755
2756 update_list:
2757 update_expression
2758 {
2759 $$ = UpdateExprs{$1}
2760 }
2761 | update_list ',' update_expression
2762 {
2763 $$ = append($1, $3)
2764 }
2765
2766 update_expression:
2767 column_name '=' expression
2768 {
2769 $$ = &UpdateExpr{Name: $1, Expr: $3}
2770 }
2771
2772 set_list:
2773 set_expression
2774 {
2775 $$ = SetExprs{$1}
2776 }
2777 | set_list ',' set_expression
2778 {
2779 $$ = append($1, $3)
2780 }
2781
2782 set_expression:
2783 reserved_sql_id '=' ON
2784 {
2785 $$ = &SetExpr{Name: $1, Expr: NewStrVal([]byte("on"))}
2786 }
2787 | reserved_sql_id '=' expression
2788 {
2789 $$ = &SetExpr{Name: $1, Expr: $3}
2790 }
2791 | charset_or_character_set charset_value collate_opt
2792 {
2793 $$ = &SetExpr{Name: NewColIdent(string($1)), Expr: $2}
2794 }
2795
2796 charset_or_character_set:
2797 CHARSET
2798 | CHARACTER SET
2799 {
2800 $$ = []byte("charset")
2801 }
2802 | NAMES
2803
2804 charset_value:
2805 sql_id
2806 {
2807 $$ = NewStrVal([]byte($1.String()))
2808 }
2809 | STRING
2810 {
2811 $$ = NewStrVal($1)
2812 }
2813 | DEFAULT
2814 {
2815 $$ = &Default{}
2816 }
2817
2818 for_from:
2819 FOR
2820 | FROM
2821
2822 exists_opt:
2823 { $$ = 0 }
2824 | IF EXISTS
2825 { $$ = 1 }
2826
2827 not_exists_opt:
2828 { $$ = struct{}{} }
2829 | IF NOT EXISTS
2830 { $$ = struct{}{} }
2831
2832 ignore_opt:
2833 { $$ = "" }
2834 | IGNORE
2835 { $$ = IgnoreStr }
2836
2837 non_add_drop_or_rename_operation:
2838 ALTER
2839 { $$ = struct{}{} }
2840 | AUTO_INCREMENT
2841 { $$ = struct{}{} }
2842 | CHARACTER
2843 { $$ = struct{}{} }
2844 | COMMENT_KEYWORD
2845 { $$ = struct{}{} }
2846 | DEFAULT
2847 { $$ = struct{}{} }
2848 | ORDER
2849 { $$ = struct{}{} }
2850 | CONVERT
2851 { $$ = struct{}{} }
2852 | PARTITION
2853 { $$ = struct{}{} }
2854 | UNUSED
2855 { $$ = struct{}{} }
2856 | ID
2857 { $$ = struct{}{} }
2858
2859 to_opt:
2860 { $$ = struct{}{} }
2861 | TO
2862 { $$ = struct{}{} }
2863 | AS
2864 { $$ = struct{}{} }
2865
2866 index_opt:
2867 INDEX
2868 { $$ = struct{}{} }
2869 | KEY
2870 { $$ = struct{}{} }
2871
2872 constraint_opt:
2873 { $$ = struct{}{} }
2874 | UNIQUE
2875 { $$ = struct{}{} }
2876 | sql_id
2877 { $$ = struct{}{} }
2878
2879 using_opt:
2880 { $$ = ColIdent{} }
2881 | USING sql_id
2882 { $$ = $2 }
2883
2884 sql_id:
2885 ID
2886 {
2887 $$ = NewColIdent(string($1))
2888 }
2889 | non_reserved_keyword
2890 {
2891 $$ = NewColIdent(string($1))
2892 }
2893
2894 reserved_sql_id:
2895 sql_id
2896 | reserved_keyword
2897 {
2898 $$ = NewColIdent(string($1))
2899 }
2900
2901 table_id:
2902 ID
2903 {
2904 $$ = NewTableIdent(string($1))
2905 }
2906 | non_reserved_keyword
2907 {
2908 $$ = NewTableIdent(string($1))
2909 }
2910
2911 reserved_table_id:
2912 table_id
2913 | reserved_keyword
2914 {
2915 $$ = NewTableIdent(string($1))
2916 }
2917
2918 /*
2919 These are not all necessarily reserved in MySQL, but some are.
2920
2921 These are more importantly reserved because they may conflict with our grammar.
2922 If you want to move one that is not reserved in MySQL (i.e. ESCAPE) to the
2923 non_reserved_keywords, you'll need to deal with any conflicts.
2924
2925 Sorted alphabetically
2926 */
2927 reserved_keyword:
2928 ADD
2929 | AND
2930 | AS
2931 | ASC
2932 | AUTO_INCREMENT
2933 | BETWEEN
2934 | BINARY
2935 | BY
2936 | CASE
2937 | COLLATE
2938 | CONVERT
2939 | CREATE
2940 | CROSS
2941 | CURRENT_DATE
2942 | CURRENT_TIME
2943 | CURRENT_TIMESTAMP
2944 | SUBSTR
2945 | SUBSTRING
2946 | DATABASE
2947 | DATABASES
2948 | DEFAULT
2949 | DELETE
2950 | DESC
2951 | DESCRIBE
2952 | DISTINCT
2953 | DIV
2954 | DROP
2955 | ELSE
2956 | END
2957 | ESCAPE
2958 | EXISTS
2959 | EXPLAIN
2960 | FALSE
2961 | FOR
2962 | FORCE
2963 | FROM
2964 | GROUP
2965 | HAVING
2966 | IF
2967 | IGNORE
2968 | IN
2969 | INDEX
2970 | INNER
2971 | INSERT
2972 | INTERVAL
2973 | INTO
2974 | IS
2975 | JOIN
2976 | KEY
2977 | LEFT
2978 | LIKE
2979 | LIMIT
2980 | LOCALTIME
2981 | LOCALTIMESTAMP
2982 | LOCK
2983 | MATCH
2984 | MAXVALUE
2985 | MOD
2986 | NATURAL
2987 | NEXT // next should be doable as non-reserved, but is not due to the special `select next num_val` query that vitess supports
2988 | NOT
2989 | NULL
2990 | ON
2991 | OR
2992 | ORDER
2993 | OUTER
2994 | REGEXP
2995 | RENAME
2996 | REPLACE
2997 | RIGHT
2998 | SCHEMA
2999 | SELECT
3000 | SEPARATOR
3001 | SET
3002 | SHOW
3003 | STRAIGHT_JOIN
3004 | TABLE
3005 | TABLES
3006 | THEN
3007 | TO
3008 | TRUE
3009 | UNION
3010 | UNIQUE
3011 | UPDATE
3012 | USE
3013 | USING
3014 | UTC_DATE
3015 | UTC_TIME
3016 | UTC_TIMESTAMP
3017 | VALUES
3018 | WHEN
3019 | WHERE
3020
3021 /*
3022 These are non-reserved Vitess, because they don't cause conflicts in the grammar.
3023 Some of them may be reserved in MySQL. The good news is we backtick quote them
3024 when we rewrite the query, so no issue should arise.
3025
3026 Sorted alphabetically
3027 */
3028 non_reserved_keyword:
3029 AGAINST
3030 | BEGIN
3031 | BIGINT
3032 | BIT
3033 | BLOB
3034 | BOOL
3035 | CHAR
3036 | CHARACTER
3037 | CHARSET
3038 | COMMENT_KEYWORD
3039 | COMMIT
3040 | COMMITTED
3041 | DATE
3042 | DATETIME
3043 | DECIMAL
3044 | DOUBLE
3045 | DUPLICATE
3046 | ENUM
3047 | EXPANSION
3048 | FLOAT_TYPE
3049 | FOREIGN
3050 | FULLTEXT
3051 | GEOMETRY
3052 | GEOMETRYCOLLECTION
3053 | GLOBAL
3054 | INT
3055 | INTEGER
3056 | ISOLATION
3057 | JSON
3058 | KEY_BLOCK_SIZE
3059 | KEYS
3060 | LANGUAGE
3061 | LAST_INSERT_ID
3062 | LESS
3063 | LEVEL
3064 | LINESTRING
3065 | LONGBLOB
3066 | LONGTEXT
3067 | MEDIUMBLOB
3068 | MEDIUMINT
3069 | MEDIUMTEXT
3070 | MODE
3071 | MULTILINESTRING
3072 | MULTIPOINT
3073 | MULTIPOLYGON
3074 | NAMES
3075 | NCHAR
3076 | NUMERIC
3077 | OFFSET
3078 | ONLY
3079 | OPTIMIZE
3080 | PARTITION
3081 | POINT
3082 | POLYGON
3083 | PRIMARY
3084 | PROCEDURE
3085 | QUERY
3086 | READ
3087 | REAL
3088 | REORGANIZE
3089 | REPAIR
3090 | REPEATABLE
3091 | ROLLBACK
3092 | SESSION
3093 | SERIALIZABLE
3094 | SHARE
3095 | SIGNED
3096 | SMALLINT
3097 | SPATIAL
3098 | START
3099 | STATUS
3100 | TEXT
3101 | THAN
3102 | TIME
3103 | TIMESTAMP
3104 | TINYBLOB
3105 | TINYINT
3106 | TINYTEXT
3107 | TRANSACTION
3108 | TRIGGER
3109 | TRUNCATE
3110 | UNCOMMITTED
3111 | UNSIGNED
3112 | UNUSED
3113 | VARBINARY
3114 | VARCHAR
3115 | VARIABLES
3116 | VIEW
3117 | VINDEX
3118 | VINDEXES
3119 | VITESS_KEYSPACES
3120 | VITESS_SHARDS
3121 | VITESS_TABLETS
3122 | VSCHEMA_TABLES
3123 | WITH
3124 | WRITE
3125 | YEAR
3126 | ZEROFILL
3127
3128 openb:
3129 '('
3130 {
3131 if incNesting(yylex) {
3132 yylex.Error("max nesting level reached")
3133 return 1
3134 }
3135 }
3136
3137 closeb:
3138 ')'
3139 {
3140 decNesting(yylex)
3141 }
3142
3143 force_eof:
3144 {
3145 forceEOF(yylex)
3146 }
3147
3148 ddl_force_eof:
3149 {
3150 forceEOF(yylex)
3151 }
3152 | openb
3153 {
3154 forceEOF(yylex)
3155 }
3156 | reserved_sql_id
3157 {
3158 forceEOF(yylex)
3159 }