forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-34581][SQL] Don't optimize out grouping expressions from aggre…
…gate expressions without aggregate function ### What changes were proposed in this pull request? This PR adds a new rule `PullOutGroupingExpressions` to pull out complex grouping expressions to a `Project` node under an `Aggregate`. These expressions are then referenced in both grouping expressions and aggregate expressions without aggregate functions to ensure that optimization rules don't change the aggregate expressions to invalid ones that no longer refer to any grouping expressions. ### Why are the changes needed? If aggregate expressions (without aggregate functions) in an `Aggregate` node are complex then the `Optimizer` can optimize out grouping expressions from them and so making aggregate expressions invalid. Here is a simple example: ``` SELECT not(t.id IS NULL) , count(*) FROM t GROUP BY t.id IS NULL ``` In this case the `BooleanSimplification` rule does this: ``` === Applying Rule org.apache.spark.sql.catalyst.optimizer.BooleanSimplification === !Aggregate [isnull(id#222)], [NOT isnull(id#222) AS (NOT (id IS NULL))apache#226, count(1) AS c#224L] Aggregate [isnull(id#222)], [isnotnull(id#222) AS (NOT (id IS NULL))apache#226, count(1) AS c#224L] +- Project [value#219 AS id#222] +- Project [value#219 AS id#222] +- LocalRelation [value#219] +- LocalRelation [value#219] ``` where `NOT isnull(id#222)` is optimized to `isnotnull(id#222)` and so it no longer refers to any grouping expression. Before this PR: ``` == Optimized Logical Plan == Aggregate [isnull(id#222)], [isnotnull(id#222) AS (NOT (id IS NULL))apache#234, count(1) AS c#232L] +- Project [value#219 AS id#222] +- LocalRelation [value#219] ``` and running the query throws an error: ``` Couldn't find id#222 in [isnull(id#222)apache#230,count(1)#226L] java.lang.IllegalStateException: Couldn't find id#222 in [isnull(id#222)apache#230,count(1)#226L] ``` After this PR: ``` == Optimized Logical Plan == Aggregate [_groupingexpression#233], [NOT _groupingexpression#233 AS (NOT (id IS NULL))apache#230, count(1) AS c#228L] +- Project [isnull(value#219) AS _groupingexpression#233] +- LocalRelation [value#219] ``` and the query works. ### Does this PR introduce _any_ user-facing change? Yes, the query works. ### How was this patch tested? Added new UT. Closes apache#32396 from peter-toth/SPARK-34581-keep-grouping-expressions-2. Authored-by: Peter Toth <peter.toth@gmail.com> Signed-off-by: Wenchen Fan <wenchen@databricks.com>
- Loading branch information
1 parent
6ce1b16
commit cfc0495
Showing
24 changed files
with
239 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...t/src/main/scala/org/apache/spark/sql/catalyst/optimizer/PullOutGroupingExpressions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* 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.optimizer | ||
|
||
import scala.collection.mutable | ||
|
||
import org.apache.spark.sql.catalyst.expressions.{Alias, Expression, NamedExpression} | ||
import org.apache.spark.sql.catalyst.expressions.aggregate.AggregateExpression | ||
import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LogicalPlan, Project} | ||
import org.apache.spark.sql.catalyst.rules.Rule | ||
|
||
/** | ||
* This rule ensures that [[Aggregate]] nodes doesn't contain complex grouping expressions in the | ||
* optimization phase. | ||
* | ||
* Complex grouping expressions are pulled out to a [[Project]] node under [[Aggregate]] and are | ||
* referenced in both grouping expressions and aggregate expressions without aggregate functions. | ||
* These references ensure that optimization rules don't change the aggregate expressions to invalid | ||
* ones that no longer refer to any grouping expressions and also simplify the expression | ||
* transformations on the node (need to transform the expression only once). | ||
* | ||
* For example, in the following query Spark shouldn't optimize the aggregate expression | ||
* `Not(IsNull(c))` to `IsNotNull(c)` as the grouping expression is `IsNull(c)`: | ||
* SELECT not(c IS NULL) | ||
* FROM t | ||
* GROUP BY c IS NULL | ||
* Instead, the aggregate expression references a `_groupingexpression` attribute: | ||
* Aggregate [_groupingexpression#233], [NOT _groupingexpression#233 AS (NOT (c IS NULL))#230] | ||
* +- Project [isnull(c#219) AS _groupingexpression#233] | ||
* +- LocalRelation [c#219] | ||
*/ | ||
object PullOutGroupingExpressions extends Rule[LogicalPlan] { | ||
override def apply(plan: LogicalPlan): LogicalPlan = { | ||
plan transform { | ||
case a: Aggregate if a.resolved => | ||
val complexGroupingExpressionMap = mutable.LinkedHashMap.empty[Expression, NamedExpression] | ||
val newGroupingExpressions = a.groupingExpressions.map { | ||
case e if !e.foldable && e.children.nonEmpty => | ||
complexGroupingExpressionMap | ||
.getOrElseUpdate(e.canonicalized, Alias(e, s"_groupingexpression")()) | ||
.toAttribute | ||
case o => o | ||
} | ||
if (complexGroupingExpressionMap.nonEmpty) { | ||
def replaceComplexGroupingExpressions(e: Expression): Expression = { | ||
e match { | ||
case _ if AggregateExpression.isAggregate(e) => e | ||
case _ if e.foldable => e | ||
case _ if complexGroupingExpressionMap.contains(e.canonicalized) => | ||
complexGroupingExpressionMap.get(e.canonicalized).map(_.toAttribute).getOrElse(e) | ||
case _ => e.mapChildren(replaceComplexGroupingExpressions) | ||
} | ||
} | ||
|
||
val newAggregateExpressions = a.aggregateExpressions | ||
.map(replaceComplexGroupingExpressions(_).asInstanceOf[NamedExpression]) | ||
val newChild = Project(a.child.output ++ complexGroupingExpressionMap.values, a.child) | ||
Aggregate(newGroupingExpressions, newAggregateExpressions, newChild) | ||
} else { | ||
a | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.