From 7d09b99279b5314675ef8e71ac7ffacfbff0e7da Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Thu, 17 Sep 2026 21:00:13 -0500 Subject: [PATCH 01/12] refactor(optimizer): add `ProjectionInliner`, one policy for inlining a projection column Several rules replace a column reference with the expression that the projection below defines for it. Each rule had its own guard against the duplication that this can cause. Add a crate-private `ProjectionInliner`. It holds the decision in one place: it classifies each definition of a projection by `DuplicationCost` (free, cheap, expensive, forbidden), counts the evaluations that the plan has after the rewrite, and reports the definitions that must stay where they are. Route the `OptimizeProjections` merge guard through it. The classification keeps the `should_push_to_leaves` split of that guard, so the plans do not change. See https://github.com/apache/datafusion/issues/8296 Co-Authored-By: Claude Fable 5.1 --- datafusion/optimizer/src/lib.rs | 1 + .../optimizer/src/optimize_projections/mod.rs | 26 +- .../optimizer/src/projection_inliner.rs | 392 ++++++++++++++++++ 3 files changed, 404 insertions(+), 15 deletions(-) create mode 100644 datafusion/optimizer/src/projection_inliner.rs diff --git a/datafusion/optimizer/src/lib.rs b/datafusion/optimizer/src/lib.rs index e31e4bb93bdcc..615e175324d09 100644 --- a/datafusion/optimizer/src/lib.rs +++ b/datafusion/optimizer/src/lib.rs @@ -50,6 +50,7 @@ pub mod filter_null_join_keys; pub mod optimize_projections; pub mod optimize_unions; pub mod optimizer; +mod projection_inliner; pub mod propagate_empty_relation; pub mod push_down_filter; pub mod push_down_limit; diff --git a/datafusion/optimizer/src/optimize_projections/mod.rs b/datafusion/optimizer/src/optimize_projections/mod.rs index fd59587872026..78f373654a42b 100644 --- a/datafusion/optimizer/src/optimize_projections/mod.rs +++ b/datafusion/optimizer/src/optimize_projections/mod.rs @@ -23,7 +23,7 @@ use datafusion_expr::logical_plan::MaterializedCtePlan; use std::sync::Arc; use datafusion_common::{ - Column, DFSchema, HashMap, JoinType, Result, assert_eq_or_internal_err, + DFSchema, JoinType, Result, assert_eq_or_internal_err, get_required_group_by_exprs_indices, internal_datafusion_err, internal_err, }; use datafusion_expr::expr::Alias; @@ -34,6 +34,7 @@ use datafusion_expr::{ }; use crate::optimize_projections::required_indices::RequiredIndices; +use crate::projection_inliner::{DuplicationCost, ProjectionInliner}; use crate::utils::NamePreserver; use datafusion_common::tree_node::{Transformed, TreeNode, TreeNodeContainer}; @@ -585,21 +586,16 @@ fn merge_consecutive_projections_one_level( return Projection::try_new_with_schema(expr, input, schema).map(Transformed::no); }; - // Count usages (referrals) of each projection expression in its input fields: - let mut column_referral_map = HashMap::<&Column, usize>::new(); - for expr in &expr { - expr.add_column_ref_counts(&mut column_referral_map); - } - - // If an expression is non-trivial (KeepInPlace) and appears more than once, do not merge - // them as consecutive projections will benefit from a compute-once approach. + // The merged projection replaces `prev_projection`, so each definition is + // evaluated one time for each reference. If a non-trivial definition is + // referenced more than one time, do not merge: consecutive projections + // compute it one time only. // For details, see: https://github.com/apache/datafusion/issues/8296 - if column_referral_map.into_iter().any(|(col, usage)| { - usage > 1 - && !prev_projection.expr[prev_projection.schema.index_of_column(col).unwrap()] - .placement() - .should_push_to_leaves() - }) { + let no_other_consumers: [&Expr; 0] = []; + if !ProjectionInliner::new(prev_projection) + .pinned(&expr, no_other_consumers, DuplicationCost::Free) + .is_empty() + { // no change return Projection::try_new_with_schema(expr, input, schema).map(Transformed::no); } diff --git a/datafusion/optimizer/src/projection_inliner.rs b/datafusion/optimizer/src/projection_inliner.rs new file mode 100644 index 0000000000000..1ecce7a89d184 --- /dev/null +++ b/datafusion/optimizer/src/projection_inliner.rs @@ -0,0 +1,392 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! A single, cost-aware check for rewriting expressions through a projection. +//! +//! Many rules move an expression that is written over the output of a +//! projection below that projection, or merge it into the projection. To do +//! this, they replace each column reference with the expression that defines +//! the column. This is called *inlining*. +//! +//! Inlining can make the plan evaluate a definition more times than before: +//! +//! - The consumer can reference the column more than one time. +//! - The projection can stay in the plan and still compute the definition. +//! +//! When the definition is volatile, this changes the query result. When the +//! definition is expensive, this makes the query slower. A typical example is a +//! column that [`CommonSubexprEliminate`] created on purpose, so that an +//! expensive expression is computed one time only. +//! +//! [`ProjectionInliner`] is the one place that makes this decision. +//! +//! [`CommonSubexprEliminate`]: crate::common_subexpr_eliminate::CommonSubexprEliminate + +use std::collections::{HashMap, HashSet}; + +use datafusion_common::Column; +use datafusion_common::tree_node::{TreeNode, TreeNodeRecursion}; +use datafusion_expr::{Expr, ExpressionPlacement, Projection}; + +/// The cost of one more evaluation of an expression. +/// +/// The variants are in increasing order of cost. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub(crate) enum DuplicationCost { + /// Column references and expressions that report + /// [`ExpressionPlacement::MoveTowardsLeafNodes`] (for example struct field + /// access). These are free, or almost free, to evaluate again. + Free, + /// Built-in operators over free or cheap operands: arithmetic, + /// comparisons, `CAST`, `CASE`, `IS NULL`, and similar. They are + /// vectorized kernels, so an extra evaluation costs little. + /// + /// A bare literal is also in this class. This keeps the merge guard of + /// `OptimizeProjections` (issue #8296) unchanged. + Cheap, + /// Contains a scalar function that reports + /// [`ExpressionPlacement::KeepInPlace`] for column arguments. This is the + /// default for a user-defined function, so the optimizer must assume that + /// it can be arbitrarily expensive. + Expensive, + /// Must never be evaluated more than one time: the expression is volatile + /// (for example `random()`) or contains a subquery. + Forbidden, +} + +impl DuplicationCost { + /// Returns the cost of one more evaluation of `expr`. + fn of(expr: &Expr) -> Self { + let expr = unalias(expr); + if expr.placement().should_push_to_leaves() { + return Self::Free; + } + + let mut cost = Self::Cheap; + expr.apply(|e| { + let node_cost = match e { + Expr::ScalarSubquery(_) + | Expr::Exists(_) + | Expr::InSubquery(_) + | Expr::SetComparison(_) => Self::Forbidden, + Expr::ScalarFunction(_) if e.is_volatile_node() => Self::Forbidden, + Expr::ScalarFunction(func) => { + // Ask the function for its own placement, as if its + // arguments were plain columns and literals. This isolates + // the cost of the function from the cost of its arguments + // (the walk visits the arguments separately). For example + // `get_field(CAST(s AS ...), 'f')` is a cheap field access + // over a cheap cast. + let arg_placements: Vec<_> = func + .args + .iter() + .map(|arg| match arg.placement() { + ExpressionPlacement::Literal => ExpressionPlacement::Literal, + _ => ExpressionPlacement::Column, + }) + .collect(); + match func.func.placement(&arg_placements) { + ExpressionPlacement::KeepInPlace => Self::Expensive, + _ => Self::Cheap, + } + } + _ => Self::Cheap, + }; + cost = cost.max(node_cost); + Ok(if cost == Self::Forbidden { + TreeNodeRecursion::Stop + } else { + TreeNodeRecursion::Continue + }) + }) + .expect("traversal is infallible"); + cost + } +} + +/// Definitions that [`ProjectionInliner::pinned`] refuses to inline. +#[derive(Debug, Default)] +pub(crate) struct PinnedDefinitions(HashSet); + +impl PinnedDefinitions { + pub(crate) fn is_empty(&self) -> bool { + self.0.is_empty() + } +} + +/// Decides if the column references of expressions over the output of a +/// projection can be replaced with their definitions, without evaluating a +/// definition too many times. +/// +/// The rule is: after the rewrite, a definition that is evaluated more than +/// one time must have a [`DuplicationCost`] of at most the `max_cost` that the +/// caller gives. See [`ProjectionInliner::pinned`]. +#[derive(Debug)] +pub(crate) struct ProjectionInliner<'a> { + /// (definition without top-level alias, cost), in output column order + definitions: Vec<(&'a Expr, DuplicationCost)>, + /// Output column -> index into `definitions` + by_column: HashMap, + /// Output column name -> index into `definitions`, for unqualified + /// references. `None` if the name is ambiguous. + by_name: HashMap<&'a str, Option>, +} + +impl<'a> ProjectionInliner<'a> { + /// Creates an inliner for the output columns of `projection`. + pub(crate) fn new(projection: &'a Projection) -> Self { + let mut inliner = Self { + definitions: Vec::with_capacity(projection.expr.len()), + by_column: HashMap::with_capacity(projection.expr.len()), + by_name: HashMap::with_capacity(projection.expr.len()), + }; + let columns = projection.schema.iter(); + for (idx, ((qualifier, field), expr)) in + columns.zip(projection.expr.iter()).enumerate() + { + let expr = unalias(expr); + inliner.definitions.push((expr, DuplicationCost::of(expr))); + inliner + .by_column + .insert(Column::new(qualifier.cloned(), field.name()), idx); + inliner + .by_name + .entry(field.name().as_str()) + .and_modify(|found| *found = None) + .or_insert(Some(idx)); + } + inliner + } + + /// Resolves `column` like [`DFSchema::index_of_column`] does: an + /// unqualified reference matches a qualified output column with the same + /// name, if there is only one. + /// + /// [`DFSchema::index_of_column`]: datafusion_common::DFSchema::index_of_column + fn index_of(&self, column: &Column) -> Option { + match self.by_column.get(column) { + Some(idx) => Some(*idx), + None if column.relation.is_none() => { + self.by_name.get(column.name.as_str()).copied().flatten() + } + None => None, + } + } + + /// Returns the definitions that must not be inlined into `inlined`. + /// + /// - `inlined`: the expressions whose column references will be replaced + /// with their definitions. A definition is evaluated one time for each + /// reference. + /// - `others`: the other known consumers of the projection. They keep + /// their column references, so a definition that they reference is + /// evaluated one more time, in the projection. + /// + /// A definition that nothing else references is assumed to be removed + /// from the projection (for example by `OptimizeProjections`). + /// + /// A definition that `inlined` references is pinned if it is then + /// evaluated more than one time and its [`DuplicationCost`] is more than + /// `max_cost`. + pub(crate) fn pinned<'i, 'o>( + &self, + inlined: impl IntoIterator, + others: impl IntoIterator, + max_cost: DuplicationCost, + ) -> PinnedDefinitions { + debug_assert!( + max_cost < DuplicationCost::Forbidden, + "a forbidden definition must never be duplicated" + ); + let inlined_references = self.count_references(inlined); + if inlined_references.is_empty() { + return PinnedDefinitions::default(); + } + let other_references = self.count_references(others); + let pinned = inlined_references + .into_iter() + .filter(|(idx, count)| { + let evaluations = count + usize::from(other_references.contains_key(idx)); + evaluations > 1 && self.definitions[*idx].1 > max_cost + }) + .map(|(idx, _)| idx) + .collect(); + PinnedDefinitions(pinned) + } + + /// Counts the references to each definition in `exprs`. + fn count_references<'e>( + &self, + exprs: impl IntoIterator, + ) -> HashMap { + let mut references: HashMap = HashMap::new(); + for expr in exprs { + expr.apply(|e| { + if let Expr::Column(col) = e + && let Some(idx) = self.index_of(col) + { + *references.entry(idx).or_default() += 1; + } + Ok(TreeNodeRecursion::Continue) + }) + .expect("traversal is infallible"); + } + references + } +} + +/// Removes all top-level aliases. +fn unalias(expr: &Expr) -> &Expr { + match expr { + Expr::Alias(alias) => unalias(&alias.expr), + _ => expr, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::test::test_table_scan; + use crate::test::udfs::{PlacementTestUDF, get_field_like}; + use arrow::datatypes::DataType; + use datafusion_expr::{ + LogicalPlan, LogicalPlanBuilder, ScalarUDF, Volatility, cast, col, lit, + }; + + fn udf(placement: ExpressionPlacement, volatility: Volatility) -> ScalarUDF { + ScalarUDF::new_from_impl( + PlacementTestUDF::new() + .with_placement(placement) + .with_volatility(volatility), + ) + } + + fn expensive(arg: Expr) -> Expr { + udf(ExpressionPlacement::KeepInPlace, Volatility::Immutable).call(vec![arg]) + } + + fn volatile(arg: Expr) -> Expr { + udf(ExpressionPlacement::KeepInPlace, Volatility::Volatile).call(vec![arg]) + } + + fn projection(exprs: Vec) -> Projection { + let plan = LogicalPlanBuilder::from(test_table_scan().unwrap()) + .project(exprs) + .unwrap() + .build() + .unwrap(); + let LogicalPlan::Projection(projection) = plan else { + unreachable!() + }; + projection + } + + #[test] + fn duplication_cost_classification() { + use DuplicationCost::*; + assert_eq!(DuplicationCost::of(&col("a")), Free); + assert_eq!(DuplicationCost::of(&get_field_like(col("a"), "x")), Free); + assert_eq!(DuplicationCost::of(&lit(1)), Cheap); + assert_eq!(DuplicationCost::of(&(col("a") * lit(2))), Cheap); + assert_eq!( + DuplicationCost::of(&cast(col("a"), DataType::Int64).alias("x")), + Cheap + ); + // A field access over a cheap operand is still cheap. + assert_eq!( + DuplicationCost::of(&get_field_like(col("a") + lit(1), "x")), + Cheap + ); + assert_eq!(DuplicationCost::of(&expensive(col("a"))), Expensive); + assert_eq!( + DuplicationCost::of(&get_field_like(expensive(col("a")), "x")), + Expensive + ); + assert_eq!( + DuplicationCost::of(&(expensive(col("a")) + lit(1))), + Expensive + ); + assert_eq!( + DuplicationCost::of(&(volatile(col("a")) + lit(1))), + Forbidden + ); + } + + #[test] + fn pinned_counts_evaluations() { + use DuplicationCost::*; + let p = projection(vec![ + col("a"), + (col("b") + lit(1)).alias("cheap"), + expensive(col("c")).alias("exp"), + ]); + let inliner = ProjectionInliner::new(&p); + let no_others: [&Expr; 0] = []; + + // One reference of an expensive definition: allowed. + let one = get_field_like(col("exp"), "x"); + assert!(inliner.pinned([&one], no_others, Free).is_empty()); + // The same reference when another consumer keeps the column: refused, + // unless the caller accepts expensive duplicates. + assert!(!inliner.pinned([&one], [&col("exp")], Cheap).is_empty()); + assert!(inliner.pinned([&one], [&col("exp")], Expensive).is_empty()); + // Two references in one expression: refused unless allowed. + let two = col("exp") + .is_not_null() + .and(get_field_like(col("exp"), "x")); + assert!(!inliner.pinned([&two], no_others, Cheap).is_empty()); + assert!(inliner.pinned([&two], no_others, Expensive).is_empty()); + // References are counted across all inlined expressions. + assert!( + !inliner + .pinned([&col("exp"), &col("exp")], no_others, Cheap) + .is_empty() + ); + // Cheap definitions follow `max_cost`. + let cheap = col("cheap").gt(lit(5)); + assert!(inliner.pinned([&cheap], [&col("cheap")], Cheap).is_empty()); + assert!(!inliner.pinned([&cheap], [&col("cheap")], Free).is_empty()); + // Free definitions are always allowed. + assert!( + inliner + .pinned([&col("a"), &col("a")], [&col("a")], Free) + .is_empty() + ); + // An unqualified reference resolves to a qualified output column. + assert_eq!(inliner.index_of(&Column::from_name("a")), Some(0)); + assert_eq!( + inliner.index_of(&Column::from_qualified_name("test.a")), + Some(0) + ); + assert_eq!(inliner.index_of(&Column::from_qualified_name("t.a")), None); + } + + #[test] + fn forbidden_definitions_are_pinned_when_duplicated() { + let p = projection(vec![volatile(col("a")).alias("r")]); + let inliner = ProjectionInliner::new(&p); + let no_others: [&Expr; 0] = []; + let max = DuplicationCost::Expensive; + assert!(inliner.pinned([&col("r")], no_others, max).is_empty()); + assert!( + !inliner + .pinned([&(col("r") + col("r"))], no_others, max) + .is_empty() + ); + assert!(!inliner.pinned([&col("r")], [&col("r")], max).is_empty()); + } +} From e6eba501c807d2ba2a8e1bf673d3622324325f53 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Thu, 17 Sep 2026 21:00:25 -0500 Subject: [PATCH 02/12] fix(optimizer): do not push a filter that duplicates an expensive projection column `PushDownFilter` inlined every non-volatile definition of a projection into the pushed predicates. When the predicates reference an expensive definition more than one time, for example a column that `CommonSubexprEliminate` made for a struct-returning UDF, the push-down evaluates that expression more than one time. `CommonSubexprEliminate` then makes the projection again below the filter, so the two rules never converge. Route the rewrite through `ProjectionInliner` and delete the local `contain` helper and the placement partition. A predicate stays above the projection when it references a volatile definition, a `MoveTowardsLeafNodes` definition (as before), or an expensive definition that the predicates reference more than one time. A predicate with one reference is still pushed. Part of https://github.com/apache/datafusion/issues/25329 Co-Authored-By: Claude Fable 5.1 --- .../optimizer/src/projection_inliner.rs | 119 +++++++++++++- datafusion/optimizer/src/push_down_filter.rs | 146 ++++++++++++++---- 2 files changed, 226 insertions(+), 39 deletions(-) diff --git a/datafusion/optimizer/src/projection_inliner.rs b/datafusion/optimizer/src/projection_inliner.rs index 1ecce7a89d184..252c11d5fb9f9 100644 --- a/datafusion/optimizer/src/projection_inliner.rs +++ b/datafusion/optimizer/src/projection_inliner.rs @@ -32,14 +32,17 @@ //! column that [`CommonSubexprEliminate`] created on purpose, so that an //! expensive expression is computed one time only. //! -//! [`ProjectionInliner`] is the one place that makes this decision. +//! [`ProjectionInliner`] is the one place that makes this decision. Rules call +//! it instead of doing the substitution themselves. //! //! [`CommonSubexprEliminate`]: crate::common_subexpr_eliminate::CommonSubexprEliminate use std::collections::{HashMap, HashSet}; -use datafusion_common::Column; -use datafusion_common::tree_node::{TreeNode, TreeNodeRecursion}; +use datafusion_common::tree_node::{ + Transformed, TransformedResult, TreeNode, TreeNodeRecursion, +}; +use datafusion_common::{Column, Result}; use datafusion_expr::{Expr, ExpressionPlacement, Projection}; /// The cost of one more evaluation of an expression. @@ -128,9 +131,9 @@ impl PinnedDefinitions { } } -/// Decides if the column references of expressions over the output of a -/// projection can be replaced with their definitions, without evaluating a -/// definition too many times. +/// Rewrites expressions over the output of a projection into expressions over +/// its input, and decides if that is possible without evaluating a definition +/// too many times. /// /// The rule is: after the rewrite, a definition that is evaluated more than /// one time must have a [`DuplicationCost`] of at most the `max_cost` that the @@ -207,6 +210,32 @@ impl<'a> ProjectionInliner<'a> { inlined: impl IntoIterator, others: impl IntoIterator, max_cost: DuplicationCost, + ) -> PinnedDefinitions { + self.pinned_impl(inlined, others, max_cost, false) + } + + /// Returns [`Self::pinned`], and also pins every + /// [`DuplicationCost::Forbidden`] definition that `inlined` references. + /// + /// Use this when the projection can stay in the plan for consumers that + /// the caller can not see. A copy of a forbidden definition would then + /// be evaluated independently of the original, and change the query + /// result. + pub(crate) fn pinned_for_known_consumers<'i, 'o>( + &self, + inlined: impl IntoIterator, + others: impl IntoIterator, + max_cost: DuplicationCost, + ) -> PinnedDefinitions { + self.pinned_impl(inlined, others, max_cost, true) + } + + fn pinned_impl<'i, 'o>( + &self, + inlined: impl IntoIterator, + others: impl IntoIterator, + max_cost: DuplicationCost, + pin_forbidden: bool, ) -> PinnedDefinitions { debug_assert!( max_cost < DuplicationCost::Forbidden, @@ -220,14 +249,51 @@ impl<'a> ProjectionInliner<'a> { let pinned = inlined_references .into_iter() .filter(|(idx, count)| { + let cost = self.definitions[*idx].1; let evaluations = count + usize::from(other_references.contains_key(idx)); - evaluations > 1 && self.definitions[*idx].1 > max_cost + (pin_forbidden && cost == DuplicationCost::Forbidden) + || (evaluations > 1 && cost > max_cost) }) .map(|(idx, _)| idx) .collect(); PinnedDefinitions(pinned) } + /// Returns `true` if `expr` references a definition in `pinned`. + pub(crate) fn references_pinned( + &self, + expr: &Expr, + pinned: &PinnedDefinitions, + ) -> bool { + !pinned.is_empty() + && expr + .exists(|e| { + Ok(matches!(e, Expr::Column(col) + if self.index_of(col).is_some_and(|idx| pinned.0.contains(&idx)))) + }) + .expect("exists closure is infallible") + } + + /// Returns the definition of `column`, without its top-level alias. + pub(crate) fn definition(&self, column: &Column) -> Option<&'a Expr> { + self.index_of(column).map(|idx| self.definitions[idx].0) + } + + /// Replaces each column reference in `expr` with its definition, without a + /// cost check. Callers must check [`Self::pinned`] first. + pub(crate) fn substitute(&self, expr: Expr) -> Result { + expr.transform_up(|e| { + if let Expr::Column(col) = &e + && let Some(definition) = self.definition(col) + { + Ok(Transformed::yes(definition.clone())) + } else { + Ok(Transformed::no(e)) + } + }) + .data() + } + /// Counts the references to each definition in `exprs`. fn count_references<'e>( &self, @@ -388,5 +454,44 @@ mod tests { .is_empty() ); assert!(!inliner.pinned([&col("r")], [&col("r")], max).is_empty()); + // Without all the consumers, a single reference is pinned as well. + assert!( + !inliner + .pinned_for_known_consumers([&col("r")], no_others, max) + .is_empty() + ); + } + + #[test] + fn substitute_and_references_pinned() -> Result<()> { + let p = projection(vec![ + col("a"), + expensive(col("c")).alias("exp"), + (col("b") + lit(1)).alias("cheap"), + ]); + let inliner = ProjectionInliner::new(&p); + let no_others: [&Expr; 0] = []; + + let twice = col("exp") + .is_not_null() + .and(get_field_like(col("exp"), "x")); + let other = col("cheap").gt(col("a")); + let pinned = inliner.pinned_for_known_consumers( + [&twice, &other], + no_others, + DuplicationCost::Cheap, + ); + assert!(inliner.references_pinned(&twice, &pinned)); + assert!(!inliner.references_pinned(&other, &pinned)); + + assert_eq!( + inliner.definition(&Column::from_name("exp")), + Some(&expensive(col("test.c"))) + ); + assert_eq!( + inliner.substitute(other)?, + (col("test.b") + lit(1)).gt(col("test.a")) + ); + Ok(()) } } diff --git a/datafusion/optimizer/src/push_down_filter.rs b/datafusion/optimizer/src/push_down_filter.rs index ab3ee7a68bbe2..84bcd0829162a 100644 --- a/datafusion/optimizer/src/push_down_filter.rs +++ b/datafusion/optimizer/src/push_down_filter.rs @@ -56,6 +56,7 @@ use datafusion_expr::{ use crate::extract_leaf_expressions::is_pure_extraction_projection; use crate::optimizer::ApplyOrder; +use crate::projection_inliner::{DuplicationCost, ProjectionInliner}; use crate::simplify_expressions::{reorder_predicates, simplify_predicates}; use crate::utils::{ ColumnReference, has_all_column_refs, is_restrict_null_predicate, schema_columns, @@ -1544,33 +1545,36 @@ fn rewrite_projection( )); } - // Partition projection expressions into non-pushable vs pushable. - // Non-pushable expressions are volatile (must not be duplicated) or - // MoveTowardsLeafNodes (cheap expressions like get_field where re-inlining - // into a filter causes optimizer instability — ExtractLeafExpressions will - // undo the push-down, creating an infinite loop that runs until the - // iteration limit is hit). - // - // Keyed by the output column rather than its flat name, for the reason - // given on `replace_cols_by_column`. `EliminateGroupByConstant` can leave a - // projection that outputs both `t.a` and `CAST(t.a AS ...)` under that one - // name. - let (non_pushable_map, pushable_map): (HashMap<_, _>, HashMap<_, _>) = projection - .schema - .iter() - .zip(projection.expr.iter()) - .map(|((qualifier, field), expr)| { - (Column::new(qualifier.cloned(), field.name()), unalias(expr)) - }) - .partition(|(_, value)| { - value.is_volatile() - || value.placement() == ExpressionPlacement::MoveTowardsLeafNodes - }); - + let inliner = ProjectionInliner::new(&projection); + + // A predicate is kept above the projection when it references: + // - a volatile definition. The projection still computes it, so a second + // evaluation below would see a different value. + // - an expensive definition that the predicates reference more than one + // time. Pushing would evaluate it more than one time, and + // `CommonSubexprEliminate` would create a projection for it again below + // the filter, so the two rules would never converge (issue #25329). A + // single reference is pushed: if the columns above still use the + // definition, that is the usual cost of a filter push-down. + // - a `MoveTowardsLeafNodes` definition (a cheap expression like + // `get_field`). Re-inlining such an expression into a filter causes + // optimizer instability: `ExtractLeafExpressions` undoes the push-down, + // which creates a loop that runs until the iteration limit is hit. + let no_other_consumers: [&Expr; 0] = []; + let pinned = inliner.pinned_for_known_consumers( + &predicates, + no_other_consumers, + DuplicationCost::Cheap, + ); let mut push_predicates = vec![]; let mut keep_predicates = vec![]; for expr in predicates { - if contains_column(&expr, &non_pushable_map) { + let references_leaf_expr = expr.column_refs().into_iter().any(|col| { + inliner.definition(col).is_some_and(|definition| { + definition.placement() == ExpressionPlacement::MoveTowardsLeafNodes + }) + }); + if references_leaf_expr || inliner.references_pinned(&expr, &pinned) { keep_predicates.push(expr); } else { push_predicates.push(expr); @@ -1581,7 +1585,7 @@ fn rewrite_projection( // re-write all filters based on this projection // E.g. in `Filter: b\n Projection: a > 1 as b`, we can swap them, but the filter must be "a > 1" projection.input = Arc::new(LogicalPlan::Filter(Filter::new( - replace_cols_by_column(expr, &pushable_map)?, + inliner.substitute(expr)?, projection.input, ))); @@ -1672,12 +1676,6 @@ fn unalias(expr: &Expr) -> &Expr { } } -/// check whether the expression uses the columns in `check_map`. -fn contains_column(e: &Expr, check_map: &HashMap) -> bool { - e.exists(|expr| Ok(matches!(expr, Expr::Column(c) if check_map.contains_key(c)))) - .unwrap() -} - fn with_filters(predicates: Vec, plan: LogicalPlan) -> LogicalPlan { if let Some(predicate) = conjunction(predicates) { LogicalPlan::Filter(Filter::new(predicate, Arc::new(plan))) @@ -1767,7 +1765,7 @@ mod tests { use crate::assert_optimized_plan_eq_snapshot; use crate::optimizer::Optimizer; use crate::simplify_expressions::SimplifyExpressions; - use crate::test::udfs::leaf_udf_expr; + use crate::test::udfs::{PlacementTestUDF, get_field_like, leaf_udf_expr}; use crate::test::*; use datafusion_expr::test::function_stub::{count, sum}; use insta::assert_snapshot; @@ -4959,6 +4957,90 @@ mod tests { ) } + fn keep_in_place_udf(arg: Expr) -> Expr { + ScalarUDF::new_from_impl( + PlacementTestUDF::new().with_placement(ExpressionPlacement::KeepInPlace), + ) + .call(vec![arg]) + } + + /// Issue #25329: `CommonSubexprEliminate` computes `c1` one time for + /// predicates that reference it twice. Pushing the predicates would compute + /// it twice, and CSE would then create the projection again below the + /// filter, so the two rules never converge. + #[test] + fn filter_with_repeated_expensive_reference_not_pushed_through_projection() + -> Result<()> { + let plan = LogicalPlanBuilder::from(test_table_scan()?) + .project(vec![keep_in_place_udf(col("a")).alias("c1"), col("b")])? + .filter( + col("c1") + .is_not_null() + .and(get_field_like(col("c1"), "x").is_null()) + .and(col("b").gt(lit(1))), + )? + .build()?; + + assert_optimized_plan_equal!( + plan, + @r#" + Filter: c1 IS NOT NULL AND get_field_like(c1, Utf8("x")) IS NULL + Projection: keep_in_place_udf(test.a) AS c1, test.b + TableScan: test, full_filters=[test.b > Int32(1)] + "# + ) + } + + /// A single reference to an expensive definition is pushed, like any + /// other computed column of a projection. + #[test] + fn filter_with_single_expensive_reference_pushed_through_projection() -> Result<()> { + let plan = LogicalPlanBuilder::from(test_table_scan()?) + .project(vec![keep_in_place_udf(col("a")).alias("c1"), col("b")])? + .filter(col("c1").is_not_null())? + .build()?; + + assert_optimized_plan_equal!( + plan, + @r" + Projection: keep_in_place_udf(test.a) AS c1, test.b + TableScan: test, full_filters=[keep_in_place_udf(test.a) IS NOT NULL] + " + ) + } + + /// The shape of draft https://github.com/apache/datafusion/pull/25388: + /// a filter over a projection that reads an expensive column of the + /// projection below it. The narrow policy pushes `c1 IS NOT NULL`, + /// because the predicates reference `c1` one time. `c2 IS NULL` stays + /// above, because `c2` is a `MoveTowardsLeafNodes` definition. + #[test] + fn filter_not_pushed_through_nested_computed_projection() -> Result<()> { + let inner = LogicalPlanBuilder::from(test_table_scan()?) + .project(vec![keep_in_place_udf(col("a")).alias("c1"), col("b")])? + .build()?; + let outer = LogicalPlanBuilder::from(inner) + .project(vec![ + get_field_like(col("c1"), "x").alias("c2"), + col("c1"), + col("b"), + ])? + .build()?; + let plan = LogicalPlanBuilder::from(outer) + .filter(col("c1").is_not_null().and(col("c2").is_null()))? + .build()?; + + assert_optimized_plan_equal!( + plan, + @r#" + Filter: c2 IS NULL + Projection: get_field_like(c1, Utf8("x")) AS c2, c1, test.b + Projection: keep_in_place_udf(test.a) AS c1, test.b + TableScan: test, full_filters=[keep_in_place_udf(test.a) IS NOT NULL] + "# + ) + } + #[test] fn filter_not_pushed_down_through_table_scan_with_fetch() -> Result<()> { let scan = test_table_scan()?; From 29513fb3a8310914d6d6658f656ae6a48e14189c Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Thu, 17 Sep 2026 21:00:37 -0500 Subject: [PATCH 03/12] fix(optimizer): do not re-inline a shared definition in leaf extraction `ExtractLeafExpressions` and `PushDownLeafProjections` merge an extracted field access into an input projection, and resolve its column references to the definitions of that projection. When `CommonSubexprEliminate` made the column for a struct-returning UDF, the merge puts a copy of the UDF call next to the original, so the plan evaluates it two times: Filter: CASE WHEN __common_expr_1 IS NOT NULL THEN __datafusion_extracted_2 IS NULL END Projection: arrow_field(t.a) AS __common_expr_1, t.a, get_field(arrow_field(t.a), 'nullable') AS __datafusion_extracted_2 Delete `would_duplicate_volatile`, `merge_would_duplicate_kept_expr` and `build_projection_replace_map`, and use `ProjectionInliner` at every merge site. The inliner counts the evaluations over the extracted expressions and over the columns that the nodes above reference directly, so it also sees the reference sites that `merge_would_duplicate_kept_expr` could not see: they are in a different node. Closes https://github.com/apache/datafusion/issues/25329 Co-Authored-By: Claude Fable 5.1 --- .../optimizer/src/extract_leaf_expressions.rs | 573 ++++++++++-------- 1 file changed, 315 insertions(+), 258 deletions(-) diff --git a/datafusion/optimizer/src/extract_leaf_expressions.rs b/datafusion/optimizer/src/extract_leaf_expressions.rs index fc54710c97777..5856310085a41 100644 --- a/datafusion/optimizer/src/extract_leaf_expressions.rs +++ b/datafusion/optimizer/src/extract_leaf_expressions.rs @@ -60,11 +60,12 @@ use std::sync::Arc; use datafusion_common::alias::AliasGenerator; use datafusion_common::tree_node::{Transformed, TreeNode, TreeNodeRecursion}; -use datafusion_common::{Column, DFSchema, Result, qualified_name}; +use datafusion_common::{Column, DFSchema, Result, internal_err, qualified_name}; use datafusion_expr::logical_plan::LogicalPlan; use datafusion_expr::{Expr, ExpressionPlacement, Projection}; use crate::optimizer::ApplyOrder; +use crate::projection_inliner::{DuplicationCost, PinnedDefinitions, ProjectionInliner}; use crate::push_down_filter::replace_cols_by_name; use crate::utils::{ColumnReference, has_all_column_refs, schema_columns}; use crate::{OptimizerConfig, OptimizerRule}; @@ -91,56 +92,41 @@ fn has_extractable_expr(exprs: &[Expr]) -> bool { }) } -/// Returns the flat names of `plan`'s output columns whose defining expression -/// is volatile (e.g. `random()`). +/// Returns the definitions of an input projection that must not be merged. /// -/// Only a [`LogicalPlan::Projection`] can define such a column: anywhere else -/// the value has already been materialized by the projection that produced it, -/// so referencing the column again does not re-evaluate anything. -fn volatile_output_columns(plan: &LogicalPlan) -> BTreeSet { - let LogicalPlan::Projection(projection) = plan else { - return BTreeSet::new(); - }; - projection - .schema - .iter() - .zip(projection.expr.iter()) - .filter(|(_, expr)| expr.is_volatile()) - .map(|((qualifier, field), _)| Column::from((qualifier, field)).flat_name()) - .collect() -} - -/// Returns `true` if building an extraction projection for `exprs` on top of -/// `input` would duplicate a volatile computation. -/// -/// When `input` is already a projection, [`build_extraction_projection_impl`] -/// *merges* into it: every column reference in an extracted expression is -/// replaced by that column's defining expression (see -/// [`build_projection_replace_map`]). Inlining a volatile definition makes the -/// merged projection evaluate it a second, independent time, so the extracted -/// value no longer matches the column it was derived from: +/// An extraction projection is *merged* into an input that is already a +/// projection. The merge keeps every expression of the input and resolves the +/// column references of the extracted expressions to their definitions (see +/// [`build_extraction_projection_impl`]). The inliner decides if that is +/// allowed: after the merge, a definition that is not free to copy must be +/// evaluated one time only. For example: /// /// ```text -/// Projection: s, get_field(s, 'a') AS field -/// Projection: named_struct('a', random()) AS s +/// Filter: __common_expr_1 IS NOT NULL AND get_field(__common_expr_1, 'a') = 1 +/// Projection: f(c) AS __common_expr_1, c /// ``` /// -/// would merge into a single projection computing `random()` twice, and -/// `field` would then differ from `s['a']` on every row. Callers skip the -/// extraction instead. -fn would_duplicate_volatile<'a>( - exprs: impl IntoIterator, - input: &LogicalPlan, -) -> bool { - let volatile = volatile_output_columns(input); - if volatile.is_empty() { - return false; - } - exprs.into_iter().any(|expr| { - expr.column_refs() - .iter() - .any(|col| volatile.contains(&col.flat_name())) - }) +/// `CommonSubexprEliminate` computes `f(c)` one time on purpose. Extracting +/// `get_field(__common_expr_1, 'a')` into the projection would make it +/// `get_field(f(c), 'a')`, while the filter still reads `__common_expr_1`, so +/// `f(c)` is computed twice. For a volatile `f` (for example `random()`) this +/// also changes the result. +/// +/// The count uses the consumers that the rule can see: the extracted +/// expressions and the columns that the nodes above reference directly. A +/// definition that nothing references after the merge is removed by +/// `OptimizeProjections`. A volatile definition is never merged, because a +/// consumer further up can still reference it. +/// +/// `inlined` are the expressions that the merge resolves, and +/// `direct_columns` the columns that the nodes above reference directly. +fn pinned_for_merge<'a>( + inliner: &ProjectionInliner<'_>, + inlined: impl IntoIterator, + direct_columns: &IndexSet, +) -> PinnedDefinitions { + let direct: Vec = direct_columns.iter().cloned().map(Expr::Column).collect(); + inliner.pinned_for_known_consumers(inlined, &direct, DuplicationCost::Free) } /// Extracts `MoveTowardsLeafNodes` sub-expressions from non-projection nodes @@ -286,15 +272,15 @@ fn extract_from_plan( return Ok(Transformed::no(plan)); } - // The extraction projection is merged into an input that is already a - // projection, which inlines the referenced columns' definitions. Skip the - // extraction when that would duplicate a volatile computation. - if inputs + // Clone the input projections, so that the extractors can check which + // expressions can be merged into them while `plan` is rewritten. + let input_projections: Vec> = inputs .iter() - .any(|input| would_duplicate_volatile(node_exprs.iter(), input)) - { - return Ok(Transformed::no(plan)); - } + .map(|input| match input { + LogicalPlan::Projection(projection) => Some(projection.clone()), + _ => None, + }) + .collect(); // Save original output schema before any transformation let original_schema = Arc::clone(plan.schema()); @@ -308,7 +294,11 @@ fn extract_from_plan( // Build per-input extractors let mut extractors: Vec = input_schemas .iter() - .map(|schema| LeafExpressionExtractor::new(schema.as_ref(), alias_generator)) + .zip(&input_projections) + .map(|(schema, projection)| { + LeafExpressionExtractor::new(schema.as_ref(), alias_generator) + .with_input_projection(projection.as_ref(), &node_exprs, &IndexSet::new()) + }) .collect(); // Build per-input column sets for routing expressions to the correct input @@ -412,12 +402,15 @@ fn routing_extract( match e.placement() { ExpressionPlacement::MoveTowardsLeafNodes => { - if let Some(idx) = find_owning_input(&e, input_column_sets) { - let col_ref = extractors[idx].add_extracted(e)?; - Ok(Transformed::yes(col_ref)) - } else { - // References columns from multiple inputs — cannot extract - Ok(Transformed::no(e)) + match find_owning_input(&e, input_column_sets) { + Some(idx) if extractors[idx].can_extract(&e) => { + let col_ref = extractors[idx].add_extracted(e)?; + Ok(Transformed::yes(col_ref)) + } + // References columns from multiple inputs, or would + // evaluate a definition of the input projection again — + // cannot extract + _ => Ok(Transformed::no(e)), } } ExpressionPlacement::Column => { @@ -430,6 +423,7 @@ fn routing_extract( && let Some(idx) = find_owning_input(&e, input_column_sets) { extractors[idx].columns_needed.insert(col.clone()); + extractors[idx].direct_columns.insert(col.clone()); } Ok(Transformed::no(e)) } @@ -448,8 +442,7 @@ fn routing_extract( /// Used for SubqueryAlias (alias-space -> input-space) and Union /// (union output-space -> per-branch input-space). fn remap_pairs_and_columns( - pairs: &[(Expr, String)], - columns: &IndexSet, + target: &ExtractionTarget, from_schema: &DFSchema, to_schema: &DFSchema, ) -> Result { @@ -460,7 +453,8 @@ fn remap_pairs_and_columns( Expr::Column(Column::new(to_q.cloned(), to_f.name())), ); } - let remapped_pairs: Vec<(Expr, String)> = pairs + let remapped_pairs: Vec<(Expr, String)> = target + .pairs .iter() .map(|(expr, alias)| { Ok(( @@ -469,21 +463,24 @@ fn remap_pairs_and_columns( )) }) .collect::>()?; - let remapped_columns: IndexSet = columns - .iter() - .filter_map(|col| { - let rewritten = - replace_cols_by_name(Expr::Column(col.clone()), &replace_map).ok()?; - if let Expr::Column(c) = rewritten { - Some(c) - } else { - Some(col.clone()) - } - }) - .collect(); + let remap_columns = |columns: &IndexSet| -> IndexSet { + columns + .iter() + .filter_map(|col| { + let rewritten = + replace_cols_by_name(Expr::Column(col.clone()), &replace_map).ok()?; + if let Expr::Column(c) = rewritten { + Some(c) + } else { + Some(col.clone()) + } + }) + .collect() + }; Ok(ExtractionTarget { pairs: remapped_pairs, - columns: remapped_columns, + columns: remap_columns(&target.columns), + direct_columns: remap_columns(&target.direct_columns), }) } @@ -498,22 +495,10 @@ struct ExtractionTarget { pairs: Vec<(Expr, String)>, /// Standalone column references needed by the parent node. columns: IndexSet, -} - -/// Build a replacement map from a projection: output_column_name -> underlying_expr. -/// -/// This is used to resolve column references through a renaming projection. -/// For example, if a projection has `user AS x`, this maps `x` -> `col("user")`. -fn build_projection_replace_map(projection: &Projection) -> HashMap { - projection - .schema - .iter() - .zip(projection.expr.iter()) - .map(|((qualifier, field), expr)| { - let key = Column::from((qualifier, field)).flat_name(); - (key, expr.clone().unalias()) - }) - .collect() + /// The columns that the nodes above reference directly, outside of the + /// extracted expressions. A merge keeps computing their definitions. See + /// [`pinned_for_merge`]. + direct_columns: IndexSet, } /// Build a recovery projection to restore the original output schema. @@ -613,6 +598,14 @@ struct LeafExpressionExtractor<'a> { input_schema: &'a DFSchema, /// Alias generator alias_generator: &'a Arc, + /// Columns that the parent node references directly, outside of the + /// extracted expressions. + direct_columns: IndexSet, + /// Set when the input is a projection that the extraction projection will + /// be merged into. See [`pinned_for_merge`]. + input_inliner: Option>, + /// The definitions of the input projection that must not be extracted. + pinned: PinnedDefinitions, } impl<'a> LeafExpressionExtractor<'a> { @@ -622,9 +615,38 @@ impl<'a> LeafExpressionExtractor<'a> { columns_needed: IndexSet::new(), input_schema, alias_generator, + direct_columns: IndexSet::new(), + input_inliner: None, + pinned: PinnedDefinitions::default(), } } + /// Sets the input projection that the extractions will be merged into. + /// `consumers` are all the expressions of the parent node that reference + /// the input, and `direct_columns` the input columns that the nodes above + /// the parent reference. See [`pinned_for_merge`]. + fn with_input_projection<'e>( + mut self, + projection: Option<&'a Projection>, + consumers: impl IntoIterator, + direct_columns: &IndexSet, + ) -> Self { + if let Some(projection) = projection { + let inliner = ProjectionInliner::new(projection); + self.pinned = pinned_for_merge(&inliner, consumers, direct_columns); + self.input_inliner = Some(inliner); + } + self + } + + /// Returns `true` if `expr` can be extracted into the input. See + /// [`pinned_for_merge`]. + fn can_extract(&self, expr: &Expr) -> bool { + self.input_inliner + .as_ref() + .is_none_or(|inliner| !inliner.references_pinned(expr, &self.pinned)) + } + /// Adds an expression to extracted set, returns column reference. fn add_extracted(&mut self, expr: Expr) -> Result { // Deduplication: reuse existing alias if same expression @@ -661,12 +683,19 @@ impl<'a> LeafExpressionExtractor<'a> { .iter() .map(|(e, a)| (e.clone(), a.clone())) .collect(); - let proj = build_extraction_projection_impl( + let Some(proj) = build_extraction_projection_impl( &pairs, &self.columns_needed, + &self.direct_columns, input, self.input_schema, - )?; + )? + else { + // `routing_extract` only extracts what `can_extract` accepts. + return internal_err!( + "extracted expressions can not be merged into the input projection" + ); + }; Ok(Some(LogicalPlan::Projection(proj))) } } @@ -696,13 +725,25 @@ fn resolve_against(schema: &DFSchema, col: &Column) -> Option { /// Deduplicates by resolved expression equality and adds pass-through /// columns as needed. Otherwise builds a fresh projection with extracted /// expressions + ALL input schema columns. +/// +/// Returns `None` if the merge would evaluate a definition of the existing +/// projection again, given that the nodes above also reference +/// `direct_columns` (see [`pinned_for_merge`]). fn build_extraction_projection_impl( extracted_exprs: &[(Expr, String)], columns_needed: &IndexSet, + direct_columns: &IndexSet, target: &Arc, target_schema: &DFSchema, -) -> Result { +) -> Result> { if let LogicalPlan::Projection(existing) = target.as_ref() { + // Resolves column references through the existing projection + let inliner = ProjectionInliner::new(existing); + let extracted = extracted_exprs.iter().map(|(e, _)| e); + if !pinned_for_merge(&inliner, extracted, direct_columns).is_empty() { + return Ok(None); + } + // Merge into existing projection let mut proj_exprs = existing.expr.clone(); @@ -720,12 +761,9 @@ fn build_extraction_projection_impl( }) .collect(); - // Resolve column references through the projection's rename mapping - let replace_map = build_projection_replace_map(existing); - // Add new extracted expressions, resolving column refs through the projection for (expr, alias) in extracted_exprs { - let resolved = replace_cols_by_name(expr.clone().alias(alias), &replace_map)?; + let resolved = inliner.substitute(expr.clone().alias(alias))?; let resolved_inner = if let Expr::Alias(a) = &resolved { a.expr.as_ref() } else { @@ -783,8 +821,10 @@ fn build_extraction_projection_impl( if output_names.contains(col.name.as_str()) { continue; } - let col_expr = Expr::Column(col.clone()); - let resolved = replace_cols_by_name(col_expr, &replace_map)?; + let resolved = inliner + .definition(col) + .cloned() + .unwrap_or_else(|| Expr::Column(col.clone())); if let Expr::Column(resolved_col) = &resolved && let Some(input_col) = resolve_against(input_schema, resolved_col) && !existing_cols.contains(&input_col) @@ -797,7 +837,7 @@ fn build_extraction_projection_impl( // the input or is ambiguous there: no single spelling is correct. } - Projection::try_new(proj_exprs, Arc::clone(&existing.input)) + Projection::try_new(proj_exprs, Arc::clone(&existing.input)).map(Some) } else { // Build new projection with extracted expressions + all input columns let mut proj_exprs = @@ -808,7 +848,7 @@ fn build_extraction_projection_impl( for (qualifier, field) in target_schema.iter() { proj_exprs.push(Expr::from((qualifier, field))); } - Projection::try_new(proj_exprs, Arc::clone(target)) + Projection::try_new(proj_exprs, Arc::clone(target)).map(Some) } } @@ -872,7 +912,7 @@ impl OptimizerRule for PushDownLeafProjections { return Ok(Transformed::no(plan)); } let alias_generator = config.alias_generator(); - match try_push_input(&plan, alias_generator)? { + match try_push_input(&plan, None, alias_generator)? { Some(new_plan) => Ok(Transformed::yes(new_plan)), None => Ok(Transformed::no(plan)), } @@ -883,14 +923,19 @@ impl OptimizerRule for PushDownLeafProjections { /// /// Returns `Some(new_subtree)` if the projection was pushed down or merged, /// `None` if there is nothing to push or the projection sits above a barrier. +/// +/// `known_direct_columns` is set when the rule built `input` itself: then +/// the pass-through columns of `input` only keep the schema, and the nodes +/// above reference only these columns directly (see [`pinned_for_merge`]). fn try_push_input( input: &LogicalPlan, + known_direct_columns: Option<&IndexSet>, alias_generator: &Arc, ) -> Result> { let LogicalPlan::Projection(proj) = input else { return Ok(None); }; - split_and_push_projection(proj, alias_generator) + split_and_push_projection(proj, known_direct_columns, alias_generator) } /// If this is a passthrough column. I.e, @@ -908,66 +953,6 @@ fn passthrough_column(expr: &Expr) -> Option<&Column> { } } -/// Returns `true` if merging the extracted `pairs` (and the pass-through -/// `columns_needed`) into `child` would inline a `KeepInPlace` definition into -/// more than one reference site — e.g. a struct-returning UDF `f(c)` behind both -/// `f(c)['a']` and `f(c)['b']`, or behind `f(c)['a']` and a bare `f(c)`. -/// -/// `CommonSubexprEliminate` hoists such an expression into a shared column so it -/// runs once; [`build_extraction_projection_impl`] then resolves that column -/// back to its definition and re-duplicates it. Mirrors the -/// `optimize_projections` merge guard (#8296): a compute-once expression used -/// more than once stays in place. -fn merge_would_duplicate_kept_expr( - pairs: &[(Expr, String)], - columns_needed: &IndexSet, - child: &Projection, -) -> bool { - // Columns whose `KeepInPlace` definition must stay put; inlining one would - // duplicate a compute-once expression. Plain columns, pushable exprs, and - // literals are all cheap to duplicate, so only `KeepInPlace` counts. - let kept_columns: std::collections::HashSet = child - .schema - .iter() - .zip(child.expr.iter()) - .filter_map(|((qualifier, field), expr)| { - if matches!(expr.placement(), ExpressionPlacement::KeepInPlace) { - Some(Column::from((qualifier, field)).flat_name()) - } else { - None - } - }) - .collect(); - if kept_columns.is_empty() { - return false; - } - - // Count every site that references a kept column. A pair that references the - // same kept column twice still inlines one copy, so count each pair at most - // once per column. - let mut usage: HashMap = HashMap::new(); - let mut seen: std::collections::HashSet = std::collections::HashSet::new(); - for (expr, _alias) in pairs { - seen.clear(); - for col in expr.column_refs() { - let name = col.flat_name(); - if kept_columns.contains(&name) && seen.insert(name.clone()) { - *usage.entry(name).or_insert(0) += 1; - } - } - } - // Pass-through / pre-existing-alias inputs are additional reference sites: - // a bare `f(c)` alongside `f(c)['a']` also pins the compute-once column, so - // counting only `pairs` under-counted and re-duplicated it (#23655). - for col in columns_needed { - let name = col.flat_name(); - if kept_columns.contains(&name) { - *usage.entry(name).or_insert(0) += 1; - } - } - usage.values().any(|&count| count > 1) -} - /// Splits a projection into extractable pieces, pushes them towards leaf /// nodes, and adds a recovery projection if needed. /// @@ -1001,6 +986,7 @@ fn merge_would_duplicate_kept_expr( /// ``` fn split_and_push_projection( proj: &Projection, + known_direct_columns: Option<&IndexSet>, alias_generator: &Arc, ) -> Result> { // Fast pre-check: skip if there are no pre-existing extracted aliases @@ -1030,10 +1016,23 @@ fn split_and_push_projection( // When building the final `extraction_pairs`, the Alias wrapper is // stripped so consumers see the usual `(inner_expr, alias_name)` tuples. - let mut extractors = vec![LeafExpressionExtractor::new( - input_schema.as_ref(), - alias_generator, - )]; + // The consumers of the input: every expression of this projection, or + // only the non-pass-through ones when the direct columns are known. + let no_direct_columns = IndexSet::new(); + let consumers = proj.expr.iter().filter(|expr| { + known_direct_columns.is_none() || passthrough_column(expr).is_none() + }); + let input_projection = match input.as_ref() { + LogicalPlan::Projection(projection) => Some(projection), + _ => None, + }; + let extractor = LeafExpressionExtractor::new(input_schema.as_ref(), alias_generator) + .with_input_projection( + input_projection, + consumers, + known_direct_columns.unwrap_or(&no_direct_columns), + ); + let mut extractors = vec![extractor]; let input_column_sets = vec![schema_columns(input_schema.as_ref())]; let original_schema = proj.schema.as_ref(); @@ -1046,6 +1045,9 @@ fn split_and_push_projection( // Pass-through — the input already produces this column, so // there is nothing to extract; just track it in the extractor. extractors[0].columns_needed.insert(col.clone()); + if known_direct_columns.is_none() { + extractors[0].direct_columns.insert(col.clone()); + } recovery_exprs.push(expr.clone()); proj_exprs_captured += 1; } else if let Expr::Alias(alias) = expr @@ -1105,26 +1107,27 @@ fn split_and_push_projection( }) .collect(); let columns_needed = &extractor.columns_needed; + let mut direct_columns = extractor.direct_columns.clone(); + if let Some(known) = known_direct_columns { + direct_columns.extend(known.iter().cloned()); + } // If no extractions found, nothing to do if extraction_pairs.is_empty() { return Ok(None); } - // Pushing into an input that is already a projection merges into it and - // inlines the referenced columns' definitions. Leave the projection alone - // when that would duplicate a volatile computation. - if would_duplicate_volatile( - extraction_pairs.iter().map(|(expr, _)| expr), - input.as_ref(), - ) { - return Ok(None); - } - - // Merging here would re-inline an expression `CommonSubexprEliminate` hoisted - // to run once, undoing it (issue #23655); leave the projection in place. - if let LogicalPlan::Projection(child) = input.as_ref() - && merge_would_duplicate_kept_expr(&extraction_pairs, columns_needed, child) + // `routing_extract` only extracts what can be merged into an input + // projection, but the pre-existing `__datafusion_extracted` aliases were + // not checked. Leave the projection in place if merging them would + // evaluate a definition of the input again (issue #23655). + if let Some(inliner) = &extractor.input_inliner + && !pinned_for_merge( + inliner, + extraction_pairs.iter().map(|(expr, _)| expr), + &direct_columns, + ) + .is_empty() { return Ok(None); } @@ -1132,8 +1135,11 @@ fn split_and_push_projection( // ── Phase 2: Push down ────────────────────────────────────────────── let proj_input = Arc::clone(&proj.input); let pushed = push_extraction_pairs( - &extraction_pairs, - columns_needed, + &ExtractionTarget { + pairs: extraction_pairs.clone(), + columns: columns_needed.clone(), + direct_columns: direct_columns.clone(), + }, proj, &proj_input, alias_generator, @@ -1158,13 +1164,18 @@ fn split_and_push_projection( // pure extraction projection, which can go through nodes that the // original projection cannot. let input_arc = Arc::clone(input); - let extraction = LogicalPlan::Projection(build_extraction_projection_impl( + let Some(extraction) = build_extraction_projection_impl( &extraction_pairs, columns_needed, + &direct_columns, &input_arc, input_schema.as_ref(), - )?); - match try_push_input(&extraction, alias_generator)? { + )? + else { + return Ok(None); + }; + let extraction = LogicalPlan::Projection(extraction); + match try_push_input(&extraction, Some(&direct_columns), alias_generator)? { Some(pushed) => pushed, // The extraction projection cannot move below `input`. Leave // the original projection unchanged: the split would put the @@ -1263,8 +1274,7 @@ pub(crate) fn is_pure_extraction_projection(exprs: &[Expr]) -> bool { /// Pushes extraction pairs down through the projection's input node, /// dispatching to the appropriate handler based on the input node type. fn push_extraction_pairs( - pairs: &[(Expr, String)], - columns_needed: &IndexSet, + target: &ExtractionTarget, proj: &Projection, proj_input: &Arc, alias_generator: &Arc, @@ -1279,12 +1289,16 @@ fn push_extraction_pairs( // extracted sub-parts) would be lost during the merge. LogicalPlan::Projection(_) if proj_exprs_captured == proj.expr.len() => { let target_schema = Arc::clone(proj_input.schema()); - let merged = build_extraction_projection_impl( - pairs, - columns_needed, + let Some(merged) = build_extraction_projection_impl( + &target.pairs, + &target.columns, + &target.direct_columns, proj_input, target_schema.as_ref(), - )?; + )? + else { + return Ok(None); + }; let merged_is_pure = is_pure_extraction_projection(&merged.expr); let merged_plan = LogicalPlan::Projection(merged); @@ -1297,7 +1311,11 @@ fn push_extraction_pairs( // This handles: Extraction → Recovery(cols) → Filter → ... → TableScan // by pushing through the recovery projection AND the filter in one pass. if merged_is_pure - && let Some(pushed) = try_push_input(&merged_plan, alias_generator)? + && let Some(pushed) = try_push_input( + &merged_plan, + Some(&target.direct_columns), + alias_generator, + )? { return Ok(Some(pushed)); } @@ -1308,12 +1326,7 @@ fn push_extraction_pairs( // Join, and anything else. // Safely bails out for nodes that don't pass through extracted // columns (Aggregate, Window) via the output schema check. - _ => try_push_into_inputs( - pairs, - columns_needed, - proj_input.as_ref(), - alias_generator, - ), + _ => try_push_into_inputs(target, proj_input.as_ref(), alias_generator), } } @@ -1325,8 +1338,7 @@ fn push_extraction_pairs( /// /// Returns `None` if any expression can't be routed or no input has pairs. fn route_to_inputs( - pairs: &[(Expr, String)], - columns: &IndexSet, + target: &ExtractionTarget, node: &LogicalPlan, input_column_sets: &[std::collections::HashSet], input_schemas: &[Arc], @@ -1336,6 +1348,7 @@ fn route_to_inputs( .map(|_| ExtractionTarget { pairs: vec![], columns: IndexSet::new(), + direct_columns: IndexSet::new(), }) .collect(); @@ -1345,17 +1358,16 @@ fn route_to_inputs( // `simple_struct.s`). Remap pairs/columns to each input's space. let union_schema = node.schema(); for (idx, input_schema) in input_schemas.iter().enumerate() { - per_input[idx] = - remap_pairs_and_columns(pairs, columns, union_schema, input_schema)?; + per_input[idx] = remap_pairs_and_columns(target, union_schema, input_schema)?; } } else { - for (expr, alias) in pairs { + for (expr, alias) in &target.pairs { match find_owning_input(expr, input_column_sets) { Some(idx) => per_input[idx].pairs.push((expr.clone(), alias.clone())), None => return Ok(None), // Cross-input expression — bail out } } - for col in columns { + for col in &target.columns { let col_expr = Expr::Column(col.clone()); match find_owning_input(&col_expr, input_column_sets) { Some(idx) => { @@ -1364,6 +1376,15 @@ fn route_to_inputs( None => return Ok(None), // Ambiguous column — bail out } } + // A direct reference counts for every input that can own it. + for col in &target.direct_columns { + let col_ref = ColumnReference::new(col.relation.as_ref(), col.name()); + for (idx, cols) in input_column_sets.iter().enumerate() { + if cols.contains(&col_ref) { + per_input[idx].direct_columns.insert(col.clone()); + } + } + } } // Check at least one input has extractions to push @@ -1403,8 +1424,7 @@ fn route_to_inputs( /// TableScan: right [user_id, order] /// ``` fn try_push_into_inputs( - pairs: &[(Expr, String)], - columns_needed: &IndexSet, + target: &ExtractionTarget, node: &LogicalPlan, alias_generator: &Arc, ) -> Result> { @@ -1421,16 +1441,22 @@ fn try_push_into_inputs( // SubqueryAlias remaps qualifiers between input and output. // Rewrite pairs/columns from alias-space to input-space before routing. + // The node itself also references its inputs directly. + let mut direct_columns = target.direct_columns.clone(); + for expr in node.expressions() { + direct_columns.extend(expr.column_refs().into_iter().cloned()); + } + let target = ExtractionTarget { + pairs: target.pairs.clone(), + columns: target.columns.clone(), + direct_columns, + }; let remapped = if let LogicalPlan::SubqueryAlias(sa) = node { - remap_pairs_and_columns(pairs, columns_needed, &sa.schema, sa.input.schema())? + remap_pairs_and_columns(&target, &sa.schema, sa.input.schema())? } else { - ExtractionTarget { - pairs: pairs.to_vec(), - columns: columns_needed.clone(), - } + target }; let pairs = &remapped.pairs[..]; - let columns_needed = &remapped.columns; // Build per-input schemas and column sets for routing let input_schemas: Vec> = @@ -1439,13 +1465,8 @@ fn try_push_into_inputs( input_schemas.iter().map(|s| schema_columns(s)).collect(); // Route pairs and columns to the appropriate inputs - let Some(per_input) = route_to_inputs( - pairs, - columns_needed, - node, - &input_column_sets, - &input_schemas, - )? + let Some(per_input) = + route_to_inputs(&remapped, node, &input_column_sets, &input_schemas)? else { return Ok(None); }; @@ -1462,23 +1483,20 @@ fn try_push_into_inputs( if per_input[idx].pairs.is_empty() { new_inputs.push(input.clone()); } else { - // Merging into an input projection inlines the referenced columns' - // definitions; bail out when that would duplicate a volatile - // computation. - if would_duplicate_volatile( - per_input[idx].pairs.iter().map(|(expr, _)| expr), - input, - ) { - return Ok(None); - } let input_arc = Arc::new(input.clone()); let target_schema = Arc::clone(input.schema()); - let proj = build_extraction_projection_impl( + // Bail out when merging into an input projection would evaluate + // one of its definitions again (see `pinned_for_merge`). + let Some(proj) = build_extraction_projection_impl( &per_input[idx].pairs, &per_input[idx].columns, + &per_input[idx].direct_columns, &input_arc, target_schema.as_ref(), - )?; + )? + else { + return Ok(None); + }; // Verify all requested aliases appear in the projection's output. // A merge may deduplicate if the same expression already exists // under a different alias, leaving the requested alias missing. @@ -1493,7 +1511,11 @@ fn try_push_into_inputs( // this input (e.g., through Filter → existing extraction projection). // This ensures the input's output schema is stable and won't change // when the TopDown pass later visits children. - let pushed = match try_push_input(&proj_plan, alias_generator)? { + let pushed = match try_push_input( + &proj_plan, + Some(&per_input[idx].direct_columns), + alias_generator, + )? { Some(pushed) => pushed, None => proj_plan, }; @@ -3988,19 +4010,24 @@ mod tests { )) } - /// `merge_would_duplicate_kept_expr` must block a merge only when it would - /// inline one `KeepInPlace` definition into more than one reference site. + /// `pinned_for_merge` must block a merge only when it would inline one + /// `KeepInPlace` definition into more than one reference site. /// It must count reference sites for `KeepInPlace` columns alone: a plain /// column is cheap to duplicate, and a merge that touches only plain /// columns must still go ahead. #[test] - fn test_merge_would_duplicate_kept_expr_counts_only_kept_columns() -> Result<()> { + fn test_pinned_for_merge_counts_only_kept_columns() -> Result<()> { let scan = test_table_scan_with_struct()?; // `k` holds a `KeepInPlace` definition. `id` is a plain column. let child = Projection::try_new( vec![keep_in_place_udf(col("user")).alias("k"), col("id")], Arc::new(scan), )?; + let inliner = ProjectionInliner::new(&child); + let blocks = |pairs: &[(Expr, String)], columns: &IndexSet| { + !pinned_for_merge(&inliner, pairs.iter().map(|(expr, _)| expr), columns) + .is_empty() + }; let no_columns = IndexSet::new(); // One reference site: the merge inlines one copy, so it is free. @@ -4008,11 +4035,7 @@ mod tests { get_field_like(col("k"), "a"), "__datafusion_extracted_1".to_string(), )]; - assert!(!merge_would_duplicate_kept_expr( - &one_site, - &no_columns, - &child - )); + assert!(!blocks(&one_site, &no_columns)); // Two reference sites: the merge would evaluate `k` two times. let two_sites = vec![ @@ -4025,20 +4048,12 @@ mod tests { "__datafusion_extracted_2".to_string(), ), ]; - assert!(merge_would_duplicate_kept_expr( - &two_sites, - &no_columns, - &child - )); + assert!(blocks(&two_sites, &no_columns)); // A pass-through reference to `k` is a second site too (#23655). let kept_column: IndexSet = std::iter::once(Column::new_unqualified("k")).collect(); - assert!(merge_would_duplicate_kept_expr( - &one_site, - &kept_column, - &child - )); + assert!(blocks(&one_site, &kept_column)); // Two sites that reference the plain column `id` are cheap to // duplicate, so the merge must still go ahead. @@ -4052,11 +4067,7 @@ mod tests { "__datafusion_extracted_2".to_string(), ), ]; - assert!(!merge_would_duplicate_kept_expr( - &plain_column_sites, - &no_columns, - &child - )); + assert!(!blocks(&plain_column_sites, &no_columns)); Ok(()) } @@ -4096,6 +4107,52 @@ mod tests { Projection: leaf_udf(test.user, Utf8("name")) AS __datafusion_extracted_1, test.id, test.user TableScan: test "#); + + Ok(()) + } + + /// Regression test for issue #25329: the #23655 shape inside a `Filter`. + /// `CommonSubexprEliminate` puts `f(c)` into a projection below the filter; + /// `ExtractLeafExpressions` must not merge `f(c)['a']` into that + /// projection, because that evaluates `f(c)` a second time. + #[test] + fn test_struct_returning_udf_in_filter_evaluated_once() -> Result<()> { + let udf = ScalarUDF::new_from_impl( + PlacementTestUDF::new().with_placement(ExpressionPlacement::KeepInPlace), + ); + let f_c = udf.call(vec![col("c")]); + let plan = LogicalPlanBuilder::from(test_table_scan()?) + .filter( + f_c.clone() + .is_not_null() + .and(get_field_like(f_c, "a").is_null()), + )? + .project(vec![col("a")])? + .build()?; + + let ctx = OptimizerContext::new(); + let optimizer = Optimizer::with_rules(vec![ + Arc::new(CommonSubexprEliminate::new()), + Arc::new(ExtractLeafExpressions::new()), + Arc::new(PushDownLeafProjections::new()), + Arc::new(OptimizeProjections::new()), + ]); + let optimized = optimizer.optimize(plan, &ctx, |_, _| {})?; + + let formatted = format!("{optimized}"); + assert_eq!( + formatted.matches("keep_in_place_udf(test.c)").count(), + 1, + "struct-returning UDF must be evaluated once; got:\n{formatted}" + ); + + insta::assert_snapshot!(formatted, @r#" + Projection: test.a + Filter: __common_expr_1 IS NOT NULL AND get_field_like(__common_expr_1, Utf8("a")) IS NULL + Projection: keep_in_place_udf(test.c) AS __common_expr_1, test.a + TableScan: test projection=[a, c] + "#); + Ok(()) } } From 10268fbde9f99f955b1e287cdf53fcbf41c52718 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Thu, 17 Sep 2026 21:00:44 -0500 Subject: [PATCH 04/12] test: cover a struct-returning function evaluated one time in `cse.slt` Add the two end-to-end shapes of the issue: the common sub-expression is referenced bare and through a field access, in a projection and in a filter. Both plans must show `arrow_field(a)` one time. Covers https://github.com/apache/datafusion/issues/23655 and https://github.com/apache/datafusion/issues/25329 Co-Authored-By: Claude Fable 5.1 --- datafusion/sqllogictest/test_files/cse.slt | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/datafusion/sqllogictest/test_files/cse.slt b/datafusion/sqllogictest/test_files/cse.slt index f8d67020be13b..aa82eb7e0775a 100644 --- a/datafusion/sqllogictest/test_files/cse.slt +++ b/datafusion/sqllogictest/test_files/cse.slt @@ -355,3 +355,52 @@ FROM cse_agg_filter statement ok DROP TABLE cse_agg_filter + +# A struct-returning function that keeps its place is evaluated one time when a +# common sub-expression is referenced both bare and through a field access. +# The projection shape is https://github.com/apache/datafusion/issues/23655 and +# the filter shape is https://github.com/apache/datafusion/issues/25329. +statement ok +CREATE TABLE cse_keep_in_place(a INT) AS VALUES (1), (2); + +# Projection shape: `arrow_field(a)` appears one time in the physical plan. +query TT +EXPLAIN SELECT CASE WHEN arrow_field(a) IS NOT NULL THEN get_field(arrow_field(a), 'name') END AS c1 FROM cse_keep_in_place; +---- +logical_plan +01)Projection: CASE WHEN __common_expr_1 IS NOT NULL THEN get_field(__common_expr_1, Utf8("name")) END AS c1 +02)--Projection: arrow_field(cse_keep_in_place.a) AS __common_expr_1 +03)----TableScan: cse_keep_in_place projection=[a] +physical_plan +01)ProjectionExec: expr=[CASE WHEN __common_expr_1@0 IS NOT NULL THEN get_field(__common_expr_1@0, name) END as c1] +02)--ProjectionExec: expr=[arrow_field(a@0) as __common_expr_1] +03)----DataSourceExec: partitions=1, partition_sizes=[1] + +query T +SELECT CASE WHEN arrow_field(a) IS NOT NULL THEN get_field(arrow_field(a), 'name') END AS c1 FROM cse_keep_in_place; +---- +a +a + +# Filter shape: `arrow_field(a)` appears one time in the physical plan. +query TT +EXPLAIN SELECT a FROM cse_keep_in_place WHERE CASE WHEN arrow_field(a) IS NOT NULL THEN arrow_field(a)['nullable'] IS NULL END; +---- +logical_plan +01)Projection: cse_keep_in_place.a +02)--Filter: CASE WHEN __common_expr_1 IS NOT NULL THEN get_field(__common_expr_1, Utf8("nullable")) IS NULL END +03)----Projection: arrow_field(cse_keep_in_place.a) AS __common_expr_1, cse_keep_in_place.a +04)------TableScan: cse_keep_in_place projection=[a] +physical_plan +01)FilterExec: CASE WHEN __common_expr_1@0 IS NOT NULL THEN get_field(__common_expr_1@0, nullable) IS NULL END, projection=[a@1] +02)--ProjectionExec: expr=[arrow_field(a@0) as __common_expr_1, a@0 as a] +03)----DataSourceExec: partitions=1, partition_sizes=[1] + +query I +SELECT a FROM cse_keep_in_place WHERE CASE WHEN arrow_field(a) IS NOT NULL THEN arrow_field(a)['nullable'] END ORDER BY a; +---- +1 +2 + +statement ok +DROP TABLE cse_keep_in_place; From 03e547078f1926eaf5ed1c2f46562c0688f17aaa Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Fri, 18 Sep 2026 09:33:54 -0500 Subject: [PATCH 05/12] fix(optimizer): ask about volatility before placement in `DuplicationCost` `DuplicationCost::of` returned `Free` as soon as the expression reported a placement of `MoveTowardsLeafNodes`, before it walked the expression for volatile nodes. A volatile function is free to report that placement, and `file_row_index()` does. Such a definition was classed `Free` and the rules inlined it into two sites. Volatility and subqueries now win over placement. `file_row_index.slt` keeps the extraction column and evaluates the call one time, which the new `EXPLAIN` there pins. Co-Authored-By: Claude Fable 5.1 --- .../optimizer/src/projection_inliner.rs | 55 ++++++++++++++++++- .../test_files/file_row_index.slt | 16 ++++++ 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/datafusion/optimizer/src/projection_inliner.rs b/datafusion/optimizer/src/projection_inliner.rs index 252c11d5fb9f9..ad416a7ab77f7 100644 --- a/datafusion/optimizer/src/projection_inliner.rs +++ b/datafusion/optimizer/src/projection_inliner.rs @@ -75,10 +75,13 @@ impl DuplicationCost { /// Returns the cost of one more evaluation of `expr`. fn of(expr: &Expr) -> Self { let expr = unalias(expr); - if expr.placement().should_push_to_leaves() { - return Self::Free; - } + // Volatility and subqueries are asked about first, before placement. + // A volatile function is free to report + // `ExpressionPlacement::MoveTowardsLeafNodes`, and `file_row_index()` + // does. Such a call is cheap to evaluate and still must never be + // evaluated at two sites, because the two sites give two different + // answers. let mut cost = Self::Cheap; expr.apply(|e| { let node_cost = match e { @@ -117,6 +120,13 @@ impl DuplicationCost { }) }) .expect("traversal is infallible"); + + if cost == Self::Forbidden { + return Self::Forbidden; + } + if expr.placement().should_push_to_leaves() { + return Self::Free; + } cost } } @@ -349,6 +359,17 @@ mod tests { udf(ExpressionPlacement::KeepInPlace, Volatility::Volatile).call(vec![arg]) } + /// A volatile function that reports + /// [`ExpressionPlacement::MoveTowardsLeafNodes`], as `file_row_index()` + /// does. + fn volatile_leaf(arg: Expr) -> Expr { + udf( + ExpressionPlacement::MoveTowardsLeafNodes, + Volatility::Volatile, + ) + .call(vec![arg]) + } + fn projection(exprs: Vec) -> Projection { let plan = LogicalPlanBuilder::from(test_table_scan().unwrap()) .project(exprs) @@ -392,6 +413,34 @@ mod tests { ); } + /// Volatility beats placement. A volatile function that reports + /// `MoveTowardsLeafNodes` is cheap to evaluate, and two evaluations still + /// give two different answers. + #[test] + fn a_volatile_leaf_function_is_forbidden() { + use DuplicationCost::*; + assert_eq!(DuplicationCost::of(&volatile_leaf(col("a"))), Forbidden); + assert_eq!( + DuplicationCost::of(&volatile_leaf(col("a")).alias("x")), + Forbidden + ); + assert_eq!( + DuplicationCost::of(&get_field_like(volatile_leaf(col("a")), "x")), + Forbidden + ); + // The same function, not volatile, is free. + assert_eq!( + DuplicationCost::of( + &udf( + ExpressionPlacement::MoveTowardsLeafNodes, + Volatility::Immutable + ) + .call(vec![col("a")]) + ), + Free + ); + } + #[test] fn pinned_counts_evaluations() { use DuplicationCost::*; diff --git a/datafusion/sqllogictest/test_files/file_row_index.slt b/datafusion/sqllogictest/test_files/file_row_index.slt index 935c4d6c72c01..2c8d3fc9fa513 100644 --- a/datafusion/sqllogictest/test_files/file_row_index.slt +++ b/datafusion/sqllogictest/test_files/file_row_index.slt @@ -42,6 +42,22 @@ ORDER BY column1 4 5 50 +# `file_row_index()` is volatile, so the extraction column must stay in the +# plan. Both references read it, and the call is evaluated one time. +query TT +EXPLAIN SELECT file_row_index(), file_row_index() + 1, column1 +FROM parquet_table +ORDER BY column1 +---- +logical_plan +01)Sort: parquet_table.column1 ASC NULLS LAST +02)--Projection: __datafusion_extracted_1 AS file_row_index(), __datafusion_extracted_1 + Int64(1) AS file_row_index() + Int64(1), parquet_table.column1 +03)----Projection: file_row_index() AS __datafusion_extracted_1, parquet_table.column1 +04)------TableScan: parquet_table projection=[column1] +physical_plan +01)SortExec: expr=[column1@2 ASC NULLS LAST], preserve_partitioning=[false] +02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/file_row_index/parquet_table/data.parquet]]}, projection=[CAST(__datafusion_file_row_index@1 AS Int64) as file_row_index(), CAST(__datafusion_file_row_index@1 AS Int64) + 1 as file_row_index() + Int64(1), column1], file_type=parquet, sort_order_for_reorder=[column1@2 ASC NULLS LAST] + query II SELECT file_row_index(), column1 FROM parquet_table From 60bd784bef817efd30975e8ae2a4bb99ccf144df Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Fri, 18 Sep 2026 09:34:25 -0500 Subject: [PATCH 06/12] refactor(optimizer): rename `pinned_for_known_consumers` The name said the opposite of the doc comment. The method is for a projection that can stay in the plan for consumers that the caller can not see, so the consumers are the unknown ones. Co-Authored-By: Claude Fable 5.1 --- datafusion/optimizer/src/extract_leaf_expressions.rs | 2 +- datafusion/optimizer/src/projection_inliner.rs | 6 +++--- datafusion/optimizer/src/push_down_filter.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/datafusion/optimizer/src/extract_leaf_expressions.rs b/datafusion/optimizer/src/extract_leaf_expressions.rs index 5856310085a41..88d65111ad066 100644 --- a/datafusion/optimizer/src/extract_leaf_expressions.rs +++ b/datafusion/optimizer/src/extract_leaf_expressions.rs @@ -126,7 +126,7 @@ fn pinned_for_merge<'a>( direct_columns: &IndexSet, ) -> PinnedDefinitions { let direct: Vec = direct_columns.iter().cloned().map(Expr::Column).collect(); - inliner.pinned_for_known_consumers(inlined, &direct, DuplicationCost::Free) + inliner.pinned_with_unknown_consumers(inlined, &direct, DuplicationCost::Free) } /// Extracts `MoveTowardsLeafNodes` sub-expressions from non-projection nodes diff --git a/datafusion/optimizer/src/projection_inliner.rs b/datafusion/optimizer/src/projection_inliner.rs index ad416a7ab77f7..47ca10e617560 100644 --- a/datafusion/optimizer/src/projection_inliner.rs +++ b/datafusion/optimizer/src/projection_inliner.rs @@ -231,7 +231,7 @@ impl<'a> ProjectionInliner<'a> { /// the caller can not see. A copy of a forbidden definition would then /// be evaluated independently of the original, and change the query /// result. - pub(crate) fn pinned_for_known_consumers<'i, 'o>( + pub(crate) fn pinned_with_unknown_consumers<'i, 'o>( &self, inlined: impl IntoIterator, others: impl IntoIterator, @@ -506,7 +506,7 @@ mod tests { // Without all the consumers, a single reference is pinned as well. assert!( !inliner - .pinned_for_known_consumers([&col("r")], no_others, max) + .pinned_with_unknown_consumers([&col("r")], no_others, max) .is_empty() ); } @@ -525,7 +525,7 @@ mod tests { .is_not_null() .and(get_field_like(col("exp"), "x")); let other = col("cheap").gt(col("a")); - let pinned = inliner.pinned_for_known_consumers( + let pinned = inliner.pinned_with_unknown_consumers( [&twice, &other], no_others, DuplicationCost::Cheap, diff --git a/datafusion/optimizer/src/push_down_filter.rs b/datafusion/optimizer/src/push_down_filter.rs index 84bcd0829162a..e4427f3536e2d 100644 --- a/datafusion/optimizer/src/push_down_filter.rs +++ b/datafusion/optimizer/src/push_down_filter.rs @@ -1561,7 +1561,7 @@ fn rewrite_projection( // optimizer instability: `ExtractLeafExpressions` undoes the push-down, // which creates a loop that runs until the iteration limit is hit. let no_other_consumers: [&Expr; 0] = []; - let pinned = inliner.pinned_for_known_consumers( + let pinned = inliner.pinned_with_unknown_consumers( &predicates, no_other_consumers, DuplicationCost::Cheap, From c6974ea5ab2d81830ab0a3557015bb3b0406ab27 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Tue, 27 Oct 2026 19:53:09 -0500 Subject: [PATCH 07/12] test: update tests for the rebase onto main Pass `direct_columns` and unwrap the `Option` in the upstream test `test_merge_appends_qualified_column_for_bare_request`. The merge now resolves the bare `a` of `test_merge_bare_column_into_qualified_projection` to its definition `test.a`. `file_row_index.slt` picks up the nullability display of #26359, and the projection now keeps the two spelled calls, because `ExtractLeafExpressions` leaves a projection unchanged when its extraction projection cannot move below the input. Co-Authored-By: Claude Opus 5.5 --- datafusion/optimizer/src/extract_leaf_expressions.rs | 6 ++++-- .../sqllogictest/test_files/file_row_index.slt | 12 ++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/datafusion/optimizer/src/extract_leaf_expressions.rs b/datafusion/optimizer/src/extract_leaf_expressions.rs index 88d65111ad066..52e7cc08dc2c6 100644 --- a/datafusion/optimizer/src/extract_leaf_expressions.rs +++ b/datafusion/optimizer/src/extract_leaf_expressions.rs @@ -2528,7 +2528,7 @@ mod tests { ## After Extraction Projection: test.a, test.b, d Filter: __datafusion_extracted_1 = Int32(1) - Projection: test.a, test.b, test.c + Int32(1) AS d, leaf_udf(a, Utf8("x")) AS __datafusion_extracted_1 + Projection: test.a, test.b, test.c + Int32(1) AS d, leaf_udf(test.a, Utf8("x")) AS __datafusion_extracted_1 TableScan: test projection=[a, b, c] ## After Pushdown @@ -2554,9 +2554,11 @@ mod tests { let merged = build_extraction_projection_impl( &[], &columns_needed, + &IndexSet::new(), &Arc::new(projection), &target_schema, - )?; + )? + .expect("the merge does not duplicate any definition"); assert_eq!( merged.expr, diff --git a/datafusion/sqllogictest/test_files/file_row_index.slt b/datafusion/sqllogictest/test_files/file_row_index.slt index 2c8d3fc9fa513..54097700e7a95 100644 --- a/datafusion/sqllogictest/test_files/file_row_index.slt +++ b/datafusion/sqllogictest/test_files/file_row_index.slt @@ -42,8 +42,9 @@ ORDER BY column1 4 5 50 -# `file_row_index()` is volatile, so the extraction column must stay in the -# plan. Both references read it, and the call is evaluated one time. +# `file_row_index()` is volatile, so no rule may inline a definition of it into +# more than one site. The query spells the call twice, so the projection keeps +# both calls, and the scan reads both from the same row index column. query TT EXPLAIN SELECT file_row_index(), file_row_index() + 1, column1 FROM parquet_table @@ -51,12 +52,11 @@ ORDER BY column1 ---- logical_plan 01)Sort: parquet_table.column1 ASC NULLS LAST -02)--Projection: __datafusion_extracted_1 AS file_row_index(), __datafusion_extracted_1 + Int64(1) AS file_row_index() + Int64(1), parquet_table.column1 -03)----Projection: file_row_index() AS __datafusion_extracted_1, parquet_table.column1 -04)------TableScan: parquet_table projection=[column1] +02)--Projection: file_row_index(), file_row_index() + Int64(1), parquet_table.column1 +03)----TableScan: parquet_table projection=[column1] physical_plan 01)SortExec: expr=[column1@2 ASC NULLS LAST], preserve_partitioning=[false] -02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/file_row_index/parquet_table/data.parquet]]}, projection=[CAST(__datafusion_file_row_index@1 AS Int64) as file_row_index(), CAST(__datafusion_file_row_index@1 AS Int64) + 1 as file_row_index() + Int64(1), column1], file_type=parquet, sort_order_for_reorder=[column1@2 ASC NULLS LAST] +02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/file_row_index/parquet_table/data.parquet]]}, projection=[CAST(__datafusion_file_row_index@1 AS Int64;N) as file_row_index(), CAST(__datafusion_file_row_index@1 AS Int64;N) + 1 as file_row_index() + Int64(1), column1], file_type=parquet, sort_order_for_reorder=[column1@2 ASC NULLS LAST] query II SELECT file_row_index(), column1 From 5121b826102999a150b8f15f1f3462e612ed702b Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Wed, 28 Oct 2026 22:35:04 -0500 Subject: [PATCH 08/12] test: check one evaluation of a shared `file_row_index()` definition The EXPLAIN test spells `file_row_index()` two times in the query, so it does not check that a rule never copies a volatile definition. Add a query that defines it one time in a subquery and references it from a filter and the outer projection. The plans must show one call, and the results must agree with that one value. The nested-projection test shape in this PR comes from https://github.com/apache/datafusion/pull/25388. Co-authored-by: Pepijn Van Eeckhoudt Co-Authored-By: Claude Opus 5.5 --- .../test_files/file_row_index.slt | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/datafusion/sqllogictest/test_files/file_row_index.slt b/datafusion/sqllogictest/test_files/file_row_index.slt index 54097700e7a95..a4b12b040dc43 100644 --- a/datafusion/sqllogictest/test_files/file_row_index.slt +++ b/datafusion/sqllogictest/test_files/file_row_index.slt @@ -58,6 +58,41 @@ physical_plan 01)SortExec: expr=[column1@2 ASC NULLS LAST], preserve_partitioning=[false] 02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/file_row_index/parquet_table/data.parquet]]}, projection=[CAST(__datafusion_file_row_index@1 AS Int64;N) as file_row_index(), CAST(__datafusion_file_row_index@1 AS Int64;N) + 1 as file_row_index() + Int64(1), column1], file_type=parquet, sort_order_for_reorder=[column1@2 ASC NULLS LAST] +# A single definition of `file_row_index()` that a filter and the projection +# above both reference. The definition is volatile, so `PushDownFilter` must +# keep the filter above the projection that computes it, and no rule may copy +# the call: the plans must show exactly one `file_row_index()` call. +query TT +EXPLAIN SELECT r, r + 1, column1 +FROM (SELECT file_row_index() AS r, column1 FROM parquet_table) +WHERE r + r > 2 +ORDER BY column1 +---- +logical_plan +01)Sort: parquet_table.column1 ASC NULLS LAST +02)--Projection: r, r + Int64(1), parquet_table.column1 +03)----Filter: r + r > Int64(2) +04)------Projection: file_row_index() AS r, parquet_table.column1 +05)--------TableScan: parquet_table projection=[column1] +physical_plan +01)SortPreservingMergeExec: [column1@2 ASC NULLS LAST] +02)--SortExec: expr=[column1@2 ASC NULLS LAST], preserve_partitioning=[true] +03)----ProjectionExec: expr=[r@0 as r, r@0 + 1 as r + Int64(1), column1@1 as column1] +04)------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1 +05)--------FilterExec: r@0 + r@0 > 2 +06)----------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/file_row_index/parquet_table/data.parquet]]}, projection=[CAST(__datafusion_file_row_index@1 AS Int64;N) as r, column1], file_type=parquet + +# Each row sees one value of `r`: `r + 1` and the filter agree with `r`. +query III +SELECT r, r + 1, column1 +FROM (SELECT file_row_index() AS r, column1 FROM parquet_table) +WHERE r + r > 2 +ORDER BY column1 +---- +2 3 30 +3 4 40 +4 5 50 + query II SELECT file_row_index(), column1 FROM parquet_table From a48898dcbdd396ff2e97ea97821dcb2bdd399bbc Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Wed, 28 Oct 2026 22:35:04 -0500 Subject: [PATCH 09/12] fix(optimizer): address review findings for the inlining policy - `DuplicationCost::of`: a volatile node of any kind is forbidden, not only a volatile scalar function. A higher-order function is expensive. - `rewrite_projection`: count references only over the predicates that can be pushed. A kept predicate reads the computed column. - Compute a definition's cost only when a check needs it. - Remove a dead lookup in `extract_leaf_expressions` and a duplicate `unalias`. - `cse.slt`: the data query now matches the EXPLAIN query. Co-Authored-By: Claude Opus 5.5 --- .../optimizer/src/extract_leaf_expressions.rs | 15 ++--- .../optimizer/src/projection_inliner.rs | 31 +++++---- datafusion/optimizer/src/push_down_filter.rs | 65 ++++++++++++++----- datafusion/sqllogictest/test_files/cse.slt | 8 +-- 4 files changed, 77 insertions(+), 42 deletions(-) diff --git a/datafusion/optimizer/src/extract_leaf_expressions.rs b/datafusion/optimizer/src/extract_leaf_expressions.rs index 52e7cc08dc2c6..351a59c9d548c 100644 --- a/datafusion/optimizer/src/extract_leaf_expressions.rs +++ b/datafusion/optimizer/src/extract_leaf_expressions.rs @@ -821,20 +821,17 @@ fn build_extraction_projection_impl( if output_names.contains(col.name.as_str()) { continue; } - let resolved = inliner - .definition(col) - .cloned() - .unwrap_or_else(|| Expr::Column(col.clone())); - if let Expr::Column(resolved_col) = &resolved - && let Some(input_col) = resolve_against(input_schema, resolved_col) + // `existing` does not produce this name, so `col` is not one of + // its outputs and has no definition to resolve: it can only be a + // column of the input. + if let Some(input_col) = resolve_against(input_schema, col) && !existing_cols.contains(&input_col) && !output_names.contains(input_col.name.as_str()) { proj_exprs.push(Expr::Column(input_col)); } - // Skip a column that resolves to a non-column expr: the existing - // projection already computes it. Also skip a column that is not in - // the input or is ambiguous there: no single spelling is correct. + // Skip a column that is not in the input or is ambiguous there: no + // single spelling is correct. } Projection::try_new(proj_exprs, Arc::clone(&existing.input)).map(Some) diff --git a/datafusion/optimizer/src/projection_inliner.rs b/datafusion/optimizer/src/projection_inliner.rs index 47ca10e617560..a16c3b95b7aee 100644 --- a/datafusion/optimizer/src/projection_inliner.rs +++ b/datafusion/optimizer/src/projection_inliner.rs @@ -62,9 +62,10 @@ pub(crate) enum DuplicationCost { /// `OptimizeProjections` (issue #8296) unchanged. Cheap, /// Contains a scalar function that reports - /// [`ExpressionPlacement::KeepInPlace`] for column arguments. This is the - /// default for a user-defined function, so the optimizer must assume that - /// it can be arbitrarily expensive. + /// [`ExpressionPlacement::KeepInPlace`] for column arguments, or a + /// higher-order function. This is the default for a user-defined + /// function, so the optimizer must assume that it can be arbitrarily + /// expensive. Expensive, /// Must never be evaluated more than one time: the expression is volatile /// (for example `random()`) or contains a subquery. @@ -81,7 +82,8 @@ impl DuplicationCost { // `ExpressionPlacement::MoveTowardsLeafNodes`, and `file_row_index()` // does. Such a call is cheap to evaluate and still must never be // evaluated at two sites, because the two sites give two different - // answers. + // answers. Every kind of function node can be volatile, not only a + // scalar function (see [`Expr::is_volatile_node`]). let mut cost = Self::Cheap; expr.apply(|e| { let node_cost = match e { @@ -89,7 +91,8 @@ impl DuplicationCost { | Expr::Exists(_) | Expr::InSubquery(_) | Expr::SetComparison(_) => Self::Forbidden, - Expr::ScalarFunction(_) if e.is_volatile_node() => Self::Forbidden, + _ if e.is_volatile_node() => Self::Forbidden, + Expr::HigherOrderFunction(_) => Self::Expensive, Expr::ScalarFunction(func) => { // Ask the function for its own placement, as if its // arguments were plain columns and literals. This isolates @@ -150,8 +153,10 @@ impl PinnedDefinitions { /// caller gives. See [`ProjectionInliner::pinned`]. #[derive(Debug)] pub(crate) struct ProjectionInliner<'a> { - /// (definition without top-level alias, cost), in output column order - definitions: Vec<(&'a Expr, DuplicationCost)>, + /// Definitions without top-level alias, in output column order. Their + /// [`DuplicationCost`] is computed only for the definitions that a check + /// references. + definitions: Vec<&'a Expr>, /// Output column -> index into `definitions` by_column: HashMap, /// Output column name -> index into `definitions`, for unqualified @@ -171,8 +176,7 @@ impl<'a> ProjectionInliner<'a> { for (idx, ((qualifier, field), expr)) in columns.zip(projection.expr.iter()).enumerate() { - let expr = unalias(expr); - inliner.definitions.push((expr, DuplicationCost::of(expr))); + inliner.definitions.push(unalias(expr)); inliner .by_column .insert(Column::new(qualifier.cloned(), field.name()), idx); @@ -259,8 +263,11 @@ impl<'a> ProjectionInliner<'a> { let pinned = inlined_references .into_iter() .filter(|(idx, count)| { - let cost = self.definitions[*idx].1; let evaluations = count + usize::from(other_references.contains_key(idx)); + if evaluations <= 1 && !pin_forbidden { + return false; + } + let cost = DuplicationCost::of(self.definitions[*idx]); (pin_forbidden && cost == DuplicationCost::Forbidden) || (evaluations > 1 && cost > max_cost) }) @@ -286,7 +293,7 @@ impl<'a> ProjectionInliner<'a> { /// Returns the definition of `column`, without its top-level alias. pub(crate) fn definition(&self, column: &Column) -> Option<&'a Expr> { - self.index_of(column).map(|idx| self.definitions[idx].0) + self.index_of(column).map(|idx| self.definitions[idx]) } /// Replaces each column reference in `expr` with its definition, without a @@ -326,7 +333,7 @@ impl<'a> ProjectionInliner<'a> { } /// Removes all top-level aliases. -fn unalias(expr: &Expr) -> &Expr { +pub(crate) fn unalias(expr: &Expr) -> &Expr { match expr { Expr::Alias(alias) => unalias(&alias.expr), _ => expr, diff --git a/datafusion/optimizer/src/push_down_filter.rs b/datafusion/optimizer/src/push_down_filter.rs index e4427f3536e2d..2c84efaa8f94c 100644 --- a/datafusion/optimizer/src/push_down_filter.rs +++ b/datafusion/optimizer/src/push_down_filter.rs @@ -56,7 +56,7 @@ use datafusion_expr::{ use crate::extract_leaf_expressions::is_pure_extraction_projection; use crate::optimizer::ApplyOrder; -use crate::projection_inliner::{DuplicationCost, ProjectionInliner}; +use crate::projection_inliner::{DuplicationCost, ProjectionInliner, unalias}; use crate::simplify_expressions::{reorder_predicates, simplify_predicates}; use crate::utils::{ ColumnReference, has_all_column_refs, is_restrict_null_predicate, schema_columns, @@ -1560,20 +1560,33 @@ fn rewrite_projection( // `get_field`). Re-inlining such an expression into a filter causes // optimizer instability: `ExtractLeafExpressions` undoes the push-down, // which creates a loop that runs until the iteration limit is hit. + // + // The references are counted over the predicates that can be pushed + // only: a predicate that is kept above reads the column that the + // projection computes, so it does not evaluate the definition again. + let references_leaf_expr: Vec = predicates + .iter() + .map(|expr| { + expr.column_refs().into_iter().any(|col| { + inliner.definition(col).is_some_and(|definition| { + definition.placement() == ExpressionPlacement::MoveTowardsLeafNodes + }) + }) + }) + .collect(); let no_other_consumers: [&Expr; 0] = []; let pinned = inliner.pinned_with_unknown_consumers( - &predicates, + predicates + .iter() + .zip(&references_leaf_expr) + .filter(|(_, kept)| !**kept) + .map(|(expr, _)| expr), no_other_consumers, DuplicationCost::Cheap, ); let mut push_predicates = vec![]; let mut keep_predicates = vec![]; - for expr in predicates { - let references_leaf_expr = expr.column_refs().into_iter().any(|col| { - inliner.definition(col).is_some_and(|definition| { - definition.placement() == ExpressionPlacement::MoveTowardsLeafNodes - }) - }); + for (expr, references_leaf_expr) in predicates.into_iter().zip(references_leaf_expr) { if references_leaf_expr || inliner.references_pinned(&expr, &pinned) { keep_predicates.push(expr); } else { @@ -1667,15 +1680,6 @@ fn replace_cols_by_column( .data() } -/// Unalias expression reference. -fn unalias(expr: &Expr) -> &Expr { - if let Expr::Alias(alias) = expr { - unalias(&alias.expr) - } else { - expr - } -} - fn with_filters(predicates: Vec, plan: LogicalPlan) -> LogicalPlan { if let Some(predicate) = conjunction(predicates) { LogicalPlan::Filter(Filter::new(predicate, Arc::new(plan))) @@ -5009,6 +5013,33 @@ mod tests { ) } + /// A predicate that stays above the projection reads the computed column, + /// so it does not count as an evaluation of the definition. The other + /// predicate references `c1` one time and is pushed. + #[test] + fn kept_predicate_does_not_pin_expensive_reference() -> Result<()> { + let plan = LogicalPlanBuilder::from(test_table_scan()?) + .project(vec![ + keep_in_place_udf(col("a")).alias("c1"), + get_field_like(col("b"), "x").alias("c2"), + ])? + .filter( + col("c1") + .eq(col("c2")) + .and(col("c1").gt(lit(ScalarValue::UInt32(Some(1))))), + )? + .build()?; + + assert_optimized_plan_equal!( + plan, + @r#" + Filter: c1 = c2 + Projection: keep_in_place_udf(test.a) AS c1, get_field_like(test.b, Utf8("x")) AS c2 + TableScan: test, full_filters=[keep_in_place_udf(test.a) > UInt32(1)] + "# + ) + } + /// The shape of draft https://github.com/apache/datafusion/pull/25388: /// a filter over a projection that reads an expensive column of the /// projection below it. The narrow policy pushes `c1 IS NOT NULL`, diff --git a/datafusion/sqllogictest/test_files/cse.slt b/datafusion/sqllogictest/test_files/cse.slt index aa82eb7e0775a..7b4fb43b293ca 100644 --- a/datafusion/sqllogictest/test_files/cse.slt +++ b/datafusion/sqllogictest/test_files/cse.slt @@ -384,20 +384,20 @@ a # Filter shape: `arrow_field(a)` appears one time in the physical plan. query TT -EXPLAIN SELECT a FROM cse_keep_in_place WHERE CASE WHEN arrow_field(a) IS NOT NULL THEN arrow_field(a)['nullable'] IS NULL END; +EXPLAIN SELECT a FROM cse_keep_in_place WHERE CASE WHEN arrow_field(a) IS NOT NULL THEN arrow_field(a)['nullable'] IS NOT NULL END; ---- logical_plan 01)Projection: cse_keep_in_place.a -02)--Filter: CASE WHEN __common_expr_1 IS NOT NULL THEN get_field(__common_expr_1, Utf8("nullable")) IS NULL END +02)--Filter: CASE WHEN __common_expr_1 IS NOT NULL THEN get_field(__common_expr_1, Utf8("nullable")) IS NOT NULL END 03)----Projection: arrow_field(cse_keep_in_place.a) AS __common_expr_1, cse_keep_in_place.a 04)------TableScan: cse_keep_in_place projection=[a] physical_plan -01)FilterExec: CASE WHEN __common_expr_1@0 IS NOT NULL THEN get_field(__common_expr_1@0, nullable) IS NULL END, projection=[a@1] +01)FilterExec: CASE WHEN __common_expr_1@0 IS NOT NULL THEN get_field(__common_expr_1@0, nullable) IS NOT NULL END, projection=[a@1] 02)--ProjectionExec: expr=[arrow_field(a@0) as __common_expr_1, a@0 as a] 03)----DataSourceExec: partitions=1, partition_sizes=[1] query I -SELECT a FROM cse_keep_in_place WHERE CASE WHEN arrow_field(a) IS NOT NULL THEN arrow_field(a)['nullable'] END ORDER BY a; +SELECT a FROM cse_keep_in_place WHERE CASE WHEN arrow_field(a) IS NOT NULL THEN arrow_field(a)['nullable'] IS NOT NULL END ORDER BY a; ---- 1 2 From 43e3d28b5764af5e2c976f09ba6bc9f3b115c147 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Wed, 28 Oct 2026 22:53:30 -0500 Subject: [PATCH 10/12] chore: use the SPDX license header in projection_inliner.rs Co-Authored-By: Claude Opus 5.5 --- .../optimizer/src/projection_inliner.rs | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/datafusion/optimizer/src/projection_inliner.rs b/datafusion/optimizer/src/projection_inliner.rs index a16c3b95b7aee..da8cb4a2832d5 100644 --- a/datafusion/optimizer/src/projection_inliner.rs +++ b/datafusion/optimizer/src/projection_inliner.rs @@ -1,19 +1,6 @@ -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership +// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements //! A single, cost-aware check for rewriting expressions through a projection. //! From d68d141b3355df54bbdf5afc9399de839034eac3 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Thu, 29 Oct 2026 21:47:00 -0500 Subject: [PATCH 11/12] refactor: replace DuplicationCost with volatility and a per rule choice DataFusion has no cost model, so the inliner does not derive one from ExpressionPlacement. A volatile call or a subquery is never evaluated at a second site. Otherwise each rule keeps its choice from main: OptimizeProjections and the leaf rules duplicate only leaf expressions, PushDownFilter duplicates any expression. Co-Authored-By: Claude Opus 5.5 --- .../optimizer/src/extract_leaf_expressions.rs | 4 +- .../optimizer/src/optimize_projections/mod.rs | 4 +- .../optimizer/src/projection_inliner.rs | 300 +++++++----------- datafusion/optimizer/src/push_down_filter.rs | 19 +- datafusion/sqllogictest/test_files/cse.slt | 8 +- 5 files changed, 136 insertions(+), 199 deletions(-) diff --git a/datafusion/optimizer/src/extract_leaf_expressions.rs b/datafusion/optimizer/src/extract_leaf_expressions.rs index 351a59c9d548c..f4a458bd9ad65 100644 --- a/datafusion/optimizer/src/extract_leaf_expressions.rs +++ b/datafusion/optimizer/src/extract_leaf_expressions.rs @@ -65,7 +65,7 @@ use datafusion_expr::logical_plan::LogicalPlan; use datafusion_expr::{Expr, ExpressionPlacement, Projection}; use crate::optimizer::ApplyOrder; -use crate::projection_inliner::{DuplicationCost, PinnedDefinitions, ProjectionInliner}; +use crate::projection_inliner::{MayDuplicate, PinnedDefinitions, ProjectionInliner}; use crate::push_down_filter::replace_cols_by_name; use crate::utils::{ColumnReference, has_all_column_refs, schema_columns}; use crate::{OptimizerConfig, OptimizerRule}; @@ -126,7 +126,7 @@ fn pinned_for_merge<'a>( direct_columns: &IndexSet, ) -> PinnedDefinitions { let direct: Vec = direct_columns.iter().cloned().map(Expr::Column).collect(); - inliner.pinned_with_unknown_consumers(inlined, &direct, DuplicationCost::Free) + inliner.pinned_with_unknown_consumers(inlined, &direct, MayDuplicate::LeafExpressions) } /// Extracts `MoveTowardsLeafNodes` sub-expressions from non-projection nodes diff --git a/datafusion/optimizer/src/optimize_projections/mod.rs b/datafusion/optimizer/src/optimize_projections/mod.rs index 78f373654a42b..89cadd1f92d00 100644 --- a/datafusion/optimizer/src/optimize_projections/mod.rs +++ b/datafusion/optimizer/src/optimize_projections/mod.rs @@ -34,7 +34,7 @@ use datafusion_expr::{ }; use crate::optimize_projections::required_indices::RequiredIndices; -use crate::projection_inliner::{DuplicationCost, ProjectionInliner}; +use crate::projection_inliner::{MayDuplicate, ProjectionInliner}; use crate::utils::NamePreserver; use datafusion_common::tree_node::{Transformed, TreeNode, TreeNodeContainer}; @@ -593,7 +593,7 @@ fn merge_consecutive_projections_one_level( // For details, see: https://github.com/apache/datafusion/issues/8296 let no_other_consumers: [&Expr; 0] = []; if !ProjectionInliner::new(prev_projection) - .pinned(&expr, no_other_consumers, DuplicationCost::Free) + .pinned(&expr, no_other_consumers, MayDuplicate::LeafExpressions) .is_empty() { // no change diff --git a/datafusion/optimizer/src/projection_inliner.rs b/datafusion/optimizer/src/projection_inliner.rs index da8cb4a2832d5..0cbe4d11a0aa8 100644 --- a/datafusion/optimizer/src/projection_inliner.rs +++ b/datafusion/optimizer/src/projection_inliner.rs @@ -2,7 +2,7 @@ // SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership // SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements -//! A single, cost-aware check for rewriting expressions through a projection. +//! A single check for rewriting expressions through a projection. //! //! Many rules move an expression that is written over the output of a //! projection below that projection, or merge it into the projection. To do @@ -14,10 +14,12 @@ //! - The consumer can reference the column more than one time. //! - The projection can stay in the plan and still compute the definition. //! -//! When the definition is volatile, this changes the query result. When the -//! definition is expensive, this makes the query slower. A typical example is a -//! column that [`CommonSubexprEliminate`] created on purpose, so that an -//! expensive expression is computed one time only. +//! When the definition is volatile, or is a subquery, this changes the query +//! result. That is never allowed. +//! +//! Otherwise a second evaluation only costs time. DataFusion does not know +//! what an expression costs, so the caller says what it may duplicate, see +//! [`MayDuplicate`]. Each rule keeps the choice it made before this module. //! //! [`ProjectionInliner`] is the one place that makes this decision. Rules call //! it instead of doing the substitution themselves. @@ -30,95 +32,52 @@ use datafusion_common::tree_node::{ Transformed, TransformedResult, TreeNode, TreeNodeRecursion, }; use datafusion_common::{Column, Result}; -use datafusion_expr::{Expr, ExpressionPlacement, Projection}; +use datafusion_expr::{Expr, Projection}; + +/// What a rule may evaluate more than one time, after it inlines a +/// definition. A definition that [`must_not_duplicate`] is never evaluated +/// more than one time, whatever the rule allows. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum MayDuplicate { + /// Only a definition that + /// [`should_push_to_leaves`](datafusion_expr::ExpressionPlacement::should_push_to_leaves): + /// a column, or a call such as struct field access that asks to move + /// towards the leaves. The merge guard of `OptimizeProjections` + /// () uses this rule. + LeafExpressions, + /// Any definition. `PushDownFilter` uses this rule: a filter that reaches + /// the scan can prune data, which is worth a second evaluation. + Any, +} -/// The cost of one more evaluation of an expression. -/// -/// The variants are in increasing order of cost. -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] -pub(crate) enum DuplicationCost { - /// Column references and expressions that report - /// [`ExpressionPlacement::MoveTowardsLeafNodes`] (for example struct field - /// access). These are free, or almost free, to evaluate again. - Free, - /// Built-in operators over free or cheap operands: arithmetic, - /// comparisons, `CAST`, `CASE`, `IS NULL`, and similar. They are - /// vectorized kernels, so an extra evaluation costs little. - /// - /// A bare literal is also in this class. This keeps the merge guard of - /// `OptimizeProjections` (issue #8296) unchanged. - Cheap, - /// Contains a scalar function that reports - /// [`ExpressionPlacement::KeepInPlace`] for column arguments, or a - /// higher-order function. This is the default for a user-defined - /// function, so the optimizer must assume that it can be arbitrarily - /// expensive. - Expensive, - /// Must never be evaluated more than one time: the expression is volatile - /// (for example `random()`) or contains a subquery. - Forbidden, +impl MayDuplicate { + fn allows(self, definition: &Expr) -> bool { + match self { + Self::LeafExpressions => definition.placement().should_push_to_leaves(), + Self::Any => true, + } + } } -impl DuplicationCost { - /// Returns the cost of one more evaluation of `expr`. - fn of(expr: &Expr) -> Self { - let expr = unalias(expr); - - // Volatility and subqueries are asked about first, before placement. - // A volatile function is free to report - // `ExpressionPlacement::MoveTowardsLeafNodes`, and `file_row_index()` - // does. Such a call is cheap to evaluate and still must never be - // evaluated at two sites, because the two sites give two different - // answers. Every kind of function node can be volatile, not only a - // scalar function (see [`Expr::is_volatile_node`]). - let mut cost = Self::Cheap; - expr.apply(|e| { - let node_cost = match e { - Expr::ScalarSubquery(_) +/// Returns `true` if a second evaluation of `expr` can change the query +/// result: `expr` contains a volatile call or a subquery. +/// +/// This is checked before placement. A volatile function is free to report +/// [`MoveTowardsLeafNodes`](datafusion_expr::ExpressionPlacement::MoveTowardsLeafNodes), +/// and `file_row_index()` does. +/// Every kind of function node can be volatile, not only a scalar function +/// (see [`Expr::is_volatile_node`]). +fn must_not_duplicate(expr: &Expr) -> bool { + expr.exists(|e| { + Ok(matches!( + e, + Expr::ScalarSubquery(_) | Expr::Exists(_) | Expr::InSubquery(_) - | Expr::SetComparison(_) => Self::Forbidden, - _ if e.is_volatile_node() => Self::Forbidden, - Expr::HigherOrderFunction(_) => Self::Expensive, - Expr::ScalarFunction(func) => { - // Ask the function for its own placement, as if its - // arguments were plain columns and literals. This isolates - // the cost of the function from the cost of its arguments - // (the walk visits the arguments separately). For example - // `get_field(CAST(s AS ...), 'f')` is a cheap field access - // over a cheap cast. - let arg_placements: Vec<_> = func - .args - .iter() - .map(|arg| match arg.placement() { - ExpressionPlacement::Literal => ExpressionPlacement::Literal, - _ => ExpressionPlacement::Column, - }) - .collect(); - match func.func.placement(&arg_placements) { - ExpressionPlacement::KeepInPlace => Self::Expensive, - _ => Self::Cheap, - } - } - _ => Self::Cheap, - }; - cost = cost.max(node_cost); - Ok(if cost == Self::Forbidden { - TreeNodeRecursion::Stop - } else { - TreeNodeRecursion::Continue - }) - }) - .expect("traversal is infallible"); - - if cost == Self::Forbidden { - return Self::Forbidden; - } - if expr.placement().should_push_to_leaves() { - return Self::Free; - } - cost - } + | Expr::SetComparison(_) + ) || e.is_volatile_node()) + }) + .expect("exists closure is infallible") } /// Definitions that [`ProjectionInliner::pinned`] refuses to inline. @@ -136,13 +95,11 @@ impl PinnedDefinitions { /// too many times. /// /// The rule is: after the rewrite, a definition that is evaluated more than -/// one time must have a [`DuplicationCost`] of at most the `max_cost` that the -/// caller gives. See [`ProjectionInliner::pinned`]. +/// one time must not [`must_not_duplicate`], and the caller's +/// [`MayDuplicate`] must allow it. See [`ProjectionInliner::pinned`]. #[derive(Debug)] pub(crate) struct ProjectionInliner<'a> { - /// Definitions without top-level alias, in output column order. Their - /// [`DuplicationCost`] is computed only for the definitions that a check - /// references. + /// Definitions without top-level alias, in output column order. definitions: Vec<&'a Expr>, /// Output column -> index into `definitions` by_column: HashMap, @@ -204,19 +161,19 @@ impl<'a> ProjectionInliner<'a> { /// from the projection (for example by `OptimizeProjections`). /// /// A definition that `inlined` references is pinned if it is then - /// evaluated more than one time and its [`DuplicationCost`] is more than - /// `max_cost`. + /// evaluated more than one time, and either [`must_not_duplicate`] holds + /// or `may_duplicate` does not allow it. pub(crate) fn pinned<'i, 'o>( &self, inlined: impl IntoIterator, others: impl IntoIterator, - max_cost: DuplicationCost, + may_duplicate: MayDuplicate, ) -> PinnedDefinitions { - self.pinned_impl(inlined, others, max_cost, false) + self.pinned_impl(inlined, others, may_duplicate, false) } - /// Returns [`Self::pinned`], and also pins every - /// [`DuplicationCost::Forbidden`] definition that `inlined` references. + /// Returns [`Self::pinned`], and also pins every definition that + /// `inlined` references and that [`must_not_duplicate`]. /// /// Use this when the projection can stay in the plan for consumers that /// the caller can not see. A copy of a forbidden definition would then @@ -226,22 +183,18 @@ impl<'a> ProjectionInliner<'a> { &self, inlined: impl IntoIterator, others: impl IntoIterator, - max_cost: DuplicationCost, + may_duplicate: MayDuplicate, ) -> PinnedDefinitions { - self.pinned_impl(inlined, others, max_cost, true) + self.pinned_impl(inlined, others, may_duplicate, true) } fn pinned_impl<'i, 'o>( &self, inlined: impl IntoIterator, others: impl IntoIterator, - max_cost: DuplicationCost, + may_duplicate: MayDuplicate, pin_forbidden: bool, ) -> PinnedDefinitions { - debug_assert!( - max_cost < DuplicationCost::Forbidden, - "a forbidden definition must never be duplicated" - ); let inlined_references = self.count_references(inlined); if inlined_references.is_empty() { return PinnedDefinitions::default(); @@ -254,9 +207,11 @@ impl<'a> ProjectionInliner<'a> { if evaluations <= 1 && !pin_forbidden { return false; } - let cost = DuplicationCost::of(self.definitions[*idx]); - (pin_forbidden && cost == DuplicationCost::Forbidden) - || (evaluations > 1 && cost > max_cost) + let definition = self.definitions[*idx]; + if must_not_duplicate(definition) { + return true; + } + evaluations > 1 && !may_duplicate.allows(definition) }) .map(|(idx, _)| idx) .collect(); @@ -284,7 +239,7 @@ impl<'a> ProjectionInliner<'a> { } /// Replaces each column reference in `expr` with its definition, without a - /// cost check. Callers must check [`Self::pinned`] first. + /// check. Callers must check [`Self::pinned`] first. pub(crate) fn substitute(&self, expr: Expr) -> Result { expr.transform_up(|e| { if let Expr::Column(col) = &e @@ -334,7 +289,8 @@ mod tests { use crate::test::udfs::{PlacementTestUDF, get_field_like}; use arrow::datatypes::DataType; use datafusion_expr::{ - LogicalPlan, LogicalPlanBuilder, ScalarUDF, Volatility, cast, col, lit, + ExpressionPlacement, LogicalPlan, LogicalPlanBuilder, ScalarUDF, Volatility, + cast, col, lit, }; fn udf(placement: ExpressionPlacement, volatility: Volatility) -> ScalarUDF { @@ -377,102 +333,84 @@ mod tests { } #[test] - fn duplication_cost_classification() { - use DuplicationCost::*; - assert_eq!(DuplicationCost::of(&col("a")), Free); - assert_eq!(DuplicationCost::of(&get_field_like(col("a"), "x")), Free); - assert_eq!(DuplicationCost::of(&lit(1)), Cheap); - assert_eq!(DuplicationCost::of(&(col("a") * lit(2))), Cheap); - assert_eq!( - DuplicationCost::of(&cast(col("a"), DataType::Int64).alias("x")), - Cheap - ); - // A field access over a cheap operand is still cheap. - assert_eq!( - DuplicationCost::of(&get_field_like(col("a") + lit(1), "x")), - Cheap - ); - assert_eq!(DuplicationCost::of(&expensive(col("a"))), Expensive); - assert_eq!( - DuplicationCost::of(&get_field_like(expensive(col("a")), "x")), - Expensive - ); - assert_eq!( - DuplicationCost::of(&(expensive(col("a")) + lit(1))), - Expensive - ); - assert_eq!( - DuplicationCost::of(&(volatile(col("a")) + lit(1))), - Forbidden - ); + fn must_not_duplicate_volatile_calls_and_subqueries() { + assert!(!must_not_duplicate(&col("a"))); + assert!(!must_not_duplicate(&expensive(col("a")))); + assert!(must_not_duplicate(&(volatile(col("a")) + lit(1)))); + assert!(must_not_duplicate(&get_field_like(volatile(col("a")), "x"))); } /// Volatility beats placement. A volatile function that reports - /// `MoveTowardsLeafNodes` is cheap to evaluate, and two evaluations still - /// give two different answers. + /// `MoveTowardsLeafNodes` still gives two different answers. #[test] - fn a_volatile_leaf_function_is_forbidden() { - use DuplicationCost::*; - assert_eq!(DuplicationCost::of(&volatile_leaf(col("a"))), Forbidden); - assert_eq!( - DuplicationCost::of(&volatile_leaf(col("a")).alias("x")), - Forbidden - ); - assert_eq!( - DuplicationCost::of(&get_field_like(volatile_leaf(col("a")), "x")), - Forbidden - ); - // The same function, not volatile, is free. - assert_eq!( - DuplicationCost::of( + fn a_volatile_leaf_function_must_not_be_duplicated() { + assert!(must_not_duplicate(&volatile_leaf(col("a")))); + assert!(must_not_duplicate(&volatile_leaf(col("a")).alias("x"))); + assert!( + MayDuplicate::LeafExpressions.allows( &udf( ExpressionPlacement::MoveTowardsLeafNodes, Volatility::Immutable ) .call(vec![col("a")]) - ), - Free + ) ); } + #[test] + fn may_duplicate_rules() { + use MayDuplicate::*; + assert!(LeafExpressions.allows(&col("a"))); + assert!(LeafExpressions.allows(&get_field_like(col("a"), "x"))); + assert!(!LeafExpressions.allows(&lit(1))); + assert!(!LeafExpressions.allows(&(col("a") * lit(2)))); + assert!(!LeafExpressions.allows(&cast(col("a"), DataType::Int64))); + assert!(!LeafExpressions.allows(&expensive(col("a")))); + assert!(Any.allows(&expensive(col("a")))); + } + #[test] fn pinned_counts_evaluations() { - use DuplicationCost::*; - let p = projection(vec![ - col("a"), - (col("b") + lit(1)).alias("cheap"), - expensive(col("c")).alias("exp"), - ]); + use MayDuplicate::*; + let p = projection(vec![col("a"), expensive(col("c")).alias("exp")]); let inliner = ProjectionInliner::new(&p); let no_others: [&Expr; 0] = []; - // One reference of an expensive definition: allowed. + // One reference: allowed. let one = get_field_like(col("exp"), "x"); - assert!(inliner.pinned([&one], no_others, Free).is_empty()); + assert!( + inliner + .pinned([&one], no_others, LeafExpressions) + .is_empty() + ); // The same reference when another consumer keeps the column: refused, - // unless the caller accepts expensive duplicates. - assert!(!inliner.pinned([&one], [&col("exp")], Cheap).is_empty()); - assert!(inliner.pinned([&one], [&col("exp")], Expensive).is_empty()); - // Two references in one expression: refused unless allowed. + // unless the caller allows any duplicate. + assert!( + !inliner + .pinned([&one], [&col("exp")], LeafExpressions) + .is_empty() + ); + assert!(inliner.pinned([&one], [&col("exp")], Any).is_empty()); + // Two references in one expression. let two = col("exp") .is_not_null() .and(get_field_like(col("exp"), "x")); - assert!(!inliner.pinned([&two], no_others, Cheap).is_empty()); - assert!(inliner.pinned([&two], no_others, Expensive).is_empty()); + assert!( + !inliner + .pinned([&two], no_others, LeafExpressions) + .is_empty() + ); + assert!(inliner.pinned([&two], no_others, Any).is_empty()); // References are counted across all inlined expressions. assert!( !inliner - .pinned([&col("exp"), &col("exp")], no_others, Cheap) + .pinned([&col("exp"), &col("exp")], no_others, LeafExpressions) .is_empty() ); - // Cheap definitions follow `max_cost`. - let cheap = col("cheap").gt(lit(5)); - assert!(inliner.pinned([&cheap], [&col("cheap")], Cheap).is_empty()); - assert!(!inliner.pinned([&cheap], [&col("cheap")], Free).is_empty()); - // Free definitions are always allowed. + // A column is always allowed. assert!( inliner - .pinned([&col("a"), &col("a")], [&col("a")], Free) + .pinned([&col("a"), &col("a")], [&col("a")], LeafExpressions) .is_empty() ); // An unqualified reference resolves to a qualified output column. @@ -489,7 +427,7 @@ mod tests { let p = projection(vec![volatile(col("a")).alias("r")]); let inliner = ProjectionInliner::new(&p); let no_others: [&Expr; 0] = []; - let max = DuplicationCost::Expensive; + let max = MayDuplicate::Any; assert!(inliner.pinned([&col("r")], no_others, max).is_empty()); assert!( !inliner @@ -522,7 +460,7 @@ mod tests { let pinned = inliner.pinned_with_unknown_consumers( [&twice, &other], no_others, - DuplicationCost::Cheap, + MayDuplicate::LeafExpressions, ); assert!(inliner.references_pinned(&twice, &pinned)); assert!(!inliner.references_pinned(&other, &pinned)); diff --git a/datafusion/optimizer/src/push_down_filter.rs b/datafusion/optimizer/src/push_down_filter.rs index 2c84efaa8f94c..73a917b57a1a8 100644 --- a/datafusion/optimizer/src/push_down_filter.rs +++ b/datafusion/optimizer/src/push_down_filter.rs @@ -56,7 +56,7 @@ use datafusion_expr::{ use crate::extract_leaf_expressions::is_pure_extraction_projection; use crate::optimizer::ApplyOrder; -use crate::projection_inliner::{DuplicationCost, ProjectionInliner, unalias}; +use crate::projection_inliner::{MayDuplicate, ProjectionInliner, unalias}; use crate::simplify_expressions::{reorder_predicates, simplify_predicates}; use crate::utils::{ ColumnReference, has_all_column_refs, is_restrict_null_predicate, schema_columns, @@ -1582,7 +1582,7 @@ fn rewrite_projection( .filter(|(_, kept)| !**kept) .map(|(expr, _)| expr), no_other_consumers, - DuplicationCost::Cheap, + MayDuplicate::Any, ); let mut push_predicates = vec![]; let mut keep_predicates = vec![]; @@ -4968,12 +4968,12 @@ mod tests { .call(vec![arg]) } - /// Issue #25329: `CommonSubexprEliminate` computes `c1` one time for - /// predicates that reference it twice. Pushing the predicates would compute - /// it twice, and CSE would then create the projection again below the - /// filter, so the two rules never converge. + /// A non-volatile definition that the predicates reference two times is + /// pushed, as on `main`: DataFusion does not know what `c1` costs, and a + /// predicate that reaches the scan can prune data. + /// `CommonSubexprEliminate` then computes it one time again. #[test] - fn filter_with_repeated_expensive_reference_not_pushed_through_projection() + fn filter_with_repeated_non_volatile_reference_pushed_through_projection() -> Result<()> { let plan = LogicalPlanBuilder::from(test_table_scan()?) .project(vec![keep_in_place_udf(col("a")).alias("c1"), col("b")])? @@ -4988,9 +4988,8 @@ mod tests { assert_optimized_plan_equal!( plan, @r#" - Filter: c1 IS NOT NULL AND get_field_like(c1, Utf8("x")) IS NULL - Projection: keep_in_place_udf(test.a) AS c1, test.b - TableScan: test, full_filters=[test.b > Int32(1)] + Projection: keep_in_place_udf(test.a) AS c1, test.b + TableScan: test, full_filters=[test.b > Int32(1), keep_in_place_udf(test.a) IS NOT NULL, get_field_like(keep_in_place_udf(test.a), Utf8("x")) IS NULL] "# ) } diff --git a/datafusion/sqllogictest/test_files/cse.slt b/datafusion/sqllogictest/test_files/cse.slt index 7b4fb43b293ca..ebeb7768bc8ef 100644 --- a/datafusion/sqllogictest/test_files/cse.slt +++ b/datafusion/sqllogictest/test_files/cse.slt @@ -388,12 +388,12 @@ EXPLAIN SELECT a FROM cse_keep_in_place WHERE CASE WHEN arrow_field(a) IS NOT NU ---- logical_plan 01)Projection: cse_keep_in_place.a -02)--Filter: CASE WHEN __common_expr_1 IS NOT NULL THEN get_field(__common_expr_1, Utf8("nullable")) IS NOT NULL END -03)----Projection: arrow_field(cse_keep_in_place.a) AS __common_expr_1, cse_keep_in_place.a +02)--Filter: CASE WHEN __common_expr_3 IS NOT NULL THEN get_field(__common_expr_3, Utf8("nullable")) IS NOT NULL END +03)----Projection: arrow_field(cse_keep_in_place.a) AS __common_expr_3, cse_keep_in_place.a 04)------TableScan: cse_keep_in_place projection=[a] physical_plan -01)FilterExec: CASE WHEN __common_expr_1@0 IS NOT NULL THEN get_field(__common_expr_1@0, nullable) IS NOT NULL END, projection=[a@1] -02)--ProjectionExec: expr=[arrow_field(a@0) as __common_expr_1, a@0 as a] +01)FilterExec: CASE WHEN __common_expr_3@0 IS NOT NULL THEN get_field(__common_expr_3@0, nullable) IS NOT NULL END, projection=[a@1] +02)--ProjectionExec: expr=[arrow_field(a@0) as __common_expr_3, a@0 as a] 03)----DataSourceExec: partitions=1, partition_sizes=[1] query I From b370e1801fcc9f8f0cfa23bcdd566db105113a3a Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Thu, 29 Oct 2026 21:55:19 -0500 Subject: [PATCH 12/12] refactor: drop MayDuplicate, keep PushDownFilter as on main Review follow-up. PushDownFilter is unchanged from main. The inliner has one rule: a volatile definition is never duplicated, and only a column or a leaf expression is evaluated more than one time. Stale cost-model text removed; a test for extraction next to a volatile column added. Co-Authored-By: Claude Opus 5.5 --- .../optimizer/src/extract_leaf_expressions.rs | 50 ++++- .../optimizer/src/optimize_projections/mod.rs | 4 +- .../optimizer/src/projection_inliner.rs | 196 ++++++------------ datafusion/optimizer/src/push_down_filter.rs | 190 ++++------------- datafusion/sqllogictest/test_files/cse.slt | 3 + 5 files changed, 147 insertions(+), 296 deletions(-) diff --git a/datafusion/optimizer/src/extract_leaf_expressions.rs b/datafusion/optimizer/src/extract_leaf_expressions.rs index f4a458bd9ad65..1ec862e2aed00 100644 --- a/datafusion/optimizer/src/extract_leaf_expressions.rs +++ b/datafusion/optimizer/src/extract_leaf_expressions.rs @@ -65,7 +65,7 @@ use datafusion_expr::logical_plan::LogicalPlan; use datafusion_expr::{Expr, ExpressionPlacement, Projection}; use crate::optimizer::ApplyOrder; -use crate::projection_inliner::{MayDuplicate, PinnedDefinitions, ProjectionInliner}; +use crate::projection_inliner::{PinnedDefinitions, ProjectionInliner}; use crate::push_down_filter::replace_cols_by_name; use crate::utils::{ColumnReference, has_all_column_refs, schema_columns}; use crate::{OptimizerConfig, OptimizerRule}; @@ -98,8 +98,8 @@ fn has_extractable_expr(exprs: &[Expr]) -> bool { /// projection. The merge keeps every expression of the input and resolves the /// column references of the extracted expressions to their definitions (see /// [`build_extraction_projection_impl`]). The inliner decides if that is -/// allowed: after the merge, a definition that is not free to copy must be -/// evaluated one time only. For example: +/// allowed: after the merge, a definition that is not a column or a leaf +/// expression must be evaluated one time only. For example: /// /// ```text /// Filter: __common_expr_1 IS NOT NULL AND get_field(__common_expr_1, 'a') = 1 @@ -126,7 +126,7 @@ fn pinned_for_merge<'a>( direct_columns: &IndexSet, ) -> PinnedDefinitions { let direct: Vec = direct_columns.iter().cloned().map(Expr::Column).collect(); - inliner.pinned_with_unknown_consumers(inlined, &direct, MayDuplicate::LeafExpressions) + inliner.pinned_with_unknown_consumers(inlined, &direct) } /// Extracts `MoveTowardsLeafNodes` sub-expressions from non-projection nodes @@ -2330,6 +2330,41 @@ mod tests { "#) } + /// A predicate that reads a volatile column does not stop the extraction + /// of a leaf expression from the other predicates. The volatile column is + /// not inlined. + #[test] + fn test_extraction_next_to_a_volatile_column() -> Result<()> { + let table_scan = test_table_scan_with_struct()?; + let plan = LogicalPlanBuilder::from(table_scan) + .project(vec![col("user"), volatile_udf(col("id")).alias("v")])? + .filter( + leaf_udf(col("user"), "status") + .eq(lit("active")) + .and(col("v").is_not_null()), + )? + .build()?; + + assert_stages!(plan, @r#" + ## Original Plan + Filter: leaf_udf(test.user, Utf8("status")) = Utf8("active") AND v IS NOT NULL + Projection: test.user, keep_in_place_udf(test.id) AS v + TableScan: test projection=[id, user] + + ## After Extraction + Projection: test.user, v + Filter: __datafusion_extracted_1 = Utf8("active") AND v IS NOT NULL + Projection: test.user, keep_in_place_udf(test.id) AS v, leaf_udf(test.user, Utf8("status")) AS __datafusion_extracted_1 + TableScan: test projection=[id, user] + + ## After Pushdown + (same as after extraction) + + ## Optimized + (same as after pushdown) + "#) + } + /// Projections with aliased columns (nothing to extract) return unchanged. #[test] fn test_projection_early_return_no_extraction() -> Result<()> { @@ -4010,10 +4045,9 @@ mod tests { } /// `pinned_for_merge` must block a merge only when it would inline one - /// `KeepInPlace` definition into more than one reference site. - /// It must count reference sites for `KeepInPlace` columns alone: a plain - /// column is cheap to duplicate, and a merge that touches only plain - /// columns must still go ahead. + /// definition that is not a leaf expression into more than one reference + /// site. A plain column may be duplicated, and a merge that touches only + /// plain columns must still go ahead. #[test] fn test_pinned_for_merge_counts_only_kept_columns() -> Result<()> { let scan = test_table_scan_with_struct()?; diff --git a/datafusion/optimizer/src/optimize_projections/mod.rs b/datafusion/optimizer/src/optimize_projections/mod.rs index 11aac6da3adfb..f7fc79338bcac 100644 --- a/datafusion/optimizer/src/optimize_projections/mod.rs +++ b/datafusion/optimizer/src/optimize_projections/mod.rs @@ -34,7 +34,7 @@ use datafusion_expr::{ }; use crate::optimize_projections::required_indices::RequiredIndices; -use crate::projection_inliner::{MayDuplicate, ProjectionInliner}; +use crate::projection_inliner::ProjectionInliner; use crate::utils::NamePreserver; use datafusion_common::tree_node::{Transformed, TreeNode, TreeNodeContainer}; @@ -593,7 +593,7 @@ fn merge_consecutive_projections_one_level( // For details, see: https://github.com/apache/datafusion/issues/8296 let no_other_consumers: [&Expr; 0] = []; if !ProjectionInliner::new(prev_projection) - .pinned(&expr, no_other_consumers, MayDuplicate::LeafExpressions) + .pinned(&expr, no_other_consumers) .is_empty() { // no change diff --git a/datafusion/optimizer/src/projection_inliner.rs b/datafusion/optimizer/src/projection_inliner.rs index 0cbe4d11a0aa8..f5594d5554902 100644 --- a/datafusion/optimizer/src/projection_inliner.rs +++ b/datafusion/optimizer/src/projection_inliner.rs @@ -14,17 +14,20 @@ //! - The consumer can reference the column more than one time. //! - The projection can stay in the plan and still compute the definition. //! -//! When the definition is volatile, or is a subquery, this changes the query -//! result. That is never allowed. +//! When the definition is volatile, this changes the query result. That is +//! never allowed. //! -//! Otherwise a second evaluation only costs time. DataFusion does not know -//! what an expression costs, so the caller says what it may duplicate, see -//! [`MayDuplicate`]. Each rule keeps the choice it made before this module. +//! Otherwise a second evaluation only costs time, and DataFusion does not +//! know what an expression costs. So only a definition that +//! [`should_push_to_leaves`] is evaluated more than one time: a column, or a +//! call such as struct field access that asks to move towards the leaves. +//! This is the merge guard of `OptimizeProjections` +//! (). //! +//! [`should_push_to_leaves`]: datafusion_expr::ExpressionPlacement::should_push_to_leaves //! [`ProjectionInliner`] is the one place that makes this decision. Rules call //! it instead of doing the substitution themselves. //! -//! [`CommonSubexprEliminate`]: crate::common_subexpr_eliminate::CommonSubexprEliminate use std::collections::{HashMap, HashSet}; @@ -34,50 +37,19 @@ use datafusion_common::tree_node::{ use datafusion_common::{Column, Result}; use datafusion_expr::{Expr, Projection}; -/// What a rule may evaluate more than one time, after it inlines a -/// definition. A definition that [`must_not_duplicate`] is never evaluated -/// more than one time, whatever the rule allows. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) enum MayDuplicate { - /// Only a definition that - /// [`should_push_to_leaves`](datafusion_expr::ExpressionPlacement::should_push_to_leaves): - /// a column, or a call such as struct field access that asks to move - /// towards the leaves. The merge guard of `OptimizeProjections` - /// () uses this rule. - LeafExpressions, - /// Any definition. `PushDownFilter` uses this rule: a filter that reaches - /// the scan can prune data, which is worth a second evaluation. - Any, -} - -impl MayDuplicate { - fn allows(self, definition: &Expr) -> bool { - match self { - Self::LeafExpressions => definition.placement().should_push_to_leaves(), - Self::Any => true, - } - } -} - /// Returns `true` if a second evaluation of `expr` can change the query -/// result: `expr` contains a volatile call or a subquery. +/// result. /// /// This is checked before placement. A volatile function is free to report /// [`MoveTowardsLeafNodes`](datafusion_expr::ExpressionPlacement::MoveTowardsLeafNodes), /// and `file_row_index()` does. -/// Every kind of function node can be volatile, not only a scalar function -/// (see [`Expr::is_volatile_node`]). fn must_not_duplicate(expr: &Expr) -> bool { - expr.exists(|e| { - Ok(matches!( - e, - Expr::ScalarSubquery(_) - | Expr::Exists(_) - | Expr::InSubquery(_) - | Expr::SetComparison(_) - ) || e.is_volatile_node()) - }) - .expect("exists closure is infallible") + expr.is_volatile() +} + +/// Returns `true` if `expr` may be evaluated more than one time. +fn may_duplicate(expr: &Expr) -> bool { + !must_not_duplicate(expr) && expr.placement().should_push_to_leaves() } /// Definitions that [`ProjectionInliner::pinned`] refuses to inline. @@ -95,8 +67,8 @@ impl PinnedDefinitions { /// too many times. /// /// The rule is: after the rewrite, a definition that is evaluated more than -/// one time must not [`must_not_duplicate`], and the caller's -/// [`MayDuplicate`] must allow it. See [`ProjectionInliner::pinned`]. +/// one time must be a column or a leaf expression, and must not be volatile. +/// See [`ProjectionInliner::pinned`]. #[derive(Debug)] pub(crate) struct ProjectionInliner<'a> { /// Definitions without top-level alias, in output column order. @@ -161,15 +133,13 @@ impl<'a> ProjectionInliner<'a> { /// from the projection (for example by `OptimizeProjections`). /// /// A definition that `inlined` references is pinned if it is then - /// evaluated more than one time, and either [`must_not_duplicate`] holds - /// or `may_duplicate` does not allow it. + /// evaluated more than one time and [`may_duplicate`] does not hold. pub(crate) fn pinned<'i, 'o>( &self, inlined: impl IntoIterator, others: impl IntoIterator, - may_duplicate: MayDuplicate, ) -> PinnedDefinitions { - self.pinned_impl(inlined, others, may_duplicate, false) + self.pinned_impl(inlined, others, false) } /// Returns [`Self::pinned`], and also pins every definition that @@ -183,16 +153,14 @@ impl<'a> ProjectionInliner<'a> { &self, inlined: impl IntoIterator, others: impl IntoIterator, - may_duplicate: MayDuplicate, ) -> PinnedDefinitions { - self.pinned_impl(inlined, others, may_duplicate, true) + self.pinned_impl(inlined, others, true) } fn pinned_impl<'i, 'o>( &self, inlined: impl IntoIterator, others: impl IntoIterator, - may_duplicate: MayDuplicate, pin_forbidden: bool, ) -> PinnedDefinitions { let inlined_references = self.count_references(inlined); @@ -208,10 +176,8 @@ impl<'a> ProjectionInliner<'a> { return false; } let definition = self.definitions[*idx]; - if must_not_duplicate(definition) { - return true; - } - evaluations > 1 && !may_duplicate.allows(definition) + (pin_forbidden && must_not_duplicate(definition)) + || (evaluations > 1 && !may_duplicate(definition)) }) .map(|(idx, _)| idx) .collect(); @@ -301,7 +267,7 @@ mod tests { ) } - fn expensive(arg: Expr) -> Expr { + fn keep_in_place(arg: Expr) -> Expr { udf(ExpressionPlacement::KeepInPlace, Volatility::Immutable).call(vec![arg]) } @@ -333,84 +299,51 @@ mod tests { } #[test] - fn must_not_duplicate_volatile_calls_and_subqueries() { - assert!(!must_not_duplicate(&col("a"))); - assert!(!must_not_duplicate(&expensive(col("a")))); - assert!(must_not_duplicate(&(volatile(col("a")) + lit(1)))); - assert!(must_not_duplicate(&get_field_like(volatile(col("a")), "x"))); + fn may_duplicate_columns_and_leaf_expressions() { + assert!(may_duplicate(&col("a"))); + assert!(may_duplicate(&get_field_like(col("a"), "x"))); + assert!(!may_duplicate(&lit(1))); + assert!(!may_duplicate(&(col("a") * lit(2)))); + assert!(!may_duplicate(&cast(col("a"), DataType::Int64))); + assert!(!may_duplicate(&keep_in_place(col("a")))); + assert!(!may_duplicate(&get_field_like(volatile(col("a")), "x"))); } /// Volatility beats placement. A volatile function that reports /// `MoveTowardsLeafNodes` still gives two different answers. #[test] fn a_volatile_leaf_function_must_not_be_duplicated() { - assert!(must_not_duplicate(&volatile_leaf(col("a")))); - assert!(must_not_duplicate(&volatile_leaf(col("a")).alias("x"))); - assert!( - MayDuplicate::LeafExpressions.allows( - &udf( - ExpressionPlacement::MoveTowardsLeafNodes, - Volatility::Immutable - ) - .call(vec![col("a")]) + assert!(!may_duplicate(&volatile_leaf(col("a")))); + assert!(!may_duplicate(&volatile_leaf(col("a")).alias("x"))); + assert!(may_duplicate( + &udf( + ExpressionPlacement::MoveTowardsLeafNodes, + Volatility::Immutable ) - ); - } - - #[test] - fn may_duplicate_rules() { - use MayDuplicate::*; - assert!(LeafExpressions.allows(&col("a"))); - assert!(LeafExpressions.allows(&get_field_like(col("a"), "x"))); - assert!(!LeafExpressions.allows(&lit(1))); - assert!(!LeafExpressions.allows(&(col("a") * lit(2)))); - assert!(!LeafExpressions.allows(&cast(col("a"), DataType::Int64))); - assert!(!LeafExpressions.allows(&expensive(col("a")))); - assert!(Any.allows(&expensive(col("a")))); + .call(vec![col("a")]) + )); } #[test] fn pinned_counts_evaluations() { - use MayDuplicate::*; - let p = projection(vec![col("a"), expensive(col("c")).alias("exp")]); + let p = projection(vec![col("a"), keep_in_place(col("c")).alias("k")]); let inliner = ProjectionInliner::new(&p); let no_others: [&Expr; 0] = []; // One reference: allowed. - let one = get_field_like(col("exp"), "x"); - assert!( - inliner - .pinned([&one], no_others, LeafExpressions) - .is_empty() - ); - // The same reference when another consumer keeps the column: refused, - // unless the caller allows any duplicate. - assert!( - !inliner - .pinned([&one], [&col("exp")], LeafExpressions) - .is_empty() - ); - assert!(inliner.pinned([&one], [&col("exp")], Any).is_empty()); - // Two references in one expression. - let two = col("exp") - .is_not_null() - .and(get_field_like(col("exp"), "x")); - assert!( - !inliner - .pinned([&two], no_others, LeafExpressions) - .is_empty() - ); - assert!(inliner.pinned([&two], no_others, Any).is_empty()); + let one = get_field_like(col("k"), "x"); + assert!(inliner.pinned([&one], no_others).is_empty()); + // The same reference when another consumer keeps the column: refused. + assert!(!inliner.pinned([&one], [&col("k")]).is_empty()); + // Two references in one expression: refused. + let two = col("k").is_not_null().and(get_field_like(col("k"), "x")); + assert!(!inliner.pinned([&two], no_others).is_empty()); // References are counted across all inlined expressions. - assert!( - !inliner - .pinned([&col("exp"), &col("exp")], no_others, LeafExpressions) - .is_empty() - ); + assert!(!inliner.pinned([&col("k"), &col("k")], no_others).is_empty()); // A column is always allowed. assert!( inliner - .pinned([&col("a"), &col("a")], [&col("a")], LeafExpressions) + .pinned([&col("a"), &col("a")], [&col("a")]) .is_empty() ); // An unqualified reference resolves to a qualified output column. @@ -427,18 +360,17 @@ mod tests { let p = projection(vec![volatile(col("a")).alias("r")]); let inliner = ProjectionInliner::new(&p); let no_others: [&Expr; 0] = []; - let max = MayDuplicate::Any; - assert!(inliner.pinned([&col("r")], no_others, max).is_empty()); + assert!(inliner.pinned([&col("r")], no_others).is_empty()); assert!( !inliner - .pinned([&(col("r") + col("r"))], no_others, max) + .pinned([&(col("r") + col("r"))], no_others) .is_empty() ); - assert!(!inliner.pinned([&col("r")], [&col("r")], max).is_empty()); + assert!(!inliner.pinned([&col("r")], [&col("r")]).is_empty()); // Without all the consumers, a single reference is pinned as well. assert!( !inliner - .pinned_with_unknown_consumers([&col("r")], no_others, max) + .pinned_with_unknown_consumers([&col("r")], no_others) .is_empty() ); } @@ -447,27 +379,21 @@ mod tests { fn substitute_and_references_pinned() -> Result<()> { let p = projection(vec![ col("a"), - expensive(col("c")).alias("exp"), - (col("b") + lit(1)).alias("cheap"), + keep_in_place(col("c")).alias("k"), + (col("b") + lit(1)).alias("b1"), ]); let inliner = ProjectionInliner::new(&p); let no_others: [&Expr; 0] = []; - let twice = col("exp") - .is_not_null() - .and(get_field_like(col("exp"), "x")); - let other = col("cheap").gt(col("a")); - let pinned = inliner.pinned_with_unknown_consumers( - [&twice, &other], - no_others, - MayDuplicate::LeafExpressions, - ); + let twice = col("k").is_not_null().and(get_field_like(col("k"), "x")); + let other = col("b1").gt(col("a")); + let pinned = inliner.pinned_with_unknown_consumers([&twice, &other], no_others); assert!(inliner.references_pinned(&twice, &pinned)); assert!(!inliner.references_pinned(&other, &pinned)); assert_eq!( - inliner.definition(&Column::from_name("exp")), - Some(&expensive(col("test.c"))) + inliner.definition(&Column::from_name("k")), + Some(&keep_in_place(col("test.c"))) ); assert_eq!( inliner.substitute(other)?, diff --git a/datafusion/optimizer/src/push_down_filter.rs b/datafusion/optimizer/src/push_down_filter.rs index 73a917b57a1a8..ab3ee7a68bbe2 100644 --- a/datafusion/optimizer/src/push_down_filter.rs +++ b/datafusion/optimizer/src/push_down_filter.rs @@ -56,7 +56,6 @@ use datafusion_expr::{ use crate::extract_leaf_expressions::is_pure_extraction_projection; use crate::optimizer::ApplyOrder; -use crate::projection_inliner::{MayDuplicate, ProjectionInliner, unalias}; use crate::simplify_expressions::{reorder_predicates, simplify_predicates}; use crate::utils::{ ColumnReference, has_all_column_refs, is_restrict_null_predicate, schema_columns, @@ -1545,49 +1544,33 @@ fn rewrite_projection( )); } - let inliner = ProjectionInliner::new(&projection); - - // A predicate is kept above the projection when it references: - // - a volatile definition. The projection still computes it, so a second - // evaluation below would see a different value. - // - an expensive definition that the predicates reference more than one - // time. Pushing would evaluate it more than one time, and - // `CommonSubexprEliminate` would create a projection for it again below - // the filter, so the two rules would never converge (issue #25329). A - // single reference is pushed: if the columns above still use the - // definition, that is the usual cost of a filter push-down. - // - a `MoveTowardsLeafNodes` definition (a cheap expression like - // `get_field`). Re-inlining such an expression into a filter causes - // optimizer instability: `ExtractLeafExpressions` undoes the push-down, - // which creates a loop that runs until the iteration limit is hit. + // Partition projection expressions into non-pushable vs pushable. + // Non-pushable expressions are volatile (must not be duplicated) or + // MoveTowardsLeafNodes (cheap expressions like get_field where re-inlining + // into a filter causes optimizer instability — ExtractLeafExpressions will + // undo the push-down, creating an infinite loop that runs until the + // iteration limit is hit). // - // The references are counted over the predicates that can be pushed - // only: a predicate that is kept above reads the column that the - // projection computes, so it does not evaluate the definition again. - let references_leaf_expr: Vec = predicates + // Keyed by the output column rather than its flat name, for the reason + // given on `replace_cols_by_column`. `EliminateGroupByConstant` can leave a + // projection that outputs both `t.a` and `CAST(t.a AS ...)` under that one + // name. + let (non_pushable_map, pushable_map): (HashMap<_, _>, HashMap<_, _>) = projection + .schema .iter() - .map(|expr| { - expr.column_refs().into_iter().any(|col| { - inliner.definition(col).is_some_and(|definition| { - definition.placement() == ExpressionPlacement::MoveTowardsLeafNodes - }) - }) + .zip(projection.expr.iter()) + .map(|((qualifier, field), expr)| { + (Column::new(qualifier.cloned(), field.name()), unalias(expr)) }) - .collect(); - let no_other_consumers: [&Expr; 0] = []; - let pinned = inliner.pinned_with_unknown_consumers( - predicates - .iter() - .zip(&references_leaf_expr) - .filter(|(_, kept)| !**kept) - .map(|(expr, _)| expr), - no_other_consumers, - MayDuplicate::Any, - ); + .partition(|(_, value)| { + value.is_volatile() + || value.placement() == ExpressionPlacement::MoveTowardsLeafNodes + }); + let mut push_predicates = vec![]; let mut keep_predicates = vec![]; - for (expr, references_leaf_expr) in predicates.into_iter().zip(references_leaf_expr) { - if references_leaf_expr || inliner.references_pinned(&expr, &pinned) { + for expr in predicates { + if contains_column(&expr, &non_pushable_map) { keep_predicates.push(expr); } else { push_predicates.push(expr); @@ -1598,7 +1581,7 @@ fn rewrite_projection( // re-write all filters based on this projection // E.g. in `Filter: b\n Projection: a > 1 as b`, we can swap them, but the filter must be "a > 1" projection.input = Arc::new(LogicalPlan::Filter(Filter::new( - inliner.substitute(expr)?, + replace_cols_by_column(expr, &pushable_map)?, projection.input, ))); @@ -1680,6 +1663,21 @@ fn replace_cols_by_column( .data() } +/// Unalias expression reference. +fn unalias(expr: &Expr) -> &Expr { + if let Expr::Alias(alias) = expr { + unalias(&alias.expr) + } else { + expr + } +} + +/// check whether the expression uses the columns in `check_map`. +fn contains_column(e: &Expr, check_map: &HashMap) -> bool { + e.exists(|expr| Ok(matches!(expr, Expr::Column(c) if check_map.contains_key(c)))) + .unwrap() +} + fn with_filters(predicates: Vec, plan: LogicalPlan) -> LogicalPlan { if let Some(predicate) = conjunction(predicates) { LogicalPlan::Filter(Filter::new(predicate, Arc::new(plan))) @@ -1769,7 +1767,7 @@ mod tests { use crate::assert_optimized_plan_eq_snapshot; use crate::optimizer::Optimizer; use crate::simplify_expressions::SimplifyExpressions; - use crate::test::udfs::{PlacementTestUDF, get_field_like, leaf_udf_expr}; + use crate::test::udfs::leaf_udf_expr; use crate::test::*; use datafusion_expr::test::function_stub::{count, sum}; use insta::assert_snapshot; @@ -4961,116 +4959,6 @@ mod tests { ) } - fn keep_in_place_udf(arg: Expr) -> Expr { - ScalarUDF::new_from_impl( - PlacementTestUDF::new().with_placement(ExpressionPlacement::KeepInPlace), - ) - .call(vec![arg]) - } - - /// A non-volatile definition that the predicates reference two times is - /// pushed, as on `main`: DataFusion does not know what `c1` costs, and a - /// predicate that reaches the scan can prune data. - /// `CommonSubexprEliminate` then computes it one time again. - #[test] - fn filter_with_repeated_non_volatile_reference_pushed_through_projection() - -> Result<()> { - let plan = LogicalPlanBuilder::from(test_table_scan()?) - .project(vec![keep_in_place_udf(col("a")).alias("c1"), col("b")])? - .filter( - col("c1") - .is_not_null() - .and(get_field_like(col("c1"), "x").is_null()) - .and(col("b").gt(lit(1))), - )? - .build()?; - - assert_optimized_plan_equal!( - plan, - @r#" - Projection: keep_in_place_udf(test.a) AS c1, test.b - TableScan: test, full_filters=[test.b > Int32(1), keep_in_place_udf(test.a) IS NOT NULL, get_field_like(keep_in_place_udf(test.a), Utf8("x")) IS NULL] - "# - ) - } - - /// A single reference to an expensive definition is pushed, like any - /// other computed column of a projection. - #[test] - fn filter_with_single_expensive_reference_pushed_through_projection() -> Result<()> { - let plan = LogicalPlanBuilder::from(test_table_scan()?) - .project(vec![keep_in_place_udf(col("a")).alias("c1"), col("b")])? - .filter(col("c1").is_not_null())? - .build()?; - - assert_optimized_plan_equal!( - plan, - @r" - Projection: keep_in_place_udf(test.a) AS c1, test.b - TableScan: test, full_filters=[keep_in_place_udf(test.a) IS NOT NULL] - " - ) - } - - /// A predicate that stays above the projection reads the computed column, - /// so it does not count as an evaluation of the definition. The other - /// predicate references `c1` one time and is pushed. - #[test] - fn kept_predicate_does_not_pin_expensive_reference() -> Result<()> { - let plan = LogicalPlanBuilder::from(test_table_scan()?) - .project(vec![ - keep_in_place_udf(col("a")).alias("c1"), - get_field_like(col("b"), "x").alias("c2"), - ])? - .filter( - col("c1") - .eq(col("c2")) - .and(col("c1").gt(lit(ScalarValue::UInt32(Some(1))))), - )? - .build()?; - - assert_optimized_plan_equal!( - plan, - @r#" - Filter: c1 = c2 - Projection: keep_in_place_udf(test.a) AS c1, get_field_like(test.b, Utf8("x")) AS c2 - TableScan: test, full_filters=[keep_in_place_udf(test.a) > UInt32(1)] - "# - ) - } - - /// The shape of draft https://github.com/apache/datafusion/pull/25388: - /// a filter over a projection that reads an expensive column of the - /// projection below it. The narrow policy pushes `c1 IS NOT NULL`, - /// because the predicates reference `c1` one time. `c2 IS NULL` stays - /// above, because `c2` is a `MoveTowardsLeafNodes` definition. - #[test] - fn filter_not_pushed_through_nested_computed_projection() -> Result<()> { - let inner = LogicalPlanBuilder::from(test_table_scan()?) - .project(vec![keep_in_place_udf(col("a")).alias("c1"), col("b")])? - .build()?; - let outer = LogicalPlanBuilder::from(inner) - .project(vec![ - get_field_like(col("c1"), "x").alias("c2"), - col("c1"), - col("b"), - ])? - .build()?; - let plan = LogicalPlanBuilder::from(outer) - .filter(col("c1").is_not_null().and(col("c2").is_null()))? - .build()?; - - assert_optimized_plan_equal!( - plan, - @r#" - Filter: c2 IS NULL - Projection: get_field_like(c1, Utf8("x")) AS c2, c1, test.b - Projection: keep_in_place_udf(test.a) AS c1, test.b - TableScan: test, full_filters=[keep_in_place_udf(test.a) IS NOT NULL] - "# - ) - } - #[test] fn filter_not_pushed_down_through_table_scan_with_fetch() -> Result<()> { let scan = test_table_scan()?; diff --git a/datafusion/sqllogictest/test_files/cse.slt b/datafusion/sqllogictest/test_files/cse.slt index ebeb7768bc8ef..fa8ed0aa486e7 100644 --- a/datafusion/sqllogictest/test_files/cse.slt +++ b/datafusion/sqllogictest/test_files/cse.slt @@ -383,6 +383,9 @@ a a # Filter shape: `arrow_field(a)` appears one time in the physical plan. +# PushDownFilter still inlines it and CommonSubexprEliminate hoists it again +# in every pass, which renumbers the alias. That is the convergence issue, not +# fixed here. query TT EXPLAIN SELECT a FROM cse_keep_in_place WHERE CASE WHEN arrow_field(a) IS NOT NULL THEN arrow_field(a)['nullable'] IS NOT NULL END; ----