Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 29.4k
[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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
c852d4611945c7cde73f5bffd37f607d7a3fe5c14aa4ae6dc379356eFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -354,6 +354,58 @@ 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 unhexDigits = { | ||
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 move to a object. it's really a static field. | ||
| 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 unhex(inputBytes: Array[Byte]): Array[Byte] = { | ||
| var bytes = inputBytes | ||
| if ((bytes.length & 0x01) != 0) { | ||
| bytes = '0'.toByte +: bytes | ||
| } | ||
| val out = new Array[Byte](bytes.length >> 1) | ||
| // two characters form the hex value. | ||
| var i = 0 | ||
| while (i < bytes.length) { | ||
| val first = unhexDigits(bytes(i)) | ||
| val second = unhexDigits(bytes(i + 1)) | ||
| if (first == -1 || second == -1) { return null} | ||
| out(i / 2) = (((first << 4) | second) & 0xFF).toByte | ||
| i += 2 | ||
| } | ||
| 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).