peter-toth commented on code in PR #53019: URL: https://github.com/apache/spark/pull/53019#discussion_r2523293942
########## sql/core/src/test/scala/org/apache/spark/sql/PlanMergeSuite.scala: ########## @@ -0,0 +1,342 @@ +/* + * 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 + +import org.apache.spark.sql.execution._ +import org.apache.spark.sql.execution.adaptive.{AdaptiveSparkPlanHelper} +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.test.SharedSparkSession + +class PlanMergeSuite extends QueryTest + with SharedSparkSession + with AdaptiveSparkPlanHelper { + import testImplicits._ + + setupTestData() + + test("Merge non-correlated scalar subqueries") { + Seq(false, true).foreach { enableAQE => + withSQLConf( + SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> enableAQE.toString) { + val df = sql( + """ + |SELECT + | (SELECT avg(key) FROM testData), + | (SELECT sum(key) FROM testData), + | (SELECT count(distinct key) FROM testData) + """.stripMargin) + + checkAnswer(df, Row(50.5, 5050, 100) :: Nil) + + val plan = df.queryExecution.executedPlan + val subqueryIds = collectWithSubqueries(plan) { case s: SubqueryExec => s.id } + val reusedSubqueryIds = collectWithSubqueries(plan) { + case rs: ReusedSubqueryExec => rs.child.id + } + + assert(subqueryIds.size == 1, "Missing or unexpected SubqueryExec in the plan") + assert(reusedSubqueryIds.size == 2, + "Missing or unexpected reused ReusedSubqueryExec in the plan") + } + } + } + + test("Merge non-correlated scalar subqueries in a subquery") { + Seq(false, true).foreach { enableAQE => + withSQLConf( + SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> enableAQE.toString) { + val df = sql( + """ + |SELECT ( + | SELECT + | SUM( + | (SELECT avg(key) FROM testData) + + | (SELECT sum(key) FROM testData) + + | (SELECT count(distinct key) FROM testData)) + | FROM testData + |) + """.stripMargin) + + checkAnswer(df, Row(520050.0) :: Nil) + + val plan = df.queryExecution.executedPlan + val subqueryIds = collectWithSubqueries(plan) { case s: SubqueryExec => s.id } + val reusedSubqueryIds = collectWithSubqueries(plan) { + case rs: ReusedSubqueryExec => rs.child.id + } + + assert(subqueryIds.size == 2, "Missing or unexpected SubqueryExec in the plan") + assert(reusedSubqueryIds.size == 5, + "Missing or unexpected reused ReusedSubqueryExec in the plan") + } + } + } + + test("Merge non-correlated scalar subqueries from different levels") { + Seq(false, true).foreach { enableAQE => + withSQLConf( + SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> enableAQE.toString) { + val df = sql( + """ + |SELECT + | (SELECT avg(key) FROM testData), + | ( + | SELECT + | SUM( + | (SELECT sum(key) FROM testData) + | ) + | FROM testData + | ) + """.stripMargin) + + checkAnswer(df, Row(50.5, 505000) :: Nil) + + val plan = df.queryExecution.executedPlan + val subqueryIds = collectWithSubqueries(plan) { case s: SubqueryExec => s.id } + val reusedSubqueryIds = collectWithSubqueries(plan) { + case rs: ReusedSubqueryExec => rs.child.id + } + + assert(subqueryIds.size == 2, "Missing or unexpected SubqueryExec in the plan") + assert(reusedSubqueryIds.size == 2, + "Missing or unexpected reused ReusedSubqueryExec in the plan") + } + } + } + + test("Merge non-correlated scalar subqueries from different parent plans") { + Seq(false, true).foreach { enableAQE => + withSQLConf( + SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> enableAQE.toString) { + val df = sql( + """ + |SELECT + | ( + | SELECT + | SUM( + | (SELECT avg(key) FROM testData) + | ) + | FROM testData + | ), + | ( + | SELECT + | SUM( + | (SELECT sum(key) FROM testData) + | ) + | FROM testData + | ) + """.stripMargin) + + checkAnswer(df, Row(5050.0, 505000) :: Nil) + + val plan = df.queryExecution.executedPlan + val subqueryIds = collectWithSubqueries(plan) { case s: SubqueryExec => s.id } + val reusedSubqueryIds = collectWithSubqueries(plan) { + case rs: ReusedSubqueryExec => rs.child.id + } + + assert(subqueryIds.size == 2, "Missing or unexpected SubqueryExec in the plan") + assert(reusedSubqueryIds.size == 4, + "Missing or unexpected reused ReusedSubqueryExec in the plan") + } + } + } + + test("Merge non-correlated scalar subqueries with conflicting names") { + Seq(false, true).foreach { enableAQE => + withSQLConf( + SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> enableAQE.toString) { + val df = sql( + """ + |SELECT + | (SELECT avg(key) AS key FROM testData), + | (SELECT sum(key) AS key FROM testData), + | (SELECT count(distinct key) AS key FROM testData) + """.stripMargin) + + checkAnswer(df, Row(50.5, 5050, 100) :: Nil) + + val plan = df.queryExecution.executedPlan + val subqueryIds = collectWithSubqueries(plan) { case s: SubqueryExec => s.id } + val reusedSubqueryIds = collectWithSubqueries(plan) { + case rs: ReusedSubqueryExec => rs.child.id + } + + assert(subqueryIds.size == 1, "Missing or unexpected SubqueryExec in the plan") + assert(reusedSubqueryIds.size == 2, + "Missing or unexpected reused ReusedSubqueryExec in the plan") + } + } + } + + test("Merge non-grouping aggregates") { Review Comment: The following 3 tests are new. ########## sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala: ########## @@ -2389,6 +2389,104 @@ class SubquerySuite extends QueryTest } } + test("Merge non-grouping aggregates") { Review Comment: Moved in https://github.com/apache/spark/pull/53019/commits/dafbd64f2b00d009c13cd7bf7e5b587d735caf58. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
