Skip to content

fix: prune columns that only a pushed-down predicate reads below round robin repartition - #27867

Draft
adriangb wants to merge 8 commits into
apache:mainfrom
adriangb:adriangb/prune-columns-round-robin
Draft

adriangb wants to merge 8 commits into
apache:mainfrom
adriangb:adriangb/prune-columns-round-robin

Conversation

@adriangb

@adriangb adriangb commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Stacked on #27925. The first four commits here belong to it. Please review only the last four.

This is an alternative to #27777. The diagnosis in the issue is by ivan-kiselev.

Rationale for this change

A scan emits columns that only its pushed-down predicate reads. They cross a round robin repartition before a computed projection drops them. A struct is not necessary:

SET datafusion.execution.target_partitions = 4;
SET datafusion.execution.parquet.pushdown_filters = true;
COPY (SELECT value AS timestamp, value * 3 AS period, value * 7 AS val FROM range(0, 20000))
  TO 'repro.parquet' STORED AS PARQUET;
CREATE EXTERNAL TABLE t STORED AS PARQUET LOCATION 'repro.parquet';
EXPLAIN FORMAT INDENT SELECT val * period AS result FROM t WHERE timestamp >= 0;
-ProjectionExec: expr=[val@2 * period@1 as result]
+ProjectionExec: expr=[val@1 * period@0 as result]
   RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
-    DataSourceExec: projection=[timestamp, period, val], predicate=timestamp@0 >= 0
+    DataSourceExec: projection=[period, val], predicate=timestamp@0 >= 0

What changes are included in this PR?

#27152 splits a projection across a round robin repartition, but only if the projection has a leaf expression. This PR also splits it if that prunes a column.

 try_split_projection
-  no leaf expression                        -> keep the plan
-  the child declines the lower projection   -> keep the plan
+  no leaf expression and no column to prune -> keep the plan
+  the child declines the lower projection:
+    leaf expressions                        -> keep the plan
+    only pruning                            -> leave a ProjectionExec below the repartition

Trade-offs

A pruning ProjectionExec can stay in the plan even if the column it drops is narrow.

If the scan absorbs only part of a filter, the FilterExec that stays takes the projection, and the split does not see it. A column that only the absorbed part reads still crosses the repartition. An SLT case pins this plan.

The ordering check also applies to leaf splits. Below an operator that is not on the list it can keep a sort column that nothing needs.

Building a smaller plan costs planning time. The cost is in ProjectionExec::try_new, which computes the equivalence properties again. Median of 15 interleaved runs on an M-series laptop, release-nonlto, physical planning only:

Query main before the ordering check was restricted in c43952d this PR
100 expressions on a 200 column table 1130 µs 2129 µs (+88%) 1515 µs (+34%)
one expression that reads 100 of 200 columns 1333 µs 2600 µs (+95%) 1806 µs (+35%)
union of 32 pruned scans 1900 µs 2728 µs (+44%) 2314 µs (+22%)
16 nested Top-Ks 2184 µs 2681 µs (+23%) 2226 µs (+2%)
simple query that prunes a column 77 µs 98 µs (+26%) 87 µs (+13%)
output order is derived 104 µs 117 µs (+13%) 119 µs (+14%)
queries that the split does not change 38 to 153 µs -6% to +2% -3% to +3%

Benchmarks

Median of 15 interleaved runs on an idle c4a-standard-16 VM. Profile release-nonlto, target_partitions = 4, pushdown_filters = true, repartition_file_scans = false. t has 10M rows. e has 3M rows with a 97 byte url. main is bb1dad1. "PR 27777" is the Rust diff of 858edef applied to that main. "This PR" in this table and the next is b163aed.

Query main PR 27777 this PR round robin output_bytes (main / both PRs)
issue query on t 399 ms 354 ms 356 ms 352.9 / 152.6 MB
no struct, predicate always true 297 ms 200 ms 201 ms 228.9 / 152.6 MB
no struct, half the rows pass 327 ms 314 ms 314 ms 114.9 / 76.6 MB
wide predicate column url 246 ms 61 ms 61 ms 667.4 / 45.9 MB
url predicate below LIMIT 2000000 93 ms 24 ms 24 ms 485.7 / 30.6 MB
wide predicate column, slow expression, 60 MB limit 655 ms 573 ms 573 ms 644.5 / 22.9 MB
ORDER BY url LIMIT 300000 944 ms 941 ms 942 ms 10.4 GB / 4.6 MB
window over url 597 ms 594 ms 596 ms 103.3 GB / 22.9 MB
window over a narrow column 475 ms 471 ms 478 ms 152.6 / 76.3 MB
GROUP BY, plan not affected 210 ms 209 ms 210 ms 152.6 MB for all

A ProjectionExec that nothing absorbs shows no clear time difference, but it lowers the memory that the query needs. This is ORDER BY url LIMIT 300000 with a slow expression above it:

