AngersZhuuuu commented on a change in pull request #28490:
URL: https://github.com/apache/spark/pull/28490#discussion_r436324728



##########
File path: sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
##########
@@ -3496,6 +3496,88 @@ class SQLQuerySuite extends QueryTest with 
SharedSparkSession with AdaptiveSpark
     checkIfSeedExistsInExplain(df2)
   }
 
+  test("SPARK-31670: Struct Field in groupByExpr with CUBE") {
+    withTable("t") {
+      sql(
+        """CREATE TABLE t(
+          |a STRING,
+          |b INT,
+          |c ARRAY<STRUCT<row_id:INT,json_string:STRING>>,
+          |d ARRAY<ARRAY<STRING>>,
+          |e ARRAY<MAP<STRING, INT>>)
+          |USING ORC""".stripMargin)
+
+      checkAnswer(
+        sql(
+          """
+            |SELECT a, each.json_string, SUM(b)
+            |FROM t
+            |LATERAL VIEW EXPLODE(c) x AS each
+            |GROUP BY a, each.json_string
+            |WITH CUBE
+            |""".stripMargin), Nil)
+
+      checkAnswer(
+        sql(
+          """
+            |SELECT a, get_json_object(each.json_string, '$.i'), SUM(b)
+            |FROM t
+            |LATERAL VIEW EXPLODE(c) X AS each
+            |GROUP BY a, get_json_object(each.json_string, '$.i')
+            |WITH CUBE
+            |""".stripMargin), Nil)
+
+      checkAnswer(
+        sql(
+          """
+            |SELECT a, each.json_string AS json_string, SUM(b)
+            |FROM t
+            |LATERAL VIEW EXPLODE(c) x AS each
+            |GROUP BY a, each.json_string
+            |WITH CUBE
+            |""".stripMargin), Nil)
+
+      checkAnswer(
+        sql(
+          """
+            |SELECT a, each.json_string as js, SUM(b)
+            |FROM t
+            |LATERAL VIEW EXPLODE(c) X AS each
+            |GROUP BY a, each.json_string
+            |WITH CUBE
+            |""".stripMargin), Nil)
+
+      checkAnswer(
+        sql(
+          """
+            |SELECT a, each.json_string as js, SUM(b)
+            |FROM t
+            |LATERAL VIEW EXPLODE(c) X AS each
+            |GROUP BY a, each.json_string
+            |WITH ROLLUP
+            |""".stripMargin), Nil)
+
+      sql(
+        """
+          |SELECT a, each.json_string, SUM(b)
+          |FROM t
+          |LATERAL VIEW EXPLODE(c) X AS each
+          |GROUP BY a, each.json_string
+          |GROUPING sets((a),(a, each.json_string))
+          |""".stripMargin).explain(true)
+
+      checkAnswer(
+        sql(
+          """
+            |SELECT a, each.json_string, SUM(b)
+            |FROM t
+            |LATERAL VIEW EXPLODE(c) X AS each
+            |GROUP BY a, each.json_string
+            |GROUPING sets((a),(a, each.json_string))
+            |""".stripMargin), Nil)

Review comment:
       > Could you add tests having queries with `HAVING` clauses?
   
   Seems I make a mistake, having won't have duplicate field since it won't 
have grouping keys in having condition




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