Skip to content

fix(parquet): run runtime row-group pruning and reordering with row selections - #27738

Open
adriangb wants to merge 3 commits into
apache:mainfrom
adriangb:parquet-runtime-prune-with-selections
Open

adriangb wants to merge 3 commits into
apache:mainfrom
adriangb:parquet-runtime-prune-with-selections

Conversation

@adriangb

@adriangb adriangb commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

When a Parquet scan has any row selection, DataFusion turns off two optimizations for the whole file:

  • runtime row-group pruning by dynamic filters (TopK, joins), and
  • statistics-based row-group reordering (reorder_by_statistics).

A selection can come from the page index or from a user ParquetAccessPlan. A selection that skips one row costs the scan both optimizations. So a scan that skips almost nothing can read far more data than a scan with no selection.

The guards came from #24359. They protected the old file-wide selection, which a pruning rebuild did not slice (#24355). #25608 moved the scan to row-group-local selections but kept the guards. This PR removes them.

What changes are included in this PR?

Commit Change
1. test: Adds an slt TopK query where the page index prunes one page of each row group. On main it shows row_groups_pruned_dynamic_filter=0.
2. fix(parquet): Removes both guards and PreparedAccessPlan::has_row_selection. The slt output changes to row_groups_pruned_dynamic_filter=4. The I/O trace from #27757 loses requests 6 to 9: the scan no longer reads row groups 0 and 1.
3. perf(parquet): Keeps the second copy of each selection only when the scan builds a runtime pruner. A scan without one (for example, deletion vectors and no dynamic filter) holds each selection once.

The guard removal alone is not enough. The pruning rebuild in rebuild_decoder_at_boundary passed RowGroupSelection::new(idx, None) for every surviving row group. That dropped the survivors' selections. With a user access plan (for example deletion vectors), that returns rows the plan excluded. Each RgPlanEntry now keeps its row group's selection, and the rebuild passes it back. Rebuilds only happen at a row-group boundary, so no survivor has started decoding and each selection is intact.

prepare now drops zero-row scan groups in all cases. Before, it dropped them only when some row group had a selection.

Review commit by commit. The second commit's slt diff is one metric.

Follow-up

The parallel rg_plan queue still mirrors the decoder frontier. apache/arrow-rs#12456 (draft) adds ParquetPushDecoder::retain_row_groups. With it, DataFusion can drop rg_plan, the frontier sync, and the cloned selections.

What is the testing strategy for this PR?

  • test_dynamic_pruning_preserves_external_row_selection (opener unit test) now asserts that runtime pruning runs with an external selection, and that row group 2 keeps its selection. It fails if the rebuild passes None: it returns [10, 11, 12, 13] instead of [10, 12].
  • dynamic_rg_pruning_with_page_index_row_selection (core integration test, renamed) asserts that pruning runs with a page-index selection.
  • test_runtime_pruning_eligible covers the rule that decides both the pruner and the selection copy.
  • reorder_by_statistics_moves_selection_with_row_group asserts that a reorder moves each selection with its row group.
  • The new slt case.
  • topk_io_dynamic_filter_with_page_pruning from #27757 shows the I/O the fix saves.
  • cargo test -p datafusion-datasource-parquet, cargo test -p datafusion --test parquet_integration and all sqllogictests pass. Clippy is clean.

I have not run benchmarks yet.

Are there any user-facing changes?

No API change. Scans with page-index or external selections can now prune row groups at runtime and reorder row groups, so they can read less data.

🤖 Generated with Claude Code

@github-actions github-actions Bot added core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) datasource Changes to the datasource crate labels Nov 25, 2026
@adriangb

Copy link
Copy Markdown
Contributor Author

@zhuqi-lucas wonder if you'd be interested in reviewing this PR / helping get apache/arrow-rs#12456 across the line?

@adriangb
adriangb marked this pull request as ready for review November 25, 2026 05:46
@adriangb
adriangb requested review from zhuqi-lucas and a max review from Copilot and removed request for Copilot November 25, 2026 05:46

@zhuqi-lucas zhuqi-lucas left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @adriangb for this! The fix looks correct to me. I checked against arrow-rs 60.2.0: at a row-group boundary, the per-row-group queue has not touched the selections of the row groups that have not started, and the offset/limit budget carries across into_builder. So passing the original selections back on a pruning rebuild is safe.

A couple of small questions:

  1. RgPlanEntry::selection is cloned for every row group, even when no runtime pruner is built. Could we clone it only when the pruner is eligible? Large external selections such as deletion vectors would otherwise be held twice until the retain_row_groups follow-up lands.
  2. Do you have any benchmark numbers? As far as I can tell, none of the existing benchmarks hit this path. ClickBench has no page index, and the sort_pushdown queries have no WHERE. A TopK case with a page-index selection, such as WHERE tenant_id = X ORDER BY ts DESC LIMIT 10, would show the gain.

Neither blocks merging. Approving.

@alamb

alamb commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Here is another small proposed test that shows this working 👍

@adriangb
adriangb force-pushed the parquet-runtime-prune-with-selections branch 2 times, most recently from 2d1292f to 01b7d4f Compare November 25, 2026 19:07
adriangb and others added 3 commits November 25, 2026 13:12
…ning

