forked from vitessio/vitess
/
sql.y
1171 lines (1077 loc) · 21.1 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 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
%{
package sqlparser
func setParseTree(yylex interface{}, stmt Statement) {
yylex.(*Tokenizer).ParseTree = stmt
}
func setAllowComments(yylex interface{}, allow bool) {
yylex.(*Tokenizer).AllowComments = allow
}
func incNesting(yylex interface{}) bool {
yylex.(*Tokenizer).nesting++
if yylex.(*Tokenizer).nesting == 200 {
return true
}
return false
}
func decNesting(yylex interface{}) {
yylex.(*Tokenizer).nesting--
}
func forceEOF(yylex interface{}) {
yylex.(*Tokenizer).ForceEOF = true
}
%}
%union {
empty struct{}
statement Statement
selStmt SelectStatement
byt byte
bytes []byte
bytes2 [][]byte
str string
selectExprs SelectExprs
selectExpr SelectExpr
columns Columns
colName *ColName
tableExprs TableExprs
tableExpr TableExpr
tableName *TableName
indexHints *IndexHints
expr Expr
boolExpr BoolExpr
valExpr ValExpr
colTuple ColTuple
valExprs ValExprs
values Values
rowTuple RowTuple
subquery *Subquery
caseExpr *CaseExpr
whens []*When
when *When
orderBy OrderBy
order *Order
limit *Limit
insRows InsertRows
updateExprs UpdateExprs
updateExpr *UpdateExpr
colIdent ColIdent
colIdents []ColIdent
tableIdent TableIdent
}
%token LEX_ERROR
%left <empty> UNION
%token <empty> SELECT INSERT UPDATE DELETE FROM WHERE GROUP HAVING ORDER BY LIMIT FOR
%token <empty> ALL DISTINCT AS EXISTS ASC DESC INTO DUPLICATE KEY DEFAULT SET LOCK
%token <empty> VALUES LAST_INSERT_ID
%token <empty> NEXT VALUE
%left <empty> JOIN STRAIGHT_JOIN LEFT RIGHT INNER OUTER CROSS NATURAL USE FORCE
%left <empty> ON
%token <empty> '(' ',' ')'
%token <bytes> ID STRING NUMBER VALUE_ARG LIST_ARG COMMENT
%token <empty> 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 <empty> OR
%left <empty> AND
%right <empty> NOT
%left <empty> BETWEEN CASE WHEN THEN ELSE
%left <empty> '=' '<' '>' LE GE NE NULL_SAFE_EQUAL IS LIKE REGEXP IN
%left <empty> '|'
%left <empty> '&'
%left <empty> SHIFT_LEFT SHIFT_RIGHT
%left <empty> '+' '-'
%left <empty> '*' '/' '%'
%left <empty> '^'
%right <empty> '~' UNARY
%right <empty> INTERVAL
%nonassoc <empty> '.'
%left <empty> END
// DDL Tokens
%token <empty> CREATE ALTER DROP RENAME ANALYZE
%token <empty> TABLE INDEX VIEW TO IGNORE IF UNIQUE USING
%token <empty> SHOW DESCRIBE EXPLAIN
// MySQL reserved words that are unused by this grammar will map to this token.
%token <empty> UNUSED
%type <statement> command
%type <selStmt> select_statement
%type <statement> insert_statement update_statement delete_statement set_statement
%type <statement> create_statement alter_statement rename_statement drop_statement
%type <statement> analyze_statement other_statement
%type <bytes2> comment_opt comment_list
%type <str> union_op
%type <str> distinct_opt straight_join_opt
%type <selectExprs> select_expression_list
%type <selectExpr> select_expression
%type <expr> expression
%type <tableExprs> table_references
%type <tableExpr> table_reference table_factor join_table
%type <str> inner_join outer_join natural_join
%type <tableName> table_name
%type <indexHints> index_hint_list
%type <colIdents> index_list
%type <boolExpr> where_expression_opt
%type <boolExpr> boolean_expression condition
%type <str> compare
%type <insRows> row_list
%type <valExpr> value value_expression
%type <str> is_suffix
%type <colTuple> col_tuple
%type <valExprs> value_expression_list
%type <values> tuple_list
%type <rowTuple> row_tuple
%type <subquery> subquery
%type <colName> column_name
%type <caseExpr> case_expression
%type <whens> when_expression_list
%type <when> when_expression
%type <valExpr> value_expression_opt else_expression_opt
%type <valExprs> group_by_opt
%type <boolExpr> 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> column_list_opt column_list
%type <updateExprs> on_dup_opt
%type <updateExprs> update_list
%type <updateExpr> update_expression
%type <empty> for_from
%type <str> ignore_opt
%type <byt> exists_opt
%type <empty> not_exists_opt non_rename_operation to_opt constraint_opt using_opt
%type <colIdent> sql_id as_ci_opt
%type <tableIdent> table_id as_opt_id
%type <empty> as_opt
%type <empty> force_eof
%start any_command
%%
any_command:
command
{
setParseTree(yylex, $1)
}
command:
select_statement
{
$$ = $1
}
| insert_statement
| update_statement
| delete_statement
| set_statement
| create_statement
| alter_statement
| rename_statement
| drop_statement
| analyze_statement
| other_statement
select_statement:
SELECT comment_opt distinct_opt straight_join_opt select_expression_list FROM table_references where_expression_opt group_by_opt having_opt order_by_opt limit_opt lock_opt
{
$$ = &Select{Comments: Comments($2), Distinct: $3, Hints: $4, SelectExprs: $5, From: $7, Where: NewWhere(WhereStr, $8), GroupBy: GroupBy($9), Having: NewWhere(HavingStr, $10), OrderBy: $11, Limit: $12, Lock: $13}
}
| SELECT comment_opt NEXT sql_id for_from table_name
{
if $4.Lowered() != "value" {
yylex.Error("expecting value after next")
return 1
}
$$ = &Select{Comments: Comments($2), SelectExprs: SelectExprs{Nextval{}}, From: TableExprs{&AliasedTableExpr{Expr: $6}}}
}
| select_statement union_op select_statement %prec UNION
{
$$ = &Union{Type: $2, Left: $1, Right: $3}
}
insert_statement:
INSERT comment_opt ignore_opt INTO table_name column_list_opt row_list on_dup_opt
{
$$ = &Insert{Comments: Comments($2), Ignore: $3, Table: $5, Columns: $6, Rows: $7, OnDup: OnDup($8)}
}
| INSERT comment_opt ignore_opt INTO table_name SET update_list on_dup_opt
{
cols := make(Columns, 0, len($7))
vals := make(ValTuple, 0, len($7))
for _, updateList := range $7 {
cols = append(cols, updateList.Name)
vals = append(vals, updateList.Expr)
}
$$ = &Insert{Comments: Comments($2), Ignore: $3, Table: $5, Columns: cols, Rows: Values{vals}, OnDup: OnDup($8)}
}
update_statement:
UPDATE comment_opt table_name SET update_list where_expression_opt order_by_opt limit_opt
{
$$ = &Update{Comments: Comments($2), Table: $3, Exprs: $5, Where: NewWhere(WhereStr, $6), OrderBy: $7, Limit: $8}
}
delete_statement:
DELETE comment_opt FROM table_name where_expression_opt order_by_opt limit_opt
{
$$ = &Delete{Comments: Comments($2), Table: $4, Where: NewWhere(WhereStr, $5), OrderBy: $6, Limit: $7}
}
set_statement:
SET comment_opt update_list
{
$$ = &Set{Comments: Comments($2), Exprs: $3}
}
create_statement:
CREATE TABLE not_exists_opt table_id force_eof
{
$$ = &DDL{Action: CreateStr, NewName: $4}
}
| CREATE constraint_opt INDEX ID using_opt ON table_id force_eof
{
// Change this to an alter statement
$$ = &DDL{Action: AlterStr, Table: $7, NewName: $7}
}
| CREATE VIEW sql_id force_eof
{
$$ = &DDL{Action: CreateStr, NewName: TableIdent($3.Lowered())}
}
alter_statement:
ALTER ignore_opt TABLE table_id non_rename_operation force_eof
{
$$ = &DDL{Action: AlterStr, Table: $4, NewName: $4}
}
| ALTER ignore_opt TABLE table_id RENAME to_opt table_id
{
// Change this to a rename statement
$$ = &DDL{Action: RenameStr, Table: $4, NewName: $7}
}
| ALTER VIEW sql_id force_eof
{
$$ = &DDL{Action: AlterStr, Table: TableIdent($3.Lowered()), NewName: TableIdent($3.Lowered())}
}
rename_statement:
RENAME TABLE table_id TO table_id
{
$$ = &DDL{Action: RenameStr, Table: $3, NewName: $5}
}
drop_statement:
DROP TABLE exists_opt table_id
{
var exists bool
if $3 != 0 {
exists = true
}
$$ = &DDL{Action: DropStr, Table: $4, IfExists: exists}
}
| DROP INDEX ID ON table_id
{
// Change this to an alter statement
$$ = &DDL{Action: AlterStr, Table: $5, NewName: $5}
}
| DROP VIEW exists_opt sql_id force_eof
{
var exists bool
if $3 != 0 {
exists = true
}
$$ = &DDL{Action: DropStr, Table: TableIdent($4.Lowered()), IfExists: exists}
}
analyze_statement:
ANALYZE TABLE table_id
{
$$ = &DDL{Action: AlterStr, Table: $3, NewName: $3}
}
other_statement:
SHOW force_eof
{
$$ = &Other{}
}
| DESCRIBE force_eof
{
$$ = &Other{}
}
| EXPLAIN force_eof
{
$$ = &Other{}
}
comment_opt:
{
setAllowComments(yylex, true)
}
comment_list
{
$$ = $2
setAllowComments(yylex, false)
}
comment_list:
{
$$ = nil
}
| comment_list COMMENT
{
$$ = append($1, $2)
}
union_op:
UNION
{
$$ = UnionStr
}
| UNION ALL
{
$$ = UnionAllStr
}
| UNION DISTINCT
{
$$ = UnionDistinctStr
}
distinct_opt:
{
$$ = ""
}
| DISTINCT
{
$$ = DistinctStr
}
straight_join_opt:
{
$$ = ""
}
| STRAIGHT_JOIN
{
$$ = StraightJoinHint
}
select_expression_list:
select_expression
{
$$ = SelectExprs{$1}
}
| select_expression_list ',' select_expression
{
$$ = append($$, $3)
}
select_expression:
'*'
{
$$ = &StarExpr{}
}
| expression as_ci_opt
{
$$ = &NonStarExpr{Expr: $1, As: $2}
}
| table_id '.' '*'
{
$$ = &StarExpr{TableName: $1}
}
expression:
boolean_expression
{
$$ = $1
}
| value_expression
{
$$ = $1
}
as_ci_opt:
{
$$ = ColIdent{}
}
| sql_id
{
$$ = $1
}
| AS sql_id
{
$$ = $2
}
table_references:
table_reference
{
$$ = TableExprs{$1}
}
| table_references ',' table_reference
{
$$ = append($$, $3)
}
table_reference:
table_factor
| join_table
table_factor:
table_name as_opt_id index_hint_list
{
$$ = &AliasedTableExpr{Expr:$1, As: $2, Hints: $3}
}
| subquery as_opt table_id
{
$$ = &AliasedTableExpr{Expr:$1, As: $3}
}
| openb table_references closeb
{
$$ = &ParenTableExpr{Exprs: $2}
}
// There is a grammar conflict here:
// 1: INSERT INTO a SELECT * FROM b JOIN c ON b.i = c.i
// 2: INSERT INTO a SELECT * FROM b JOIN c ON DUPLICATE KEY UPDATE a.i = 1
// When yacc encounters the ON clause, it cannot determine which way to
// resolve. The %prec override below makes the parser choose the
// first construct, which automatically makes the second construct a
// syntax error. This is the same behavior as MySQL.
join_table:
table_reference inner_join table_factor %prec JOIN
{
$$ = &JoinTableExpr{LeftExpr: $1, Join: $2, RightExpr: $3}
}
| table_reference inner_join table_factor ON boolean_expression
{
$$ = &JoinTableExpr{LeftExpr: $1, Join: $2, RightExpr: $3, On: $5}
}
| table_reference outer_join table_reference ON boolean_expression
{
$$ = &JoinTableExpr{LeftExpr: $1, Join: $2, RightExpr: $3, On: $5}
}
| table_reference natural_join table_factor
{
$$ = &JoinTableExpr{LeftExpr: $1, Join: $2, RightExpr: $3}
}
as_opt:
{ $$ = struct{}{} }
| AS
{ $$ = struct{}{} }
as_opt_id:
{
$$ = ""
}
| table_id
{
$$ = $1
}
| AS table_id
{
$$ = $2
}
inner_join:
JOIN
{
$$ = JoinStr
}
| INNER JOIN
{
$$ = JoinStr
}
| CROSS JOIN
{
$$ = JoinStr
}
| STRAIGHT_JOIN
{
$$ = StraightJoinStr
}
outer_join:
LEFT JOIN
{
$$ = LeftJoinStr
}
| LEFT OUTER JOIN
{
$$ = LeftJoinStr
}
| RIGHT JOIN
{
$$ = RightJoinStr
}
| RIGHT OUTER JOIN
{
$$ = RightJoinStr
}
natural_join:
NATURAL JOIN
{
$$ = NaturalJoinStr
}
| NATURAL outer_join
{
if $2 == LeftJoinStr {
$$ = NaturalLeftJoinStr
} else {
$$ = NaturalRightJoinStr
}
}
table_name:
table_id
{
$$ = &TableName{Name: $1}
}
| table_id '.' table_id
{
$$ = &TableName{Qualifier: $1, Name: $3}
}
index_hint_list:
{
$$ = nil
}
| USE INDEX openb index_list closeb
{
$$ = &IndexHints{Type: UseStr, Indexes: $4}
}
| IGNORE INDEX openb index_list closeb
{
$$ = &IndexHints{Type: IgnoreStr, Indexes: $4}
}
| FORCE INDEX openb index_list closeb
{
$$ = &IndexHints{Type: ForceStr, Indexes: $4}
}
index_list:
sql_id
{
$$ = []ColIdent{$1}
}
| index_list ',' sql_id
{
$$ = append($1, $3)
}
where_expression_opt:
{
$$ = nil
}
| WHERE boolean_expression
{
$$ = $2
}
boolean_expression:
condition
| boolean_expression AND boolean_expression
{
$$ = &AndExpr{Left: $1, Right: $3}
}
| boolean_expression OR boolean_expression
{
$$ = &OrExpr{Left: $1, Right: $3}
}
| NOT boolean_expression
{
$$ = &NotExpr{Expr: $2}
}
| openb boolean_expression closeb
{
$$ = &ParenBoolExpr{Expr: $2}
}
| boolean_expression IS is_suffix
{
$$ = &IsExpr{Operator: $3, Expr: $1}
}
condition:
TRUE
{
$$ = BoolVal(true)
}
| FALSE
{
$$ = BoolVal(false)
}
| value_expression compare value_expression
{
$$ = &ComparisonExpr{Left: $1, Operator: $2, Right: $3}
}
| value_expression IN col_tuple
{
$$ = &ComparisonExpr{Left: $1, Operator: InStr, Right: $3}
}
| value_expression NOT IN col_tuple
{
$$ = &ComparisonExpr{Left: $1, Operator: NotInStr, Right: $4}
}
| value_expression LIKE value_expression
{
$$ = &ComparisonExpr{Left: $1, Operator: LikeStr, Right: $3}
}
| value_expression NOT LIKE value_expression
{
$$ = &ComparisonExpr{Left: $1, Operator: NotLikeStr, Right: $4}
}
| value_expression REGEXP value_expression
{
$$ = &ComparisonExpr{Left: $1, Operator: RegexpStr, Right: $3}
}
| value_expression NOT REGEXP value_expression
{
$$ = &ComparisonExpr{Left: $1, Operator: NotRegexpStr, Right: $4}
}
| value_expression BETWEEN value_expression AND value_expression
{
$$ = &RangeCond{Left: $1, Operator: BetweenStr, From: $3, To: $5}
}
| value_expression NOT BETWEEN value_expression AND value_expression
{
$$ = &RangeCond{Left: $1, Operator: NotBetweenStr, From: $4, To: $6}
}
| value_expression IS is_suffix
{
$$ = &IsExpr{Operator: $3, Expr: $1}
}
| EXISTS subquery
{
$$ = &ExistsExpr{Subquery: $2}
}
is_suffix:
NULL
{
$$ = IsNullStr
}
| NOT NULL
{
$$ = IsNotNullStr
}
| TRUE
{
$$ = IsTrueStr
}
| NOT TRUE
{
$$ = IsNotTrueStr
}
| FALSE
{
$$ = IsFalseStr
}
| NOT FALSE
{
$$ = IsNotFalseStr
}
compare:
'='
{
$$ = EqualStr
}
| '<'
{
$$ = LessThanStr
}
| '>'
{
$$ = GreaterThanStr
}
| LE
{
$$ = LessEqualStr
}
| GE
{
$$ = GreaterEqualStr
}
| NE
{
$$ = NotEqualStr
}
| NULL_SAFE_EQUAL
{
$$ = NullSafeEqualStr
}
col_tuple:
openb value_expression_list closeb
{
$$ = ValTuple($2)
}
| subquery
{
$$ = $1
}
| LIST_ARG
{
$$ = ListArg($1)
}
subquery:
openb select_statement closeb
{
$$ = &Subquery{$2}
}
value_expression_list:
value_expression
{
$$ = ValExprs{$1}
}
| value_expression_list ',' value_expression
{
$$ = append($1, $3)
}
value_expression:
value
{
$$ = $1
}
| column_name
{
$$ = $1
}
| row_tuple
{
$$ = $1
}
| value_expression '&' value_expression
{
$$ = &BinaryExpr{Left: $1, Operator: BitAndStr, Right: $3}
}
| value_expression '|' value_expression
{
$$ = &BinaryExpr{Left: $1, Operator: BitOrStr, Right: $3}
}
| value_expression '^' value_expression
{
$$ = &BinaryExpr{Left: $1, Operator: BitXorStr, Right: $3}
}
| value_expression '+' value_expression
{
$$ = &BinaryExpr{Left: $1, Operator: PlusStr, Right: $3}
}
| value_expression '-' value_expression
{
$$ = &BinaryExpr{Left: $1, Operator: MinusStr, Right: $3}
}
| value_expression '*' value_expression
{
$$ = &BinaryExpr{Left: $1, Operator: MultStr, Right: $3}
}
| value_expression '/' value_expression
{
$$ = &BinaryExpr{Left: $1, Operator: DivStr, Right: $3}
}
| value_expression '%' value_expression
{
$$ = &BinaryExpr{Left: $1, Operator: ModStr, Right: $3}
}
| value_expression SHIFT_LEFT value_expression
{
$$ = &BinaryExpr{Left: $1, Operator: ShiftLeftStr, Right: $3}
}
| value_expression SHIFT_RIGHT value_expression
{
$$ = &BinaryExpr{Left: $1, Operator: ShiftRightStr, Right: $3}
}
| '+' value_expression %prec UNARY
{
if num, ok := $2.(NumVal); ok {
$$ = num
} else {
$$ = &UnaryExpr{Operator: UPlusStr, Expr: $2}
}
}
| '-' value_expression %prec UNARY
{
if num, ok := $2.(NumVal); ok {
// Handle double negative
if num[0] == '-' {
$$ = num[1:]
} else {
$$ = append(NumVal("-"), num...)
}
} else {
$$ = &UnaryExpr{Operator: UMinusStr, Expr: $2}
}
}
| '~' value_expression
{
$$ = &UnaryExpr{Operator: TildaStr, Expr: $2}
}
| INTERVAL value_expression sql_id
{
// This rule prevents the usage of INTERVAL
// as a function. If support is needed for that,
// we'll need to revisit this. The solution
// will be non-trivial because of grammar conflicts.
$$ = &IntervalExpr{Expr: $2, Unit: $3}
}
| table_id openb closeb
{
$$ = &FuncExpr{Name: string($1)}
}
| table_id openb select_expression_list closeb
{
$$ = &FuncExpr{Name: string($1), Exprs: $3}
}
| table_id openb DISTINCT select_expression_list closeb
{
$$ = &FuncExpr{Name: string($1), Distinct: true, Exprs: $4}
}
| IF openb select_expression_list closeb
{
$$ = &FuncExpr{Name: "if", Exprs: $3}
}
| case_expression
{
$$ = $1
}
case_expression:
CASE value_expression_opt when_expression_list else_expression_opt END
{
$$ = &CaseExpr{Expr: $2, Whens: $3, Else: $4}
}
value_expression_opt:
{
$$ = nil
}
| value_expression
{
$$ = $1
}
when_expression_list:
when_expression
{
$$ = []*When{$1}
}
| when_expression_list when_expression
{
$$ = append($1, $2)
}
when_expression:
WHEN boolean_expression THEN value_expression
{
$$ = &When{Cond: $2, Val: $4}
}
else_expression_opt:
{
$$ = nil
}
| ELSE value_expression
{
$$ = $2
}
column_name:
sql_id
{
$$ = &ColName{Name: $1}
}
| table_id '.' sql_id
{
$$ = &ColName{Qualifier: &TableName{Name: $1}, Name: $3}
}
| table_id '.' table_id '.' sql_id
{
$$ = &ColName{Qualifier: &TableName{Qualifier: $1, Name: $3}, Name: $5}
}
value:
STRING
{
$$ = StrVal($1)
}
| NUMBER
{
$$ = NumVal($1)
}
| VALUE_ARG
{
$$ = ValArg($1)
}
| NULL
{
$$ = &NullVal{}
}
group_by_opt:
{
$$ = nil
}
| GROUP BY value_expression_list
{
$$ = $3
}
having_opt:
{
$$ = nil
}
| HAVING boolean_expression
{
$$ = $2
}
order_by_opt:
{
$$ = nil
}
| ORDER BY order_list
{
$$ = $3
}
order_list:
order
{
$$ = OrderBy{$1}
}
| order_list ',' order
{
$$ = append($1, $3)
}
order:
value_expression asc_desc_opt
{
$$ = &Order{Expr: $1, Direction: $2}
}
asc_desc_opt:
{
$$ = AscScr
}
| ASC
{
$$ = AscScr
}
| DESC
{
$$ = DescScr
}
limit_opt:
{
$$ = nil
}
| LIMIT value_expression
{
$$ = &Limit{Rowcount: $2}
}
| LIMIT value_expression ',' value_expression
{
$$ = &Limit{Offset: $2, Rowcount: $4}
}
lock_opt:
{
$$ = ""