viirya commented on a change in pull request #31595:
URL: https://github.com/apache/spark/pull/31595#discussion_r580913391
##########
File path:
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSetOperationsSuite.scala
##########
@@ -808,6 +813,53 @@ class DataFrameSetOperationsSuite extends QueryTest with
SharedSparkSession {
// scalastyle:on
checkAnswer(union, row1 :: row2 :: Nil)
}
+
+ test("SPARK-34474: Remove unnecessary Union under Distinct") {
+ Seq(RemoveNoopUnion.ruleName, "").map { ruleName =>
+ withSQLConf(SQLConf.OPTIMIZER_EXCLUDED_RULES.key -> ruleName) {
+ val distinctUnionDF1 = testData.union(testData).distinct()
+ checkAnswer(distinctUnionDF1, testData.distinct())
+
+
+ val distinctUnionDF2 =
testData.union(testData).dropDuplicates(Seq("key"))
+ checkAnswer(distinctUnionDF2, testData.dropDuplicates(Seq("key")))
+
+ val distinctUnionDF3 = sql(
+ """
+ |select key, value from testData
+ |union
+ |select key, value from testData
+ |""".stripMargin)
+ checkAnswer(distinctUnionDF3, testData.distinct())
+
+ val distinctUnionDF4 = sql(
+ """
+ |select distinct key, expr
+ |from
+ |(
+ | select key, key + 1 as expr
+ | from testData
+ | union all
+ | select key, key + 2 as expr
+ | from testData
+ |)
Review comment:
I posted the query plan with/without this rule for the query from the
diff in the above link. I don't see the query plan is different. I'm not sure
which query plan is wrong?
```
sql(
"""
|select key, key + 2 as expr
|from testData4
|""".stripMargin)
```
```
== Physical Plan ==
*(1) Project [key#2641, (key#2641 + 2) AS expr#2645]
+- *(1) SerializeFromObject [knownnotnull(assertnotnull(input[0,
org.apache.spark.sql.test.SQLTestData$TestData4, true])).key AS key#2641]
+- Scan[obj#2640]
```
```
== Physical Plan ==
*(1) Project [key#2732, (key#2732 + 2) AS expr#2736]
+- *(1) SerializeFromObject [knownnotnull(assertnotnull(input[0,
org.apache.spark.sql.test.SQLTestData$TestData4, true])).key AS key#2732]
+- Scan[obj#2731]
```
----------------------------------------------------------------
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]