-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
join
9938 lines (9422 loc) · 382 KB
/
join
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 abc
(
a INT,
b INT,
c INT,
INDEX ab (a,b) STORING (c),
INDEX bc (b,c) STORING (a)
)
----
exec-ddl
CREATE TABLE stu
(
s INT,
t INT,
u INT,
PRIMARY KEY (s,t,u),
INDEX uts (u,t,s)
)
----
exec-ddl
CREATE TABLE xyz
(
x INT,
y INT,
z INT,
INDEX xy (x,y) STORING (z),
INDEX yz (y,z) STORING (x)
)
----
exec-ddl
CREATE TABLE pqr
(
p INT PRIMARY KEY,
q INT,
r INT,
s STRING,
t STRING,
INDEX q (q),
INDEX r (r),
INDEX s (s) STORING (r),
INDEX rs (r,s),
INDEX ts (t,s)
)
----
exec-ddl
CREATE TABLE zz (
a INT8 PRIMARY KEY,
b INT8 NULL,
c INT8 NULL,
INDEX idx_b (b ASC),
CONSTRAINT idx_c UNIQUE (c)
)
----
exec-ddl
CREATE TABLE virt (
k INT PRIMARY KEY,
i INT,
j INT NOT NULL,
v1 INT AS (i + 10) VIRTUAL,
v2 INT AS (i + 100) VIRTUAL,
v3 INT AS (i + j) VIRTUAL,
CHECK (j IN (10, 20, 30))
)
----
exec-ddl
CREATE TABLE large (m INT, n INT)
----
exec-ddl
CREATE TABLE medium (m INT, n INT)
----
exec-ddl
CREATE TABLE small (m INT, n INT)
----
exec-ddl
ALTER TABLE large INJECT STATISTICS '[
{
"columns": ["m"],
"created_at": "2018-05-01 1:00:00.00000+00:00",
"row_count": 100000000000,
"distinct_count": 100000000
}
]'
----
exec-ddl
ALTER TABLE medium INJECT STATISTICS '[
{
"columns": ["m"],
"created_at": "2018-05-01 1:00:00.00000+00:00",
"row_count": 100000,
"distinct_count": 1000
}
]'
----
exec-ddl
ALTER TABLE small INJECT STATISTICS '[
{
"columns": ["m"],
"created_at": "2018-05-01 1:00:00.00000+00:00",
"row_count": 10,
"distinct_count": 6
}
]'
----
# --------------------------------------------------
# ReorderJoins
# --------------------------------------------------
exec-ddl
ALTER TABLE abc INJECT STATISTICS '[
{
"columns": ["a"],
"created_at": "2018-05-01 1:00:00.00000+00:00",
"row_count": 1000,
"distinct_count": 1000
}
]'
----
exec-ddl
ALTER TABLE stu INJECT STATISTICS '[
{
"columns": ["s"],
"created_at": "2018-05-01 1:00:00.00000+00:00",
"row_count": 10000,
"distinct_count": 1000
}
]'
----
exec-ddl
ALTER TABLE xyz INJECT STATISTICS '[
{
"columns": ["x"],
"created_at": "2018-05-01 1:00:00.00000+00:00",
"row_count": 1000,
"distinct_count": 1000
}
]'
----
# Check that the equality condition abc.a = xyz.x is synthesized.
opt expect=ReorderJoins
SELECT * FROM abc, stu, xyz WHERE abc.a=stu.s AND stu.s=xyz.x
----
inner-join (merge)
├── columns: a:1!null b:2 c:3 s:7!null t:8!null u:9!null x:12!null y:13 z:14
├── left ordering: +7
├── right ordering: +12
├── fd: (7)==(1,12), (12)==(1,7), (1)==(7,12)
├── scan stu
│ ├── columns: s:7!null t:8!null u:9!null
│ ├── key: (7-9)
│ └── ordering: +7
├── inner-join (merge)
│ ├── columns: a:1!null b:2 c:3 x:12!null y:13 z:14
│ ├── left ordering: +1
│ ├── right ordering: +12
│ ├── fd: (1)==(12), (12)==(1)
│ ├── ordering: +(1|12) [actual: +1]
│ ├── scan abc@ab
│ │ ├── columns: a:1 b:2 c:3
│ │ └── ordering: +1
│ ├── scan xyz@xy
│ │ ├── columns: x:12 y:13 z:14
│ │ └── ordering: +12
│ └── filters (true)
└── filters (true)
memo expect=ReorderJoins
SELECT * FROM abc, stu, xyz WHERE abc.a=stu.s AND stu.s=xyz.x
----
memo (optimized, ~42KB, required=[presentation: a:1,b:2,c:3,s:7,t:8,u:9,x:12,y:13,z:14])
├── G1: (inner-join G2 G3 G4) (inner-join G3 G2 G4) (inner-join G5 G6 G7) (inner-join G6 G5 G7) (inner-join G8 G9 G7) (inner-join G9 G8 G7) (merge-join G2 G3 G10 inner-join,+1,+7) (merge-join G3 G2 G10 inner-join,+7,+1) (lookup-join G3 G10 abc@ab,keyCols=[7],outCols=(1-3,7-9,12-14)) (merge-join G5 G6 G10 inner-join,+7,+12) (merge-join G6 G5 G10 inner-join,+12,+7) (lookup-join G6 G10 stu,keyCols=[12],outCols=(1-3,7-9,12-14)) (merge-join G8 G9 G10 inner-join,+7,+12) (lookup-join G8 G10 xyz@xy,keyCols=[7],outCols=(1-3,7-9,12-14)) (merge-join G9 G8 G10 inner-join,+12,+7)
│ └── [presentation: a:1,b:2,c:3,s:7,t:8,u:9,x:12,y:13,z:14]
│ ├── best: (merge-join G5="[ordering: +7]" G6="[ordering: +(1|12)]" G10 inner-join,+7,+12)
│ └── cost: 13045.10
├── G2: (scan abc,cols=(1-3)) (scan abc@ab,cols=(1-3)) (scan abc@bc,cols=(1-3))
│ ├── [ordering: +1]
│ │ ├── best: (scan abc@ab,cols=(1-3))
│ │ └── cost: 1094.72
│ └── []
│ ├── best: (scan abc,cols=(1-3))
│ └── cost: 1094.72
├── G3: (inner-join G5 G9 G7) (inner-join G9 G5 G7) (merge-join G5 G9 G10 inner-join,+7,+12) (lookup-join G5 G10 xyz@xy,keyCols=[7],outCols=(7-9,12-14)) (merge-join G9 G5 G10 inner-join,+12,+7) (lookup-join G9 G10 stu,keyCols=[12],outCols=(7-9,12-14))
│ ├── [ordering: +(7|12)]
│ │ ├── best: (merge-join G5="[ordering: +7]" G9="[ordering: +12]" G10 inner-join,+7,+12)
│ │ └── cost: 11920.36
│ └── []
│ ├── best: (merge-join G5="[ordering: +7]" G9="[ordering: +12]" G10 inner-join,+7,+12)
│ └── cost: 11920.36
├── G4: (filters G11)
├── G5: (scan stu,cols=(7-9)) (scan stu@uts,cols=(7-9))
│ ├── [ordering: +7]
│ │ ├── best: (scan stu,cols=(7-9))
│ │ └── cost: 10624.62
│ └── []
│ ├── best: (scan stu,cols=(7-9))
│ └── cost: 10624.62
├── G6: (inner-join G2 G9 G12) (inner-join G9 G2 G12) (merge-join G2 G9 G10 inner-join,+1,+12) (lookup-join G2 G10 xyz@xy,keyCols=[1],outCols=(1-3,12-14)) (merge-join G9 G2 G10 inner-join,+12,+1) (lookup-join G9 G10 abc@ab,keyCols=[12],outCols=(1-3,12-14))
│ ├── [ordering: +(1|12)]
│ │ ├── best: (merge-join G2="[ordering: +1]" G9="[ordering: +12]" G10 inner-join,+1,+12)
│ │ └── cost: 2219.46
│ └── []
│ ├── best: (merge-join G2="[ordering: +1]" G9="[ordering: +12]" G10 inner-join,+1,+12)
│ └── cost: 2219.46
├── G7: (filters G13)
├── G8: (inner-join G2 G5 G4) (inner-join G5 G2 G4) (merge-join G2 G5 G10 inner-join,+1,+7) (lookup-join G2 G10 stu,keyCols=[1],outCols=(1-3,7-9)) (merge-join G5 G2 G10 inner-join,+7,+1) (lookup-join G5 G10 abc@ab,keyCols=[7],outCols=(1-3,7-9))
│ ├── [ordering: +(1|7)]
│ │ ├── best: (merge-join G5="[ordering: +7]" G2="[ordering: +1]" G10 inner-join,+7,+1)
│ │ └── cost: 11920.36
│ └── []
│ ├── best: (merge-join G5="[ordering: +7]" G2="[ordering: +1]" G10 inner-join,+7,+1)
│ └── cost: 11920.36
├── G9: (scan xyz,cols=(12-14)) (scan xyz@xy,cols=(12-14)) (scan xyz@yz,cols=(12-14))
│ ├── [ordering: +12]
│ │ ├── best: (scan xyz@xy,cols=(12-14))
│ │ └── cost: 1094.72
│ └── []
│ ├── best: (scan xyz,cols=(12-14))
│ └── cost: 1094.72
├── G10: (filters)
├── G11: (eq G14 G15)
├── G12: (filters G16)
├── G13: (eq G15 G17)
├── G14: (variable a)
├── G15: (variable s)
├── G16: (eq G14 G17)
└── G17: (variable x)
# Cross joins are not reordered apart from commutation.
memo
SELECT * FROM abc, stu, xyz, pqr WHERE a = 1
----
memo (optimized, ~28KB, required=[presentation: a:1,b:2,c:3,s:7,t:8,u:9,x:12,y:13,z:14,p:18,q:19,r:20,s:21,t:22])
├── G1: (inner-join G2 G3 G4) (inner-join G3 G2 G4)
│ └── [presentation: a:1,b:2,c:3,s:7,t:8,u:9,x:12,y:13,z:14,p:18,q:19,r:20,s:21,t:22]
│ ├── best: (inner-join G3 G2 G4)
│ └── cost: 325035580.20
├── G2: (select G5 G6) (scan abc@ab,cols=(1-3),constrained)
│ └── []
│ ├── best: (scan abc@ab,cols=(1-3),constrained)
│ └── cost: 15.09
├── G3: (inner-join G7 G8 G4) (inner-join G8 G7 G4)
│ └── []
│ ├── best: (inner-join G8 G7 G4)
│ └── cost: 100035565.07
├── G4: (filters)
├── G5: (scan abc,cols=(1-3)) (scan abc@ab,cols=(1-3)) (scan abc@bc,cols=(1-3))
│ └── []
│ ├── best: (scan abc,cols=(1-3))
│ └── cost: 1094.72
├── G6: (filters G9)
├── G7: (scan stu,cols=(7-9)) (scan stu@uts,cols=(7-9))
│ └── []
│ ├── best: (scan stu,cols=(7-9))
│ └── cost: 10624.62
├── G8: (inner-join G10 G11 G4) (inner-join G11 G10 G4)
│ └── []
│ ├── best: (inner-join G10 G11 G4)
│ └── cost: 12249.91
├── G9: (eq G12 G13)
├── G10: (scan xyz,cols=(12-14)) (scan xyz@xy,cols=(12-14)) (scan xyz@yz,cols=(12-14))
│ └── []
│ ├── best: (scan xyz,cols=(12-14))
│ └── cost: 1094.72
├── G11: (scan pqr,cols=(18-22))
│ └── []
│ ├── best: (scan pqr,cols=(18-22))
│ └── cost: 1125.02
├── G12: (variable a)
└── G13: (const 1)
# Joins that exceed reorder_joins_limit are not reordered apart from
# commutation.
memo join-limit=1
SELECT *
FROM stu, abc, xyz, pqr
WHERE u = a AND a = x AND x = p
----
memo (optimized, ~37KB, required=[presentation: s:1,t:2,u:3,a:6,b:7,c:8,x:12,y:13,z:14,p:18,q:19,r:20,s:21,t:22])
├── G1: (inner-join G2 G3 G4) (inner-join G3 G2 G4) (merge-join G2 G3 G5 inner-join,+3,+6) (merge-join G3 G2 G5 inner-join,+6,+3) (lookup-join G3 G5 stu@uts,keyCols=[6],outCols=(1-3,6-8,12-14,18-22))
│ └── [presentation: s:1,t:2,u:3,a:6,b:7,c:8,x:12,y:13,z:14,p:18,q:19,r:20,s:21,t:22]
│ ├── best: (merge-join G2="[ordering: +3]" G3="[ordering: +(6|12|18)]" G5 inner-join,+3,+6)
│ └── cost: 14200.14
├── G2: (scan stu,cols=(1-3)) (scan stu@uts,cols=(1-3))
│ ├── [ordering: +3]
│ │ ├── best: (scan stu@uts,cols=(1-3))
│ │ └── cost: 10624.62
│ └── []
│ ├── best: (scan stu,cols=(1-3))
│ └── cost: 10624.62
├── G3: (inner-join G6 G7 G8) (inner-join G7 G6 G8) (merge-join G6 G7 G5 inner-join,+6,+12) (merge-join G7 G6 G5 inner-join,+12,+6) (lookup-join G7 G5 abc@ab,keyCols=[12],outCols=(6-8,12-14,18-22))
│ ├── [ordering: +(6|12|18)]
│ │ ├── best: (merge-join G6="[ordering: +6]" G7="[ordering: +(12|18)]" G5 inner-join,+6,+12)
│ │ └── cost: 3374.50
│ └── []
│ ├── best: (merge-join G6="[ordering: +6]" G7="[ordering: +(12|18)]" G5 inner-join,+6,+12)
│ └── cost: 3374.50
├── G4: (filters G9)
├── G5: (filters)
├── G6: (scan abc,cols=(6-8)) (scan abc@ab,cols=(6-8)) (scan abc@bc,cols=(6-8))
│ ├── [ordering: +6]
│ │ ├── best: (scan abc@ab,cols=(6-8))
│ │ └── cost: 1094.72
│ └── []
│ ├── best: (scan abc,cols=(6-8))
│ └── cost: 1094.72
├── G7: (inner-join G10 G11 G12) (inner-join G11 G10 G12) (merge-join G10 G11 G5 inner-join,+12,+18) (lookup-join G10 G5 pqr,keyCols=[12],outCols=(12-14,18-22)) (merge-join G11 G10 G5 inner-join,+18,+12) (lookup-join G11 G5 xyz@xy,keyCols=[18],outCols=(12-14,18-22))
│ ├── [ordering: +(12|18)]
│ │ ├── best: (merge-join G10="[ordering: +12]" G11="[ordering: +18]" G5 inner-join,+12,+18)
│ │ └── cost: 2249.76
│ └── []
│ ├── best: (merge-join G10="[ordering: +12]" G11="[ordering: +18]" G5 inner-join,+12,+18)
│ └── cost: 2249.76
├── G8: (filters G13)
├── G9: (eq G14 G15)
├── G10: (scan xyz,cols=(12-14)) (scan xyz@xy,cols=(12-14)) (scan xyz@yz,cols=(12-14))
│ ├── [ordering: +12]
│ │ ├── best: (scan xyz@xy,cols=(12-14))
│ │ └── cost: 1094.72
│ └── []
│ ├── best: (scan xyz,cols=(12-14))
│ └── cost: 1094.72
├── G11: (scan pqr,cols=(18-22))
│ ├── [ordering: +18]
│ │ ├── best: (scan pqr,cols=(18-22))
│ │ └── cost: 1125.02
│ └── []
│ ├── best: (scan pqr,cols=(18-22))
│ └── cost: 1125.02
├── G12: (filters G16)
├── G13: (eq G15 G17)
├── G14: (variable u)
├── G15: (variable a)
├── G16: (eq G17 G18)
├── G17: (variable x)
└── G18: (variable p)
# Case where an implicit filter is used in the final plan (abc.a = pqr.p in the
# merge join).
opt expect=ReorderJoins format=hide-all
SELECT *
FROM abc,
stu,
xyz,
(SELECT * FROM pqr WHERE p = 5)
WHERE a = stu.s
AND q = y
AND stu.s = p
----
inner-join (lookup stu)
├── inner-join (lookup xyz@yz)
│ ├── inner-join (lookup abc@ab)
│ │ ├── scan pqr
│ │ │ └── constraint: /18: [/5 - /5]
│ │ └── filters (true)
│ └── filters (true)
└── filters (true)
# The apply join at the top of the tree should not be reordered. However, the
# lower-level inner join can be reordered despite the fact that it has outer
# columns.
memo expect=ReorderJoins
SELECT *
FROM abc
INNER JOIN LATERAL (
SELECT *
FROM (SELECT * FROM (VALUES (a+1), (b*2)) f(v))
INNER JOIN stu
ON s = v
)
ON a = v
----
memo (optimized, ~15KB, required=[presentation: a:1,b:2,c:3,v:7,s:8,t:9,u:10])
├── G1: (inner-join-apply G2 G3 G4)
│ └── [presentation: a:1,b:2,c:3,v:7,s:8,t:9,u:10]
│ ├── best: (inner-join-apply G2 G3 G4)
│ └── cost: 1189.24
├── G2: (scan abc,cols=(1-3)) (scan abc@ab,cols=(1-3)) (scan abc@bc,cols=(1-3))
│ └── []
│ ├── best: (scan abc,cols=(1-3))
│ └── cost: 1094.72
├── G3: (inner-join G5 G6 G7) (inner-join G6 G5 G7) (lookup-join G5 G8 stu,keyCols=[7],outCols=(7-10)) (merge-join G6 G5 G8 inner-join,+8,+7)
│ └── []
│ ├── best: (lookup-join G5 G8 stu,keyCols=[7],outCols=(7-10))
│ └── cost: 81.45
├── G4: (filters G9)
├── G5: (values G10 id=v1)
│ ├── [ordering: +7]
│ │ ├── best: (sort G5)
│ │ └── cost: 0.14
│ └── []
│ ├── best: (values G10 id=v1)
│ └── cost: 0.03
├── G6: (scan stu,cols=(8-10)) (scan stu@uts,cols=(8-10))
│ ├── [ordering: +8]
│ │ ├── best: (scan stu,cols=(8-10))
│ │ └── cost: 10624.62
│ └── []
│ ├── best: (scan stu,cols=(8-10))
│ └── cost: 10624.62
├── G7: (filters G11)
├── G8: (filters)
├── G9: (eq G12 G13)
├── G10: (scalar-list G14 G15)
├── G11: (eq G16 G13)
├── G12: (variable a)
├── G13: (variable column1)
├── G14: (tuple G17)
├── G15: (tuple G18)
├── G16: (variable s)
├── G17: (scalar-list G19)
├── G18: (scalar-list G20)
├── G19: (plus G12 G21)
├── G20: (mult G22 G23)
├── G21: (const 1)
├── G22: (variable b)
└── G23: (const 2)
# Only commutation is possible for a single join.
exploretrace rule=ReorderJoins format=hide-all
SELECT * FROM abc, stu WHERE a = s
----
----
================================================================================
ReorderJoins
================================================================================
Source expression:
inner-join (hash)
├── scan abc
├── scan stu
└── filters
└── a = s
New expression 1 of 1:
inner-join (hash)
├── scan stu
├── scan abc
└── filters
└── a = s
----
----
# No-op case because the joins have join flags.
opt expect-not=ReorderJoins format=hide-all
SELECT *
FROM abc
INNER HASH JOIN stu
ON a = s
INNER MERGE JOIN xyz
ON s = x
----
inner-join (merge)
├── flags: force merge join
├── sort
│ └── inner-join (hash)
│ ├── flags: force hash join (store right side)
│ ├── scan abc
│ ├── scan stu
│ └── filters
│ └── a = s
├── scan xyz@xy
└── filters (true)
# No-op case because InnerJoinApply is not matched.
opt expect-not=ReorderJoins format=hide-all
SELECT *
FROM abc
INNER JOIN LATERAL (SELECT * FROM (VALUES (a+1), (b*2)) f(v))
ON a = v
----
inner-join-apply
├── scan abc
├── values
│ ├── (a + 1,)
│ └── (b * 2,)
└── filters
└── a = column1
opt expect=ReorderJoins format=hide-all
SELECT * FROM abc
LEFT JOIN stu ON a = s
LEFT JOIN small ON a = m
----
right-join (merge)
├── scan stu
├── left-join (merge)
│ ├── scan abc@ab
│ ├── sort
│ │ └── scan small
│ └── filters (true)
└── filters (true)
# Don't add a join between xyz and stu to the memo (because doing so would
# create a cross join).
exploretrace rule=ReorderJoins format=hide-all
SELECT *
FROM stu
INNER JOIN abc
ON s = a
INNER JOIN xyz
ON b = y
----
----
================================================================================
ReorderJoins
================================================================================
Source expression:
inner-join (hash)
├── inner-join (hash)
│ ├── scan stu
│ ├── scan abc
│ └── filters
│ └── s = a
├── scan xyz
└── filters
└── b = y
New expression 1 of 1:
inner-join (hash)
├── inner-join (hash)
│ ├── scan abc
│ ├── scan stu
│ └── filters
│ └── s = a
├── scan xyz
└── filters
└── b = y
================================================================================
ReorderJoins
================================================================================
Source expression:
inner-join (hash)
├── inner-join (merge)
│ ├── scan stu
│ ├── scan abc@ab
│ └── filters (true)
├── scan xyz
└── filters
└── b = y
New expression 1 of 3:
inner-join (hash)
├── scan stu
├── inner-join (hash)
│ ├── scan abc
│ ├── scan xyz
│ └── filters
│ └── b = y
└── filters
└── s = a
New expression 2 of 3:
inner-join (hash)
├── inner-join (hash)
│ ├── scan abc
│ ├── scan xyz
│ └── filters
│ └── b = y
├── scan stu
└── filters
└── s = a
New expression 3 of 3:
inner-join (hash)
├── scan xyz
├── inner-join (merge)
│ ├── scan stu
│ ├── scan abc@ab
│ └── filters (true)
└── filters
└── b = y
----
----
# Left join under an inner join. Push the inner join into the left input of the
# left join.
opt expect=ReorderJoins
SELECT *
FROM
(
SELECT *
FROM medium
LEFT JOIN large ON large.m = medium.m
) medium
INNER JOIN small ON medium.n = small.n
----
right-join (hash)
├── columns: m:1 n:2!null m:6 n:7 m:11 n:12!null
├── fd: (2)==(12), (12)==(2)
├── scan large
│ └── columns: large.m:6 large.n:7
├── inner-join (hash)
│ ├── columns: medium.m:1 medium.n:2!null small.m:11 small.n:12!null
│ ├── fd: (2)==(12), (12)==(2)
│ ├── scan medium
│ │ └── columns: medium.m:1 medium.n:2
│ ├── scan small
│ │ └── columns: small.m:11 small.n:12
│ └── filters
│ └── medium.n:2 = small.n:12 [outer=(2,12), constraints=(/2: (/NULL - ]; /12: (/NULL - ]), fd=(2)==(12), (12)==(2)]
└── filters
└── large.m:6 = medium.m:1 [outer=(1,6), constraints=(/1: (/NULL - ]; /6: (/NULL - ]), fd=(1)==(6), (6)==(1)]
# Left join under an inner join. Push the inner join into the left input of the
# left join, but leave the (small.m:9 = large.n:6) OR (large.n:6 IS NULL) filter
# on a select above the join tree. It can't be pushed into the left join because
# it originated from an inner join.
opt expect=ReorderJoins
SELECT *
FROM
(
SELECT medium.n, large.n
FROM medium
LEFT JOIN large ON large.m = medium.m
) result(a, b)
INNER JOIN small
ON result.a = small.n AND (small.m = result.b OR result.b IS NULL)
----
project
├── columns: a:2!null b:7 m:11 n:12!null
├── fd: (2)==(12), (12)==(2)
└── select
├── columns: medium.m:1 medium.n:2!null large.m:6 large.n:7 small.m:11 small.n:12!null
├── fd: (2)==(12), (12)==(2)
├── right-join (hash)
│ ├── columns: medium.m:1 medium.n:2!null large.m:6 large.n:7 small.m:11 small.n:12!null
│ ├── fd: (2)==(12), (12)==(2)
│ ├── scan large
│ │ └── columns: large.m:6 large.n:7
│ ├── inner-join (hash)
│ │ ├── columns: medium.m:1 medium.n:2!null small.m:11 small.n:12!null
│ │ ├── fd: (2)==(12), (12)==(2)
│ │ ├── scan medium
│ │ │ └── columns: medium.m:1 medium.n:2
│ │ ├── scan small
│ │ │ └── columns: small.m:11 small.n:12
│ │ └── filters
│ │ └── medium.n:2 = small.n:12 [outer=(2,12), constraints=(/2: (/NULL - ]; /12: (/NULL - ]), fd=(2)==(12), (12)==(2)]
│ └── filters
│ └── large.m:6 = medium.m:1 [outer=(1,6), constraints=(/1: (/NULL - ]; /6: (/NULL - ]), fd=(1)==(6), (6)==(1)]
└── filters
└── (small.m:11 = large.n:7) OR (large.n:7 IS NULL) [outer=(7,11)]
# Semi join under an inner join. Push the inner join into the left input of the
# semi join.
opt expect=ReorderJoins
SELECT *
FROM
(
SELECT medium.n
FROM medium
WHERE EXISTS (SELECT * FROM large WHERE medium.m = large.m)
) medium
INNER JOIN small ON small.n = medium.n
----
project
├── columns: n:2!null m:11 n:12!null
├── fd: (2)==(12), (12)==(2)
└── semi-join (hash)
├── columns: medium.m:1 medium.n:2!null small.m:11 small.n:12!null
├── fd: (2)==(12), (12)==(2)
├── inner-join (hash)
│ ├── columns: medium.m:1 medium.n:2!null small.m:11 small.n:12!null
│ ├── fd: (2)==(12), (12)==(2)
│ ├── scan medium
│ │ └── columns: medium.m:1 medium.n:2
│ ├── scan small
│ │ └── columns: small.m:11 small.n:12
│ └── filters
│ └── small.n:12 = medium.n:2 [outer=(2,12), constraints=(/2: (/NULL - ]; /12: (/NULL - ]), fd=(2)==(12), (12)==(2)]
├── scan large
│ └── columns: large.m:6
└── filters
└── medium.m:1 = large.m:6 [outer=(1,6), constraints=(/1: (/NULL - ]; /6: (/NULL - ]), fd=(1)==(6), (6)==(1)]
# Anti join under an inner join. Push the inner join into the left input of the
# anti join.
opt expect=ReorderJoins
SELECT *
FROM
(
SELECT medium.n
FROM medium
WHERE NOT EXISTS (SELECT * FROM large WHERE medium.m <> large.m)
) medium
INNER JOIN small ON small.n = medium.n
----
project
├── columns: n:2!null m:11 n:12!null
├── fd: (2)==(12), (12)==(2)
└── anti-join (cross)
├── columns: medium.m:1 medium.n:2!null small.m:11 small.n:12!null
├── fd: (2)==(12), (12)==(2)
├── inner-join (hash)
│ ├── columns: medium.m:1 medium.n:2!null small.m:11 small.n:12!null
│ ├── fd: (2)==(12), (12)==(2)
│ ├── scan medium
│ │ └── columns: medium.m:1 medium.n:2
│ ├── scan small
│ │ └── columns: small.m:11 small.n:12
│ └── filters
│ └── small.n:12 = medium.n:2 [outer=(2,12), constraints=(/2: (/NULL - ]; /12: (/NULL - ]), fd=(2)==(12), (12)==(2)]
├── scan large
│ └── columns: large.m:6
└── filters
└── medium.m:1 != large.m:6 [outer=(1,6), constraints=(/1: (/NULL - ]; /6: (/NULL - ])]
# Inner join in the right input of a left join; no reordering is valid apart
# from commutation.
exploretrace rule=ReorderJoins format=hide-all
SELECT *
FROM small
LEFT JOIN
(
SELECT large.m
FROM large
INNER JOIN medium
ON large.n = medium.n
) large
ON small.m = large.m
----
----
================================================================================
ReorderJoins
================================================================================
Source expression:
project
└── left-join (hash)
├── scan small
├── inner-join (hash)
│ ├── scan large
│ ├── scan medium
│ └── filters
│ └── large.n = medium.n
└── filters
└── small.m = large.m
New expression 1 of 1:
project
└── left-join (hash)
├── scan small
├── inner-join (hash)
│ ├── scan medium
│ ├── scan large
│ └── filters
│ └── large.n = medium.n
└── filters
└── small.m = large.m
================================================================================
ReorderJoins
================================================================================
Source expression:
project
└── left-join (hash)
├── scan small
├── inner-join (hash)
│ ├── scan large
│ ├── scan medium
│ └── filters
│ └── large.n = medium.n
└── filters
└── small.m = large.m
No new expressions.
----
----
exploretrace rule=ReorderJoins format=hide-all
SELECT *
FROM
(
SELECT medium.n, large.n
FROM medium
LEFT JOIN large ON large.m = medium.m
) result(a, b)
INNER JOIN small
ON result.a = small.n AND (small.m = result.b OR result.b IS NULL)
INNER JOIN xyz ON True
----
----
================================================================================
ReorderJoins
================================================================================
Source expression:
project
└── inner-join (cross)
├── inner-join (hash)
│ ├── left-join (hash)
│ │ ├── scan medium
│ │ ├── scan large
│ │ └── filters
│ │ └── large.m = medium.m
│ ├── scan small
│ └── filters
│ ├── medium.n = small.n
│ └── (small.m = large.n) OR (large.n IS NULL)
├── scan xyz
└── filters (true)
No new expressions.
================================================================================
ReorderJoins
================================================================================
Source expression:
project
└── inner-join (cross)
├── inner-join (hash)
│ ├── right-join (hash)
│ │ ├── scan large
│ │ ├── scan medium
│ │ └── filters
│ │ └── large.m = medium.m
│ ├── scan small
│ └── filters
│ ├── medium.n = small.n
│ └── (small.m = large.n) OR (large.n IS NULL)
├── scan xyz
└── filters (true)
New expression 1 of 2:
project
└── inner-join (cross)
├── select
│ ├── left-join (hash)
│ │ ├── inner-join (hash)
│ │ │ ├── scan medium
│ │ │ ├── scan small
│ │ │ └── filters
│ │ │ └── medium.n = small.n
│ │ ├── scan large
│ │ └── filters
│ │ └── large.m = medium.m
│ └── filters
│ └── (small.m = large.n) OR (large.n IS NULL)
├── scan xyz
└── filters (true)
New expression 2 of 2:
project
└── inner-join (cross)
├── inner-join (hash)
│ ├── scan small
│ ├── right-join (hash)
│ │ ├── scan large
│ │ ├── scan medium
│ │ └── filters
│ │ └── large.m = medium.m
│ └── filters
│ ├── medium.n = small.n
│ └── (small.m = large.n) OR (large.n IS NULL)
├── scan xyz
└── filters (true)
================================================================================
ReorderJoins
================================================================================
Source expression:
project
└── inner-join (cross)
├── select
│ ├── right-join (hash)
│ │ ├── scan large
│ │ ├── inner-join (hash)
│ │ │ ├── scan medium
│ │ │ ├── scan small
│ │ │ └── filters
│ │ │ └── medium.n = small.n
│ │ └── filters
│ │ └── large.m = medium.m
│ └── filters
│ └── (small.m = large.n) OR (large.n IS NULL)
├── scan xyz
└── filters (true)
New expression 1 of 1:
project
└── inner-join (cross)
├── scan xyz
├── select
│ ├── right-join (hash)
│ │ ├── scan large
│ │ ├── inner-join (hash)
│ │ │ ├── scan medium
│ │ │ ├── scan small
│ │ │ └── filters
│ │ │ └── medium.n = small.n
│ │ └── filters
│ │ └── large.m = medium.m
│ └── filters
│ └── (small.m = large.n) OR (large.n IS NULL)
└── filters (true)
----
----
# Full join on top of a full join.
exploretrace rule=ReorderJoins format=hide-all
SELECT * FROM abc
FULL JOIN stu ON s = a
FULL JOIN xyz ON b = y
----
----
================================================================================
ReorderJoins
================================================================================
Source expression:
full-join (hash)
├── full-join (hash)
│ ├── scan abc
│ ├── scan stu
│ └── filters
│ └── s = a
├── scan xyz
└── filters
└── b = y
New expression 1 of 1:
full-join (hash)
├── full-join (hash)
│ ├── scan stu
│ ├── scan abc
│ └── filters
│ └── s = a
├── scan xyz
└── filters
└── b = y
================================================================================
ReorderJoins
================================================================================
Source expression:
full-join (hash)
├── full-join (merge)
│ ├── scan stu
│ ├── scan abc@ab
│ └── filters (true)
├── scan xyz
└── filters
└── b = y
New expression 1 of 3:
full-join (hash)
├── full-join (hash)
│ ├── scan abc
│ ├── scan xyz
│ └── filters
│ └── b = y
├── scan stu
└── filters
└── s = a
New expression 2 of 3:
full-join (hash)
├── scan stu
├── full-join (hash)
│ ├── scan abc
│ ├── scan xyz
│ └── filters
│ └── b = y
└── filters
└── s = a
New expression 3 of 3:
full-join (hash)
├── scan xyz
├── full-join (merge)
│ ├── scan stu
│ ├── scan abc@ab
│ └── filters (true)
└── filters
└── b = y
----
----
# Full join on top of a full join. Lower join is in right input of upper join.
exploretrace rule=ReorderJoins format=hide-all
SELECT * FROM abc
FULL JOIN
(
SELECT * FROM stu
FULL JOIN xyz ON t = y
) f(s, t, u, x, y, z)
ON a = x
----