-
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 7 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,67 @@ 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 Serializable { | ||
|
|
||
| override def dataType: DataType = BinaryType | ||
|
|
||
| override def checkInputDataTypes(): TypeCheckResult = { | ||
|
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. and remove this once the expression extends ExpectsInputTypes |
||
| if (child.dataType.isInstanceOf[StringType] || child.dataType == NullType) { | ||
| TypeCheckResult.TypeCheckSuccess | ||
| } else { | ||
| TypeCheckResult.TypeCheckFailure(s"unHex accepts String type, not ${child.dataType}") | ||
| } | ||
| } | ||
|
|
||
| override def eval(input: InternalRow): Any = { | ||
| val num = child.eval(input) | ||
| if (num == null) { | ||
| null | ||
| } else { | ||
| unhex(num.asInstanceOf[UTF8String].getBytes) | ||
| } | ||
| } | ||
|
|
||
| private val hexDigits = { | ||
| val array = Array.fill[Byte](128)(-1) | ||
| (0 to 9).foreach(i => array('0' + i) = i.toByte) | ||
| (0 to 5).foreach(i => array('A' + i) = (i + 10).toByte) | ||
| (0 to 5).foreach(i => array('a' + i) = (i + 10).toByte) | ||
| array | ||
| } | ||
|
|
||
| private def toDigit(b: Byte): Byte = { | ||
| val digit = hexDigits(b) | ||
|
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. This should be a |
||
| if (digit == -1) { | ||
| throw new NumberFormatException(s"invalid hex number $b") | ||
| } | ||
| digit | ||
| } | ||
|
|
||
| private def unhex(inputBytes: Array[Byte]): Array[Byte] = { | ||
| var bytes = inputBytes | ||
| if ((bytes.length & 0x01) != 0) { | ||
| bytes = 48.toByte +: bytes // padding with '0' | ||
|
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. I think we can padding with |
||
| } | ||
| val out = new Array[Byte](bytes.length >> 1) | ||
| // two characters form the hex value. | ||
| var i = 0 | ||
| while (i < bytes.length) { | ||
| try { | ||
| out(i / 2) = ((toDigit(bytes(i)) << 4) | toDigit(bytes(i + 1)) & 0xFF).toByte | ||
| i += 2 | ||
| } 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 |
||
| 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 |
||
| } | ||
| } | ||
| out | ||
| } | ||
| } | ||
|
|
||
| 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.
this should just implement ExpectsInputTypes, and use BinaryType as the only type.
There will be a rule in #7175 to do implicit type casting from null type to expected type(s).