cloud-fan commented on code in PR #37525:
URL: https://github.com/apache/spark/pull/37525#discussion_r1080778360
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala:
##########
@@ -1314,6 +1313,135 @@ class PlannerSuite extends SharedSparkSession with
AdaptiveSparkPlanHelper {
assert(topKs.size == 1)
assert(sorts.isEmpty)
}
+
+ test("SPARK-40086: an attribute and its aliased version in aggregate
expressions should not " +
+ "introduce extra shuffle") {
+ withTempView("df") {
+ spark.range(5).select(col("id"),
col("id").as("value")).createTempView("df")
+ val df = sql("SELECT id, max(value) FROM df GROUP BY id")
+
+ val planned = df.queryExecution.executedPlan
+
+ assert(collect(planned) { case h: HashAggregateExec => h }.nonEmpty)
+
+ val exchanges = collect(planned) { case s: ShuffleExchangeExec => s }
+ assert(exchanges.size == 0)
+ }
+ }
+
+ test("SPARK-40086: multiple aliases to the same attribute in aggregate
expressions should not " +
+ "introduce extra shuffle") {
+ withTempView("df") {
+ spark.range(5).select(col("id").as("key"),
col("id").as("value")).createTempView("df")
+ val df = sql("SELECT key, max(value) FROM df GROUP BY key")
+
+ val planned = df.queryExecution.executedPlan
+
+ assert(collect(planned) { case h: HashAggregateExec => h }.nonEmpty)
+
+ val exchanges = collect(planned) { case s: ShuffleExchangeExec => s }
+ assert(exchanges.size == 0)
+ }
+ }
+
+ test("SPARK-40086: multiple aliases to the same attribute with complex
required distribution " +
+ "should not introduce extra shuffle") {
+ withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+ val df = spark.range(5)
+ val df1 = df.repartition($"id" + $"id")
+ .select($"id".as("key1"), $"id".as("value1"), ($"id" +
$"id").as("idPlusId1"))
+ val df2 = df.repartition($"id" + $"id")
+ .select($"id".as("key2"), $"id".as("value2"), ($"id" +
$"id").as("idPlusId2"))
+ val df3 = df1.join(df2, $"key1" + $"value1" === $"idPlusId2")
+
+ val planned = df3.queryExecution.executedPlan
+
+ val numShuffles = collect(planned) {
+ case e: ShuffleExchangeExec => e
+ }
+ // before: numShuffles is 4
+ assert(numShuffles.size == 2)
+ val numOutputPartitioning = collectFirst(planned) {
+ case e: SortMergeJoinExec => e.outputPartitioning match {
+ case PartitioningCollection(Seq(PartitioningCollection(l),
PartitioningCollection(r))) =>
+ l ++ r
+ case _ => Seq.empty
+ }
+ }.get
+ assert(numOutputPartitioning.size == 8)
+ }
+ }
+
+ test("SPARK-42049: Improve AliasAwareOutputExpression - ordering -
multi-alias") {
+ Seq(0, 1, 2, 5).foreach { limit =>
+ withSQLConf(SQLConf.EXPRESSION_PROJECTION_CANDIDATE_LIMIT.key ->
limit.toString) {
+ val df = spark.range(2).orderBy($"id").selectExpr("id as x", "id as
y", "id as z")
+ val outputOrdering = df.queryExecution.optimizedPlan.outputOrdering
+ limit match {
+ case 5 =>
+ assert(outputOrdering.size == 1)
+ assert(outputOrdering.head.sameOrderExpressions.size == 2)
+
assert(outputOrdering.head.sameOrderExpressions.map(_.asInstanceOf[Attribute].name)
+ .toSet.subsetOf(Set("x", "y", "z")))
+ case 2 =>
+ assert(outputOrdering.size == 1)
+ assert(outputOrdering.head.sameOrderExpressions.size == 1)
+
assert(outputOrdering.head.sameOrderExpressions.map(_.asInstanceOf[Attribute].name)
+ .toSet.subsetOf(Set("x", "y", "z")))
+ case 1 =>
+ assert(outputOrdering.size == 1)
+ assert(outputOrdering.head.sameOrderExpressions.size == 0)
+ case 0 =>
+ assert(outputOrdering.size == 0)
+ }
+ }
+ }
+ }
+
+ test("SPARK-42049: Improve AliasAwareOutputExpression - partitioning -
multi-alias") {
+ Seq(0, 1, 5).foreach { limit =>
+ withSQLConf(SQLConf.EXPRESSION_PROJECTION_CANDIDATE_LIMIT.key ->
limit.toString) {
+ val df = spark.range(2).repartition($"id").selectExpr("id as x", "id
as y", "id as z")
+ val outputPartitioning =
stripAQEPlan(df.queryExecution.executedPlan).outputPartitioning
+ limit match {
+ case 5 =>
+ val p =
outputPartitioning.asInstanceOf[PartitioningCollection].partitionings
+ assert(p.size == 3)
+ assert(p.flatMap(_.asInstanceOf[HashPartitioning].expressions
+ .map(_.asInstanceOf[Attribute].name)).toSet == Set("x", "y",
"z"))
+ case 1 =>
+ val p = outputPartitioning.asInstanceOf[HashPartitioning]
+ assert(p.expressions.size == 1)
+ assert(p.expressions.map(_.asInstanceOf[Attribute].name)
+ .toSet.subsetOf(Set("x", "y", "z")))
+ case 0 =>
+ // the references of child output partitioning is not the subset
of output,
+ // so it has been pruned
+ assert(outputPartitioning.isInstanceOf[UnknownPartitioning])
+ }
+ }
+ }
+ }
+
+ test("SPARK-42049: Improve AliasAwareOutputExpression - ordering -
multi-references") {
+ val df = spark.range(2).selectExpr("id as a", "id as b")
+ .orderBy($"a" + $"b").selectExpr("a as x", "b as y")
+ val outputOrdering = df.queryExecution.optimizedPlan.outputOrdering
+ assert(outputOrdering.size == 1)
+ assert(outputOrdering.head.sameOrderExpressions.size == 0)
+ }
+
+ test("SPARK-42049: Improve AliasAwareOutputExpression - partitioning -
multi-references") {
Review Comment:
some tests here are not really about the planner. Shall we add a test suite
for output ordering and partitioning?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]