forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.y
5497 lines (5125 loc) · 133 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
//
// 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.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
// Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
// Portions Copyright (c) 1994, Regents of the University of California
%{
package parser
import (
"fmt"
"go/constant"
"go/token"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
)
// MaxUint is the maximum value of an uint.
const MaxUint = ^uint(0)
// MaxInt is the maximum value of an int.
const MaxInt = int(MaxUint >> 1)
func unimplemented(sqllex sqlLexer) int {
sqllex.Error("unimplemented")
return 1
}
func unimplementedWithIssue(sqllex sqlLexer, issue int) int {
sqllex.Error(fmt.Sprintf("unimplemented "+
"(see issue https://github.com/cockroachdb/cockroach/issues/%d)",
issue))
return 1
}
%}
%{
// sqlSymUnion represents a union of types, providing accessor methods
// to retrieve the underlying type stored in the union's empty interface.
// The purpose of the sqlSymUnion struct is to reduce the memory footprint of
// the sqlSymType because only one value (of a variety of types) is ever needed
// to be stored in the union field at a time.
//
// By using an empty interface, we lose the type checking previously provided
// by yacc and the Go compiler when dealing with union values. Instead, runtime
// type assertions must be relied upon in the methods below, and as such, the
// parser should be thoroughly tested whenever new syntax is added.
//
// It is important to note that when assigning values to sqlSymUnion.val, all
// nil values should be typed so that they are stored as nil instances in the
// empty interface, instead of setting the empty interface to nil. This means
// that:
// $$ = []String(nil)
// should be used, instead of:
// $$ = nil
// to assign a nil string slice to the union.
type sqlSymUnion struct {
val interface{}
}
// The following accessor methods come in three forms, depending on the
// type of the value being accessed and whether a nil value is admissible
// for the corresponding grammar rule.
// - Values and pointers are directly type asserted from the empty
// interface, regardless of whether a nil value is admissible or
// not. A panic occurs if the type assertion is incorrect; no panic occurs
// if a nil is not expected but present. (TODO(knz): split this category of
// accessor in two; with one checking for unexpected nils.)
// Examples: bool(), tableWithIdx().
//
// - Interfaces where a nil is admissible are handled differently
// because a nil instance of an interface inserted into the empty interface
// becomes a nil instance of the empty interface and therefore will fail a
// direct type assertion. Instead, a guarded type assertion must be used,
// which returns nil if the type assertion fails.
// Examples: expr(), stmt().
//
// - Interfaces where a nil is not admissible are implemented as a direct
// type assertion, which causes a panic to occur if an unexpected nil
// is encountered.
// Examples: namePart(), tblDef().
//
func (u *sqlSymUnion) numVal() *NumVal {
return u.val.(*NumVal)
}
func (u *sqlSymUnion) strVal() *StrVal {
if stmt, ok := u.val.(*StrVal); ok {
return stmt
}
return nil
}
func (u *sqlSymUnion) bool() bool {
return u.val.(bool)
}
func (u *sqlSymUnion) strPtr() *string {
return u.val.(*string)
}
func (u *sqlSymUnion) strs() []string {
return u.val.([]string)
}
func (u *sqlSymUnion) tableWithIdx() *TableNameWithIndex {
return u.val.(*TableNameWithIndex)
}
func (u *sqlSymUnion) tableWithIdxList() TableNameWithIndexList {
return u.val.(TableNameWithIndexList)
}
func (u *sqlSymUnion) namePart() NamePart {
return u.val.(NamePart)
}
func (u *sqlSymUnion) nameList() NameList {
return u.val.(NameList)
}
func (u *sqlSymUnion) unresolvedName() UnresolvedName {
return u.val.(UnresolvedName)
}
func (u *sqlSymUnion) unresolvedNames() UnresolvedNames {
return u.val.(UnresolvedNames)
}
func (u *sqlSymUnion) functionReference() FunctionReference {
return u.val.(FunctionReference)
}
func (u *sqlSymUnion) resolvableFunctionReference() ResolvableFunctionReference {
return ResolvableFunctionReference{u.val.(FunctionReference)}
}
func (u *sqlSymUnion) normalizableTableName() NormalizableTableName {
return NormalizableTableName{u.val.(TableNameReference)}
}
func (u *sqlSymUnion) newNormalizableTableName() *NormalizableTableName {
return &NormalizableTableName{u.val.(TableNameReference)}
}
func (u *sqlSymUnion) tablePatterns() TablePatterns {
return u.val.(TablePatterns)
}
func (u *sqlSymUnion) tableNameReferences() TableNameReferences {
return u.val.(TableNameReferences)
}
func (u *sqlSymUnion) indexHints() *IndexHints {
return u.val.(*IndexHints)
}
func (u *sqlSymUnion) arraySubscript() *ArraySubscript {
return u.val.(*ArraySubscript)
}
func (u *sqlSymUnion) arraySubscripts() ArraySubscripts {
if as, ok := u.val.(ArraySubscripts); ok {
return as
}
return nil
}
func (u *sqlSymUnion) stmt() Statement {
if stmt, ok := u.val.(Statement); ok {
return stmt
}
return nil
}
func (u *sqlSymUnion) stmts() []Statement {
return u.val.([]Statement)
}
func (u *sqlSymUnion) slct() *Select {
return u.val.(*Select)
}
func (u *sqlSymUnion) selectStmt() SelectStatement {
return u.val.(SelectStatement)
}
func (u *sqlSymUnion) colDef() *ColumnTableDef {
return u.val.(*ColumnTableDef)
}
func (u *sqlSymUnion) constraintDef() ConstraintTableDef {
return u.val.(ConstraintTableDef)
}
func (u *sqlSymUnion) tblDef() TableDef {
return u.val.(TableDef)
}
func (u *sqlSymUnion) tblDefs() TableDefs {
return u.val.(TableDefs)
}
func (u *sqlSymUnion) colQual() NamedColumnQualification {
return u.val.(NamedColumnQualification)
}
func (u *sqlSymUnion) colQualElem() ColumnQualification {
return u.val.(ColumnQualification)
}
func (u *sqlSymUnion) colQuals() []NamedColumnQualification {
return u.val.([]NamedColumnQualification)
}
func (u *sqlSymUnion) colType() ColumnType {
if colType, ok := u.val.(ColumnType); ok {
return colType
}
return nil
}
func (u *sqlSymUnion) tableRefCols() []ColumnID {
if refCols, ok := u.val.([]ColumnID); ok {
return refCols
}
return nil
}
func (u *sqlSymUnion) castTargetType() CastTargetType {
return u.val.(CastTargetType)
}
func (u *sqlSymUnion) colTypes() []ColumnType {
return u.val.([]ColumnType)
}
func (u *sqlSymUnion) expr() Expr {
if expr, ok := u.val.(Expr); ok {
return expr
}
return nil
}
func (u *sqlSymUnion) exprs() Exprs {
return u.val.(Exprs)
}
func (u *sqlSymUnion) selExpr() SelectExpr {
return u.val.(SelectExpr)
}
func (u *sqlSymUnion) selExprs() SelectExprs {
return u.val.(SelectExprs)
}
func (u *sqlSymUnion) retClause() ReturningClause {
return u.val.(ReturningClause)
}
func (u *sqlSymUnion) aliasClause() AliasClause {
return u.val.(AliasClause)
}
func (u *sqlSymUnion) asOfClause() AsOfClause {
return u.val.(AsOfClause)
}
func (u *sqlSymUnion) tblExpr() TableExpr {
return u.val.(TableExpr)
}
func (u *sqlSymUnion) tblExprs() TableExprs {
return u.val.(TableExprs)
}
func (u *sqlSymUnion) from() *From {
return u.val.(*From)
}
func (u *sqlSymUnion) joinCond() JoinCond {
return u.val.(JoinCond)
}
func (u *sqlSymUnion) when() *When {
return u.val.(*When)
}
func (u *sqlSymUnion) whens() []*When {
return u.val.([]*When)
}
func (u *sqlSymUnion) updateExpr() *UpdateExpr {
return u.val.(*UpdateExpr)
}
func (u *sqlSymUnion) updateExprs() UpdateExprs {
return u.val.(UpdateExprs)
}
func (u *sqlSymUnion) limit() *Limit {
return u.val.(*Limit)
}
func (u *sqlSymUnion) targetList() TargetList {
return u.val.(TargetList)
}
func (u *sqlSymUnion) targetListPtr() *TargetList {
return u.val.(*TargetList)
}
func (u *sqlSymUnion) privilegeType() privilege.Kind {
return u.val.(privilege.Kind)
}
func (u *sqlSymUnion) privilegeList() privilege.List {
return u.val.(privilege.List)
}
func (u *sqlSymUnion) onConflict() *OnConflict {
return u.val.(*OnConflict)
}
func (u *sqlSymUnion) orderBy() OrderBy {
return u.val.(OrderBy)
}
func (u *sqlSymUnion) order() *Order {
return u.val.(*Order)
}
func (u *sqlSymUnion) orders() []*Order {
return u.val.([]*Order)
}
func (u *sqlSymUnion) groupBy() GroupBy {
return u.val.(GroupBy)
}
func (u *sqlSymUnion) dir() Direction {
return u.val.(Direction)
}
func (u *sqlSymUnion) alterTableCmd() AlterTableCmd {
return u.val.(AlterTableCmd)
}
func (u *sqlSymUnion) alterTableCmds() AlterTableCmds {
return u.val.(AlterTableCmds)
}
func (u *sqlSymUnion) isoLevel() IsolationLevel {
return u.val.(IsolationLevel)
}
func (u *sqlSymUnion) userPriority() UserPriority {
return u.val.(UserPriority)
}
func (u *sqlSymUnion) idxElem() IndexElem {
return u.val.(IndexElem)
}
func (u *sqlSymUnion) idxElems() IndexElemList {
return u.val.(IndexElemList)
}
func (u *sqlSymUnion) dropBehavior() DropBehavior {
return u.val.(DropBehavior)
}
func (u *sqlSymUnion) validationBehavior() ValidationBehavior {
return u.val.(ValidationBehavior)
}
func (u *sqlSymUnion) interleave() *InterleaveDef {
return u.val.(*InterleaveDef)
}
func (u *sqlSymUnion) windowDef() *WindowDef {
return u.val.(*WindowDef)
}
func (u *sqlSymUnion) window() Window {
return u.val.(Window)
}
func (u *sqlSymUnion) op() operator {
return u.val.(operator)
}
func (u *sqlSymUnion) cmpOp() ComparisonOperator {
return u.val.(ComparisonOperator)
}
func (u *sqlSymUnion) durationField() durationField {
return u.val.(durationField)
}
func (u *sqlSymUnion) kvOption() KVOption {
return u.val.(KVOption)
}
func (u *sqlSymUnion) kvOptions() []KVOption {
if colType, ok := u.val.([]KVOption); ok {
return colType
}
return nil
}
%}
%union {
id int
pos int
empty struct{}
str string
union sqlSymUnion
}
%type <[]Statement> stmt_block
%type <[]Statement> stmt_list
%type <Statement> stmt
%type <Statement> alter_table_stmt
%type <Statement> backup_stmt
%type <Statement> copy_from_stmt
%type <Statement> create_stmt
%type <Statement> create_database_stmt
%type <Statement> create_index_stmt
%type <Statement> create_table_stmt
%type <Statement> create_table_as_stmt
%type <Statement> create_user_stmt
%type <Statement> create_view_stmt
%type <Statement> delete_stmt
%type <Statement> drop_stmt
%type <Statement> explain_stmt
%type <Statement> explainable_stmt
%type <Statement> help_stmt
%type <Statement> prepare_stmt
%type <Statement> preparable_stmt
%type <Statement> execute_stmt
%type <Statement> deallocate_stmt
%type <Statement> grant_stmt
%type <Statement> insert_stmt
%type <Statement> release_stmt
%type <Statement> rename_stmt
%type <Statement> reset_stmt
%type <Statement> revoke_stmt
%type <*Select> select_stmt
%type <Statement> savepoint_stmt
%type <Statement> set_stmt
%type <Statement> show_stmt
%type <Statement> split_stmt
%type <Statement> testing_relocate_stmt
%type <Statement> scatter_stmt
%type <Statement> transaction_stmt
%type <Statement> truncate_stmt
%type <Statement> update_stmt
%type <[]string> opt_incremental
%type <KVOption> kv_option
%type <[]KVOption> kv_option_list opt_with_options
%type <str> opt_equal_value
%type <*Select> select_no_parens
%type <SelectStatement> select_clause select_with_parens simple_select values_clause
%type <empty> alter_using
%type <Expr> alter_column_default
%type <Direction> opt_asc_desc
%type <AlterTableCmd> alter_table_cmd
%type <AlterTableCmds> alter_table_cmds
%type <empty> opt_collate_clause
%type <DropBehavior> opt_drop_behavior
%type <DropBehavior> opt_interleave_drop_behavior
%type <ValidationBehavior> opt_validate_behavior
%type <str> opt_template_clause opt_encoding_clause opt_lc_collate_clause opt_lc_ctype_clause
%type <*string> opt_password
%type <IsolationLevel> transaction_iso_level
%type <UserPriority> transaction_user_priority
%type <str> name opt_name opt_name_parens opt_to_savepoint
%type <str> savepoint_name
%type <operator> subquery_op
%type <FunctionReference> func_name
%type <empty> opt_collate
%type <UnresolvedName> qualified_name
%type <UnresolvedName> table_pattern
%type <TableExpr> insert_target
%type <*TableNameWithIndex> table_name_with_index
%type <TableNameWithIndexList> table_name_with_index_list
%type <operator> math_op
%type <IsolationLevel> iso_level
%type <UserPriority> user_priority
%type <empty> opt_encoding
%type <TableDefs> opt_table_elem_list table_elem_list
%type <*InterleaveDef> opt_interleave
%type <empty> opt_all_clause
%type <bool> distinct_clause
%type <NameList> opt_column_list
%type <OrderBy> sort_clause opt_sort_clause
%type <[]*Order> sortby_list
%type <IndexElemList> index_params
%type <NameList> name_list opt_name_list
%type <Exprs> opt_array_bounds
%type <*From> from_clause update_from_clause
%type <TableExprs> from_list
%type <UnresolvedNames> qualified_name_list
%type <TablePatterns> table_pattern_list
%type <UnresolvedName> any_name
%type <TableNameReferences> table_name_list
%type <Exprs> expr_list
%type <UnresolvedName> attrs
%type <SelectExprs> target_list
%type <UpdateExprs> set_clause_list
%type <*UpdateExpr> set_clause multiple_set_clause
%type <ArraySubscripts> array_subscripts
%type <UnresolvedName> qname_indirection
%type <NamePart> name_indirection_elem
%type <Exprs> ctext_expr_list ctext_row
%type <GroupBy> group_clause
%type <*Limit> select_limit
%type <TableNameReferences> relation_expr_list
%type <ReturningClause> returning_clause
%type <bool> all_or_distinct
%type <empty> join_outer
%type <JoinCond> join_qual
%type <str> join_type
%type <Exprs> extract_list
%type <Exprs> overlay_list
%type <Exprs> position_list
%type <Exprs> substr_list
%type <Exprs> trim_list
%type <Exprs> execute_param_clause
%type <durationField> opt_interval interval_second
%type <Expr> overlay_placing
%type <bool> opt_unique opt_column
%type <empty> opt_set_data
%type <*Limit> limit_clause offset_clause
%type <Expr> select_limit_value
// %type <empty> opt_select_fetch_first_value
%type <empty> row_or_rows
// %type <empty> first_or_next
%type <Statement> insert_rest
%type <NameList> opt_conf_expr
%type <*OnConflict> on_conflict
%type <Statement> generic_set set_rest set_rest_more transaction_mode_list opt_transaction_mode_list set_exprs_internal
%type <empty> opt_read_write
%type <NameList> opt_storing
%type <*ColumnTableDef> column_def
%type <TableDef> table_elem
%type <Expr> where_clause
%type <NamePart> glob_indirection
%type <NamePart> name_indirection
%type <*ArraySubscript> array_subscript
%type <Expr> opt_slice_bound
%type <*IndexHints> opt_index_hints
%type <*IndexHints> index_hints_param
%type <*IndexHints> index_hints_param_list
%type <Expr> a_expr b_expr c_expr a_expr_const d_expr
%type <Expr> substr_from substr_for
%type <Expr> in_expr
%type <Expr> having_clause
%type <Expr> array_expr
%type <Expr> interval
%type <[]ColumnType> type_list prep_type_clause
%type <Exprs> array_expr_list
%type <Expr> row explicit_row implicit_row
%type <Expr> case_expr case_arg case_default
%type <*When> when_clause
%type <[]*When> when_clause_list
%type <ComparisonOperator> sub_type
%type <Expr> ctext_expr
%type <Expr> numeric_only
%type <AliasClause> alias_clause opt_alias_clause
%type <bool> opt_ordinality
%type <*Order> sortby
%type <IndexElem> index_elem
%type <TableExpr> table_ref
%type <TableExpr> joined_table
%type <UnresolvedName> relation_expr
%type <TableExpr> relation_expr_opt_alias
%type <SelectExpr> target_elem
%type <*UpdateExpr> single_set_clause
%type <AsOfClause> opt_as_of_clause
%type <str> explain_option_name
%type <[]string> explain_option_list
%type <ColumnType> typename simple_typename const_typename
%type <ColumnType> numeric opt_numeric_modifiers
%type <*NumVal> opt_float
%type <ColumnType> character const_character
%type <ColumnType> character_with_length character_without_length
%type <ColumnType> const_datetime const_interval
%type <ColumnType> bit const_bit bit_with_length bit_without_length
%type <ColumnType> character_base
%type <CastTargetType> postgres_oid
%type <CastTargetType> cast_target
%type <str> extract_arg
%type <empty> opt_varying
%type <*NumVal> signed_iconst
%type <Expr> opt_boolean_or_string
%type <Exprs> var_list
%type <UnresolvedName> var_name
%type <str> unrestricted_name type_function_name
%type <str> non_reserved_word
%type <str> non_reserved_word_or_sconst
%type <Expr> var_value
%type <Expr> zone_value
%type <Expr> string_or_placeholder
%type <Expr> string_or_placeholder_list
%type <str> unreserved_keyword type_func_name_keyword
%type <str> col_name_keyword reserved_keyword
%type <ConstraintTableDef> table_constraint constraint_elem
%type <TableDef> index_def
%type <TableDef> family_def
%type <[]NamedColumnQualification> col_qual_list
%type <NamedColumnQualification> col_qualification
%type <ColumnQualification> col_qualification_elem
%type <empty> key_actions key_delete key_match key_update key_action
%type <Expr> func_application func_expr_common_subexpr
%type <Expr> func_expr func_expr_windowless
%type <empty> common_table_expr
%type <empty> with_clause opt_with opt_with_clause
%type <empty> cte_list
%type <empty> within_group_clause
%type <Expr> filter_clause
%type <Exprs> opt_partition_clause
%type <Window> window_clause window_definition_list
%type <*WindowDef> window_definition over_clause window_specification
%type <str> opt_existing_window_name
%type <empty> opt_frame_clause frame_extent frame_bound
%type <[]ColumnID> opt_tableref_col_list tableref_col_list
%type <TargetList> targets
%type <*TargetList> on_privilege_target_clause
%type <NameList> grantee_list for_grantee_clause
%type <privilege.List> privileges privilege_list
%type <privilege.Kind> privilege
// Non-keyword token types.
%token <str> IDENT SCONST BCONST
%token <*NumVal> ICONST FCONST
%token <str> PLACEHOLDER
%token <str> TYPECAST TYPEANNOTATE DOT_DOT
%token <str> LESS_EQUALS GREATER_EQUALS NOT_EQUALS
%token <str> NOT_REGMATCH REGIMATCH NOT_REGIMATCH
%token <str> ERROR
// If you want to make any keyword changes, update the keyword table in
// src/include/parser/kwlist.h and add new keywords to the appropriate one of
// the reserved-or-not-so-reserved keyword lists, below; search this file for
// "Keyword category lists".
// Ordinary key words in alphabetical order.
%token <str> ACTION ADD
%token <str> ALL ALTER ANALYSE ANALYZE AND ANY ANNOTATE_TYPE ARRAY AS ASC
%token <str> ASYMMETRIC AT
%token <str> BACKUP BEGIN BETWEEN BIGINT BIGSERIAL BIT
%token <str> BLOB BOOL BOOLEAN BOTH BY BYTEA BYTES
%token <str> CASCADE CASE CAST CHAR
%token <str> CHARACTER CHARACTERISTICS CHECK
%token <str> CLUSTER COALESCE COLLATE COLLATION COLUMN COLUMNS COMMIT
%token <str> COMMITTED CONCAT CONFLICT CONSTRAINT CONSTRAINTS
%token <str> COPY COVERING CREATE
%token <str> CROSS CUBE CURRENT CURRENT_CATALOG CURRENT_DATE
%token <str> CURRENT_ROLE CURRENT_TIME CURRENT_TIMESTAMP
%token <str> CURRENT_USER CYCLE
%token <str> DATA DATABASE DATABASES DATE DAY DEC DECIMAL DEFAULT
%token <str> DEALLOCATE DEFERRABLE DELETE DESC
%token <str> DISTINCT DO DOUBLE DROP
%token <str> ELSE ENCODING END ESCAPE EXCEPT
%token <str> EXISTS EXECUTE EXPLAIN EXTRACT EXTRACT_DURATION
%token <str> FALSE FAMILY FETCH FILTER FIRST FLOAT FLOORDIV FOLLOWING FOR
%token <str> FORCE_INDEX FOREIGN FROM FULL
%token <str> GRANT GRANTS GREATEST GROUP GROUPING
%token <str> HAVING HELP HIGH HOUR
%token <str> INCREMENTAL IF IFNULL ILIKE IN INTERLEAVE
%token <str> INDEX INDEXES INITIALLY
%token <str> INNER INSERT INT INT2VECTOR INT8 INT64 INTEGER
%token <str> INTERSECT INTERVAL INTO IS ISOLATION
%token <str> JOIN
%token <str> KEY KEYS
%token <str> LATERAL LC_CTYPE LC_COLLATE
%token <str> LEADING LEAST LEFT LEVEL LIKE LIMIT LOCAL
%token <str> LOCALTIME LOCALTIMESTAMP LOW LSHIFT
%token <str> MATCH MINUTE MONTH
%token <str> NAN NAME NAMES NATURAL NEXT NO NO_INDEX_JOIN NORMAL
%token <str> NOT NOTHING NULL NULLIF
%token <str> NULLS NUMERIC
%token <str> OF OFF OFFSET OID ON ONLY OPTIONS OR
%token <str> ORDER ORDINALITY OUT OUTER OVER OVERLAPS OVERLAY
%token <str> PARENT PARTIAL PARTITION PASSWORD PLACING POSITION
%token <str> PRECEDING PRECISION PREPARE PRIMARY PRIORITY
%token <str> RANGE READ REAL RECURSIVE REF REFERENCES
%token <str> REGCLASS REGPROC REGPROCEDURE REGNAMESPACE REGTYPE
%token <str> RENAME REPEATABLE
%token <str> RELEASE RESET RESTORE RESTRICT RETURNING REVOKE RIGHT ROLLBACK ROLLUP
%token <str> ROW ROWS RSHIFT
%token <str> SAVEPOINT SCATTER SEARCH SECOND SELECT
%token <str> SERIAL SERIALIZABLE SESSION SESSION_USER SET SETTING SETTINGS SHOW
%token <str> SIMILAR SIMPLE SMALLINT SMALLSERIAL SNAPSHOT SOME SPLIT SQL
%token <str> START STATUS STDIN STRICT STRING STORING SUBSTRING
%token <str> SYMMETRIC SYSTEM
%token <str> TABLE TABLES TEMPLATE TESTING_RANGES TESTING_RELOCATE TEXT THEN
%token <str> TIME TIMESTAMP TIMESTAMPTZ TO TRAILING TRANSACTION TREAT TRIM TRUE
%token <str> TRUNCATE TYPE
%token <str> UNBOUNDED UNCOMMITTED UNION UNIQUE UNKNOWN
%token <str> UPDATE UPSERT USER USERS USING
%token <str> VALID VALIDATE VALUE VALUES VARCHAR VARIADIC VIEW VARYING
%token <str> WHEN WHERE WINDOW WITH WITHIN WITHOUT WRITE
%token <str> YEAR
%token <str> ZONE
// The grammar thinks these are keywords, but they are not in the kwlist.h list
// and so can never be entered directly. The filter in parser.c creates these
// tokens when required (based on looking one token ahead).
//
// NOT_LA exists so that productions such as NOT LIKE can be given the same
// precedence as LIKE; otherwise they'd effectively have the same precedence as
// NOT, at least with respect to their left-hand subexpression. WITH_LA is
// needed to make the grammar LALR(1).
%token NOT_LA WITH_LA AS_LA
// Precedence: lowest to highest
%nonassoc VALUES // see value_clause
%nonassoc SET // see relation_expr_opt_alias
%left UNION EXCEPT
%left INTERSECT
%left OR
%left AND
%right NOT
%nonassoc IS // IS sets precedence for IS NULL, etc
%nonassoc '<' '>' '=' LESS_EQUALS GREATER_EQUALS NOT_EQUALS
%nonassoc '~' BETWEEN IN LIKE ILIKE SIMILAR NOT_REGMATCH REGIMATCH NOT_REGIMATCH NOT_LA
%nonassoc ESCAPE // ESCAPE must be just above LIKE/ILIKE/SIMILAR
%nonassoc OVERLAPS
%left POSTFIXOP // dummy for postfix OP rules
// To support target_elem without AS, we must give IDENT an explicit priority
// between POSTFIXOP and OP. We can safely assign the same priority to various
// unreserved keywords as needed to resolve ambiguities (this can't have any
// bad effects since obviously the keywords will still behave the same as if
// they weren't keywords). We need to do this for PARTITION, RANGE, ROWS to
// support opt_existing_window_name; and for RANGE, ROWS so that they can
// follow a_expr without creating postfix-operator problems; and for NULL so
// that it can follow b_expr in col_qual_list without creating postfix-operator
// problems.
//
// To support CUBE and ROLLUP in GROUP BY without reserving them, we give them
// an explicit priority lower than '(', so that a rule with CUBE '(' will shift
// rather than reducing a conflicting rule that takes CUBE as a function name.
// Using the same precedence as IDENT seems right for the reasons given above.
//
// The frame_bound productions UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING are
// even messier: since UNBOUNDED is an unreserved keyword (per spec!), there is
// no principled way to distinguish these from the productions a_expr
// PRECEDING/FOLLOWING. We hack this up by giving UNBOUNDED slightly lower
// precedence than PRECEDING and FOLLOWING. At present this doesn't appear to
// cause UNBOUNDED to be treated differently from other unreserved keywords
// anywhere else in the grammar, but it's definitely risky. We can blame any
// funny behavior of UNBOUNDED on the SQL standard, though.
%nonassoc UNBOUNDED // ideally should have same precedence as IDENT
%nonassoc IDENT NULL PARTITION RANGE ROWS PRECEDING FOLLOWING CUBE ROLLUP
%left CONCAT // multi-character ops
%left '|'
%left '#'
%left '&'
%left LSHIFT RSHIFT
%left '+' '-'
%left '*' '/' FLOORDIV '%'
%left '^'
// Unary Operators
%left AT // sets precedence for AT TIME ZONE
%left COLLATE
%right UMINUS
%left '[' ']'
%left '(' ')'
%left TYPEANNOTATE
%left TYPECAST
%left '.'
// These might seem to be low-precedence, but actually they are not part
// of the arithmetic hierarchy at all in their use as JOIN operators.
// We make them high-precedence to support their use as function names.
// They wouldn't be given a precedence at all, were it not that we need
// left-associativity among the JOIN rules themselves.
%left JOIN CROSS LEFT FULL RIGHT INNER NATURAL
%%
stmt_block:
stmt_list
{
sqllex.(*Scanner).stmts = $1.stmts()
}
stmt_list:
stmt_list ';' stmt
{
if $3.stmt() != nil {
$$.val = append($1.stmts(), $3.stmt())
}
}
| stmt
{
if $1.stmt() != nil {
$$.val = []Statement{$1.stmt()}
} else {
$$.val = []Statement(nil)
}
}
stmt:
alter_table_stmt
| backup_stmt
| copy_from_stmt
| create_stmt
| delete_stmt
| drop_stmt
| explain_stmt
| help_stmt
| prepare_stmt
| execute_stmt
| deallocate_stmt
| grant_stmt
| insert_stmt
| rename_stmt
| revoke_stmt
| savepoint_stmt
| select_stmt
{
$$.val = $1.slct()
}
| set_stmt
| show_stmt
| split_stmt
| testing_relocate_stmt
| scatter_stmt
| transaction_stmt
| release_stmt
| reset_stmt
| truncate_stmt
| update_stmt
| /* EMPTY */
{
$$.val = Statement(nil)
}
alter_table_stmt:
ALTER TABLE relation_expr alter_table_cmds
{
$$.val = &AlterTable{Table: $3.normalizableTableName(), IfExists: false, Cmds: $4.alterTableCmds()}
}
| ALTER TABLE IF EXISTS relation_expr alter_table_cmds
{
$$.val = &AlterTable{Table: $5.normalizableTableName(), IfExists: true, Cmds: $6.alterTableCmds()}
}
alter_table_cmds:
alter_table_cmd
{
$$.val = AlterTableCmds{$1.alterTableCmd()}
}
| alter_table_cmds ',' alter_table_cmd
{
$$.val = append($1.alterTableCmds(), $3.alterTableCmd())
}
alter_table_cmd:
// ALTER TABLE <name> ADD <coldef>
ADD column_def
{
$$.val = &AlterTableAddColumn{columnKeyword: false, IfNotExists: false, ColumnDef: $2.colDef()}
}
// ALTER TABLE <name> ADD IF NOT EXISTS <coldef>
| ADD IF NOT EXISTS column_def
{
$$.val = &AlterTableAddColumn{columnKeyword: false, IfNotExists: true, ColumnDef: $5.colDef()}
}
// ALTER TABLE <name> ADD COLUMN <coldef>
| ADD COLUMN column_def
{
$$.val = &AlterTableAddColumn{columnKeyword: true, IfNotExists: false, ColumnDef: $3.colDef()}
}
// ALTER TABLE <name> ADD COLUMN IF NOT EXISTS <coldef>
| ADD COLUMN IF NOT EXISTS column_def
{
$$.val = &AlterTableAddColumn{columnKeyword: true, IfNotExists: true, ColumnDef: $6.colDef()}
}
// ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT}
| ALTER opt_column name alter_column_default
{
$$.val = &AlterTableSetDefault{columnKeyword: $2.bool(), Column: Name($3), Default: $4.expr()}
}
// ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL
| ALTER opt_column name DROP NOT NULL
{
$$.val = &AlterTableDropNotNull{columnKeyword: $2.bool(), Column: Name($3)}
}
// ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL
| ALTER opt_column name SET NOT NULL { return unimplemented(sqllex) }
// ALTER TABLE <name> DROP [COLUMN] IF EXISTS <colname> [RESTRICT|CASCADE]
| DROP opt_column IF EXISTS name opt_drop_behavior
{
$$.val = &AlterTableDropColumn{
columnKeyword: $2.bool(),
IfExists: true,
Column: Name($5),
DropBehavior: $6.dropBehavior(),
}
}
// ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE]
| DROP opt_column name opt_drop_behavior
{
$$.val = &AlterTableDropColumn{
columnKeyword: $2.bool(),
IfExists: false,
Column: Name($3),
DropBehavior: $4.dropBehavior(),
}
}
// ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
// [ USING <expression> ]
| ALTER opt_column name opt_set_data TYPE typename opt_collate_clause alter_using { return unimplemented(sqllex) }
// ALTER TABLE <name> ADD CONSTRAINT ...
| ADD table_constraint opt_validate_behavior
{
$$.val = &AlterTableAddConstraint{
ConstraintDef: $2.constraintDef(),
ValidationBehavior: $3.validationBehavior(),
}
}
// ALTER TABLE <name> ALTER CONSTRAINT ...
| ALTER CONSTRAINT name { return unimplemented(sqllex) }
// ALTER TABLE <name> VALIDATE CONSTRAINT ...
| VALIDATE CONSTRAINT name
{
$$.val = &AlterTableValidateConstraint{
Constraint: Name($3),
}
}
// ALTER TABLE <name> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE]
| DROP CONSTRAINT IF EXISTS name opt_drop_behavior
{
$$.val = &AlterTableDropConstraint{
IfExists: true,
Constraint: Name($5),
DropBehavior: $6.dropBehavior(),
}
}
// ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE]
| DROP CONSTRAINT name opt_drop_behavior
{
$$.val = &AlterTableDropConstraint{
IfExists: false,
Constraint: Name($3),
DropBehavior: $4.dropBehavior(),
}
}
alter_column_default:
SET DEFAULT a_expr
{
$$.val = $3.expr()
}
| DROP DEFAULT
{
$$.val = nil
}
opt_drop_behavior:
CASCADE
{
$$.val = DropCascade
}
| RESTRICT
{
$$.val = DropRestrict
}
| /* EMPTY */
{
$$.val = DropDefault
}
opt_validate_behavior:
NOT VALID
{
$$.val = ValidationSkip
}
| /* EMPTY */
{
$$.val = ValidationDefault
}
opt_collate_clause:
COLLATE unrestricted_name { return unimplementedWithIssue(sqllex, 2473) }
| /* EMPTY */ {}
alter_using:
USING a_expr { return unimplemented(sqllex) }
| /* EMPTY */ {}
backup_stmt:
BACKUP targets TO string_or_placeholder opt_as_of_clause opt_incremental opt_with_options
{
$$.val = &Backup{Targets: $2.targetList(), To: $4.expr(), IncrementalFrom: $6.exprs(), AsOf: $5.asOfClause(), Options: $7.kvOptions()}
}
| RESTORE targets FROM string_or_placeholder_list opt_as_of_clause opt_with_options
{
$$.val = &Restore{Targets: $2.targetList(), From: $4.exprs(), AsOf: $5.asOfClause(), Options: $6.kvOptions()}
}
string_or_placeholder: