cloud-fan commented on code in PR #37483:
URL: https://github.com/apache/spark/pull/37483#discussion_r959122627
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala:
##########
@@ -1120,28 +1120,58 @@ case class Hex(child: Expression)
""",
since = "1.5.0",
group = "math_funcs")
-case class Unhex(child: Expression)
+case class Unhex(child: Expression, failOnError: Boolean = false)
extends UnaryExpression with ImplicitCastInputTypes with NullIntolerant {
+ def this(expr: Expression) = this(expr, false)
+
override def inputTypes: Seq[AbstractDataType] = Seq(StringType)
override def nullable: Boolean = true
override def dataType: DataType = BinaryType
- protected override def nullSafeEval(num: Any): Any =
- Hex.unhex(num.asInstanceOf[UTF8String].getBytes)
+ protected override def nullSafeEval(num: Any): Any = {
+ val result = Hex.unhex(num.asInstanceOf[UTF8String].getBytes)
+ if (failOnError && result == null) {
+ // The failOnError is set only from `ToBinary` function - hence we might
safely set `hint`
+ // parameter to `try_to_binary`.
+ throw QueryExecutionErrors.invalidInputInConversionError(
+ BinaryType,
+ num.asInstanceOf[UTF8String],
+ UTF8String.fromString("HEX"),
+ "try_to_binary")
+ }
+ result
+ }
override protected def doGenCode(ctx: CodegenContext, ev: ExprCode):
ExprCode = {
- nullSafeCodeGen(ctx, ev, (c) => {
+ nullSafeCodeGen(ctx, ev, c => {
val hex = Hex.getClass.getName.stripSuffix("$")
+ val maybeFailOnErrorCode = if (failOnError) {
+ val format = UTF8String.fromString("BASE64");
+ val binaryType = ctx.addReferenceObj("to", BinaryType,
BinaryType.getClass.getName)
+ s"""
+ |if (${ev.value} == null) {
+ | throw QueryExecutionErrors.invalidInputInConversionError(
+ | $binaryType,
+ | $c,
+ | $format,
+ | "try_to_binary");
Review Comment:
how can we know it's `try_to_binary` or `to_binary`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]