You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2021/02/24 01:33:56 UTC

[GitHub] [spark] maropu commented on a change in pull request #31595: [SPARK-34474][SQL] Remove unnecessary Union under Distinct/Deduplicate

maropu commented on a change in pull request #31595:
URL: https://github.com/apache/spark/pull/31595#discussion_r581500501



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -501,6 +502,46 @@ object RemoveNoopOperators extends Rule[LogicalPlan] {
   }
 }
 
+/**
+ * Remove no-op `Union` from the query plan that do not make any modifications.
+ */
+object RemoveNoopUnion extends Rule[LogicalPlan] {
+  /**
+   * This only removes the `Project` that has only attributes or aliased attributes
+   * from its child.
+   */
+  private def removeAliasOnlyProject(plan: LogicalPlan): LogicalPlan = plan match {
+    case p @ Project(projectList, child) =>
+      val originalOutputs = projectList.collect {

Review comment:
       nit: `originalOutput`?

##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -501,6 +502,46 @@ object RemoveNoopOperators extends Rule[LogicalPlan] {
   }
 }
 
+/**
+ * Remove no-op `Union` from the query plan that do not make any modifications.
+ */
+object RemoveNoopUnion extends Rule[LogicalPlan] {
+  /**
+   * This only removes the `Project` that has only attributes or aliased attributes
+   * from its child.
+   */
+  private def removeAliasOnlyProject(plan: LogicalPlan): LogicalPlan = plan match {
+    case p @ Project(projectList, child) =>
+      val originalOutputs = projectList.collect {
+        case Alias(a: Attribute, _) => a
+        case a: Attribute => a
+      }
+      if (child.output.size == p.output.size && child.output == originalOutputs) {
+        child
+      } else {
+        p
+      }
+    case _ => plan
+  }
+
+  private def removeUnion(u: Union): Option[LogicalPlan] = {
+    val unionChildren = u.children.map(c => removeAliasOnlyProject(c))

Review comment:
       nit: `val unionChildren = u.children.map(removeAliasOnlyProject)`

##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -501,6 +502,46 @@ object RemoveNoopOperators extends Rule[LogicalPlan] {
   }
 }
 
+/**
+ * Remove no-op `Union` from the query plan that do not make any modifications.
+ */
+object RemoveNoopUnion extends Rule[LogicalPlan] {
+  /**
+   * This only removes the `Project` that has only attributes or aliased attributes
+   * from its child.
+   */
+  private def removeAliasOnlyProject(plan: LogicalPlan): LogicalPlan = plan match {
+    case p @ Project(projectList, child) =>

Review comment:
       How about a `Aggregate` case?




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org