Skip to content

Apache DataFusion SQL Query Engine

Refactor optimize projections rule, combines (eliminate, merge, pushdown projections) (#8340)

Mustafa Akur19bdcdc4140f
diff --git a/datafusion/core/tests/sql/explain_analyze.rs b/datafusion/core/tests/sql/explain_analyze.rs
index 0ebd3a0c69..ecb5766a3b 100644
--- a/datafusion/core/tests/sql/explain_analyze.rs
+++ b/datafusion/core/tests/sql/explain_analyze.rs
@@ -560,7 +560,7 @@ async fn csv_explain_verbose_plans() {
// Since the plan contains path that are environmentally
// dependant(e.g. full path of the test file), only verify
// important content
- assert_contains!(&actual, "logical_plan after push_down_projection");
+ assert_contains!(&actual, "logical_plan after optimize_projections");
assert_contains!(&actual, "physical_plan");
assert_contains!(&actual, "FilterExec: c2@1 > 10");
assert_contains!(actual, "ProjectionExec: expr=[c1@0 as c1]");
diff --git a/datafusion/expr/src/logical_plan/mod.rs b/datafusion/expr/src/logical_plan/mod.rs
index 51d78cd721..bc722dd69a 100644
--- a/datafusion/expr/src/logical_plan/mod.rs
+++ b/datafusion/expr/src/logical_plan/mod.rs
@@ -33,10 +33,11 @@ pub use ddl::{
};
pub use dml::{DmlStatement, WriteOp};
pub use plan::{
- Aggregate, Analyze, CrossJoin, DescribeTable, Distinct, DistinctOn, EmptyRelation,
- Explain, Extension, Filter, Join, JoinConstraint, JoinType, Limit, LogicalPlan,
- Partitioning, PlanType, Prepare, Projection, Repartition, Sort, StringifiedPlan,
- Subquery, SubqueryAlias, TableScan, ToStringifiedPlan, Union, Unnest, Values, Window,
+ projection_schema, Aggregate, Analyze, CrossJoin, DescribeTable, Distinct,
+ DistinctOn, EmptyRelation, Explain, Extension, Filter, Join, JoinConstraint,
+ JoinType, Limit, LogicalPlan, Partitioning, PlanType, Prepare, Projection,
+ Repartition, Sort, StringifiedPlan, Subquery, SubqueryAlias, TableScan,
+ ToStringifiedPlan, Union, Unnest, Values, Window,
};
pub use statement::{
SetVariable, Statement, TransactionAccessMode, TransactionConclusion, TransactionEnd,
diff --git a/datafusion/expr/src/logical_plan/plan.rs b/datafusion/expr/src/logical_plan/plan.rs
index 69ba42d34a..ea7a48d2c4 100644
--- a/datafusion/expr/src/logical_plan/plan.rs
+++ b/datafusion/expr/src/logical_plan/plan.rs
@@ -551,15 +551,9 @@ impl LogicalPlan {
Projection::try_new(projection.expr.to_vec(), Arc::new(inputs[0].clone()))
.map(LogicalPlan::Projection)
}
- LogicalPlan::Window(Window {
- window_expr,
- schema,
- ..
- }) => Ok(LogicalPlan::Window(Window {
- input: Arc::new(inputs[0].clone()),
- window_expr: window_expr.to_vec(),
- schema: schema.clone(),
- })),
+ LogicalPlan::Window(Window { window_expr, .. }) => Ok(LogicalPlan::Window(
+ Window::try_new(window_expr.to_vec(), Arc::new(inputs[0].clone()))?,
+ )),
LogicalPlan::Aggregate(Aggregate {
group_expr,
aggr_expr,
@@ -837,10 +831,19 @@ impl LogicalPlan {
LogicalPlan::Extension(e) => Ok(LogicalPlan::Extension(Extension {
node: e.node.from_template(&expr, inputs),
})),
- LogicalPlan::Union(Union { schema, .. }) => Ok(LogicalPlan::Union(Union {
- inputs: inputs.iter().cloned().map(Arc::new).collect(),
- schema: schema.clone(),
- })),
+ LogicalPlan::Union(Union { schema, .. }) => {
+ let input_schema = inputs[0].schema();
+ // If inputs are not pruned do not change schema.
+ let schema = if schema.fields().len() == input_schema.fields().len() {
+ schema
+ } else {
+ input_schema
+ };
+ Ok(LogicalPlan::Union(Union {
+ inputs: inputs.iter().cloned().map(Arc::new).collect(),
+ schema: schema.clone(),
+ }))
+ }
LogicalPlan::Distinct(distinct) => {
let distinct = match distinct {
Distinct::All(_) => Distinct::All(Arc::new(inputs[0].clone())),
@@ -1792,11 +1795,8 @@ pub struct Projection {
impl Projection {
/// Create a new Projection
pub fn try_new(expr: Vec<Expr>, input: Arc<LogicalPlan>) -> Result<Self> {
- let schema = Arc::new(DFSchema::new_with_metadata(
- exprlist_to_fields(&expr, &input)?,
- input.schema().metadata().clone(),
- )?);
- Self::try_new_with_schema(expr, input, schema)
+ let projection_schema = projection_schema(&input, &expr)?;
+ Self::try_new_with_schema(expr, input, projection_schema)
}
/// Create a new Projection using the specified output schema
@@ -1808,11 +1808,6 @@ impl Projection {
if expr.len() != schema.fields().len() {
return plan_err!("Projection has mismatch between number of expressions ({}) and number of fields in schema ({})", expr.len(), schema.fields().len());
}
- // Update functional dependencies of `input` according to projection
- // expressions:
- let id_key_groups = calc_func_dependencies_for_project(&expr, &input)?;
- let schema = schema.as_ref().clone();
- let schema = Arc::new(schema.with_functional_dependencies(id_key_groups));
Ok(Self {
expr,
input,
@@ -1836,6 +1831,29 @@ impl Projection {
}
}
+/// Computes the schema of the result produced by applying a projection to the input logical plan.
+///
+/// # Arguments
+///
+/// * `input`: A reference to the input `LogicalPlan` for which the projection schema
+/// will be computed.
+/// * `exprs`: A slice of `Expr` expressions representing the projection operation to apply.
+///
+/// # Returns
+///
+/// A `Result` containing an `Arc<DFSchema>` representing the schema of the result
+/// produced by the projection operation. If the schema computation is successful,
+/// the `Result` will contain the schema; otherwise, it will contain an error.
+pub fn projection_schema(input: &LogicalPlan, exprs: &[Expr]) -> Result<Arc<DFSchema>> {
+ let mut schema = DFSchema::new_with_metadata(
+ exprlist_to_fields(exprs, input)?,
+ input.schema().metadata().clone(),
+ )?;
+ schema = schema
+ .with_functional_dependencies(calc_func_dependencies_for_project(exprs, input)?);
+ Ok(Arc::new(schema))
+}
+
/// Aliased subquery
#[derive(Clone, PartialEq, Eq, Hash)]
// mark non_exhaustive to encourage use of try_new/new()
@@ -1934,8 +1952,7 @@ impl Window {
/// Create a new window operator.
pub fn try_new(window_expr: Vec<Expr>, input: Arc<LogicalPlan>) -> Result<Self> {
let mut window_fields: Vec<DFField> = input.schema().fields().clone();
- window_fields
- .extend_from_slice(&exprlist_to_fields(window_expr.iter(), input.as_ref())?);
+ window_fields.extend_from_slice(&exprlist_to_fields(window_expr.iter(), &input)?);
let metadata = input.schema().metadata().clone();
// Update functional dependencies for window:
@@ -2357,6 +2374,13 @@ impl Aggregate {
schema,
})
}
+
+ /// Get the length of the group by expression in the output schema
+ /// This is not simply group by expression length. Expression may be
+ /// GroupingSet, etc. In these case we need to get inner expression lengths.
+ pub fn group_expr_len(&self) -> Result<usize> {
+ grouping_set_expr_count(&self.group_expr)
+ }
}
/// Checks whether any expression in `group_expr` contains `Expr::GroupingSet`.
diff --git a/datafusion/optimizer/src/eliminate_project.rs b/datafusion/optimizer/src/eliminate_project.rs
deleted file mode 100644
index d3226eaa78..0000000000
--- a/datafusion/optimizer/src/eliminate_project.rs
+++ /dev/null
@@ -1,94 +0,0 @@
-// 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.
-
-use crate::optimizer::ApplyOrder;
-use crate::{OptimizerConfig, OptimizerRule};
-use datafusion_common::{DFSchemaRef, Result};
-use datafusion_expr::logical_plan::LogicalPlan;
-use datafusion_expr::{Expr, Projection};
-
-/// Optimization rule that eliminate unnecessary [LogicalPlan::Projection].
-#[derive(Default)]
-pub struct EliminateProjection;
-
-impl EliminateProjection {
- #[allow(missing_docs)]
- pub fn new() -> Self {
- Self {}
- }
-}
-
-impl OptimizerRule for EliminateProjection {
- fn try_optimize(
- &self,
- plan: &LogicalPlan,
- _config: &dyn OptimizerConfig,
- ) -> Result<Option<LogicalPlan>> {
- match plan {
- LogicalPlan::Projection(projection) => {
- let child_plan = projection.input.as_ref();
- match child_plan {
- LogicalPlan::Union(_)
- | LogicalPlan::Filter(_)
- | LogicalPlan::TableScan(_)
- | LogicalPlan::SubqueryAlias(_)
- | LogicalPlan::Sort(_) => {
- if can_eliminate(projection, child_plan.schema()) {
- Ok(Some(child_plan.clone()))
- } else {
- Ok(None)
- }
- }
- _ => {
- if plan.schema() == child_plan.schema() {
- Ok(Some(child_plan.clone()))
- } else {
- Ok(None)
- }
- }
- }
- }
- _ => Ok(None),
- }
- }
-
- fn name(&self) -> &str {
- "eliminate_projection"
- }
-
- fn apply_order(&self) -> Option<ApplyOrder> {
- Some(ApplyOrder::TopDown)
- }
-}
-
-pub(crate) fn can_eliminate(projection: &Projection, schema: &DFSchemaRef) -> bool {
- if projection.expr.len() != schema.fields().len() {
- return false;
- }
- for (i, e) in projection.expr.iter().enumerate() {
- match e {
- Expr::Column(c) => {
- let d = schema.fields().get(i).unwrap();
- if c != &d.qualified_column() && c != &d.unqualified_column() {
- return false;
- }
- }
- _ => return false,
- }
- }
- true
-}
diff --git a/datafusion/optimizer/src/lib.rs b/datafusion/optimizer/src/lib.rs
index ede0ac5c71..d8b0c14589 100644
--- a/datafusion/optimizer/src/lib.rs
+++ b/datafusion/optimizer/src/lib.rs
@@ -27,10 +27,10 @@ pub mod eliminate_limit;
pub mod eliminate_nested_union;
pub mod eliminate_one_union;
pub mod eliminate_outer_join;
-pub mod eliminate_project;
pub mod extract_equijoin_predicate;
pub mod filter_null_join_keys;
pub mod merge_projection;
+pub mod optimize_projections;
pub mod optimizer;
pub mod propagate_empty_relation;
pub mod push_down_filter;
diff --git a/datafusion/optimizer/src/merge_projection.rs b/datafusion/optimizer/src/merge_projection.rs
index ec040cba6f..f7b750011e 100644
--- a/datafusion/optimizer/src/merge_projection.rs
+++ b/datafusion/optimizer/src/merge_projection.rs
@@ -15,105 +15,9 @@
// specific language governing permissions and limitations
// under the License.
-use std::collections::HashMap;
-
-use crate::optimizer::ApplyOrder;
-use crate::push_down_filter::replace_cols_by_name;
-use crate::{OptimizerConfig, OptimizerRule};
-
-use datafusion_common::Result;
-use datafusion_expr::{Expr, LogicalPlan, Projection};
-
-/// Optimization rule that merge [LogicalPlan::Projection].
-#[derive(Default)]
-pub struct MergeProjection;
-
-impl MergeProjection {
- #[allow(missing_docs)]
- pub fn new() -> Self {
- Self {}
- }
-}
-
-impl OptimizerRule for MergeProjection {
- fn try_optimize(
- &self,
- plan: &LogicalPlan,
- _config: &dyn OptimizerConfig,
- ) -> Result<Option<LogicalPlan>> {
- match plan {
- LogicalPlan::Projection(parent_projection) => {
- match parent_projection.input.as_ref() {
- LogicalPlan::Projection(child_projection) => {
- let new_plan =
- merge_projection(parent_projection, child_projection)?;
- Ok(Some(
- self.try_optimize(&new_plan, _config)?.unwrap_or(new_plan),
- ))
- }
- _ => Ok(None),
- }
- }
- _ => Ok(None),
- }
- }
-
- fn name(&self) -> &str {
- "merge_projection"
- }
-
- fn apply_order(&self) -> Option<ApplyOrder> {
- Some(ApplyOrder::TopDown)
- }
-}
-
-pub(super) fn merge_projection(
- parent_projection: &Projection,
- child_projection: &Projection,
-) -> Result<LogicalPlan> {
- let replace_map = collect_projection_expr(child_projection);
- let new_exprs = parent_projection
- .expr
- .iter()
- .map(|expr| replace_cols_by_name(expr.clone(), &replace_map))
- .enumerate()
- .map(|(i, e)| match e {
- Ok(e) => {
- let parent_expr = parent_projection.schema.fields()[i].qualified_name();
- e.alias_if_changed(parent_expr)
- }
- Err(e) => Err(e),
- })
- .collect::<Result<Vec<_>>>()?;
- // Use try_new, since schema changes with changing expressions.
- let new_plan = LogicalPlan::Projection(Projection::try_new(
- new_exprs,
- child_projection.input.clone(),
- )?);
- Ok(new_plan)
-}
-
-pub fn collect_projection_expr(projection: &Projection) -> HashMap<String, Expr> {
- projection
- .schema
- .fields()
- .iter()
- .enumerate()
- .flat_map(|(i, field)| {
- // strip alias
- let expr = projection.expr[i].clone().unalias();
- // Convert both qualified and unqualified fields
- [
- (field.name().clone(), expr.clone()),
- (field.qualified_name(), expr),
- ]
- })
- .collect::<HashMap<_, _>>()
-}
-
#[cfg(test)]
mod tests {
- use crate::merge_projection::MergeProjection;
+ use crate::optimize_projections::OptimizeProjections;
use datafusion_common::Result;
use datafusion_expr::{
binary_expr, col, lit, logical_plan::builder::LogicalPlanBuilder, LogicalPlan,
@@ -124,7 +28,7 @@ mod tests {
use crate::test::*;
fn assert_optimized_plan_equal(plan: &LogicalPlan, expected: &str) -> Result<()> {
- assert_optimized_plan_eq(Arc::new(MergeProjection::new()), plan, expected)
+ assert_optimized_plan_eq(Arc::new(OptimizeProjections::new()), plan, expected)
}
#[test]
@@ -136,7 +40,7 @@ mod tests {
.build()?;
let expected = "Projection: Int32(1) + test.a\
- \n TableScan: test";
+ \n TableScan: test projection=[a]";
assert_optimized_plan_equal(&plan, expected)
}
@@ -150,7 +54,7 @@ mod tests {
.build()?;
let expected = "Projection: Int32(1) + test.a\
- \n TableScan: test";
+ \n TableScan: test projection=[a]";
assert_optimized_plan_equal(&plan, expected)
}
@@ -163,7 +67,7 @@ mod tests {
.build()?;
let expected = "Projection: test.a AS alias\
- \n TableScan: test";
+ \n TableScan: test projection=[a]";
assert_optimized_plan_equal(&plan, expected)
}
}
diff --git a/datafusion/optimizer/src/optimize_projections.rs b/datafusion/optimizer/src/optimize_projections.rs
new file mode 100644
index 0000000000..3d0565a6af
--- /dev/null
+++ b/datafusion/optimizer/src/optimize_projections.rs
@@ -0,0 +1,848 @@
+// 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.
+
+//! Optimizer rule to prune unnecessary Columns from the intermediate schemas inside the [LogicalPlan].
+//! This rule
+//! - Removes unnecessary columns that are not showed at the output, and that are not used during computation.
+//! - Adds projection to decrease table column size before operators that benefits from less memory at its input.
+//! - Removes unnecessary [LogicalPlan::Projection] from the [LogicalPlan].
+use crate::optimizer::ApplyOrder;
+use datafusion_common::{Column, DFSchema, DFSchemaRef, JoinType, Result};
+use datafusion_expr::expr::{Alias, ScalarFunction};
+use datafusion_expr::{
+ logical_plan::LogicalPlan, projection_schema, Aggregate, BinaryExpr, Cast, Distinct,
+ Expr, Projection, ScalarFunctionDefinition, TableScan, Window,
+};
+use hashbrown::HashMap;
+use itertools::{izip, Itertools};
+use std::collections::HashSet;
+use std::sync::Arc;
+
+use crate::{OptimizerConfig, OptimizerRule};
+
+/// A rule for optimizing logical plans by removing unused Columns/Fields.
+///
+/// `OptimizeProjections` is an optimizer rule that identifies and eliminates columns from a logical plan
+/// that are not used in any downstream operations. This can improve query performance and reduce unnecessary
+/// data processing.
+///
+/// The rule analyzes the input logical plan, determines the necessary column indices, and then removes any
+/// unnecessary columns. Additionally, it eliminates any unnecessary projections in the plan.
+#[derive(Default)]
+pub struct OptimizeProjections {}
+
+impl OptimizeProjections {
+ #[allow(missing_docs)]
+ pub fn new() -> Self {
+ Self {}
+ }
+}
+
+impl OptimizerRule for OptimizeProjections {
+ fn try_optimize(
+ &self,
+ plan: &LogicalPlan,
+ config: &dyn OptimizerConfig,
+ ) -> Result<Option<LogicalPlan>> {
+ // All of the fields at the output are necessary.
+ let indices = require_all_indices(plan);
+ optimize_projections(plan, config, &indices)
+ }
+
+ fn name(&self) -> &str {
+ "optimize_projections"
+ }
+
+ fn apply_order(&self) -> Option<ApplyOrder> {
+ None
+ }
+}
+
+/// Removes unnecessary columns (e.g Columns that are not referred at the output schema and
+/// Columns that are not used during any computation, expression evaluation) from the logical plan and its inputs.
+///
+/// # Arguments
+///
+/// - `plan`: A reference to the input `LogicalPlan` to be optimized.
+/// - `_config`: A reference to the optimizer configuration (not currently used).
+/// - `indices`: A slice of column indices that represent the necessary column indices for downstream operations.
+///
+/// # Returns
+///
+/// - `Ok(Some(LogicalPlan))`: An optimized `LogicalPlan` with unnecessary columns removed.
+/// - `Ok(None)`: If the optimization process results in a logical plan that doesn't require further propagation.
+/// - `Err(error)`: If an error occurs during the optimization process.
+fn optimize_projections(
+ plan: &LogicalPlan,
+ _config: &dyn OptimizerConfig,
+ indices: &[usize],
+) -> Result<Option<LogicalPlan>> {
+ // `child_required_indices` stores
+ // - indices of the columns required for each child
+ // - a flag indicating whether putting a projection above children is beneficial for the parent.
+ // As an example LogicalPlan::Filter benefits from small tables. Hence for filter child this flag would be `true`.
+ let child_required_indices: Option<Vec<(Vec<usize>, bool)>> = match plan {
+ LogicalPlan::Sort(_)
+ | LogicalPlan::Filter(_)
+ | LogicalPlan::Repartition(_)
+ | LogicalPlan::Unnest(_)
+ | LogicalPlan::Union(_)
+ | LogicalPlan::SubqueryAlias(_)
+ | LogicalPlan::Distinct(Distinct::On(_)) => {
+ // Re-route required indices from the parent + column indices referred by expressions in the plan
+ // to the child.
+ // All of these operators benefits from small tables at their inputs. Hence projection_beneficial flag is `true`.
+ let exprs = plan.expressions();
+ let child_req_indices = plan
+ .inputs()
+ .into_iter()
+ .map(|input| {
+ let required_indices =
+ get_all_required_indices(indices, input, exprs.iter())?;
+ Ok((required_indices, true))
+ })
+ .collect::<Result<Vec<_>>>()?;
+ Some(child_req_indices)
+ }
+ LogicalPlan::Limit(_) | LogicalPlan::Prepare(_) => {
+ // Re-route required indices from the parent + column indices referred by expressions in the plan
+ // to the child.
+ // Limit, Prepare doesn't benefit from small column numbers. Hence projection_beneficial flag is `false`.
+ let exprs = plan.expressions();
+ let child_req_indices = plan
+ .inputs()
+ .into_iter()
+ .map(|input| {
+ let required_indices =
+ get_all_required_indices(indices, input, exprs.iter())?;
+ Ok((required_indices, false))
+ })
+ .collect::<Result<Vec<_>>>()?;
+ Some(child_req_indices)
+ }
+ LogicalPlan::Copy(_)
+ | LogicalPlan::Ddl(_)
+ | LogicalPlan::Dml(_)
+ | LogicalPlan::Explain(_)
+ | LogicalPlan::Analyze(_)
+ | LogicalPlan::Subquery(_)
+ | LogicalPlan::Distinct(Distinct::All(_)) => {
+ // Require all of the fields of the Dml, Ddl, Copy, Explain, Analyze, Subquery, Distinct::All input(s).
+ // Their child plan can be treated as final plan. Otherwise expected schema may not match.
+ // TODO: For some subquery variants we may not need to require all indices for its input.
+ // such as Exists<SubQuery>.
+ let child_requirements = plan
+ .inputs()
+ .iter()
+ .map(|input| {
+ // Require all of the fields for each input.
+ // No projection since all of the fields at the child is required
+ (require_all_indices(input), false)
+ })
+ .collect::<Vec<_>>();
+ Some(child_requirements)
+ }
+ LogicalPlan::EmptyRelation(_)
+ | LogicalPlan::Statement(_)
+ | LogicalPlan::Values(_)
+ | LogicalPlan::Extension(_)
+ | LogicalPlan::DescribeTable(_) => {
+ // EmptyRelation, Values, DescribeTable, Statement has no inputs stop iteration
+
+ // TODO: Add support for extension
+ // It is not known how to direct requirements to children for LogicalPlan::Extension.
+ // Safest behaviour is to stop propagation.
+ None
+ }
+ LogicalPlan::Projection(proj) => {
+ return if let Some(proj) = merge_consecutive_projections(proj)? {
+ rewrite_projection_given_requirements(&proj, _config, indices)?
+ .map(|res| Ok(Some(res)))
+ // Even if projection cannot be optimized, return merged version
+ .unwrap_or_else(|| Ok(Some(LogicalPlan::Projection(proj))))
+ } else {
+ rewrite_projection_given_requirements(proj, _config, indices)
+ };
+ }
+ LogicalPlan::Aggregate(aggregate) => {
+ // Split parent requirements to group by and aggregate sections
+ let group_expr_len = aggregate.group_expr_len()?;
+ let (_group_by_reqs, mut aggregate_reqs): (Vec<usize>, Vec<usize>) =
+ indices.iter().partition(|&&idx| idx < group_expr_len);
+ // Offset aggregate indices so that they point to valid indices at the `aggregate.aggr_expr`
+ aggregate_reqs
+ .iter_mut()
+ .for_each(|idx| *idx -= group_expr_len);
+
+ // Group by expressions are same
+ let new_group_bys = aggregate.group_expr.clone();
+
+ // Only use absolutely necessary aggregate expressions required by parent.
+ let new_aggr_expr = get_at_indices(&aggregate.aggr_expr, &aggregate_reqs);
+ let all_exprs_iter = new_group_bys.iter().chain(new_aggr_expr.iter());
+ let necessary_indices =
+ indices_referred_by_exprs(&aggregate.input, all_exprs_iter)?;
+
+ let aggregate_input = if let Some(input) =
+ optimize_projections(&aggregate.input, _config, &necessary_indices)?
+ {
+ input
+ } else {
+ aggregate.input.as_ref().clone()
+ };
+
+ // Simplify input of the aggregation by adding a projection so that its input only contains
+ // absolutely necessary columns for the aggregate expressions. Please no that we use aggregate.input.schema()
+ // because necessary_indices refers to fields in this schema.
+ let necessary_exprs =
+ get_required_exprs(aggregate.input.schema(), &necessary_indices);
+ let (aggregate_input, _is_added) =
+ add_projection_on_top_if_helpful(aggregate_input, necessary_exprs, true)?;
+
+ // Create new aggregate plan with updated input, and absolutely necessary fields.
+ return Aggregate::try_new(
+ Arc::new(aggregate_input),
+ new_group_bys,
+ new_aggr_expr,
+ )
+ .map(|aggregate| Some(LogicalPlan::Aggregate(aggregate)));
+ }
+ LogicalPlan::Window(window) => {
+ // Split parent requirements to child and window expression sections.
+ let n_input_fields = window.input.schema().fields().len();
+ let (child_reqs, mut window_reqs): (Vec<usize>, Vec<usize>) =
+ indices.iter().partition(|&&idx| idx < n_input_fields);
+ // Offset window expr indices so that they point to valid indices at the `window.window_expr`
+ window_reqs
+ .iter_mut()
+ .for_each(|idx| *idx -= n_input_fields);
+
+ // Only use window expressions that are absolutely necessary by parent requirements.
+ let new_window_expr = get_at_indices(&window.window_expr, &window_reqs);
+
+ // All of the required column indices at the input of the window by parent, and window expression requirements.
+ let required_indices = get_all_required_indices(
+ &child_reqs,
+ &window.input,
+ new_window_expr.iter(),
+ )?;
+ let window_child = if let Some(new_window_child) =
+ optimize_projections(&window.input, _config, &required_indices)?
+ {
+ new_window_child
+ } else {
+ window.input.as_ref().clone()
+ };
+ // When no window expression is necessary, just use window input. (Remove window operator)
+ return if new_window_expr.is_empty() {
+ Ok(Some(window_child))
+ } else {
+ // Calculate required expressions at the input of the window.
+ // Please note that we use `old_child`, because `required_indices` refers to `old_child`.
+ let required_exprs =
+ get_required_exprs(window.input.schema(), &required_indices);
+ let (window_child, _is_added) =
+ add_projection_on_top_if_helpful(window_child, required_exprs, true)?;
+ let window = Window::try_new(new_window_expr, Arc::new(window_child))?;
+ Ok(Some(LogicalPlan::Window(window)))
+ };
+ }
+ LogicalPlan::Join(join) => {
+ let left_len = join.left.schema().fields().len();
+ let (left_req_indices, right_req_indices) =
+ split_join_requirements(left_len, indices, &join.join_type);
+ let exprs = plan.expressions();
+ let left_indices =
+ get_all_required_indices(&left_req_indices, &join.left, exprs.iter())?;
+ let right_indices =
+ get_all_required_indices(&right_req_indices, &join.right, exprs.iter())?;
+ // Join benefits from small columns numbers at its input (decreases memory usage)
+ // Hence each child benefits from projection.
+ Some(vec![(left_indices, true), (right_indices, true)])
+ }
+ LogicalPlan::CrossJoin(cross_join) => {
+ let left_len = cross_join.left.schema().fields().len();
+ let (left_child_indices, right_child_indices) =
+ split_join_requirements(left_len, indices, &JoinType::Inner);
+ // Join benefits from small columns numbers at its input (decreases memory usage)
+ // Hence each child benefits from projection.
+ Some(vec![
+ (left_child_indices, true),
+ (right_child_indices, true),
+ ])
+ }
+ LogicalPlan::TableScan(table_scan) => {
+ let projection_fields = table_scan.projected_schema.fields();
+ let schema = table_scan.source.schema();
+ // We expect to find all of the required indices of the projected schema fields.
+ // among original schema. If at least one of them cannot be found. Use all of the fields in the file.
+ // (No projection at the source)
+ let projection = indices
+ .iter()
+ .map(|&idx| {
+ schema.fields().iter().position(|field_source| {
+ projection_fields[idx].field() == field_source
+ })
+ })
+ .collect::<Option<Vec<_>>>();
+
+ return Ok(Some(LogicalPlan::TableScan(TableScan::try_new(
+ table_scan.table_name.clone(),
+ table_scan.source.clone(),
+ projection,
+ table_scan.filters.clone(),
+ table_scan.fetch,
+ )?)));
+ }
+ };
+
+ let child_required_indices =
+ if let Some(child_required_indices) = child_required_indices {
+ child_required_indices
+ } else {
+ // Stop iteration, cannot propagate requirement down below this operator.
+ return Ok(None);
+ };
+
+ let new_inputs = izip!(child_required_indices, plan.inputs().into_iter())
+ .map(|((required_indices, projection_beneficial), child)| {
+ let (input, mut is_changed) = if let Some(new_input) =
+ optimize_projections(child, _config, &required_indices)?
+ {
+ (new_input, true)
+ } else {
+ (child.clone(), false)
+ };
+ let project_exprs = get_required_exprs(child.schema(), &required_indices);
+ let (input, is_projection_added) = add_projection_on_top_if_helpful(
+ input,
+ project_exprs,
+ projection_beneficial,
+ )?;
+ is_changed |= is_projection_added;
+ Ok(is_changed.then_some(input))
+ })
+ .collect::<Result<Vec<Option<_>>>>()?;
+ // All of the children are same in this case, no need to change plan
+ if new_inputs.iter().all(|child| child.is_none()) {
+ Ok(None)
+ } else {
+ // At least one of the children is changed.
+ let new_inputs = izip!(new_inputs, plan.inputs())
+ // If new_input is `None`, this means child is not changed. Hence use `old_child` during construction.
+ .map(|(new_input, old_child)| new_input.unwrap_or_else(|| old_child.clone()))
+ .collect::<Vec<_>>();
+ let res = plan.with_new_inputs(&new_inputs)?;
+ Ok(Some(res))
+ }
+}
+
+/// Merge Consecutive Projections
+///
+/// Given a projection `proj`, this function attempts to merge it with a previous
+/// projection if it exists and if the merging is beneficial. Merging is considered
+/// beneficial when expressions in the current projection are non-trivial and referred to
+/// more than once in its input fields. This can act as a caching mechanism for non-trivial
+/// computations.
+///
+/// # Arguments
+///
+/// * `proj` - A reference to the `Projection` to be merged.
+///
+/// # Returns
+///
+/// A `Result` containing an `Option` of the merged `Projection`. If merging is not beneficial
+/// it returns `Ok(None)`.
+fn merge_consecutive_projections(proj: &Projection) -> Result<Option<Projection>> {
+ let prev_projection = if let LogicalPlan::Projection(prev) = proj.input.as_ref() {
+ prev
+ } else {
+ return Ok(None);
+ };
+
+ // Count usages (referral counts) of each projection expression in its input fields
+ let column_referral_map: HashMap<Column, usize> = proj
+ .expr
+ .iter()
+ .flat_map(|expr| expr.to_columns())
+ .fold(HashMap::new(), |mut map, cols| {
+ cols.into_iter()
+ .for_each(|col| *map.entry(col.clone()).or_default() += 1);
+ map
+ });
+
+ // Merging these projections is not beneficial, e.g
+ // If an expression is not trivial and it is referred more than 1, consecutive projections will be
+ // beneficial as caching mechanism for non-trivial computations.
+ // See discussion in: https://github.com/apache/arrow-datafusion/issues/8296
+ if column_referral_map.iter().any(|(col, usage)| {
+ *usage > 1
+ && !is_expr_trivial(
+ &prev_projection.expr
+ [prev_projection.schema.index_of_column(col).unwrap()],
+ )
+ }) {
+ return Ok(None);
+ }
+
+ // If all of the expression of the top projection can be rewritten. Rewrite expressions and create a new projection
+ let new_exprs = proj
+ .expr
+ .iter()
+ .map(|expr| rewrite_expr(expr, prev_projection))
+ .collect::<Result<Option<Vec<_>>>>()?;
+ new_exprs
+ .map(|exprs| Projection::try_new(exprs, prev_projection.input.clone()))
+ .transpose()
+}
+
+/// Trim Expression
+///
+/// Trim the given expression by removing any unnecessary layers of abstraction.
+/// If the expression is an alias, the function returns the underlying expression.
+/// Otherwise, it returns the original expression unchanged.
+///
+/// # Arguments
+///
+/// * `expr` - The input expression to be trimmed.
+///
+/// # Returns
+///
+/// The trimmed expression. If the input is an alias, the underlying expression is returned.
+///
+/// Without trimming, during projection merge we can end up unnecessary indirections inside the expressions.
+/// Consider:
+///
+/// Projection (a1 + b1 as sum1)
+/// --Projection (a as a1, b as b1)
+/// ----Source (a, b)
+///
+/// After merge we want to produce
+///
+/// Projection (a + b as sum1)
+/// --Source(a, b)
+///
+/// Without trimming we would end up
+///
+/// Projection (a as a1 + b as b1 as sum1)
+/// --Source(a, b)
+fn trim_expr(expr: Expr) -> Expr {
+ match expr {
+ Expr::Alias(alias) => *alias.expr,
+ _ => expr,
+ }
+}
+
+// Check whether expression is trivial (e.g it doesn't include computation.)
+fn is_expr_trivial(expr: &Expr) -> bool {
+ matches!(expr, Expr::Column(_) | Expr::Literal(_))
+}
+
+// Exit early when None is seen.
+macro_rules! rewrite_expr_with_check {
+ ($expr:expr, $input:expr) => {
+ if let Some(val) = rewrite_expr($expr, $input)? {
+ val
+ } else {
+ return Ok(None);
+ }
+ };
+}
+
+// Rewrites expression using its input projection (Merges consecutive projection expressions).
+/// Rewrites an projections expression using its input projection
+/// (Helper during merging consecutive projection expressions).
+///
+/// # Arguments
+///
+/// * `expr` - A reference to the expression to be rewritten.
+/// * `input` - A reference to the input (itself a projection) of the projection expression.
+///
+/// # Returns
+///
+/// A `Result` containing an `Option` of the rewritten expression. If the rewrite is successful,
+/// it returns `Ok(Some)` with the modified expression. If the expression cannot be rewritten
+/// it returns `Ok(None)`.
+fn rewrite_expr(expr: &Expr, input: &Projection) -> Result<Option<Expr>> {
+ Ok(match expr {
+ Expr::Column(col) => {
+ // Find index of column
+ let idx = input.schema.index_of_column(col)?;
+ Some(input.expr[idx].clone())
+ }
+ Expr::BinaryExpr(binary) => {
+ let lhs = trim_expr(rewrite_expr_with_check!(&binary.left, input));
+ let rhs = trim_expr(rewrite_expr_with_check!(&binary.right, input));
+ Some(Expr::BinaryExpr(BinaryExpr::new(
+ Box::new(lhs),
+ binary.op,
+ Box::new(rhs),
+ )))
+ }
+ Expr::Alias(alias) => {
+ let new_expr = trim_expr(rewrite_expr_with_check!(&alias.expr, input));
+ Some(Expr::Alias(Alias::new(
+ new_expr,
+ alias.relation.clone(),
+ alias.name.clone(),
+ )))
+ }
+ Expr::Literal(_val) => Some(expr.clone()),
+ Expr::Cast(cast) => {
+ let new_expr = rewrite_expr_with_check!(&cast.expr, input);
+ Some(Expr::Cast(Cast::new(
+ Box::new(new_expr),
+ cast.data_type.clone(),
+ )))
+ }
+ Expr::ScalarFunction(scalar_fn) => {
+ let fun = if let ScalarFunctionDefinition::BuiltIn { fun, .. } =
+ scalar_fn.func_def
+ {
+ fun
+ } else {
+ return Ok(None);
+ };
+ scalar_fn
+ .args
+ .iter()
+ .map(|expr| rewrite_expr(expr, input))
+ .collect::<Result<Option<Vec<_>>>>()?
+ .map(|new_args| Expr::ScalarFunction(ScalarFunction::new(fun, new_args)))
+ }
+ _ => {
+ // Unsupported type to merge in consecutive projections
+ None
+ }
+ })
+}
+
+/// Retrieves a set of outer-referenced columns from an expression.
+/// Please note that `expr.to_columns()` API doesn't return these columns.
+///
+/// # Arguments
+///
+/// * `expr` - The expression to be analyzed for outer-referenced columns.
+///
+/// # Returns
+///
+/// A `HashSet<Column>` containing columns that are referenced by the expression.
+fn outer_columns(expr: &Expr) -> HashSet<Column> {
+ let mut columns = HashSet::new();
+ outer_columns_helper(expr, &mut columns);
+ columns
+}
+
+/// Helper function to accumulate outer-referenced columns referred by the `expr`.
+///
+/// # Arguments
+///
+/// * `expr` - The expression to be analyzed for outer-referenced columns.
+/// * `columns` - A mutable reference to a `HashSet<Column>` where the detected columns are collected.
+fn outer_columns_helper(expr: &Expr, columns: &mut HashSet<Column>) {
+ match expr {
+ Expr::OuterReferenceColumn(_, col) => {
+ columns.insert(col.clone());
+ }
+ Expr::BinaryExpr(binary_expr) => {
+ outer_columns_helper(&binary_expr.left, columns);
+ outer_columns_helper(&binary_expr.right, columns);
+ }
+ Expr::ScalarSubquery(subquery) => {
+ for expr in &subquery.outer_ref_columns {
+ outer_columns_helper(expr, columns);
+ }
+ }
+ Expr::Exists(exists) => {
+ for expr in &exists.subquery.outer_ref_columns {
+ outer_columns_helper(expr, columns);
+ }
+ }
+ Expr::Alias(alias) => {
+ outer_columns_helper(&alias.expr, columns);
+ }
+ _ => {}
+ }
+}
+
+/// Generates the required expressions(Column) that resides at `indices` of the `input_schema`.
+///
+/// # Arguments
+///
+/// * `input_schema` - A reference to the input schema.
+/// * `indices` - A slice of `usize` indices specifying which columns are required.
+///
+/// # Returns
+///
+/// A vector of `Expr::Column` expressions, that sits at `indices` of the `input_schema`.
+fn get_required_exprs(input_schema: &Arc<DFSchema>, indices: &[usize]) -> Vec<Expr> {
+ let fields = input_schema.fields();
+ indices
+ .iter()
+ .map(|&idx| Expr::Column(fields[idx].qualified_column()))
+ .collect()
+}
+
+/// Get indices of the necessary fields referred by all of the `exprs` among input LogicalPlan.
+///
+/// # Arguments
+///
+/// * `input`: The input logical plan to analyze for index requirements.
+/// * `exprs`: An iterator of expressions for which we want to find necessary field indices at the input.
+///
+/// # Returns
+///
+/// A [Result] object that contains the required field indices for the `input` operator, to be able to calculate
+/// successfully all of the `exprs`.
+fn indices_referred_by_exprs<'a, I: Iterator<Item = &'a Expr>>(
+ input: &LogicalPlan,
+ exprs: I,
+) -> Result<Vec<usize>> {
+ let new_indices = exprs
+ .flat_map(|expr| indices_referred_by_expr(input.schema(), expr))
+ .flatten()
+ // Make sure no duplicate entries exists and indices are ordered.
+ .sorted()
+ .dedup()
+ .collect::<Vec<_>>();
+ Ok(new_indices)
+}
+
+/// Get indices of the necessary fields referred by the `expr` among input schema.
+///
+/// # Arguments
+///
+/// * `input_schema`: The input schema to search for indices referred by expr.
+/// * `expr`: An expression for which we want to find necessary field indices at the input schema.
+///
+/// # Returns
+///
+/// A [Result] object that contains the required field indices of the `input_schema`, to be able to calculate
+/// the `expr` successfully.
+fn indices_referred_by_expr(
+ input_schema: &DFSchemaRef,
+ expr: &Expr,
+) -> Result<Vec<usize>> {
+ let mut cols = expr.to_columns()?;
+ // Get outer referenced columns (expr.to_columns() doesn't return these columns).
+ cols.extend(outer_columns(expr));
+ cols.iter()
+ .filter(|&col| input_schema.has_column(col))
+ .map(|col| input_schema.index_of_column(col))
+ .collect::<Result<Vec<_>>>()
+}
+
+/// Get all required indices for the input (indices required by parent + indices referred by `exprs`)
+///
+/// # Arguments
+///
+/// * `parent_required_indices` - A slice of indices required by the parent plan.
+/// * `input` - The input logical plan to analyze for index requirements.
+/// * `exprs` - An iterator of expressions used to determine required indices.
+///
+/// # Returns
+///
+/// A `Result` containing a vector of `usize` indices containing all required indices.
+fn get_all_required_indices<'a, I: Iterator<Item = &'a Expr>>(
+ parent_required_indices: &[usize],
+ input: &LogicalPlan,
+ exprs: I,
+) -> Result<Vec<usize>> {
+ let referred_indices = indices_referred_by_exprs(input, exprs)?;
+ Ok(merge_vectors(parent_required_indices, &referred_indices))
+}
+
+/// Retrieves a list of expressions at specified indices from a slice of expressions.
+///
+/// This function takes a slice of expressions `exprs` and a slice of `usize` indices `indices`.
+/// It returns a new vector containing the expressions from `exprs` that correspond to the provided indices (with bound check).
+///
+/// # Arguments
+///
+/// * `exprs` - A slice of expressions from which expressions are to be retrieved.
+/// * `indices` - A slice of `usize` indices specifying the positions of the expressions to be retrieved.
+///
+/// # Returns
+///
+/// A vector of expressions that correspond to the specified indices. If any index is out of bounds,
+/// the associated expression is skipped in the result.
+fn get_at_indices(exprs: &[Expr], indices: &[usize]) -> Vec<Expr> {
+ indices
+ .iter()
+ // Indices may point to further places than `exprs` len.
+ .filter_map(|&idx| exprs.get(idx).cloned())
+ .collect()
+}
+
+/// Merges two slices of `usize` values into a single vector with sorted (ascending) and deduplicated elements.
+///
+/// # Arguments
+///
+/// * `lhs` - The first slice of `usize` values to be merged.
+/// * `rhs` - The second slice of `usize` values to be merged.
+///
+/// # Returns
+///
+/// A vector of `usize` values containing the merged, sorted, and deduplicated elements from `lhs` and `rhs`.
+/// As an example merge of [3, 2, 4] and [3, 6, 1] will produce [1, 2, 3, 6]
+fn merge_vectors(lhs: &[usize], rhs: &[usize]) -> Vec<usize> {
+ let mut merged = lhs.to_vec();
+ merged.extend(rhs);
+ // Make sure to run sort before dedup.
+ // Dedup removes consecutive same entries
+ // If sort is run before it, all duplicates are removed.
+ merged.sort();
+ merged.dedup();
+ merged
+}
+
+/// Splits requirement indices for a join into left and right children based on the join type.
+///
+/// This function takes the length of the left child, a slice of requirement indices, and the type
+/// of join (e.g., INNER, LEFT, RIGHT, etc.) as arguments. Depending on the join type, it divides
+/// the requirement indices into those that apply to the left child and those that apply to the right child.
+///
+/// - For INNER, LEFT, RIGHT, and FULL joins, the requirements are split between left and right children.
+/// The right child indices are adjusted to point to valid positions in the right child by subtracting
+/// the length of the left child.
+///
+/// - For LEFT ANTI, LEFT SEMI, RIGHT SEMI, and RIGHT ANTI joins, all requirements are re-routed to either
+/// the left child or the right child directly, depending on the join type.
+///
+/// # Arguments
+///
+/// * `left_len` - The length of the left child.
+/// * `indices` - A slice of requirement indices.
+/// * `join_type` - The type of join (e.g., INNER, LEFT, RIGHT, etc.).
+///
+/// # Returns
+///
+/// A tuple containing two vectors of `usize` indices: the first vector represents the requirements for
+/// the left child, and the second vector represents the requirements for the right child. The indices
+/// are appropriately split and adjusted based on the join type.
+fn split_join_requirements(
+ left_len: usize,
+ indices: &[usize],
+ join_type: &JoinType,
+) -> (Vec<usize>, Vec<usize>) {
+ match join_type {
+ // In these cases requirements split to left and right child.
+ JoinType::Inner | JoinType::Left | JoinType::Right | JoinType::Full => {
+ let (left_child_reqs, mut right_child_reqs): (Vec<usize>, Vec<usize>) =
+ indices.iter().partition(|&&idx| idx < left_len);
+ // Decrease right side index by `left_len` so that they point to valid positions in the right child.
+ right_child_reqs.iter_mut().for_each(|idx| *idx -= left_len);
+ (left_child_reqs, right_child_reqs)
+ }
+ // All requirements can be re-routed to left child directly.
+ JoinType::LeftAnti | JoinType::LeftSemi => (indices.to_vec(), vec![]),
+ // All requirements can be re-routed to right side directly. (No need to change index, join schema is right child schema.)
+ JoinType::RightSemi | JoinType::RightAnti => (vec![], indices.to_vec()),
+ }
+}
+
+/// Adds a projection on top of a logical plan if it is beneficial and reduces the number of columns for the parent operator.
+///
+/// This function takes a `LogicalPlan`, a list of projection expressions, and a flag indicating whether
+/// the projection is beneficial. If the projection is beneficial and reduces the number of columns in
+/// the plan, a new `LogicalPlan` with the projection is created and returned, along with a `true` flag.
+/// If the projection is unnecessary or doesn't reduce the number of columns, the original plan is returned
+/// with a `false` flag.
+///
+/// # Arguments
+///
+/// * `plan` - The input `LogicalPlan` to potentially add a projection to.
+/// * `project_exprs` - A list of expressions for the projection.
+/// * `projection_beneficial` - A flag indicating whether the projection is beneficial.
+///
+/// # Returns
+///
+/// A `Result` containing a tuple with two values: the resulting `LogicalPlan` (with or without
+/// the added projection) and a `bool` flag indicating whether the projection was added (`true`) or not (`false`).
+fn add_projection_on_top_if_helpful(
+ plan: LogicalPlan,
+ project_exprs: Vec<Expr>,
+ projection_beneficial: bool,
+) -> Result<(LogicalPlan, bool)> {
+ // Make sure projection decreases table column size, otherwise it is unnecessary.
+ if !projection_beneficial || project_exprs.len() >= plan.schema().fields().len() {
+ Ok((plan, false))
+ } else {
+ let new_plan = Projection::try_new(project_exprs, Arc::new(plan))
+ .map(LogicalPlan::Projection)?;
+ Ok((new_plan, true))
+ }
+}
+
+/// Collects and returns a vector of all indices of the fields in the schema of a logical plan.
+///
+/// # Arguments
+///
+/// * `plan` - A reference to the `LogicalPlan` for which indices are required.
+///
+/// # Returns
+///
+/// A vector of `usize` indices representing all fields in the schema of the provided logical plan.
+fn require_all_indices(plan: &LogicalPlan) -> Vec<usize> {
+ (0..plan.schema().fields().len()).collect()
+}
+
+/// Rewrite Projection Given Required fields by its parent(s).
+///
+/// # Arguments
+///
+/// * `proj` - A reference to the original projection to be rewritten.
+/// * `_config` - A reference to the optimizer configuration (unused in the function).
+/// * `indices` - A slice of indices representing the required columns by the parent(s) of projection.
+///
+/// # Returns
+///
+/// A `Result` containing an `Option` of the rewritten logical plan. If the
+/// rewrite is successful, it returns `Some` with the optimized logical plan.
+/// If the logical plan remains unchanged it returns `Ok(None)`.
+fn rewrite_projection_given_requirements(
+ proj: &Projection,
+ _config: &dyn OptimizerConfig,
+ indices: &[usize],
+) -> Result<Option<LogicalPlan>> {
+ let exprs_used = get_at_indices(&proj.expr, indices);
+ let required_indices = indices_referred_by_exprs(&proj.input, exprs_used.iter())?;
+ return if let Some(input) =
+ optimize_projections(&proj.input, _config, &required_indices)?
+ {
+ if &projection_schema(&input, &exprs_used)? == input.schema() {
+ Ok(Some(input))
+ } else {
+ let new_proj = Projection::try_new(exprs_used, Arc::new(input.clone()))?;
+ let new_proj = LogicalPlan::Projection(new_proj);
+ Ok(Some(new_proj))
+ }
+ } else if exprs_used.len() < proj.expr.len() {
+ // Projection expression used is different than the existing projection
+ // In this case, even if child doesn't change we should update projection to use less columns.
+ if &projection_schema(&proj.input, &exprs_used)? == proj.input.schema() {
+ Ok(Some(proj.input.as_ref().clone()))
+ } else {
+ let new_proj = Projection::try_new(exprs_used, proj.input.clone())?;
+ let new_proj = LogicalPlan::Projection(new_proj);
+ Ok(Some(new_proj))
+ }
+ } else {
+ // Projection doesn't change.
+ Ok(None)
+ };
+}
diff --git a/datafusion/optimizer/src/optimizer.rs b/datafusion/optimizer/src/optimizer.rs
index e93565fef0..c7ad31f39b 100644
--- a/datafusion/optimizer/src/optimizer.rs
+++ b/datafusion/optimizer/src/optimizer.rs
@@ -27,15 +27,13 @@ use crate::eliminate_limit::EliminateLimit;
use crate::eliminate_nested_union::EliminateNestedUnion;
use crate::eliminate_one_union::EliminateOneUnion;
use crate::eliminate_outer_join::EliminateOuterJoin;
-use crate::eliminate_project::EliminateProjection;
use crate::extract_equijoin_predicate::ExtractEquijoinPredicate;
use crate::filter_null_join_keys::FilterNullJoinKeys;
-use crate::merge_projection::MergeProjection;
+use crate::optimize_projections::OptimizeProjections;
use crate::plan_signature::LogicalPlanSignature;
use crate::propagate_empty_relation::PropagateEmptyRelation;
use crate::push_down_filter::PushDownFilter;
use crate::push_down_limit::PushDownLimit;
-use crate::push_down_projection::PushDownProjection;
use crate::replace_distinct_aggregate::ReplaceDistinctWithAggregate;
use crate::rewrite_disjunctive_predicate::RewriteDisjunctivePredicate;
use crate::scalar_subquery_to_join::ScalarSubqueryToJoin;
@@ -234,7 +232,6 @@ impl Optimizer {
// run it again after running the optimizations that potentially converted
// subqueries to joins
Arc::new(SimplifyExpressions::new()),
- Arc::new(MergeProjection::new()),
Arc::new(RewriteDisjunctivePredicate::new()),
Arc::new(EliminateDuplicatedExpr::new()),
Arc::new(EliminateFilter::new()),
@@ -255,10 +252,7 @@ impl Optimizer {
Arc::new(SimplifyExpressions::new()),
Arc::new(UnwrapCastInComparison::new()),
Arc::new(CommonSubexprEliminate::new()),
- Arc::new(PushDownProjection::new()),
- Arc::new(EliminateProjection::new()),
- // PushDownProjection can pushdown Projections through Limits, do PushDownLimit again.
- Arc::new(PushDownLimit::new()),
+ Arc::new(OptimizeProjections::new()),
];
Self::with_rules(rules)
diff --git a/datafusion/optimizer/src/push_down_projection.rs b/datafusion/optimizer/src/push_down_projection.rs
index 59a5357c97..bdd6634763 100644
--- a/datafusion/optimizer/src/push_down_projection.rs
+++ b/datafusion/optimizer/src/push_down_projection.rs
@@ -18,530 +18,26 @@
//! Projection Push Down optimizer rule ensures that only referenced columns are
//! loaded into memory
-use std::collections::{BTreeSet, HashMap, HashSet};
-use std::sync::Arc;
-
-use crate::eliminate_project::can_eliminate;
-use crate::merge_projection::merge_projection;
-use crate::optimizer::ApplyOrder;
-use crate::push_down_filter::replace_cols_by_name;
-use crate::{OptimizerConfig, OptimizerRule};
-use arrow::error::Result as ArrowResult;
-use datafusion_common::ScalarValue::UInt8;
-use datafusion_common::{
- plan_err, Column, DFSchema, DFSchemaRef, DataFusionError, Result,
-};
-use datafusion_expr::expr::{AggregateFunction, Alias};
-use datafusion_expr::{
- logical_plan::{Aggregate, LogicalPlan, Projection, TableScan, Union},
- utils::{expr_to_columns, exprlist_to_columns, exprlist_to_fields},
- Expr, LogicalPlanBuilder, SubqueryAlias,
-};
-
-// if projection is empty return projection-new_plan, else return new_plan.
-#[macro_export]
-macro_rules! generate_plan {
- ($projection_is_empty:expr, $plan:expr, $new_plan:expr) => {
- if $projection_is_empty {
- $new_plan
- } else {
- $plan.with_new_inputs(&[$new_plan])?
- }
- };
-}
-
-/// Optimizer that removes unused projections and aggregations from plans
-/// This reduces both scans and
-#[derive(Default)]
-pub struct PushDownProjection {}
-
-impl OptimizerRule for PushDownProjection {
- fn try_optimize(
- &self,
- plan: &LogicalPlan,
- _config: &dyn OptimizerConfig,
- ) -> Result<Option<LogicalPlan>> {
- let projection = match plan {
- LogicalPlan::Projection(projection) => projection,
- LogicalPlan::Aggregate(agg) => {
- let mut required_columns = HashSet::new();
- for e in agg.aggr_expr.iter().chain(agg.group_expr.iter()) {
- expr_to_columns(e, &mut required_columns)?
- }
- let new_expr = get_expr(&required_columns, agg.input.schema())?;
- let projection = LogicalPlan::Projection(Projection::try_new(
- new_expr,
- agg.input.clone(),
- )?);
- let optimized_child = self
- .try_optimize(&projection, _config)?
- .unwrap_or(projection);
- return Ok(Some(plan.with_new_inputs(&[optimized_child])?));
- }
- LogicalPlan::TableScan(scan) if scan.projection.is_none() => {
- return Ok(Some(push_down_scan(&HashSet::new(), scan, false)?));
- }
- _ => return Ok(None),
- };
-
- let child_plan = &*projection.input;
- let projection_is_empty = projection.expr.is_empty();
-
- let new_plan = match child_plan {
- LogicalPlan::Projection(child_projection) => {
- let new_plan = merge_projection(projection, child_projection)?;
- self.try_optimize(&new_plan, _config)?.unwrap_or(new_plan)
- }
- LogicalPlan::Join(join) => {
- // collect column in on/filter in join and projection.
- let mut push_columns: HashSet<Column> = HashSet::new();
- for e in projection.expr.iter() {
- expr_to_columns(e, &mut push_columns)?;
- }
- for (l, r) in join.on.iter() {
- expr_to_columns(l, &mut push_columns)?;
- expr_to_columns(r, &mut push_columns)?;
- }
- if let Some(expr) = &join.filter {
- expr_to_columns(expr, &mut push_columns)?;
- }
-
- let new_left = generate_projection(
- &push_columns,
- join.left.schema(),
- join.left.clone(),
- )?;
- let new_right = generate_projection(
- &push_columns,
- join.right.schema(),
- join.right.clone(),
- )?;
- let new_join = child_plan.with_new_inputs(&[new_left, new_right])?;
-
- generate_plan!(projection_is_empty, plan, new_join)
- }
- LogicalPlan::CrossJoin(join) => {
- // collect column in on/filter in join and projection.
- let mut push_columns: HashSet<Column> = HashSet::new();
- for e in projection.expr.iter() {
- expr_to_columns(e, &mut push_columns)?;
- }
- let new_left = generate_projection(
- &push_columns,
- join.left.schema(),
- join.left.clone(),
- )?;
- let new_right = generate_projection(
- &push_columns,
- join.right.schema(),
- join.right.clone(),
- )?;
- let new_join = child_plan.with_new_inputs(&[new_left, new_right])?;
-
- generate_plan!(projection_is_empty, plan, new_join)
- }
- LogicalPlan::TableScan(scan)
- if !scan.projected_schema.fields().is_empty() =>
- {
- let mut used_columns: HashSet<Column> = HashSet::new();
- if projection_is_empty {
- push_down_scan(&used_columns, scan, true)?
- } else {
- for expr in projection.expr.iter() {
- expr_to_columns(expr, &mut used_columns)?;
- }
- let new_scan = push_down_scan(&used_columns, scan, true)?;
-
- plan.with_new_inputs(&[new_scan])?
- }
- }
- LogicalPlan::Union(union) => {
- let mut required_columns = HashSet::new();
- exprlist_to_columns(&projection.expr, &mut required_columns)?;
- // When there is no projection, we need to add the first column to the projection
- // Because if push empty down, children may output different columns.
- if required_columns.is_empty() {
- required_columns.insert(union.schema.fields()[0].qualified_column());
- }
- // we don't push down projection expr, we just prune columns, so we just push column
- // because push expr may cause more cost.
- let projection_column_exprs = get_expr(&required_columns, &union.schema)?;
- let mut inputs = Vec::with_capacity(union.inputs.len());
- for input in &union.inputs {
- let mut replace_map = HashMap::new();
- for (i, field) in input.schema().fields().iter().enumerate() {
- replace_map.insert(
- union.schema.fields()[i].qualified_name(),
- Expr::Column(field.qualified_column()),
- );
- }
-
- let exprs = projection_column_exprs
- .iter()
- .map(|expr| replace_cols_by_name(expr.clone(), &replace_map))
- .collect::<Result<Vec<_>>>()?;
-
- inputs.push(Arc::new(LogicalPlan::Projection(Projection::try_new(
- exprs,
- input.clone(),
- )?)))
- }
- // create schema of all used columns
- let schema = DFSchema::new_with_metadata(
- exprlist_to_fields(&projection_column_exprs, child_plan)?,
- union.schema.metadata().clone(),
- )?;
- let new_union = LogicalPlan::Union(Union {
- inputs,
- schema: Arc::new(schema),
- });
-
- generate_plan!(projection_is_empty, plan, new_union)
- }
- LogicalPlan::SubqueryAlias(subquery_alias) => {
- let replace_map = generate_column_replace_map(subquery_alias);
- let mut required_columns = HashSet::new();
- exprlist_to_columns(&projection.expr, &mut required_columns)?;
-
- let new_required_columns = required_columns
- .iter()
- .map(|c| {
- replace_map.get(c).cloned().ok_or_else(|| {
- DataFusionError::Internal("replace column failed".to_string())
- })
- })
- .collect::<Result<HashSet<_>>>()?;
-
- let new_expr =
- get_expr(&new_required_columns, subquery_alias.input.schema())?;
- let new_projection = LogicalPlan::Projection(Projection::try_new(
- new_expr,
- subquery_alias.input.clone(),
- )?);
- let new_alias = child_plan.with_new_inputs(&[new_projection])?;
-
- generate_plan!(projection_is_empty, plan, new_alias)
- }
- LogicalPlan::Aggregate(agg) => {
- let mut required_columns = HashSet::new();
- exprlist_to_columns(&projection.expr, &mut required_columns)?;
- // Gather all columns needed for expressions in this Aggregate
- let mut new_aggr_expr = vec![];
- for e in agg.aggr_expr.iter() {
- let column = Column::from_name(e.display_name()?);
- if required_columns.contains(&column) {
- new_aggr_expr.push(e.clone());
- }
- }
-
- // if new_aggr_expr emtpy and aggr is COUNT(UInt8(1)), push it
- if new_aggr_expr.is_empty() && agg.aggr_expr.len() == 1 {
- if let Expr::AggregateFunction(AggregateFunction {
- fun, args, ..
- }) = &agg.aggr_expr[0]
- {
- if matches!(fun, datafusion_expr::AggregateFunction::Count)
- && args.len() == 1
- && args[0] == Expr::Literal(UInt8(Some(1)))
- {
- new_aggr_expr.push(agg.aggr_expr[0].clone());
- }
- }
- }
-
- let new_agg = LogicalPlan::Aggregate(Aggregate::try_new(
- agg.input.clone(),
- agg.group_expr.clone(),
- new_aggr_expr,
- )?);
-
- generate_plan!(projection_is_empty, plan, new_agg)
- }
- LogicalPlan::Window(window) => {
- let mut required_columns = HashSet::new();
- exprlist_to_columns(&projection.expr, &mut required_columns)?;
- // Gather all columns needed for expressions in this Window
- let mut new_window_expr = vec![];
- for e in window.window_expr.iter() {
- let column = Column::from_name(e.display_name()?);
- if required_columns.contains(&column) {
- new_window_expr.push(e.clone());
- }
- }
-
- if new_window_expr.is_empty() {
- // none columns in window expr are needed, remove the window expr
- let input = window.input.clone();
- let new_window = restrict_outputs(input.clone(), &required_columns)?
- .unwrap_or((*input).clone());
-
- generate_plan!(projection_is_empty, plan, new_window)
- } else {
- let mut referenced_inputs = HashSet::new();
- exprlist_to_columns(&new_window_expr, &mut referenced_inputs)?;
- window
- .input
- .schema()
- .fields()
- .iter()
- .filter(|f| required_columns.contains(&f.qualified_column()))
- .for_each(|f| {
- referenced_inputs.insert(f.qualified_column());
- });
-
- let input = window.input.clone();
- let new_input = restrict_outputs(input.clone(), &referenced_inputs)?
- .unwrap_or((*input).clone());
- let new_window = LogicalPlanBuilder::from(new_input)
- .window(new_window_expr)?
- .build()?;
-
- generate_plan!(projection_is_empty, plan, new_window)
- }
- }
- LogicalPlan::Filter(filter) => {
- if can_eliminate(projection, child_plan.schema()) {
- // when projection schema == filter schema, we can commute directly.
- let new_proj =
- plan.with_new_inputs(&[filter.input.as_ref().clone()])?;
- child_plan.with_new_inputs(&[new_proj])?
- } else {
- let mut required_columns = HashSet::new();
- exprlist_to_columns(&projection.expr, &mut required_columns)?;
- exprlist_to_columns(
- &[filter.predicate.clone()],
- &mut required_columns,
- )?;
-
- let new_expr = get_expr(&required_columns, filter.input.schema())?;
- let new_projection = LogicalPlan::Projection(Projection::try_new(
- new_expr,
- filter.input.clone(),
- )?);
- let new_filter = child_plan.with_new_inputs(&[new_projection])?;
-
- generate_plan!(projection_is_empty, plan, new_filter)
- }
- }
- LogicalPlan::Sort(sort) => {
- if can_eliminate(projection, child_plan.schema()) {
- // can commute
- let new_proj = plan.with_new_inputs(&[(*sort.input).clone()])?;
- child_plan.with_new_inputs(&[new_proj])?
- } else {
- let mut required_columns = HashSet::new();
- exprlist_to_columns(&projection.expr, &mut required_columns)?;
- exprlist_to_columns(&sort.expr, &mut required_columns)?;
-
- let new_expr = get_expr(&required_columns, sort.input.schema())?;
- let new_projection = LogicalPlan::Projection(Projection::try_new(
- new_expr,
- sort.input.clone(),
- )?);
- let new_sort = child_plan.with_new_inputs(&[new_projection])?;
-
- generate_plan!(projection_is_empty, plan, new_sort)
- }
- }
- LogicalPlan::Limit(limit) => {
- // can commute
- let new_proj = plan.with_new_inputs(&[limit.input.as_ref().clone()])?;
- child_plan.with_new_inputs(&[new_proj])?
- }
- _ => return Ok(None),
- };
-
- Ok(Some(new_plan))
- }
-
- fn name(&self) -> &str {
- "push_down_projection"
- }
-
- fn apply_order(&self) -> Option<ApplyOrder> {
- Some(ApplyOrder::TopDown)
- }
-}
-
-impl PushDownProjection {
- #[allow(missing_docs)]
- pub fn new() -> Self {
- Self {}
- }
-}
-
-fn generate_column_replace_map(
- subquery_alias: &SubqueryAlias,
-) -> HashMap<Column, Column> {
- subquery_alias
- .input
- .schema()
- .fields()
- .iter()
- .enumerate()
- .map(|(i, field)| {
- (
- subquery_alias.schema.fields()[i].qualified_column(),
- field.qualified_column(),
- )
- })
- .collect()
-}
-
-pub fn collect_projection_expr(projection: &Projection) -> HashMap<String, Expr> {
- projection
- .schema
- .fields()
- .iter()
- .enumerate()
- .flat_map(|(i, field)| {
- // strip alias, as they should not be part of filters
- let expr = match &projection.expr[i] {
- Expr::Alias(Alias { expr, .. }) => expr.as_ref().clone(),
- expr => expr.clone(),
- };
-
- // Convert both qualified and unqualified fields
- [
- (field.name().clone(), expr.clone()),
- (field.qualified_name(), expr),
- ]
- })
- .collect::<HashMap<_, _>>()
-}
-
-/// Get the projection exprs from columns in the order of the schema
-fn get_expr(columns: &HashSet<Column>, schema: &DFSchemaRef) -> Result<Vec<Expr>> {
- let expr = schema
- .fields()
- .iter()
- .flat_map(|field| {
- let qc = field.qualified_column();
- let uqc = field.unqualified_column();
- if columns.contains(&qc) || columns.contains(&uqc) {
- Some(Expr::Column(qc))
- } else {
- None
- }
- })
- .collect::<Vec<Expr>>();
- if columns.len() != expr.len() {
- plan_err!("required columns can't push down, columns: {columns:?}")
- } else {
- Ok(expr)
- }
-}
-
-fn generate_projection(
- used_columns: &HashSet<Column>,
- schema: &DFSchemaRef,
- input: Arc<LogicalPlan>,
-) -> Result<LogicalPlan> {
- let expr = schema
- .fields()
- .iter()
- .flat_map(|field| {
- let column = field.qualified_column();
- if used_columns.contains(&column) {
- Some(Expr::Column(column))
- } else {
- None
- }
- })
- .collect::<Vec<_>>();
-
- Ok(LogicalPlan::Projection(Projection::try_new(expr, input)?))
-}
-
-fn push_down_scan(
- used_columns: &HashSet<Column>,
- scan: &TableScan,
- has_projection: bool,
-) -> Result<LogicalPlan> {
- // once we reach the table scan, we can use the accumulated set of column
- // names to construct the set of column indexes in the scan
- //
- // we discard non-existing columns because some column names are not part of the schema,
- // e.g. when the column derives from an aggregation
- //
- // Use BTreeSet to remove potential duplicates (e.g. union) as
- // well as to sort the projection to ensure deterministic behavior
- let schema = scan.source.schema();
- let mut projection: BTreeSet<usize> = used_columns
- .iter()
- .filter(|c| {
- c.relation.is_none() || c.relation.as_ref().unwrap() == &scan.table_name
- })
- .map(|c| schema.index_of(&c.name))
- .filter_map(ArrowResult::ok)
- .collect();
-
- if !has_projection && projection.is_empty() {
- // for table scan without projection, we default to return all columns
- projection = schema
- .fields()
- .iter()
- .enumerate()
- .map(|(i, _)| i)
- .collect::<BTreeSet<usize>>();
- }
-
- // Building new projection from BTreeSet
- // preserving source projection order if it exists
- let projection = if let Some(original_projection) = &scan.projection {
- original_projection
- .clone()
- .into_iter()
- .filter(|idx| projection.contains(idx))
- .collect::<Vec<_>>()
- } else {
- projection.into_iter().collect::<Vec<_>>()
- };
-
- TableScan::try_new(
- scan.table_name.clone(),
- scan.source.clone(),
- Some(projection),
- scan.filters.clone(),
- scan.fetch,
- )
- .map(LogicalPlan::TableScan)
-}
-
-fn restrict_outputs(
- plan: Arc<LogicalPlan>,
- permitted_outputs: &HashSet<Column>,
-) -> Result<Option<LogicalPlan>> {
- let schema = plan.schema();
- if permitted_outputs.len() == schema.fields().len() {
- return Ok(None);
- }
- Ok(Some(generate_projection(
- permitted_outputs,
- schema,
- plan.clone(),
- )?))
-}
-
#[cfg(test)]
mod tests {
use std::collections::HashMap;
+ use std::sync::Arc;
use std::vec;
- use super::*;
- use crate::eliminate_project::EliminateProjection;
+ use crate::optimize_projections::OptimizeProjections;
use crate::optimizer::Optimizer;
use crate::test::*;
use crate::OptimizerContext;
use arrow::datatypes::{DataType, Field, Schema};
- use datafusion_common::{DFField, DFSchema};
+ use datafusion_common::{Column, DFField, DFSchema, Result};
use datafusion_expr::builder::table_scan_with_filters;
use datafusion_expr::expr::{self, Cast};
use datafusion_expr::logical_plan::{
builder::LogicalPlanBuilder, table_scan, JoinType,
};
use datafusion_expr::{
- col, count, lit, max, min, AggregateFunction, Expr, WindowFrame, WindowFunction,
+ col, count, lit, max, min, AggregateFunction, Expr, LogicalPlan, Projection,
+ WindowFrame, WindowFunction,
};
#[test]
@@ -867,7 +363,7 @@ mod tests {
// Build the LogicalPlan directly (don't use PlanBuilder), so
// that the Column references are unqualified (e.g. their
// relation is `None`). PlanBuilder resolves the expressions
- let expr = vec![col("a"), col("b")];
+ let expr = vec![col("test.a"), col("test.b")];
let plan =
LogicalPlan::Projection(Projection::try_new(expr, Arc::new(table_scan))?);
@@ -1126,24 +622,14 @@ mod tests {
}
fn optimize(plan: &LogicalPlan) -> Result<LogicalPlan> {
- let optimizer = Optimizer::with_rules(vec![
- Arc::new(PushDownProjection::new()),
- Arc::new(EliminateProjection::new()),
- ]);
- let mut optimized_plan = optimizer
+ let optimizer = Optimizer::with_rules(vec![Arc::new(OptimizeProjections::new())]);
+ let optimized_plan = optimizer
.optimize_recursively(
optimizer.rules.get(0).unwrap(),
plan,
&OptimizerContext::new(),
)?
.unwrap_or_else(|| plan.clone());
- optimized_plan = optimizer
- .optimize_recursively(
- optimizer.rules.get(1).unwrap(),
- &optimized_plan,
- &OptimizerContext::new(),
- )?
- .unwrap_or(optimized_plan);
Ok(optimized_plan)
}
}
diff --git a/datafusion/optimizer/tests/optimizer_integration.rs b/datafusion/optimizer/tests/optimizer_integration.rs
index 872071e52f..e593b07361 100644
--- a/datafusion/optimizer/tests/optimizer_integration.rs
+++ b/datafusion/optimizer/tests/optimizer_integration.rs
@@ -185,8 +185,9 @@ fn between_date32_plus_interval() -> Result<()> {
let plan = test_sql(sql)?;
let expected =
"Aggregate: groupBy=[[]], aggr=[[COUNT(Int64(1))]]\
- \n Filter: test.col_date32 >= Date32(\"10303\") AND test.col_date32 <= Date32(\"10393\")\
- \n TableScan: test projection=[col_date32]";
+ \n Projection: \
+ \n Filter: test.col_date32 >= Date32(\"10303\") AND test.col_date32 <= Date32(\"10393\")\
+ \n TableScan: test projection=[col_date32]";
assert_eq!(expected, format!("{plan:?}"));
Ok(())
}
@@ -198,8 +199,9 @@ fn between_date64_plus_interval() -> Result<()> {
let plan = test_sql(sql)?;
let expected =
"Aggregate: groupBy=[[]], aggr=[[COUNT(Int64(1))]]\
- \n Filter: test.col_date64 >= Date64(\"890179200000\") AND test.col_date64 <= Date64(\"897955200000\")\
- \n TableScan: test projection=[col_date64]";
+ \n Projection: \
+ \n Filter: test.col_date64 >= Date64(\"890179200000\") AND test.col_date64 <= Date64(\"897955200000\")\
+ \n TableScan: test projection=[col_date64]";
assert_eq!(expected, format!("{plan:?}"));
Ok(())
}
diff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.slt
index a14c179326..8859005548 100644
--- a/datafusion/sqllogictest/test_files/aggregate.slt
+++ b/datafusion/sqllogictest/test_files/aggregate.slt
@@ -2799,10 +2799,9 @@ query TT
EXPLAIN SELECT c2, c3 FROM aggregate_test_100 group by rollup(c2, c3) limit 3;
----
logical_plan
-Projection: aggregate_test_100.c2, aggregate_test_100.c3
---Limit: skip=0, fetch=3
-----Aggregate: groupBy=[[ROLLUP (aggregate_test_100.c2, aggregate_test_100.c3)]], aggr=[[]]
-------TableScan: aggregate_test_100 projection=[c2, c3]
+Limit: skip=0, fetch=3
+--Aggregate: groupBy=[[ROLLUP (aggregate_test_100.c2, aggregate_test_100.c3)]], aggr=[[]]
+----TableScan: aggregate_test_100 projection=[c2, c3]
physical_plan
GlobalLimitExec: skip=0, fetch=3
--AggregateExec: mode=Final, gby=[c2@0 as c2, c3@1 as c3], aggr=[], lim=[3]
diff --git a/datafusion/sqllogictest/test_files/explain.slt b/datafusion/sqllogictest/test_files/explain.slt
index c8eff2f301..18792735ff 100644
--- a/datafusion/sqllogictest/test_files/explain.slt
+++ b/datafusion/sqllogictest/test_files/explain.slt
@@ -192,7 +192,6 @@ logical_plan after decorrelate_predicate_subquery SAME TEXT AS ABOVE
logical_plan after scalar_subquery_to_join SAME TEXT AS ABOVE
logical_plan after extract_equijoin_predicate SAME TEXT AS ABOVE
logical_plan after simplify_expressions SAME TEXT AS ABOVE
-logical_plan after merge_projection SAME TEXT AS ABOVE
logical_plan after rewrite_disjunctive_predicate SAME TEXT AS ABOVE
logical_plan after eliminate_duplicated_expr SAME TEXT AS ABOVE
logical_plan after eliminate_filter SAME TEXT AS ABOVE
@@ -209,11 +208,7 @@ logical_plan after single_distinct_aggregation_to_group_by SAME TEXT AS ABOVE
logical_plan after simplify_expressions SAME TEXT AS ABOVE
logical_plan after unwrap_cast_in_comparison SAME TEXT AS ABOVE
logical_plan after common_sub_expression_eliminate SAME TEXT AS ABOVE
-logical_plan after push_down_projection
-Projection: simple_explain_test.a, simple_explain_test.b, simple_explain_test.c
---TableScan: simple_explain_test projection=[a, b, c]
-logical_plan after eliminate_projection TableScan: simple_explain_test projection=[a, b, c]
-logical_plan after push_down_limit SAME TEXT AS ABOVE
+logical_plan after optimize_projections TableScan: simple_explain_test projection=[a, b, c]
logical_plan after eliminate_nested_union SAME TEXT AS ABOVE
logical_plan after simplify_expressions SAME TEXT AS ABOVE
logical_plan after unwrap_cast_in_comparison SAME TEXT AS ABOVE
@@ -223,7 +218,6 @@ logical_plan after decorrelate_predicate_subquery SAME TEXT AS ABOVE
logical_plan after scalar_subquery_to_join SAME TEXT AS ABOVE
logical_plan after extract_equijoin_predicate SAME TEXT AS ABOVE
logical_plan after simplify_expressions SAME TEXT AS ABOVE
-logical_plan after merge_projection SAME TEXT AS ABOVE
logical_plan after rewrite_disjunctive_predicate SAME TEXT AS ABOVE
logical_plan after eliminate_duplicated_expr SAME TEXT AS ABOVE
logical_plan after eliminate_filter SAME TEXT AS ABOVE
@@ -240,9 +234,7 @@ logical_plan after single_distinct_aggregation_to_group_by SAME TEXT AS ABOVE
logical_plan after simplify_expressions SAME TEXT AS ABOVE
logical_plan after unwrap_cast_in_comparison SAME TEXT AS ABOVE
logical_plan after common_sub_expression_eliminate SAME TEXT AS ABOVE
-logical_plan after push_down_projection SAME TEXT AS ABOVE
-logical_plan after eliminate_projection SAME TEXT AS ABOVE
-logical_plan after push_down_limit SAME TEXT AS ABOVE
+logical_plan after optimize_projections SAME TEXT AS ABOVE
logical_plan TableScan: simple_explain_test projection=[a, b, c]
initial_physical_plan CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/example.csv]]}, projection=[a, b, c], has_header=true
initial_physical_plan_with_stats CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/example.csv]]}, projection=[a, b, c], has_header=true, statistics=[Rows=Absent, Bytes=Absent, [(Col[0]:),(Col[1]:),(Col[2]:)]]
diff --git a/datafusion/sqllogictest/test_files/limit.slt b/datafusion/sqllogictest/test_files/limit.slt
index 9e093336a1..182195112e 100644
--- a/datafusion/sqllogictest/test_files/limit.slt
+++ b/datafusion/sqllogictest/test_files/limit.slt
@@ -361,18 +361,20 @@ EXPLAIN SELECT COUNT(*) FROM (SELECT a FROM t1 WHERE a > 3 LIMIT 3 OFFSET 6);
----
logical_plan
Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1)) AS COUNT(*)]]
---Limit: skip=6, fetch=3
-----Filter: t1.a > Int32(3)
-------TableScan: t1 projection=[a]
+--Projection:
+----Limit: skip=6, fetch=3
+------Filter: t1.a > Int32(3)
+--------TableScan: t1 projection=[a]
physical_plan
AggregateExec: mode=Final, gby=[], aggr=[COUNT(*)]
--CoalescePartitionsExec
----AggregateExec: mode=Partial, gby=[], aggr=[COUNT(*)]
------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
---------GlobalLimitExec: skip=6, fetch=3
-----------CoalesceBatchesExec: target_batch_size=8192
-------------FilterExec: a@0 > 3
---------------MemoryExec: partitions=1, partition_sizes=[1]
+--------ProjectionExec: expr=[]
+----------GlobalLimitExec: skip=6, fetch=3
+------------CoalesceBatchesExec: target_batch_size=8192
+--------------FilterExec: a@0 > 3
+----------------MemoryExec: partitions=1, partition_sizes=[1]
query I
SELECT COUNT(*) FROM (SELECT a FROM t1 WHERE a > 3 LIMIT 3 OFFSET 6);
diff --git a/datafusion/sqllogictest/test_files/subquery.slt b/datafusion/sqllogictest/test_files/subquery.slt
index 4729c3f010..430e676fa4 100644
--- a/datafusion/sqllogictest/test_files/subquery.slt
+++ b/datafusion/sqllogictest/test_files/subquery.slt
@@ -437,7 +437,7 @@ Projection: t1.t1_id, (<subquery>) AS t2_int
------Projection: t2.t2_int
--------Filter: t2.t2_int = outer_ref(t1.t1_int)
----------TableScan: t2
---TableScan: t1 projection=[t1_id]
+--TableScan: t1 projection=[t1_id, t1_int]
query TT
explain SELECT t1_id from t1 where t1_int = (SELECT t2_int FROM t2 WHERE t2.t2_int = t1.t1_int limit 1)
@@ -484,27 +484,29 @@ query TT
explain SELECT t1_id, t1_name FROM t1 WHERE EXISTS (SELECT sum(t1.t1_int + t2.t2_id) FROM t2 WHERE t1.t1_name = t2.t2_name)
----
logical_plan
-Filter: EXISTS (<subquery>)
---Subquery:
-----Projection: SUM(outer_ref(t1.t1_int) + t2.t2_id)
-------Aggregate: groupBy=[[]], aggr=[[SUM(CAST(outer_ref(t1.t1_int) + t2.t2_id AS Int64))]]
---------Filter: outer_ref(t1.t1_name) = t2.t2_name
-----------TableScan: t2
---TableScan: t1 projection=[t1_id, t1_name]
+Projection: t1.t1_id, t1.t1_name
+--Filter: EXISTS (<subquery>)
+----Subquery:
+------Projection: SUM(outer_ref(t1.t1_int) + t2.t2_id)
+--------Aggregate: groupBy=[[]], aggr=[[SUM(CAST(outer_ref(t1.t1_int) + t2.t2_id AS Int64))]]
+----------Filter: outer_ref(t1.t1_name) = t2.t2_name
+------------TableScan: t2
+----TableScan: t1 projection=[t1_id, t1_name, t1_int]
#support_agg_correlated_columns2
query TT
explain SELECT t1_id, t1_name FROM t1 WHERE EXISTS (SELECT count(*) FROM t2 WHERE t1.t1_name = t2.t2_name having sum(t1_int + t2_id) >0)
----
logical_plan
-Filter: EXISTS (<subquery>)
---Subquery:
-----Projection: COUNT(*)
-------Filter: SUM(outer_ref(t1.t1_int) + t2.t2_id) > Int64(0)
---------Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1)) AS COUNT(*), SUM(CAST(outer_ref(t1.t1_int) + t2.t2_id AS Int64))]]
-----------Filter: outer_ref(t1.t1_name) = t2.t2_name
-------------TableScan: t2
---TableScan: t1 projection=[t1_id, t1_name]
+Projection: t1.t1_id, t1.t1_name
+--Filter: EXISTS (<subquery>)
+----Subquery:
+------Projection: COUNT(*)
+--------Filter: SUM(outer_ref(t1.t1_int) + t2.t2_id) > Int64(0)
+----------Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1)) AS COUNT(*), SUM(CAST(outer_ref(t1.t1_int) + t2.t2_id AS Int64))]]
+------------Filter: outer_ref(t1.t1_name) = t2.t2_name
+--------------TableScan: t2
+----TableScan: t1 projection=[t1_id, t1_name, t1_int]
#support_join_correlated_columns
query TT
@@ -1012,3 +1014,31 @@ catan-prod1-daily success catan-prod1-daily high
#2
#3
#4
+
+statement ok
+create table t(a bigint);
+
+# Result of query below shouldn't depend on
+# number of optimization passes
+# See issue: https://github.com/apache/arrow-datafusion/issues/8296
+statement ok
+set datafusion.optimizer.max_passes = 1;
+
+query TT
+explain select a/2, a/2 + 1 from t
+----
+logical_plan
+Projection: t.a / Int64(2)Int64(2)t.a AS t.a / Int64(2), t.a / Int64(2)Int64(2)t.a AS t.a / Int64(2) + Int64(1)
+--Projection: t.a / Int64(2) AS t.a / Int64(2)Int64(2)t.a
+----TableScan: t projection=[a]
+
+statement ok
+set datafusion.optimizer.max_passes = 3;
+
+query TT
+explain select a/2, a/2 + 1 from t
+----
+logical_plan
+Projection: t.a / Int64(2)Int64(2)t.a AS t.a / Int64(2), t.a / Int64(2)Int64(2)t.a AS t.a / Int64(2) + Int64(1)
+--Projection: t.a / Int64(2) AS t.a / Int64(2)Int64(2)t.a
+----TableScan: t projection=[a]
diff --git a/datafusion/sqllogictest/test_files/window.slt b/datafusion/sqllogictest/test_files/window.slt
index 55b8843a0b..b2491478d8 100644
--- a/datafusion/sqllogictest/test_files/window.slt
+++ b/datafusion/sqllogictest/test_files/window.slt
@@ -1731,26 +1731,28 @@ logical_plan
Projection: COUNT(*) AS global_count
--Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1)) AS COUNT(*)]]
----SubqueryAlias: a
-------Sort: aggregate_test_100.c1 ASC NULLS LAST
---------Aggregate: groupBy=[[aggregate_test_100.c1]], aggr=[[]]
-----------Projection: aggregate_test_100.c1
-------------Filter: aggregate_test_100.c13 != Utf8("C2GT5KVyOPZpgKVl110TyZO0NcJ434")
---------------TableScan: aggregate_test_100 projection=[c1, c13], partial_filters=[aggregate_test_100.c13 != Utf8("C2GT5KVyOPZpgKVl110TyZO0NcJ434")]
+------Projection:
+--------Sort: aggregate_test_100.c1 ASC NULLS LAST
+----------Aggregate: groupBy=[[aggregate_test_100.c1]], aggr=[[]]
+------------Projection: aggregate_test_100.c1
+--------------Filter: aggregate_test_100.c13 != Utf8("C2GT5KVyOPZpgKVl110TyZO0NcJ434")
+----------------TableScan: aggregate_test_100 projection=[c1, c13], partial_filters=[aggregate_test_100.c13 != Utf8("C2GT5KVyOPZpgKVl110TyZO0NcJ434")]
physical_plan
ProjectionExec: expr=[COUNT(*)@0 as global_count]
--AggregateExec: mode=Final, gby=[], aggr=[COUNT(*)]
----CoalescePartitionsExec
------AggregateExec: mode=Partial, gby=[], aggr=[COUNT(*)]
--------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=2
-----------AggregateExec: mode=FinalPartitioned, gby=[c1@0 as c1], aggr=[]
-------------CoalesceBatchesExec: target_batch_size=4096
---------------RepartitionExec: partitioning=Hash([c1@0], 2), input_partitions=2
-----------------AggregateExec: mode=Partial, gby=[c1@0 as c1], aggr=[]
-------------------ProjectionExec: expr=[c1@0 as c1]
---------------------CoalesceBatchesExec: target_batch_size=4096
-----------------------FilterExec: c13@1 != C2GT5KVyOPZpgKVl110TyZO0NcJ434
-------------------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
---------------------------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c1, c13], has_header=true
+----------ProjectionExec: expr=[]
+------------AggregateExec: mode=FinalPartitioned, gby=[c1@0 as c1], aggr=[]
+--------------CoalesceBatchesExec: target_batch_size=4096
+----------------RepartitionExec: partitioning=Hash([c1@0], 2), input_partitions=2
+------------------AggregateExec: mode=Partial, gby=[c1@0 as c1], aggr=[]
+--------------------ProjectionExec: expr=[c1@0 as c1]
+----------------------CoalesceBatchesExec: target_batch_size=4096
+------------------------FilterExec: c13@1 != C2GT5KVyOPZpgKVl110TyZO0NcJ434
+--------------------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+----------------------------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c1, c13], has_header=true
query I
SELECT count(*) as global_count FROM
diff --git a/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs b/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs
index cee3a34649..1c5dbe9ce8 100644
--- a/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs
+++ b/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs
@@ -501,10 +501,11 @@ async fn simple_intersect() -> Result<()> {
assert_expected_plan(
"SELECT COUNT(*) FROM (SELECT data.a FROM data INTERSECT SELECT data2.a FROM data2);",
"Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]]\
- \n LeftSemi Join: data.a = data2.a\
- \n Aggregate: groupBy=[[data.a]], aggr=[[]]\
- \n TableScan: data projection=[a]\
- \n TableScan: data2 projection=[a]",
+ \n Projection: \
+ \n LeftSemi Join: data.a = data2.a\
+ \n Aggregate: groupBy=[[data.a]], aggr=[[]]\
+ \n TableScan: data projection=[a]\
+ \n TableScan: data2 projection=[a]",
)
.await
}
@@ -514,10 +515,11 @@ async fn simple_intersect_table_reuse() -> Result<()> {
assert_expected_plan(
"SELECT COUNT(*) FROM (SELECT data.a FROM data INTERSECT SELECT data.a FROM data);",
"Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]]\
- \n LeftSemi Join: data.a = data.a\
- \n Aggregate: groupBy=[[data.a]], aggr=[[]]\
- \n TableScan: data projection=[a]\
- \n TableScan: data projection=[a]",
+ \n Projection: \
+ \n LeftSemi Join: data.a = data.a\
+ \n Aggregate: groupBy=[[data.a]], aggr=[[]]\
+ \n TableScan: data projection=[a]\
+ \n TableScan: data projection=[a]",
)
.await
}