[SQL] Optimization to pull expensive operations out of filters - #6818
Merged
Conversation
mythical-fred
approved these changes
Aug 6, 2026
mythical-fred
left a comment
There was a problem hiding this comment.
Nice optimization. Hoisting expensive predicate operands into a preceding map lets ImplementNow see a simple field-vs-window comparison, and as a bonus lets projection pushdown drop the source columns whose only role was feeding the expensive call. The VARIANT test asserting no VARIANT-typed operator output past the hoisting map is a strong invariant to lock in.
A few things I liked:
- Dedup via
EquivalenceContext.equivon the hoisted list — BETWEEN and other patterns that duplicate operands (like the twoTRANSFORM(...)copies from a range check) collapse to a single hoisted column. The nested-closure test pins that. - Correctly skipping any subexpression that contains
now()— otherwise ImplementNow would lose the temporal shape. - Marking VARIANT casts and
VARIANT_INDEXas expensive inExpensive.java— these really are runtime function calls under the hood, so the existingcontainsExternalFunctionheuristic was missing them. - Simplify pass before analysis normalizes the predicate shape so
collect/rewritesee a predictable tree. - Result-restoring projection is annotated
IsProjection, so downstream projection pushdown can peel it off when the extra columns are unused.
Approving.
ryzhyk
approved these changes
Aug 6, 2026
ryzhyk
left a comment
Contributor
There was a problem hiding this comment.
Works well on a real world program with hundreds of filters.
lalithsuresh
approved these changes
Aug 6, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Aug 6, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Aug 7, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Aug 7, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Aug 7, 2026
mihaibudiu
enabled auto-merge
August 7, 2026 05:58
Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
mihaibudiu
enabled auto-merge
August 7, 2026 06:57
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.
Fixes #6815
This is most useful for temporal filters. When compiling something like
WHERE expensive(x) <= NOW() ...this makes sure thatexpensive(x)is evaluated only once. As a bonus, if the x field is not needed after the where, the optimizer can actually drop it.Checklist