Skip to content

Commit de01a8d

Browse files
mgaido91cloud-fan
authored andcommitted
[SPARK-24373][SQL] Add AnalysisBarrier to RelationalGroupedDataset's and KeyValueGroupedDataset's child
## What changes were proposed in this pull request? When we create a `RelationalGroupedDataset` or a `KeyValueGroupedDataset` we set its child to the `logicalPlan` of the `DataFrame` we need to aggregate. Since the `logicalPlan` is already analyzed, we should not analyze it again. But this happens when the new plan of the aggregate is analyzed. The current behavior in most of the cases is likely to produce no harm, but in other cases re-analyzing an analyzed plan can change it, since the analysis is not idempotent. This can cause issues like the one described in the JIRA (missing to find a cached plan). The PR adds an `AnalysisBarrier` to the `logicalPlan` which is used as child of `RelationalGroupedDataset` or a `KeyValueGroupedDataset`. ## How was this patch tested? added UT Author: Marco Gaido <marcogaido91@gmail.com> Closes apache#21432 from mgaido91/SPARK-24373.
1 parent 672209f commit de01a8d

4 files changed

Lines changed: 104 additions & 8 deletions

File tree

sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class Dataset[T] private[sql](
196196
}
197197

198198
// Wraps analyzed logical plans with an analysis barrier so we won't traverse/resolve it again.
199-
@transient private val planWithBarrier = AnalysisBarrier(logicalPlan)
199+
@transient private[sql] val planWithBarrier = AnalysisBarrier(logicalPlan)
200200

