-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-18969][SQL] Support grouping by nondeterministic expressions #16404
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
Changes from all 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 |
|---|---|---|
|
|
@@ -1918,28 +1918,37 @@ class Analyzer( | |
| case p: Project => p | ||
| case f: Filter => f | ||
|
|
||
| case a: Aggregate if a.groupingExpressions.exists(!_.deterministic) => | ||
| val nondeterToAttr = getNondeterToAttr(a.groupingExpressions) | ||
| val newChild = Project(a.child.output ++ nondeterToAttr.values, a.child) | ||
| a.transformExpressions { case e => | ||
| nondeterToAttr.get(e).map(_.toAttribute).getOrElse(e) | ||
| }.copy(child = newChild) | ||
|
|
||
| // todo: It's hard to write a general rule to pull out nondeterministic expressions | ||
| // from LogicalPlan, currently we only do it for UnaryNode which has same output | ||
| // schema with its child. | ||
| case p: UnaryNode if p.output == p.child.output && p.expressions.exists(!_.deterministic) => | ||
| val nondeterministicExprs = p.expressions.filterNot(_.deterministic).flatMap { expr => | ||
| val leafNondeterministic = expr.collect { | ||
| case n: Nondeterministic => n | ||
| } | ||
| leafNondeterministic.map { e => | ||
| val ne = e match { | ||
| case n: NamedExpression => n | ||
| case _ => Alias(e, "_nondeterministic")(isGenerated = true) | ||
| } | ||
| new TreeNodeRef(e) -> ne | ||
| } | ||
| }.toMap | ||
| val nondeterToAttr = getNondeterToAttr(p.expressions) | ||
| val newPlan = p.transformExpressions { case e => | ||
| nondeterministicExprs.get(new TreeNodeRef(e)).map(_.toAttribute).getOrElse(e) | ||
| nondeterToAttr.get(e).map(_.toAttribute).getOrElse(e) | ||
| } | ||
| val newChild = Project(p.child.output ++ nondeterministicExprs.values, p.child) | ||
| val newChild = Project(p.child.output ++ nondeterToAttr.values, p.child) | ||
| Project(p.output, newPlan.withNewChildren(newChild :: Nil)) | ||
| } | ||
|
|
||
| private def getNondeterToAttr(exprs: Seq[Expression]): Map[Expression, NamedExpression] = { | ||
| exprs.filterNot(_.deterministic).flatMap { expr => | ||
| val leafNondeterministic = expr.collect { case n: Nondeterministic => n } | ||
|
Member
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. Not all the non-deterministic expressions extend
Contributor
Author
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.
Member
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 rule is running once. Thus, it should be safe; otherwise, it might generate many useless Maybe added a nagative test case for this check sql(s"CREATE TEMPORARY FUNCTION statefulUDF AS '${classOf[StatefulUDF].getName}'")
val df = Seq((1, 1)).toDF("a", "b")
df.createOrReplaceTempView("data")
sql("select a, statefulUDF(), sum(b) from data group by a, 2").show()
Contributor
Author
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. is it same with the existing test?
Contributor
Author
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.
Member
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.
Contributor
Author
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. seems we need to add a new API, here we wanna get non-deterministic leaf nodes, and trait
Contributor
Author
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 problem was already there, let's send a new PR to fix it. |
||
| leafNondeterministic.distinct.map { e => | ||
| val ne = e match { | ||
| case n: NamedExpression => n | ||
| case _ => Alias(e, "_nondeterministic")(isGenerated = true) | ||
| } | ||
| e -> ne | ||
| } | ||
| }.toMap | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.analysis | ||
|
|
||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.plans.logical.LocalRelation | ||
|
|
||
| /** | ||
| * Test suite for moving non-deterministic expressions into Project. | ||
| */ | ||
| class PullOutNondeterministicSuite extends AnalysisTest { | ||
|
|
||
| private lazy val a = 'a.int | ||
| private lazy val b = 'b.int | ||
| private lazy val r = LocalRelation(a, b) | ||
| private lazy val rnd = Rand(10).as('_nondeterministic) | ||
| private lazy val rndref = rnd.toAttribute | ||
|
|
||
| test("no-op on filter") { | ||
| checkAnalysis( | ||
| r.where(Rand(10) > Literal(1.0)), | ||
| r.where(Rand(10) > Literal(1.0)) | ||
| ) | ||
| } | ||
|
|
||
| test("sort") { | ||
| checkAnalysis( | ||
| r.sortBy(SortOrder(Rand(10), Ascending)), | ||
| r.select(a, b, rnd).sortBy(SortOrder(rndref, Ascending)).select(a, b) | ||
| ) | ||
| } | ||
|
|
||
| test("aggregate") { | ||
| checkAnalysis( | ||
| r.groupBy(Rand(10))(Rand(10).as("rnd")), | ||
| r.select(a, b, rnd).groupBy(rndref)(rndref.as("rnd")) | ||
| ) | ||
| } | ||
| } |
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.
Just a question. What is the reason why we need to have such a condition
p.output == p.child.output?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 narrow down the scope of the affected operators, but ideally we should use a white-list
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.
Thanks! I see