forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres.spec.js
More file actions
2678 lines (2656 loc) · 93.1 KB
/
postgres.spec.js
File metadata and controls
2678 lines (2656 loc) · 93.1 KB
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
const { expect } = require('chai')
const { conflictToSQL } = require('../src/insert')
const { procToSQL } = require('../src/proc')
const Parser = require('../src/parser').default
describe('Postgres', () => {
const parser = new Parser();
const opt = {
database: 'postgresql'
}
function getParsedSql(sql, opt) {
const ast = parser.astify(sql, opt);
return parser.sqlify(ast, opt);
}
const SQL_LIST = [
{
title: 'select with_query_name',
sql: [
`WITH
subQ1 AS (SELECT * FROM Roster WHERE SchoolID = 52),
subQ2 AS (SELECT SchoolID FROM subQ1)
SELECT DISTINCT * FROM subQ2;`,
`WITH "subQ1" AS (SELECT * FROM "Roster" WHERE SchoolID = 52), "subQ2" AS (SELECT SchoolID FROM "subQ1") SELECT DISTINCT * FROM "subQ2"`
]
},
{
title: 'select subquery',
sql: [
`SELECT AVG ( PointsScored )
FROM
( SELECT PointsScored
FROM Stats
WHERE SchoolID = 77 )`,
'SELECT AVG(PointsScored) FROM (SELECT PointsScored FROM "Stats" WHERE SchoolID = 77)'
]
},
{
title: 'select subquery have alias',
sql: [
`SELECT r.LastName
FROM
( SELECT * FROM Roster) AS r`,
'SELECT "r".LastName FROM (SELECT * FROM "Roster") AS "r"'
]
},
{
title: 'select implicit "comma cross join"',
sql: [
'SELECT * FROM Roster, TeamMascot',
'SELECT * FROM "Roster", "TeamMascot"'
]
},
{
title: 'select inner join using',
sql: [
`SELECT FirstName
FROM Roster INNER JOIN PlayerStats
USING (LastName);`,
'SELECT FirstName FROM "Roster" INNER JOIN "PlayerStats" USING (LastName)'
]
},
{
title: 'set op UNION',
sql: [
`(SELECT s FROM t1) UNION (SELECT s FROM t2)`,
'(SELECT s FROM "t1") UNION (SELECT s FROM "t2")',
],
},
{
title: 'set op UNION ALL',
sql: [
`(SELECT s FROM t1) UNION ALL (SELECT s FROM t2)`,
'(SELECT s FROM "t1") UNION ALL (SELECT s FROM "t2")',
],
},
{
title: 'set op UNION DISTINCT',
sql: [
`(SELECT s FROM t1) UNION DISTINCT (SELECT s FROM t2)`,
'(SELECT s FROM "t1") UNION DISTINCT (SELECT s FROM "t2")',
],
},
{
title: 'Window Fns with qualified frame clause',
sql: [
`SELECT
first_name,
SUM(user_age) OVER (PARTITION BY user_city ORDER BY created_at DESC) AS age_window
FROM roster`,
'SELECT first_name, SUM(user_age) OVER (PARTITION BY user_city ORDER BY created_at DESC) AS "age_window" FROM "roster"'
]
},
{
title: 'Window Fns',
sql: [
`SELECT
first_name,
SUM(user_age) OVER (PARTITION BY user_city ORDER BY created_at) AS age_window
FROM roster`,
'SELECT first_name, SUM(user_age) OVER (PARTITION BY user_city ORDER BY created_at ASC) AS "age_window" FROM "roster"'
]
},
{
title: 'Window Fns + ROWS following',
sql: [
`SELECT
first_name,
SUM(user_age) OVER (
PARTITION BY user_city
ORDER BY created_at ASC
ROWS 1 FOLLOWING
) AS age_window
FROM roster`,
'SELECT first_name, SUM(user_age) OVER (PARTITION BY user_city ORDER BY created_at ASC ROWS 1 FOLLOWING) AS "age_window" FROM "roster"'
]
},
{
title: 'Window Fns + ROWS unbounded following',
sql: [
`SELECT
first_name,
SUM(user_age) OVER (
PARTITION BY user_city
ROWS UNbounded FOLLOWING
) AS age_window
FROM roster`,
'SELECT first_name, SUM(user_age) OVER (PARTITION BY user_city ROWS UNBOUNDED FOLLOWING) AS "age_window" FROM "roster"'
]
},
{
title: 'Window Fns + ROWS unbounded preceding',
sql: [
`SELECT
first_name,
SUM(user_age) OVER (
PARTITION BY user_city
ORDER BY created_at ASC
ROWS UNbounded preceding
) AS age_window
FROM roster`,
'SELECT first_name, SUM(user_age) OVER (PARTITION BY user_city ORDER BY created_at ASC ROWS UNBOUNDED PRECEDING) AS "age_window" FROM "roster"'
]
},
{
title: 'Window Fns + ROWS between',
sql: [
`SELECT
"first_name",
SUM(user_age) OVER (
PARTITION BY user_city
ORDER BY created_at DESC
ROWS BETWEEN 1 preceding AND 5 FOLLOWING
) AS age_window,
SUM(user_age) OVER (
PARTITION BY user_city
ORDER BY created_at DESC
ROWS BETWEEN unbounded preceding AND unbounded following
) AS age_window2
FROM roster`,
'SELECT "first_name", SUM(user_age) OVER (PARTITION BY user_city ORDER BY created_at DESC ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING) AS "age_window", SUM(user_age) OVER (PARTITION BY user_city ORDER BY created_at DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "age_window2" FROM "roster"'
]
},
{
title: 'Windows Fns + Rows between following',
sql: [
`SELECT
SUM(column3) OVER (
PARTITION BY column1
ORDER BY column2 ASC
ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING
)
FROM table1;`,
'SELECT SUM(column3) OVER (PARTITION BY column1 ORDER BY column2 ASC ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING) FROM "table1"'
]
},
{
title: 'Window Fns + ROWS unbounded preceding + current row',
sql: [
`SELECT
first_name,
SUM(user_age) OVER (
PARTITION BY user_city
ORDER BY created_at, user_id ASC
ROWS BETWEEN UNbounded preceding AND CURRENT ROW
) AS age_window
FROM roster`,
'SELECT first_name, SUM(user_age) OVER (PARTITION BY user_city ORDER BY created_at ASC, user_id ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS "age_window" FROM "roster"'
]
},
{
title: 'Window Fns + RANKING',
sql: [
`SELECT
ROW_NUMBER() OVER (
PARTITION BY user_city
ORDER BY created_at
) AS age_window
FROM roster`,
'SELECT ROW_NUMBER() OVER (PARTITION BY user_city ORDER BY created_at ASC) AS "age_window" FROM "roster"'
]
},
{
title: 'Window Fns + DENSE_RANK w/ empty OVER',
sql: [
`SELECT
DENSE_RANK() OVER () AS age_window
FROM roster`,
'SELECT DENSE_RANK() OVER () AS "age_window" FROM "roster"'
]
},
{
title: 'Window Fns + LAG',
sql: [
`SELECT
LAG(user_name, 10) OVER (
PARTITION BY user_city
ORDER BY created_at
) AS age_window
FROM roster`,
'SELECT LAG(user_name, 10) OVER (PARTITION BY user_city ORDER BY created_at ASC) AS "age_window" FROM "roster"'
]
},
{
title: 'Window Fns + LEAD',
sql: [
`SELECT
LEAD("user_name", 10) OVER (
PARTITION BY user_city
ORDER BY "created_at"
) AS age_window
FROM roster`,
'SELECT LEAD("user_name", 10) OVER (PARTITION BY user_city ORDER BY "created_at" ASC) AS "age_window" FROM "roster"'
]
},
{
title: 'Window Fns + NTH_VALUE',
sql: [
`SELECT
NTH_VALUE(user_name, 10) OVER (
PARTITION BY user_city
ORDER BY created_at
) AS age_window
FROM roster`,
'SELECT NTH_VALUE(user_name, 10) OVER (PARTITION BY user_city ORDER BY created_at ASC) AS "age_window" FROM "roster"'
]
},
{
title: 'Window Fns + LAG + explicit NULLS',
sql: [
`SELECT
LAG(user_name) ignore NULLS OVER (
PARTITION BY user_city
ORDER BY created_at
) AS age_window
FROM roster`,
'SELECT LAG(user_name) IGNORE NULLS OVER (PARTITION BY user_city ORDER BY created_at ASC) AS "age_window" FROM "roster"'
]
},
{
title: 'Window Fns + FIRST_VALUE',
sql: [
`SELECT
FIRST_VALUE(user_name) ignore NULLS OVER (
PARTITION BY user_city
ORDER BY created_at, ranking
) AS age_window
FROM roster`,
'SELECT FIRST_VALUE(user_name) IGNORE NULLS OVER (PARTITION BY user_city ORDER BY created_at ASC, ranking ASC) AS "age_window" FROM "roster"'
]
},
{
title: 'array column',
sql: [
"SELECT ARRAY[col1, col2, 1, 'str_literal'] from tableb",
`SELECT ARRAY[col1,col2,1,'str_literal'] FROM "tableb"`
]
},
{
title: 'array column index',
sql: [
"select (array['a', 'b', 'c'])[2]",
`SELECT (ARRAY['a','b','c'])[2]`
]
},
{
title: 'array cast column index',
sql: [
"select ('{a, b, c}'::text[])[2]",
`SELECT ('{a, b, c}'::TEXT[])[2]`
]
},
{
title: 'column array index',
sql: [
`with t as (
select array['a', 'b', 'c'] as a
)
select a[2]
from t`,
`WITH "t" AS (SELECT ARRAY['a','b','c'] AS "a") SELECT a[2] FROM "t"`
]
},
{
title: 'array slice with both bounds',
sql: [
'SELECT schedule[1:2] FROM sal_emp',
'SELECT schedule[1:2] FROM "sal_emp"'
]
},
{
title: 'array slice with start only',
sql: [
'SELECT schedule[2:] FROM sal_emp',
'SELECT schedule[2:] FROM "sal_emp"'
]
},
{
title: 'array slice with end only',
sql: [
'SELECT schedule[:3] FROM sal_emp',
'SELECT schedule[:3] FROM "sal_emp"'
]
},
{
title: 'array slice open bounds',
sql: [
'SELECT schedule[:] FROM sal_emp',
'SELECT schedule[:] FROM "sal_emp"'
]
},
{
title: 'multidimensional array slice',
sql: [
'SELECT schedule[1:2][1:1] FROM sal_emp',
'SELECT schedule[1:2][1:1] FROM "sal_emp"'
]
},
{
title: 'array slice mixed with single index',
sql: [
'SELECT schedule[1:2][2] FROM sal_emp',
'SELECT schedule[1:2][2] FROM "sal_emp"'
]
},
{
title: 'array slice in where clause',
sql: [
"SELECT * FROM sal_emp WHERE pay_by_quarter[1:2] = '{10000,10000}'",
`SELECT * FROM "sal_emp" WHERE pay_by_quarter[1:2] = '{10000,10000}'`
]
},
{
title: 'array slice with function call bounds',
sql: [
'SELECT arr[array_lower(arr, 1):array_upper(arr, 1)] FROM t',
'SELECT arr[array_lower(arr, 1):array_upper(arr, 1)] FROM "t"'
]
},
{
title: 'array slice with arithmetic expression bounds',
sql: [
'SELECT arr[(1+1):5] FROM t',
'SELECT arr[(1 + 1):5] FROM "t"'
]
},
{
title: 'array slice with column ref bounds',
sql: [
'SELECT arr[start_idx:end_idx] FROM t',
'SELECT arr[start_idx:end_idx] FROM "t"'
]
},
{
title: 'row function column',
sql: [
"SELECT ROW(col1, col2, 'literal', 1) from tableb",
`SELECT ROW(col1, col2, 'literal', 1) FROM "tableb"`
]
},
{
title: 'json column',
sql: [
`SELECT
d.metadata->>'communication_status' as communication_status
FROM
device d
WHERE d.metadata->>'communication_status' IS NOT NULL
LIMIT 10;`,
`SELECT "d".metadata ->> 'communication_status' AS "communication_status" FROM "device" AS "d" WHERE "d".metadata ->> 'communication_status' IS NOT NULL LIMIT 10`
]
},
{
title: 'case when in pg',
sql: [
`SELECT SUM(CASE WHEN status = 'ACTIVE' THEN 1 ELSE 0 END) FROM tablename`,
`SELECT SUM(CASE WHEN status = 'ACTIVE' THEN 1 ELSE 0 END) FROM "tablename"`
]
},
{
title: 'case when multiple condition in pg',
sql: [
`select case
when
ee.start_time <= current_timestamp
and ee.end_time > current_timestamp
then
true
else
false
end
is_live,
is_upcoming from abc`,
`SELECT CASE WHEN "ee".start_time <= CURRENT_TIMESTAMP AND "ee".end_time > CURRENT_TIMESTAMP THEN TRUE ELSE FALSE END AS "is_live", is_upcoming FROM "abc"`
]
},
{
title: 'key keyword in pg',
sql: [
`SELECT * FROM partitions WHERE location IS NULL AND code like 'XX-%' AND key <> 1;`,
`SELECT * FROM "partitions" WHERE location IS NULL AND code LIKE 'XX-%' AND key <> 1`
]
},
{
title: 'a multi-line single-quoted string',
sql: [
`SELECT 'Hello '
'world!' AS x;`,
`SELECT 'Hello world!' AS "x"`
]
},
{
title: 'left join',
sql: [
`select
person.first_name,
department.dept_name
from
person
left join department on person.dept_id = department.dept_id`,
'SELECT "person".first_name, "department".dept_name FROM "person" LEFT JOIN "department" ON "person".dept_id = "department".dept_id'
]
},
{
title: 'create table with serial',
sql: [
`create table posts(id serial primary key, name varchar(128))`,
`CREATE TABLE "posts" (id SERIAL PRIMARY KEY, name VARCHAR(128))`
]
},
{
title: 'cast to interval',
sql: [
`select '1 week'::interval`,
`SELECT '1 week'::INTERVAL`
]
},
{
title: 'with clause support double quote',
sql: [
`with "cte name" as (
select 1
)
select * from "cte name"`,
`WITH "cte name" AS (SELECT 1) SELECT * FROM "cte name"`
]
},
{
title: 'select from values as',
sql: [
`select *
from (values (0, 0), (1, null), (null, 2), (3, 4)) as t(a,b)
where a is distinct from "b"`,
`SELECT * FROM (VALUES (0,0), (1,NULL), (NULL,2), (3,4)) AS "t(a, b)" WHERE a IS DISTINCT FROM "b"`
]
},
{
title: 'select from values as without parentheses',
sql: [
`select last(col) FROM VALUES(10),(5),(20) AS tab(col)`,
`SELECT last(col) FROM VALUES (10), (5), (20) AS "tab(col)"`
]
},
{
title: 'aggr_fun percentile_cont',
sql: [
`select percentile_cont(0.25) within group (order by a asc) as p25
from (values (0),(0),(1),(2),(3),(4)) as t(a)`,
`SELECT PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY a ASC) AS "p25" FROM (VALUES (0), (0), (1), (2), (3), (4)) AS "t(a)"`
]
},
{
title: 'aggr_fun percentile_cont with array args',
sql: [
`select percentile_cont(array[0.5, 1]) within group (order by a asc) as p25
from (values (0),(0),(1),(2),(3),(4)) as t(a)`,
`SELECT PERCENTILE_CONT(ARRAY[0.5,1]) WITHIN GROUP (ORDER BY a ASC) AS "p25" FROM (VALUES (0), (0), (1), (2), (3), (4)) AS "t(a)"`
]
},
{
title: 'aggr_fun mode',
sql: [
`select mode() within group (order by a asc) as p25
from (values (0),(0),(1),(2),(3),(4)) as t(a)`,
`SELECT MODE() WITHIN GROUP (ORDER BY a ASC) AS "p25" FROM (VALUES (0), (0), (1), (2), (3), (4)) AS "t(a)"`
]
},
{
title: 'similar to keyword in pg',
sql: [
`select name similar to 'John%' from (values ('John Doe'),('Jane Doe'),('Bob John')) as t(name)`,
`SELECT name SIMILAR TO 'John%' FROM (VALUES ('John Doe'), ('Jane Doe'), ('Bob John')) AS "t(name)"`
]
},
{
title: 'show tables',
sql: [
`show tables`,
`SHOW TABLES`
]
},
{
title: 'String Constants',
sql: [
`select ''''`,
`SELECT ''''`
]
},
{
title: 'String Constants',
sql: [
`SELECT '''To be, or not'', it starts.' AS x;`,
`SELECT '''To be, or not'', it starts.' AS "x"`
]
},
{
title: 'String Constants',
sql: [
`SELECT 'foo'
'bar';`,
`SELECT 'foobar'`
]
},
{
title: 'String Constants with C-Style Escapes',
sql: [
`SELECT E'\\''`,
`SELECT E'\\''`
]
},
{
title: 'schema prefix',
sql: [
`SELECT "public"."Property"."id",
"public"."Property"."title",
"public"."Property"."description",
"public"."Property"."views",
"public"."Property"."saves",
"public"."Property"."postcode",
"public"."Property"."createdAt"
FROM "public"."Property"
WHERE 1 = 1
ORDER BY "public"."Property"."createdAt"`,
`SELECT "public"."Property"."id", "public"."Property"."title", "public"."Property"."description", "public"."Property"."views", "public"."Property"."saves", "public"."Property"."postcode", "public"."Property"."createdAt" FROM "public"."Property" WHERE 1 = 1 ORDER BY "public"."Property"."createdAt" ASC`
]
},
{
title: 'cast to datatype array',
sql: [
"select '{1,2,3}'::int[]",
"SELECT '{1,2,3}'::INT[]"
]
},
{
title: 'cast to datatype two dimension array',
sql: [
"select '{{1,2},{2,3},{3,4}}'::int[][]",
"SELECT '{{1,2},{2,3},{3,4}}'::INT[][]"
]
},
{
title: 'a newline before cast symbol',
sql: [
`select round(0.598736
::numeric, 2)`,
"SELECT round(0.598736::NUMERIC, 2)"
]
},
{
title: 'access array index in func parameter',
sql: [
'select round(arr[1])',
'SELECT round(arr[1])'
]
},
{
title: 'access array index in where clause',
sql: [
'SELECT * FROM a INNER JOIN b ON c = d[1]',
'SELECT * FROM "a" INNER JOIN "b" ON c = d[1]'
]
},
{
title: 'distinct on',
sql: [
"SELECT DISTINCT ON (a->>'someJsonAttribute', b, c) a->>'someJsonAttribute', b, c FROM tbl",
`SELECT DISTINCT ON (a ->> 'someJsonAttribute', b, c) a ->> 'someJsonAttribute', b, c FROM "tbl"`
]
},
{
title: 'select current_date only',
sql: [
'select current_date',
'SELECT CURRENT_DATE'
]
},
{
title: 'window function',
sql: [
`SELECT sum(salary) OVER w, avg(salary) OVER w
FROM empsalary
WINDOW w AS (PARTITION BY depname ORDER BY salary DESC);`,
'SELECT SUM(salary) OVER w, AVG(salary) OVER w FROM "empsalary" WINDOW w AS (PARTITION BY depname ORDER BY salary DESC)'
]
},
{
title: '$ field id with parameters',
sql: [
'SELECT * FROM tablea WHERE comment_id = $<3>;',
'SELECT * FROM "tablea" WHERE comment_id = $<3>'
]
},
{
title: 'cast with binary expr',
sql: [
'select (3-2)::float / (2 * 123) + 111',
'SELECT (3 - 2)::FLOAT / (2 * 123) + 111'
]
},
{
title: 'cast with binary expr and cast',
sql: [
'select (2)::float/(3)::float',
'SELECT (2)::FLOAT / (3)::FLOAT'
]
},
{
title: 'on expr with and',
sql: [
`select *
from organization
JOIN payment ON organization.id = payment.organization_id and createdat = month`,
'SELECT * FROM "organization" INNER JOIN "payment" ON "organization".id = "payment".organization_id AND createdat = month'
]
},
{
title: 'support tablesample',
sql: [
'select * from product.organization tablesample bernoulli(1)',
'SELECT * FROM "product"."organization" TABLESAMPLE bernoulli(1)'
]
},
{
title: 'support on clause with function and expr',
sql: [
`select * from pg_database a
join pg_database b
on a.oid = b.oid AND upper(a.datctype) = upper(b.datctype)`,
'SELECT * FROM "pg_database" AS "a" INNER JOIN "pg_database" AS "b" ON "a".oid = "b".oid AND upper("a".datctype) = upper("b".datctype)'
]
},
{
title: 'support trim function',
sql: [
`SELECT TRIM('.' from '....test.....') AS TrimmedString;`,
`SELECT TRIM('.' FROM '....test.....') AS "TrimmedString"`
]
},
{
title: 'from values without as',
sql: [
`with statuses as (
select a
from (
values ('Closed'), ('Verified'), ('Done')
) s(a)
) select * from statuses`,
`WITH "statuses" AS (SELECT a FROM (VALUES ('Closed'), ('Verified'), ('Done')) AS "s(a)") SELECT * FROM "statuses"`
]
},
{
title: 'double dollar-quoted string',
sql: [
'SELECT $$foo bar$$;',
'SELECT $$foo bar$$'
]
},
{
title: 'single dollar-quoted string',
sql: [
"select $SomeTag$Dianne's horse$SomeTag$",
"SELECT $SomeTag$Dianne's horse$SomeTag$"
]
},
{
title: 'nested block comments',
sql: [
"select /* /* */ */ col from tbl",
'SELECT col FROM "tbl"'
]
},
{
title: 'select into',
sql: [
"select c1, c2 into t1 from t2",
'SELECT c1, c2 INTO "t1" FROM "t2"'
]
},
{
title: 'select;',
sql: [
"select;",
'SELECT'
]
},
{
title: 'with insert',
sql: [
`CREATE TABLE stuff(id SERIAL PRIMARY KEY, name VARCHAR);
WITH new_stuff AS (
INSERT INTO stuff (name) VALUES ('foo'), ('bar') RETURNING id
)
SELECT id
FROM new_stuff;`,
`CREATE TABLE "stuff" (id SERIAL PRIMARY KEY, name VARCHAR) ; WITH "new_stuff" AS (INSERT INTO "stuff" (name) VALUES ('foo'), ('bar') RETURNING id) SELECT id FROM "new_stuff"`
]
},
{
title: 'offset without limit',
sql: [
'select c1 from t1 offset 11',
'SELECT c1 FROM "t1" OFFSET 11'
]
},
{
title: 'support empty space after ::',
sql: [
'SELECT (COALESCE(wp.weight, 0))::double(10) as net_weight , wp.gross_weight:: double(10) FROM wp ;',
'SELECT (COALESCE("wp".weight, 0))::DOUBLE(10) AS "net_weight", "wp".gross_weight::DOUBLE(10) FROM "wp"'
]
},
{
title: 'support nested json traversal',
sql: [
"SELECT meta.data->'foo'->'bar' as value FROM meta;",
`SELECT "meta".data -> 'foo' -> 'bar' AS "value" FROM "meta"`
]
},
{
title: 'support nulls first or last after order by',
sql: [
'SELECT has_geometry FROM rooms WHERE rooms.index = 200 ORDER BY has_geometry DESC NULLS LAST;',
'SELECT has_geometry FROM "rooms" WHERE "rooms".index = 200 ORDER BY has_geometry DESC NULLS LAST'
]
},
{
title: 'support nulls after order by with default val',
sql: [
'SELECT has_geometry FROM rooms WHERE rooms.index = 200 ORDER BY has_geometry ASC NULLS;',
'SELECT has_geometry FROM "rooms" WHERE "rooms".index = 200 ORDER BY has_geometry ASC NULLS'
]
},
{
title: 'support lateral with subquery',
sql: [
'SELECT * FROM foo, LATERAL (SELECT * FROM bar WHERE bar.id = foo.bar_id) ss;',
'SELECT * FROM "foo", LATERAL (SELECT * FROM "bar" WHERE "bar".id = "foo".bar_id) AS "ss"'
]
},
{
title: 'support lateral with function',
sql: [
`SELECT p1.id, p2.id, v1, v2
FROM polygons p1, polygons p2,
LATERAL vertices(p1.poly) v1,
LATERAL vertices(p2.poly) v2
WHERE (v1 - v2) < 10 AND p1.id != p2.id;`,
'SELECT "p1".id, "p2".id, v1, v2 FROM "polygons" AS "p1", "polygons" AS "p2", LATERAL vertices("p1".poly) AS "v1", LATERAL vertices("p2".poly) AS "v2" WHERE (v1 - v2) < 10 AND "p1".id != "p2".id'
]
},
{
title: 'support lateral with join',
sql: [
`SELECT m.name
FROM manufacturers m LEFT JOIN LATERAL get_product_names(m.id) pname ON true
WHERE pname IS NULL;`,
'SELECT "m".name FROM "manufacturers" AS "m" LEFT JOIN LATERAL get_product_names("m".id) AS "pname" ON TRUE WHERE pname IS NULL'
]
},
{
title: 'support escape char patten matching',
sql: [
"select c1 from t1 where c2 like 'abc' escape '!'",
`SELECT c1 FROM "t1" WHERE c2 LIKE 'abc' ESCAPE '!'`
]
},
{
title: 'with or without timezone',
sql: [
'select cast(c as time with time zone)',
'SELECT CAST(c AS TIME WITH TIME ZONE)'
]
},
{
title: 'with or without timezone',
sql: [
'select cast(c as timestamp without time zone)',
'SELECT CAST(c AS TIMESTAMP WITHOUT TIME ZONE)'
]
},
{
title: 'bytea datatype',
sql: [
'SELECT \'abc \\153\\154\\155 \\052\\251\\124\'::bytea;',
"SELECT 'abc \\153\\154\\155 \\052\\251\\124'::BYTEA"
]
},
{
title: 'deallocate statement',
sql: [
'DEALLOCATE pdo_stmt_1',
'DEALLOCATE pdo_stmt_1'
]
},
{
title: 'deallocate statement with prepare',
sql: [
'DEALLOCATE PREPARE ALL',
'DEALLOCATE PREPARE ALL'
]
},
{
title: 'filter after aggregate expression',
sql: [
"SELECT date_trunc('month', buy_window) AS month_window, marketplace, SUM(currency_amount_a) FILTER (WHERE currency_symbol_a IN ('REN', 'EUR')) + SUM(currency_amount_b) FILTER (WHERE currency_symbol_b IN ('REN', 'EUR')) as volume FROM currency.forex WHERE buy_window >= to_timestamp(1522540800) GROUP BY project, month",
`SELECT date_trunc('month', buy_window) AS "month_window", marketplace, SUM(currency_amount_a) FILTER (WHERE currency_symbol_a IN ('REN', 'EUR')) + SUM(currency_amount_b) FILTER (WHERE currency_symbol_b IN ('REN', 'EUR')) AS "volume" FROM "currency"."forex" WHERE buy_window >= to_timestamp(1522540800) GROUP BY project, month`,
]
},
{
title: 'decimal without prefix 0',
sql: [
`SELECT date_trunc('month', time_window) , SUM(ren) * .999 as ren_normalized FROM currencies."forex" WHERE memory_address = '\x881d40237659c251811cec9c364ef91dc08d300c' GROUP BY 1`,
`SELECT date_trunc('month', time_window), SUM(ren) * 0.999 AS "ren_normalized" FROM "currencies"."forex" WHERE memory_address = '1d40237659c251811cec9c364ef91dc08d300c' GROUP BY 1`
]
},
{
title: 'values as table name',
sql: [
`with values as (
select 1 as value
)
select * from values`,
'WITH "values" AS (SELECT 1 AS "value") SELECT * FROM "values"',
]
},
{
title: 'bigserial datatype',
sql: [
'create table if not exists "users" ( "id" bigserial, "name" varchar(128) not null, "second_name" varchar(128) default null )',
'CREATE TABLE IF NOT EXISTS "users" ("id" BIGSERIAL, "name" VARCHAR(128) NOT NULL, "second_name" VARCHAR(128) DEFAULT NULL)',
]
},
{
title: 'delete statement',
sql: [
'DELETE FROM users WHERE id = 2;',
'DELETE FROM "users" WHERE id = 2',
]
},
{
title: 'delete statement with returning',
sql: [
'DELETE FROM users WHERE id = 2 RETURNING id, email as email_address;',
'DELETE FROM "users" WHERE id = 2 RETURNING id, email AS "email_address"',
]
},
{
title: 'delete statement with returning *',
sql: [
'DELETE FROM users WHERE id = 2 RETURNING *;',
'DELETE FROM "users" WHERE id = 2 RETURNING *',
]
},
{
title: 'column quoted data type',
sql: [
`select 'a'::"char" as b;`,
`SELECT 'a'::"CHAR" AS "b"`
]
},
{
title: 'set with quoted string',
sql: [
`set "foo.bar" = 'a';`,
`SET "foo.bar" = 'a'`
]
},
{
title: 'show stmt',
sql: [
'show "foo.bar";',
'SHOW "foo.bar"'
]
},
{
title: 'create now at time zone',
sql: [
`CREATE TABLE IF NOT EXISTS "users" ( "id" BIGSERIAL PRIMARY KEY, "date_created" TIMESTAMP WITHOUT TIME ZONE DEFAULT (now() at time zone 'utc'), "first_name" VARCHAR(128) NOT NULL );`,
`CREATE TABLE IF NOT EXISTS "users" ("id" BIGSERIAL PRIMARY KEY, "date_created" TIMESTAMP WITHOUT TIME ZONE DEFAULT (now() AT TIME ZONE 'utc'), "first_name" VARCHAR(128) NOT NULL)`
]
},
{
title: 'from clause in update',
sql: [
`UPDATE t1 SET c1 = 'x' FROM t2 WHERE c3 = t2.c2`,
`UPDATE "t1" SET c1 = 'x' FROM "t2" WHERE c3 = "t2".c2`,
]
},
{
title: 'from clause in update with select',
sql: [
`UPDATE t1 SET c1 = 'x' FROM (select c2 from t2) WHERE c3 = c2`,
`UPDATE "t1" SET c1 = 'x' FROM (SELECT c2 FROM "t2") WHERE c3 = c2`,
]
},
{
title: 'drop index',
sql: [
'drop index concurrently title_index cascade',
'DROP INDEX CONCURRENTLY title_index CASCADE'
],
},
{
title: 'with clause in update',
sql: [
`WITH olds AS (SELECT test_field_1, test_field_2 FROM test_tbl WHERE test_field_1=5)
UPDATE test_tbl SET test_field_2 ='tested!' WHERE test_field_1=5
RETURNING (SELECT test_field_1 FROM olds) AS test_field_1_old,
(SELECT test_field_2 FROM olds) AS test_field_2_old;`,
`WITH "olds" AS (SELECT test_field_1, test_field_2 FROM "test_tbl" WHERE test_field_1 = 5) UPDATE "test_tbl" SET test_field_2 = 'tested!' WHERE test_field_1 = 5 RETURNING (SELECT test_field_1 FROM "olds") AS "test_field_1_old", (SELECT test_field_2 FROM "olds") AS "test_field_2_old"`
]
},
{
title: 'string concatenator in where clause',
sql: [
"SELECT * from tests where name = 'test' || 'abc';",
`SELECT * FROM "tests" WHERE name = 'test' || 'abc'`
]
},
{
title: 'alter table add constraint',
sql: [
'ALTER TABLE if exists only address ADD CONSTRAINT user_id_address_fk FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE ON UPDATE RESTRICT;',
'ALTER TABLE IF EXISTS ONLY "address" ADD CONSTRAINT "user_id_address_fk" FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE ON UPDATE RESTRICT'
]
},
{
title: 'table constructor in join',
sql: [
`select last_name, salary, title
from employees e left join (
salaries s inner join titles t on s.emp_no = t.emp_no
) on e.emp_no = s.emp_no`,
'SELECT last_name, salary, title FROM "employees" AS "e" LEFT JOIN ("salaries" AS "s" INNER JOIN "titles" AS "t" ON "s".emp_no = "t".emp_no) ON "e".emp_no = "s".emp_no'
]
},
{
title: 'table constructor in from',
sql: [
`select last_name, salary
from (
employees inner join salaries on employees.emp_no = salaries.emp_no
);`,
'SELECT last_name, salary FROM ("employees" INNER JOIN "salaries" ON "employees".emp_no = "salaries".emp_no)'
]
},
{
title: 'select from scheme.table.column',
sql: [
'select public.t1.* from public.t1;',
'SELECT public.t1.* FROM "public"."t1"'
]
},
{
title: 'ntile function',
sql: [
`SELECT name,
amount,