-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c852d46
Add function unhex
zhichao-li 11945c7
style
zhichao-li cde73f5
update to use AutoCastInputTypes
zhichao-li bffd37f
change to use Hex in apache common package
zhichao-li 607d7a3
use checkInputTypes
zhichao-li fe5c14a
add todigit
zhichao-li a4ae6dc
add udf_unhex to whitelist
zhichao-li 379356e
remove exception checking
zhichao-li 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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -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") | ||
|
|
||
|
|
||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
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.
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).