Skip to content

Commit f0fb0c8

Browse files
zhengruifengdongjoon-hyun
authored andcommitted
[SPARK-49719][SQL] Make UUID and SHUFFLE accept integer seed
### What changes were proposed in this pull request? Make `UUID` and `SHUFFLE` accept integer `seed` ### Why are the changes needed? In most cases, `seed` accept both int and long, but `UUID` and `SHUFFLE` only accept long seed ```py In [1]: spark.sql("SELECT RAND(1L), RAND(1), SHUFFLE(array(1, 20, 3, 5), 1L), UUID(1L)").show() +------------------+------------------+---------------------------+--------------------+ | rand(1)| rand(1)|shuffle(array(1, 20, 3, 5))| uuid()| +------------------+------------------+---------------------------+--------------------+ |0.6363787615254752|0.6363787615254752| [20, 1, 3, 5]|1ced31d7-59ef-4bb...| +------------------+------------------+---------------------------+--------------------+ In [2]: spark.sql("SELECT UUID(1)").show() ... AnalysisException: [INVALID_PARAMETER_VALUE.LONG] The value of parameter(s) `seed` in `UUID` is invalid: expects a long literal, but got "1". SQLSTATE: 22023; line 1 pos 7 ... In [3]: spark.sql("SELECT SHUFFLE(array(1, 20, 3, 5), 1)").show() ... AnalysisException: [INVALID_PARAMETER_VALUE.LONG] The value of parameter(s) `seed` in `shuffle` is invalid: expects a long literal, but got "1". SQLSTATE: 22023; line 1 pos 7 ... ``` ### Does this PR introduce _any_ user-facing change? yes after this fix: ```py In [2]: spark.sql("SELECT SHUFFLE(array(1, 20, 3, 5), 1L), SHUFFLE(array(1, 20, 3, 5), 1), UUID(1L), UUID(1)").show() +---------------------------+---------------------------+--------------------+--------------------+ |shuffle(array(1, 20, 3, 5))|shuffle(array(1, 20, 3, 5))| uuid()| uuid()| +---------------------------+---------------------------+--------------------+--------------------+ | [20, 1, 3, 5]| [20, 1, 3, 5]|1ced31d7-59ef-4bb...|1ced31d7-59ef-4bb...| +---------------------------+---------------------------+--------------------+--------------------+ ``` ### How was this patch tested? added tests ### Was this patch authored or co-authored using generative AI tooling? no Closes #48166 from zhengruifeng/int_seed. Authored-by: Ruifeng Zheng <[email protected]> Signed-off-by: Dongjoon Hyun <[email protected]>
1 parent 94dca78 commit f0fb0c8

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/randomExpressions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ trait ExpressionWithRandomSeed extends Expression {
8181

8282
private[catalyst] object ExpressionWithRandomSeed {
8383
def expressionToSeed(e: Expression, source: String): Option[Long] = e match {
84+
case IntegerLiteral(seed) => Some(seed)
8485
case LongLiteral(seed) => Some(seed)
8586
case Literal(null, _) => None
8687
case _ => throw QueryCompilationErrors.invalidRandomSeedParameter(source, e)

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,14 @@ class CollectionExpressionsSuite
22932293
evaluateWithMutableProjection(Shuffle(ai0, seed2)))
22942294
assert(evaluateWithUnsafeProjection(Shuffle(ai0, seed1)) !==
22952295
evaluateWithUnsafeProjection(Shuffle(ai0, seed2)))
2296+
2297+
val seed3 = Literal.create(r.nextInt())
2298+
assert(evaluateWithoutCodegen(new Shuffle(ai0, seed3)) ===
2299+
evaluateWithoutCodegen(new Shuffle(ai0, seed3)))
2300+
assert(evaluateWithMutableProjection(new Shuffle(ai0, seed3)) ===
2301+
evaluateWithMutableProjection(new Shuffle(ai0, seed3)))
2302+
assert(evaluateWithUnsafeProjection(new Shuffle(ai0, seed3)) ===
2303+
evaluateWithUnsafeProjection(new Shuffle(ai0, seed3)))
22962304
}
22972305

22982306
test("Array Except") {

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/MiscExpressionsSuite.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ class MiscExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
7171
evaluateWithMutableProjection(Uuid(seed2)))
7272
assert(evaluateWithUnsafeProjection(Uuid(seed1)) !==
7373
evaluateWithUnsafeProjection(Uuid(seed2)))
74+
75+
val seed3 = Literal.create(r.nextInt())
76+
assert(evaluateWithoutCodegen(new Uuid(seed3)) === evaluateWithoutCodegen(new Uuid(seed3)))
77+
assert(evaluateWithMutableProjection(new Uuid(seed3)) ===
78+
evaluateWithMutableProjection(new Uuid(seed3)))
79+
assert(evaluateWithUnsafeProjection(new Uuid(seed3)) ===
80+
evaluateWithUnsafeProjection(new Uuid(seed3)))
7481
}
7582

7683
test("PrintToStderr") {

0 commit comments

Comments
 (0)