-
Notifications
You must be signed in to change notification settings - Fork 2k
implement preimage for date_trunc
#18648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
drin
wants to merge
8
commits into
apache:main
Choose a base branch
from
drin:octalene.feat-optimize-datetrunc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+895
−36
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cfca589
feat: Implemented preimage for date_trunc
drin 377816a
test: added sql logic test for date_trunc preimage
drin ee405b2
fix: changed function comments
drin 3326794
feat: expanded rewrite_with_preimage
drin 780a131
fix: update date_part and floor
drin ba4ca28
feat: bug fixes and cleanup
drin 860028c
feat: accommodate PR feedback
drin cc59a58
chore: ran cargo fmt
drin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,23 @@ use crate::Expr; | |
| pub enum PreimageResult { | ||
| /// No preimage exists for the specified value | ||
| None, | ||
| /// The expression always evaluates to the specified constant | ||
| /// given that `expr` is within the interval | ||
| Range { expr: Expr, interval: Box<Interval> }, | ||
| /// For some UDF, a `preimage` implementation determines that: | ||
| /// the result `udf_result` in `udf_result = UDF(expr)` | ||
| /// is equivalent to `udf_result = UDF(i)` for any `i` in `interval`. | ||
| /// | ||
| /// Then, `is_boundary` indicates a boundary condition where: | ||
| /// the original expression `UDF(expr)` is compared to a value `lit` where: | ||
| /// `UDF(lit) == lit` | ||
| /// This condition is important for two scenarios: | ||
| /// 1. `<` and `>=` operators: | ||
| /// if `Some(false)`, expression rewrite should use `interval.upper` | ||
| /// 2. `=` and `!=` operators: | ||
| /// if `Some(false)`, expression rewrite can use constant (false and true, respectively) | ||
| /// | ||
| /// if is_boundary is `None`, then the boundary condition never applies. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While trying to understand this, I wonder if it might be easier to express by instead of adding a field to enum PreimageResult {
/// ...
Range { expr: Expr, interval: Box<Interval> },
// The original expression UDF(lit) = lit
// 1. `<` and `>=` operators:
/// if `Some(false)`, expression rewrite should use `interval.upper`
/// 2. `=` and `!=` operators:
/// if `Some(false)`, expression rewrite can use constant (false and true, respectively)
Boundary { expr: Expr, interval: Box<Interval> },
} |
||
| Range { | ||
| expr: Expr, | ||
| interval: Box<Interval>, | ||
| is_boundary: Option<bool>, | ||
| }, | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous documentation here describes the relationship between
exprandintervalas being direct, but the idea shouldn't be thatexpr >= lower and expr < upper, It should be:udf(expr) <op> literallogically implies thatexprcan be directly compared withinterval.That is, if
udf(expr) <op> literalis true, then there is a transformation involvingexprandintervalthat is logically equivalent.Specifically:
floor(x) < 8, thenpreimageshould returninterval: [8, 9)such that the expression can be rewritten tox < 8.floor(x) < 8.3, thenpreimageshould returninterval: [8, 9)such that the expression can be rewritten tox < 9.Notice that both expressions yield the same interval because both
8and8.3are literals in that range, and have equivalent outputs (floor(8) == floor(8.3) == { for y in [8, 9): floor(y) }).Then,
rewrite_with_preimagemust accommodate the predicate operator (<in this case) to correctly transform the expression using the preimage interval and a boundary condition (is_boundary = floor(y) == y).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't fully understand this logic
For the example
floor(x) < 8.3I would expect there to be no preimage as defined here -- specifically there is no range of inputs for whichfloor(x)evaluates to8.3I agree that it is valid simplificaition to rewrite
floor(x) < 8.3tox < 9.0, but it seems different than "preimage" 🤔Maybe we just need to give it a different name (maybe that is what you have tried to do with
is_boundary)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW I checked with datafusion-cli and the
floor(x) < 8.3case is not optimized today (the preimage is not applied here)