allisonwang-db commented on a change in pull request #29031:
URL: https://github.com/apache/spark/pull/29031#discussion_r464723488



##########
File path: 
sql/core/src/test/scala/org/apache/spark/sql/execution/RemoveRedundantProjectsSuite.scala
##########
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution
+
+import org.apache.spark.sql.{DataFrame, QueryTest}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.test.{SharedSparkSession, SQLTestUtils}
+import org.apache.spark.util.Utils
+
+class RemoveRedundantProjectsSuite extends QueryTest with SharedSparkSession 
with SQLTestUtils {
+
+  private def assertProjectExecCount(df: DataFrame, expected: Integer): Unit = 
{
+    withClue(df.queryExecution) {
+      val plan = df.queryExecution.executedPlan
+      val actual = plan.collectWithSubqueries { case p: ProjectExec => p }.size
+      assert(actual == expected)
+    }
+  }
+
+  private def assertProjectExec(query: String, enabled: Integer, disabled: 
Integer): Unit = {
+    val df = sql(query)
+    assertProjectExecCount(df, enabled)
+    val result = df.collect()
+    withSQLConf(SQLConf.REMOVE_REDUNDANT_PROJECTS_ENABLED.key -> "false") {
+      val df2 = sql(query)
+      assertProjectExecCount(df2, disabled)
+      checkAnswer(df2, result)
+    }
+  }
+
+  private val tmpPath = Utils.createTempDir()
+
+  override def beforeAll(): Unit = {
+    super.beforeAll()
+    tmpPath.delete()
+    val path = tmpPath.getAbsolutePath
+    spark.range(100).selectExpr("id % 10 as key", "cast(id * 2 as int) as a",
+      "cast(id * 3 as int) as b", "cast(id as string) as c", "array(id, id + 
1, id + 3) as d")
+      .write.partitionBy("key").parquet(path)
+    spark.read.parquet(path).createOrReplaceTempView("testView")
+  }
+
+  override def afterAll(): Unit = {
+    Utils.deleteRecursively(tmpPath)
+    super.afterAll()
+  }
+
+  test("project") {
+    val query = "select * from testView"
+    assertProjectExec(query, 0, 0)
+  }
+
+  test("project with filter") {
+    val query = "select * from testView where a > 5"
+    assertProjectExec(query, 0, 1)
+  }
+
+  test("project with specific column ordering") {
+    val query = "select key, a, b, c from testView"
+    assertProjectExec(query, 1, 1)
+  }
+
+  test("project with extra columns") {
+    val query = "select a, b, c, key, a from testView"
+    assertProjectExec(query, 1, 1)
+  }
+
+  test("project with fewer columns") {
+    val query = "select a from testView where a > 3"
+    assertProjectExec(query, 1, 1)
+  }
+
+  test("aggregate without ordering requirement") {
+    val query = "select sum(a) as sum_a, key, last(b) as last_b " +
+      "from (select key, a, b from testView where a > 100) group by key"
+    assertProjectExec(query, 0, 1)
+  }
+
+  test("aggregate with ordering requirement") {
+    val query = "select a, sum(b) as sum_b from testView group by a"
+    assertProjectExec(query, 1, 1)
+  }
+
+  test("join without ordering requirement") {
+    val query = "select t1.key, t2.key, t1.a, t2.b from (select key, a, b, c 
from testView)" +
+      " as t1 join (select key, a, b, c from testView) as t2 on t1.c > t2.c 
and t1.key > 10"
+    assertProjectExec(query, 1, 3)
+  }
+
+  test("join with ordering requirement") {
+    val query = "select * from (select key, a, c, b from testView) as t1 join 
" +
+      "(select key, a, b, c from testView) as t2 on t1.key = t2.key where t2.a 
> 50"
+    assertProjectExec(query, 2, 2)
+  }
+
+  test("window function") {
+    val query = "select key, b, avg(a) over (partition by key order by a " +
+      "rows between 1 preceding and 1 following) as avg from testView"
+    assertProjectExec(query, 1, 2)
+  }
+
+  test("generate") {
+    val query = "select a, key, explode(d) from testView where a > 10"
+    assertProjectExec(query, 0, 1)
+  }
+
+  test("subquery") {
+    testData

Review comment:
       @cloud-fan The reason why I am using testData here is that the view test 
view here can't have extra `Project', but using normal DataFrame creation 
method it will always create a new `Project` to map their names. So I just 
reused this test data. Is there a way to create DataFrame without introducing 
new Projects?




----------------------------------------------------------------
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:
us...@infra.apache.org



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

Reply via email to