Skip to content

[SPARK-52232][SQL] Fix non-deterministic queries to produce different results at every step #50957

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Pajaraja
Copy link
Contributor

What changes were proposed in this pull request?

Enable deterministic queries to work with rCTEs. Fix bug where non-deterministic queries produce same result every iteration.

Why are the changes needed?

Currently, recursive CTEs create a new plan for each iteration of the recursion, so the expressions that use randomness "roll-back" to their initial value, causing things like "rand()" to produce the same result every time.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

New tests in golden files.

Was this patch authored or co-authored using generative AI tooling?

No.

@github-actions github-actions bot added the SQL label May 20, 2025
val recursionReseeded = if (recursion.deterministic) {
recursion
} else {
recursion.transformExpressionsDown {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should use transformAllExpressionsWithSubqueries

} else {
recursion.transformExpressionsDown {
case e: ExpressionWithRandomSeed =>
e.withShiftedSeed(currentLevel)
Copy link
Contributor

Choose a reason for hiding this comment

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

does the currentLevel start with 0?

@@ -1267,6 +1267,9 @@ case class Shuffle(child: Expression, randomSeed: Option[Long] = None) extends U

override def withNewSeed(seed: Long): Shuffle = copy(randomSeed = Some(seed))

override def withShiftedSeed(shift: Long): Shuffle =
copy(randomSeed = Some(randomSeed.get + shift))
Copy link
Contributor

Choose a reason for hiding this comment

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

this may cause errors, we should do randomSeed.map(_ + shift)

@@ -260,6 +260,8 @@ case class Uuid(randomSeed: Option[Long] = None) extends LeafExpression with Non

override def withNewSeed(seed: Long): Uuid = Uuid(Some(seed))

override def withShiftedSeed(shift: Long): Uuid = Uuid(Some(randomSeed.get + shift))
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

@@ -114,6 +115,8 @@ case class Rand(child: Expression, hideSeed: Boolean = false) extends Nondetermi

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

override def withShiftedSeed(shift: Long): Rand = Rand(Add(child, Literal(shift)), hideSeed)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
override def withShiftedSeed(shift: Long): Rand = Rand(Add(child, Literal(shift)), hideSeed)
override def withShiftedSeed(shift: Long): Rand = Rand(Add(child, Literal(shift), evalMode = EvalMode.LEGACY), hideSeed)

Copy link
Contributor

Choose a reason for hiding this comment

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

to make sure it won't cause overflow error

@@ -165,6 +168,8 @@ case class Randn(child: Expression, hideSeed: Boolean = false) extends Nondeterm

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

override def withShiftedSeed(shift: Long): Randn = Randn(Add(child, Literal(shift)), hideSeed)
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

-- !query output
org.apache.spark.sql.AnalysisException
{
"errorClass" : "ASSIGNMENT_ARITY_MISMATCH",
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we have this error?


test("Random rCTEs produce different results in different iterations - RAND") {
val df = sql("""WITH RECURSIVE randoms(val) AS (
| SELECT CAST(floor(rand() * 50 + 1) AS INT)
Copy link
Contributor

Choose a reason for hiding this comment

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

shall we specify a seed to make the test result stable?

import org.apache.spark.sql.functions.countDistinct
import org.apache.spark.sql.test.SharedSparkSession

class RecursiveCTESuite extends QueryTest with SharedSparkSession {
Copy link
Contributor

Choose a reason for hiding this comment

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

how does this test suite provide more test coverage in addition to the golden files?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants