Skip to content

Apache DataFusion SQL Query Engine

fix: keep a projection's field names when decorrelating a predicate s… (#26657)

Singleton123747e7145c
diff --git a/datafusion/optimizer/src/rewrite_set_comparison.rs b/datafusion/optimizer/src/rewrite_set_comparison.rs
index aca0f6693b..62c4b7408b 100644
--- a/datafusion/optimizer/src/rewrite_set_comparison.rs
+++ b/datafusion/optimizer/src/rewrite_set_comparison.rs
@@ -6,7 +6,7 @@
//! `> ALL`) into boolean expressions built from `EXISTS` subqueries
//! that capture SQL three-valued logic.
-use crate::utils::merge_into_schema;
+use crate::utils::{NamePreserver, merge_into_schema};
use crate::{OptimizerConfig, OptimizerRule};
use datafusion_common::tree_node::{Transformed, TreeNode};
use datafusion_common::{Column, DFSchema, ExprSchema, Result, ScalarValue, plan_err};
@@ -36,8 +36,19 @@ impl RewriteSetComparison {
if let Some(merge_schema) = merge_into_schema(&plan)? {
schema = merge_schema;
}
+ // This rule replaces `x > ALL (…)` with a `CASE WHEN EXISTS …`, whose display
+ // name is not the name of the field it produces. Rewriting an expression
+ // without preserving its name therefore changes the output schema, and the
+ // optimizer's "the schema is invariant under optimization" check fails with an
+ // internal error rather than returning the answer. `simplify_exprs` and
+ // `type_coercion` preserve names around `map_expressions` for the same reason;
+ // doing it here also stops this rule leaving mismatched names in `Aggregate`
+ // and `Window` nodes, which a projection-only fix could not reach.
+ let name_preserver = NamePreserver::new(&plan);
plan.map_expressions(|expr| {
+ let original_name = name_preserver.save(&expr);
expr.transform_up(|expr| rewrite_set_comparison(expr, &schema))
+ .map(|t| t.update_data(|expr| original_name.restore(expr)))
})
}
}
diff --git a/datafusion/sqllogictest/test_files/subquery.slt b/datafusion/sqllogictest/test_files/subquery.slt
index 2380ba6366..999f415c3c 100644
--- a/datafusion/sqllogictest/test_files/subquery.slt
+++ b/datafusion/sqllogictest/test_files/subquery.slt
@@ -4094,6 +4094,45 @@ DROP TABLE nested_agg_outer;
statement ok
DROP TABLE nested_agg_inner;
+# A set comparison in a projection without an alias must not change the projection's
+# output field name. `RewriteSetComparison` replaces `x > ALL (…)` with a `CASE` whose
+# display name differs from the field name, and rebuilding the projection from the
+# expression names failed the optimizer's "schema is invariant" check with an internal
+# error instead of returning the answer.
+query B
+select 2 > all (select v from (values (1), (0)) t(v));
+----
+true
+
+query BI
+select 2 > all (select v from (values (1), (0)) t(v)), 9 as k;
+----
+true 9
+
+# A control rather than a regression test: an aliased set comparison already keeps its
+# name on `main`, so this one does not change with the fix. It pins the alias arm of the
+# name comparison against a rule that starts renaming aliased projections.
+query B
+select 2 > all (select v from (values (1), (0)) t(v)) as aliased;
+----
+true
+
+# A qualified column next to the set comparison is the other side of the same check:
+# `Expr::schema_name` keeps the qualifier (`t_qual.k`) while the field is named after the
+# bare column, so losing the qualifier breaks the schema just as adding a stale name does.
+# Fails on `main` with the same internal error.
+statement ok
+create table t_qual(k int) as values (1), (2);
+
+query IB
+select t_qual.k, t_qual.k > all (select v from (values (1), (0)) s(v)) from t_qual order by t_qual.k;
+----
+1 false
+2 true
+
+statement ok
+drop table t_qual;
+
# Regression tests for #25258 - Unaliased set comparison subqueries return wrong results
statement ok