-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[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
base: master
Are you sure you want to change the base?
[SPARK-52232][SQL] Fix non-deterministic queries to produce different results at every step #50957
Conversation
… results at every step
val recursionReseeded = if (recursion.deterministic) { | ||
recursion | ||
} else { | ||
recursion.transformExpressionsDown { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
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.