-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathparse_test.go
1950 lines (1822 loc) · 70.7 KB
/
parse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 The Cockroach Authors.
//
// 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.
package parser
import (
"bytes"
"fmt"
"go/constant"
"reflect"
"regexp"
"strings"
"testing"
"unicode/utf8"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/testutils"
_ "github.com/cockroachdb/cockroach/pkg/util/log" // for flags
)
// TestParse verifies that we can parse the supplied SQL and regenerate the SQL
// string from the syntax tree.
func TestParse(t *testing.T) {
testData := []struct {
sql string
}{
{``},
{`VALUES ("")`},
{`BEGIN TRANSACTION`},
{`BEGIN TRANSACTION READ ONLY`},
{`BEGIN TRANSACTION READ WRITE`},
{`BEGIN TRANSACTION ISOLATION LEVEL SNAPSHOT`},
{`BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE`},
{`BEGIN TRANSACTION PRIORITY LOW`},
{`BEGIN TRANSACTION PRIORITY NORMAL`},
{`BEGIN TRANSACTION PRIORITY HIGH`},
{`BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE, PRIORITY HIGH`},
{`BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE, PRIORITY HIGH, READ WRITE`},
{`COMMIT TRANSACTION`},
{`ROLLBACK TRANSACTION`},
{"SAVEPOINT foo"},
{`CREATE DATABASE a`},
{`CREATE DATABASE a TEMPLATE = 'template0'`},
{`CREATE DATABASE a TEMPLATE = 'invalid'`},
{`CREATE DATABASE a ENCODING = 'UTF8'`},
{`CREATE DATABASE a ENCODING = 'INVALID'`},
{`CREATE DATABASE a LC_COLLATE = 'C.UTF-8'`},
{`CREATE DATABASE a LC_COLLATE = 'INVALID'`},
{`CREATE DATABASE a LC_CTYPE = 'C.UTF-8'`},
{`CREATE DATABASE a LC_CTYPE = 'INVALID'`},
{`CREATE DATABASE a TEMPLATE = 'template0' ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'INVALID'`},
{`CREATE DATABASE IF NOT EXISTS a`},
{`CREATE DATABASE IF NOT EXISTS a TEMPLATE = 'template0'`},
{`CREATE DATABASE IF NOT EXISTS a TEMPLATE = 'invalid'`},
{`CREATE DATABASE IF NOT EXISTS a ENCODING = 'UTF8'`},
{`CREATE DATABASE IF NOT EXISTS a ENCODING = 'INVALID'`},
{`CREATE DATABASE IF NOT EXISTS a LC_COLLATE = 'C.UTF-8'`},
{`CREATE DATABASE IF NOT EXISTS a LC_COLLATE = 'INVALID'`},
{`CREATE DATABASE IF NOT EXISTS a LC_CTYPE = 'C.UTF-8'`},
{`CREATE DATABASE IF NOT EXISTS a LC_CTYPE = 'INVALID'`},
{`CREATE DATABASE IF NOT EXISTS a TEMPLATE = 'template0' ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'INVALID'`},
{`CREATE INDEX a ON b (c)`},
{`CREATE INDEX a ON b.c (d)`},
{`CREATE INDEX ON a (b)`},
{`CREATE INDEX ON a (b) STORING (c)`},
{`CREATE INDEX ON a (b) INTERLEAVE IN PARENT c (d)`},
{`CREATE INDEX ON a (b) INTERLEAVE IN PARENT c.d (e)`},
{`CREATE INDEX ON a (b ASC, c DESC)`},
{`CREATE UNIQUE INDEX a ON b (c)`},
{`CREATE UNIQUE INDEX a ON b (c) STORING (d)`},
{`CREATE UNIQUE INDEX a ON b (c) INTERLEAVE IN PARENT d (e, f)`},
{`CREATE UNIQUE INDEX a ON b (c) INTERLEAVE IN PARENT d.e (f, g)`},
{`CREATE UNIQUE INDEX a ON b.c (d)`},
{`CREATE TABLE a ()`},
{`CREATE TABLE a (b INT)`},
{`CREATE TABLE a (b INT, c INT)`},
{`CREATE TABLE a (b CHAR)`},
{`CREATE TABLE a (b CHAR(3))`},
{`CREATE TABLE a (b VARCHAR)`},
{`CREATE TABLE a (b VARCHAR(3))`},
{`CREATE TABLE a (b STRING)`},
{`CREATE TABLE a (b STRING(3))`},
{`CREATE TABLE a (b FLOAT)`},
{`CREATE TABLE a (b SERIAL)`},
{`CREATE TABLE a (b SMALLSERIAL)`},
{`CREATE TABLE a (b BIGSERIAL)`},
{`CREATE TABLE a (b TIME)`},
{`CREATE TABLE a (b UUID)`},
{`CREATE TABLE a (b INET)`},
{`CREATE TABLE a (b INT NULL)`},
{`CREATE TABLE a (b INT CONSTRAINT maybe NULL)`},
{`CREATE TABLE a (b INT NOT NULL)`},
{`CREATE TABLE a (b INT CONSTRAINT always NOT NULL)`},
{`CREATE TABLE a (b INT PRIMARY KEY)`},
{`CREATE TABLE a (b INT UNIQUE)`},
{`CREATE TABLE a (b INT NULL PRIMARY KEY)`},
{`CREATE TABLE a (b INT DEFAULT 1)`},
{`CREATE TABLE a (b INT CONSTRAINT one DEFAULT 1)`},
{`CREATE TABLE a (b INT DEFAULT now())`},
{`CREATE TABLE a (a INT CHECK (a > 0))`},
{`CREATE TABLE a (a INT CONSTRAINT positive CHECK (a > 0))`},
{`CREATE TABLE a (a INT DEFAULT 1 CHECK (a > 0))`},
{`CREATE TABLE a (a INT CONSTRAINT one DEFAULT 1 CHECK (a > 0))`},
{`CREATE TABLE a (a INT DEFAULT 1 CONSTRAINT positive CHECK (a > 0))`},
{`CREATE TABLE a (a INT CONSTRAINT one DEFAULT 1 CONSTRAINT positive CHECK (a > 0))`},
{`CREATE TABLE a (a INT CONSTRAINT one CHECK (a > 0) CONSTRAINT two CHECK (a < 10))`},
// "0" lost quotes previously.
{`CREATE TABLE a (b INT, c TEXT, PRIMARY KEY (b, c, "0"))`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON UPDATE RESTRICT)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON DELETE RESTRICT)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON DELETE RESTRICT ON UPDATE RESTRICT)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON UPDATE CASCADE)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON DELETE CASCADE)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON DELETE CASCADE ON UPDATE CASCADE)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON UPDATE SET NULL)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON DELETE SET NULL)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON DELETE SET NULL ON UPDATE SET NULL)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON UPDATE SET DEFAULT)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON DELETE SET DEFAULT)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON DELETE SET DEFAULT ON UPDATE SET DEFAULT)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON DELETE RESTRICT ON UPDATE SET DEFAULT)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON DELETE SET DEFAULT ON UPDATE CASCADE)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON DELETE CASCADE ON UPDATE SET NULL)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b) REFERENCES other ON DELETE SET NULL ON UPDATE RESTRICT)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b, c) REFERENCES other)`},
{`CREATE TABLE a (b INT, c TEXT, FOREIGN KEY (b, c) REFERENCES other (x, y))`},
{`CREATE TABLE a (b INT, c TEXT, CONSTRAINT s FOREIGN KEY (b, c) REFERENCES other (x, y))`},
{`CREATE TABLE a (b INT, c TEXT, INDEX (b, c))`},
{`CREATE TABLE a (b INT, c TEXT, INDEX d (b, c))`},
{`CREATE TABLE a (b INT, c TEXT, CONSTRAINT d UNIQUE (b, c))`},
{`CREATE TABLE a (b INT, c TEXT, CONSTRAINT d UNIQUE (b, c) INTERLEAVE IN PARENT d (e, f))`},
{`CREATE TABLE a (b INT, UNIQUE (b))`},
{`CREATE TABLE a (b INT, UNIQUE (b) STORING (c))`},
{`CREATE TABLE a (b INT, INDEX (b))`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON UPDATE RESTRICT)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON DELETE RESTRICT)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON DELETE RESTRICT ON UPDATE RESTRICT)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON UPDATE CASCADE)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON DELETE CASCADE)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON DELETE CASCADE ON UPDATE CASCADE)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON UPDATE SET NULL)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON DELETE SET NULL)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON DELETE SET NULL ON UPDATE SET NULL)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON UPDATE SET DEFAULT)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON DELETE SET DEFAULT)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON DELETE SET DEFAULT ON UPDATE SET DEFAULT)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON DELETE RESTRICT ON UPDATE SET DEFAULT)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON DELETE SET DEFAULT ON UPDATE CASCADE)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON DELETE CASCADE ON UPDATE SET NULL)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON DELETE SET NULL ON UPDATE RESTRICT)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON UPDATE CASCADE)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON UPDATE SET NULL)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo ON UPDATE SET DEFAULT)`},
{`CREATE TABLE a (b INT, c INT CONSTRAINT ref REFERENCES foo)`},
{`CREATE TABLE a (b INT, c INT REFERENCES foo (bar))`},
{`CREATE TABLE a (b INT, INDEX (b) STORING (c))`},
{`CREATE TABLE a (b INT, c TEXT, INDEX (b ASC, c DESC) STORING (c))`},
{`CREATE TABLE a (b INT, INDEX (b) INTERLEAVE IN PARENT c (d, e))`},
{`CREATE TABLE a (b INT, FAMILY (b))`},
{`CREATE TABLE a (b INT, c STRING, FAMILY foo (b), FAMILY (c))`},
{`CREATE TABLE a (b INT) INTERLEAVE IN PARENT foo (c, d)`},
{`CREATE TABLE a (b INT) INTERLEAVE IN PARENT foo (c) CASCADE`},
{`CREATE TABLE a.b (b INT)`},
{`CREATE TABLE IF NOT EXISTS a (b INT)`},
{`CREATE TABLE a (b INT) PARTITION BY LIST (b) (PARTITION p1 VALUES (1, DEFAULT), PARTITION p2 VALUES (2), (3))`},
{`CREATE TABLE a (b INT) PARTITION BY RANGE (b) (PARTITION p1 VALUES LESS THAN (1), PARTITION p2 VALUES LESS THAN (2, 3), PARTITION p3 VALUES LESS THAN (MAXVALUE))`},
// This montrosity was added on the assumption that it's more readable
// than all on one line. Feel free to rip it out if you come across it
// and disagree.
{regexp.MustCompile(`\n\s*`).ReplaceAllLiteralString(
`CREATE TABLE a (b INT, c INT, d INT) PARTITION BY LIST (b) (
PARTITION p1 VALUES (1) PARTITION BY LIST (c) (
PARTITION p1_1 VALUES (3), PARTITION p1_2 VALUES (4, 5)
), PARTITION p2 VALUES (6) PARTITION BY RANGE (c) (
PARTITION p2_1 VALUES LESS THAN (7) PARTITION BY LIST (d) (
PARTITION p2_1_1 VALUES (8)
)
)
)`, ``),
},
{`CREATE TABLE a AS SELECT * FROM b`},
{`CREATE TABLE IF NOT EXISTS a AS SELECT * FROM b`},
{`CREATE TABLE a AS SELECT * FROM b ORDER BY c`},
{`CREATE TABLE IF NOT EXISTS a AS SELECT * FROM b ORDER BY c`},
{`CREATE TABLE a AS SELECT * FROM b LIMIT 3`},
{`CREATE TABLE IF NOT EXISTS a AS SELECT * FROM b LIMIT 3`},
{`CREATE TABLE a AS VALUES ('one', 1), ('two', 2), ('three', 3)`},
{`CREATE TABLE IF NOT EXISTS a AS VALUES ('one', 1), ('two', 2), ('three', 3)`},
{`CREATE TABLE a (str, num) AS VALUES ('one', 1), ('two', 2), ('three', 3)`},
{`CREATE TABLE IF NOT EXISTS a (str, num) AS VALUES ('one', 1), ('two', 2), ('three', 3)`},
{`CREATE TABLE a AS SELECT * FROM b UNION SELECT * FROM c`},
{`CREATE TABLE IF NOT EXISTS a AS SELECT * FROM b UNION SELECT * FROM c`},
{`CREATE TABLE a AS SELECT * FROM b UNION VALUES ('one', 1) ORDER BY c LIMIT 5`},
{`CREATE TABLE IF NOT EXISTS a AS SELECT * FROM b UNION VALUES ('one', 1) ORDER BY c LIMIT 5`},
{`CREATE TABLE a (b STRING COLLATE "DE")`},
{`CREATE TABLE a (b STRING[] COLLATE "DE")`},
{`CREATE VIEW a AS SELECT * FROM b`},
{`CREATE VIEW a AS SELECT b.* FROM b LIMIT 5`},
{`CREATE VIEW a AS (SELECT c, d FROM b WHERE c > 0 ORDER BY c)`},
{`CREATE VIEW a (x, y) AS SELECT c, d FROM b`},
{`CREATE VIEW a AS VALUES (1, 'one'), (2, 'two')`},
{`CREATE VIEW a (x, y) AS VALUES (1, 'one'), (2, 'two')`},
{`CREATE VIEW a AS TABLE b`},
{`DELETE FROM a`},
{`DELETE FROM a.b`},
{`DELETE FROM a WHERE a = b`},
{`DELETE FROM a WHERE a = b LIMIT c`},
{`DELETE FROM a WHERE a = b RETURNING a, b`},
{`DELETE FROM a WHERE a = b RETURNING 1, 2`},
{`DELETE FROM a WHERE a = b RETURNING a + b`},
{`DELETE FROM a WHERE a = b RETURNING NOTHING`},
{`DISCARD ALL`},
{`DROP DATABASE a`},
{`DROP DATABASE IF EXISTS a`},
{`DROP DATABASE a CASCADE`},
{`DROP DATABASE a RESTRICT`},
{`DROP TABLE a`},
{`DROP TABLE a.b`},
{`DROP TABLE a, b`},
{`DROP TABLE IF EXISTS a`},
{`DROP TABLE a RESTRICT`},
{`DROP TABLE a.b RESTRICT`},
{`DROP TABLE a, b RESTRICT`},
{`DROP TABLE IF EXISTS a RESTRICT`},
{`DROP TABLE a CASCADE`},
{`DROP TABLE a.b CASCADE`},
{`DROP TABLE a, b CASCADE`},
{`DROP TABLE IF EXISTS a CASCADE`},
{`DROP INDEX a.b@c`},
{`DROP INDEX a`},
{`DROP INDEX a.b`},
{`DROP INDEX IF EXISTS a.b@c`},
{`DROP INDEX a.b@c, d@f`},
{`DROP INDEX IF EXISTS a.b@c, d@f`},
{`DROP INDEX a.b@c CASCADE`},
{`DROP INDEX IF EXISTS a.b@c RESTRICT`},
{`DROP VIEW a`},
{`DROP VIEW a.b`},
{`DROP VIEW a, b`},
{`DROP VIEW IF EXISTS a`},
{`DROP VIEW a RESTRICT`},
{`DROP VIEW IF EXISTS a, b RESTRICT`},
{`DROP VIEW a.b CASCADE`},
{`DROP VIEW a, b CASCADE`},
{`CANCEL JOB a`},
{`CANCEL QUERY a`},
{`RESUME JOB a`},
{`PAUSE JOB a`},
{`EXPLAIN SELECT 1`},
{`EXPLAIN EXPLAIN SELECT 1`},
{`EXPLAIN (A, B, C) SELECT 1`},
{`SELECT * FROM [EXPLAIN SELECT 1]`},
{`SELECT * FROM [SHOW TRANSACTION STATUS]`},
{`SHOW barfoo`},
{`SHOW database`},
{`SHOW TIME ZONE`},
{`SHOW CLUSTER SETTING a`},
{`SHOW CLUSTER SETTING all`},
{`SHOW DATABASES`},
{`SHOW TABLES`},
{`SHOW TABLES FROM a`},
{`SHOW COLUMNS FROM a`},
{`SHOW COLUMNS FROM a.b.c`},
{`SHOW INDEXES FROM a`},
{`SHOW INDEXES FROM a.b.c`},
{`SHOW CONSTRAINTS FROM a`},
{`SHOW CONSTRAINTS FROM a.b.c`},
{`SHOW TABLES FROM a; SHOW COLUMNS FROM b`},
{`SHOW USERS`},
{`SHOW JOBS`},
{`SHOW CLUSTER QUERIES`},
{`SHOW LOCAL QUERIES`},
{`SHOW CLUSTER SESSIONS`},
{`SHOW LOCAL SESSIONS`},
{`SHOW TRACE FOR SESSION`},
{`SHOW KV TRACE FOR SESSION`},
{`SHOW TRACE FOR SELECT 42`},
{`SHOW TRACE FOR TABLE foo`},
{`SHOW KV TRACE FOR TABLE foo`},
{`SHOW TESTING_RANGES FROM TABLE d.t`},
{`SHOW TESTING_RANGES FROM TABLE t`},
{`SHOW TESTING_RANGES FROM INDEX d.t@i`},
{`SHOW TESTING_RANGES FROM INDEX t@i`},
{`SHOW TESTING_RANGES FROM INDEX d.i`},
{`SHOW TESTING_RANGES FROM INDEX i`},
{`SHOW EXPERIMENTAL_FINGERPRINTS FROM TABLE d.t`},
{`EXPERIMENTAL SHOW ZONE CONFIGURATIONS`},
{`EXPERIMENTAL SHOW ZONE CONFIGURATION FOR RANGE default`},
{`EXPERIMENTAL SHOW ZONE CONFIGURATION FOR RANGE meta`},
{`EXPERIMENTAL SHOW ZONE CONFIGURATION FOR DATABASE db`},
{`EXPERIMENTAL SHOW ZONE CONFIGURATION FOR TABLE db.t`},
{`EXPERIMENTAL SHOW ZONE CONFIGURATION FOR TABLE t`},
// Tables are the default, but can also be specified with
// GRANT x ON TABLE y. However, the stringer does not output TABLE.
{`SHOW GRANTS`},
{`SHOW GRANTS ON foo`},
{`SHOW GRANTS ON foo, db.foo`},
{`SHOW GRANTS ON DATABASE foo, bar`},
{`SHOW GRANTS ON DATABASE foo FOR bar`},
{`SHOW GRANTS FOR bar, baz`},
{`SHOW TRANSACTION ISOLATION LEVEL`},
{`SHOW TRANSACTION PRIORITY`},
{`PREPARE a AS SELECT 1`},
{`PREPARE a (INT) AS SELECT $1`},
{`PREPARE a (STRING, STRING) AS SELECT $1, $2`},
{`PREPARE a AS INSERT INTO a VALUES (1)`},
{`PREPARE a (INT) AS INSERT INTO a VALUES ($1)`},
{`PREPARE a AS UPDATE a SET b = 1`},
{`PREPARE a (INT) AS UPDATE a SET b = $1`},
{`PREPARE a AS UPSERT INTO a VALUES (1)`},
{`PREPARE a (INT) AS UPSERT INTO a VALUES ($1)`},
{`PREPARE a AS DELETE FROM a`},
{`PREPARE a (INT) AS DELETE FROM a WHERE b = $1`},
{`PREPARE a AS BACKUP DATABASE a TO 'b'`},
{`PREPARE a (STRING) AS BACKUP DATABASE a TO $1`},
{`PREPARE a AS RESTORE DATABASE a FROM 'b'`},
{`PREPARE a (STRING) AS RESTORE DATABASE a FROM $1`},
{`PREPARE a AS CANCEL QUERY 1`},
{`PREPARE a (STRING) AS CANCEL QUERY $1`},
{`PREPARE a AS CANCEL JOB 1`},
{`PREPARE a (INT) AS CANCEL JOB $1`},
{`PREPARE a AS PAUSE JOB 1`},
{`PREPARE a (INT) AS PAUSE JOB $1`},
{`PREPARE a AS RESUME JOB 1`},
{`PREPARE a (INT) AS RESUME JOB $1`},
{`PREPARE a AS IMPORT TABLE a CREATE USING 'b' CSV DATA ('c') WITH temp = 'd'`},
{`PREPARE a (STRING, STRING, STRING) AS IMPORT TABLE a CREATE USING $1 CSV DATA ($2) WITH temp = $3`},
{`EXECUTE a`},
{`EXECUTE a (1)`},
{`EXECUTE a (1, 1)`},
{`EXECUTE a (1 + 1)`},
{`DEALLOCATE a`},
{`DEALLOCATE ALL`},
// Tables are the default, but can also be specified with
// GRANT x ON TABLE y. However, the stringer does not output TABLE.
{`GRANT SELECT ON foo TO root`},
{`GRANT SELECT, DELETE, UPDATE ON foo, db.foo TO root, bar`},
{`GRANT DROP ON DATABASE foo TO root`},
{`GRANT ALL ON DATABASE foo TO root, test`},
{`GRANT SELECT, INSERT ON DATABASE bar TO foo, bar, baz`},
{`GRANT SELECT, INSERT ON DATABASE db1, db2 TO foo, bar, baz`},
{`GRANT SELECT, INSERT ON DATABASE db1, db2 TO "test-user"`},
// Tables are the default, but can also be specified with
// REVOKE x ON TABLE y. However, the stringer does not output TABLE.
{`REVOKE SELECT ON foo FROM root`},
{`REVOKE UPDATE, DELETE ON foo, db.foo FROM root, bar`},
{`REVOKE INSERT ON DATABASE foo FROM root`},
{`REVOKE ALL ON DATABASE foo FROM root, test`},
{`REVOKE SELECT, INSERT ON DATABASE bar FROM foo, bar, baz`},
{`REVOKE SELECT, INSERT ON DATABASE db1, db2 FROM foo, bar, baz`},
{`INSERT INTO a VALUES (1)`},
{`INSERT INTO a.b VALUES (1)`},
{`INSERT INTO a VALUES (1, 2)`},
{`INSERT INTO a VALUES (1, DEFAULT)`},
{`INSERT INTO a VALUES (1, 2), (3, 4)`},
{`INSERT INTO a VALUES (a + 1, 2 * 3)`},
{`INSERT INTO a(a, b) VALUES (1, 2)`},
{`INSERT INTO a(a, a.b) VALUES (1, 2)`},
{`INSERT INTO a SELECT b, c FROM d`},
{`INSERT INTO a DEFAULT VALUES`},
{`INSERT INTO a VALUES (1) RETURNING a, b`},
{`INSERT INTO a VALUES (1, 2) RETURNING 1, 2`},
{`INSERT INTO a VALUES (1, 2) RETURNING a + b, c`},
{`INSERT INTO a VALUES (1, 2) RETURNING NOTHING`},
{`UPSERT INTO a VALUES (1)`},
{`UPSERT INTO a.b VALUES (1)`},
{`UPSERT INTO a VALUES (1, 2)`},
{`UPSERT INTO a VALUES (1, DEFAULT)`},
{`UPSERT INTO a VALUES (1, 2), (3, 4)`},
{`UPSERT INTO a VALUES (a + 1, 2 * 3)`},
{`UPSERT INTO a(a, b) VALUES (1, 2)`},
{`UPSERT INTO a(a, a.b) VALUES (1, 2)`},
{`UPSERT INTO a SELECT b, c FROM d`},
{`UPSERT INTO a DEFAULT VALUES`},
{`UPSERT INTO a DEFAULT VALUES RETURNING a, b`},
{`UPSERT INTO a DEFAULT VALUES RETURNING 1, 2`},
{`UPSERT INTO a DEFAULT VALUES RETURNING a + b`},
{`UPSERT INTO a DEFAULT VALUES RETURNING NOTHING`},
{`INSERT INTO a VALUES (1) ON CONFLICT DO NOTHING`},
{`INSERT INTO a VALUES (1) ON CONFLICT (a) DO NOTHING`},
{`INSERT INTO a VALUES (1) ON CONFLICT DO UPDATE SET a = 1`},
{`INSERT INTO a VALUES (1) ON CONFLICT (a) DO UPDATE SET a = 1`},
{`INSERT INTO a VALUES (1) ON CONFLICT (a, b) DO UPDATE SET a = 1`},
{`INSERT INTO a VALUES (1) ON CONFLICT (a) DO UPDATE SET a = 1, b = excluded.a`},
{`INSERT INTO a VALUES (1) ON CONFLICT (a) DO UPDATE SET a = 1 WHERE b > 2`},
{`INSERT INTO a VALUES (1) ON CONFLICT (a) DO UPDATE SET a = DEFAULT`},
{`INSERT INTO a VALUES (1) ON CONFLICT (a) DO UPDATE SET (a, b) = (SELECT 1, 2)`},
{`INSERT INTO a VALUES (1) ON CONFLICT (a) DO UPDATE SET (a, b) = (SELECT 1, 2) RETURNING a, b`},
{`INSERT INTO a VALUES (1) ON CONFLICT (a) DO UPDATE SET (a, b) = (SELECT 1, 2) RETURNING 1, 2`},
{`INSERT INTO a VALUES (1) ON CONFLICT (a) DO UPDATE SET (a, b) = (SELECT 1, 2) RETURNING a + b`},
{`INSERT INTO a VALUES (1) ON CONFLICT (a) DO UPDATE SET (a, b) = (SELECT 1, 2) RETURNING NOTHING`},
{`SELECT 1 + 1`},
{`SELECT -1`},
{`SELECT +1`},
{`SELECT .1`},
{`SELECT 1.2e1`},
{`SELECT 1.2e+1`},
{`SELECT 1.2e-1`},
{`SELECT true AND false`},
{`SELECT true AND NULL`},
{`SELECT true = false`},
{`SELECT (true = false)`},
{`SELECT (ARRAY['a', 'b'])[2]`},
{`SELECT (ARRAY(VALUES (1), (2)))[1]`},
{`SELECT (SELECT 1)`},
{`SELECT ((SELECT 1))`},
{`SELECT (SELECT ARRAY['a', 'b'])[2]`},
{`SELECT ((SELECT ARRAY['a', 'b']))[2]`},
{`SELECT ((((VALUES (1)))))`},
{`SELECT EXISTS (SELECT 1)`},
{`SELECT (VALUES (1))`},
{`SELECT (1, 2, 3)`},
{`SELECT (ROW(1, 2, 3))`},
{`SELECT (ROW())`},
{`SELECT (TABLE a)`},
{`SELECT 0x1`},
{`SELECT 'Deutsch' COLLATE "DE"`},
{`SELECT a @> b`},
{`SELECT a <@ b`},
{`SELECT a ? b`},
{`SELECT a ?| b`},
{`SELECT a ?& b`},
{`SELECT a->'x'`},
{`SELECT a#>'{x}'`},
{`SELECT a#>>'{x}'`},
{`SELECT (a->'x')->'y'`},
{`SELECT (a->'x')->>'y'`},
{`SELECT ''::JSON`},
{`SELECT ''::JSONB`},
{`SELECT 1 FROM t`},
{`SELECT 1, 2 FROM t`},
{`SELECT * FROM t`},
{`SELECT "*" FROM t`},
{`SELECT a, b FROM t`},
{`SELECT a AS b FROM t`},
{`SELECT a.* FROM t`},
{`SELECT a = b FROM t`},
{`SELECT $1 FROM t`},
{`SELECT $1, $2 FROM t`},
{`SELECT NULL FROM t`},
{`SELECT 0.1 FROM t`},
{`SELECT a FROM t`},
{`SELECT a.b FROM t`},
{`SELECT a.b.* FROM t`},
{`SELECT a.b[1] FROM t`},
{`SELECT a.b[1 + 1:4][3] FROM t`},
{`SELECT a.b[:4][3] FROM t`},
{`SELECT a.b[1 + 1:][3] FROM t`},
{`SELECT a.b[:][3] FROM t`},
{`SELECT 'a' FROM t`},
{`SELECT 'a' FROM t@bar`},
{`SELECT 'a' FROM t@primary`},
{`SELECT 'a' FROM t@like`},
{`SELECT 'a' FROM t@{NO_INDEX_JOIN}`},
{`SELECT 'a' FROM t@{FORCE_INDEX=bar,NO_INDEX_JOIN}`},
{`SELECT * FROM t AS "of" AS OF SYSTEM TIME '2016-01-01'`},
{`SELECT '1':::INT`},
{`SELECT '1'::INT`},
{`SELECT BOOL 'foo'`},
{`SELECT INT 'foo'`},
{`SELECT REAL 'foo'`},
{`SELECT DECIMAL 'foo'`},
{`SELECT DATE 'foo'`},
{`SELECT TIME 'foo'`},
{`SELECT TIMESTAMP 'foo'`},
{`SELECT TIMESTAMP WITH TIME ZONE 'foo'`},
{`SELECT CHAR 'foo'`},
{`SELECT '192.168.0.1':::INET`},
{`SELECT '192.168.0.1'::INET`},
{`SELECT 'a' AS "12345"`},
{`SELECT 'a' AS clnm`},
{`SELECT 'a' AS primary`},
{`SELECT 'a' AS like`},
{`SELECT 0xf0 FROM t`},
{`SELECT 0xF0 FROM t`},
// Escaping may change since the scanning process loses information
// (you can write e'\'' or ''''), but these are the idempotent cases.
// Generally, anything that needs to escape plus \ and ' leads to an
// escaped string.
{`SELECT e'a\'a' FROM t`},
{`SELECT e'a\\\\na' FROM t`},
{`SELECT e'\\\\n' FROM t`},
{`SELECT "a""a" FROM t`},
{`SELECT a FROM "t\n"`}, // no escaping in sql identifiers
{`SELECT a FROM "t"""`}, // no escaping in sql identifiers
{`SELECT "full" FROM t`}, // must quote column name keyword
{`SELECT "FROM" FROM t`},
{`SELECT CAST(1 AS TEXT)`},
{`SELECT ANNOTATE_TYPE(1, TEXT)`},
{`SELECT a FROM t AS bar`},
{`SELECT a FROM t AS bar (bar1)`},
{`SELECT a FROM t AS bar (bar1, bar2, bar3)`},
{`SELECT a FROM t WITH ORDINALITY`},
{`SELECT a FROM t WITH ORDINALITY AS bar`},
{`SELECT a FROM (SELECT 1 FROM t)`},
{`SELECT a FROM (SELECT 1 FROM t) AS bar`},
{`SELECT a FROM (SELECT 1 FROM t) AS bar (bar1)`},
{`SELECT a FROM (SELECT 1 FROM t) AS bar (bar1, bar2, bar3)`},
{`SELECT a FROM (SELECT 1 FROM t) WITH ORDINALITY`},
{`SELECT a FROM (SELECT 1 FROM t) WITH ORDINALITY AS bar`},
{`SELECT a FROM generate_series(1, 32)`},
{`SELECT a FROM generate_series(1, 32) AS s (x)`},
{`SELECT a FROM generate_series(1, 32) WITH ORDINALITY AS s (x)`},
{`SELECT a FROM t1, t2`},
{`SELECT a FROM t AS t1`},
{`SELECT a FROM t AS t1 (c1)`},
{`SELECT a FROM t AS t1 (c1, c2, c3, c4)`},
{`SELECT a FROM s.t`},
{`SELECT count(DISTINCT a) FROM t`},
{`SELECT count(ALL a) FROM t`},
{`SELECT a FROM t WHERE a = b`},
{`SELECT a FROM t WHERE NOT (a = b)`},
{`SELECT a FROM t WHERE EXISTS (SELECT 1 FROM t)`},
{`SELECT a FROM t WHERE NOT true`},
{`SELECT a FROM t WHERE NOT false`},
{`SELECT a FROM t WHERE a IN (b)`},
{`SELECT a FROM t WHERE a IN (b, c)`},
{`SELECT a FROM t WHERE a IN (SELECT a FROM t)`},
{`SELECT a FROM t WHERE a NOT IN (b, c)`},
{`SELECT a FROM t WHERE a = ANY (ARRAY[b, c])`},
{`SELECT a FROM t WHERE a = ANY ARRAY[b, c]`},
{`SELECT a FROM t WHERE a != SOME (ARRAY[b, c])`},
{`SELECT a FROM t WHERE a != SOME ARRAY[b, c]`},
{`SELECT a FROM t WHERE a = ANY (SELECT 1)`},
{`SELECT a FROM t WHERE a LIKE ALL (ARRAY[b, c])`},
{`SELECT a FROM t WHERE a LIKE ALL ARRAY[b, c]`},
{`SELECT a FROM t WHERE a LIKE b`},
{`SELECT a FROM t WHERE a NOT LIKE b`},
{`SELECT a FROM t WHERE a ILIKE b`},
{`SELECT a FROM t WHERE a NOT ILIKE b`},
{`SELECT a FROM t WHERE a SIMILAR TO b`},
{`SELECT a FROM t WHERE a NOT SIMILAR TO b`},
{`SELECT a FROM t WHERE a ~ b`},
{`SELECT a FROM t WHERE a !~ b`},
{`SELECT a FROM t WHERE a ~* c`},
{`SELECT a FROM t WHERE a !~* c`},
{`SELECT a FROM t WHERE a BETWEEN b AND c`},
{`SELECT a FROM t WHERE a NOT BETWEEN b AND c`},
{`SELECT a FROM t WHERE a IS NULL`},
{`SELECT a FROM t WHERE a IS NOT NULL`},
{`SELECT a FROM t WHERE a IS true`},
{`SELECT a FROM t WHERE a IS NOT true`},
{`SELECT a FROM t WHERE a IS false`},
{`SELECT a FROM t WHERE a IS NOT false`},
{`SELECT a FROM t WHERE a IS OF (INT)`},
{`SELECT a FROM t WHERE a IS NOT OF (FLOAT, STRING)`},
{`SELECT a FROM t WHERE a IS DISTINCT FROM b`},
{`SELECT a FROM t WHERE a IS NOT DISTINCT FROM b`},
{`SELECT a FROM t WHERE a < b`},
{`SELECT a FROM t WHERE a <= b`},
{`SELECT a FROM t WHERE a >= b`},
{`SELECT a FROM t WHERE a != b`},
{`SELECT a FROM t WHERE a = (SELECT a FROM t)`},
{`SELECT a FROM t WHERE a = (b)`},
{`SELECT a FROM t WHERE CASE WHEN a = b THEN c END`},
{`SELECT a FROM t WHERE CASE WHEN a = b THEN c ELSE d END`},
{`SELECT a FROM t WHERE CASE WHEN a = b THEN c WHEN b = d THEN d ELSE d END`},
{`SELECT a FROM t WHERE CASE aa WHEN a = b THEN c END`},
{`SELECT a FROM t WHERE a = b()`},
{`SELECT a FROM t WHERE a = b(c)`},
{`SELECT a FROM t WHERE a = b(c, d)`},
{`SELECT a FROM t WHERE a = count(*)`},
{`SELECT a FROM t WHERE a = IF(b, c, d)`},
{`SELECT a FROM t WHERE a = IFNULL(b, c)`},
{`SELECT a FROM t WHERE a = NULLIF(b, c)`},
{`SELECT a FROM t WHERE a = COALESCE(a, b, c, d, e)`},
{`SELECT (a.b) FROM t WHERE (b.c) = 2`},
{`SELECT a FROM t ORDER BY a`},
{`SELECT a FROM t ORDER BY a ASC`},
{`SELECT a FROM t ORDER BY a DESC`},
{`SELECT a FROM t ORDER BY PRIMARY KEY t`},
{`SELECT a FROM t ORDER BY PRIMARY KEY t ASC`},
{`SELECT a FROM t ORDER BY PRIMARY KEY t DESC`},
{`SELECT a FROM t ORDER BY INDEX t@foo`},
{`SELECT a FROM t ORDER BY INDEX t@foo ASC`},
{`SELECT a FROM t ORDER BY INDEX t@foo DESC`},
{`SELECT a FROM t ORDER BY INDEX t@primary`},
{`SELECT a FROM t ORDER BY INDEX t@like`},
{`SELECT 1 FROM t GROUP BY a`},
{`SELECT 1 FROM t GROUP BY a, b`},
{`SELECT a FROM t HAVING a = b`},
{`SELECT a FROM t WINDOW w AS ()`},
{`SELECT a FROM t WINDOW w AS (w2)`},
{`SELECT a FROM t WINDOW w AS (PARTITION BY b)`},
{`SELECT a FROM t WINDOW w AS (PARTITION BY b, 1 + 2)`},
{`SELECT a FROM t WINDOW w AS (ORDER BY c)`},
{`SELECT a FROM t WINDOW w AS (ORDER BY c, 1 + 2)`},
{`SELECT a FROM t WINDOW w AS (PARTITION BY b ORDER BY c)`},
{`SELECT avg(1) OVER w FROM t`},
{`SELECT avg(1) OVER () FROM t`},
{`SELECT avg(1) OVER (w) FROM t`},
{`SELECT avg(1) OVER (PARTITION BY b) FROM t`},
{`SELECT avg(1) OVER (ORDER BY c) FROM t`},
{`SELECT avg(1) OVER (PARTITION BY b ORDER BY c) FROM t`},
{`SELECT avg(1) OVER (w PARTITION BY b ORDER BY c) FROM t`},
{`SELECT a FROM t UNION SELECT 1 FROM t`},
{`SELECT a FROM t UNION SELECT 1 FROM t UNION SELECT 1 FROM t`},
{`SELECT a FROM t UNION ALL SELECT 1 FROM t`},
{`SELECT a FROM t EXCEPT SELECT 1 FROM t`},
{`SELECT a FROM t EXCEPT ALL SELECT 1 FROM t`},
{`SELECT a FROM t INTERSECT SELECT 1 FROM t`},
{`SELECT a FROM t INTERSECT ALL SELECT 1 FROM t`},
{`SELECT a FROM t1 JOIN t2 ON a = b`},
{`SELECT a FROM t1 JOIN t2 USING (a)`},
{`SELECT a FROM t1 LEFT JOIN t2 ON a = b`},
{`SELECT a FROM t1 RIGHT JOIN t2 ON a = b`},
{`SELECT a FROM t1 INNER JOIN t2 ON a = b`},
{`SELECT a FROM t1 CROSS JOIN t2`},
{`SELECT a FROM t1 NATURAL JOIN t2`},
{`SELECT a FROM t1 INNER JOIN t2 USING (a)`},
{`SELECT a FROM t1 FULL JOIN t2 USING (a)`},
{`SELECT * FROM (t1 WITH ORDINALITY AS o1 CROSS JOIN t2 WITH ORDINALITY AS o2) WITH ORDINALITY AS o3`},
{`SELECT a FROM t1 AS OF SYSTEM TIME '2016-01-01'`},
{`SELECT a FROM t1, t2 AS OF SYSTEM TIME '2016-01-01'`},
{`SELECT a FROM t LIMIT a`},
{`SELECT a FROM t OFFSET b`},
{`SELECT a FROM t LIMIT a OFFSET b`},
{`SELECT a FROM t FOR UPDATE`},
{`SELECT a FROM t LIMIT a OFFSET b FOR UPDATE`},
{`SELECT DISTINCT * FROM t`},
{`SELECT DISTINCT a, b FROM t`},
{`SET a = 3`},
{`SET a = 3, 4`},
{`SET a = '3'`},
{`SET a = 3.0`},
{`SET a = $1`},
{`SET a = off`},
{`SET TRANSACTION READ ONLY`},
{`SET TRANSACTION READ WRITE`},
{`SET TRANSACTION ISOLATION LEVEL SNAPSHOT`},
{`SET TRANSACTION ISOLATION LEVEL SERIALIZABLE`},
{`SET TRANSACTION PRIORITY LOW`},
{`SET TRANSACTION PRIORITY NORMAL`},
{`SET TRANSACTION PRIORITY HIGH`},
{`SET TRANSACTION ISOLATION LEVEL SNAPSHOT, PRIORITY HIGH`},
{`SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE`},
{`SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SNAPSHOT`},
{`SET CLUSTER SETTING a = 3`},
{`SET CLUSTER SETTING a = '3s'`},
{`SET CLUSTER SETTING a = '3'`},
{`SET CLUSTER SETTING a = 3.0`},
{`SET CLUSTER SETTING a = $1`},
{`SET CLUSTER SETTING a = off`},
{`SELECT * FROM (VALUES (1, 2)) AS foo`},
{`SELECT * FROM (VALUES (1, 2)) AS foo (a, b)`},
{`SELECT * FROM [123 AS t]`},
{`SELECT * FROM [123(1, 2, 3) AS t]`},
{`SELECT * FROM [123() AS t]`},
{`SELECT * FROM t@[123]`},
{`SELECT * FROM t@{FORCE_INDEX=[123],NO_INDEX_JOIN}`},
{`SELECT * FROM [123 AS t]@[456]`},
{`SELECT * FROM [123 AS t]@{FORCE_INDEX=[456],NO_INDEX_JOIN}`},
{`TABLE a`}, // Shorthand for: SELECT * FROM a; used e.g. in CREATE VIEW v AS TABLE t
{`TABLE [123 AS a]`},
{`TRUNCATE TABLE a`},
{`TRUNCATE TABLE a, b.c`},
{`TRUNCATE TABLE a CASCADE`},
{`UPDATE a SET b = 3`},
{`UPDATE a.b SET b = 3`},
{`UPDATE a SET b.c = 3`},
{`UPDATE a SET b = 3, c = DEFAULT`},
{`UPDATE a SET b = 3 + 4`},
{`UPDATE a SET (b, c) = (3, DEFAULT)`},
{`UPDATE a SET (b, c) = (SELECT 3, 4)`},
{`UPDATE a SET b = 3 WHERE a = b`},
{`UPDATE a SET b = 3 WHERE a = b RETURNING a`},
{`UPDATE a SET b = 3 WHERE a = b RETURNING 1, 2`},
{`UPDATE a SET b = 3 WHERE a = b RETURNING a, a + b`},
{`UPDATE a SET b = 3 WHERE a = b RETURNING NOTHING`},
{`UPDATE t AS "0" SET k = ''`}, // "0" lost its quotes
{`SELECT * FROM "0" JOIN "0" USING (id, "0")`}, // last "0" lost its quotes.
{`ALTER DATABASE a RENAME TO b`},
{`ALTER TABLE a RENAME TO b`},
{`ALTER TABLE IF EXISTS a RENAME TO b`},
{`ALTER INDEX a@b RENAME TO b`},
{`ALTER INDEX b RENAME TO b`},
{`ALTER INDEX a@primary RENAME TO like`},
{`ALTER INDEX IF EXISTS a@b RENAME TO b`},
{`ALTER INDEX IF EXISTS a@primary RENAME TO like`},
{`ALTER TABLE a RENAME COLUMN c1 TO c2`},
{`ALTER TABLE IF EXISTS a RENAME COLUMN c1 TO c2`},
{`ALTER TABLE a ADD b INT, ADD CONSTRAINT a_idx UNIQUE (a)`},
{`ALTER TABLE a ADD IF NOT EXISTS b INT, ADD CONSTRAINT a_idx UNIQUE (a)`},
{`ALTER TABLE IF EXISTS a ADD b INT, ADD CONSTRAINT a_idx UNIQUE (a)`},
{`ALTER TABLE IF EXISTS a ADD IF NOT EXISTS b INT, ADD CONSTRAINT a_idx UNIQUE (a)`},
{`ALTER TABLE a ADD COLUMN b INT, ADD CONSTRAINT a_idx UNIQUE (a)`},
{`ALTER TABLE a ADD COLUMN IF NOT EXISTS b INT, ADD CONSTRAINT a_idx UNIQUE (a) NOT VALID`},
{`ALTER TABLE IF EXISTS a ADD COLUMN b INT, ADD CONSTRAINT a_idx UNIQUE (a)`},
{`ALTER TABLE IF EXISTS a ADD COLUMN IF NOT EXISTS b INT, ADD CONSTRAINT a_idx UNIQUE (a)`},
{`ALTER TABLE a ADD b INT FAMILY fam_a`},
{`ALTER TABLE a ADD b INT CREATE FAMILY`},
{`ALTER TABLE a ADD b INT CREATE FAMILY fam_b`},
{`ALTER TABLE a ADD b INT CREATE IF NOT EXISTS FAMILY fam_b`},
{`ALTER TABLE a DROP b, DROP CONSTRAINT a_idx`},
{`ALTER TABLE a DROP IF EXISTS b, DROP CONSTRAINT a_idx`},
{`ALTER TABLE IF EXISTS a DROP b, DROP CONSTRAINT a_idx`},
{`ALTER TABLE IF EXISTS a DROP IF EXISTS b, DROP CONSTRAINT a_idx`},
{`ALTER TABLE a DROP COLUMN b, DROP CONSTRAINT a_idx`},
{`ALTER TABLE a DROP COLUMN IF EXISTS b, DROP CONSTRAINT a_idx`},
{`ALTER TABLE IF EXISTS a DROP COLUMN b, DROP CONSTRAINT a_idx`},
{`ALTER TABLE IF EXISTS a DROP COLUMN IF EXISTS b, DROP CONSTRAINT a_idx`},
{`ALTER TABLE a DROP COLUMN b CASCADE`},
{`ALTER TABLE a DROP COLUMN b RESTRICT`},
{`ALTER TABLE a DROP CONSTRAINT b CASCADE`},
{`ALTER TABLE a DROP CONSTRAINT IF EXISTS b RESTRICT`},
{`ALTER TABLE a VALIDATE CONSTRAINT a`},
{`ALTER TABLE a ALTER COLUMN b SET DEFAULT 42`},
{`ALTER TABLE a ALTER COLUMN b SET DEFAULT NULL`},
{`ALTER TABLE a ALTER COLUMN b DROP DEFAULT`},
{`ALTER TABLE a ALTER COLUMN b DROP NOT NULL`},
{`ALTER TABLE a ALTER b DROP NOT NULL`},
{`COPY t FROM STDIN`},
{`COPY t (a, b, c) FROM STDIN`},
{`ALTER TABLE a SPLIT AT VALUES (1)`},
{`ALTER TABLE a SPLIT AT SELECT * FROM t`},
{`ALTER TABLE d.a SPLIT AT VALUES ('b', 2)`},
{`ALTER INDEX a@i SPLIT AT VALUES (1)`},
{`ALTER INDEX d.a@i SPLIT AT VALUES (2)`},
{`ALTER INDEX i SPLIT AT VALUES (1)`},
{`ALTER INDEX d.i SPLIT AT VALUES (2)`},
{`ALTER TABLE a TESTING_RELOCATE VALUES (ARRAY[1], 1)`},
{`ALTER TABLE a TESTING_RELOCATE SELECT * FROM t`},
{`ALTER TABLE d.a TESTING_RELOCATE VALUES (ARRAY[1, 2, 3], 'b', 2)`},
{`ALTER INDEX d.i TESTING_RELOCATE VALUES (ARRAY[1], 2)`},
{`ALTER TABLE a SCATTER`},
{`ALTER TABLE a SCATTER FROM (1, 2, 3) TO (4, 5, 6)`},
{`ALTER TABLE d.a SCATTER`},
{`ALTER INDEX d.i SCATTER FROM (1) TO (2)`},
{`ALTER RANGE default EXPERIMENTAL CONFIGURE ZONE 'foo'`},
{`ALTER RANGE meta EXPERIMENTAL CONFIGURE ZONE 'foo'`},
{`ALTER DATABASE db EXPERIMENTAL CONFIGURE ZONE 'foo'`},
{`ALTER TABLE db.t EXPERIMENTAL CONFIGURE ZONE 'foo'`},
{`ALTER TABLE t EXPERIMENTAL CONFIGURE ZONE 'foo'`},
{`ALTER TABLE t EXPERIMENTAL CONFIGURE ZONE b'foo'`},
{`ALTER TABLE t EXPERIMENTAL CONFIGURE ZONE NULL`},
{`EXPERIMENTAL SCRUB TABLE x`},
{`EXPERIMENTAL SCRUB TABLE x WITH OPTIONS INDEX ALL`},
{`EXPERIMENTAL SCRUB TABLE x WITH OPTIONS INDEX (index_name)`},
{`EXPERIMENTAL SCRUB TABLE x WITH OPTIONS PHYSICAL`},
{`EXPERIMENTAL SCRUB TABLE x WITH OPTIONS PHYSICAL, INDEX (index_name)`},
{`EXPERIMENTAL SCRUB TABLE x WITH OPTIONS PHYSICAL, INDEX ALL`},
{`BACKUP foo TO 'bar'`},
{`BACKUP foo.foo, baz.baz TO 'bar'`},
{`SHOW BACKUP 'bar'`},
{`BACKUP foo TO 'bar' AS OF SYSTEM TIME '1' INCREMENTAL FROM 'baz'`},
{`BACKUP foo TO $1 INCREMENTAL FROM 'bar', $2, 'baz'`},
{`BACKUP DATABASE foo TO 'bar'`},
{`BACKUP DATABASE foo, baz TO 'bar'`},
{`BACKUP DATABASE foo TO 'bar' AS OF SYSTEM TIME '1' INCREMENTAL FROM 'baz'`},
{`RESTORE foo FROM 'bar'`},
{`RESTORE foo FROM $1`},
{`RESTORE foo FROM $1, $2, 'bar'`},
{`RESTORE foo, baz FROM 'bar'`},
{`RESTORE foo, baz FROM 'bar' AS OF SYSTEM TIME '1'`},
{`RESTORE DATABASE foo FROM 'bar'`},
{`RESTORE DATABASE foo, baz FROM 'bar'`},
{`RESTORE DATABASE foo, baz FROM 'bar' AS OF SYSTEM TIME '1'`},
{`BACKUP foo TO 'bar' WITH key1, key2 = 'value'`},
{`RESTORE foo FROM 'bar' WITH key1, key2 = 'value'`},
{`IMPORT TABLE foo CREATE USING 'nodelocal:///some/file' CSV DATA ('path/to/some/file', $1) WITH temp = 'path/to/temp'`},
{`IMPORT TABLE foo (id INT PRIMARY KEY, email STRING, age INT) CSV DATA ('path/to/some/file', $1) WITH temp = 'path/to/temp'`},
{`IMPORT TABLE foo (id INT, email STRING, age INT) CSV DATA ('path/to/some/file', $1) WITH comma = ',', "nullif" = 'n/a', temp = $2`},
{`SET ROW (1, true, NULL)`},
// Regression for #15926
{`SELECT * FROM ((t1 NATURAL JOIN t2 WITH ORDINALITY AS o1)) WITH ORDINALITY AS o2`},
}
for _, d := range testData {
stmts, err := Parse(d.sql)
if err != nil {
t.Fatalf("%s: expected success, but found %s", d.sql, err)
}
s := stmts.String()
if d.sql != s {
t.Errorf("expected \n%s\n, but found \n%s", d.sql, s)
}
}
}
// TestParse2 verifies that we can parse the supplied SQL and regenerate the
// expected SQL string from the syntax tree. Note that if the input and output
// SQL strings are the same, the test case should go in TestParse instead.
func TestParse2(t *testing.T) {
testData := []struct {
sql string
expected string
}{
{`CREATE DATABASE a WITH ENCODING = 'foo'`,
`CREATE DATABASE a ENCODING = 'foo'`},
{`CREATE DATABASE a TEMPLATE = template0`,
`CREATE DATABASE a TEMPLATE = 'template0'`},
{`CREATE DATABASE a TEMPLATE = invalid`,
`CREATE DATABASE a TEMPLATE = 'invalid'`},
{`CREATE TABLE a (b INT, UNIQUE INDEX foo (b))`,
`CREATE TABLE a (b INT, CONSTRAINT foo UNIQUE (b))`},
{`CREATE TABLE a (b INT, UNIQUE INDEX foo (b) INTERLEAVE IN PARENT c (d))`,
`CREATE TABLE a (b INT, CONSTRAINT foo UNIQUE (b) INTERLEAVE IN PARENT c (d))`},
{`CREATE INDEX ON a (b) COVERING (c)`, `CREATE INDEX ON a (b) STORING (c)`},
{`SELECT TIMESTAMP WITHOUT TIME ZONE 'foo'`, `SELECT TIMESTAMP 'foo'`},
{`SELECT CAST('foo' AS TIMESTAMP WITHOUT TIME ZONE)`, `SELECT CAST('foo' AS TIMESTAMP)`},
{`SELECT CAST(1 AS "char")`, `SELECT CAST(1 AS CHAR)`},
{`SELECT 'a' FROM t@{FORCE_INDEX=bar}`, `SELECT 'a' FROM t@bar`},
{`SELECT 'a' FROM t@{NO_INDEX_JOIN,FORCE_INDEX=bar}`,
`SELECT 'a' FROM t@{FORCE_INDEX=bar,NO_INDEX_JOIN}`},
{`SELECT 'a' FROM t@{FORCE_INDEX=[123]}`, `SELECT 'a' FROM t@[123]`},
{`SELECT 'a' FROM [123 AS t]@{FORCE_INDEX=[456]}`, `SELECT 'a' FROM [123 AS t]@[456]`},
{`SELECT a FROM t WHERE a IS UNKNOWN`, `SELECT a FROM t WHERE a IS NULL`},
{`SELECT a FROM t WHERE a IS NOT UNKNOWN`, `SELECT a FROM t WHERE a IS NOT NULL`},
{`SELECT - - 5`, `SELECT -(-5)`},
{`SELECT a FROM t WHERE b = - 2`, `SELECT a FROM t WHERE b = (-2)`},
{`SELECT a FROM t WHERE a = b AND a = c`, `SELECT a FROM t WHERE (a = b) AND (a = c)`},
{`SELECT a FROM t WHERE a = b OR a = c`, `SELECT a FROM t WHERE (a = b) OR (a = c)`},
{`SELECT a FROM t WHERE NOT a = b`, `SELECT a FROM t WHERE NOT (a = b)`},
{`SELECT a FROM t WHERE a = b & c`, `SELECT a FROM t WHERE a = (b & c)`},
{`SELECT a FROM t WHERE a = b | c`, `SELECT a FROM t WHERE a = (b | c)`},
{`SELECT a FROM t WHERE a = b # c`, `SELECT a FROM t WHERE a = (b # c)`},
{`SELECT a FROM t WHERE a = b ^ c`, `SELECT a FROM t WHERE a = (b ^ c)`},
{`SELECT a FROM t WHERE a = b + c`, `SELECT a FROM t WHERE a = (b + c)`},
{`SELECT a FROM t WHERE a = b - c`, `SELECT a FROM t WHERE a = (b - c)`},
{`SELECT a FROM t WHERE a = b * c`, `SELECT a FROM t WHERE a = (b * c)`},
{`SELECT a FROM t WHERE a = b / c`, `SELECT a FROM t WHERE a = (b / c)`},
{`SELECT a FROM t WHERE a = b % c`, `SELECT a FROM t WHERE a = (b % c)`},
{`SELECT a FROM t WHERE a = b || c`, `SELECT a FROM t WHERE a = (b || c)`},
{`SELECT a FROM t WHERE a = + b`, `SELECT a FROM t WHERE a = (+b)`},
{`SELECT a FROM t WHERE a = - b`, `SELECT a FROM t WHERE a = (-b)`},
{`SELECT a FROM t WHERE a = ~ b`, `SELECT a FROM t WHERE a = (~b)`},
// Escaped string literals are not always escaped the same because
// '''' and e'\'' scan to the same token. It's more convenient to
// prefer escaping ' and \, so we do that.
{`SELECT 'a''a'`,
`SELECT e'a\'a'`},
{`SELECT 'a\a'`,
`SELECT e'a\\a'`},
{`SELECT 'a\n'`,
`SELECT e'a\\n'`},
{"SELECT '\n'",
`SELECT e'\n'`},
{"SELECT '\n\\'",
`SELECT e'\n\\'`},
{`SELECT "a'a" FROM t`,
`SELECT "a'a" FROM t`},
// Hexadecimal literal strings are turned into regular strings.
{`SELECT x'61'`, `SELECT b'a'`},
{`SELECT X'61'`, `SELECT b'a'`},
// Comments are stripped.
{`SELECT 1 FROM t -- hello world`,
`SELECT 1 FROM t`},
{`SELECT /* hello world */ 1 FROM t`,
`SELECT 1 FROM t`},
{`SELECT /* hello */ 1 FROM /* world */ t`,
`SELECT 1 FROM t`},
// Alias expressions are always output using AS.
{`SELECT 1 FROM t t1`,
`SELECT 1 FROM t AS t1`},
{`SELECT 1 FROM t t1 (c1, c2)`,
`SELECT 1 FROM t AS t1 (c1, c2)`},
// Alternate not-equal operator.
{`SELECT a FROM t WHERE a <> b`,
`SELECT a FROM t WHERE a != b`},
// OUTER is syntactic sugar.
{`SELECT a FROM t1 LEFT OUTER JOIN t2 ON a = b`,
`SELECT a FROM t1 LEFT JOIN t2 ON a = b`},
{`SELECT a FROM t1 RIGHT OUTER JOIN t2 ON a = b`,
`SELECT a FROM t1 RIGHT JOIN t2 ON a = b`},
// Some functions are nearly keywords.
{`SELECT CURRENT_SCHEMA`,
`SELECT current_schema()`},
{`SELECT CURRENT_TIMESTAMP`,
`SELECT current_timestamp()`},
{`SELECT CURRENT_DATE`,
`SELECT current_date()`},
{`SELECT POSITION(a IN b)`,
`SELECT strpos(b, a)`},
{`SELECT TRIM(BOTH a FROM b)`,
`SELECT btrim(b, a)`},
{`SELECT TRIM(LEADING a FROM b)`,
`SELECT ltrim(b, a)`},
{`SELECT TRIM(TRAILING a FROM b)`,
`SELECT rtrim(b, a)`},
{`SELECT TRIM(a, b)`,
`SELECT btrim(a, b)`},
{`SELECT CURRENT_USER`,
`SELECT current_user()`},
{`SELECT SESSION_USER`,
`SELECT current_user()`},
{`SELECT USER`,
`SELECT current_user()`},
// Offset has an optional ROW/ROWS keyword.
{`SELECT a FROM t1 OFFSET a ROW`,
`SELECT a FROM t1 OFFSET a`},
{`SELECT a FROM t1 OFFSET a ROWS`,
`SELECT a FROM t1 OFFSET a`},
// We allow OFFSET before LIMIT, but always output LIMIT first.
{`SELECT a FROM t OFFSET a LIMIT b`,
`SELECT a FROM t LIMIT b OFFSET a`},
// We allow FOR UPDATE before LIMIT, but always output LIMIT first.
{`SELECT a FROM t FOR UPDATE LIMIT b`,
`SELECT a FROM t LIMIT b FOR UPDATE`},
// FETCH FIRST ... is alternative syntax for LIMIT.
{`SELECT a FROM t FETCH FIRST 3 ROWS ONLY`,
`SELECT a FROM t LIMIT 3`},
{`SELECT a FROM t FETCH NEXT 3 ROWS ONLY`,
`SELECT a FROM t LIMIT 3`},
{`SELECT a FROM t FETCH FIRST ROW ONLY`,
`SELECT a FROM t LIMIT 1`},
{`SELECT a FROM t FETCH FIRST (2 * a) ROWS ONLY`,
`SELECT a FROM t LIMIT 2 * a`},
{`SELECT a FROM t OFFSET b FETCH FIRST (2 * a) ROWS ONLY`,
`SELECT a FROM t LIMIT 2 * a OFFSET b`},
{`SELECT a FROM t FETCH FIRST (2 * a) ROWS ONLY OFFSET b`,
`SELECT a FROM t LIMIT 2 * a OFFSET b`},
// Double negation. See #1800.
{`SELECT *,-/* comment */-5`,
`SELECT *, -(-5)`},
{"SELECT -\n-5",
`SELECT -(-5)`},
{`SELECT -0.-/*test*/-1`,
`SELECT (-0.) - (-1)`,