feat(optimize): push filter through Distinct nodes - #26
Merged
Merged
Conversation
Predicates are pure, so filter(distinct(X), pred) = distinct(filter(X, pred)). Adding a Distinct arm to try_push_filter lets the optimizer move a WHERE clause below a WITH DISTINCT, shrinking the row set before dedup rather than after. Adds tests/distinct_optimize.rs with 7 tests covering passthrough vars, renamed aliases, plan-shape assertions, idempotence, and cost verification.
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.
Why
Predicate pushdown skips
Distinctnodes today, leaving aFiltersitting above a
Distinctafter optimization. This is suboptimal: sincepredicates are pure,
filter(distinct(X), pred) = distinct(filter(X, pred)).Moving the filter below the dedup step shrinks the row set before the
Distinct operator sees it, reducing both the cardinality it must process
and the overall cost.
The motivating case is
WITH DISTINCT ... WHERE ...: the WHERE landsabove the Distinct in the plan, and previously the optimizer could not
push it further.
What
src/optimize.rs: add aPlan::Distinctarm totry_push_filterthat recurses through the Distinct node, pushing the filter into its
input (6 lines).
tests/distinct_optimize.rs: 7 new integration tests coveringpassthrough-var push, renamed-alias push, plan-shape position check,
optimizer idempotence for RETURN DISTINCT and WITH DISTINCT + WHERE,
cost improvement, and descend-into-RETURN-DISTINCT-input.
Tests
cargo test- 213 tests pass (206 existing + 7 new)cargo clippy --all-targets -- -D warnings- cleancargo fmt --check- cleanSelf-merge gate
Generated by Claude Code