decomposed date_trunc optimization into expr simplifier #18648
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.
This decomposes a custom optimizer rule into the datafusion expression simplifier (work-in-progress).
Which issue does this PR close?
Closes #18319.
Rationale for this change
To transform binary expressions that compare
date_truncwith a constant value into a form that can be better utilized (improved performance).For Bauplan, we can see the following (approximate average over a handful of runs):
Q1:
Q2:
What changes are included in this PR?
A few additional support functions and additional match arms in the simplifier match expression.
Are these changes tested?
Our custom rule has tests of the expression transformations and for correct evaluation results. These will be added to the PR after the implementation is in approximately good shape.
Are there any user-facing changes?
Better performance and occasionally confusing explain plan. In short, a
date_trunc('month', col) = '2025-12-03'::DATEwill always be false (because the truncation result can never be a non-truncated value), which may produce an unexpected expression (false).Explain plan details below (may be overkill but it was fun to figure out):
Initial query:
After simplify_expressions:
Before and after
date_trunc_optimizer(our custom rule):