Memory limit main PR 27777 this PR
1300 MB fails, 15 of 15 runs 978 ms 971 ms
1500 MB fails, 15 of 15 runs 970 ms 979 ms
1700 MB 992 ms, repartition spills 5.7 MB 984 ms, no spill 983 ms, no spill
2000 MB 977 ms, repartition spills 2.8 MB 992 ms, no spill 985 ms, no spill

On main the allocation that fails is that of SortPreservingMergeExec, which shares the pool with the repartition. output_bytes counts a string buffer again for each batch that shares it, so the GB values are not memory use. A checksum of the answers agrees for every query that completes, except the bare LIMIT, which returns an arbitrary set of rows.

AI assistance

Claude Code wrote the code, the tests and this description at the request of the PR author.

What is the testing strategy for this PR?

Review the last four commits one by one: a mechanical rename, the tests with the plans without pruning, the pruning, and the ProjectionExec that stays if nothing absorbs it. No expectation that exists on main changes.

  • Plans that change: the issue query, the same without a struct, duplicate columns, zero referenced columns, CoalescePartitionsExec over a scan, and the case from fix: prune unused inputs beneath computed repartition projections #27777.
  • Plans that gain a pruning projection: two windows, a merge with a limit, and a Top-K of which no column is read.
  • Plans that must not change: the ordering queries, and a window where get_field must stay above the repartition.
  • A differential test, not part of this PR: 20,736 generated queries with constants, equalities, grouping, joins, unions and windows, run on main and on this PR. The results agree wherever main plans the query. None newly fails to plan. Before the fix, 428 did.
  • Unit tests for two plans that no known SQL produces: a CoalescePartitionsExec whose input absorbs nothing, and a hash repartition.

Are there any user-facing changes?

No public API changes. EXPLAIN can show fewer columns in a scan.

🤖 Generated with Claude Code

@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Nov 30, 2026
let new_properties = new_projection.equivalence_properties();
for ordering in plan.equivalence_properties().oeq_class().iter() {
if !new_properties.ordering_satisfy(ordering.iter().cloned())? {
return Ok(None);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is giving up entirely the right move here? Are there scenarios where soime projection pruning just not the columns that order depends on would be the right move?

let new_child = new_child
.transform_down(remove_unnecessary_projections)
.data()?;
if leaves.is_empty() && count_projections(&new_child)? > count_projections(child)? {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Could we add a comment explaining what this condition is suppossed to do?

let child = repartition.input();
let child_schema = child.schema();
let passthrough_indices = collect_outer_columns(projection.expr(), &leaves)?;
if leaves.is_empty() && passthrough_indices.len() == child_schema.fields().len() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same here, a comment explaining what the condition is / why we need it would be great.

@ivan-kiselev

Copy link
Copy Markdown

Found one regression with a model here:

SET datafusion.execution.target_partitions = 4;
SET datafusion.execution.batch_size = 2;
SET datafusion.execution.parquet.pushdown_filters = true;

COPY (
  SELECT column1 AS c, column2 AS a, column3 AS b
  FROM VALUES (6, 5, 1), (6, 4, 2), (6, -1, 7), (6, 9, -3)
) TO 'constant-input.parquet' STORED AS PARQUET;

CREATE EXTERNAL TABLE t STORED AS PARQUET
LOCATION 'constant-input.parquet';

SELECT a + b + 1 AS k, a
FROM t
WHERE c = a + b AND c + 1 = 7
ORDER BY k, a;

Pre-PR optimizer returnins (7, -1), (7, 4), (7, 5), (7, 9).
Head of this branch: SanityCheckPlan: order and distribution requirements stopped holding after physical optimizer rule 'ProjectionPushdown

@adriangb

adriangb commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Thank you, this is a real bug. c makes k constant, so the sort leaves k out of its key. The split then dropped c, and k was no longer known to be constant. The guard compared only orderings.

72e7b57 fixes it, and your query is now an SLT case. If you cherry-picked this PR before that commit, please also take 72e7b57 and c43952d.

@adriangb
adriangb marked this pull request as ready for review December 1, 2026 19:29
adriangb and others added 8 commits December 1, 2026 12:15
A check of the split needs to build one before the lower half moves down.
No behavior change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Three of these queries fail to plan. The split drops a column that gives the
output its order, or makes it constant, and an ancestor relies on that.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The split compares the output with that of a trial split. If the trial loses a
constant, an equality or an ordering, the lower half also passes through the
columns that it came from. It checks again after the lower half has moved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…them

Otherwise the split reads a sort column, and builds a trial split, for an
ordering that nothing needs. A sort replaces the ordering of its input, and
`OutputRequirementExec` declares the one that the query needs. An operator
that is not known to declare every ordering it relies on keeps the check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Mechanical. The next commits also split a projection that has no leaf.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The plans are those without pruning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A scan outputs the columns that its predicate reads, and a round robin
repartition keeps them alive. Split a projection also if that only drops a
column, so that the scan absorbs the lower half.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A sort or a window declines a projection that drops its key. The key is dead
above it, so a `ProjectionExec` drops it before the repartition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Predicate-only columns remain in scan output below a computed projection and round-robin repartition

2 participants