feat: Support sort prefix removal for logical constants in Sort Pushdown #19445
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?
Rationale for this change
After establishing the high-level API for sort pushdown (#19064), it was identified that the optimizer did not handle cases where the sort prefix is a logical constant (e.g., due to a filter).
For example, in a query like:
SELECT * FROM table WHERE region = 'US' ORDER BY region, timeThe column
regionis constant. Previously, the optimizer attempted to push downSort(region, time). If the underlying source (e.g., Parquet or a custom TableProvider) only supported sorting bytime(or had an index ontime), the pushdown would fail or be unsupported.By stripping the constant prefix, we can push down
Sort(time), which the source is more likely to support, leading to significant performance improvements (e.g., skipping row groups via dynamic top-k pushdown).What changes are included in this PR?
optimizemethod inphysical-optimizer/src/pushdown_sort.rs.EquivalenceProperties.required_orderingbefore callingtry_pushdown_sorton the source.test_sort_pushdown_prefix_removalthat uses aMockPlanWithConstantsto verify that aSortExecis correctly removed when the sort column is constant.Are these changes tested?
test_sort_pushdown_prefix_removalhas been added todatafusion/physical-optimizer/src/pushdown_sort.rs.Exactpushdown results in the test case.Are there any user-facing changes?
ORDER BYandWHEREclauses on sorted data, as the optimizer can now push down sort requirements more aggressively.