Add EnsureRequirements: merged EnforceDistribution + EnforceSorting with idempotent pushdown_sorts#21976
Draft
zhuqi-lucas wants to merge 1 commit intoapache:mainfrom
Draft
Add EnsureRequirements: merged EnforceDistribution + EnforceSorting with idempotent pushdown_sorts#21976zhuqi-lucas wants to merge 1 commit intoapache:mainfrom
zhuqi-lucas wants to merge 1 commit intoapache:mainfrom
Conversation
50ce304 to
dfb1043
Compare
…ceSorting ## Summary Replace the separate `EnforceDistribution` and `EnforceSorting` optimizer rules with a single `EnsureRequirements` rule in the default optimizer chain. This makes the composition idempotent by fixing distribution-awareness in `pushdown_sorts` and fetch preservation in `EnforceDistribution`. ## Problem `EnforceDistribution` and `EnforceSorting` are coupled through `SortExec.preserve_partitioning` but run as independent rules. This caused: 1. **Production 502 errors**: `pushdown_sorts` set `preserve_partitioning=true` without `SortPreservingMergeExec`, violating `SinglePartition` requirements from `GlobalLimitExec` → `SanityCheckPlan` failure. 2. **Non-idempotent composition**: Running the rules multiple times produced different (sometimes invalid) plans. 3. **Lost fetch values** (apache#14150): `EnforceDistribution` dropped `fetch` from `SortPreservingMergeExec` when stripping and re-adding distribution operators. DataFusion was the only major engine with separate rules — Spark (`EnsureRequirements`) and Presto (`AddExchanges`) use a single rule. ## Changes ### `EnsureRequirements` rule (new) - Composes `EnforceDistribution::optimize()` + `EnforceSorting::optimize()` - Replaces both rules in the default optimizer chain - 53 comprehensive tests including idempotency verification ### Distribution-aware `pushdown_sorts` (fix) - Add `distribution_requirement` field to `ParentRequirements` - New `add_sort_above_with_distribution()` inserts `SortPreservingMergeExec` when parent requires `SinglePartition` and input has multiple partitions - Propagate distribution through recursion with `stronger_distribution()` - Reset distribution below partition-merging nodes (SPM, single-partition outputs) ### Fix `EnforceDistribution` fetch preservation (apache#14150) - `remove_dist_changing_operators()` now saves fetch from removed SPM/Coalesce - `add_merge_on_top()` re-applies saved fetch to new operators ## Testing | Suite | Result | |-------|--------| | EnsureRequirements (new) | 53 passed | | enforce_sorting (existing) | 124 passed, 0 regressions | | enforce_distribution (existing) | 66 passed, 0 regressions | | SLT (465 files) | 1 pre-existing failure only | | **Total** | **243 unit + 464 SLT, 0 new failures** | Idempotency verified: - All partition counts 1-64 - Triple + 10x consecutive optimization passes - SortMergeJoin, HashJoin, Window, Aggregate topologies - PR apache#53/apache#54 regression scenarios - apache#14150 fetch preservation across passes Closes: apache#14150 Part of: apache#21973
dfb1043 to
ba7e30e
Compare
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.
Summary
Replace the separate
EnforceDistributionandEnforceSortingoptimizer rules with a singleEnsureRequirementsrule in the default optimizer chain. Fixpushdown_sortsto be distribution-aware and fixEnforceDistributionfetch preservation (#14150), making the composition idempotent.Epic: #21973
Problem
EnforceDistributionandEnforceSortingrun as separate rules, but sorting and distribution are coupled throughSortExec.preserve_partitioning. This caused:Production 502 errors:
pushdown_sortssetpreserve_partitioning=trueon multi-partition input without insertingSortPreservingMergeExec, violatingSinglePartitionrequirements fromGlobalLimitExec→SanityCheckPlanfailure.Non-idempotent composition: Running the rules multiple times produced different (sometimes invalid) plans.
Lost fetch values (Bug: applying multiple times
EnforceDistributiongenerates invalid plan #14150):EnforceDistributiondroppedfetchfromSortPreservingMergeExec/CoalescePartitionsExecwhen stripping and re-adding distribution operators.DataFusion was the only major query engine with separate rules — Spark (
EnsureRequirements) and Presto/Trino (AddExchanges) handle both in a single rule.Changes
1.
EnsureRequirementsrule (new, replaces default chain)EnforceDistribution::optimize()+EnforceSorting::optimize()in a single ruleArc::new(EnforceDistribution) + Arc::new(EnforceSorting)in the default optimizer chain2. Distribution-aware
pushdown_sorts(sort_pushdown.rs)distribution_requirement: Distributionfield toParentRequirementsadd_sort_above_with_distribution()inutils.rs— insertsSortPreservingMergeExecwhen parent requiresSinglePartitionand input has multiple partitionsadd_sort_abovecall sites to distribution-aware variantstronger_distribution()helper3. Fix
EnforceDistributionfetch preservation (#14150)remove_dist_changing_operators()now saves fetch from removed SPM/Coalesceadd_merge_on_top()re-applies saved fetch to re-created operators4. Updated SLT
explain.slt: Two optimizer rule names (EnforceDistribution,EnforceSorting) become one (EnsureRequirements) in EXPLAIN VERBOSE outputTesting
Idempotency coverage
Architecture
Idempotent because:
pushdown_sortsnow carriesdistribution_requirementand usesadd_sort_above_with_distributionEnforceDistributionpreservesfetchacross strip/re-add cyclesEnsureRequirementstwice produces identical plans (verified by 53 tests)Next Steps (future PRs)
pushdown_sortsoptimizations in the bottom-upensure_sortingpasspushdown_sortstop-down pass entirelytransform_upfor both distribution + sorting, like Spark)Closes: #14150