-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-24994][SQL][FOLLOW-UP] Handle foldable, timezone and cleanup #29775
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -103,9 +103,9 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] { | |
| // In case both sides have integral type, optimize the comparison by removing casts or | ||
| // moving cast to the literal side. | ||
| case be @ BinaryComparison( | ||
| Cast(fromExp, toType: IntegralType, _), Literal(value, literalType)) | ||
| Cast(fromExp, toType: IntegralType, tz), Literal(value, literalType)) | ||
| if canImplicitlyCast(fromExp, toType, literalType) => | ||
| simplifyIntegralComparison(be, fromExp, toType, value) | ||
| simplifyIntegralComparison(be, fromExp, toType, value, tz) | ||
|
|
||
| case _ => exp | ||
| } | ||
|
|
@@ -120,7 +120,8 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] { | |
| exp: BinaryComparison, | ||
| fromExp: Expression, | ||
| toType: IntegralType, | ||
| value: Any): Expression = { | ||
| value: Any, | ||
| tz: Option[String]): Expression = { | ||
|
|
||
| val fromType = fromExp.dataType | ||
| val (min, max) = getRange(fromType) | ||
|
|
@@ -184,7 +185,7 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] { | |
| } else { | ||
| // This means `value` is within range `(min, max)`. Optimize this by moving the cast to the | ||
| // literal side. | ||
| val lit = Cast(Literal(value), fromType) | ||
| val lit = Cast(Literal(value), fromType, tz) | ||
|
Member
Author
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. We can also evaluate this and pass the value. It will be optimized away by ConstantFolding anyway. |
||
| exp match { | ||
| case GreaterThan(_, _) => GreaterThan(fromExp, lit) | ||
| case GreaterThanOrEqual(_, _) => GreaterThanOrEqual(fromExp, lit) | ||
|
|
@@ -202,9 +203,12 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] { | |
| * i.e., the conversion is injective. Note this only handles the case when both sides are of | ||
| * integral type. | ||
| */ | ||
| private def canImplicitlyCast(fromExp: Expression, toType: DataType, | ||
| private def canImplicitlyCast( | ||
| fromExp: Expression, | ||
| toType: DataType, | ||
| literalType: DataType): Boolean = { | ||
| toType.sameType(literalType) && | ||
| !fromExp.foldable && | ||
| fromExp.dataType.isInstanceOf[IntegralType] && | ||
| toType.isInstanceOf[IntegralType] && | ||
| Cast.canUpCast(fromExp.dataType, toType) | ||
|
|
||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
This looks irrelevant as a follow-up.
Is there a comment on this?
pass timezone info to the generated cast on the literal value?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.
It's hard for me to think a valid test case for this in the scope of SPARK-24994 (Integral Types). Although Spark handles some time-related types with the integral types, I believe we need to revisit this on new PR which aims to support time.
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.
Thanks @dongjoon-hyun . I discovered this in tests - without this some of the existing tests will fail because of timezone mismatch (although I think this shouldn't affect correctness given we only handle integral types?).
In the follow-up, the current plan is to only extend to numeric types (e.g., float/double/decimal). I don't know whether support for time-related types is important or not.
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.
Could you give me some pointer for the failures? Or, can we have a reproducer? Usually, every code change had better have a test coverage. If there is a test case for this, it sounds reasonable.
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.
(oops just found out my comment was not sent out successfully)
This is because
ResolveTimeZonewill try to add timezone info to all expressions that don't have it during query analysis. However, since theCastexpr was generated at optimization phase, it will not have the timezone info. As result,PlanTest.comparePlanswill fail because of mismatch. I can try to come up with a test if necessary.On the other hand, I think instead of using
Cast, we may just directly use the value, since theCastwill be optimized away by ConstantFolding anyways later. What do you think?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.
yea this also works: just evaluate the cast and make a literal.
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.
Thanks. I'll do that then. I'm not totally sure about
Cast.canonicalize- somehow it is not used in query analysis and optimization and the lazy val is not initialized before the plan comparison.