GitHub user gatorsmile opened a pull request:
https://github.com/apache/spark/pull/11120
[SPARK-13235] [SQL] Removed an Extra Distinct from the Plan with Union
Distinct
Currently, the parser added two `Distinct` operators in the plan if we are
using `Union Distinct`. This PR is to remove the extra `Distinct` from the
plan.
For example, before the fix, the following query has a plan with two
`Distinct`
```scala
sql("select * from t0 union select * from t0").explain(true)
```
```
== Parsed Logical Plan ==
'Project [unresolvedalias(*,None)]
+- 'Subquery u_2
+- 'Distinct
+- 'Project [unresolvedalias(*,None)]
+- 'Subquery u_1
+- 'Distinct
+- 'Union
:- 'Project [unresolvedalias(*,None)]
: +- 'UnresolvedRelation `t0`, None
+- 'Project [unresolvedalias(*,None)]
+- 'UnresolvedRelation `t0`, None
== Analyzed Logical Plan ==
id: bigint
Project [id#16L]
+- Subquery u_2
+- Distinct
+- Project [id#16L]
+- Subquery u_1
+- Distinct
+- Union
:- Project [id#16L]
: +- Subquery t0
: +- Relation[id#16L] ParquetRelation
+- Project [id#16L]
+- Subquery t0
+- Relation[id#16L] ParquetRelation
== Optimized Logical Plan ==
Aggregate [id#16L], [id#16L]
+- Aggregate [id#16L], [id#16L]
+- Union
:- Project [id#16L]
: +- Relation[id#16L] ParquetRelation
+- Project [id#16L]
+- Relation[id#16L] ParquetRelation
```
After the fix, the plan is changed without the extra `Distinct` as follows:
```
== Parsed Logical Plan ==
'Project [unresolvedalias(*,None)]
+- 'Subquery u_1
+- 'Distinct
+- 'Union
:- 'Project [unresolvedalias('id,None)]
: +- 'UnresolvedRelation `t0`, None
+- 'Project [unresolvedalias(cast('id as int) AS id#16,None)]
+- 'UnresolvedRelation `t0`, None
== Analyzed Logical Plan ==
id: bigint
Project [id#17L]
+- Subquery u_1
+- Distinct
+- Union
:- Project [id#17L]
: +- Project [id#17L]
: +- Subquery t0
: +- Relation[id#17L] ParquetRelation
+- Project [cast(id#16 as bigint) AS id#20L]
+- Project [cast(id#17L as int) AS id#16]
+- Subquery t0
+- Relation[id#17L] ParquetRelation
== Optimized Logical Plan ==
Aggregate [id#17L], [id#17L]
+- Union
:- Project [id#17L]
: +- Relation[id#17L] ParquetRelation
+- Project [cast(cast(id#17L as int) as bigint) AS id#20L]
+- Relation[id#17L] ParquetRelation
```
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/gatorsmile/spark unionDistinct
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/spark/pull/11120.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #11120
----
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]