peter-toth commented on code in PR #32298:
URL: https://github.com/apache/spark/pull/32298#discussion_r843979947


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/MergeScalarSubqueriesSuite.scala:
##########
@@ -0,0 +1,355 @@
+/*
+ * 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.catalyst.optimizer
+
+import org.apache.spark.sql.catalyst.dsl.expressions._
+import org.apache.spark.sql.catalyst.dsl.plans._
+import org.apache.spark.sql.catalyst.expressions.{Attribute, 
CreateNamedStruct, GetStructField, Literal, ScalarSubquery}
+import org.apache.spark.sql.catalyst.expressions.aggregate.{CollectList, 
CollectSet}
+import org.apache.spark.sql.catalyst.plans._
+import org.apache.spark.sql.catalyst.plans.logical._
+import org.apache.spark.sql.catalyst.rules._
+
+class MergeScalarSubqueriesSuite extends PlanTest {
+
+  override def beforeEach(): Unit = {
+    CTERelationDef.curId.set(0)
+  }
+
+  private object Optimize extends RuleExecutor[LogicalPlan] {
+    val batches = Batch("MergeScalarSubqueries", Once, MergeScalarSubqueries) 
:: Nil
+  }
+
+  val testRelation = LocalRelation('a.int, 'b.int, 'c.string)
+
+  private def extractorExpression(cteIndex: Int, output: Seq[Attribute], 
fieldIndex: Int) = {
+    GetStructField(
+      ScalarSubquery(CTERelationRef(cteIndex, _resolved = true, output, 
subquery = true)),
+      fieldIndex).as("scalarsubquery()")
+  }
+
+  test("Merging subqueries with projects") {
+    val subquery1 = ScalarSubquery(testRelation.select(('a + 1).as("a_plus1")))
+    val subquery2 = ScalarSubquery(testRelation.select(('a + 2).as("a_plus2")))
+    val subquery3 = ScalarSubquery(testRelation.select('b))
+    val subquery4 = ScalarSubquery(testRelation.select(('a + 
1).as("a_plus1_2")))
+    val subquery5 = ScalarSubquery(testRelation.select(('a + 
2).as("a_plus2_2")))
+    val subquery6 = ScalarSubquery(testRelation.select('b.as("b_2")))
+    val originalQuery = testRelation
+      .select(
+        subquery1,
+        subquery2,
+        subquery3,
+        subquery4,
+        subquery5,
+        subquery6)
+
+    val mergedSubquery = testRelation
+      .select(
+        ('a + 1).as("a_plus1"),
+        ('a + 2).as("a_plus2"),
+        'b)
+      .select(
+        CreateNamedStruct(Seq(
+          Literal("a_plus1"), 'a_plus1,
+          Literal("a_plus2"), 'a_plus2,
+          Literal("b"), 'b
+        )).as("mergedValue"))
+    val analyzedMergedSubquery = mergedSubquery.analyze
+    val correctAnswer = WithCTE(
+      testRelation
+        .select(
+          extractorExpression(0, analyzedMergedSubquery.output, 0),
+          extractorExpression(0, analyzedMergedSubquery.output, 1),
+          extractorExpression(0, analyzedMergedSubquery.output, 2),
+          extractorExpression(0, analyzedMergedSubquery.output, 0),
+          extractorExpression(0, analyzedMergedSubquery.output, 1),
+          extractorExpression(0, analyzedMergedSubquery.output, 2)),
+      Seq(CTERelationDef(analyzedMergedSubquery, 0)))
+
+    comparePlans(Optimize.execute(originalQuery.analyze), 
correctAnswer.analyze)
+  }
+
+  test("Merging subqueries with aggregates") {
+    val subquery1 = 
ScalarSubquery(testRelation.groupBy('b)(max('a).as("max_a")))
+    val subquery2 = 
ScalarSubquery(testRelation.groupBy('b)(sum('a).as("sum_a")))
+    val subquery3 = ScalarSubquery(testRelation.groupBy('b)('b))
+    val subquery4 = 
ScalarSubquery(testRelation.groupBy('b)(max('a).as("max_a_2")))
+    val subquery5 = 
ScalarSubquery(testRelation.groupBy('b)(sum('a).as("sum_a_2")))
+    val subquery6 = ScalarSubquery(testRelation.groupBy('b)('b.as("b_2")))
+    val originalQuery = testRelation
+      .select(
+        subquery1,
+        subquery2,
+        subquery3,
+        subquery4,
+        subquery5,
+        subquery6)
+
+    val mergedSubquery = testRelation
+      .groupBy('b)(
+        max('a).as("max_a"),
+        sum('a).as("sum_a"),
+        'b)
+      .select(CreateNamedStruct(Seq(
+        Literal("max_a"), 'max_a,
+        Literal("sum_a"), 'sum_a,
+        Literal("b"), 'b
+      )).as("mergedValue"))
+    val analyzedMergedSubquery = mergedSubquery.analyze
+    val correctAnswer = WithCTE(
+      testRelation
+        .select(
+          extractorExpression(0, analyzedMergedSubquery.output, 0),
+          extractorExpression(0, analyzedMergedSubquery.output, 1),
+          extractorExpression(0, analyzedMergedSubquery.output, 2),
+          extractorExpression(0, analyzedMergedSubquery.output, 0),
+          extractorExpression(0, analyzedMergedSubquery.output, 1),
+          extractorExpression(0, analyzedMergedSubquery.output, 2)),
+      Seq(CTERelationDef(analyzedMergedSubquery, 0)))
+
+    comparePlans(Optimize.execute(originalQuery.analyze), 
correctAnswer.analyze)
+  }
+
+  test("Merging subqueries with filters") {
+    val subquery1 = ScalarSubquery(testRelation.where('a > 1).select('a))
+    // Despite having an extra Project node, `subquery2` is mergeable with 
`subquery1`
+    val subquery2 = ScalarSubquery(testRelation.where('a > 
1).select('b.as("b_1")).select('b_1))
+    // Despite lacking a Project node, `subquery3` is mergeable with the 
result of merging
+    // `subquery1` and `subquery2`

Review Comment:
   Added a new test: 
https://github.com/apache/spark/pull/32298/commits/1ff64e461ea5c710660121727dabf77b1318a587



-- 
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]

Reply via email to