Add a TopK query where the page index prunes one page of each row group.
On main, the live row selection turns off runtime dynamic-filter pruning,
so the plan shows row_groups_pruned_dynamic_filter=0. The next commit
changes this output.

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

Any row selection in the access plan turned off runtime dynamic-filter
row-group pruning and statistics reordering for the whole file. The
guards protected the old file-wide selection, which a pruning rebuild
did not slice (apache#24355). Selections are now row-group-local, so each
selection moves with its row group. A page index that skips a single row
no longer costs the scan these optimizations.

The pruning rebuild passed `None` for every surviving row group, which
dropped page-index and external selections. Each `RgPlanEntry` now keeps
its row group's selection, and the rebuild passes it back. Rebuilds run
only at a row-group boundary, so no survivor has started decoding.

Remove `PreparedAccessPlan::has_row_selection` and both guards.
`prepare` now drops zero-row scan groups in all cases.

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

Each `RgPlanEntry` cloned its row group's selection so a pruning rebuild
can pass it back. A scan without a runtime pruner never reads that copy,
so large external selections such as deletion vectors were held twice.

Decide pruner eligibility before building `rg_plan`, and store `None`
when the scan builds no pruner. Move the eligibility check into
`runtime_pruning_eligible` so the clone and the pruner use one rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
adriangb pushed a commit to adriangb/datafusion that referenced this pull request Nov 25, 2026
…runing (apache#27757)

## Which issue does this PR close?

- Related to apache#24358
- Related to apache#27738

## Rationale for this change

apache#27738 lets runtime (TopK
dynamic filter) row group pruning run when the page index has installed
a row selection. Its tests check the `row_groups_pruned_dynamic_filter`
metric, but nothing shows the effect on the I/O the scan actually does.


## What changes are included in this PR?

This PR adds an I/O trace test for that case, with the expected output
on `main` today, so that the fix shows up as a diff in the requests made
to object storage.


## What is the testing strategy for this PR?

This PR only adds a test:

```shell
cargo test -p datafusion --test parquet_integration -- io::topk_io
```

## Are there any user-facing changes?

No, this only adds a test.

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
@adriangb
adriangb force-pushed the parquet-runtime-prune-with-selections branch from 01b7d4f to 4c95c03 Compare November 25, 2026 19:14
@adriangb

Copy link
Copy Markdown
Contributor Author

Here is another small proposed test that shows this working 👍

* [test(parquet): add I/O test for TopK dynamic filter with page index pruning #27757](https://github.com/apache/datafusion/pull/27757)

Merged your PR into main and rebased this one. It shows the diff now.

@adriangb

Copy link
Copy Markdown
Contributor Author

@zhuqi-lucas I applied point (1) in 4c95c03. I'm adding a benchmark. I will open a separate PR for just the benchmark that we can merge first and rebase here like we did with Andrew's. I'll post the numbers here in the meantime.

@adriangb

adriangb commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Benchmark results

#27761 adds a sort-pushdown benchmark query that hits this path. It writes the sort_pushdown_inexact data with a page index (about 5 pages per row group) and adds q5:

SELECT l_orderkey, l_partkey, l_suppkey FROM lineitem
WHERE l_orderkey BETWEEN 0 AND 5000
   OR l_orderkey BETWEEN 200000 AND 205000
   OR ... -- 30 ranges, step 200000, width 5000
ORDER BY l_orderkey DESC LIMIT 10

Setup:

  • Data: TPC-H SF1 lineitem sorted by l_orderkey, 61 row groups of 100K rows over 3 files.
  • Config: pushdown_filters=true, default partitions.
  • Build and hardware: release build on a laptop, local disk.
main this PR
row_groups_pruned_statistics 61 total → 38 matched 61 total → 38 matched
page_index_pages_pruned 190 total → 38 matched 190 total → 38 matched
row_groups_pruned_dynamic_filter 0 21
Median of 30 iterations (run 1 / run 2) 5.9 / 6.2 ms 5.4 / 5.7 ms
Min of 30 iterations (run 1 / run 2) 5.5 / 5.5 ms 5.0 / 5.0 ms

The median is about 8% faster on local disk. The time saved is small there, because each surviving row group reads only about one page.

The bigger change is that the scan skips 21 row groups. On object storage, each skipped row group saves requests. The I/O trace from #27757 shows this: its requests 6 to 9 go away with this PR.

With --simulate-latency (SIMULATE_LATENCY=true in bench.sh), the same query on the same data shows a larger gain. The simulation adds 19-230 ms time to first byte per GET, at 90 MB/s per connection and 128 connections.

30 iterations, --simulate-latency main this PR
Median (run 1 / run 2) 428 / 436 ms 378 / 376 ms
Min (run 1 / run 2) 291 / 294 ms 234 / 209 ms

The median is about 13% faster and the best run is 20-29% faster. The per-request latency is random, so the spread is wide: the max is about 560-650 ms on both builds.

A plain WHERE tenant_id = X filter did not show the effect. With 18 partitions, each scan has about one matching row group, and that row group holds fewer rows than the LIMIT. So the dynamic filter has nothing left to prune. That is why q5 uses an OR of ranges.

I'll wait a day or two to merge this. If we merge #27761 before that I'll rebase and do a CI benchamrk run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate datasource Changes to the datasource crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Structurally eliminate rg_plan / RowSelection drift in the parquet runtime prune path (retain_row_groups)

3 participants