Skip to content

Commit

Permalink
Planner: use DFSchema::merge in create_relation_subquery
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aalexandrov committed Jul 14, 2024
1 parent 06c5e7f commit c0a65a4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion datafusion/sql/src/relation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
// the `for` loop in `plan_table_with_joins`.
let old_from_schema = planner_context.set_outer_from_schema(None).unwrap();
let new_query_schema = match planner_context.outer_query_schema() {
Some(lhs) => Some(Arc::new(lhs.join(&old_from_schema)?)),
Some(old_query_schema) => {
let mut new_query_schema = old_from_schema.as_ref().clone();
new_query_schema.merge(&old_query_schema);
Some(Arc::new(new_query_schema))
}
None => Some(Arc::clone(&old_from_schema)),
};
let old_query_schema = planner_context.set_outer_query_schema(new_query_schema);
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sql/tests/sql_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3219,7 +3219,7 @@ fn lateral_comma_join_with_shadowing() {
let sql = "\
SELECT * FROM j1, LATERAL (\
SELECT * FROM j1, LATERAL (\
SELECT * FROM j2 WHERE j1.j1_id = j2_id\
SELECT * FROM j2 WHERE j1_id = j2_id\
) as j2\
) as j2;";
let expected = "Projection: j1.j1_id, j1.j1_string, j2.j1_id, j2.j1_string, j2.j2_id, j2.j2_string\
Expand Down

0 comments on commit c0a65a4

Please sign in to comment.