-
Notifications
You must be signed in to change notification settings - Fork 169
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
feat: Add support for TryCast expression in Spark 3.2 and 3.3 #416
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2fdf148
working on trycast
vaibhawvipul 39d5ca1
code refactor
vaibhawvipul 57dfe84
compilation fix
vaibhawvipul 63c0c14
bug fixes and supporting try_Cast
vaibhawvipul 1c50315
removing trycast var and comment
vaibhawvipul d770c13
removing issue comment
vaibhawvipul 159a43d
adding comments
vaibhawvipul 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 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 |
---|---|---|
|
@@ -604,6 +604,52 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim | |
|
||
def exprToProtoInternal(expr: Expression, inputs: Seq[Attribute]): Option[Expr] = { | ||
SQLConf.get | ||
|
||
def handleCast( | ||
child: Expression, | ||
inputs: Seq[Attribute], | ||
dt: DataType, | ||
timeZoneId: Option[String], | ||
actualEvalModeStr: String): Option[Expr] = { | ||
|
||
val childExpr = exprToProtoInternal(child, inputs) | ||
if (childExpr.isDefined) { | ||
val castSupport = | ||
CometCast.isSupported(child.dataType, dt, timeZoneId, actualEvalModeStr) | ||
|
||
def getIncompatMessage(reason: Option[String]): String = | ||
"Comet does not guarantee correct results for cast " + | ||
s"from ${child.dataType} to $dt " + | ||
s"with timezone $timeZoneId and evalMode $actualEvalModeStr" + | ||
reason.map(str => s" ($str)").getOrElse("") | ||
|
||
castSupport match { | ||
case Compatible(_) => | ||
castToProto(timeZoneId, dt, childExpr, actualEvalModeStr) | ||
case Incompatible(reason) => | ||
if (CometConf.COMET_CAST_ALLOW_INCOMPATIBLE.get()) { | ||
logWarning(getIncompatMessage(reason)) | ||
castToProto(timeZoneId, dt, childExpr, actualEvalModeStr) | ||
} else { | ||
withInfo( | ||
expr, | ||
s"${getIncompatMessage(reason)}. To enable all incompatible casts, set " + | ||
s"${CometConf.COMET_CAST_ALLOW_INCOMPATIBLE.key}=true") | ||
None | ||
} | ||
case Unsupported => | ||
withInfo( | ||
expr, | ||
s"Unsupported cast from ${child.dataType} to $dt " + | ||
s"with timezone $timeZoneId and evalMode $actualEvalModeStr") | ||
None | ||
} | ||
} else { | ||
withInfo(expr, child) | ||
None | ||
} | ||
} | ||
|
||
expr match { | ||
case a @ Alias(_, _) => | ||
val r = exprToProtoInternal(a.child, inputs) | ||
|
@@ -617,50 +663,17 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim | |
val value = cast.eval() | ||
exprToProtoInternal(Literal(value, dataType), inputs) | ||
|
||
case UnaryExpression(child) if expr.prettyName == "trycast" => | ||
val timeZoneId = SQLConf.get.sessionLocalTimeZone | ||
handleCast(child, inputs, expr.dataType, Some(timeZoneId), "TRY") | ||
Comment on lines
+666
to
+668
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. I like this approach 👍 |
||
|
||
case Cast(child, dt, timeZoneId, evalMode) => | ||
val childExpr = exprToProtoInternal(child, inputs) | ||
if (childExpr.isDefined) { | ||
val evalModeStr = if (evalMode.isInstanceOf[Boolean]) { | ||
// Spark 3.2 & 3.3 has ansiEnabled boolean | ||
if (evalMode.asInstanceOf[Boolean]) "ANSI" else "LEGACY" | ||
} else { | ||
// Spark 3.4+ has EvalMode enum with values LEGACY, ANSI, and TRY | ||
evalMode.toString | ||
} | ||
val castSupport = | ||
CometCast.isSupported(child.dataType, dt, timeZoneId, evalModeStr) | ||
|
||
def getIncompatMessage(reason: Option[String]) = | ||
"Comet does not guarantee correct results for cast " + | ||
s"from ${child.dataType} to $dt " + | ||
s"with timezone $timeZoneId and evalMode $evalModeStr" + | ||
reason.map(str => s" ($str)").getOrElse("") | ||
|
||
castSupport match { | ||
case Compatible(_) => | ||
castToProto(timeZoneId, dt, childExpr, evalModeStr) | ||
case Incompatible(reason) => | ||
if (CometConf.COMET_CAST_ALLOW_INCOMPATIBLE.get()) { | ||
logWarning(getIncompatMessage(reason)) | ||
castToProto(timeZoneId, dt, childExpr, evalModeStr) | ||
} else { | ||
withInfo( | ||
expr, | ||
s"${getIncompatMessage(reason)}. To enable all incompatible casts, set " + | ||
s"${CometConf.COMET_CAST_ALLOW_INCOMPATIBLE.key}=true") | ||
None | ||
} | ||
case Unsupported => | ||
withInfo( | ||
expr, | ||
s"Unsupported cast from ${child.dataType} to $dt " + | ||
s"with timezone $timeZoneId and evalMode $evalModeStr") | ||
None | ||
} | ||
val evalModeStr = if (evalMode.isInstanceOf[Boolean]) { | ||
if (evalMode.asInstanceOf[Boolean]) "ANSI" else "LEGACY" | ||
vaibhawvipul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
withInfo(expr, child) | ||
None | ||
evalMode.toString | ||
} | ||
handleCast(child, inputs, dt, timeZoneId, evalModeStr) | ||
|
||
case add @ Add(left, right, _) if supportedDataType(left.dataType) => | ||
val leftExpr = exprToProtoInternal(left, inputs) | ||
|
This file contains 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.
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.
Just making sure whether it is
trycast
, nottry_cast
. Looks like Spark 3.4 is usingtry_cast
, haven't been able to find the source for Spark 3.3There 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.
yes, it is
trycast
.