maropu commented on a change in pull request #31595:
URL: https://github.com/apache/spark/pull/31595#discussion_r581534668
##########
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:
I thought it was like this:
```
scala> sql("select distinct * from (select id from t1 group by id union
select id as key from t2 group by id)").explain(true)
== Analyzed Logical Plan ==
id: bigint
Distinct
+- Project [id#6L]
+- SubqueryAlias __auto_generated_subquery_name
+- Distinct
+- Union false, false
:- Aggregate [id#6L], [id#6L]
: +- SubqueryAlias spark_catalog.default.t1
: +- Relation[id#6L] parquet
+- Aggregate [id#7L], [id#7L AS key#13L]
+- SubqueryAlias spark_catalog.default.t2
+- Relation[id#7L] parquet
== Optimized Logical Plan ==
Aggregate [id#6L], [id#6L]
+- Aggregate [id#6L], [id#6L]
+- Union false, false
:- Aggregate [id#6L], [id#6L]
: +- Relation[id#6L] parquet
+- Aggregate [id#7L], [id#7L AS key#13L]
+- Relation[id#7L] parquet
```
----------------------------------------------------------------
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:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]