Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions datafusion/sql/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ struct RewrittenUnnestExprGroups {
expr_groups: Vec<Vec<Expr>>,
}

/// 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<Vec<HashSet<Column>>>);

impl LazyUsingColumns {
fn get(&mut self, plan: &LogicalPlan) -> Result<&[HashSet<Column>]> {
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>>) -> Vec<Expr> {
expr_groups.into_iter().flatten().collect()
}
Expand Down Expand Up @@ -1114,9 +1129,16 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
) -> Result<Vec<SelectExpr>> {
let mut prepared_select_exprs = Vec::with_capacity(projection.len());
let mut error_builder = DataFusionErrorBuilder::new();
let mut using_columns = LazyUsingColumns::default();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could/should put this structure on the PlannerContext as it applies to all calls within that query?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @alamb! I don't think it fits on PlannerContext: the set depends on the SELECT's input plan, and the context is shared by every SELECT in the query (derived tables, subqueries, set-operation legs, CTEs), each with a different input. Keeping it correct there would mean saving and restoring it around every SELECT like outer_queries_schemas_stack, or keying it by plan. One projection is exactly the scope where the input is fixed, so that's where the cache lives.

What's left is that each nested SELECT still walks its own input once, inner SELECTs included. Reusing an inner SELECT's set in the outer one could remove that, but it needs a way to tie sets to subtrees. Happy to file a follow-up if profiles show it's still significant after this.

@alamb alamb Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for a follow up -- what I was thinking was that if there are other calls like this that can be cached it would be valuable to consolidate / make a pattern that could be easily followed rather than just a bunch of one offs

Thanks for the response


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),
}
Expand All @@ -1129,6 +1151,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
&self,
sql: SelectItem,
plan: &LogicalPlan,
using_columns: &mut LazyUsingColumns,
empty_from: bool,
planner_context: &mut PlannerContext,
) -> Result<SelectExpr> {
Expand All @@ -1138,7 +1161,7 @@ impl<S: ContextProvider> 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))
Expand All @@ -1149,7 +1172,7 @@ impl<S: ContextProvider> 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.
Expand All @@ -1170,6 +1193,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
}
let planned_options = self.plan_wildcard_options(
plan,
using_columns,
empty_from,
planner_context,
options,
Expand All @@ -1192,6 +1216,7 @@ impl<S: ContextProvider> 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,
Expand Down Expand Up @@ -1229,6 +1254,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
fn plan_wildcard_options(
&self,
plan: &LogicalPlan,
using_columns: &mut LazyUsingColumns,
empty_from: bool,
planner_context: &mut PlannerContext,
options: WildcardAdditionalOptions,
Expand All @@ -1248,6 +1274,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
self.sql_select_to_rex(
SelectItem::UnnamedExpr(item.expr.clone()),
plan,
using_columns,
empty_from,
planner_context,
)
Expand Down
20 changes: 20 additions & 0 deletions datafusion/sqllogictest/test_files/wildcard.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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;