Repository navigation
fix: prune unused inputs beneath computed repartition projections #27777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -169,6 +169,34 @@ impl ProjectionExec { | |||||||
| Self::try_from_projector(projector, input, overrides_metadata) | ||||||||
| } | ||||||||
|
|
||||||||
| pub(crate) fn with_pruned_input( | ||||||||
| &self, | ||||||||
| exprs: Vec<ProjectionExpr>, | ||||||||
| input: Arc<dyn ExecutionPlan>, | ||||||||
| ) -> Result<Self> { | ||||||||
| // Input pruning must preserve output values, metadata, ordering, and partitioning. | ||||||||
| let projector = self | ||||||||
| .projector | ||||||||
| .with_new_projection_preserving_schema(ProjectionExprs::new(exprs)); | ||||||||
| debug_assert_eq!( | ||||||||
| projector.output_schema(), | ||||||||
| projector | ||||||||
| .projection() | ||||||||
| .make_projector_with_schema_metadata( | ||||||||
| &input.schema(), | ||||||||
| self.schema().as_ref() | ||||||||
| )? | ||||||||
| .output_schema() | ||||||||
| ); | ||||||||
| Ok(Self { | ||||||||
| projector, | ||||||||
| input, | ||||||||
| metrics: ExecutionPlanMetricsSet::new(), | ||||||||
| cache: Arc::clone(&self.cache), | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rewrite can remove an ordering that an ancestor needs. The query then fails to plan. SET datafusion.execution.target_partitions = 4;
SET datafusion.execution.parquet.pushdown_filters = true;
COPY (SELECT value * 2 AS c, value AS a, value AS b FROM range(0, 1000) ORDER BY c)
TO 'ordered.parquet' STORED AS PARQUET;
CREATE EXTERNAL TABLE ordered STORED AS PARQUET LOCATION 'ordered.parquet' WITH ORDER (c ASC);
SELECT a + b AS k FROM ordered WHERE c = a + b ORDER BY k LIMIT 2;
The file is sorted by The projection keeps its cached properties here, so the rewrite does not see the loss. A possible fix is to recompute the properties and to compare the output orderings before and after the rewrite. If an ordering is lost, the rewrite can keep the columns that the input is sorted by. Could you add this query as an SLT case? |
||||||||
| overrides_metadata: self.overrides_metadata, | ||||||||
| }) | ||||||||
| } | ||||||||
|
|
||||||||
| fn try_from_projector( | ||||||||
| projector: Projector, | ||||||||
| input: Arc<dyn ExecutionPlan>, | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,10 @@ use crate::coalesce::LimitedBatchCoalescer; | |
| use crate::execution_plan::{CardinalityEffect, EvaluationType, SchedulingType}; | ||
| use crate::hash_utils::create_hashes; | ||
| use crate::metrics::{BaselineMetrics, SpillMetrics}; | ||
| use crate::projection::{ProjectionExec, all_columns, make_with_child, update_expr}; | ||
| use crate::projection::{ | ||
| ProjectionExec, ProjectionExpr, ProjectionExprs, all_columns, make_with_child, | ||
| update_expr, | ||
| }; | ||
| use crate::sorts::streaming_merge::StreamingMergeBuilder; | ||
| use crate::spill::spill_manager::SpillManager; | ||
| use crate::spill::spill_pool::{self, SpillPoolSink, SpillPoolWriter}; | ||
|
|
@@ -1951,6 +1954,37 @@ impl ExecutionPlan for RepartitionExec { | |
| &self, | ||
| projection: &ProjectionExec, | ||
| ) -> Result<Option<Arc<dyn ExecutionPlan>>> { | ||
| if !all_columns(projection.expr()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The inserted SET datafusion.execution.target_partitions = 4;
CREATE TABLE t AS
SELECT value AS c, named_struct('v', value, 'w', 1) AS s FROM range(0, 50000);
EXPLAIN FORMAT INDENT
SELECT s['v'] + 1 AS v, rn FROM (SELECT s, row_number() OVER (ORDER BY c) AS rn FROM t);The window column is shortened to -ProjectionExec: expr=[get_field(s@1, v) + 1 as v, rn@2 as rn]
+ProjectionExec: expr=[__datafusion_leaf_0@1 + 1 as v, rn@0 as rn]
RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
- BoundedWindowAggExec
+ ProjectionExec: expr=[rn@2 as rn, get_field(s@1, v) as __datafusion_leaf_0]
+ BoundedWindowAggExecI think that the leaf split from #27152 sees the inserted projection as an operator that absorbs the leaf. I did not measure the cost, and it is probably small for |
||
| && matches!(self.partitioning(), Partitioning::RoundRobinBatch(_)) | ||
| { | ||
| let indices = projection.projection_expr().column_indices(); | ||
| if indices.len() >= self.input().schema().fields().len() { | ||
| return Ok(None); | ||
| } | ||
|
|
||
| // Keep computations above repartitioning so they retain its parallelism. | ||
| let columns = ProjectionExprs::from_indices(&indices, &self.input().schema()); | ||
| let mut exprs = Vec::with_capacity(projection.expr().len()); | ||
| for expr in projection.expr() { | ||
| let Some(rewritten) = update_expr(&expr.expr, columns.as_ref(), false)? | ||
| else { | ||
| return Ok(None); | ||
| }; | ||
| exprs.push(ProjectionExpr::new(rewritten, expr.alias.clone())); | ||
| } | ||
| let input = Arc::new(ProjectionExec::try_new( | ||
| columns.iter().cloned(), | ||
| Arc::clone(self.input()), | ||
| )?); | ||
| let repartition = Arc::new(self.clone()).replace_children( | ||
| vec![input], | ||
| ReplaceChildrenOptions::new(ChildrenPropertiesMode::Recompute), | ||
| )?; | ||
| return Ok(Some(Arc::new( | ||
| projection.with_pruned_input(exprs, repartition)?, | ||
| ))); | ||
| } | ||
|
|
||
| // If the projection does not narrow the schema, we should not try to push it down. | ||
| if projection.expr().len() >= projection.input().schema().fields().len() { | ||
| return Ok(None); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please also add a non fuzz test for this? Specifically can we add a SLT test that shows the explain plan that changes with / without this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alamb done! 🙇