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
|
# title: lateral
# execute: false
SELECT a, m FROM z LATERAL VIEW EXPLODE([1, 2]) q AS m;
SELECT
"z"."a" AS "a",
"q"."m" AS "m"
FROM "z" AS "z"
LATERAL VIEW
EXPLODE(ARRAY(1, 2)) q AS "m";
# title: unnest
# execute: false
SELECT x FROM UNNEST([1, 2]) AS q(x, y);
SELECT
"q"."x" AS "x"
FROM UNNEST(ARRAY(1, 2)) AS "q"("x", "y");
# title: Union in CTE
WITH cte AS (
(
SELECT
a
FROM
x
)
UNION ALL
(
SELECT
b AS a
FROM
y
)
)
SELECT
*
FROM
cte;
WITH "cte" AS (
(
SELECT
"x"."a" AS "a"
FROM "x" AS "x"
)
UNION ALL
(
SELECT
"y"."b" AS "a"
FROM "y" AS "y"
)
)
SELECT
"cte"."a" AS "a"
FROM "cte";
# title: Chained CTEs
WITH cte1 AS (
SELECT a
FROM x
), cte2 AS (
SELECT a + 1 AS a
FROM cte1
)
SELECT
a
FROM cte1
UNION ALL
SELECT
a
FROM cte2;
WITH "cte1" AS (
SELECT
"x"."a" AS "a"
FROM "x" AS "x"
)
SELECT
"cte1"."a" AS "a"
FROM "cte1"
UNION ALL
SELECT
"cte1"."a" + 1 AS "a"
FROM "cte1";
# title: Correlated subquery
SELECT a, SUM(b) AS sum_b
FROM (
SELECT x.a, y.b
FROM x, y
WHERE (SELECT max(b) FROM y WHERE x.b = y.b) >= 0 AND x.b = y.b
) d
WHERE (TRUE AND TRUE OR 'a' = 'b') AND a > 1
GROUP BY a;
WITH "_u_0" AS (
SELECT
MAX("y"."b") AS "_col_0",
"y"."b" AS "_u_1"
FROM "y" AS "y"
GROUP BY
"y"."b"
)
SELECT
"x"."a" AS "a",
SUM("y"."b") AS "sum_b"
FROM "x" AS "x"
LEFT JOIN "_u_0" AS "_u_0"
ON "x"."b" = "_u_0"."_u_1"
JOIN "y" AS "y"
ON "x"."b" = "y"."b"
WHERE
"_u_0"."_col_0" >= 0 AND "x"."a" > 1
GROUP BY
"x"."a";
# title: Root subquery
(SELECT a FROM x) LIMIT 1;
(
SELECT
"x"."a" AS "a"
FROM "x" AS "x"
)
LIMIT 1;
# title: Root subquery is union
(SELECT b FROM x UNION SELECT b FROM y ORDER BY b) LIMIT 1;
(
SELECT
"x"."b" AS "b"
FROM "x" AS "x"
UNION
SELECT
"y"."b" AS "b"
FROM "y" AS "y"
ORDER BY
"b"
)
LIMIT 1;
# title: broadcast
# dialect: spark
SELECT /*+ BROADCAST(y) */ x.b FROM x JOIN y ON x.b = y.b;
SELECT /*+ BROADCAST(`y`) */
`x`.`b` AS `b`
FROM `x` AS `x`
JOIN `y` AS `y`
ON `x`.`b` = `y`.`b`;
# title: aggregate
# execute: false
SELECT AGGREGATE(ARRAY(x.a, x.b), 0, (x, acc) -> x + acc + a) AS sum_agg FROM x;
SELECT
AGGREGATE(ARRAY("x"."a", "x"."b"), 0, ("x", "acc") -> "x" + "acc" + "x"."a") AS "sum_agg"
FROM "x" AS "x";
# title: values
SELECT cola, colb FROM (VALUES (1, 'test'), (2, 'test2')) AS tab(cola, colb);
SELECT
"tab"."cola" AS "cola",
"tab"."colb" AS "colb"
FROM (VALUES
(1, 'test'),
(2, 'test2')) AS "tab"("cola", "colb");
# title: spark values
# dialect: spark
SELECT cola, colb FROM (VALUES (1, 'test'), (2, 'test2')) AS tab(cola, colb);
SELECT
`tab`.`cola` AS `cola`,
`tab`.`colb` AS `colb`
FROM VALUES
(1, 'test'),
(2, 'test2') AS `tab`(`cola`, `colb`);
# title: complex CTE dependencies
WITH m AS (
SELECT a, b FROM (VALUES (1, 2)) AS a1(a, b)
), n AS (
SELECT a, b FROM m WHERE m.a = 1
), o AS (
SELECT a, b FROM m WHERE m.a = 2
) SELECT
n.a,
n.b,
o.b
FROM n
FULL OUTER JOIN o ON n.a = o.a
CROSS JOIN n AS n2
WHERE o.b > 0 AND n.a = n2.a;
WITH "m" AS (
SELECT
"a1"."a" AS "a",
"a1"."b" AS "b"
FROM (VALUES
(1, 2)) AS "a1"("a", "b")
), "n" AS (
SELECT
"m"."a" AS "a",
"m"."b" AS "b"
FROM "m"
WHERE
"m"."a" = 1
), "o" AS (
SELECT
"m"."a" AS "a",
"m"."b" AS "b"
FROM "m"
WHERE
"m"."a" = 2
)
SELECT
"n"."a" AS "a",
"n"."b" AS "b",
"o"."b" AS "b"
FROM "n"
JOIN "n" AS "n2"
ON "n"."a" = "n2"."a"
FULL JOIN "o"
ON "n"."a" = "o"."a"
WHERE
"o"."b" > 0;
# title: Broadcast hint
# dialect: spark
WITH m AS (
SELECT
x.a,
x.b
FROM x
), n AS (
SELECT
y.b,
y.c
FROM y
), joined as (
SELECT /*+ BROADCAST(n) */
m.a,
n.c
FROM m JOIN n ON m.b = n.b
)
SELECT
joined.a,
joined.c
FROM joined;
SELECT /*+ BROADCAST(`y`) */
`x`.`a` AS `a`,
`y`.`c` AS `c`
FROM `x` AS `x`
JOIN `y` AS `y`
ON `x`.`b` = `y`.`b`;
# title: Mix Table and Column Hints
# dialect: spark
WITH m AS (
SELECT
x.a,
x.b
FROM x
), n AS (
SELECT
y.b,
y.c
FROM y
), joined as (
SELECT /*+ BROADCAST(m), MERGE(m, n) */
m.a,
n.c
FROM m JOIN n ON m.b = n.b
)
SELECT
/*+ COALESCE(3) */
joined.a,
joined.c
FROM joined;
SELECT /*+ COALESCE(3),
BROADCAST(`x`),
MERGE(`x`, `y`) */
`x`.`a` AS `a`,
`y`.`c` AS `c`
FROM `x` AS `x`
JOIN `y` AS `y`
ON `x`.`b` = `y`.`b`;
WITH cte1 AS (
WITH cte2 AS (
SELECT a, b FROM x
)
SELECT a1
FROM (
WITH cte3 AS (SELECT 1)
SELECT a AS a1, b AS b1 FROM cte2
)
)
SELECT a1 FROM cte1;
SELECT
"x"."a" AS "a1"
FROM "x" AS "x";
# title: recursive cte
WITH RECURSIVE cte1 AS (
SELECT *
FROM (
SELECT 1 AS a, 2 AS b
) base
CROSS JOIN (SELECT 3 c) y
UNION ALL
SELECT *
FROM cte1
WHERE a < 1
)
SELECT *
FROM cte1;
WITH RECURSIVE "base" AS (
SELECT
1 AS "a",
2 AS "b"
), "y" AS (
SELECT
3 AS "c"
), "cte1" AS (
SELECT
"base"."a" AS "a",
"base"."b" AS "b",
"y"."c" AS "c"
FROM "base" AS "base"
CROSS JOIN "y" AS "y"
UNION ALL
SELECT
"cte1"."a" AS "a",
"cte1"."b" AS "b",
"cte1"."c" AS "c"
FROM "cte1"
WHERE
"cte1"."a" < 1
)
SELECT
"cte1"."a" AS "a",
"cte1"."b" AS "b",
"cte1"."c" AS "c"
FROM "cte1";
# title: right join should not push down to from
SELECT x.a, y.b
FROM x
RIGHT JOIN y
ON x.a = y.b
WHERE x.b = 1;
SELECT
"x"."a" AS "a",
"y"."b" AS "b"
FROM "x" AS "x"
RIGHT JOIN "y" AS "y"
ON "x"."a" = "y"."b"
WHERE
"x"."b" = 1;
# title: right join can push down to itself
SELECT x.a, y.b
FROM x
RIGHT JOIN y
ON x.a = y.b
WHERE y.b = 1;
WITH "y_2" AS (
SELECT
"y"."b" AS "b"
FROM "y" AS "y"
WHERE
"y"."b" = 1
)
SELECT
"x"."a" AS "a",
"y"."b" AS "b"
FROM "x" AS "x"
RIGHT JOIN "y_2" AS "y"
ON "x"."a" = "y"."b";
# title: lateral column alias reference
SELECT x.a + 1 AS c, c + 1 AS d FROM x;
SELECT
"x"."a" + 1 AS "c",
"x"."a" + 2 AS "d"
FROM "x" AS "x";
# title: column reference takes priority over lateral column alias reference
SELECT x.a + 1 AS b, b + 1 AS c FROM x;
SELECT
"x"."a" + 1 AS "b",
"x"."b" + 1 AS "c"
FROM "x" AS "x";
# title: unqualified struct element is selected in the outer query
# execute: false
WITH "cte" AS (
SELECT
FROM_JSON("value", 'STRUCT<f1: STRUCT<f2: STRUCT<f3: STRUCT<f4: STRING>>>>') AS "struct"
FROM "tbl"
) SELECT "struct"."f1"."f2"."f3"."f4" AS "f4" FROM "cte";
SELECT
FROM_JSON("tbl"."value", 'STRUCT<f1: STRUCT<f2: STRUCT<f3: STRUCT<f4: STRING>>>>')."f1"."f2"."f3"."f4" AS "f4"
FROM "tbl" AS "tbl";
# title: qualified struct element is selected in the outer query
# execute: false
WITH "cte" AS (
SELECT
FROM_JSON("value", 'STRUCT<f1: STRUCT<f2: INTEGER>, STRUCT<f3: STRING>>') AS "struct"
FROM "tbl"
) SELECT "cte"."struct"."f1"."f2" AS "f2", "cte"."struct"."f1"."f3" AS "f3" FROM "cte";
SELECT
FROM_JSON("tbl"."value", 'STRUCT<f1: STRUCT<f2: INTEGER>, STRUCT<f3: STRING>>')."f1"."f2" AS "f2",
FROM_JSON("tbl"."value", 'STRUCT<f1: STRUCT<f2: INTEGER>, STRUCT<f3: STRING>>')."f1"."f3" AS "f3"
FROM "tbl" AS "tbl";
# title: left join doesnt push down predicate to join in merge subqueries
# execute: false
SELECT
main_query.id,
main_query.score
FROM (
SELECT
alias_1.id,
score
FROM (
SELECT
company_table.score AS score,
id
FROM company_table
) AS alias_1
JOIN (
SELECT
id
FROM (
SELECT
company_table_2.id,
CASE WHEN unlocked.company_id IS NULL THEN 0 ELSE 1 END AS is_exported
FROM company_table AS company_table_2
LEFT JOIN unlocked AS unlocked
ON company_table_2.id = unlocked.company_id
)
WHERE
NOT id IS NULL AND is_exported = FALSE
) AS alias_2
ON (
alias_1.id = alias_2.id
)
) AS main_query;
SELECT
"company_table"."id" AS "id",
"company_table"."score" AS "score"
FROM "company_table" AS "company_table"
JOIN "company_table" AS "company_table_2"
ON "company_table"."id" = "company_table_2"."id"
LEFT JOIN "unlocked" AS "unlocked"
ON "company_table_2"."id" = "unlocked"."company_id"
WHERE
CASE WHEN "unlocked"."company_id" IS NULL THEN 0 ELSE 1 END = FALSE
AND NOT "company_table_2"."id" IS NULL;
# title: db.table alias clash
# execute: false
select * from db1.tbl, db2.tbl;
SELECT
*
FROM "db1"."tbl" AS "tbl"
CROSS JOIN "db2"."tbl" AS "tbl_2";
# execute: false
SELECT
*,
IFF(
IFF(
uploaded_at >= '2022-06-16',
'workday',
'bamboohr'
) = source_system,
1,
0
) AS sort_order
FROM
unioned
WHERE
(
source_system = 'workday'
AND '9999-01-01' >= '2022-06-16'
)
OR (
source_system = 'bamboohr'
AND '0001-01-01' < '2022-06-16'
) QUALIFY ROW_NUMBER() OVER (
PARTITION BY unique_filter_key
ORDER BY
sort_order DESC,
1
) = 1;
SELECT
*,
IFF(
IFF("unioned"."uploaded_at" >= '2022-06-16', 'workday', 'bamboohr') = "unioned"."source_system",
1,
0
) AS "sort_order"
FROM "unioned" AS "unioned"
WHERE
"unioned"."source_system" = 'bamboohr' OR "unioned"."source_system" = 'workday'
QUALIFY
ROW_NUMBER() OVER (PARTITION BY "unioned"."unique_filter_key" ORDER BY "unioned"."sort_order" DESC, 1) = 1;
# title: pivoted source with explicit selections
# execute: false
SELECT * FROM (SELECT a, b, c FROM sc.tb) PIVOT (SUM(c) FOR b IN ('x','y','z'));
SELECT
"_q_1"."a" AS "a",
"_q_1"."x" AS "x",
"_q_1"."y" AS "y",
"_q_1"."z" AS "z"
FROM (
SELECT
"tb"."a" AS "a",
"tb"."b" AS "b",
"tb"."c" AS "c"
FROM "sc"."tb" AS "tb"
) AS "_q_0" PIVOT(SUM("_q_0"."c") FOR "_q_0"."b" IN ('x', 'y', 'z')) AS "_q_1";
# title: pivoted source with implicit selections
# execute: false
SELECT * FROM (SELECT * FROM u) PIVOT (SUM(f) FOR h IN ('x', 'y'));
SELECT
"_q_1"."g" AS "g",
"_q_1"."x" AS "x",
"_q_1"."y" AS "y"
FROM (
SELECT
"u"."f" AS "f",
"u"."g" AS "g",
"u"."h" AS "h"
FROM "u" AS "u"
) AS "_q_0" PIVOT(SUM("_q_0"."f") FOR "_q_0"."h" IN ('x', 'y')) AS "_q_1";
# title: selecting explicit qualified columns from pivoted source with explicit selections
# execute: false
SELECT piv.x, piv.y FROM (SELECT f, h FROM u) PIVOT (SUM(f) FOR h IN ('x', 'y')) AS piv;
SELECT
"piv"."x" AS "x",
"piv"."y" AS "y"
FROM (
SELECT
"u"."f" AS "f",
"u"."h" AS "h"
FROM "u" AS "u"
) AS "_q_0" PIVOT(SUM("_q_0"."f") FOR "_q_0"."h" IN ('x', 'y')) AS "piv";
# title: selecting explicit unqualified columns from pivoted source with implicit selections
# execute: false
SELECT x, y FROM u PIVOT (SUM(f) FOR h IN ('x', 'y'));
SELECT
"_q_0"."x" AS "x",
"_q_0"."y" AS "y"
FROM "u" AS "u" PIVOT(SUM("u"."f") FOR "u"."h" IN ('x', 'y')) AS "_q_0";
# title: selecting all columns from a pivoted CTE source, using alias for the aggregation and generating bigquery
# execute: false
# dialect: bigquery
WITH u_cte(f, g, h) AS (SELECT * FROM u) SELECT * FROM u_cte PIVOT(SUM(f) AS sum FOR h IN ('x', 'y'));
WITH `u_cte` AS (
SELECT
`u`.`f` AS `f`,
`u`.`g` AS `g`,
`u`.`h` AS `h`
FROM `u` AS `u`
)
SELECT
`_q_0`.`g` AS `g`,
`_q_0`.`sum_x` AS `sum_x`,
`_q_0`.`sum_y` AS `sum_y`
FROM `u_cte` AS `u_cte` PIVOT(SUM(`u_cte`.`f`) AS `sum` FOR `u_cte`.`h` IN ('x', 'y')) AS `_q_0`;
# title: selecting all columns from a pivoted source and generating snowflake
# execute: false
# dialect: snowflake
SELECT * FROM u PIVOT (SUM(f) FOR h IN ('x', 'y'));
SELECT
"_Q_0"."G" AS "G",
"_Q_0"."'x'" AS "'x'",
"_Q_0"."'y'" AS "'y'"
FROM "U" AS "U" PIVOT(SUM("U"."F") FOR "U"."H" IN ('x', 'y')) AS "_Q_0"
;
# title: selecting all columns from a pivoted source and generating spark
# note: spark doesn't allow pivot aliases or qualified columns for the pivot's "field" (`h`)
# execute: false
# dialect: spark
SELECT * FROM u PIVOT (SUM(f) FOR h IN ('x', 'y'));
SELECT
`_q_0`.`g` AS `g`,
`_q_0`.`x` AS `x`,
`_q_0`.`y` AS `y`
FROM (
SELECT
*
FROM `u` AS `u` PIVOT(SUM(`u`.`f`) FOR `h` IN ('x', 'y'))
) AS `_q_0`;
# title: quoting is maintained
# dialect: snowflake
with cte1("id", foo) as (select 1, 2) select "id" from cte1;
WITH "CTE1" AS (
SELECT
1 AS "id"
)
SELECT
"CTE1"."id" AS "id"
FROM "CTE1";
# title: ensures proper quoting happens after all optimizations
# execute: false
SELECT "foO".x FROM (SELECT 1 AS x) AS "foO";
WITH "foO" AS (
SELECT
1 AS "x"
)
SELECT
"foO"."x" AS "x"
FROM "foO" AS "foO";
# title: lateral subquery
# execute: false
# dialect: postgres
SELECT u.user_id, l.log_date
FROM users u
CROSS JOIN LATERAL (
SELECT l.log_date
FROM logs l
WHERE l.user_id = u.user_id AND l.log_date <= 100
ORDER BY l.log_date DESC NULLS LAST
LIMIT 1
) l;
SELECT
"u"."user_id" AS "user_id",
"l"."log_date" AS "log_date"
FROM "users" AS "u"
CROSS JOIN LATERAL (
SELECT
"l"."log_date" AS "log_date"
FROM "logs" AS "l"
WHERE
"l"."log_date" <= 100 AND "l"."user_id" = "u"."user_id"
ORDER BY
"l"."log_date" DESC NULLS LAST
LIMIT 1
) AS "l";
# title: bigquery column identifiers are case-insensitive
# execute: false
# dialect: bigquery
WITH cte AS (
SELECT
refresh_date AS `reFREsh_date`,
term AS `TeRm`,
`rank`
FROM `bigquery-public-data.GooGle_tReNDs.TOp_TeRmS`
)
SELECT
refresh_date AS `Day`,
term AS Top_Term,
rank,
FROM cte
WHERE
rank = 1
AND refresh_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 2 WEEK)
GROUP BY `dAy`, `top_term`, rank
ORDER BY `DaY` DESC;
SELECT
`top_terms`.`refresh_date` AS `day`,
`top_terms`.`term` AS `top_term`,
`top_terms`.`rank` AS `rank`
FROM `bigquery-public-data`.`GooGle_tReNDs`.`TOp_TeRmS` AS `top_terms`
WHERE
`top_terms`.`rank` = 1
AND CAST(`top_terms`.`refresh_date` AS DATE) >= DATE_SUB(CURRENT_DATE, INTERVAL 2 WEEK)
GROUP BY
`day`,
`top_term`,
`rank`
ORDER BY
`day` DESC;
# title: group by keys cannot be simplified
SELECT a + 1 + 1 + 1 + 1 AS b, 2 + 1 AS c FROM x GROUP BY a + 1 + 1 HAVING a + 1 + 1 + 1 + 1 > 1;
SELECT
"x"."a" + 1 + 1 + 1 + 1 AS "b",
3 AS "c"
FROM "x" AS "x"
GROUP BY
"x"."a" + 1 + 1
HAVING
"x"."a" + 1 + 1 + 1 + 1 > 1;
# title: replace alias with mult expression without wrapping it
WITH cte AS (SELECT a * b AS c, a AS d, b as e FROM x) SELECT c + d - (c - e) AS f FROM cte;
SELECT
"x"."a" * "x"."b" + "x"."a" - (
"x"."a" * "x"."b" - "x"."b"
) AS "f"
FROM "x" AS "x";
# title: wrapped table without alias
# execute: false
SELECT * FROM (tbl);
SELECT
*
FROM (
"tbl" AS "tbl"
);
# title: wrapped table with alias
# execute: false
SELECT * FROM (tbl AS tbl);
SELECT
*
FROM (
"tbl" AS "tbl"
);
# title: wrapped join of tables without alias
SELECT a, c FROM (x LEFT JOIN y ON a = c);
SELECT
"x"."a" AS "a",
"y"."c" AS "c"
FROM (
"x" AS "x"
LEFT JOIN "y" AS "y"
ON "x"."a" = "y"."c"
);
# title: wrapped join of tables with alias
# execute: false
SELECT a, c FROM (x LEFT JOIN y ON a = c) AS t;
SELECT
"x"."a" AS "a",
"y"."c" AS "c"
FROM "x" AS "x"
LEFT JOIN "y" AS "y"
ON "x"."a" = "y"."c";
# title: chained wrapped joins without aliases
# execute: false
SELECT * FROM ((a CROSS JOIN ((b CROSS JOIN c) CROSS JOIN (d CROSS JOIN e))));
SELECT
*
FROM (
(
"a" AS "a"
CROSS JOIN (
(
"b" AS "b"
CROSS JOIN "c" AS "c"
)
CROSS JOIN (
"d" AS "d"
CROSS JOIN "e" AS "e"
)
)
)
);
# title: chained wrapped joins with aliases
# execute: false
SELECT * FROM ((a AS foo CROSS JOIN b AS bar) CROSS JOIN c AS baz);
SELECT
*
FROM (
(
"a" AS "foo"
CROSS JOIN "b" AS "bar"
)
CROSS JOIN "c" AS "baz"
);
# title: table joined with join construct
SELECT x.a, y.b, z.c FROM x LEFT JOIN (y INNER JOIN z ON y.c = z.c) ON x.b = y.b;
SELECT
"x"."a" AS "a",
"y"."b" AS "b",
"z"."c" AS "c"
FROM "x" AS "x"
LEFT JOIN (
"y" AS "y"
JOIN "z" AS "z"
ON "y"."c" = "z"."c"
)
ON "x"."b" = "y"."b";
# title: select * from table joined with join construct
# execute: false
SELECT * FROM x LEFT JOIN (y INNER JOIN z ON y.c = z.c) ON x.b = y.b;
SELECT
"y"."b" AS "b",
"y"."c" AS "c",
"z"."a" AS "a",
"z"."c" AS "c",
"x"."a" AS "a",
"x"."b" AS "b"
FROM "x" AS "x"
LEFT JOIN (
"y" AS "y"
JOIN "z" AS "z"
ON "y"."c" = "z"."c"
)
ON "x"."b" = "y"."b";
# title: select * from wrapped subquery
# execute: false
SELECT * FROM ((SELECT * FROM tbl));
WITH "_q_0" AS (
SELECT
*
FROM "tbl" AS "tbl"
)
SELECT
*
FROM (
"_q_0" AS "_q_0"
);
# title: select * from wrapped subquery joined to a table (known schema)
SELECT * FROM ((SELECT * FROM x) INNER JOIN y ON a = c);
SELECT
"x"."a" AS "a",
"x"."b" AS "b",
"y"."b" AS "b",
"y"."c" AS "c"
FROM (
"x" AS "x"
JOIN "y" AS "y"
ON "x"."a" = "y"."c"
);
# title: select * from wrapped subquery joined to a table (unknown schema)
# execute: false
SELECT * FROM ((SELECT c FROM t1) JOIN t2);
WITH "_q_0" AS (
SELECT
"t1"."c" AS "c"
FROM "t1" AS "t1"
)
SELECT
*
FROM (
"_q_0" AS "_q_0"
CROSS JOIN "t2" AS "t2"
);
# title: select specific columns from wrapped subquery joined to a table
SELECT b FROM ((SELECT a FROM x) INNER JOIN y ON a = b);
SELECT
"y"."b" AS "b"
FROM (
"x" AS "x"
JOIN "y" AS "y"
ON "x"."a" = "y"."b"
);
# title: select * from wrapped join of subqueries (unknown schema)
# execute: false
SELECT * FROM ((SELECT * FROM t1) JOIN (SELECT * FROM t2));
WITH "_q_0" AS (
SELECT
*
FROM "t1" AS "t1"
), "_q_1" AS (
SELECT
*
FROM "t2" AS "t2"
)
SELECT
*
FROM (
"_q_0" AS "_q_0"
CROSS JOIN "_q_1" AS "_q_1"
);
# title: select * from wrapped join of subqueries (known schema)
SELECT * FROM ((SELECT * FROM x) INNER JOIN (SELECT * FROM y) ON a = c);
SELECT
"x"."a" AS "a",
"x"."b" AS "b",
"y"."b" AS "b",
"y"."c" AS "c"
FROM (
"x" AS "x"
JOIN "y" AS "y"
ON "x"."a" = "y"."c"
);
# title: replace scalar subquery, wrap resulting column in a MAX
SELECT a, SUM(c) / (SELECT SUM(c) FROM y) * 100 AS foo FROM y INNER JOIN x ON y.b = x.b GROUP BY a;
WITH "_u_0" AS (
SELECT
SUM("y"."c") AS "_col_0"
FROM "y" AS "y"
)
SELECT
"x"."a" AS "a",
SUM("y"."c") / MAX("_u_0"."_col_0") * 100 AS "foo"
FROM "y" AS "y"
CROSS JOIN "_u_0" AS "_u_0"
JOIN "x" AS "x"
ON "y"."b" = "x"."b"
GROUP BY
"x"."a";
|