You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by we...@apache.org on 2022/02/11 13:15:33 UTC

[spark] branch branch-3.2 updated: [SPARK-38185][SQL] Fix data incorrect if aggregate function is empty

This is an automated email from the ASF dual-hosted git repository.

wenchen pushed a commit to branch branch-3.2
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-3.2 by this push:
     new 3711a81  [SPARK-38185][SQL] Fix data incorrect if aggregate function is empty
3711a81 is described below

commit 3711a813d5a85524aae6fa3219b39cecb592f748
Author: ulysses-you <ul...@gmail.com>
AuthorDate: Fri Feb 11 21:11:52 2022 +0800

    [SPARK-38185][SQL] Fix data incorrect if aggregate function is empty
    
    Add `aggregateExpressions.nonEmpty` check in `groupOnly` function.
    
    The group only condition should check if the aggregate expression is empty.
    
    In DataFrame api, it is allowed to make a empty aggregations.
    
    So the following query should return 1 rather than 0 because it's a global aggregate.
    ```scala
    val emptyAgg = Map.empty[String, String]
    spark.range(2).where("id > 2").agg(emptyAgg).limit(1).count
    ```
    
    yes, bug fix
    
    Add test
    
    Closes #35490 from ulysses-you/SPARK-38185.
    
    Authored-by: ulysses-you <ul...@gmail.com>
    Signed-off-by: Wenchen Fan <we...@databricks.com>
    (cherry picked from commit 25a4c5fa84d64e37cf5c27c7b2f0f29867330bf2)
    Signed-off-by: Wenchen Fan <we...@databricks.com>
---
 .../spark/sql/catalyst/plans/logical/basicLogicalOperators.scala   | 7 ++++++-
 .../test/scala/org/apache/spark/sql/DataFrameAggregateSuite.scala  | 5 +++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala
index 9a1b641..7f33f28 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala
@@ -982,7 +982,12 @@ case class Aggregate(
 
   // Whether this Aggregate operator is group only. For example: SELECT a, a FROM t GROUP BY a
   private[sql] def groupOnly: Boolean = {
-    aggregateExpressions.forall(a => groupingExpressions.exists(g => a.semanticEquals(g)))
+    // aggregateExpressions can be empty through Dateset.agg,
+    // so we should also check groupingExpressions is non empty
+    groupingExpressions.nonEmpty && aggregateExpressions.map {
+      case Alias(child, _) => child
+      case e => e
+    }.forall(a => groupingExpressions.exists(g => a.semanticEquals(g)))
   }
 }
 
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/DataFrameAggregateSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/DataFrameAggregateSuite.scala
index c46db8f..1d12811 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/DataFrameAggregateSuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/DataFrameAggregateSuite.scala
@@ -1422,6 +1422,11 @@ class DataFrameAggregateSuite extends QueryTest
     val res = df.select($"d".cast("decimal(12, 2)").as("d")).agg(avg($"d").cast("string"))
     checkAnswer(res, Row("9999999999.990000"))
   }
+
+  test("SPARK-38185: Fix data incorrect if aggregate function is empty") {
+    val emptyAgg = Map.empty[String, String]
+    assert(spark.range(2).where("id > 2").agg(emptyAgg).limit(1).count == 1)
+  }
 }
 
 case class B(c: Option[Double])

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org