Skip to content
Snippets Groups Projects
Commit 6d05c1c1 authored by Yuming Wang's avatar Yuming Wang Committed by Takuya UESHIN
Browse files

[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 <wgyumg@gmail.com>

Closes #18136 from wangyum/SPARK-20910.
parent c8045f8b
No related branches found
No related tags found
No related merge requests found
......@@ -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"),
......
......@@ -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")
}
}
......@@ -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()))
}
}
......@@ -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());
-- 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment