Repository: spark Updated Branches: refs/heads/master c8045f8b4 -> 6d05c1c1d
[SPARK-20910][SQL] Add build-in SQL function - UUID ## What changes were proposed in this pull request? Add build-int SQL function - UUID. ## How was this patch tested? unit tests Author: Yuming Wang <[email protected]> Closes #18136 from wangyum/SPARK-20910. Project: http://git-wip-us.apache.org/repos/asf/spark/repo Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/6d05c1c1 Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/6d05c1c1 Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/6d05c1c1 Branch: refs/heads/master Commit: 6d05c1c1da9104c903099a6790b39427c867ed2b Parents: c8045f8 Author: Yuming Wang <[email protected]> Authored: Thu Jun 1 16:15:24 2017 +0900 Committer: Takuya UESHIN <[email protected]> Committed: Thu Jun 1 16:15:24 2017 +0900 ---------------------------------------------------------------------- .../catalyst/analysis/FunctionRegistry.scala | 1 + .../spark/sql/catalyst/expressions/misc.scala | 28 ++++++++++++++++++++ .../expressions/MiscExpressionsSuite.scala | 5 ++++ .../sql-tests/inputs/string-functions.sql | 3 +++ .../sql-tests/results/string-functions.sql.out | 10 ++++++- 5 files changed, 46 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/spark/blob/6d05c1c1/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala ---------------------------------------------------------------------- diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala index 8081036..116b26f 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala @@ -380,6 +380,7 @@ object FunctionRegistry { expression[AssertTrue]("assert_true"), expression[Crc32]("crc32"), expression[Md5]("md5"), + expression[Uuid]("uuid"), expression[Murmur3Hash]("hash"), expression[Sha1]("sha"), expression[Sha1]("sha1"), http://git-wip-us.apache.org/repos/asf/spark/blob/6d05c1c1/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/misc.scala ---------------------------------------------------------------------- diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/misc.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/misc.scala index bb9368c..3fc4bb7 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/misc.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/misc.scala @@ -17,9 +17,12 @@ package org.apache.spark.sql.catalyst.expressions +import java.util.UUID + import org.apache.spark.sql.catalyst.InternalRow import org.apache.spark.sql.catalyst.expressions.codegen._ import org.apache.spark.sql.types._ +import org.apache.spark.unsafe.types.UTF8String /** * Print the result of an expression to stderr (used for debugging codegen). @@ -104,3 +107,28 @@ case class CurrentDatabase() extends LeafExpression with Unevaluable { override def nullable: Boolean = false override def prettyName: String = "current_database" } + +// scalastyle:off line.size.limit +@ExpressionDescription( + usage = "_FUNC_() - Returns an universally unique identifier (UUID) string. The value is returned as a canonical UUID 36-character string.", + extended = """ + Examples: + > SELECT _FUNC_(); + 46707d92-02f4-4817-8116-a4c3b23e6266 + """) +// scalastyle:on line.size.limit +case class Uuid() extends LeafExpression { + + override def deterministic: Boolean = false + + override def nullable: Boolean = false + + override def dataType: DataType = StringType + + override def eval(input: InternalRow): Any = UTF8String.fromString(UUID.randomUUID().toString) + + override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { + ev.copy(code = s"final UTF8String ${ev.value} = " + + s"UTF8String.fromString(java.util.UUID.randomUUID().toString());", isNull = "false") + } +} http://git-wip-us.apache.org/repos/asf/spark/blob/6d05c1c1/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/MiscExpressionsSuite.scala ---------------------------------------------------------------------- diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/MiscExpressionsSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/MiscExpressionsSuite.scala index a26d070..4fe7b43 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/MiscExpressionsSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/MiscExpressionsSuite.scala @@ -39,4 +39,9 @@ class MiscExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { checkEvaluation(AssertTrue(Cast(Literal(1), BooleanType)), null) } + test("uuid") { + checkEvaluation(Length(Uuid()), 36) + assert(evaluate(Uuid()) !== evaluate(Uuid())) + } + } http://git-wip-us.apache.org/repos/asf/spark/blob/6d05c1c1/sql/core/src/test/resources/sql-tests/inputs/string-functions.sql ---------------------------------------------------------------------- diff --git a/sql/core/src/test/resources/sql-tests/inputs/string-functions.sql b/sql/core/src/test/resources/sql-tests/inputs/string-functions.sql index e6dcea4..d82df11 100644 --- a/sql/core/src/test/resources/sql-tests/inputs/string-functions.sql +++ b/sql/core/src/test/resources/sql-tests/inputs/string-functions.sql @@ -12,3 +12,6 @@ FROM (SELECT id col1, id col2, id col3, id col4 FROM range(10)) t; -- replace function select replace('abc', 'b', '123'); select replace('abc', 'b'); + +-- uuid +select length(uuid()), (uuid() <> uuid()); http://git-wip-us.apache.org/repos/asf/spark/blob/6d05c1c1/sql/core/src/test/resources/sql-tests/results/string-functions.sql.out ---------------------------------------------------------------------- diff --git a/sql/core/src/test/resources/sql-tests/results/string-functions.sql.out b/sql/core/src/test/resources/sql-tests/results/string-functions.sql.out index abf0cc4..4093a7b 100644 --- a/sql/core/src/test/resources/sql-tests/results/string-functions.sql.out +++ b/sql/core/src/test/resources/sql-tests/results/string-functions.sql.out @@ -1,5 +1,5 @@ -- Automatically generated by SQLQueryTestSuite --- Number of queries: 6 +-- Number of queries: 7 -- !query 0 @@ -70,3 +70,11 @@ select replace('abc', 'b') struct<replace(abc, b, ):string> -- !query 5 output ac + + +-- !query 6 +select length(uuid()), (uuid() <> uuid()) +-- !query 6 schema +struct<length(uuid()):int,(NOT (uuid() = uuid())):boolean> +-- !query 6 output +36 true --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
