Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-31594][SQL] Do not display the seed of rand/randn with no argument in output schema #28392

Closed
wants to merge 4 commits into from
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 @@ -83,9 +83,12 @@ trait ExpressionWithRandomSeed {
""",
since = "1.5.0")
// scalastyle:on line.size.limit
case class Rand(child: Expression) extends RDG with ExpressionWithRandomSeed {
case class Rand(child: Expression, hideSeed: Boolean = false)
extends RDG with ExpressionWithRandomSeed {

def this() = this(Literal(Utils.random.nextLong(), LongType))
def this() = this(Literal(Utils.random.nextLong(), LongType), true)

def this(child: Expression) = this(child, false)

override def withNewSeed(seed: Long): Rand = Rand(Literal(seed, LongType))

Expand All @@ -101,7 +104,12 @@ case class Rand(child: Expression) extends RDG with ExpressionWithRandomSeed {
isNull = FalseLiteral)
}

override def freshCopy(): Rand = Rand(child)
override def freshCopy(): Rand = Rand(child, hideSeed)

override def flatArguments: Iterator[Any] = Iterator(child)
override def sql: String = {
s"rand(${if (hideSeed) "" else child.sql})"
}
}

object Rand {
Expand All @@ -126,9 +134,12 @@ object Rand {
""",
since = "1.5.0")
// scalastyle:on line.size.limit
case class Randn(child: Expression) extends RDG with ExpressionWithRandomSeed {
case class Randn(child: Expression, hideSeed: Boolean = false)
extends RDG with ExpressionWithRandomSeed {

def this() = this(Literal(Utils.random.nextLong(), LongType))
def this() = this(Literal(Utils.random.nextLong(), LongType), true)

def this(child: Expression) = this(child, false)

override def withNewSeed(seed: Long): Randn = Randn(Literal(seed, LongType))

Expand All @@ -144,7 +155,12 @@ case class Randn(child: Expression) extends RDG with ExpressionWithRandomSeed {
isNull = FalseLiteral)
}

override def freshCopy(): Randn = Randn(child)
override def freshCopy(): Randn = Randn(child, hideSeed)

override def flatArguments: Iterator[Any] = Iterator(child)
override def sql: String = {
s"randn(${if (hideSeed) "" else child.sql})"
}
}

object Randn {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ class RandomSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(Rand(5419823303878592871L), 0.7145363364564755)
checkEvaluation(Randn(5419823303878592871L), 0.7816815274533012)
}

test("SPARK-31594: Do not display the seed of rand/randn with no argument in output schema") {
assert(Rand(Literal(1L), true).sql === "rand()")
assert(Randn(Literal(1L), true).sql === "randn()")
assert(Rand(Literal(1L), false).sql === "rand(1L)")
assert(Randn(Literal(1L), false).sql === "randn(1L)")
}
}
23 changes: 23 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3425,6 +3425,29 @@ class SQLQuerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark
assert(SQLConf.get.getConf(SQLConf.CODEGEN_FALLBACK) === true)
}
}

test("SPARK-31594: Do not display the seed of rand/randn with no argument in output schema") {
def checkIfSeedExistsInExplain(df: DataFrame): Unit = {
val output = new java.io.ByteArrayOutputStream()
Console.withOut(output) {
df.explain()
}
val projectExplainOutput = output.toString.split("\n").find(_.contains("Project")).get
assert(projectExplainOutput.matches(""".*randn?\(-?[0-9]+\).*"""))
}
val df1 = sql("SELECT rand()")
assert(df1.schema.head.name === "rand()")
checkIfSeedExistsInExplain(df1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we add assert at line 3435, this test will fail.

val df2 = sql("SELECT rand(1L)")
assert(df2.schema.head.name === "rand(1)")
checkIfSeedExistsInExplain(df2)
val df3 = sql("SELECT randn()")
assert(df3.schema.head.name === "randn()")
checkIfSeedExistsInExplain(df1)
val df4 = sql("SELECT randn(1L)")
assert(df4.schema.head.name === "randn(1)")
checkIfSeedExistsInExplain(df2)
}
}

case class Foo(bar: Option[String])