fix(optimizer): preserve filter execution order in PushDownFilter#21646
Open
SAY-5 wants to merge 1 commit intoapache:mainfrom
Open
fix(optimizer): preserve filter execution order in PushDownFilter#21646SAY-5 wants to merge 1 commit intoapache:mainfrom
SAY-5 wants to merge 1 commit intoapache:mainfrom
Conversation
When PushDownFilter collapses a parent Filter into a child Filter, the
combined predicate list was built as parent_predicates.chain(child_predicates).
Because the unoptimized plan evaluates inner (child) filters before
outer (parent) filters, this reversed the user-authored execution order
of selective predicates: a query written as
LogicalPlanBuilder::from(scan)
.filter(col("a").eq(lit(10)))? // applied first (inner)
.filter(col("b").gt(lit(11)))? // applied second (outer)
was optimized into 'Filter: b > 11 AND a = 10' rather than the
expected 'Filter: a = 10 AND b > 11'. In environments without
filter-selectivity statistics this matters: users who put their cheap
or highly-selective filter first expect it to execute first.
Build the conjunction as child_predicates.chain(parents_predicates)
so PushDownFilter actually preserves the execution order it claims to.
Update the two_filters_on_same_depth snapshot to assert the corrected
ordering and document why it matters in a short comment.
Closes apache#21642.
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.
Closes #21642.
When
PushDownFiltercollapses a parentFilterinto a childFilter, the combined predicate list was built asparents_predicates.chain(child_predicates):Because the unoptimized plan evaluates inner (child) filters before outer (parent) filters, this reversed the user-authored execution order of selective predicates. The reproducer in the issue:
…was optimized into
Filter: b > 11 AND a = 10rather than the expectedFilter: a = 10 AND b > 11. In environments without filter-selectivity statistics this matters: users who place a cheap or highly-selective filter first expect it to execute first.Fix
Build the conjunction as
child_predicates.chain(parents_predicates)soPushDownFilteractually preserves the execution order it claims to in the existinguse IndexSet to remove dupes while preserving predicate ordercomment.Snapshot update
two_filters_on_same_depthhad itsassert_optimized_plan_equal!snapshot frozen at the buggy order. The unoptimized plan in that test appliesa <= 1first (inner filter) anda >= 1second (outer), so the optimized result must bea <= 1 AND a >= 1. Updated the snapshot and added an inline comment documenting the invariant.Verification
cargo test -p datafusion-optimizer --lib push_down_filterpasses 79/79 tests with the fix and the snapshot update applied.