|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | + |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +# Test PropagateEmptyRelation rule: outer joins where one side is |
| 19 | +# an EmptyRelation should be replaced with a null-padded projection. |
| 20 | + |
| 21 | +statement ok |
| 22 | +create table t1(a int, b varchar, c double); |
| 23 | + |
| 24 | +statement ok |
| 25 | +create table t2(x int, y varchar, z double); |
| 26 | + |
| 27 | +statement ok |
| 28 | +insert into t1 values (1, 'a', 10.0), (2, 'b', 20.0), (3, 'c', 30.0); |
| 29 | + |
| 30 | +statement ok |
| 31 | +insert into t2 values (1, 'p', 100.0), (2, 'q', 200.0); |
| 32 | + |
| 33 | +statement ok |
| 34 | +set datafusion.explain.logical_plan_only = true; |
| 35 | + |
| 36 | +### |
| 37 | +### LEFT JOIN with empty right (WHERE false subquery) |
| 38 | +### |
| 39 | + |
| 40 | +# The join should be eliminated — no join operator in the plan |
| 41 | +query TT |
| 42 | +explain select * from t1 left join (select * from t2 where false) r on t1.a = r.x; |
| 43 | +---- |
| 44 | +logical_plan |
| 45 | +01)Projection: t1.a, t1.b, t1.c, Int32(NULL) AS x, Utf8View(NULL) AS y, Float64(NULL) AS z |
| 46 | +02)--TableScan: t1 projection=[a, b, c] |
| 47 | + |
| 48 | +# Verify result correctness — all left rows with NULLs on right |
| 49 | +query ITRITR rowsort |
| 50 | +select * from t1 left join (select * from t2 where false) r on t1.a = r.x; |
| 51 | +---- |
| 52 | +1 a 10 NULL NULL NULL |
| 53 | +2 b 20 NULL NULL NULL |
| 54 | +3 c 30 NULL NULL NULL |
| 55 | + |
| 56 | +### |
| 57 | +### RIGHT JOIN with empty left |
| 58 | +### |
| 59 | + |
| 60 | +query TT |
| 61 | +explain select * from (select * from t1 where false) l right join t2 on l.a = t2.x; |
| 62 | +---- |
| 63 | +logical_plan |
| 64 | +01)Projection: Int32(NULL) AS a, Utf8View(NULL) AS b, Float64(NULL) AS c, t2.x, t2.y, t2.z |
| 65 | +02)--TableScan: t2 projection=[x, y, z] |
| 66 | + |
| 67 | +query ITRITR rowsort |
| 68 | +select * from (select * from t1 where false) l right join t2 on l.a = t2.x; |
| 69 | +---- |
| 70 | +NULL NULL NULL 1 p 100 |
| 71 | +NULL NULL NULL 2 q 200 |
| 72 | + |
| 73 | +### |
| 74 | +### FULL JOIN with empty right |
| 75 | +### |
| 76 | + |
| 77 | +query TT |
| 78 | +explain select * from t1 full join (select * from t2 where false) r on t1.a = r.x; |
| 79 | +---- |
| 80 | +logical_plan |
| 81 | +01)Projection: t1.a, t1.b, t1.c, Int32(NULL) AS x, Utf8View(NULL) AS y, Float64(NULL) AS z |
| 82 | +02)--TableScan: t1 projection=[a, b, c] |
| 83 | + |
| 84 | +query ITRITR rowsort |
| 85 | +select * from t1 full join (select * from t2 where false) r on t1.a = r.x; |
| 86 | +---- |
| 87 | +1 a 10 NULL NULL NULL |
| 88 | +2 b 20 NULL NULL NULL |
| 89 | +3 c 30 NULL NULL NULL |
| 90 | + |
| 91 | +### |
| 92 | +### FULL JOIN with empty left |
| 93 | +### |
| 94 | + |
| 95 | +query TT |
| 96 | +explain select * from (select * from t1 where false) l full join t2 on l.a = t2.x; |
| 97 | +---- |
| 98 | +logical_plan |
| 99 | +01)Projection: Int32(NULL) AS a, Utf8View(NULL) AS b, Float64(NULL) AS c, t2.x, t2.y, t2.z |
| 100 | +02)--TableScan: t2 projection=[x, y, z] |
| 101 | + |
| 102 | +query ITRITR rowsort |
| 103 | +select * from (select * from t1 where false) l full join t2 on l.a = t2.x; |
| 104 | +---- |
| 105 | +NULL NULL NULL 1 p 100 |
| 106 | +NULL NULL NULL 2 q 200 |
| 107 | + |
| 108 | +### |
| 109 | +### Filter on top of optimized join |
| 110 | +### |
| 111 | + |
| 112 | +query TT |
| 113 | +explain select * from t1 left join (select * from t2 where false) r on t1.a = r.x where t1.a > 1; |
| 114 | +---- |
| 115 | +logical_plan |
| 116 | +01)Projection: t1.a, t1.b, t1.c, Int32(NULL) AS x, Utf8View(NULL) AS y, Float64(NULL) AS z |
| 117 | +02)--Filter: t1.a > Int32(1) |
| 118 | +03)----TableScan: t1 projection=[a, b, c] |
| 119 | + |
| 120 | +query ITRITR rowsort |
| 121 | +select * from t1 left join (select * from t2 where false) r on t1.a = r.x where t1.a > 1; |
| 122 | +---- |
| 123 | +2 b 20 NULL NULL NULL |
| 124 | +3 c 30 NULL NULL NULL |
| 125 | + |
| 126 | +### |
| 127 | +### Cleanup |
| 128 | +### |
| 129 | + |
| 130 | +statement ok |
| 131 | +set datafusion.explain.logical_plan_only = false; |
| 132 | + |
| 133 | +statement ok |
| 134 | +drop table t1; |
| 135 | + |
| 136 | +statement ok |
| 137 | +drop table t2; |
0 commit comments