Skip to content

Commit cef9462

Browse files
committed
fix(sql): fix a bug when planning semi- or antijoins
Currently, the `exclude_using_columns` called from `expand_wildcard` doesn't consider the filtering semantics of semi- and antijoins when expanding wildcards on top of joins defined via `USING(<columns>)` syntax. From each set of columns equated by a `USING(<column>)` expression, the code currently (1) sorts the set entries, and (2) retains only the first entry from each set. Because of that, the columns surviving the `exclude_using_columns` call might be wrongly chosen from the filtering side if the table qualifier from that side is lexicographically before the filtered side qualifier. For example, given this schema of two identical tables: ```sql create table s(x1 int, x2 int, x3 int); create table t(x1 int, x2 int, x3 int); ``` One would expect that the schema of queries where the `s` and `t` names are swapped will be identical. However, currently this is not the case: ```sql -- Q1 schema: x1 int, x2 int, x3 int (because s < t) select * from s left semi join t using (x1); -- Q2 schema: x2 int, x3 int (because t < s) select * from t left semi join s using (x1); ``` This commit fixes the issue and adds some regression tests.
1 parent 11b9693 commit cef9462

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

datafusion/expr/src/utils.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,17 @@ fn get_exprs_except_skipped(
386386
/// (once for each join side), but an unqualified wildcard should include it only once.
387387
/// This function returns the columns that should be excluded.
388388
fn exclude_using_columns(plan: &LogicalPlan) -> Result<HashSet<Column>> {
389+
let output_columns: HashSet<_> = plan.schema().columns().iter().cloned().collect();
389390
let using_columns = plan.using_columns()?;
390391
let excluded = using_columns
391392
.into_iter()
392393
// For each USING JOIN condition, only expand to one of each join column in projection
393394
.flat_map(|cols| {
394-
let mut cols = cols.into_iter().collect::<Vec<_>>();
395+
// Only consider columns that survive in the output schema.
396+
let mut cols = cols
397+
.into_iter()
398+
.filter(|c| output_columns.contains(c))
399+
.collect::<Vec<_>>();
395400
// sort join columns to make sure we consistently keep the same
396401
// qualified column
397402
cols.sort();

datafusion/sql/tests/sql_integration.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4847,6 +4847,40 @@ fn test_using_join_wildcard_schema() {
48474847
);
48484848
}
48494849

4850+
#[test]
4851+
fn test_using_join_wildcard_schema_semi_anti() {
4852+
let left_expected = vec!["s.x1".to_string(), "s.x2".to_string(), "s.x3".to_string()];
4853+
let right_expected = vec!["t.x1".to_string(), "t.x2".to_string(), "t.x3".to_string()];
4854+
4855+
let sql = "WITH
4856+
s AS (SELECT 1 AS x1, 2 AS x2, 3 AS x3),
4857+
t AS (SELECT 1 AS x1, 4 AS x2, 5 AS x3)
4858+
SELECT * FROM s LEFT SEMI JOIN t USING (x1)";
4859+
let plan = logical_plan(sql).unwrap();
4860+
assert_eq!(plan.schema().field_names(), left_expected);
4861+
4862+
let sql = "WITH
4863+
s AS (SELECT 1 AS x1, 2 AS x2, 3 AS x3),
4864+
t AS (SELECT 1 AS x1, 4 AS x2, 5 AS x3)
4865+
SELECT * FROM s RIGHT SEMI JOIN t USING (x1)";
4866+
let plan = logical_plan(sql).unwrap();
4867+
assert_eq!(plan.schema().field_names(), right_expected);
4868+
4869+
let sql = "WITH
4870+
s AS (SELECT 1 AS x1, 2 AS x2, 3 AS x3),
4871+
t AS (SELECT 1 AS x1, 4 AS x2, 5 AS x3)
4872+
SELECT * FROM s LEFT ANTI JOIN t USING (x1)";
4873+
let plan = logical_plan(sql).unwrap();
4874+
assert_eq!(plan.schema().field_names(), left_expected);
4875+
4876+
let sql = "WITH
4877+
s AS (SELECT 1 AS x1, 2 AS x2, 3 AS x3),
4878+
t AS (SELECT 1 AS x1, 4 AS x2, 5 AS x3)
4879+
SELECT * FROM s RIGHT ANTI JOIN t USING (x1)";
4880+
let plan = logical_plan(sql).unwrap();
4881+
assert_eq!(plan.schema().field_names(), right_expected);
4882+
}
4883+
48504884
#[test]
48514885
fn test_2_nested_lateral_join_with_the_deepest_join_referencing_the_outer_most_relation()
48524886
{

0 commit comments

Comments
 (0)