Skip to content

Commit c0a65a4

Browse files
committed
Planner: use DFSchema::merge in create_relation_subquery
In order to compute the `set_outer_from_schema` argument we currently use `DFSchema::join`. When we combine the current outer FROM schema with the current outer query schema columns from the latter should override columns from the first, so the correct way is to use `DFSchema::merge`. To witness the fix, note that the query in the fixed test case isn't planned as expected without the accompanying changes.
1 parent 06c5e7f commit c0a65a4

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

datafusion/sql/src/relation/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
161161
// the `for` loop in `plan_table_with_joins`.
162162
let old_from_schema = planner_context.set_outer_from_schema(None).unwrap();
163163
let new_query_schema = match planner_context.outer_query_schema() {
164-
Some(lhs) => Some(Arc::new(lhs.join(&old_from_schema)?)),
164+
Some(old_query_schema) => {
165+
let mut new_query_schema = old_from_schema.as_ref().clone();
166+
new_query_schema.merge(&old_query_schema);
167+
Some(Arc::new(new_query_schema))
168+
}
165169
None => Some(Arc::clone(&old_from_schema)),
166170
};
167171
let old_query_schema = planner_context.set_outer_query_schema(new_query_schema);

datafusion/sql/tests/sql_integration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3219,7 +3219,7 @@ fn lateral_comma_join_with_shadowing() {
32193219
let sql = "\
32203220
SELECT * FROM j1, LATERAL (\
32213221
SELECT * FROM j1, LATERAL (\
3222-
SELECT * FROM j2 WHERE j1.j1_id = j2_id\
3222+
SELECT * FROM j2 WHERE j1_id = j2_id\
32233223
) as j2\
32243224
) as j2;";
32253225
let expected = "Projection: j1.j1_id, j1.j1_string, j2.j1_id, j2.j1_string, j2.j2_id, j2.j2_string\

0 commit comments

Comments
 (0)