-
Notifications
You must be signed in to change notification settings - Fork 2
/
fugue_sql.g4
2218 lines (1964 loc) · 57 KB
/
fugue_sql.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.
*
* This file is based on
* https://github.com/apache/spark/blob/d2bec5e265e0aa4fa527c3f43cfe738cdbdc4598/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4
*/
grammar fugue_sql;
//==========================
// Start of the Fugue rules
//==========================
fugueLanguage:
fugueSingleTask+ EOF
;
fugueSingleStatement:
fugueSingleTask EOF
;
fugueSingleTask
: fugueNestableTask
| fugueOutputTransformTask
| fugueOutputTask
| fuguePrintTask
| fugueSaveTask
| fugueModuleTask
;
fugueNestableTask
: (assign=fugueAssignment)? q=query (checkpoint=fugueCheckpoint)? (broadcast=fugueBroadcast)? (y=fugueYield)?
;
fugueNestableTaskCollectionNoSelect
: fugueTransformTask
| fugueProcessTask
| fugueZipTask
| fugueCreateTask
| fugueCreateDataTask
| fugueLoadTask
| fugueSaveAndUseTask
| fugueRenameColumnsTask
| fugueAlterColumnsTask
| fugueDropColumnsTask
| fugueDropnaTask
| fugueFillnaTask
| fugueSampleTask
| fugueTakeTask
;
fugueTransformTask:
TRANSFORM (dfs=fugueDataFrames)? (partition=fuguePrepartition)? params=fugueSingleOutputExtensionCommonWild (CALLBACK callback=fugueExtension)?
;
fugueProcessTask:
PROCESS (dfs=fugueDataFrames)? (partition=fuguePrepartition)? params=fugueSingleOutputExtensionCommon
;
fugueSaveAndUseTask:
SAVE AND USE (df=fugueDataFrame)? (partition=fuguePrepartition)? m=fugueSaveMode (single=fugueSingleFile)? (fmt=fugueFileFormat)? path=fuguePath (params=fugueParams)?
;
fugueRenameColumnsTask:
RENAME COLUMNS cols=fugueRenameExpression (FROM df=fugueDataFrame)?
;
fugueAlterColumnsTask:
ALTER COLUMNS cols=fugueSchema (FROM df=fugueDataFrame)?
;
fugueDropColumnsTask:
DROP COLUMNS cols=fugueCols (IF EXISTS)? (FROM df=fugueDataFrame)?
;
fugueDropnaTask:
DROP ROWS IF how=(ALL|ANY) (THENULL|THENULLS) (ON cols=fugueCols)? (FROM df=fugueDataFrame)?
;
fugueFillnaTask:
FILL (THENULL|THENULLS) params=fugueParams (FROM df=fugueDataFrame)?
;
fugueSampleTask:
SAMPLE REPLACE? method=fugueSampleMethod (SEED seed=INTEGER_VALUE)? (FROM df=fugueDataFrame)? (PREPARTITION BY by=fugueCols)?
;
fugueTakeTask:
TAKE (rows=INTEGER_VALUE (ROW|ROWS))? (FROM df=fugueDataFrame)? ((partition=fuguePrepartition)|(PRESORT presort=fugueColsSort))? ((THENULL|THENULLS) na_position=(FIRST|LAST))?
;
fugueZipTask:
ZIP dfs=fugueDataFrames (how=fugueZipType)? (BY by=fugueCols)? (PRESORT presort=fugueColsSort)?
;
fugueCreateTask:
CREATE params=fugueSingleOutputExtensionCommon
;
fugueCreateDataTask:
CREATE DATA? data=fugueJsonArray SCHEMA schema=fugueSchema
;
fugueLoadTask:
LOAD (fmt=fugueFileFormat)? ((path=fuguePath)|(paths=fuguePaths)) (params=fugueParams)? (COLUMNS columns=fugueLoadColumns)?
;
fugueOutputTask:
OUTPUT (dfs=fugueDataFrames)? (partition=fuguePrepartition)? USING fugueUsing=fugueExtension (params=fugueParams)?
;
fuguePrintTask
: PRINT rows=INTEGER_VALUE (ROW|ROWS) (FROM dfs=fugueDataFrames)? (count=ROWCOUNT)? (TITLE title=STRING)?
| PRINT (dfs=fugueDataFrames)? (count=ROWCOUNT)? (TITLE title=STRING)?
;
fugueSaveTask:
SAVE (df=fugueDataFrame)? (partition=fuguePrepartition)? m=fugueSaveMode (single=fugueSingleFile)? (fmt=fugueFileFormat)? path=fuguePath (params=fugueParams)?
;
fugueOutputTransformTask:
OUTTRANSFORM (dfs=fugueDataFrames)? (partition=fuguePrepartition)? USING fugueUsing=fugueExtension (params=fugueParams)? (CALLBACK callback=fugueExtension)?
;
fugueModuleTask:
(assign=fugueAssignment)? SUB (dfs=fugueDataFrames)? USING fugueUsing=fugueExtension (params=fugueParams)?
;
fugueSqlEngine:
CONNECT fugueUsing=fugueExtension (params=fugueParams)?
;
fugueSingleFile
: single=SINGLE
;
fugueLoadColumns
: schema=fugueSchema
| cols = fugueCols
;
fugueSaveMode
: TO
| OVERWRITE
| APPEND
;
fugueFileFormat
: PARQUET
| CSV
| JSON
;
fuguePath
: STRING
;
fuguePaths
: '[' fuguePath (',' fuguePath)* ']'
;
fugueCheckpoint
: LAZY? (PERSIST | WEAK CHECKPOINT) (params=fugueParams)? #fugueCheckpointWeak
| LAZY? STRONG? CHECKPOINT (partition=fuguePrepartition)? (single=fugueSingleFile)? (params=fugueParams)? #fugueCheckpointStrong
| LAZY? DETERMINISTIC CHECKPOINT (ns=fugueCheckpointNamespace)? (partition=fuguePrepartition)? (single=fugueSingleFile)? (params=fugueParams)? #fugueCheckpointDeterministic
;
fugueCheckpointNamespace
: STRING
;
fugueYield
: YIELD (FILE | TABLE | VIEW) (AS name=fugueIdentifier)?
| YIELD LOCAL? DATAFRAME (AS name=fugueIdentifier)?
;
fugueBroadcast:
BROADCAST
;
fugueDataFrames
: fugueDataFrame (',' fugueDataFrame)* #fugueDataFramesList
| fugueDataFramePair (',' fugueDataFramePair)* #fugueDataFramesDict
;
fugueDataFramePair
: key=fugueIdentifier (':' | EQUAL) value=fugueDataFrame
;
fugueDataFrame
: fugueIdentifier fugueDataFrameMember? #fugueDataFrameSource
| '(' task=fugueNestableTask ')' #fugueDataFrameNested
;
fugueDataFrameMember
: '[' (index=INTEGER_VALUE|key=fugueIdentifier) ']'
;
fugueAssignment:
varname=fugueIdentifier sign=fugueAssignmentSign
;
fugueAssignmentSign
: EQUAL
;
fugueSingleOutputExtensionCommonWild:
USING fugueUsing=fugueExtension (params=fugueParams)? (SCHEMA schema=fugueWildSchema)?
;
fugueSingleOutputExtensionCommon:
USING fugueUsing=fugueExtension (params=fugueParams)? (SCHEMA schema=fugueSchema)?
;
fugueExtension:
(domain=fugueIdentifier ':')? fugueIdentifier ('.' fugueIdentifier)*
;
fugueSampleMethod:
percentage=(INTEGER_VALUE | DECIMAL_VALUE) (PERCENTLIT | PERCENT)
| APPROX? rows=INTEGER_VALUE ROWS
;
fugueZipType
: CROSS
| INNER
| LEFT OUTER
| RIGHT OUTER
| FULL OUTER
;
fuguePrepartition
: (algo=fuguePartitionAlgo)? PREPARTITION num=fuguePartitionNum (BY by=fugueCols)? (PRESORT presort=fugueColsSort)?
| (algo=fuguePartitionAlgo)? PREPARTITION BY by=fugueCols (PRESORT presort=fugueColsSort)?
;
fuguePartitionAlgo
: HASH | RAND | EVEN | COARSE
;
fuguePartitionNum
: fuguePartitionNumber
| '(' fuguePartitionNum ')'
| fuguePartitionNum (PLUS|MINUS|ASTERISK|SLASH) fuguePartitionNum
;
fuguePartitionNumber
: MINUS? DECIMAL_VALUE
| MINUS? INTEGER_VALUE
| ROWCOUNT
| CONCURRENCY
;
fugueParams
: PARAMS pairs=fugueJsonPairs #fugueParamsPairs
| PARAMS? obj=fugueJsonObj #fugueParamsObj
;
fugueCols:
fugueColumnIdentifier (',' fugueColumnIdentifier)*
;
fugueColsSort:
fugueColSort (',' fugueColSort)*
;
fugueColSort:
fugueColumnIdentifier (ASC | DESC)?
;
fugueColumnIdentifier:
fugueIdentifier
;
fugueRenameExpression:
fugueRenamePair (',' fugueRenamePair)*
;
fugueWildSchema:
fugueWildSchemaPair (',' fugueWildSchemaPair)* fugueSchemaOp*
;
fugueWildSchemaPair
: pair=fugueSchemaPair
| ASTERISK
;
fugueSchemaOp
: (MINUS|TILDE) fugueSchemaKey (',' fugueSchemaKey)*
| PLUS fugueSchema
;
fugueSchema:
fugueSchemaPair (',' fugueSchemaPair)*
;
fugueSchemaPair:
key=fugueSchemaKey ':' value=fugueSchemaType
;
fugueSchemaKey:
fugueIdentifier
;
fugueSchemaType
: fugueIdentifier #fugueSchemaSimpleType
| '[' fugueSchemaType ']' #fugueSchemaListType
| '{' fugueSchema '}' #fugueSchemaStructType
| '<' fugueSchemaType ',' fugueSchemaType '>' #fugueSchemaMapType
;
fugueRenamePair:
key=fugueSchemaKey ':' value=fugueSchemaKey
;
// From https://github.com/antlr/grammars-v4/blob/master/json/JSON.g4
fugueJson
: fugueJsonValue
;
fugueJsonObj
: '{' fugueJsonPairs ','? '}'
| '{' '}'
| '(' fugueJsonPairs ','? ')'
| '(' ')'
;
fugueJsonPairs
: fugueJsonPair (',' fugueJsonPair)*
;
fugueJsonPair
: key=fugueJsonKey (EQUAL | ':') value=fugueJsonValue
;
fugueJsonKey
: fugueIdentifier
| fugueJsonString
;
fugueJsonArray
: '[' fugueJsonValue (',' fugueJsonValue)* ','? ']'
| '[' ']'
;
fugueJsonValue
: fugueJsonString
| fugueJsonNumber
| fugueJsonObj
| fugueJsonArray
| fugueJsonBool
| fugueJsonNull
;
fugueJsonNumber
: number
;
fugueJsonString
: STRING
;
fugueJsonBool
: 'true'
| TRUE
| 'false'
| FALSE
;
fugueJsonNull
: 'null'
| THENULL
;
fugueIdentifier:
identifier
;
//========================
// End of the Fugue rules
//========================
singleStatement
: statement ';'* EOF
;
singleExpression
: namedExpression EOF
;
singleTableIdentifier
: tableIdentifier EOF
;
singleMultipartIdentifier
: multipartIdentifier EOF
;
singleFunctionIdentifier
: functionIdentifier EOF
;
singleDataType
: dataType EOF
;
singleTableSchema
: colTypeList EOF
;
statement
: query #statementDefault
| ctes? dmlStatementNoWith #dmlStatement
| USE NAMESPACE? multipartIdentifier #use
| CREATE theNamespace (IF NOT EXISTS)? multipartIdentifier
(commentSpec |
locationSpec |
(WITH (DBPROPERTIES | PROPERTIES) tablePropertyList))* #createNamespace
| ALTER theNamespace multipartIdentifier
SET (DBPROPERTIES | PROPERTIES) tablePropertyList #setNamespaceProperties
| ALTER theNamespace multipartIdentifier
SET locationSpec #setNamespaceLocation
| DROP theNamespace (IF EXISTS)? multipartIdentifier
(RESTRICT | CASCADE)? #dropNamespace
| SHOW (DATABASES | NAMESPACES) ((FROM | IN) multipartIdentifier)?
(LIKE? pattern=STRING)? #showNamespaces
| createTableHeader ('(' colTypeList ')')? tableProvider
createTableClauses
(AS? query)? #createTable
| createTableHeader ('(' columns=colTypeList ')')?
(commentSpec |
(PARTITIONED BY '(' partitionColumns=colTypeList ')' |
PARTITIONED BY partitionColumnNames=identifierList) |
bucketSpec |
skewSpec |
rowFormat |
createFileFormat |
locationSpec |
(TBLPROPERTIES tableProps=tablePropertyList))*
(AS? query)? #createHiveTable
| CREATE TABLE (IF NOT EXISTS)? target=tableIdentifier
LIKE source=tableIdentifier
(tableProvider |
rowFormat |
createFileFormat |
locationSpec |
(TBLPROPERTIES tableProps=tablePropertyList))* #createTableLike
| replaceTableHeader ('(' colTypeList ')')? tableProvider
createTableClauses
(AS? query)? #replaceTable
| ANALYZE TABLE multipartIdentifier partitionSpec? COMPUTE STATISTICS
(identifier | FOR COLUMNS identifierSeq | FOR ALL COLUMNS)? #analyze
| ALTER TABLE multipartIdentifier
ADD (COLUMN | COLUMNS)
columns=qualifiedColTypeWithPositionList #addTableColumns
| ALTER TABLE multipartIdentifier
ADD (COLUMN | COLUMNS)
'(' columns=qualifiedColTypeWithPositionList ')' #addTableColumns
| ALTER TABLE table=multipartIdentifier
RENAME COLUMN
ifrom=multipartIdentifier TO to=errorCapturingIdentifier #renameTableColumn
| ALTER TABLE multipartIdentifier
DROP (COLUMN | COLUMNS)
'(' columns=multipartIdentifierList ')' #dropTableColumns
| ALTER TABLE multipartIdentifier
DROP (COLUMN | COLUMNS) columns=multipartIdentifierList #dropTableColumns
| ALTER (TABLE | VIEW) ifrom=multipartIdentifier
RENAME TO to=multipartIdentifier #renameTable
| ALTER (TABLE | VIEW) multipartIdentifier
SET TBLPROPERTIES tablePropertyList #setTableProperties
| ALTER (TABLE | VIEW) multipartIdentifier
UNSET TBLPROPERTIES (IF EXISTS)? tablePropertyList #unsetTableProperties
| ALTER TABLE table=multipartIdentifier
(ALTER | CHANGE) COLUMN? column=multipartIdentifier
alterColumnAction? #alterTableAlterColumn
| ALTER TABLE table=multipartIdentifier partitionSpec?
CHANGE COLUMN?
colName=multipartIdentifier colType colPosition? #hiveChangeColumn
| ALTER TABLE table=multipartIdentifier partitionSpec?
REPLACE COLUMNS
'(' columns=qualifiedColTypeWithPositionList ')' #hiveReplaceColumns
| ALTER TABLE multipartIdentifier (partitionSpec)?
SET SERDE STRING (WITH SERDEPROPERTIES tablePropertyList)? #setTableSerDe
| ALTER TABLE multipartIdentifier (partitionSpec)?
SET SERDEPROPERTIES tablePropertyList #setTableSerDe
| ALTER (TABLE | VIEW) multipartIdentifier ADD (IF NOT EXISTS)?
partitionSpecLocation+ #addTablePartition
| ALTER TABLE multipartIdentifier
ifrom=partitionSpec RENAME TO to=partitionSpec #renameTablePartition
| ALTER (TABLE | VIEW) multipartIdentifier
DROP (IF EXISTS)? partitionSpec (',' partitionSpec)* PURGE? #dropTablePartitions
| ALTER TABLE multipartIdentifier
(partitionSpec)? SET locationSpec #setTableLocation
| ALTER TABLE multipartIdentifier RECOVER PARTITIONS #recoverPartitions
| DROP TABLE (IF EXISTS)? multipartIdentifier PURGE? #dropTable
| DROP VIEW (IF EXISTS)? multipartIdentifier #dropView
| CREATE (OR REPLACE)? (GLOBAL? TEMPORARY)?
VIEW (IF NOT EXISTS)? multipartIdentifier
identifierCommentList?
(commentSpec |
(PARTITIONED ON identifierList) |
(TBLPROPERTIES tablePropertyList))*
AS query #createView
| CREATE (OR REPLACE)? GLOBAL? TEMPORARY VIEW
tableIdentifier ('(' colTypeList ')')? tableProvider
(OPTIONS tablePropertyList)? #createTempViewUsing
| ALTER VIEW multipartIdentifier AS? query #alterViewQuery
| CREATE (OR REPLACE)? TEMPORARY? FUNCTION (IF NOT EXISTS)?
multipartIdentifier AS className=STRING
(USING resource (',' resource)*)? #createFunction
| DROP TEMPORARY? FUNCTION (IF EXISTS)? multipartIdentifier #dropFunction
| EXPLAIN (LOGICAL | FORMATTED | EXTENDED | CODEGEN | COST)?
statement #explain
| SHOW TABLES ((FROM | IN) multipartIdentifier)?
(LIKE? pattern=STRING)? #showTables
| SHOW TABLE EXTENDED ((FROM | IN) ns=multipartIdentifier)?
LIKE pattern=STRING partitionSpec? #showTable
| SHOW TBLPROPERTIES table=multipartIdentifier
('(' key=tablePropertyKey ')')? #showTblProperties
| SHOW COLUMNS (FROM | IN) table=multipartIdentifier
((FROM | IN) ns=multipartIdentifier)? #showColumns
| SHOW VIEWS ((FROM | IN) multipartIdentifier)?
(LIKE? pattern=STRING)? #showViews
| SHOW PARTITIONS multipartIdentifier partitionSpec? #showPartitions
| SHOW identifier? FUNCTIONS
(LIKE? (multipartIdentifier | pattern=STRING))? #showFunctions
| SHOW CREATE TABLE multipartIdentifier (AS SERDE)? #showCreateTable
| SHOW CURRENT NAMESPACE #showCurrentNamespace
| (DESC | DESCRIBE) FUNCTION EXTENDED? describeFuncName #describeFunction
| (DESC | DESCRIBE) theNamespace EXTENDED?
multipartIdentifier #describeNamespace
| (DESC | DESCRIBE) TABLE? option=(EXTENDED | FORMATTED)?
multipartIdentifier partitionSpec? describeColName? #describeRelation
| (DESC | DESCRIBE) QUERY? query #describeQuery
| COMMENT ON theNamespace multipartIdentifier IS
comment=(STRING | THENULL) #commentNamespace
| COMMENT ON TABLE multipartIdentifier IS comment=(STRING | THENULL) #commentTable
| REFRESH TABLE multipartIdentifier #refreshTable
| REFRESH (STRING | .*?) #refreshResource
| CACHE LAZY? TABLE multipartIdentifier
(OPTIONS options=tablePropertyList)? (AS? query)? #cacheTable
| UNCACHE TABLE (IF EXISTS)? multipartIdentifier #uncacheTable
| CLEAR CACHE #clearCache
| LOAD DATA LOCAL? INPATH path=STRING OVERWRITE? INTO TABLE
multipartIdentifier partitionSpec? #loadData
| TRUNCATE TABLE multipartIdentifier partitionSpec? #truncateTable
| MSCK REPAIR TABLE multipartIdentifier #repairTable
| op=(ADD | LIST) identifier (STRING | .*?) #manageResource
| SET ROLE .*? #failNativeCommand
| SET .*? #setConfiguration
| RESET #resetConfiguration
| unsupportedHiveNativeCommands .*? #failNativeCommand
;
unsupportedHiveNativeCommands
: kw1=CREATE kw2=ROLE
| kw1=DROP kw2=ROLE
| kw1=GRANT kw2=ROLE?
| kw1=REVOKE kw2=ROLE?
| kw1=SHOW kw2=GRANT
| kw1=SHOW kw2=ROLE kw3=GRANT?
| kw1=SHOW kw2=PRINCIPALS
| kw1=SHOW kw2=ROLES
| kw1=SHOW kw2=CURRENT kw3=ROLES
| kw1=EXPORT kw2=TABLE
| kw1=IMPORT kw2=TABLE
| kw1=SHOW kw2=COMPACTIONS
| kw1=SHOW kw2=CREATE kw3=TABLE
| kw1=SHOW kw2=TRANSACTIONS
| kw1=SHOW kw2=INDEXES
| kw1=SHOW kw2=LOCKS
| kw1=CREATE kw2=INDEX
| kw1=DROP kw2=INDEX
| kw1=ALTER kw2=INDEX
| kw1=LOCK kw2=TABLE
| kw1=LOCK kw2=DATABASE
| kw1=UNLOCK kw2=TABLE
| kw1=UNLOCK kw2=DATABASE
| kw1=CREATE kw2=TEMPORARY kw3=MACRO
| kw1=DROP kw2=TEMPORARY kw3=MACRO
| kw1=ALTER kw2=TABLE tableIdentifier kw3=NOT kw4=CLUSTERED
| kw1=ALTER kw2=TABLE tableIdentifier kw3=CLUSTERED kw4=BY
| kw1=ALTER kw2=TABLE tableIdentifier kw3=NOT kw4=SORTED
| kw1=ALTER kw2=TABLE tableIdentifier kw3=SKEWED kw4=BY
| kw1=ALTER kw2=TABLE tableIdentifier kw3=NOT kw4=SKEWED
| kw1=ALTER kw2=TABLE tableIdentifier kw3=NOT kw4=STORED kw5=AS kw6=DIRECTORIES
| kw1=ALTER kw2=TABLE tableIdentifier kw3=SET kw4=SKEWED kw5=LOCATION
| kw1=ALTER kw2=TABLE tableIdentifier kw3=EXCHANGE kw4=PARTITION
| kw1=ALTER kw2=TABLE tableIdentifier kw3=ARCHIVE kw4=PARTITION
| kw1=ALTER kw2=TABLE tableIdentifier kw3=UNARCHIVE kw4=PARTITION
| kw1=ALTER kw2=TABLE tableIdentifier kw3=TOUCH
| kw1=ALTER kw2=TABLE tableIdentifier partitionSpec? kw3=COMPACT
| kw1=ALTER kw2=TABLE tableIdentifier partitionSpec? kw3=CONCATENATE
| kw1=ALTER kw2=TABLE tableIdentifier partitionSpec? kw3=SET kw4=FILEFORMAT
| kw1=ALTER kw2=TABLE tableIdentifier partitionSpec? kw3=REPLACE kw4=COLUMNS
| kw1=START kw2=TRANSACTION
| kw1=COMMIT
| kw1=ROLLBACK
| kw1=DFS
;
createTableHeader
: CREATE TEMPORARY? EXTERNAL? TABLE (IF NOT EXISTS)? multipartIdentifier
;
replaceTableHeader
: (CREATE OR)? REPLACE TABLE multipartIdentifier
;
bucketSpec
: CLUSTERED BY identifierList
(SORTED BY orderedIdentifierList)?
INTO INTEGER_VALUE BUCKETS
;
skewSpec
: SKEWED BY identifierList
ON (constantList | nestedConstantList)
(STORED AS DIRECTORIES)?
;
locationSpec
: LOCATION STRING
;
commentSpec
: COMMENT STRING
;
query
: fugueSqlEngine? ctes? queryTerm queryOrganization
;
insertInto
: INSERT OVERWRITE TABLE? multipartIdentifier (partitionSpec (IF NOT EXISTS)?)? #insertOverwriteTable
| INSERT INTO TABLE? multipartIdentifier partitionSpec? (IF NOT EXISTS)? #insertIntoTable
| INSERT OVERWRITE LOCAL? DIRECTORY path=STRING rowFormat? createFileFormat? #insertOverwriteHiveDir
| INSERT OVERWRITE LOCAL? DIRECTORY (path=STRING)? tableProvider (OPTIONS options=tablePropertyList)? #insertOverwriteDir
;
partitionSpecLocation
: partitionSpec locationSpec?
;
partitionSpec
: PARTITION '(' partitionVal (',' partitionVal)* ')'
;
partitionVal
: identifier (EQUAL constant)?
;
theNamespace
: NAMESPACE
| DATABASE
| SCHEMA
;
describeFuncName
: qualifiedName
| STRING
| comparisonOperator
| arithmeticOperator
| predicateOperator
;
describeColName
: nameParts+=identifier ('.' nameParts+=identifier)*
;
ctes
: WITH namedQuery (',' namedQuery)*
;
namedQuery
: name=errorCapturingIdentifier (columnAliases=identifierList)? AS? '(' query ')'
;
tableProvider
: USING multipartIdentifier
;
createTableClauses
:((OPTIONS options=tablePropertyList) |
(PARTITIONED BY partitioning=transformList) |
bucketSpec |
locationSpec |
commentSpec |
(TBLPROPERTIES tableProps=tablePropertyList))*
;
tablePropertyList
: '(' tableProperty (',' tableProperty)* ')'
;
tableProperty
: key=tablePropertyKey (EQUAL? value=tablePropertyValue)?
;
tablePropertyKey
: identifier ('.' identifier)*
| STRING
;
tablePropertyValue
: INTEGER_VALUE
| DECIMAL_VALUE
| booleanValue
| STRING
;
constantList
: '(' constant (',' constant)* ')'
;
nestedConstantList
: '(' constantList (',' constantList)* ')'
;
createFileFormat
: STORED AS fileFormat
| STORED BY storageHandler
;
fileFormat
: INPUTFORMAT inFmt=STRING OUTPUTFORMAT outFmt=STRING #tableFileFormat
| identifier #genericFileFormat
;
storageHandler
: STRING (WITH SERDEPROPERTIES tablePropertyList)?
;
resource
: identifier STRING
;
dmlStatementNoWith
: insertInto queryTerm queryOrganization #singleInsertQuery
| fromClause multiInsertQueryBody+ #multiInsertQuery
| DELETE FROM multipartIdentifier tableAlias whereClause? #deleteFromTable
| UPDATE multipartIdentifier tableAlias setClause whereClause? #updateTable
| MERGE INTO target=multipartIdentifier targetAlias=tableAlias
USING (source=multipartIdentifier |
'(' sourceQuery=query')') sourceAlias=tableAlias
ON mergeCondition=booleanExpression
matchedClause*
notMatchedClause* #mergeIntoTable
;
queryOrganization
: (ORDER BY order+=sortItem (',' order+=sortItem)*)?
(CLUSTER BY clusterBy+=expression (',' clusterBy+=expression)*)?
(DISTRIBUTE BY distributeBy+=expression (',' distributeBy+=expression)*)?
(SORT BY sort+=sortItem (',' sort+=sortItem)*)?
windowClause?
(LIMIT (ALL | limit=expression))?
;
multiInsertQueryBody
: insertInto fromStatementBody
;
queryTerm
: queryPrimary #queryTermDefault
| fugueNestableTaskCollectionNoSelect #fugueTerm
| left=queryTerm
theOperator=(INTERSECT | UNION | EXCEPT | SETMINUS) setQuantifier? right=queryTerm #setOperation
| left=queryTerm
theOperator=INTERSECT setQuantifier? right=queryTerm #setOperation
| left=queryTerm
theOperator=(UNION | EXCEPT | SETMINUS) setQuantifier? right=queryTerm #setOperation
;
queryPrimary
: querySpecification #queryPrimaryDefault
| fromStatement #fromStmt
| TABLE multipartIdentifier #table
| inlineTable #inlineTableDefault1
// TODO: subquery is causing ambiguity such as SELECT * FROM (SELECT * FROM a)
// it thinks the first FROM is an identity not a keyword
// when SQL_standard_keyword_behavior=False
//| '(' query ')' #subquery
;
sortItem
: expression ordering=(ASC | DESC)? (THENULLS nullOrder=(LAST | FIRST))?
;
fromStatement
: fromClause fromStatementBody+
;
fromStatementBody
: transformClause
whereClause?
queryOrganization
| selectClause
lateralView*
whereClause?
aggregationClause?
havingClause?
windowClause?
queryOrganization
;
querySpecification
: transformClause
optionalFromClause
whereClause? #transformQuerySpecification
| selectClause
optionalFromClause
lateralView*
whereClause?
aggregationClause?
havingClause?
windowClause? #regularQuerySpecification
;
optionalFromClause // add this to easily capture no FROM cases
: fromClause?
;
transformClause
: (SELECT kind=TRANSFORM '(' namedExpressionSeq ')'
| kind=MAP namedExpressionSeq
| kind=REDUCE namedExpressionSeq)
inRowFormat=rowFormat?
(RECORDWRITER recordWriter=STRING)?
USING script=STRING
(AS (identifierSeq | colTypeList | ('(' (identifierSeq | colTypeList) ')')))?
outRowFormat=rowFormat?
(RECORDREADER recordReader=STRING)?
;
selectClause
: SELECT (hints+=hint)* setQuantifier? namedExpressionSeq
;
setClause
: SET assignmentList
;
matchedClause
: WHEN MATCHED (AND matchedCond=booleanExpression)? THEN matchedAction
;
notMatchedClause
: WHEN NOT MATCHED (AND notMatchedCond=booleanExpression)? THEN notMatchedAction
;
matchedAction
: DELETE
| UPDATE SET ASTERISK
| UPDATE SET assignmentList
;
notMatchedAction
: INSERT ASTERISK
| INSERT '(' columns=multipartIdentifierList ')'
VALUES '(' expression (',' expression)* ')'
;
assignmentList
: assignment (',' assignment)*
;
assignment
: key=multipartIdentifier EQUAL value=expression
;
whereClause
: WHERE booleanExpression
;
havingClause
: HAVING booleanExpression
;
hint
: '/*+' hintStatements+=hintStatement (','? hintStatements+=hintStatement)* '*/'
;
hintStatement
: hintName=identifier
| hintName=identifier '(' parameters+=primaryExpression (',' parameters+=primaryExpression)* ')'
;
fromClause
: FROM relation (',' relation)* lateralView* pivotClause?
;
aggregationClause
: GROUP BY groupingExpressions+=expression (',' groupingExpressions+=expression)* (
WITH kind=ROLLUP
| WITH kind=CUBE
| kind=GROUPING SETS '(' groupingSet (',' groupingSet)* ')')?
| GROUP BY kind=GROUPING SETS '(' groupingSet (',' groupingSet)* ')'
;
groupingSet
: '(' (expression (',' expression)*)? ')'
| expression
;
pivotClause
: PIVOT '(' aggregates=namedExpressionSeq FOR pivotColumn IN '(' pivotValues+=pivotValue (',' pivotValues+=pivotValue)* ')' ')'
;
pivotColumn
: identifiers+=identifier
| '(' identifiers+=identifier (',' identifiers+=identifier)* ')'
;
pivotValue
: expression (AS? identifier)?
;
lateralView
: LATERAL VIEW (OUTER)? qualifiedName '(' (expression (',' expression)*)? ')' tblName=identifier (AS? colName+=identifier (',' colName+=identifier)*)?
;
setQuantifier
: DISTINCT
| ALL
;
relation
: relationPrimary joinRelation*
;
joinRelation
: joinType JOIN right=relationPrimary joinCriteria?
| NATURAL joinType JOIN right=relationPrimary
;
joinType
: INNER?
| CROSS
| LEFT OUTER?
| LEFT? SEMI
| RIGHT OUTER?
| FULL OUTER?
| LEFT? ANTI
;
joinCriteria
: ON booleanExpression
| USING identifierList
;
sample
: TABLESAMPLE (SYSTEM | BERNOULLI | RESERVOIR)? '(' sampleMethod? ')'
;
sampleMethod
: negativeSign=MINUS? percentage=(INTEGER_VALUE | DECIMAL_VALUE) (PERCENTLIT | PERCENT) #sampleByPercentile
| expression ROWS #sampleByRows
| sampleType=BUCKET numerator=INTEGER_VALUE OUT OF denominator=INTEGER_VALUE
(ON (identifier | qualifiedName '(' ')'))? #sampleByBucket
| bytes=expression #sampleByBytes
;
identifierList
: '(' identifierSeq ')'
;
identifierSeq
: ident+=errorCapturingIdentifier (',' ident+=errorCapturingIdentifier)*
;
orderedIdentifierList
: '(' orderedIdentifier (',' orderedIdentifier)* ')'
;
orderedIdentifier
: ident=errorCapturingIdentifier ordering=(ASC | DESC)?
;
identifierCommentList
: '(' identifierComment (',' identifierComment)* ')'
;
identifierComment
: identifier commentSpec?
;
relationPrimary