feat: sort file groups by statistics during sort pushdown (Sort pushdown phase 2)#21182
Open
zhuqi-lucas wants to merge 3 commits intoapache:mainfrom
Open
feat: sort file groups by statistics during sort pushdown (Sort pushdown phase 2)#21182zhuqi-lucas wants to merge 3 commits intoapache:mainfrom
zhuqi-lucas wants to merge 3 commits intoapache:mainfrom
Conversation
Sort files within each file group by min/max statistics during sort pushdown to better align with the requested ordering. When files are non-overlapping and within-file ordering is guaranteed (e.g. Parquet with sorting_columns metadata), the SortExec is completely eliminated. Key changes: - ParquetSource::try_pushdown_sort returns Exact when natural ordering satisfies the request, enabling sort elimination - FileScanConfig sorts files within groups by statistics and verifies non-overlapping property to determine Exact vs Inexact - Multi-group files are redistributed consecutively to preserve both sort elimination and I/O parallelism across partitions - Statistics-based file reordering as fallback when FileSource returns Unsupported (benefits TopK via better dynamic filter pruning) - New sort_pushdown benchmark for measuring sort elimination speedup Closes apache#17348 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
Author
Contributor
There was a problem hiding this comment.
Pull request overview
Implements statistics-driven file group ordering as part of sort pushdown, enabling sort elimination when within-file ordering matches and files are non-overlapping, plus a best-effort stats-based reorder fallback when exact pushdown isn’t possible.
Changes:
- Add file-group reordering by min/max statistics (and non-overlap validation) to enable
SortExecelimination for exactly ordered, non-overlapping files. - Extend Parquet sort pushdown to return
Exactwhen Parquet ordering metadata satisfies the requested ordering. - Add/adjust SLT + Rust tests and a new benchmark to validate and measure the optimization.
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| datafusion/sqllogictest/test_files/sort_pushdown.slt | Adds SLT coverage for stats reorder, sort elimination, LIMIT behavior, multi-partition behavior, and inferred ordering from Parquet metadata. |
| datafusion/physical-optimizer/src/pushdown_sort.rs | Updates module docs to reflect new capabilities (Exact elimination + stats-based ordering). |
| datafusion/datasource/src/file_scan_config.rs | Implements core stats-based reordering, non-overlap validation, “exact” preservation logic, and cross-group redistribution. Adds unit tests. |
| datafusion/datasource-parquet/src/source.rs | Returns Exact when Parquet natural ordering satisfies the requested sort. |
| datafusion/core/tests/physical_optimizer/pushdown_sort.rs | Updates a prefix-match test to reflect Exact pushdown / sort elimination behavior. |
| benchmarks/src/sort_pushdown.rs | Adds a benchmark to measure sort elimination and LIMIT benefits on sorted, non-overlapping parquet files. |
| benchmarks/src/lib.rs | Registers the new sort_pushdown benchmark module. |
| benchmarks/src/bin/dfbench.rs | Exposes sort-pushdown as a new dfbench subcommand. |
| benchmarks/bench.sh | Adds bench.sh targets to run the new sort pushdown benchmarks. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
… improve docs - Remove dead stats computation in reverse_file_groups branch (reverse path is always Inexact, so all_non_overlapping is unused) - Add reverse prefix matching documentation to pushdown_sort module Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ting Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
|
Very exciting! I hope I have wifi on the plane later today so I can review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Closes #17348
Closes #19329
Rationale for this change
This PR implements the core optimization described in the EPIC Sort pushdown / partially sorted scans: using file-level min/max statistics to optimize scan order and eliminate unnecessary sort operators.
Currently, when a query has
ORDER BY, DataFusion always inserts aSortExeceven when the data is already sorted across files. This PR enables:sorting_columnsmetadata (noWITH ORDERneeded)What changes are included in this PR?
Architecture
Three Optimization Paths
Path 1: Sort Elimination (Exact) — removes SortExec entirely
When the file source's natural ordering satisfies the query (e.g., Parquet files with
sorting_columnsmetadata), and files within each group are non-overlapping, theSortExecis completely eliminated.Path 2: Reverse Scan (Inexact) — existing optimization, enhanced
When the requested order is the reverse of the natural ordering,
reverse_row_groups=trueis set. SortExec stays but benefits from approximate ordering.Path 3: Statistics-Based File Reordering — new fallback
When the FileSource returns
Unsupported, files are reordered by their min/max statistics to approximate the requested order. This benefits TopK queries via better dynamic filter pruning.Multi-Partition Design
For multiple execution partitions, the optimization works per-partition:
When bin-packing interleaves file ranges across groups, files are redistributed using consecutive assignment to ensure groups are ordered relative to each other:
Automatic Ordering Inference
DataFusion already infers ordering from Parquet
sorting_columnsmetadata (viaordering_from_parquet_metadata). With this PR, the inferred ordering flows through sort pushdown automatically — users don't needWITH ORDERfor sorted Parquet files.Files Changed
datasource-parquet/src/source.rsExactwhen natural ordering satisfies requestdatasource/src/file_scan_config.rsphysical-optimizer/src/pushdown_sort.rscore/tests/physical_optimizer/pushdown_sort.rssqllogictest/test_files/sort_pushdown.sltbenchmarks/src/sort_pushdown.rsbenchmarks/{lib,bin/dfbench,bench}.{rs,sh}Benchmark Results
300k rows, 8 non-overlapping sorted parquet files, single partition:
ORDER BY col ASC(full scan)ORDER BY col ASC LIMIT 100ORDER BY col ASC(wide,SELECT *)ORDER BY col ASC LIMIT 100(wide)LIMIT queries benefit most (67-74%) because sort elimination + limit pushdown means only the first few rows are read.
Tests
Unit Tests (12 new)
SLT Integration Tests (5 new groups)
target_partitions=2→ SPM + per-partition sort eliminationIntegration Tests
Test plan
cargo test -p datafusion-datasource(111 tests pass)cargo test -p datafusion-datasource-parquet(96 tests pass)cargo test -p datafusion-physical-optimizer(27 tests pass)cargo test -p datafusion --test core_integration(919 tests pass)cargo test -p datafusionall tests (1997+ pass)cargo clippy— 0 warnings🤖 Generated with Claude Code