-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathinline
More file actions
2001 lines (1903 loc) · 80.7 KB
/
Copy pathinline
File metadata and controls
2001 lines (1903 loc) · 80.7 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
exec-ddl
CREATE TABLE a (k INT PRIMARY KEY, i INT, f FLOAT, s STRING, j JSON)
----
exec-ddl
CREATE TABLE xy (x INT PRIMARY KEY, y INT)
----
exec-ddl
CREATE TABLE computed (a INT PRIMARY KEY, b INT, c INT AS (a+b+1) STORED)
----
exec-ddl
CREATE TABLE b (k INT PRIMARY KEY, i INT, f FLOAT, s STRING NOT NULL, j JSON)
----
exec-ddl
CREATE TABLE virt (
k INT PRIMARY KEY,
i INT,
s STRING,
j JSON,
v STRING AS (lower(s)) VIRTUAL,
x JSON AS (j->'x') VIRTUAL,
INDEX (v),
INVERTED INDEX (x)
)
----
# --------------------------------------------------
# InlineConstVar
# --------------------------------------------------
norm expect=InlineConstVar
SELECT k FROM b WHERE i=5 AND i IN (1, 2, 3, 4, 5)
----
project
├── columns: k:1!null
├── key: (1)
└── select
├── columns: k:1!null i:2!null
├── key: (1)
├── fd: ()-->(2)
├── scan b
│ ├── columns: k:1!null i:2
│ ├── key: (1)
│ └── fd: (1)-->(2)
└── filters
└── i:2 = 5 [outer=(2), constraints=(/2: [/5 - /5]; tight), fd=()-->(2)]
norm expect=InlineConstVar
SELECT k FROM b WHERE i=8 AND 3 = mod(i, 5)
----
project
├── columns: k:1!null
├── key: (1)
└── select
├── columns: k:1!null i:2!null
├── key: (1)
├── fd: ()-->(2)
├── scan b
│ ├── columns: k:1!null i:2
│ ├── key: (1)
│ └── fd: (1)-->(2)
└── filters
└── i:2 = 8 [outer=(2), constraints=(/2: [/8 - /8]; tight), fd=()-->(2)]
norm expect=InlineConstVar
SELECT k FROM b WHERE i=4 AND i IN (1, 2, 3, 4)
----
project
├── columns: k:1!null
├── key: (1)
└── select
├── columns: k:1!null i:2!null
├── key: (1)
├── fd: ()-->(2)
├── scan b
│ ├── columns: k:1!null i:2
│ ├── key: (1)
│ └── fd: (1)-->(2)
└── filters
└── i:2 = 4 [outer=(2), constraints=(/2: [/4 - /4]; tight), fd=()-->(2)]
# Case that requires multiple iterations to fully inline.
norm expect=InlineConstVar
SELECT * FROM xy WHERE x=y AND y=4 AND x IN (1, 2, 3, 4)
----
select
├── columns: x:1!null y:2!null
├── cardinality: [0 - 1]
├── key: ()
├── fd: ()-->(1,2)
├── scan xy
│ ├── columns: x:1!null y:2
│ ├── key: (1)
│ └── fd: (1)-->(2)
└── filters
├── x:1 = 4 [outer=(1), constraints=(/1: [/4 - /4]; tight), fd=()-->(1)]
└── y:2 = 4 [outer=(2), constraints=(/2: [/4 - /4]; tight), fd=()-->(2)]
norm expect=InlineConstVar
SELECT * FROM xy WHERE x=y AND y=4 AND x=3
----
values
├── columns: x:1!null y:2!null
├── cardinality: [0 - 0]
├── key: ()
└── fd: ()-->(1,2)
# Can't inline composite types.
norm expect-not=InlineConstVar
SELECT * FROM (VALUES (0.0), (0.00), (0.000)) AS v (x) WHERE x = 0 AND x::STRING = '0.00';
----
select
├── columns: x:1!null
├── cardinality: [0 - 3]
├── immutable
├── fd: ()-->(1)
├── values
│ ├── columns: column1:1!null
│ ├── cardinality: [3 - 3]
│ ├── (0.0,)
│ ├── (0.00,)
│ └── (0.000,)
└── filters
├── column1:1 = 0 [outer=(1), immutable, constraints=(/1: [/0 - /0]; tight), fd=()-->(1)]
└── column1:1::STRING = '0.00' [outer=(1), immutable]
# The rule should trigger, but not inline the composite type.
norm expect=InlineConstVar
SELECT * FROM (VALUES (0.0, 'a'), (0.00, 'b'), (0.000, 'b')) AS v (x, y) WHERE x = 0 AND x::STRING = '0.00' AND y = 'b' AND y IN ('a', 'b');
----
select
├── columns: x:1!null y:2!null
├── cardinality: [0 - 3]
├── immutable
├── fd: ()-->(1,2)
├── values
│ ├── columns: column1:1!null column2:2!null
│ ├── cardinality: [3 - 3]
│ ├── (0.0, 'a')
│ ├── (0.00, 'b')
│ └── (0.000, 'b')
└── filters
├── column1:1 = 0 [outer=(1), immutable, constraints=(/1: [/0 - /0]; tight), fd=()-->(1)]
├── column1:1::STRING = '0.00' [outer=(1), immutable]
└── column2:2 = 'b' [outer=(2), constraints=(/2: [/'b' - /'b']; tight), fd=()-->(2)]
# Ensure that InlineConstVar fires before filter pushdown rules.
norm expect=InlineConstVar
SELECT * FROM a INNER JOIN xy ON True WHERE y=10 AND i<y
----
inner-join (cross)
├── columns: k:1!null i:2!null f:3 s:4 j:5 x:8!null y:9!null
├── key: (1,8)
├── fd: ()-->(9), (1)-->(2-5)
├── select
│ ├── columns: k:1!null i:2!null f:3 s:4 j:5
│ ├── key: (1)
│ ├── fd: (1)-->(2-5)
│ ├── scan a
│ │ ├── columns: k:1!null i:2 f:3 s:4 j:5
│ │ ├── key: (1)
│ │ └── fd: (1)-->(2-5)
│ └── filters
│ └── i:2 < 10 [outer=(2), constraints=(/2: (/NULL - /9]; tight)]
├── select
│ ├── columns: x:8!null y:9!null
│ ├── key: (8)
│ ├── fd: ()-->(9)
│ ├── scan xy
│ │ ├── columns: x:8!null y:9
│ │ ├── key: (8)
│ │ └── fd: (8)-->(9)
│ └── filters
│ └── y:9 = 10 [outer=(9), constraints=(/9: [/10 - /10]; tight), fd=()-->(9)]
└── filters (true)
# Regression test for #164666. A placeholder equality like i = $1 should
# propagate into correlated subqueries referencing the same column.
norm expect=InlineConstVar
SELECT k FROM a WHERE i = $1 AND EXISTS(SELECT 1 FROM xy WHERE y = a.i)
----
project
├── columns: k:1!null
├── has-placeholder
├── key: (1)
└── select
├── columns: k:1!null i:2!null
├── has-placeholder
├── key: (1)
├── fd: ()-->(2)
├── scan a
│ ├── columns: k:1!null i:2
│ ├── key: (1)
│ └── fd: (1)-->(2)
└── filters
├── i:2 = $1 [outer=(2), constraints=(/2: (/NULL - ]), fd=()-->(2)]
└── coalesce [subquery]
├── subquery
│ └── project
│ ├── columns: column14:14!null
│ ├── cardinality: [0 - 1]
│ ├── has-placeholder
│ ├── key: ()
│ ├── fd: ()-->(14)
│ ├── limit
│ │ ├── columns: y:9!null
│ │ ├── cardinality: [0 - 1]
│ │ ├── has-placeholder
│ │ ├── key: ()
│ │ ├── fd: ()-->(9)
│ │ ├── select
│ │ │ ├── columns: y:9!null
│ │ │ ├── has-placeholder
│ │ │ ├── fd: ()-->(9)
│ │ │ ├── limit hint: 1.00
│ │ │ ├── scan xy
│ │ │ │ ├── columns: y:9
│ │ │ │ └── limit hint: 100.00
│ │ │ └── filters
│ │ │ └── y:9 = $1 [outer=(9), constraints=(/9: (/NULL - ]), fd=()-->(9)]
│ │ └── 1
│ └── projections
│ └── true [as=column14:14]
└── false
# Regression test for #170544. InlineConstVar must not replace a variable with a
# constant whose type is equivalent but not identical to the variable's type
# without a cast, since that changes the result of type-sensitive expressions
# such as pg_typeof. Here n is NAME and the constant 'hello' is STRING; the
# inlined constant is cast back to NAME so pg_typeof(n) still reports 'name' and
# the row is not incorrectly filtered out.
exec-ddl
CREATE TABLE t_name (n NAME)
----
norm expect=InlineConstVar
SELECT * FROM t_name WHERE n = 'hello' AND pg_typeof(n)::TEXT = 'name'
----
select
├── columns: n:1!null
├── fd: ()-->(1)
├── scan t_name
│ └── columns: n:1
└── filters
└── n:1 = 'hello' [outer=(1), constraints=(/1: [/'hello' - /'hello']; tight), fd=()-->(1)]
# --------------------------------------------------
# InlineProjectConstants
# --------------------------------------------------
# Inline constants from Project expression.
norm expect=InlineProjectConstants
UPDATE computed SET a=1, b=2
----
update computed
├── columns: <none>
├── fetch columns: a:6 b:7 c:8
├── update-mapping:
│ ├── a_new:11 => a:1
│ ├── b_new:12 => b:2
│ └── c_comp:13 => c:3
├── cardinality: [0 - 0]
├── volatile, mutations
└── project
├── columns: c_comp:13!null a_new:11!null b_new:12!null a:6!null b:7 c:8
├── key: (6)
├── fd: ()-->(11-13), (6)-->(7,8)
├── scan computed
│ ├── columns: a:6!null b:7 c:8
│ ├── computed column expressions
│ │ └── c:8
│ │ └── (a:6 + b:7) + 1
│ ├── flags: avoid-full-scan
│ ├── key: (6)
│ └── fd: (6)-->(7,8)
└── projections
├── 4 [as=c_comp:13]
├── 1 [as=a_new:11]
└── 2 [as=b_new:12]
# Inline constants from Values expression.
norm expect=InlineProjectConstants
SELECT one+two+three+four FROM (VALUES (1, $1:::int, 2, $2:::int)) AS t(one, two, three, four)
----
project
├── columns: "?column?":5
├── cardinality: [1 - 1]
├── immutable, has-placeholder
├── key: ()
├── fd: ()-->(5)
├── values
│ ├── columns: column2:2 column4:4
│ ├── cardinality: [1 - 1]
│ ├── has-placeholder
│ ├── key: ()
│ ├── fd: ()-->(2,4)
│ └── ($1, $2)
└── projections
└── column4:4 + ((column2:2 + 1) + 2) [as="?column?":5, outer=(2,4), immutable]
# Multiple constant columns, multiple refs to each, interspersed with other
# columns.
norm expect=InlineProjectConstants
SELECT one+two, x, one*two, y FROM (SELECT x, 1 AS one, y, 2 AS two FROM xy)
----
project
├── columns: "?column?":7!null x:1!null "?column?":8!null y:2
├── key: (1)
├── fd: ()-->(7,8), (1)-->(2)
├── scan xy
│ ├── columns: x:1!null y:2
│ ├── key: (1)
│ └── fd: (1)-->(2)
└── projections
├── 3 [as="?column?":7]
└── 2 [as="?column?":8]
# Constant column reference within correlated subquery (which becomes
# uncorrelated as a result).
norm expect=InlineProjectConstants
SELECT EXISTS(SELECT * FROM a WHERE k=one AND i=two) FROM (VALUES (1, 2)) AS t(one, two)
----
values
├── columns: exists:11
├── cardinality: [1 - 1]
├── key: ()
├── fd: ()-->(11)
└── tuple
└── coalesce
├── subquery
│ └── project
│ ├── columns: column12:12!null
│ ├── cardinality: [0 - 1]
│ ├── key: ()
│ ├── fd: ()-->(12)
│ ├── select
│ │ ├── columns: k:3!null i:4!null
│ │ ├── cardinality: [0 - 1]
│ │ ├── key: ()
│ │ ├── fd: ()-->(3,4)
│ │ ├── scan a
│ │ │ ├── columns: k:3!null i:4
│ │ │ ├── key: (3)
│ │ │ └── fd: (3)-->(4)
│ │ └── filters
│ │ ├── k:3 = 1 [outer=(3), constraints=(/3: [/1 - /1]; tight), fd=()-->(3)]
│ │ └── i:4 = 2 [outer=(4), constraints=(/4: [/2 - /2]; tight), fd=()-->(4)]
│ └── projections
│ └── true [as=column12:12]
└── false
# Do not inline constants from Values expression with multiple rows.
norm expect-not=InlineProjectConstants
SELECT one+two FROM (VALUES (1, 2), (3, 4)) AS t(one, two)
----
project
├── columns: "?column?":3!null
├── cardinality: [2 - 2]
├── immutable
├── values
│ ├── columns: column1:1!null column2:2!null
│ ├── cardinality: [2 - 2]
│ ├── (1, 2)
│ └── (3, 4)
└── projections
└── column1:1 + column2:2 [as="?column?":3, outer=(1,2), immutable]
# --------------------------------------------------
# InlineSelectConstants
# --------------------------------------------------
# Inline constants from Project expression.
norm expect=InlineSelectConstants
SELECT * FROM (SELECT 1 AS one from xy) WHERE one > 0
----
project
├── columns: one:5!null
├── fd: ()-->(5)
├── scan xy
└── projections
└── 1 [as=one:5]
# Inline constants from Values expression.
norm expect=InlineSelectConstants
SELECT *
FROM (VALUES ($1:::int, 1, $2:::float, 2)) AS t(one, two, three, four)
WHERE one = two OR three = four
----
select
├── columns: one:1 two:2!null three:3 four:4!null
├── cardinality: [0 - 1]
├── has-placeholder
├── key: ()
├── fd: ()-->(1-4)
├── values
│ ├── columns: column1:1 column2:2!null column3:3 column4:4!null
│ ├── cardinality: [1 - 1]
│ ├── has-placeholder
│ ├── key: ()
│ ├── fd: ()-->(1-4)
│ └── ($1, 1, $2, 2)
└── filters
└── (column1:1 = 1) OR (column3:3 = 2.0) [outer=(1,3)]
# Multiple constant columns, multiple refs to each, interspersed with other
# columns.
norm expect=InlineSelectConstants
SELECT * FROM (SELECT x, 1 AS one, y, 2 AS two FROM xy) WHERE x=one AND y=two
----
project
├── columns: x:1!null one:5!null y:2!null two:6!null
├── cardinality: [0 - 1]
├── key: ()
├── fd: ()-->(1,2,5,6)
├── select
│ ├── columns: x:1!null y:2!null
│ ├── cardinality: [0 - 1]
│ ├── key: ()
│ ├── fd: ()-->(1,2)
│ ├── scan xy
│ │ ├── columns: x:1!null y:2
│ │ ├── key: (1)
│ │ └── fd: (1)-->(2)
│ └── filters
│ ├── x:1 = 1 [outer=(1), constraints=(/1: [/1 - /1]; tight), fd=()-->(1)]
│ └── y:2 = 2 [outer=(2), constraints=(/2: [/2 - /2]; tight), fd=()-->(2)]
└── projections
├── 1 [as=one:5]
└── 2 [as=two:6]
# Do not inline constants from Values expression with multiple rows.
norm expect-not=InlineSelectConstants
SELECT * FROM (VALUES (1, 2), (3, 4)) AS t(one, two) WHERE one=two
----
select
├── columns: one:1!null two:2!null
├── cardinality: [0 - 2]
├── fd: (1)==(2), (2)==(1)
├── values
│ ├── columns: column1:1!null column2:2!null
│ ├── cardinality: [2 - 2]
│ ├── (1, 2)
│ └── (3, 4)
└── filters
└── column1:1 = column2:2 [outer=(1,2), constraints=(/1: (/NULL - ]; /2: (/NULL - ]), fd=(1)==(2), (2)==(1)]
# --------------------------------------------------
# InlineJoinConstantsLeft + InlineJoinConstantsRight
# --------------------------------------------------
norm expect=InlineJoinConstantsLeft
SELECT * FROM (SELECT 1 AS one) LEFT JOIN a ON k=one
----
left-join (cross)
├── columns: one:1!null k:2 i:3 f:4 s:5 j:6
├── cardinality: [1 - 1]
├── multiplicity: left-rows(exactly-one), right-rows(exactly-one)
├── key: ()
├── fd: ()-->(1-6)
├── values
│ ├── columns: one:1!null
│ ├── cardinality: [1 - 1]
│ ├── key: ()
│ ├── fd: ()-->(1)
│ └── (1,)
├── select
│ ├── columns: k:2!null i:3 f:4 s:5 j:6
│ ├── cardinality: [0 - 1]
│ ├── key: ()
│ ├── fd: ()-->(2-6)
│ ├── scan a
│ │ ├── columns: k:2!null i:3 f:4 s:5 j:6
│ │ ├── key: (2)
│ │ └── fd: (2)-->(3-6)
│ └── filters
│ └── k:2 = 1 [outer=(2), constraints=(/2: [/1 - /1]; tight), fd=()-->(2)]
└── filters (true)
norm expect=InlineJoinConstantsRight
SELECT * FROM a FULL JOIN (SELECT 1 AS one) ON k=one
----
full-join (cross)
├── columns: k:1 i:2 f:3 s:4 j:5 one:8
├── cardinality: [1 - ]
├── multiplicity: left-rows(exactly-one), right-rows(one-or-more)
├── key: (1)
├── fd: (1)-->(2-5,8)
├── scan a
│ ├── columns: k:1!null i:2 f:3 s:4 j:5
│ ├── key: (1)
│ └── fd: (1)-->(2-5)
├── values
│ ├── columns: one:8!null
│ ├── cardinality: [1 - 1]
│ ├── key: ()
│ ├── fd: ()-->(8)
│ └── (1,)
└── filters
└── k:1 = 1 [outer=(1), constraints=(/1: [/1 - /1]; tight), fd=()-->(1)]
norm expect=(InlineJoinConstantsLeft,InlineJoinConstantsRight)
SELECT * FROM (SELECT 1 AS one) INNER JOIN (SELECT 2 AS two) ON one=two
----
values
├── columns: one:1!null two:2!null
├── cardinality: [0 - 0]
├── key: ()
└── fd: ()-->(1,2)
# Constant column exists in input, but is not referenced.
norm expect-not=(InlineJoinConstantsLeft,InlineJoinConstantsRight)
SELECT * FROM a INNER JOIN (SELECT 1 AS one, y FROM xy) ON k=y
----
inner-join (hash)
├── columns: k:1!null i:2 f:3 s:4 j:5 one:12!null y:9!null
├── multiplicity: left-rows(zero-or-more), right-rows(zero-or-one)
├── fd: ()-->(12), (1)-->(2-5), (1)==(9), (9)==(1)
├── scan a
│ ├── columns: k:1!null i:2 f:3 s:4 j:5
│ ├── key: (1)
│ └── fd: (1)-->(2-5)
├── project
│ ├── columns: one:12!null y:9
│ ├── fd: ()-->(12)
│ ├── scan xy
│ │ └── columns: y:9
│ └── projections
│ └── 1 [as=one:12]
└── filters
└── k:1 = y:9 [outer=(1,9), constraints=(/1: (/NULL - ]; /9: (/NULL - ]), fd=(1)==(9), (9)==(1)]
# Regression test for #50841
exec-ddl
CREATE TABLE kv (k INT8 PRIMARY KEY, v INT8);
----
exec-ddl
CREATE TABLE kv2 (k2 INT8 PRIMARY KEY, v2 INT8);
----
# Ensure that lookup joins are used.
opt expect-not=InlineJoinConstantsLeft format=hide-all
SELECT v, v2
FROM (SELECT unnest('{1}'::INT8[]) AS lookup_k)
INNER LOOKUP JOIN (SELECT k, v FROM kv) ON k = lookup_k
INNER LOOKUP JOIN (SELECT k2, v2 FROM kv2) ON k2 = lookup_k;
----
project
└── inner-join (lookup kv2)
├── flags: force lookup join (into right side)
├── lookup columns are key
├── inner-join (lookup kv)
│ ├── flags: force lookup join (into right side)
│ ├── lookup columns are key
│ ├── values
│ │ └── (1,)
│ └── filters (true)
└── filters (true)
# Ensure that merge joins are used.
opt expect-not=InlineJoinConstantsLeft format=hide-all
SELECT v, v2
FROM (SELECT unnest('{1}'::INT8[]) AS lookup_k)
INNER MERGE JOIN (SELECT k, v FROM kv) ON k = lookup_k
INNER MERGE JOIN (SELECT k2, v2 FROM kv2) ON k2 = lookup_k;
----
project
└── inner-join (merge)
├── flags: force merge join
├── inner-join (merge)
│ ├── flags: force merge join
│ ├── values
│ │ └── (1,)
│ ├── scan kv
│ └── filters (true)
├── scan kv2
└── filters (true)
# Ensure that merge joins are used.
opt expect-not=InlineJoinConstantsRight format=hide-all
SELECT v, v2
FROM (SELECT k, v FROM kv)
INNER MERGE JOIN (SELECT unnest('{1}'::INT8[]) AS lookup_k) ON k = lookup_k
INNER MERGE JOIN (SELECT k2, v2 FROM kv2) ON k2 = lookup_k;
----
project
└── inner-join (merge)
├── flags: force merge join
├── project
│ ├── scan kv
│ │ └── constraint: /1: [/1 - /1]
│ └── projections
│ └── 1
├── scan kv2
└── filters (true)
# --------------------------------------------------
# PushSelectIntoInlinableProject
# --------------------------------------------------
# Inline comparison.
norm expect=PushSelectIntoInlinableProject
SELECT * FROM (SELECT i=1 AS expr FROM a) a WHERE expr IS NULL
----
project
├── columns: expr:8
├── select
│ ├── columns: i:2
│ ├── scan a
│ │ └── columns: i:2
│ └── filters
│ └── (i:2 = 1) IS NULL [outer=(2)]
└── projections
└── i:2 = 1 [as=expr:8, outer=(2)]
# Inline arithmetic.
norm expect=PushSelectIntoInlinableProject
SELECT * FROM (SELECT k*2+1 AS expr FROM a) a WHERE expr > 10
----
project
├── columns: expr:8!null
├── immutable
├── select
│ ├── columns: k:1!null
│ ├── immutable
│ ├── key: (1)
│ ├── scan a
│ │ ├── columns: k:1!null
│ │ └── key: (1)
│ └── filters
│ └── (k:1 * 2) > 9 [outer=(1), immutable]
└── projections
└── (k:1 * 2) + 1 [as=expr:8, outer=(1), immutable]
# Inline boolean logic.
norm expect=PushSelectIntoInlinableProject
SELECT * FROM (SELECT NOT(k>1 AND k<=5) AS expr FROM a) a WHERE expr
----
project
├── columns: expr:8!null
├── select
│ ├── columns: k:1!null
│ ├── key: (1)
│ ├── scan a
│ │ ├── columns: k:1!null
│ │ └── key: (1)
│ └── filters
│ └── (k:1 <= 1) OR (k:1 > 5) [outer=(1), constraints=(/1: (/NULL - /1] [/6 - ]; tight)]
└── projections
└── (k:1 <= 1) OR (k:1 > 5) [as=expr:8, outer=(1)]
# Inline case expressions.
norm expect=PushSelectIntoInlinableProject
SELECT * FROM (SELECT CASE WHEN k>0 THEN f>0 WHEN k<0 THEN f<0 ELSE false END AS expr FROM a) a WHERE expr
----
project
├── columns: expr:8
├── select
│ ├── columns: k:1!null f:3
│ ├── key: (1)
│ ├── fd: (1)-->(3)
│ ├── scan a
│ │ ├── columns: k:1!null f:3
│ │ ├── key: (1)
│ │ └── fd: (1)-->(3)
│ └── filters
│ └── CASE WHEN k:1 > 0 THEN f:3 > 0.0 WHEN k:1 < 0 THEN f:3 < 0.0 ELSE false END [outer=(1,3)]
└── projections
└── CASE WHEN k:1 > 0 THEN f:3 > 0.0 WHEN k:1 < 0 THEN f:3 < 0.0 ELSE false END [as=expr:8, outer=(1,3)]
# Inline constants.
norm expect=PushSelectIntoInlinableProject
SELECT * FROM (SELECT (f IS NULL OR f != 10.5) AS expr FROM a) a WHERE expr
----
project
├── columns: expr:8
├── select
│ ├── columns: f:3
│ ├── scan a
│ │ └── columns: f:3
│ └── filters
│ └── (f:3 IS NULL) OR (f:3 != 10.5) [outer=(3), constraints=(/3: [/NULL - /10.499999999999998] [/10.500000000000002 - ]; tight)]
└── projections
└── (f:3 IS NULL) OR (f:3 != 10.5) [as=expr:8, outer=(3)]
# Reference the expression to inline multiple times.
norm expect=PushSelectIntoInlinableProject
SELECT * FROM (SELECT f+1 AS expr FROM a) a WHERE expr=expr
----
project
├── columns: expr:8
├── immutable
├── select
│ ├── columns: f:3
│ ├── immutable
│ ├── scan a
│ │ └── columns: f:3
│ └── filters
│ └── (f:3 + 1.0) IS DISTINCT FROM CAST(NULL AS FLOAT8) [outer=(3), immutable]
└── projections
└── f:3 + 1.0 [as=expr:8, outer=(3), immutable]
# Use outer references in both inlined expression and in referencing expression.
norm expect=PushSelectIntoInlinableProject
SELECT * FROM a WHERE EXISTS(SELECT * FROM (SELECT (x-i) AS expr FROM xy) WHERE expr > i*i)
----
semi-join (cross)
├── columns: k:1!null i:2 f:3 s:4 j:5
├── immutable
├── key: (1)
├── fd: (1)-->(2-5)
├── scan a
│ ├── columns: k:1!null i:2 f:3 s:4 j:5
│ ├── key: (1)
│ └── fd: (1)-->(2-5)
├── scan xy
│ ├── columns: x:8!null
│ └── key: (8)
└── filters
└── (x:8 - i:2) > (i:2 * i:2) [outer=(2,8), immutable]
exec-ddl
CREATE TABLE crdb_internal.zones (
zone_id INT NOT NULL,
cli_specifier STRING NULL,
config_yaml BYTES NOT NULL,
config_protobuf BYTES NOT NULL
)
----
# Regression test for #28827. Ensure that inlining is not applied when there
# is a correlated subquery in the filter.
norm
SELECT
subq_0.c0 AS c0
FROM (SELECT zone_id+1 AS c0, zone_id+2 as c1 FROM crdb_internal.zones) AS subq_0
WHERE
1
>= CASE
WHEN subq_0.c1 IS NOT NULL
THEN pg_catalog.extract(
CAST(
CASE
WHEN
(
EXISTS(
SELECT
ref_1.config_yaml AS c0,
ref_1.config_yaml AS c1,
subq_0.c0 AS c2,
ref_1.config_yaml AS c3
FROM
crdb_internal.zones AS ref_1
WHERE
subq_0.c0 IS NOT NULL
LIMIT
52
)
)
THEN pg_catalog.version()
ELSE pg_catalog.version()
END
AS TEXT
),
CAST(pg_catalog.current_date() AS DATE)
)
ELSE 1
END
LIMIT
107
----
project
├── columns: c0:8!null
├── cardinality: [0 - 107]
├── volatile
└── limit
├── columns: c0:8!null c1:9!null
├── cardinality: [0 - 107]
├── volatile
├── select
│ ├── columns: c0:8!null c1:9!null
│ ├── volatile
│ ├── limit hint: 107.00
│ ├── project
│ │ ├── columns: c0:8!null c1:9!null
│ │ ├── immutable
│ │ ├── limit hint: 321.00
│ │ ├── scan zones
│ │ │ ├── columns: crdb_internal.public.zones.zone_id:1!null
│ │ │ └── limit hint: 321.00
│ │ └── projections
│ │ ├── crdb_internal.public.zones.zone_id:1 + 1 [as=c0:8, outer=(1), immutable]
│ │ └── crdb_internal.public.zones.zone_id:1 + 2 [as=c1:9, outer=(1), immutable]
│ └── filters
│ └── le [outer=(8,9), volatile, correlated-subquery]
│ ├── case
│ │ ├── true
│ │ ├── when
│ │ │ ├── c1:9 IS NOT NULL
│ │ │ └── function: extract
│ │ │ ├── case
│ │ │ │ ├── true
│ │ │ │ ├── when
│ │ │ │ │ ├── exists
│ │ │ │ │ │ └── select
│ │ │ │ │ │ ├── columns: ref_1.config_yaml:12!null
│ │ │ │ │ │ ├── outer: (8)
│ │ │ │ │ │ ├── scan zones [as=ref_1]
│ │ │ │ │ │ │ └── columns: ref_1.config_yaml:12!null
│ │ │ │ │ │ └── filters
│ │ │ │ │ │ └── c0:8 IS NOT NULL [outer=(8), constraints=(/8: (/NULL - ]; tight)]
│ │ │ │ │ └── version()
│ │ │ │ └── version()
│ │ │ └── '2017-05-10'
│ │ └── 1.0
│ └── 1.0
└── 107
# --------------------------------------------------
# InlineSelectVirtualColumns
# --------------------------------------------------
# Inline indexed virtual columns in a Select below the Project.
norm expect=InlineSelectVirtualColumns
SELECT * FROM virt WHERE v = 'foo'
----
project
├── columns: k:1!null i:2 s:3 j:4 v:5 x:6
├── immutable
├── key: (1)
├── fd: (1)-->(2-4,6), (3)-->(5)
├── select
│ ├── columns: k:1!null i:2 s:3 j:4
│ ├── immutable
│ ├── key: (1)
│ ├── fd: (1)-->(2-4)
│ ├── scan virt
│ │ ├── columns: k:1!null i:2 s:3 j:4
│ │ ├── computed column expressions
│ │ │ ├── v:5
│ │ │ │ └── lower(s:3)
│ │ │ └── x:6
│ │ │ └── j:4->'x'
│ │ ├── key: (1)
│ │ └── fd: (1)-->(2-4)
│ └── filters
│ └── lower(s:3) = 'foo' [outer=(3), immutable]
└── projections
├── lower(s:3) [as=v:5, outer=(3), immutable]
└── j:4->'x' [as=x:6, outer=(4), immutable]
# Inline inverted-indexed virtual columns.
norm expect=InlineSelectVirtualColumns
SELECT * FROM virt WHERE x @> '1'
----
project
├── columns: k:1!null i:2 s:3 j:4 v:5 x:6
├── immutable
├── key: (1)
├── fd: (1)-->(2-4,6), (3)-->(5)
├── select
│ ├── columns: k:1!null i:2 s:3 j:4
│ ├── immutable
│ ├── key: (1)
│ ├── fd: (1)-->(2-4)
│ ├── scan virt
│ │ ├── columns: k:1!null i:2 s:3 j:4
│ │ ├── computed column expressions
│ │ │ ├── v:5
│ │ │ │ └── lower(s:3)
│ │ │ └── x:6
│ │ │ └── j:4->'x'
│ │ ├── key: (1)
│ │ └── fd: (1)-->(2-4)
│ └── filters
│ └── (j:4->'x') @> '1' [outer=(4), immutable]
└── projections
├── lower(s:3) [as=v:5, outer=(3), immutable]
└── j:4->'x' [as=x:6, outer=(4), immutable]
# Split the filters and inline only virtual columns.
norm expect=InlineSelectVirtualColumns
SELECT * FROM (
SELECT i, v, upper(s) AS w FROM virt
) WHERE v = 'foo' AND w = 'FOO' AND i = 10
----
select
├── columns: i:2!null v:5 w:10!null
├── immutable
├── fd: ()-->(2,10)
├── project
│ ├── columns: w:10 v:5 i:2!null
│ ├── immutable
│ ├── fd: ()-->(2)
│ ├── select
│ │ ├── columns: i:2!null s:3
│ │ ├── immutable
│ │ ├── fd: ()-->(2)
│ │ ├── scan virt
│ │ │ ├── columns: i:2 s:3
│ │ │ └── computed column expressions
│ │ │ ├── v:5
│ │ │ │ └── lower(s:3)
│ │ │ └── x:6
│ │ │ └── j:4->'x'
│ │ └── filters
│ │ ├── lower(s:3) = 'foo' [outer=(3), immutable]
│ │ └── i:2 = 10 [outer=(2), constraints=(/2: [/10 - /10]; tight), fd=()-->(2)]
│ └── projections
│ ├── upper(s:3) [as=w:10, outer=(3), immutable]
│ └── lower(s:3) [as=v:5, outer=(3), immutable]
└── filters
└── w:10 = 'FOO' [outer=(10), constraints=(/10: [/'FOO' - /'FOO']; tight), fd=()-->(10)]
# Do not inline correlated subqueries.
norm expect-not=InlineSelectVirtualColumns
SELECT * FROM virt v1
WHERE EXISTS (
SELECT * FROM virt v2 WHERE v1.v = v2.s
)
----
semi-join (hash)
├── columns: k:1!null i:2 s:3 j:4 v:5 x:6
├── immutable
├── key: (1)
├── fd: (1)-->(2-4,6), (3)-->(5)
├── project
│ ├── columns: v:5 x:6 k:1!null i:2 s:3 j:4
│ ├── immutable
│ ├── key: (1)
│ ├── fd: (1)-->(2-4,6), (3)-->(5)
│ ├── scan virt
│ │ ├── columns: k:1!null i:2 s:3 j:4
│ │ ├── computed column expressions
│ │ │ ├── v:5
│ │ │ │ └── lower(s:3)
│ │ │ └── x:6
│ │ │ └── j:4->'x'
│ │ ├── key: (1)
│ │ └── fd: (1)-->(2-4)
│ └── projections
│ ├── lower(s:3) [as=v:5, outer=(3), immutable]
│ └── j:4->'x' [as=x:6, outer=(4), immutable]
├── scan virt
│ ├── columns: s:12
│ └── computed column expressions
│ ├── v:14
│ │ └── lower(s:12)
│ └── x:15
│ └── j:13->'x'
└── filters
└── v:5 = s:12 [outer=(5,12), constraints=(/5: (/NULL - ]; /12: (/NULL - ]), fd=(5)==(12), (12)==(5)]
# Regression test for #63794. Inlining filters on virtual columns during
# exploration should not violate the ordering choice that all columns in an
# ordering column group should be equal according to the expression's FD.
exec-ddl
CREATE TABLE t63794_a (a INT)
----
exec-ddl
CREATE TABLE t63794_bc (b INT, c INT AS (b % 2) VIRTUAL, INDEX (b))
----
opt format=hide-all
SELECT a.a
FROM t63794_a AS a
JOIN t63794_bc AS bc1
JOIN t63794_bc AS bc2 ON true
ON a.a = bc1.b AND
bc1.b = bc2.b AND
bc1.c = bc2.c
JOIN t63794_bc AS bc3 ON
bc1.b = bc3.b AND bc2.b = bc3.c
----
project
└── inner-join (hash)
├── scan t63794_a [as=a]
├── inner-join (merge)
│ ├── project
│ │ ├── scan t63794_bc@t63794_bc_b_idx
│ │ └── projections
│ │ └── b % 2
│ ├── inner-join (merge)
│ │ ├── project
│ │ │ ├── scan t63794_bc@t63794_bc_b_idx
│ │ │ └── projections
│ │ │ └── b % 2
│ │ ├── project
│ │ │ ├── scan t63794_bc@t63794_bc_b_idx
│ │ │ └── projections
│ │ │ └── b % 2
│ │ └── filters (true)
│ └── filters (true)
└── filters
└── a = b
# --------------------------------------------------