Skip to content

Commit b566818

Browse files
committed
[SPARK-25951][SQL] Remove Alias when canonicalize
1 parent 4afb350 commit b566818

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Canonicalize.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ package org.apache.spark.sql.catalyst.expressions
3434
*/
3535
object Canonicalize {
3636
def execute(e: Expression): Expression = {
37-
expressionReorder(ignoreNamesTypes(e))
37+
expressionReorder(ignoreNamesTypes(removeAlias(e)))
3838
}
3939

4040
/** Remove names and nullability from types. */
@@ -91,4 +91,10 @@ object Canonicalize {
9191

9292
case _ => e
9393
}
94+
95+
/** Remove Aliases, */
96+
private def removeAlias(e: Expression): Expression = e match {
97+
case a: Alias => a.child
98+
case _ => e
99+
}
94100
}

sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,23 @@ class PlannerSuite extends SharedSQLContext {
780780
classOf[PartitioningCollection])
781781
}
782782
}
783+
784+
test("SPARK-25951: avoid redundant shuffle on rename") {
785+
val renamedA = Alias(exprA, "a")()
786+
val doubleRename = Alias(Alias(exprA, "a")(), "b")()
787+
val plan1 = ShuffleExchangeExec(
788+
HashPartitioning(exprA :: Nil, 5),
789+
DummySparkPlan(outputPartitioning = HashPartitioning(doubleRename :: Nil, 5)))
790+
val plan2 = ShuffleExchangeExec(
791+
HashPartitioning(exprA :: Nil, 5),
792+
DummySparkPlan(outputPartitioning = HashPartitioning(renamedA :: Nil, 5)))
793+
val smjExec = SortMergeJoinExec(
794+
doubleRename :: Nil, renamedA :: Nil, Inner, None, plan1, plan2)
795+
796+
val outputPlan = EnsureRequirements(spark.sessionState.conf).apply(smjExec)
797+
assertDistributionRequirementsAreSatisfied(outputPlan)
798+
assert(outputPlan.collect { case _: ShuffleExchangeExec => true }.isEmpty)
799+
}
783800
}
784801

785802
// Used for unit-testing EnsureRequirements

0 commit comments

Comments
 (0)