diff --git a/datafusion/sql/src/select.rs b/datafusion/sql/src/select.rs index 59cef5df0e975..118cef10b52ab 100644 --- a/datafusion/sql/src/select.rs +++ b/datafusion/sql/src/select.rs @@ -80,6 +80,21 @@ struct RewrittenUnnestExprGroups { expr_groups: Vec>, } +/// The USING-join column sets of a SELECT's input plan, collected on first +/// use: collecting walks the whole input plan, and wildcard-only projections +/// never need them. +#[derive(Default)] +struct LazyUsingColumns(Option>>); + +impl LazyUsingColumns { + fn get(&mut self, plan: &LogicalPlan) -> Result<&[HashSet]> { + if self.0.is_none() { + self.0 = Some(plan.using_columns()?); + } + Ok(self.0.as_deref().unwrap_or_default()) + } +} + fn flatten_expr_groups(expr_groups: Vec>) -> Vec { expr_groups.into_iter().flatten().collect() } @@ -1114,9 +1129,16 @@ impl SqlToRel<'_, S> { ) -> Result> { let mut prepared_select_exprs = Vec::with_capacity(projection.len()); let mut error_builder = DataFusionErrorBuilder::new(); + let mut using_columns = LazyUsingColumns::default(); for expr in projection { - match self.sql_select_to_rex(expr, plan, empty_from, planner_context) { + match self.sql_select_to_rex( + expr, + plan, + &mut using_columns, + empty_from, + planner_context, + ) { Ok(expr) => prepared_select_exprs.push(expr), Err(err) => error_builder.add_error(err), } @@ -1129,6 +1151,7 @@ impl SqlToRel<'_, S> { &self, sql: SelectItem, plan: &LogicalPlan, + using_columns: &mut LazyUsingColumns, empty_from: bool, planner_context: &mut PlannerContext, ) -> Result { @@ -1138,7 +1161,7 @@ impl SqlToRel<'_, S> { let col = normalize_col_with_schemas_and_ambiguity_check( expr, &[&[plan.schema()]], - &plan.using_columns()?, + using_columns.get(plan)?, )?; Ok(SelectExpr::Expression(col)) @@ -1149,7 +1172,7 @@ impl SqlToRel<'_, S> { let col = normalize_col_with_schemas_and_ambiguity_check( select_expr, &[&[plan.schema()]], - &plan.using_columns()?, + using_columns.get(plan)?, )?; let name = self.ident_normalizer.normalize(alias); // avoiding adding an alias if the column name is the same. @@ -1170,6 +1193,7 @@ impl SqlToRel<'_, S> { } let planned_options = self.plan_wildcard_options( plan, + using_columns, empty_from, planner_context, options, @@ -1192,6 +1216,7 @@ impl SqlToRel<'_, S> { let qualifier = self.object_name_to_table_reference(object_name)?; let planned_options = self.plan_wildcard_options( plan, + using_columns, empty_from, planner_context, options, @@ -1229,6 +1254,7 @@ impl SqlToRel<'_, S> { fn plan_wildcard_options( &self, plan: &LogicalPlan, + using_columns: &mut LazyUsingColumns, empty_from: bool, planner_context: &mut PlannerContext, options: WildcardAdditionalOptions, @@ -1248,6 +1274,7 @@ impl SqlToRel<'_, S> { self.sql_select_to_rex( SelectItem::UnnamedExpr(item.expr.clone()), plan, + using_columns, empty_from, planner_context, ) diff --git a/datafusion/sqllogictest/test_files/wildcard.slt b/datafusion/sqllogictest/test_files/wildcard.slt index 7a84429b90a21..1bbcaf946bb19 100644 --- a/datafusion/sqllogictest/test_files/wildcard.slt +++ b/datafusion/sqllogictest/test_files/wildcard.slt @@ -146,3 +146,23 @@ select *, a as aka from t; statement count 0 drop table t; + +# REPLACE over a USING join: the replacements resolve the unqualified join key +# through the USING columns, and the key appears once +statement ok +create table using_l(k int, v int) as values (1, 10), (2, 20); + +statement ok +create table using_r(k int, w int) as values (1, 100), (2, 200); + +query III rowsort +select * replace (k + v as v, k * w as w) from using_l join using_r using (k); +---- +1 11 100 +2 22 400 + +statement count 0 +drop table using_l; + +statement count 0 +drop table using_r;