-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathPostgreSqlParser.g4
3620 lines (3119 loc) · 90.9 KB
/
PostgreSqlParser.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
/*
* [The "MIT license"]
* Copyright (C) 2014 Sam Harwell, Tunnel Vision Laboratories, LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 2. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
* 3. Except as contained in this notice, the name of Tunnel Vision
* Laboratories, LLC. shall not be used in advertising or otherwise to
* promote the sale, use or other dealings in this Software without prior
* written authorization from Tunnel Vision Laboratories, LLC.
*/
/**
* This file is an adaptation of antlr's sql/postgresql/PostgreSQLParser.g4 grammar.
* Reference: https://github.com/antlr/grammars-v4/blob/master/sql/postgresql/PostgreSQLParser.g4
*/
/**
* Reference Doc: https://www.postgresql.org/docs/16.1/sql-commands.html
*/
// $antlr-format alignTrailingComments true, columnLimit 150, minEmptyLines 1, maxEmptyLinesToKeep 1, reflowComments false, useTab false
// $antlr-format allowShortRulesOnASingleLine false, allowShortBlocksOnASingleLine true, alignSemicolons hanging, alignColons hanging
// $antlr-format spaceBeforeAssignmentOperators false, keepEmptyLinesAtTheStartOfBlocks true
parser grammar PostgreSqlParser;
options {
tokenVocab= PostgreSqlLexer;
caseInsensitive= true;
superClass=SQLParserBase;
}
@header {
import { SQLParserBase } from '../SQLParserBase';
}
program
: singleStmt* EOF
;
singleStmt
: stmt SEMI?
;
stmt
: altereventtrigstmt
| altercollationstmt
| alterdatabasestmt
| alterdatabasesetstmt
| alterdefaultprivilegesstmt
| alterdomainstmt
| alterenumstmt
| alterextensionstmt
| alterextensioncontentsstmt
| alterfdwstmt
| alterforeignserverstmt
| alterfunctionstmt
| altergroupstmt
| alterobjectdependsstmt
| alterobjectschemastmt
| alterownerstmt
| alteroperatorstmt
| altertypestmt
| alterpolicystmt
| alterprocedurestmt
| alterseqstmt
| altersystemstmt
| altertablestmt
| altertblspcstmt
| altercompositetypestmt
| alterpublicationstmt
| alterrolesetstmt
| alterroutinestmt
| alterrolestmt
| altersubscriptionstmt
| alterstatsstmt
| altertsconfigurationstmt
| altertsdictionarystmt
| alterusermappingstmt
| analyzestmt
| callstmt
| checkpointstmt
| closeportalstmt
| clusterstmt
| commentstmt
| constraintssetstmt
| copystmt
| createamstmt
| createasstmt
| createassertionstmt
| createcaststmt
| createconversionstmt
| createdomainstmt
| createextensionstmt
| createfdwstmt
| createforeignserverstmt
| createforeigntablestmt
| createfunctionstmt
| creategroupstmt
| creatematviewstmt
| createopclassstmt
| createopfamilystmt
| createpublicationstmt
| alteropfamilystmt
| createpolicystmt
| createplangstmt
| createschemastmt
| createseqstmt
| createstmt
| createsubscriptionstmt
| createstatsstmt
| createtablespacestmt
| createtransformstmt
| createtrigstmt
| createeventtrigstmt
| createrolestmt
| createuserstmt
| createusermappingstmt
| createdbstmt
| deallocatestmt
| declarecursorstmt
| definestmt
| deletestmt
| discardstmt
| dostmt
| dropstmt
| executestmt
| explainstmt
| fetchstmt
| grantstmt
| grantrolestmt
| mergestmt
| importforeignschemastmt
| indexstmt
| insertstmt
| listenstmt
| refreshmatviewstmt
| loadstmt
| lockstmt
| notifystmt
| preparestmt
| reassignownedstmt
| reindexstmt
| removeaggrstmt
| removefuncstmt
| removeoperstmt
| renamestmt
| revokestmt
| revokerolestmt
| rulestmt
| seclabelstmt
| selectstmt
| transactionstmt
| truncatestmt
| unlistenstmt
| updatestmt
| vacuumstmt
| variableresetstmt
| variablesetstmt
| variableshowstmt
| viewstmt
| MetaCommand EndMetaCommand?
;
callstmt
: KW_CALL func_application
;
createrolestmt
: KW_CREATE KW_ROLE rolespec KW_WITH? createoptroleelem*
;
alteroptroleelem
: KW_PASSWORD (sconst | KW_NULL)
| (KW_ENCRYPTED | KW_UNENCRYPTED) KW_PASSWORD sconst
| KW_INHERIT
| KW_NOINHERIT
| KW_CREATEUSER
| KW_NOCREATEUSER
| KW_CREATEROLE
| KW_NOCREATEROLE
| KW_CREATEDB
| KW_NOCREATEDB
| KW_SUPERUSER
| KW_NOSUPERUSER
| KW_LOGIN
| KW_NOLOGIN
| KW_REPLICATION
| KW_NOREPLICATION
| KW_BYPASSRLS
| KW_NOBYPASSRLS
| KW_CONNECTION KW_LIMIT signediconst
| KW_VALID KW_UNTIL sconst
| KW_IN (KW_ROLE | KW_GROUP) name_list
| (KW_ROLE | KW_USER) role_list
| KW_ADMIN name_list
| identifier
;
createoptroleelem
: alteroptroleelem
| KW_SYSID Integral
| (KW_ADMIN | KW_ROLE) role_list
| KW_IN (KW_ROLE | KW_GROUP) role_list
;
createuserstmt
: KW_CREATE KW_USER rolespec KW_WITH? createoptroleelem*
;
alterrolestmt
: KW_ALTER (KW_ROLE | KW_USER) rolespec KW_WITH? alteroptroleelem*
;
alterrolesetstmt
: KW_ALTER (KW_ROLE | KW_USER) (KW_ALL | rolespec) KW_IN KW_DATABASE database_name setresetclause
;
alterroutinestmt
: KW_ALTER KW_ROUTINE routine_name func_args? alter_routine_cluase
;
alter_routine_cluase
: routine_action (routine_action)* KW_RESTRICT?
| KW_RENAME KW_TO routine_name_create
| KW_OWNER KW_TO rolespec
| KW_SET KW_SCHEMA schema_name_create
| KW_NO? KW_DEPENDS KW_ON KW_EXTENSION colid
;
routine_action
: KW_IMMUTABLE
| KW_STABLE
| KW_VOLATILE
| KW_NOT? KW_LEAKPROOF
| (KW_EXTERNAL? KW_SECURITY KW_INVOKER | KW_EXTERNAL? KW_SECURITY KW_DEFINER)
| KW_PARALLEL (KW_UNSAFE | KW_RESTRICTED | KW_SAFE)
| KW_COST collabel
| KW_ROWS colid
| KW_SET colid (KW_TO | EQUAL) (colid | KW_DEFAULT)
| KW_SET colid KW_FROM KW_CURRENT
| KW_RESET (colid | KW_ALL)
;
creategroupstmt
: KW_CREATE KW_GROUP rolespec KW_WITH? createoptroleelem*
;
altergroupstmt
: KW_ALTER KW_GROUP rolespec (KW_ADD | KW_DROP) KW_USER role_list
;
createschemastmt
: KW_CREATE KW_SCHEMA opt_if_not_exists? (
schema_name_create? KW_AUTHORIZATION rolespec
| schema_name_create
) schema_stmt*
;
schema_name_create
: any_name # schemaNameCreate
;
schema_stmt
: createstmt
| indexstmt
| createseqstmt
| createtrigstmt
| grantstmt
| viewstmt
;
variablesetstmt
: KW_SET (KW_LOCAL | KW_SESSION)? set_rest
;
set_rest
: KW_TRANSACTION transaction_mode_list
| KW_SESSION KW_CHARACTERISTICS KW_AS KW_TRANSACTION transaction_mode_list
| set_rest_more
;
generic_set
: (KW_ALL | var_name) (KW_TO | EQUAL)? (KW_DEFAULT | var_list)?
;
set_rest_more
: KW_TIME KW_ZONE zone_value
| KW_CATALOG sconst
| KW_SCHEMA schema_name
| KW_NAMES (sconst | KW_DEFAULT)?
| KW_ROLE nonreservedword_or_sconst
| KW_SESSION KW_AUTHORIZATION nonreservedword_or_sconst
| KW_XML KW_OPTION document_or_content
| KW_TRANSACTION KW_SNAPSHOT sconst
| var_name KW_FROM KW_CURRENT
| generic_set
;
var_name
: colid (DOT colid)*
;
var_list
: var_value (COMMA var_value)*
;
var_value
: opt_boolean_or_string
| numericonly
;
iso_level
: KW_READ (KW_UNCOMMITTED | KW_COMMITTED)
| KW_REPEATABLE KW_READ
| KW_SERIALIZABLE
;
opt_boolean_or_string_column
: KW_TRUE
| KW_FALSE
| KW_ON
| column_name
| type_func_name_keyword
| sconst
;
opt_boolean_or_string
: KW_TRUE
| KW_FALSE
| KW_ON
| nonreservedword_or_sconst
;
zone_value
: sconst
| KW_DEFAULT
| identifier
| KW_INTERVAL sconst opt_interval?
| KW_INTERVAL opt_float sconst
| numericonly
| KW_LOCAL
;
nonreservedword_or_sconst
: nonreservedword
| sconst
;
variableresetstmt
: KW_RESET reset_rest
;
reset_rest
: KW_TIME KW_ZONE
| KW_TRANSACTION KW_ISOLATION KW_LEVEL
| KW_SESSION KW_AUTHORIZATION
| KW_ALL
| var_name
;
setresetclause
: KW_SET set_rest
| variableresetstmt
;
functionsetresetclause
: KW_SET set_rest_more
| variableresetstmt
;
variableshowstmt
: KW_SHOW (
var_name
| KW_TIME KW_ZONE
| KW_TRANSACTION KW_ISOLATION KW_LEVEL
| KW_SESSION KW_AUTHORIZATION
| KW_ALL
)
;
constraintssetstmt
: KW_SET KW_CONSTRAINTS (KW_ALL | qualified_name_list) (KW_DEFERRED | KW_IMMEDIATE)
;
checkpointstmt
: KW_CHECKPOINT
;
discardstmt
: KW_DISCARD (KW_ALL | KW_TEMP | KW_TEMPORARY | KW_PLANS | KW_SEQUENCES)
;
altertablestmt
: KW_ALTER KW_TABLE opt_if_exists? relation_expr (alter_table_cmds | partition_cmd)
| KW_ALTER KW_TABLE KW_ALL KW_IN opttablespace (KW_OWNED KW_BY role_list)? KW_SET KW_TABLESPACE tablespace_name_create KW_NOWAIT?
| KW_ALTER KW_TABLE opt_if_exists? table_name index_partition_cmd partitionboundspec
| KW_ALTER KW_TABLE opt_if_exists? table_name KW_DETACH KW_PARTITION qualified_name (
KW_CONCURRENTLY
| KW_FINALIZE
)?
| KW_ALTER KW_INDEX opt_if_exists? qualified_name (alter_table_cmds | index_partition_cmd)
| KW_ALTER KW_INDEX KW_ALL KW_IN opttablespace (KW_OWNED KW_BY role_list)? KW_SET opttablespace KW_NOWAIT?
| KW_ALTER KW_SEQUENCE opt_if_exists? qualified_name alter_table_cmds
| KW_ALTER KW_MATERIALIZED? KW_VIEW opt_if_exists? view_name alter_table_cmds
| KW_ALTER KW_MATERIALIZED KW_VIEW KW_ALL KW_IN opttablespace (KW_OWNED KW_BY role_list)? KW_SET KW_TABLESPACE tablespace_name_create KW_NOWAIT?
| KW_ALTER KW_FOREIGN KW_TABLE opt_if_exists? relation_expr alter_table_cmds
;
alter_table_cmds
: alter_table_cmd (COMMA alter_table_cmd)*
;
partition_cmd
: index_partition_cmd partitionboundspec
| KW_DETACH KW_PARTITION qualified_name
;
index_partition_cmd
: KW_ATTACH KW_PARTITION qualified_name
;
alter_table_cmd
: KW_ADD (KW_CONSTRAINT colid)? constraintelem
| KW_ALTER KW_CONSTRAINT colid constraintattributeElem*
| KW_VALIDATE KW_CONSTRAINT colid
| KW_DROP KW_CONSTRAINT opt_if_exists? colid opt_drop_behavior?
| KW_SET KW_WITHOUT (KW_OIDS | KW_CLUSTER)
| KW_CLUSTER KW_ON colid
| KW_SET (KW_LOGGED | KW_UNLOGGED)
| KW_ENABLE (KW_REPLICA | KW_ALWAYS)? KW_TRIGGER
| KW_DISABLE KW_TRIGGER (KW_ALL | KW_USER | colid)
| KW_ENABLE (KW_ALWAYS | KW_REPLICA) KW_RULE colid
| KW_DISABLE KW_RULE colid
| KW_NO? KW_INHERIT qualified_name
| KW_OF any_name
| KW_NOT KW_OF
| KW_OWNER KW_TO rolespec
| KW_SET KW_TABLESPACE tablespace_name_create
| KW_REPLICA KW_IDENTITY (KW_NOTHING | KW_FULL | KW_DEFAULT | existingindex)
| (KW_ENABLE | KW_DISABLE | (KW_NO? KW_FORCE)) KW_ROW KW_LEVEL KW_SECURITY
| KW_DROP KW_COLUMN? opt_if_exists? column_name opt_drop_behavior?
| KW_ADD KW_COLUMN? opt_if_not_exists? column_def
| KW_ALTER KW_COLUMN? column_name (alter_column_default | alter_generic_options)
| KW_ALTER KW_COLUMN? column_name (KW_DROP | KW_SET) KW_NOT KW_NULL
| KW_ALTER KW_COLUMN? column_name KW_DROP KW_EXPRESSION opt_if_exists?
| KW_ALTER KW_COLUMN? column_name KW_SET KW_STATISTICS signediconst
| (KW_ALTER KW_COLUMN? column_name)? (KW_SET | KW_RESET) reloptions
| KW_ALTER KW_COLUMN? column_name KW_SET KW_STORAGE colid
| KW_ALTER KW_COLUMN? column_name KW_ADD KW_GENERATED generated_when KW_AS KW_IDENTITY (
OPEN_PAREN seqoptelem+ CLOSE_PAREN
)?
| KW_ALTER KW_COLUMN? column_name (
KW_RESTART (KW_WITH? numericonly)?
| KW_SET (seqoptelem | KW_GENERATED generated_when)
)+
| KW_ALTER KW_COLUMN? column_name KW_DROP KW_IDENTITY opt_if_exists?
| KW_ALTER KW_COLUMN? column_name (KW_SET KW_DATA)? KW_TYPE typename opt_collate_clause? (
KW_USING expression
)?
| alter_generic_options
;
alter_column_default
: KW_SET KW_DEFAULT expression
| KW_DROP KW_DEFAULT
;
opt_drop_behavior
: KW_CASCADE
| KW_RESTRICT
;
opt_collate_clause
: KW_COLLATE any_name
;
reloptions
: OPEN_PAREN reloption_elem (COMMA reloption_elem)* CLOSE_PAREN
;
opt_reloptions
: KW_WITH reloptions
;
reloption_elem
: collabel (EQUAL def_arg | DOT def_elem)?
;
partitionboundspec
: KW_FOR KW_VALUES KW_WITH OPEN_PAREN KW_MODULUS numericonly COMMA KW_REMAINDER numericonly CLOSE_PAREN
| KW_FOR KW_VALUES KW_IN execute_param_clause
| KW_FOR KW_VALUES KW_FROM partitionboundexpr KW_TO partitionboundexpr
| KW_DEFAULT
;
partitionboundexpr
: OPEN_PAREN partitionboundchoose (COMMA partitionboundchoose)* CLOSE_PAREN
;
partitionboundchoose
: expr_list
| KW_MINVALUE
| KW_MAXVALUE
;
altercompositetypestmt
: KW_ALTER KW_TYPE any_name alter_type_cmd (COMMA alter_type_cmd)*
;
alter_type_cmd
: KW_ADD KW_ATTRIBUTE tablefuncelement opt_drop_behavior?
| KW_DROP KW_ATTRIBUTE opt_if_exists? colid opt_drop_behavior?
| KW_ALTER KW_ATTRIBUTE colid (KW_SET KW_DATA)? KW_TYPE typename opt_collate_clause? opt_drop_behavior?
;
closeportalstmt
: KW_CLOSE (colid | KW_ALL)
;
copystmt
: KW_COPY KW_BINARY? table_name opt_column_list? (KW_FROM | KW_TO) KW_PROGRAM? (
sconst
| KW_STDIN
| KW_STDOUT
) (KW_USING? KW_DELIMITERS sconst)? KW_WITH? copy_options where_clause?
| KW_COPY OPEN_PAREN preparablestmt CLOSE_PAREN KW_TO KW_PROGRAM? (
sconst
| KW_STDIN
| KW_STDOUT
) KW_WITH? copy_options
;
copy_options
: (
KW_BINARY
| KW_FREEZE
| (KW_NULL | KW_DELIMITER | KW_ESCAPE | KW_QUOTE) KW_AS? sconst
| KW_CSV
| KW_HEADER
| KW_FORCE KW_QUOTE (column_list | STAR)
| KW_FORCE KW_NOT? KW_NULL column_list
| KW_ENCODING sconst
)*
| OPEN_PAREN copy_generic_opt_elem (COMMA copy_generic_opt_elem)* CLOSE_PAREN
;
copy_generic_opt_elem
: collabel (
opt_boolean_or_string
| numericonly
| STAR
| OPEN_PAREN opt_boolean_or_string_column (COMMA opt_boolean_or_string_column)* CLOSE_PAREN
)?
;
createstmt
: KW_CREATE opttemp? KW_TABLE opt_if_not_exists? table_name_create (
OPEN_PAREN tableelementlist? CLOSE_PAREN optinherit? partitionspec? table_access_method_clause? optwith? oncommitoption? opttablespace?
| KW_OF any_name opttypedtableelementlist? partitionspec? table_access_method_clause? optwith? oncommitoption? opttablespace?
| KW_PARTITION KW_OF qualified_name opttypedtableelementlist? partitionboundspec partitionspec? table_access_method_clause? optwith?
oncommitoption? opttablespace?
) # columnCreateTable
;
opttemp
: KW_TEMPORARY
| KW_TEMP
| (KW_LOCAL | KW_GLOBAL) (KW_TEMPORARY | KW_TEMP)
| KW_UNLOGGED
;
opttypedtableelementlist
: OPEN_PAREN typedtableelement (COMMA typedtableelement)* CLOSE_PAREN
;
tableelementlist
: tableelement (COMMA tableelement)*
;
tableelement
: (KW_CONSTRAINT colid)? constraintelem
| column_def
| KW_LIKE qualified_name ((KW_INCLUDING | KW_EXCLUDING) tablelikeoption)*
;
typedtableelement
: column_name_create (KW_WITH KW_OPTIONS)? colconstraint*
| (KW_CONSTRAINT colid)? constraintelem
;
column_def
: column_name_create colType=typename create_generic_options? (
KW_STORAGE (KW_PLAIN | KW_EXTERNAL | KW_EXTENDED | KW_MAIN | KW_DEFAULT | colid)
)? (KW_COMPRESSION colid)? (opt_collate_clause)? (KW_WITH KW_OPTIONS)? colconstraint*
;
colconstraint
: (KW_CONSTRAINT colid)? colconstraintelem (KW_NOT? KW_DEFERRABLE)? (
KW_INITIALLY (KW_DEFERRED | KW_IMMEDIATE)
)?
;
colconstraintelem
: KW_NOT? KW_NULL
| KW_UNIQUE opt_definition? optconstablespace?
| KW_UNIQUE (KW_NULLS KW_NOT? KW_DISTINCT)? (
(KW_INCLUDE index_params)? with_clause? optconstablespace?
)
| KW_PRIMARY KW_KEY opt_definition? optconstablespace?
| KW_CHECK OPEN_PAREN expression CLOSE_PAREN (KW_NO KW_INHERIT)?
| KW_DEFAULT primaryExpression
| KW_GENERATED generated_when KW_AS (
KW_IDENTITY (OPEN_PAREN seqoptelem+ CLOSE_PAREN)?
| OPEN_PAREN expression CLOSE_PAREN KW_STORED
)
| KW_REFERENCES qualified_name opt_column_list? key_match? key_actions?
| opt_collate_clause
;
generated_when
: KW_ALWAYS
| KW_BY KW_DEFAULT
;
tablelikeoption
: KW_COMMENTS
| KW_CONSTRAINTS
| KW_DEFAULTS
| KW_IDENTITY
| KW_GENERATED
| KW_INDEXES
| KW_STATISTICS
| KW_STORAGE
| KW_ALL
;
constraintelem
: KW_CHECK OPEN_PAREN expression CLOSE_PAREN constraintattributeElem*
| (KW_UNIQUE | ( KW_PRIMARY KW_KEY)) (
opt_column_list opt_c_include? opt_definition? optconstablespace? constraintattributeElem*
| existingindex constraintattributeElem*
)
| KW_EXCLUDE table_access_method_clause? OPEN_PAREN exclusionconstraintelem (
COMMA exclusionconstraintelem
)* CLOSE_PAREN opt_c_include? opt_definition? optconstablespace? (
KW_WHERE OPEN_PAREN expression CLOSE_PAREN
)? constraintattributeElem*
| KW_FOREIGN KW_KEY opt_column_list KW_REFERENCES qualified_name opt_column_list? key_match? key_actions? constraintattributeElem*
;
opt_column_list
: OPEN_PAREN column_list CLOSE_PAREN
;
opt_column_list_create
: OPEN_PAREN column_name_create (COMMA column_name_create)* CLOSE_PAREN
;
column_list
: column_name (COMMA column_name)*
;
opt_c_include
: KW_INCLUDE opt_column_list
;
key_match
: KW_MATCH (KW_FULL | KW_PARTIAL | KW_SIMPLE)
;
exclusionconstraintelem
: index_elem KW_WITH (any_operator | KW_OPERATOR OPEN_PAREN any_operator CLOSE_PAREN)
;
key_actions
: key_update key_delete?
| key_delete key_update?
;
key_update
: KW_ON KW_UPDATE key_action
;
key_delete
: KW_ON KW_DELETE key_action
;
key_action
: KW_NO KW_ACTION
| KW_RESTRICT
| KW_CASCADE
| KW_SET (KW_NULL | KW_DEFAULT) column_list?
;
optinherit
: KW_INHERITS OPEN_PAREN qualified_name_list CLOSE_PAREN
;
partitionspec
: KW_PARTITION KW_BY colid OPEN_PAREN part_elem (COMMA part_elem)* CLOSE_PAREN
;
part_elem
: (column_name | func_expr_windowless | (OPEN_PAREN expression CLOSE_PAREN)) opt_collate_clause? any_name?
;
table_access_method_clause
: KW_USING colid
;
optwith
: KW_WITH reloptions
| (KW_WITHOUT | KW_WITH) KW_OIDS
;
oncommitoption
: KW_ON KW_COMMIT (KW_DROP | KW_DELETE KW_ROWS | KW_PRESERVE KW_ROWS)
;
opttablespace
: KW_TABLESPACE tablespace_name
;
optconstablespace
: KW_USING KW_INDEX opttablespace
;
existingindex
: KW_USING KW_INDEX colid
;
createstatsstmt
: KW_CREATE KW_STATISTICS (opt_if_not_exists? any_name)? opt_column_list? KW_ON (
column_expr (COMMA column_expr)*
| expr_list
) from_clause
;
alterstatsstmt
: KW_ALTER KW_STATISTICS opt_if_exists? any_name KW_SET KW_STATISTICS signediconst
;
createasstmt
: KW_CREATE opttemp? KW_TABLE opt_if_not_exists? create_as_target KW_AS selectstmt opt_with_data? # queryCreateTable
;
create_as_target
: table_name_create opt_column_list_create? table_access_method_clause? optwith? oncommitoption? opttablespace?
;
opt_with_data
: KW_WITH (KW_DATA | KW_NO KW_DATA)
;
creatematviewstmt
: KW_CREATE KW_UNLOGGED? KW_MATERIALIZED KW_VIEW opt_if_not_exists? view_name_create opt_column_list_create? table_access_method_clause?
opt_reloptions? opttablespace? KW_AS selectstmt opt_with_data? # createMaterializedView
;
refreshmatviewstmt
: KW_REFRESH KW_MATERIALIZED KW_VIEW KW_CONCURRENTLY? view_name opt_with_data?
;
createseqstmt
: KW_CREATE opttemp? KW_SEQUENCE opt_if_not_exists? qualified_name (seqoptelem+)?
;
alterseqstmt
: KW_ALTER KW_SEQUENCE opt_if_exists? qualified_name seqoptelem+
;
seqoptelem
: KW_AS simpletypename
| KW_CACHE numericonly?
| KW_INCREMENT KW_BY? numericonly
| (KW_MAXVALUE | KW_MINVALUE) numericonly
| KW_NO (KW_MAXVALUE | KW_MINVALUE | KW_CYCLE)
| KW_OWNED KW_BY column_name
| KW_SEQUENCE KW_NAME any_name
| KW_START KW_WITH? numericonly
| KW_RESTART KW_WITH? numericonly?
;
numericonly
: (PLUS | MINUS)? Numeric
| signediconst
;
createplangstmt
: KW_CREATE opt_or_replace? KW_TRUSTED? KW_PROCEDURAL? KW_LANGUAGE colid (
KW_HANDLER any_name (KW_INLINE any_name)? (KW_VALIDATOR any_name | KW_NO KW_VALIDATOR)?
)?
;
createtablespacestmt
: KW_CREATE opttablespace (KW_OWNER rolespec)? KW_LOCATION sconst opt_reloptions?
;
createextensionstmt
: KW_CREATE KW_EXTENSION opt_if_not_exists? colid KW_WITH? (
KW_SCHEMA schema_name
| (KW_VERSION | KW_FROM) nonreservedword_or_sconst
| KW_CASCADE
)*
;
alterextensionstmt
: KW_ALTER KW_EXTENSION colid KW_UPDATE (KW_TO nonreservedword_or_sconst)*
;
alterextensioncontentsstmt
: KW_ALTER KW_EXTENSION colid (KW_ADD | KW_DROP) (
KW_TRANSFORM KW_FOR typename KW_LANGUAGE colid
| KW_ROUTINE routine_with_argtypes
| KW_PROCEDURE procedure_with_argtypes
| KW_OPERATOR (KW_CLASS | KW_FAMILY) any_name table_access_method_clause
| KW_OPERATOR operator_with_argtypes
| KW_FUNCTION function_with_argtypes
| (KW_DOMAIN | KW_TYPE) typename
| KW_CAST OPEN_PAREN typename KW_AS typename CLOSE_PAREN
| KW_AGGREGATE aggregate_with_argtypes
| object_type_name
| object_type_any_name
)
;
createfdwstmt
: KW_CREATE KW_FOREIGN KW_DATA KW_WRAPPER colid (fdw_option+)? create_generic_options?
;
fdw_option
: (KW_HANDLER | KW_VALIDATOR) any_name?
| KW_NO (KW_HANDLER | KW_VALIDATOR)
;
alterfdwstmt
: KW_ALTER KW_FOREIGN KW_DATA KW_WRAPPER colid (
(fdw_option+)? alter_generic_options
| fdw_option+
)
;
create_generic_options
: KW_OPTIONS OPEN_PAREN generic_option_elem (COMMA generic_option_elem)* CLOSE_PAREN
;
alter_generic_options
: KW_OPTIONS OPEN_PAREN alter_generic_option_elem (COMMA alter_generic_option_elem)* CLOSE_PAREN
;
alter_generic_option_elem
: (KW_SET | KW_ADD | KW_DROP) generic_option_elem
;
generic_option_elem
: collabel sconst
;
createforeignserverstmt
: KW_CREATE KW_SERVER opt_if_not_exists? colid (KW_TYPE sconst)? foreign_server_version? KW_FOREIGN KW_DATA KW_WRAPPER colid
create_generic_options?
;
foreign_server_version
: KW_VERSION (sconst | KW_NULL)
;
alterforeignserverstmt
: KW_ALTER KW_SERVER colid (
alter_generic_options
| foreign_server_version alter_generic_options?
)
;
createforeigntablestmt
: KW_CREATE KW_FOREIGN KW_TABLE opt_if_not_exists? table_name_create OPEN_PAREN tableelementlist? CLOSE_PAREN optinherit? KW_SERVER colid
create_generic_options? # createForeignTable
| KW_CREATE KW_FOREIGN KW_TABLE opt_if_not_exists? table_name_create KW_PARTITION KW_OF table_name opttypedtableelementlist? partitionboundspec
KW_SERVER colid create_generic_options? # createPartitionForeignTable
;
importforeignschemastmt
: KW_IMPORT KW_FOREIGN KW_SCHEMA schema_name (
(KW_LIMIT KW_TO | KW_EXCEPT) OPEN_PAREN relation_expr_list CLOSE_PAREN
)? KW_FROM KW_SERVER colid KW_INTO colid create_generic_options?
;
createusermappingstmt
: KW_CREATE KW_USER KW_MAPPING opt_if_not_exists? KW_FOR (rolespec | KW_USER) KW_SERVER colid create_generic_options?
;
alterusermappingstmt
: KW_ALTER KW_USER KW_MAPPING KW_FOR (rolespec | KW_USER) KW_SERVER colid alter_generic_options
;
createpolicystmt
: KW_CREATE KW_POLICY colid KW_ON qualified_name (
KW_AS (KW_PERMISSIVE | KW_RESTRICTIVE | identifier)
)? (KW_FOR (KW_ALL | KW_SELECT | KW_INSERT | KW_UPDATE | KW_DELETE))? (KW_TO role_list)? rowsecurityoptionalexpr? rowsecurityoptionalwithcheck?
;
alterpolicystmt
: KW_ALTER KW_POLICY colid KW_ON qualified_name (KW_TO role_list)? rowsecurityoptionalexpr? rowsecurityoptionalwithcheck?
;
alterprocedurestmt
: KW_ALTER KW_PROCEDURE procedure_name func_args? (
procedure_action (procedure_action)* KW_RESTRICT?
| KW_RENAME KW_TO procedure_name_create
| KW_OWNER KW_TO rolespec
| KW_SET KW_SCHEMA schema_name_create
| KW_NO? KW_DEPENDS KW_ON KW_EXTENSION colid
)
;
procedure_action
: (KW_EXTERNAL? KW_SECURITY KW_INVOKER | KW_EXTERNAL? KW_SECURITY KW_DEFINER)
| KW_SET colid (KW_TO | STAR) (colid | KW_DEFAULT)
| KW_SET colid KW_FROM KW_CURRENT
| KW_RESET (colid | KW_ALL)
;
rowsecurityoptionalexpr
: KW_USING OPEN_PAREN expression CLOSE_PAREN
;
rowsecurityoptionalwithcheck
: KW_WITH KW_CHECK OPEN_PAREN expression CLOSE_PAREN
;
createamstmt
: KW_CREATE KW_ACCESS KW_METHOD colid KW_TYPE (KW_INDEX | KW_TABLE) KW_HANDLER any_name
;
createtrigstmt
: KW_CREATE opt_or_replace? KW_TRIGGER colid triggeractiontime triggerevents KW_ON table_name (
KW_REFERENCING ((KW_NEW | KW_OLD) (KW_TABLE | KW_ROW) KW_AS? colid)+
)? (KW_FOR KW_EACH? (KW_ROW | KW_STATEMENT))? triggerwhen? KW_EXECUTE function_or_procedure OPEN_PAREN triggerfuncargs CLOSE_PAREN
| KW_CREATE opt_or_replace? KW_CONSTRAINT? KW_TRIGGER colid triggeractiontime triggerevents KW_ON table_name (
KW_FROM qualified_name
)? constraintattributeElem* (KW_FOR KW_EACH? (KW_ROW | KW_STATEMENT))? triggerwhen? KW_EXECUTE function_or_procedure OPEN_PAREN triggerfuncargs
CLOSE_PAREN
;
triggeractiontime
: KW_BEFORE
| KW_AFTER
| KW_INSTEAD KW_OF
;
triggerevents
: triggeroneevent (KW_OR triggeroneevent)*
;
triggeroneevent
: KW_INSERT
| KW_DELETE
| KW_UPDATE (KW_OF column_list)?
| KW_TRUNCATE
;
triggerwhen
: KW_WHEN OPEN_PAREN expression CLOSE_PAREN
;
function_or_procedure
: KW_FUNCTION function_name
| KW_PROCEDURE procedure_name
;
triggerfuncargs
: (triggerfuncarg |) (COMMA triggerfuncarg)*
;
triggerfuncarg
: Integral
| Numeric
| sconst
| collabel
;
constraintattributeElem
: KW_NOT? KW_DEFERRABLE
| KW_INITIALLY (KW_IMMEDIATE | KW_DEFERRED)