-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-8227][SQL]Add function unhex #7113
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
Changes from 3 commits
c852d46
11945c7
cde73f5
bffd37f
607d7a3
fe5c14a
a4ae6dc
379356e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -354,6 +354,44 @@ case class Pow(left: Expression, right: Expression) | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Performs the inverse operation of HEX. | ||
| * Resulting characters are returned as a byte array. | ||
| */ | ||
| case class UnHex(child: Expression) | ||
| extends UnaryExpression with AutoCastInputTypes with Serializable { | ||
|
|
||
| override def expectedChildTypes: Seq[DataType] = Seq(StringType) | ||
|
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. @davies this could be changed to
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. hex() return StringType, so unhex() should take StringType, not BinaryType. |
||
|
|
||
| override def dataType: DataType = BinaryType | ||
|
|
||
| override def eval(input: InternalRow): Any = { | ||
| val num = child.eval(input) | ||
| if (num == null) { | ||
| null | ||
| } else { | ||
| unhex(num.asInstanceOf[UTF8String].toString) | ||
|
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. Converting UTF8String to String is slow, please use |
||
| } | ||
| } | ||
|
|
||
| private def unhex(s: String): Array[Byte] = { | ||
| // append a leading 0 if needed | ||
| val str = if (s.length % 2 == 1) {"0" + s} else {s} | ||
| val result = new Array[Byte](str.length / 2) | ||
| var i = 0 | ||
| while (i < str.length()) { | ||
| try { | ||
| result(i / 2) = Integer.parseInt(str.substring(i, i + 2), 16).asInstanceOf[Byte] | ||
| } catch { | ||
|
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. Can we use common-codec here? http://commons.apache.org/proper/commons-codec/archives/1.10/apidocs/org/apache/commons/codec/binary/Hex.html |
||
| // invalid character present, return null | ||
| case _: NumberFormatException => return null | ||
|
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. We could check the value from |
||
| } | ||
| i += 2 | ||
| } | ||
| result | ||
| } | ||
| } | ||
|
|
||
| case class Hypot(left: Expression, right: Expression) | ||
| extends BinaryMathExpression(math.hypot, "HYPOT") | ||
|
|
||
|
|
||
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 think
unhexonly works on StringType, so we should not useAutoCastInputTypeshere, we should usecheckInputDataTypesinstead.