Skip to content

Commit 9e90e17

Browse files
Lordwormsjonahgao
andauthored
fix: Add additional required expression for natural join (#11713)
* adding test file * replace expand_wildcard * refine test --------- Co-authored-by: jonahgao <[email protected]>
1 parent 81668f3 commit 9e90e17

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

datafusion/expr/src/logical_plan/builder.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,12 @@ pub fn wrap_projection_for_join_if_necessary(
15311531

15321532
let need_project = join_keys.iter().any(|key| !matches!(key, Expr::Column(_)));
15331533
let plan = if need_project {
1534-
let mut projection = expand_wildcard(input_schema, &input, None)?;
1534+
// Include all columns from the input and extend them with the join keys
1535+
let mut projection = input_schema
1536+
.columns()
1537+
.into_iter()
1538+
.map(Expr::Column)
1539+
.collect::<Vec<_>>();
15351540
let join_key_items = alias_join_keys
15361541
.iter()
15371542
.flat_map(|expr| expr.try_as_col().is_none().then_some(expr))

datafusion/sqllogictest/test_files/join.slt

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,4 +1130,51 @@ SELECT * FROM
11301130
(SELECT * FROM t1 CROSS JOIN t2)
11311131
WHERE t1.a + t2.a IS NULL;
11321132
----
1133-
NULL NULL
1133+
NULL NULL
1134+
1135+
statement ok
1136+
CREATE TABLE t5(v0 BIGINT, v1 STRING, v2 BIGINT, v3 STRING, v4 BOOLEAN);
1137+
1138+
statement ok
1139+
CREATE TABLE t1(v0 BIGINT, v1 STRING);
1140+
1141+
statement ok
1142+
CREATE TABLE t0(v0 BIGINT, v1 DOUBLE);
1143+
1144+
query TT
1145+
explain SELECT *
1146+
FROM t1
1147+
NATURAL JOIN t5
1148+
INNER JOIN t0 ON (t0.v1 + t5.v0) > 0
1149+
WHERE t0.v1 = t1.v0;
1150+
----
1151+
logical_plan
1152+
01)Projection: t1.v0, t1.v1, t5.v2, t5.v3, t5.v4, t0.v0, t0.v1
1153+
02)--Inner Join: CAST(t1.v0 AS Float64) = t0.v1 Filter: t0.v1 + CAST(t5.v0 AS Float64) > Float64(0)
1154+
03)----Projection: t1.v0, t1.v1, t5.v0, t5.v2, t5.v3, t5.v4
1155+
04)------Inner Join: Using t1.v0 = t5.v0, t1.v1 = t5.v1
1156+
05)--------TableScan: t1 projection=[v0, v1]
1157+
06)--------TableScan: t5 projection=[v0, v1, v2, v3, v4]
1158+
07)----TableScan: t0 projection=[v0, v1]
1159+
physical_plan
1160+
01)CoalesceBatchesExec: target_batch_size=8192
1161+
02)--HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(CAST(t1.v0 AS Float64)@6, v1@1)], filter=v1@1 + CAST(v0@0 AS Float64) > 0, projection=[v0@0, v1@1, v2@3, v3@4, v4@5, v0@7, v1@8]
1162+
03)----CoalescePartitionsExec
1163+
04)------ProjectionExec: expr=[v0@0 as v0, v1@1 as v1, v0@2 as v0, v2@3 as v2, v3@4 as v3, v4@5 as v4, CAST(v0@0 AS Float64) as CAST(t1.v0 AS Float64)]
1164+
05)--------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
1165+
06)----------CoalesceBatchesExec: target_batch_size=8192
1166+
07)------------HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(v0@0, v0@0), (v1@1, v1@1)], projection=[v0@0, v1@1, v0@2, v2@4, v3@5, v4@6]
1167+
08)--------------MemoryExec: partitions=1, partition_sizes=[0]
1168+
09)--------------MemoryExec: partitions=1, partition_sizes=[0]
1169+
10)----MemoryExec: partitions=1, partition_sizes=[0]
1170+
1171+
1172+
1173+
statement ok
1174+
drop table t5;
1175+
1176+
statement ok
1177+
drop table t1;
1178+
1179+
statement ok
1180+
drop table t0;

0 commit comments

Comments
 (0)