Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 6
-- Number of queries: 7


-- !query 0
Expand Down Expand Up @@ -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