diff options
author | zhichao.li <zhichao.li@intel.com> | 2015-07-01 22:19:51 -0700 |
---|---|---|
committer | Davies Liu <davies@databricks.com> | 2015-07-01 22:19:51 -0700 |
commit | b285ac5ba85fe0b32b00726ad7d3a2efb602e885 (patch) | |
tree | dd4903378a6ff8c7543c2740782ddccb9c7801af /sql/core | |
parent | 4e4f74b5e1267d1ada4a8f57b86aee0d9c17d90a (diff) | |
download | spark-b285ac5ba85fe0b32b00726ad7d3a2efb602e885.tar.gz spark-b285ac5ba85fe0b32b00726ad7d3a2efb602e885.tar.bz2 spark-b285ac5ba85fe0b32b00726ad7d3a2efb602e885.zip |
[SPARK-8227] [SQL] Add function unhex
cc chenghao-intel adrian-wang
Author: zhichao.li <zhichao.li@intel.com>
Closes #7113 from zhichao-li/unhex and squashes the following commits:
379356e [zhichao.li] remove exception checking
a4ae6dc [zhichao.li] add udf_unhex to whitelist
fe5c14a [zhichao.li] add todigit
607d7a3 [zhichao.li] use checkInputTypes
bffd37f [zhichao.li] change to use Hex in apache common package
cde73f5 [zhichao.li] update to use AutoCastInputTypes
11945c7 [zhichao.li] style
c852d46 [zhichao.li] Add function unhex
Diffstat (limited to 'sql/core')
-rw-r--r-- | sql/core/src/main/scala/org/apache/spark/sql/functions.scala | 18 | ||||
-rw-r--r-- | sql/core/src/test/scala/org/apache/spark/sql/MathExpressionsSuite.scala | 10 |
2 files changed, 28 insertions, 0 deletions
diff --git a/sql/core/src/main/scala/org/apache/spark/sql/functions.scala b/sql/core/src/main/scala/org/apache/spark/sql/functions.scala index 4e8f3f96bf..e6f623bdf3 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/functions.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/functions.scala @@ -1054,6 +1054,24 @@ object functions { def hex(colName: String): Column = hex(Column(colName)) /** + * Inverse of hex. Interprets each pair of characters as a hexadecimal number + * and converts to the byte representation of number. + * + * @group math_funcs + * @since 1.5.0 + */ + def unhex(column: Column): Column = UnHex(column.expr) + + /** + * Inverse of hex. Interprets each pair of characters as a hexadecimal number + * and converts to the byte representation of number. + * + * @group math_funcs + * @since 1.5.0 + */ + def unhex(colName: String): Column = unhex(Column(colName)) + + /** * Computes `sqrt(a^2^ + b^2^)` without intermediate overflow or underflow. * * @group math_funcs diff --git a/sql/core/src/test/scala/org/apache/spark/sql/MathExpressionsSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/MathExpressionsSuite.scala index d6331aa4ff..c03cde38d7 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/MathExpressionsSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/MathExpressionsSuite.scala @@ -225,6 +225,16 @@ class MathExpressionsSuite extends QueryTest { checkAnswer(data.selectExpr("hex(cast(d as binary))"), Seq(Row("68656C6C6F"))) } + test("unhex") { + val data = Seq(("1C", "737472696E67")).toDF("a", "b") + checkAnswer(data.select(unhex('a)), Row(Array[Byte](28.toByte))) + checkAnswer(data.select(unhex('b)), Row("string".getBytes)) + checkAnswer(data.selectExpr("unhex(a)"), Row(Array[Byte](28.toByte))) + checkAnswer(data.selectExpr("unhex(b)"), Row("string".getBytes)) + checkAnswer(data.selectExpr("""unhex("##")"""), Row(null)) + checkAnswer(data.selectExpr("""unhex("G123")"""), Row(null)) + } + test("hypot") { testTwoToOneMathFunction(hypot, hypot, math.hypot) } |