201201
/**
202202
* Currently [[ExpressionEncoder]] is the only implementation of [[Encoder]], here we turn the

sql/core/src/main/scala/org/apache/spark/sql/KeyValueGroupedDataset.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class KeyValueGroupedDataset[K, V] private[sql](
4949
private implicit val kExprEnc = encoderFor(kEncoder)
5050
private implicit val vExprEnc = encoderFor(vEncoder)
5151

52-
private def logicalPlan = queryExecution.analyzed
52+
private def logicalPlan = AnalysisBarrier(queryExecution.analyzed)
5353
private def sparkSession = queryExecution.sparkSession
5454

5555
/**

sql/core/src/main/scala/org/apache/spark/sql/RelationalGroupedDataset.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,17 @@ class RelationalGroupedDataset protected[sql](
6363
groupType match {
6464
case RelationalGroupedDataset.GroupByType =>
6565
Dataset.ofRows(
66-
df.sparkSession, Aggregate(groupingExprs, aliasedAgg, df.logicalPlan))
66+
df.sparkSession, Aggregate(groupingExprs, aliasedAgg, df.planWithBarrier))
6767
case RelationalGroupedDataset.RollupType =>
6868
Dataset.ofRows(
69-
df.sparkSession, Aggregate(Seq(Rollup(groupingExprs)), aliasedAgg, df.logicalPlan))
69+
df.sparkSession, Aggregate(Seq(Rollup(groupingExprs)), aliasedAgg, df.planWithBarrier))
7070
case RelationalGroupedDataset.CubeType =>
7171
Dataset.ofRows(
72-
df.sparkSession, Aggregate(Seq(Cube(groupingExprs)), aliasedAgg, df.logicalPlan))
72+
df.sparkSession, Aggregate(Seq(Cube(groupingExprs)), aliasedAgg, df.planWithBarrier))
7373
case RelationalGroupedDataset.PivotType(pivotCol, values) =>
7474
val aliasedGrps = groupingExprs.map(alias)
7575
Dataset.ofRows(
76-
df.sparkSession, Pivot(Some(aliasedGrps), pivotCol, values, aggExprs, df.logicalPlan))
76+
df.sparkSession, Pivot(Some(aliasedGrps), pivotCol, values, aggExprs, df.planWithBarrier))
7777
}
7878
}
7979

@@ -433,7 +433,7 @@ class RelationalGroupedDataset protected[sql](
433433
df.exprEnc.schema,
434434
groupingAttributes,
435435
df.logicalPlan.output,
436-
df.logicalPlan))
436+
df.planWithBarrier))
437437
}
438438

439439
/**
@@ -459,7 +459,7 @@ class RelationalGroupedDataset protected[sql](
459459
case other => Alias(other, other.toString)()
460460
}
461461
val groupingAttributes = groupingNamedExpressions.map(_.toAttribute)
462-
val child = df.logicalPlan
462+
val child = df.planWithBarrier
463463
val project = Project(groupingNamedExpressions ++ child.output, child)
464464
val output = expr.dataType.asInstanceOf[StructType].toAttributes
465465
val plan = FlatMapGroupsInPandas(groupingAttributes, expr, output, project)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql
19+
20+
import org.apache.spark.api.python.PythonEvalType
21+
import org.apache.spark.sql.catalyst.expressions.PythonUDF
22+
import org.apache.spark.sql.catalyst.plans.logical.AnalysisBarrier
23+
import org.apache.spark.sql.functions.udf
24+
import org.apache.spark.sql.test.SharedSQLContext
25+
import org.apache.spark.sql.types.{LongType, StructField, StructType}
26+
27+
class GroupedDatasetSuite extends QueryTest with SharedSQLContext {
28+
import testImplicits._
29+
30+
private val scalaUDF = udf((x: Long) => { x + 1 })
31+
private lazy val datasetWithUDF = spark.range(1).toDF("s").select($"s", scalaUDF($"s"))
32+
33+
private def assertContainsAnalysisBarrier(ds: Dataset[_], atLevel: Int = 1): Unit = {
34+
assert(atLevel >= 0)
35+
var children = Seq(ds.queryExecution.logical)
36+
(1 to atLevel).foreach { _ =>
37+
children = children.flatMap(_.children)
38+
}
39+
val barriers = children.collect {
40+
case ab: AnalysisBarrier => ab
41+
}
42+
assert(barriers.nonEmpty, s"Plan does not contain AnalysisBarrier at level $atLevel:\n" +
43+
ds.queryExecution.logical)
44+
}
45+
46+
test("SPARK-24373: avoid running Analyzer rules twice on RelationalGroupedDataset") {
47+
val groupByDataset = datasetWithUDF.groupBy()
48+
val rollupDataset = datasetWithUDF.rollup("s")
49+
val cubeDataset = datasetWithUDF.cube("s")
50+
val pivotDataset = datasetWithUDF.groupBy().pivot("s", Seq(1, 2))
51+
datasetWithUDF.cache()
52+
Seq(groupByDataset, rollupDataset, cubeDataset, pivotDataset).foreach { rgDS =>
53+
val df = rgDS.count()
54+
assertContainsAnalysisBarrier(df)
55+
assertCached(df)
56+
}
57+
58+
val flatMapGroupsInRDF = datasetWithUDF.groupBy().flatMapGroupsInR(
59+
Array.emptyByteArray,
60+
Array.emptyByteArray,
61+
Array.empty,
62+
StructType(Seq(StructField("s", LongType))))
63+
val flatMapGroupsInPandasDF = datasetWithUDF.groupBy().flatMapGroupsInPandas(PythonUDF(
64+
"pyUDF",
65+
null,
66+
StructType(Seq(StructField("s", LongType))),
67+
Seq.empty,
68+
PythonEvalType.SQL_GROUPED_MAP_PANDAS_UDF,
69+
true))
70+
Seq(flatMapGroupsInRDF, flatMapGroupsInPandasDF).foreach { df =>
71+
assertContainsAnalysisBarrier(df, 2)
72+
assertCached(df)
73+
}
74+
datasetWithUDF.unpersist(true)
75+
}
76+
77+
test("SPARK-24373: avoid running Analyzer rules twice on KeyValueGroupedDataset") {
78+
val kvDasaset = datasetWithUDF.groupByKey(_.getLong(0))
79+
datasetWithUDF.cache()
80+
val mapValuesKVDataset = kvDasaset.mapValues(_.getLong(0)).reduceGroups(_ + _)
81+
val keysKVDataset = kvDasaset.keys
82+
val flatMapGroupsKVDataset = kvDasaset.flatMapGroups((k, _) => Seq(k))
83+
val aggKVDataset = kvDasaset.count()
84+
val otherKVDataset = spark.range(1).groupByKey(_ + 1)
85+
val cogroupKVDataset = kvDasaset.cogroup(otherKVDataset)((k, _, _) => Seq(k))
86+
Seq((mapValuesKVDataset, 1),
87+
(keysKVDataset, 2),
88+
(flatMapGroupsKVDataset, 2),
89+
(aggKVDataset, 1),
90+
(cogroupKVDataset, 2)).foreach { case (df, analysisBarrierDepth) =>
91+
assertContainsAnalysisBarrier(df, analysisBarrierDepth)
92+
assertCached(df)
93+
}
94+
datasetWithUDF.unpersist(true)
95+
}
96+
}

0 commit comments

Comments
 (0)