-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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-37199][SQL] Add deterministic field to QueryPlan #34470
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,13 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]] | |
AttributeSet.fromAttributeSets(expressions.map(_.references)) -- producedAttributes | ||
} | ||
|
||
/** | ||
* Returns true when the all the expressions in the current node as well as all of its children | ||
* are deterministic | ||
*/ | ||
lazy val deterministic: Boolean = expressions.forall(_.deterministic) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait .. why is this in query plan? What about physical plans vs logical plans? should both be marked? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should move this to logical plan only since it doesn't make sense physical plans have different determinism. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. physical plan can override this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can physical plan have a different determinism to ones in logical plan? e.g., Sample is non-deterministic. I think physical plans of Sample should always be non-deterministic. Otherwise, the output will be inconsistent for which physical plan is used. The opposite case is the same too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yea, if we override this lazy val in a logical plan, we should do it in the corresponding physical plan as well. Moving this to logical plan is also OK, if we don't need it in physical plan at all. cc @maryannxue There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if we optimize something, that should always happen in optimizer with logical plans ... right? If we can do something with physical plans, we will have to add another argument for every non deterministic plan e.g.) case class Sample(
lowerBound: Double,
upperBound: Double,
withReplacement: Boolean,
seed: Long,
+ deterministic: Boolean,
child: LogicalPlan) extends UnaryNode {
case class SampleExec(
lowerBound: Double,
upperBound: Double,
withReplacement: Boolean,
seed: Long,
+ deterministic: Boolean,
child: SparkPlan) extends UnaryExecNode with CodegenSupport { which is pretty much different from how we do in Otherwise, we will have to recalculate it for each plan, etc. |
||
children.forall(_.deterministic) | ||
|
||
/** | ||
* Attributes that are referenced by expressions but not provided by this node's children. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1931,18 +1931,29 @@ class SubquerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark | |
sql( | ||
""" | ||
|SELECT c1, s, s * 10 FROM ( | ||
| SELECT c1, (SELECT FIRST(c2) FROM t2 WHERE t1.c1 = t2.c1) s FROM t1) | ||
| SELECT c1, (SELECT MIN(c2) FROM t2 WHERE t1.c1 = t2.c1) s FROM t1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the error if we don't make this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one is fine, but the one below fails with: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a side note - I have been arguing, that first/last should be deterministic functions, but it has not gotten any attention - #29810. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we not change the test query and assert the error instead?
+1 even though FIRST/LAST are not truly deterministic during execution. The purpose of this field is for determining the eligibility of query rewrites. Postgres has a nice categorization of those: SUM, AVG are not completely deterministic (when running distributed-ly) neither, but we can still do query optimizations over them, and I think it'd be fine for LAST/FIRST too. Differently, rand() has to be marked as non-deterministic because we don't want query rewrites to move, duplicate or dedup it. |
||
|""".stripMargin), | ||
correctAnswer) | ||
checkAnswer( | ||
sql( | ||
""" | ||
|SELECT c1, s, s * 10 FROM ( | ||
| SELECT c1, SUM((SELECT FIRST(c2) FROM t2 WHERE t1.c1 = t2.c1)) s | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this query also fail in other databases like pgsql? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this subquery semantically the same as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It seems fine to mark first/last deterministic? #34470 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Thanks @cloud-fan! |
||
| SELECT c1, SUM((SELECT MIN(c2) FROM t2 WHERE t1.c1 = t2.c1)) s | ||
| FROM t1 GROUP BY c1 | ||
|) | ||
|""".stripMargin), | ||
correctAnswer) | ||
} | ||
} | ||
|
||
test("SPARK-37199: deterministic in QueryPlan considers subquery") { | ||
val deterministicQueryPlan = sql("select (select 1 as b) as b") | ||
.queryExecution.executedPlan | ||
assert(deterministicQueryPlan.deterministic) | ||
|
||
val nonDeterministicQueryPlan = sql("select (select rand(1) as b) as b") | ||
.queryExecution.executedPlan | ||
assert(!nonDeterministicQueryPlan.deterministic) | ||
} | ||
|
||
} |
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.
qq: should we mark all non-deterministic plans as so? e.g.
Sample
?