-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrinoParser.g4
1124 lines (995 loc) · 40.5 KB
/
TrinoParser.g4
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.
*/
// $antlr-format alignTrailingComments true, columnLimit 150, minEmptyLines 1, maxEmptyLinesToKeep 1, reflowComments false, useTab false
// $antlr-format allowShortRulesOnASingleLine false, allowShortBlocksOnASingleLine true, alignSemicolons hanging, alignColons hanging
parser grammar TrinoParser;
options {
tokenVocab = TrinoLexer;
}
// Modified entrypoint
parse
: statements* EOF
;
statements
: singleStatement
| standaloneExpression
| standalonePathSpecification
| standaloneType
| standaloneRowPattern SEMICOLON_?
| standaloneFunctionSpecification
;
singleStatement
: statement SEMICOLON_
;
standaloneExpression
: expression SEMICOLON_
;
standalonePathSpecification
: pathSpecification SEMICOLON_
;
standaloneType
: type SEMICOLON_
;
standaloneRowPattern
: rowPattern SEMICOLON_
;
standaloneFunctionSpecification
: functionSpecification SEMICOLON_
;
statement
: rootQuery # statementDefault
| USE_ schema = identifier # use
| USE_ catalog = identifier DOT_ schema = identifier # use
| CREATE_ CATALOG_ (IF_ NOT_ EXISTS_)? catalog = identifier USING_ connectorName = identifier (
COMMENT_ string_
)? (AUTHORIZATION_ principal)? (WITH_ properties)? # createCatalog
| DROP_ CATALOG_ (IF_ EXISTS_)? catalog = identifier (CASCADE_ | RESTRICT_)? # dropCatalog
| CREATE_ SCHEMA_ (IF_ NOT_ EXISTS_)? qualifiedName (AUTHORIZATION_ principal)? (
WITH_ properties
)? # createSchema
| DROP_ SCHEMA_ (IF_ EXISTS_)? qualifiedName (CASCADE_ | RESTRICT_)? # dropSchema
| ALTER_ SCHEMA_ qualifiedName RENAME_ TO_ identifier # renameSchema
| ALTER_ SCHEMA_ qualifiedName SET_ AUTHORIZATION_ principal # setSchemaAuthorization
| CREATE_ (OR_ REPLACE_)? TABLE_ (IF_ NOT_ EXISTS_)? qualifiedName columnAliases? (
COMMENT_ string_
)? (WITH_ properties)? AS_ (rootQuery | LPAREN_ rootQuery RPAREN_) (WITH_ (NO_)? DATA_)? # createTableAsSelect
| CREATE_ (OR_ REPLACE_)? TABLE_ (IF_ NOT_ EXISTS_)? qualifiedName LPAREN_ tableElement (
COMMA_ tableElement
)* RPAREN_ (COMMENT_ string_)? (WITH_ properties)? # createTable
| DROP_ TABLE_ (IF_ EXISTS_)? qualifiedName # dropTable
| INSERT_ INTO_ qualifiedName columnAliases? rootQuery # insertInto
| DELETE_ FROM_ qualifiedName (WHERE_ booleanExpression)? # delete
| TRUNCATE_ TABLE_ qualifiedName # truncateTable
| COMMENT_ ON_ TABLE_ qualifiedName IS_ (string_ | NULL_) # commentTable
| COMMENT_ ON_ VIEW_ qualifiedName IS_ (string_ | NULL_) # commentView
| COMMENT_ ON_ COLUMN_ qualifiedName IS_ (string_ | NULL_) # commentColumn
| ALTER_ TABLE_ (IF_ EXISTS_)? from = qualifiedName RENAME_ TO_ to = qualifiedName # renameTable
| ALTER_ TABLE_ (IF_ EXISTS_)? tableName = qualifiedName ADD_ COLUMN_ (IF_ NOT_ EXISTS_)? column = columnDefinition # addColumn
| ALTER_ TABLE_ (IF_ EXISTS_)? tableName = qualifiedName RENAME_ COLUMN_ (IF_ EXISTS_)? from = qualifiedName TO_ to = identifier # renameColumn
| ALTER_ TABLE_ (IF_ EXISTS_)? tableName = qualifiedName DROP_ COLUMN_ (IF_ EXISTS_)? column = qualifiedName # dropColumn
| ALTER_ TABLE_ (IF_ EXISTS_)? tableName = qualifiedName ALTER_ COLUMN_ columnName = qualifiedName SET_ DATA_ TYPE_ type # setColumnType
| ALTER_ TABLE_ tableName = qualifiedName SET_ AUTHORIZATION_ principal # setTableAuthorization
| ALTER_ TABLE_ tableName = qualifiedName SET_ PROPERTIES_ propertyAssignments # setTableProperties
| ALTER_ TABLE_ tableName = qualifiedName EXECUTE_ procedureName = identifier (
LPAREN_ (callArgument (COMMA_ callArgument)*)? RPAREN_
)? (WHERE_ where = booleanExpression)? # tableExecute
| ANALYZE_ qualifiedName (WITH_ properties)? # analyze
| CREATE_ (OR_ REPLACE_)? MATERIALIZED_ VIEW_ (IF_ NOT_ EXISTS_)? qualifiedName (
GRACE_ PERIOD_ interval
)? (COMMENT_ string_)? (WITH_ properties)? AS_ rootQuery # createMaterializedView
| CREATE_ (OR_ REPLACE_)? VIEW_ qualifiedName (COMMENT_ string_)? (
SECURITY_ (DEFINER_ | INVOKER_)
)? AS_ rootQuery # createView
| REFRESH_ MATERIALIZED_ VIEW_ qualifiedName # refreshMaterializedView
| DROP_ MATERIALIZED_ VIEW_ (IF_ EXISTS_)? qualifiedName # dropMaterializedView
| ALTER_ MATERIALIZED_ VIEW_ (IF_ EXISTS_)? from = qualifiedName RENAME_ TO_ to = qualifiedName # renameMaterializedView
| ALTER_ MATERIALIZED_ VIEW_ qualifiedName SET_ PROPERTIES_ propertyAssignments # setMaterializedViewProperties
| DROP_ VIEW_ (IF_ EXISTS_)? qualifiedName # dropView
| ALTER_ VIEW_ from = qualifiedName RENAME_ TO_ to = qualifiedName # renameView
| ALTER_ VIEW_ from = qualifiedName SET_ AUTHORIZATION_ principal # setViewAuthorization
| CALL_ qualifiedName LPAREN_ (callArgument (COMMA_ callArgument)*)? RPAREN_ # call
| CREATE_ (OR_ REPLACE_)? functionSpecification # createFunction
| DROP_ FUNCTION_ (IF_ EXISTS_)? functionDeclaration # dropFunction
| CREATE_ ROLE_ name = identifier (WITH_ ADMIN_ grantor)? (IN_ catalog = identifier)? # createRole
| DROP_ ROLE_ name = identifier (IN_ catalog = identifier)? # dropRole
| GRANT_ roles TO_ principal (COMMA_ principal)* (WITH_ ADMIN_ OPTION_)? (GRANTED_ BY_ grantor)? (
IN_ catalog = identifier
)? # grantRoles
| REVOKE_ (ADMIN_ OPTION_ FOR_)? roles FROM_ principal (COMMA_ principal)* (
GRANTED_ BY_ grantor
)? (IN_ catalog = identifier)? # revokeRoles
| SET_ ROLE_ (ALL_ | NONE_ | role = identifier) (IN_ catalog = identifier)? # setRole
| GRANT_ (privilege (COMMA_ privilege)* | ALL_ PRIVILEGES_) ON_ (SCHEMA_ | TABLE_)? qualifiedName TO_ grantee = principal (
WITH_ GRANT_ OPTION_
)? # grant
| DENY_ (privilege (COMMA_ privilege)* | ALL_ PRIVILEGES_) ON_ (SCHEMA_ | TABLE_)? qualifiedName TO_ grantee = principal # deny
| REVOKE_ (GRANT_ OPTION_ FOR_)? (privilege (COMMA_ privilege)* | ALL_ PRIVILEGES_) ON_ (
SCHEMA_
| TABLE_
)? qualifiedName FROM_ grantee = principal # revoke
| SHOW_ GRANTS_ (ON_ TABLE_? qualifiedName)? # showGrants
| EXPLAIN_ (LPAREN_ explainOption (COMMA_ explainOption)* RPAREN_)? statement # explain
| EXPLAIN_ ANALYZE_ VERBOSE_? statement # explainAnalyze
| SHOW_ CREATE_ TABLE_ qualifiedName # showCreateTable
| SHOW_ CREATE_ SCHEMA_ qualifiedName # showCreateSchema
| SHOW_ CREATE_ VIEW_ qualifiedName # showCreateView
| SHOW_ CREATE_ MATERIALIZED_ VIEW_ qualifiedName # showCreateMaterializedView
| SHOW_ TABLES_ ((FROM_ | IN_) qualifiedName)? (
LIKE_ pattern = string_ (ESCAPE_ escape = string_)?
)? # showTables
| SHOW_ SCHEMAS_ ((FROM_ | IN_) identifier)? (
LIKE_ pattern = string_ (ESCAPE_ escape = string_)?
)? # showSchemas
| SHOW_ CATALOGS_ (LIKE_ pattern = string_ (ESCAPE_ escape = string_)?)? # showCatalogs
| SHOW_ COLUMNS_ (FROM_ | IN_) qualifiedName? (
LIKE_ pattern = string_ (ESCAPE_ escape = string_)?
)? # showColumns
| SHOW_ STATS_ FOR_ qualifiedName # showStats
| SHOW_ STATS_ FOR_ LPAREN_ rootQuery RPAREN_ # showStatsForQuery
| SHOW_ CURRENT_? ROLES_ ((FROM_ | IN_) identifier)? # showRoles
| SHOW_ ROLE_ GRANTS_ ((FROM_ | IN_) identifier)? # showRoleGrants
| DESCRIBE_ qualifiedName # showColumns
| DESC_ qualifiedName # showColumns
| SHOW_ FUNCTIONS_ ((FROM_ | IN_) qualifiedName)? (
LIKE_ pattern = string_ (ESCAPE_ escape = string_)?
)? # showFunctions
| SHOW_ SESSION_ (LIKE_ pattern = string_ (ESCAPE_ escape = string_)?)? # showSession
| SET_ SESSION_ AUTHORIZATION_ authorizationUser # setSessionAuthorization
| RESET_ SESSION_ AUTHORIZATION_ # resetSessionAuthorization
| SET_ SESSION_ qualifiedName EQ_ expression # setSession
| RESET_ SESSION_ qualifiedName # resetSession
| START_ TRANSACTION_ (transactionMode (COMMA_ transactionMode)*)? # startTransaction
| COMMIT_ WORK_? # commit
| ROLLBACK_ WORK_? # rollback
| PREPARE_ identifier FROM_ statement # prepare
| DEALLOCATE_ PREPARE_ identifier # deallocate
| EXECUTE_ identifier (USING_ expression (COMMA_ expression)*)? # execute
| EXECUTE_ IMMEDIATE_ string_ (USING_ expression (COMMA_ expression)*)? # executeImmediate
| DESCRIBE_ INPUT_ identifier # describeInput
| DESCRIBE_ OUTPUT_ identifier # describeOutput
| SET_ PATH_ pathSpecification # setPath
| SET_ TIME_ ZONE_ (LOCAL_ | expression) # setTimeZone
| UPDATE_ qualifiedName SET_ updateAssignment (COMMA_ updateAssignment)* (
WHERE_ where = booleanExpression
)? # update
| MERGE_ INTO_ qualifiedName (AS_? identifier)? USING_ relation ON_ expression mergeCase+ # merge
;
rootQuery
: withFunction? query
;
withFunction
: WITH_ functionSpecification (COMMA_ functionSpecification)*
;
query
: with? queryNoWith
;
with
: WITH_ RECURSIVE_? namedQuery (COMMA_ namedQuery)*
;
tableElement
: columnDefinition
| likeClause
;
columnDefinition
: identifier type (NOT_ NULL_)? (COMMENT_ string_)? (WITH_ properties)?
;
likeClause
: LIKE_ qualifiedName (optionType = (INCLUDING_ | EXCLUDING_) PROPERTIES_)?
;
properties
: LPAREN_ propertyAssignments RPAREN_
;
propertyAssignments
: property (COMMA_ property)*
;
property
: identifier EQ_ propertyValue
;
propertyValue
: DEFAULT_ # defaultPropertyValue
| expression # nonDefaultPropertyValue
;
queryNoWith
: queryTerm (ORDER_ BY_ sortItem (COMMA_ sortItem)*)? (
OFFSET_ offset = rowCount (ROW_ | ROWS_)?
)? (
LIMIT_ limit = limitRowCount
| FETCH_ (FIRST_ | NEXT_) (fetchFirst = rowCount)? (ROW_ | ROWS_) (ONLY_ | WITH_ TIES_)
)?
;
limitRowCount
: ALL_
| rowCount
;
rowCount
: INTEGER_VALUE_
| QUESTION_MARK_
;
queryTerm
: queryPrimary # queryTermDefault
| left = queryTerm operator = INTERSECT_ setQuantifier? right = queryTerm # setOperation
| left = queryTerm operator = (UNION_ | EXCEPT_) setQuantifier? right = queryTerm # setOperation
;
queryPrimary
: querySpecification # queryPrimaryDefault
| TABLE_ qualifiedName # table
| VALUES_ expression (COMMA_ expression)* # inlineTable
| LPAREN_ queryNoWith RPAREN_ # subquery
;
sortItem
: expression ordering = (ASC_ | DESC_)? (NULLS_ nullOrdering = (FIRST_ | LAST_))?
;
querySpecification
: SELECT_ setQuantifier? selectItem (COMMA_ selectItem)* (FROM_ relation (COMMA_ relation)*)? (
WHERE_ where = booleanExpression
)? (GROUP_ BY_ groupBy)? (HAVING_ having = booleanExpression)? (
WINDOW_ windowDefinition (COMMA_ windowDefinition)*
)?
;
groupBy
: setQuantifier? groupingElement (COMMA_ groupingElement)*
;
groupingElement
: groupingSet # singleGroupingSet
| ROLLUP_ LPAREN_ (expression (COMMA_ expression)*)? RPAREN_ # rollup
| CUBE_ LPAREN_ (expression (COMMA_ expression)*)? RPAREN_ # cube
| GROUPING_ SETS_ LPAREN_ groupingSet (COMMA_ groupingSet)* RPAREN_ # multipleGroupingSets
;
groupingSet
: LPAREN_ (expression (COMMA_ expression)*)? RPAREN_
| expression
;
windowDefinition
: name = identifier AS_ LPAREN_ windowSpecification RPAREN_
;
windowSpecification
: (existingWindowName = identifier)? (
PARTITION_ BY_ partition += expression (COMMA_ partition += expression)*
)? (ORDER_ BY_ sortItem (COMMA_ sortItem)*)? windowFrame?
;
namedQuery
: name = identifier (columnAliases)? AS_ LPAREN_ query RPAREN_
;
setQuantifier
: DISTINCT_
| ALL_
;
selectItem
: expression (AS_? identifier)? # selectSingle
| primaryExpression DOT_ ASTERISK_ (AS_ columnAliases)? # selectAll
| ASTERISK_ # selectAll
;
relation
: left = relation (
CROSS_ JOIN_ right = sampledRelation
| joinType JOIN_ rightRelation = relation joinCriteria
| NATURAL_ joinType JOIN_ right = sampledRelation
) # joinRelation
| sampledRelation # relationDefault
;
joinType
: INNER_?
| (LEFT_ | RIGHT_ | FULL_) OUTER_?
;
joinCriteria
: ON_ booleanExpression
| USING_ LPAREN_ identifier (COMMA_ identifier)* RPAREN_
;
sampledRelation
: patternRecognition (TABLESAMPLE_ sampleType LPAREN_ percentage = expression RPAREN_)?
;
sampleType
: BERNOULLI_
| SYSTEM_
;
trimsSpecification
: LEADING_
| TRAILING_
| BOTH_
;
listAggOverflowBehavior
: ERROR_
| TRUNCATE_ string_? listaggCountIndication
;
listaggCountIndication
: (WITH_ | WITHOUT_) COUNT_
;
patternRecognition
: aliasedRelation (
MATCH_RECOGNIZE_ LPAREN_ (
PARTITION_ BY_ partition += expression (COMMA_ partition += expression)*
)? (ORDER_ BY_ sortItem (COMMA_ sortItem)*)? (
MEASURES_ measureDefinition (COMMA_ measureDefinition)*
)? rowsPerMatch? (AFTER_ MATCH_ skipTo)? (INITIAL_ | SEEK_)? PATTERN_ LPAREN_ rowPattern RPAREN_ (
SUBSET_ subsetDefinition (COMMA_ subsetDefinition)*
)? DEFINE_ variableDefinition (COMMA_ variableDefinition)* RPAREN_ (
AS_? identifier columnAliases?
)?
)?
;
measureDefinition
: expression AS_ identifier
;
rowsPerMatch
: ONE_ ROW_ PER_ MATCH_
| ALL_ ROWS_ PER_ MATCH_ emptyMatchHandling?
;
emptyMatchHandling
: SHOW_ EMPTY_ MATCHES_
| OMIT_ EMPTY_ MATCHES_
| WITH_ UNMATCHED_ ROWS_
;
skipTo
: SKIP_ (TO_ (NEXT_ ROW_ | (FIRST_ | LAST_)? identifier) | PAST_ LAST_ ROW_)
;
subsetDefinition
: name = identifier EQ_ LPAREN_ union += identifier (COMMA_ union += identifier)* RPAREN_
;
variableDefinition
: identifier AS_ expression
;
aliasedRelation
: relationPrimary (AS_? identifier columnAliases?)?
;
columnAliases
: LPAREN_ identifier (COMMA_ identifier)* RPAREN_
;
relationPrimary
: qualifiedName queryPeriod? # tableName
| LPAREN_ query RPAREN_ # subqueryRelation
| UNNEST_ LPAREN_ expression (COMMA_ expression)* RPAREN_ (WITH_ ORDINALITY_)? # unnest
| LATERAL_ LPAREN_ query RPAREN_ # lateral
| TABLE_ LPAREN_ tableFunctionCall RPAREN_ # tableFunctionInvocation
| LPAREN_ relation RPAREN_ # parenthesizedRelation
;
tableFunctionCall
: qualifiedName LPAREN_ (tableFunctionArgument (COMMA_ tableFunctionArgument)*)? (
COPARTITION_ copartitionTables (COMMA_ copartitionTables)*
)? RPAREN_
;
tableFunctionArgument
: (identifier RDOUBLEARROW_)? (
tableArgument
| descriptorArgument
| expression
) // descriptor before expression to avoid parsing descriptor as a function call
;
tableArgument
: tableArgumentRelation (
PARTITION_ BY_ (LPAREN_ (expression (COMMA_ expression)*)? RPAREN_ | expression)
)? (PRUNE_ WHEN_ EMPTY_ | KEEP_ WHEN_ EMPTY_)? (
ORDER_ BY_ (LPAREN_ sortItem (COMMA_ sortItem)* RPAREN_ | sortItem)
)?
;
tableArgumentRelation
: TABLE_ LPAREN_ qualifiedName RPAREN_ (AS_? identifier columnAliases?)? # tableArgumentTable
| TABLE_ LPAREN_ query RPAREN_ (AS_? identifier columnAliases?)? # tableArgumentQuery
;
descriptorArgument
: DESCRIPTOR_ LPAREN_ descriptorField (COMMA_ descriptorField)* RPAREN_
| CAST_ LPAREN_ NULL_ AS_ DESCRIPTOR_ RPAREN_
;
descriptorField
: identifier type?
;
copartitionTables
: LPAREN_ qualifiedName COMMA_ qualifiedName (COMMA_ qualifiedName)* RPAREN_
;
expression
: booleanExpression
;
booleanExpression
: valueExpression predicate_? # predicated
| NOT_ booleanExpression # logicalNot
| booleanExpression AND_ booleanExpression # and
| booleanExpression OR_ booleanExpression # or
;
// workaround for https://github.com/antlr/antlr4/issues/780
predicate_
: comparisonOperator right = valueExpression # comparison
| comparisonOperator comparisonQuantifier LPAREN_ query RPAREN_ # quantifiedComparison
| NOT_? BETWEEN_ lower = valueExpression AND_ upper = valueExpression # between
| NOT_? IN_ LPAREN_ expression (COMMA_ expression)* RPAREN_ # inList
| NOT_? IN_ LPAREN_ query RPAREN_ # inSubquery
| NOT_? LIKE_ pattern = valueExpression (ESCAPE_ escape = valueExpression)? # like
| IS_ NOT_? NULL_ # nullPredicate
| IS_ NOT_? DISTINCT_ FROM_ right = valueExpression # distinctFrom
;
valueExpression
: primaryExpression # valueExpressionDefault
| valueExpression AT_ timeZoneSpecifier # atTimeZone
| operator = (MINUS_ | PLUS_) valueExpression # arithmeticUnary
| left = valueExpression operator = (ASTERISK_ | SLASH_ | PERCENT_) right = valueExpression # arithmeticBinary
| left = valueExpression operator = (PLUS_ | MINUS_) right = valueExpression # arithmeticBinary
| left = valueExpression CONCAT_ right = valueExpression # concatenation
;
primaryExpression
: NULL_ # nullLiteral
| interval # intervalLiteral
| identifier string_ # typeConstructor
| DOUBLE_ PRECISION_ string_ # typeConstructor
| number # numericLiteral
| booleanValue # booleanLiteral
| string_ # stringLiteral
| BINARY_LITERAL_ # binaryLiteral
| QUESTION_MARK_ # parameter
| POSITION_ LPAREN_ valueExpression IN_ valueExpression RPAREN_ # position
| LPAREN_ expression (COMMA_ expression)+ RPAREN_ # rowConstructor
| ROW_ LPAREN_ expression (COMMA_ expression)* RPAREN_ # rowConstructor
| name = LISTAGG_ LPAREN_ setQuantifier? expression (COMMA_ string_)? (
ON_ OVERFLOW_ listAggOverflowBehavior
)? RPAREN_ (WITHIN_ GROUP_ LPAREN_ ORDER_ BY_ sortItem (COMMA_ sortItem)* RPAREN_) filter? # listagg
| processingMode? qualifiedName LPAREN_ (label = identifier DOT_)? ASTERISK_ RPAREN_ filter? over? # functionCall
| processingMode? qualifiedName LPAREN_ (setQuantifier? expression (COMMA_ expression)*)? (
ORDER_ BY_ sortItem (COMMA_ sortItem)*
)? RPAREN_ filter? (nullTreatment? over)? # functionCall
| identifier over # measure
| identifier RARROW_ expression # lambda
| LPAREN_ (identifier (COMMA_ identifier)*)? RPAREN_ RARROW_ expression # lambda
| LPAREN_ query RPAREN_ # subqueryExpression
// This is an extension to ANSI_ SQL, which considers EXISTS_ to be a <boolean expression>
| EXISTS_ LPAREN_ query RPAREN_ # exists
| CASE_ operand = expression whenClause+ (ELSE_ elseExpression = expression)? END_ # simpleCase
| CASE_ whenClause+ (ELSE_ elseExpression = expression)? END_ # searchedCase
| CAST_ LPAREN_ expression AS_ type RPAREN_ # cast
| TRY_CAST_ LPAREN_ expression AS_ type RPAREN_ # cast
| ARRAY_ LSQUARE_ (expression (COMMA_ expression)*)? RSQUARE_ # arrayConstructor
| value = primaryExpression LSQUARE_ index = valueExpression RSQUARE_ # subscript
| identifier # columnReference
| base_ = primaryExpression DOT_ fieldName = identifier # dereference
| name = CURRENT_DATE_ # specialDateTimeFunction
| name = CURRENT_TIME_ (LPAREN_ precision = INTEGER_VALUE_ RPAREN_)? # specialDateTimeFunction
| name = CURRENT_TIMESTAMP_ (LPAREN_ precision = INTEGER_VALUE_ RPAREN_)? # specialDateTimeFunction
| name = LOCALTIME_ (LPAREN_ precision = INTEGER_VALUE_ RPAREN_)? # specialDateTimeFunction
| name = LOCALTIMESTAMP_ (LPAREN_ precision = INTEGER_VALUE_ RPAREN_)? # specialDateTimeFunction
| name = CURRENT_USER_ # currentUser
| name = CURRENT_CATALOG_ # currentCatalog
| name = CURRENT_SCHEMA_ # currentSchema
| name = CURRENT_PATH_ # currentPath
| TRIM_ LPAREN_ (trimsSpecification? trimChar = valueExpression? FROM_)? trimSource = valueExpression RPAREN_ # trim
| TRIM_ LPAREN_ trimSource = valueExpression COMMA_ trimChar = valueExpression RPAREN_ # trim
| SUBSTRING_ LPAREN_ valueExpression FROM_ valueExpression (FOR_ valueExpression)? RPAREN_ # substring
| NORMALIZE_ LPAREN_ valueExpression (COMMA_ normalForm)? RPAREN_ # normalize
| EXTRACT_ LPAREN_ identifier FROM_ valueExpression RPAREN_ # extract
| LPAREN_ expression RPAREN_ # parenthesizedExpression
| GROUPING_ LPAREN_ (qualifiedName (COMMA_ qualifiedName)*)? RPAREN_ # groupingOperation
| JSON_EXISTS_ LPAREN_ jsonPathInvocation (jsonExistsErrorBehavior ON_ ERROR_)? RPAREN_ # jsonExists
| JSON_VALUE_ LPAREN_ jsonPathInvocation (RETURNING_ type)? (
emptyBehavior = jsonValueBehavior ON_ EMPTY_
)? (errorBehavior = jsonValueBehavior ON_ ERROR_)? RPAREN_ # jsonValue
| JSON_QUERY_ LPAREN_ jsonPathInvocation (RETURNING_ type (FORMAT_ jsonRepresentation)?)? (
jsonQueryWrapperBehavior WRAPPER_
)? ((KEEP_ | OMIT_) QUOTES_ (ON_ SCALAR_ TEXT_STRING_)?)? (
emptyBehavior = jsonQueryBehavior ON_ EMPTY_
)? (errorBehavior = jsonQueryBehavior ON_ ERROR_)? RPAREN_ # jsonQuery
| JSON_OBJECT_ LPAREN_ (
jsonObjectMember (COMMA_ jsonObjectMember)* (NULL_ ON_ NULL_ | ABSENT_ ON_ NULL_)? (
WITH_ UNIQUE_ KEYS_?
| WITHOUT_ UNIQUE_ KEYS_?
)?
)? (RETURNING_ type (FORMAT_ jsonRepresentation)?)? RPAREN_ # jsonObject
| JSON_ARRAY_ LPAREN_ (
jsonValueExpression (COMMA_ jsonValueExpression)* (NULL_ ON_ NULL_ | ABSENT_ ON_ NULL_)?
)? (RETURNING_ type (FORMAT_ jsonRepresentation)?)? RPAREN_ # jsonArray
;
jsonPathInvocation
: jsonValueExpression COMMA_ path = string_ (PASSING_ jsonArgument (COMMA_ jsonArgument)*)?
;
jsonValueExpression
: expression (FORMAT_ jsonRepresentation)?
;
jsonRepresentation
: JSON_ (ENCODING_ (UTF8_ | UTF16_ | UTF32_))? // TODO_ add implementation-defined JSON_ representation option
;
jsonArgument
: jsonValueExpression AS_ identifier
;
jsonExistsErrorBehavior
: TRUE_
| FALSE_
| UNKNOWN_
| ERROR_
;
jsonValueBehavior
: ERROR_
| NULL_
| DEFAULT_ expression
;
jsonQueryWrapperBehavior
: WITHOUT_ ARRAY_?
| WITH_ (CONDITIONAL_ | UNCONDITIONAL_)? ARRAY_?
;
jsonQueryBehavior
: ERROR_
| NULL_
| EMPTY_ (ARRAY_ | OBJECT_)
;
jsonObjectMember
: KEY_? expression VALUE_ jsonValueExpression
| expression COLON_ jsonValueExpression
;
processingMode
: RUNNING_
| FINAL_
;
nullTreatment
: IGNORE_ NULLS_
| RESPECT_ NULLS_
;
// renamed from "string" to avoid golang name conflict
string_
: STRING_ # basicStringLiteral
| UNICODE_STRING_ (UESCAPE_ STRING_)? # unicodeStringLiteral
;
timeZoneSpecifier
: TIME_ ZONE_ interval # timeZoneInterval
| TIME_ ZONE_ string_ # timeZoneString
;
comparisonOperator
: EQ_
| NEQ_
| LT_
| LTE_
| GT_
| GTE_
;
comparisonQuantifier
: ALL_
| SOME_
| ANY_
;
booleanValue
: TRUE_
| FALSE_
;
interval
: INTERVAL_ sign = (PLUS_ | MINUS_)? string_ from = intervalField (TO_ to = intervalField)?
;
intervalField
: YEAR_
| MONTH_
| DAY_
| HOUR_
| MINUTE_
| SECOND_
;
normalForm
: NFD_
| NFC_
| NFKD_
| NFKC_
;
type
: ROW_ LPAREN_ rowField (COMMA_ rowField)* RPAREN_ # rowType
| INTERVAL_ from = intervalField (TO_ to = intervalField)? # intervalType
| base_ = TIMESTAMP_ (LPAREN_ precision = typeParameter RPAREN_)? (WITHOUT_ TIME_ ZONE_)? # dateTimeType
| base_ = TIMESTAMP_ (LPAREN_ precision = typeParameter RPAREN_)? WITH_ TIME_ ZONE_ # dateTimeType
| base_ = TIME_ (LPAREN_ precision = typeParameter RPAREN_)? (WITHOUT_ TIME_ ZONE_)? # dateTimeType
| base_ = TIME_ (LPAREN_ precision = typeParameter RPAREN_)? WITH_ TIME_ ZONE_ # dateTimeType
| DOUBLE_ PRECISION_ # doublePrecisionType
| ARRAY_ LT_ type GT_ # legacyArrayType
| MAP_ LT_ keyType = type COMMA_ valueType = type GT_ # legacyMapType
| type ARRAY_ (LSQUARE_ INTEGER_VALUE_ RSQUARE_)? # arrayType
| identifier (LPAREN_ typeParameter (COMMA_ typeParameter)* RPAREN_)? # genericType
;
rowField
: type
| identifier type
;
typeParameter
: INTEGER_VALUE_
| type
;
whenClause
: WHEN_ condition = expression THEN_ result = expression
;
filter
: FILTER_ LPAREN_ WHERE_ booleanExpression RPAREN_
;
mergeCase
: WHEN_ MATCHED_ (AND_ condition = expression)? THEN_ UPDATE_ SET_ targets += identifier EQ_ values += expression (
COMMA_ targets += identifier EQ_ values += expression
)* # mergeUpdate
| WHEN_ MATCHED_ (AND_ condition = expression)? THEN_ DELETE_ # mergeDelete
| WHEN_ NOT_ MATCHED_ (AND_ condition = expression)? THEN_ INSERT_ (
LPAREN_ targets += identifier (COMMA_ targets += identifier)* RPAREN_
)? VALUES_ LPAREN_ values += expression (COMMA_ values += expression)* RPAREN_ # mergeInsert
;
over
: OVER_ (windowName = identifier | LPAREN_ windowSpecification RPAREN_)
;
windowFrame
: (MEASURES_ measureDefinition (COMMA_ measureDefinition)*)? frameExtent (AFTER_ MATCH_ skipTo)? (
INITIAL_
| SEEK_
)? (PATTERN_ LPAREN_ rowPattern RPAREN_)? (SUBSET_ subsetDefinition (COMMA_ subsetDefinition)*)? (
DEFINE_ variableDefinition (COMMA_ variableDefinition)*
)?
;
// renamed start and stop to avoid Dart name conflict
frameExtent
: frameType = RANGE_ start_ = frameBound
| frameType = ROWS_ start_ = frameBound
| frameType = GROUPS_ start_ = frameBound
| frameType = RANGE_ BETWEEN_ start_ = frameBound AND_ end_ = frameBound
| frameType = ROWS_ BETWEEN_ start_ = frameBound AND_ end_ = frameBound
| frameType = GROUPS_ BETWEEN_ start_ = frameBound AND_ end_ = frameBound
;
frameBound
: UNBOUNDED_ boundType = PRECEDING_ # unboundedFrame
| UNBOUNDED_ boundType = FOLLOWING_ # unboundedFrame
| CURRENT_ ROW_ # currentRowBound
| expression boundType = (PRECEDING_ | FOLLOWING_) # boundedFrame
;
rowPattern
: patternPrimary patternQuantifier? # quantifiedPrimary
| rowPattern rowPattern # patternConcatenation
| rowPattern VBAR_ rowPattern # patternAlternation
;
patternPrimary
: identifier # patternVariable
| LPAREN_ RPAREN_ # emptyPattern
| PERMUTE_ LPAREN_ rowPattern (COMMA_ rowPattern)* RPAREN_ # patternPermutation
| LPAREN_ rowPattern RPAREN_ # groupedPattern
| CARET_ # partitionStartAnchor
| DOLLAR_ # partitionEndAnchor
| LCURLYHYPHEN_ rowPattern RCURLYHYPHEN_ # excludedPattern
;
patternQuantifier
: ASTERISK_ (reluctant = QUESTION_MARK_)? # zeroOrMoreQuantifier
| PLUS_ (reluctant = QUESTION_MARK_)? # oneOrMoreQuantifier
| QUESTION_MARK_ (reluctant = QUESTION_MARK_)? # zeroOrOneQuantifier
| LCURLY_ exactly = INTEGER_VALUE_ RCURLY_ (reluctant = QUESTION_MARK_)? # rangeQuantifier
| LCURLY_ (atLeast = INTEGER_VALUE_)? COMMA_ (atMost = INTEGER_VALUE_)? RCURLY_ (
reluctant = QUESTION_MARK_
)? # rangeQuantifier
;
updateAssignment
: identifier EQ_ expression
;
explainOption
: FORMAT_ value = (TEXT_ | GRAPHVIZ_ | JSON_) # explainFormat
| TYPE_ value = (LOGICAL_ | DISTRIBUTED_ | VALIDATE_ | IO_) # explainType
;
transactionMode
: ISOLATION_ LEVEL_ levelOfIsolation # isolationLevel
| READ_ accessMode = (ONLY_ | WRITE_) # transactionAccessMode
;
levelOfIsolation
: READ_ UNCOMMITTED_ # readUncommitted
| READ_ COMMITTED_ # readCommitted
| REPEATABLE_ READ_ # repeatableRead
| SERIALIZABLE_ # serializable
;
callArgument
: expression # positionalArgument
| identifier RDOUBLEARROW_ expression # namedArgument
;
pathElement
: identifier DOT_ identifier # qualifiedArgument
| identifier # unqualifiedArgument
;
pathSpecification
: pathElement (COMMA_ pathElement)*
;
functionSpecification
: FUNCTION_ functionDeclaration returnsClause routineCharacteristic* controlStatement
;
functionDeclaration
: qualifiedName LPAREN_ (parameterDeclaration (COMMA_ parameterDeclaration)*)? RPAREN_
;
parameterDeclaration
: identifier? type
;
returnsClause
: RETURNS_ type
;
routineCharacteristic
: LANGUAGE_ identifier # languageCharacteristic
| NOT_? DETERMINISTIC_ # deterministicCharacteristic
| RETURNS_ NULL_ ON_ NULL_ INPUT_ # returnsNullOnNullInputCharacteristic
| CALLED_ ON_ NULL_ INPUT_ # calledOnNullInputCharacteristic
| SECURITY_ (DEFINER_ | INVOKER_) # securityCharacteristic
| COMMENT_ string_ # commentCharacteristic
;
controlStatement
: RETURN_ valueExpression # returnStatement
| SET_ identifier EQ_ expression # assignmentStatement
| CASE_ expression caseStatementWhenClause+ elseClause? END_ CASE_ # simpleCaseStatement
| CASE_ caseStatementWhenClause+ elseClause? END_ CASE_ # searchedCaseStatement
| IF_ expression THEN_ sqlStatementList elseIfClause* elseClause? END_ IF_ # ifStatement
| ITERATE_ identifier # iterateStatement
| LEAVE_ identifier # leaveStatement
| BEGIN_ (variableDeclaration SEMICOLON_)* sqlStatementList? END_ # compoundStatement
| (label = identifier COLON_)? LOOP_ sqlStatementList END_ LOOP_ # loopStatement
| (label = identifier COLON_)? WHILE_ expression DO_ sqlStatementList END_ WHILE_ # whileStatement
| (label = identifier COLON_)? REPEAT_ sqlStatementList UNTIL_ expression END_ REPEAT_ # repeatStatement
;
caseStatementWhenClause
: WHEN_ expression THEN_ sqlStatementList
;
elseIfClause
: ELSEIF_ expression THEN_ sqlStatementList
;
elseClause
: ELSE_ sqlStatementList
;
variableDeclaration
: DECLARE_ identifier (COMMA_ identifier)* type (DEFAULT_ valueExpression)?
;
sqlStatementList
: (controlStatement SEMICOLON_)+
;
privilege
: CREATE_
| SELECT_
| DELETE_
| INSERT_
| UPDATE_
;
qualifiedName
: identifier (DOT_ identifier)*
;
queryPeriod
: FOR_ rangeType AS_ OF_ end = valueExpression
;
rangeType
: TIMESTAMP_
| VERSION_
;
grantor
: principal # specifiedPrincipal
| CURRENT_USER_ # currentUserGrantor
| CURRENT_ROLE_ # currentRoleGrantor
;
principal
: identifier # unspecifiedPrincipal
| USER_ identifier # userPrincipal
| ROLE_ identifier # rolePrincipal
;
roles
: identifier (COMMA_ identifier)*
;
identifier
: IDENTIFIER_ # unquotedIdentifier
| QUOTED_IDENTIFIER_ # quotedIdentifier
| nonReserved # unquotedIdentifier
| BACKQUOTED_IDENTIFIER_ # backQuotedIdentifier
| DIGIT_IDENTIFIER_ # digitIdentifier
;
number
: MINUS_? DECIMAL_VALUE_ # decimalLiteral
| MINUS_? DOUBLE_VALUE_ # doubleLiteral
| MINUS_? INTEGER_VALUE_ # integerLiteral
;
authorizationUser
: identifier # identifierUser
| string_ # stringUser
;
nonReserved
// IMPORTANT: this rule must only contain tokens. Nested rules are not supported. See SqlParser.exitNonReserved
: ABSENT_
| ADD_
| ADMIN_
| AFTER_
| ALL_
| ANALYZE_
| ANY_
| ARRAY_
| ASC_
| AT_
| AUTHORIZATION_
| BEGIN_
| BERNOULLI_
| BOTH_
| CALL_
| CALLED_
| CASCADE_
| CATALOG_
| CATALOGS_
| COLUMN_
| COLUMNS_
| COMMENT_
| COMMIT_
| COMMITTED_
| CONDITIONAL_
| COPARTITION_
| COUNT_
| CURRENT_
| DATA_
| DATE_
| DAY_
| DECLARE_
| DEFAULT_
| DEFINE_
| DEFINER_
| DENY_
| DESC_
| DESCRIPTOR_
| DETERMINISTIC_
| DISTRIBUTED_
| DO_
| DOUBLE_
| ELSEIF_
| EMPTY_
| ENCODING_
| ERROR_
| EXCLUDING_
| EXPLAIN_
| FETCH_
| FILTER_
| FINAL_
| FIRST_
| FOLLOWING_
| FORMAT_
| FUNCTION_
| FUNCTIONS_
| GRACE_
| GRANT_
| GRANTED_
| GRANTS_
| GRAPHVIZ_
| GROUPS_
| HOUR_
| IF_
| IGNORE_
| IMMEDIATE_
| INCLUDING_
| INITIAL_
| INPUT_
| INTERVAL_
| INVOKER_
| IO_
| ITERATE_
| ISOLATION_
| JSON_
| KEEP_
| KEY_
| KEYS_
| LANGUAGE_
| LAST_
| LATERAL_
| LEADING_
| LEAVE_
| LEVEL_
| LIMIT_
| LOCAL_
| LOGICAL_
| LOOP_
| MAP_
| MATCH_