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 2024/07/08 09:22:44 UTC
(spark) branch branch-3.5 updated: [SPARK-48719][SQL][3.5] Fix the calculation bug of RegrSlope & RegrIntercept when the first parameter is null
This is an automated email from the ASF dual-hosted git repository.
wenchen pushed a commit to branch branch-3.5
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-3.5 by this push:
new 67047cde8bb7 [SPARK-48719][SQL][3.5] Fix the calculation bug of RegrSlope & RegrIntercept when the first parameter is null
67047cde8bb7 is described below
commit 67047cde8bb70e32f2b6f8162388240dee215aa4
Author: Wei Guo <gu...@gmail.com>
AuthorDate: Mon Jul 8 17:22:27 2024 +0800
[SPARK-48719][SQL][3.5] Fix the calculation bug of RegrSlope & RegrIntercept when the first parameter is null
### What changes were proposed in this pull request?
This PR aims to fix the calculation bug of RegrSlope&RegrIntercept when the first parameter is null. Regardless of whether the first parameter(y) or the second parameter(x) is null, this tuple should be filtered out.
### Why are the changes needed?
Fix bug.
### Does this PR introduce _any_ user-facing change?
Yes, the calculation changes when the first value of a tuple is null, but the value is truly correct.
### How was this patch tested?
Pass GA and test with build/sbt "~sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z linear-regression.sql"
### Was this patch authored or co-authored using generative AI tooling?
No.
Closes #47230 from wayneguow/SPARK-48719_3_5.
Authored-by: Wei Guo <gu...@gmail.com>
Signed-off-by: Wenchen Fan <we...@databricks.com>
---
.../expressions/aggregate/linearRegression.scala | 20 ++++++++++++++----
.../analyzer-results/linear-regression.sql.out | 4 ++--
.../sql-tests/inputs/linear-regression.sql | 2 +-
.../sql-tests/results/linear-regression.sql.out | 24 +++++++++++-----------
4 files changed, 31 insertions(+), 19 deletions(-)
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/linearRegression.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/linearRegression.scala
index 40518982958c..7d73cf211a6e 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/linearRegression.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/linearRegression.scala
@@ -271,8 +271,14 @@ case class RegrSlope(left: Expression, right: Expression) extends DeclarativeAgg
override lazy val initialValues: Seq[Expression] = covarPop.initialValues ++ varPop.initialValues
- override lazy val updateExpressions: Seq[Expression] =
- covarPop.updateExpressions ++ varPop.updateExpressions
+ override lazy val updateExpressions: Seq[Expression] = {
+ // RegrSlope only handles paris where both y and x are non-empty, so we need additional
+ // judgment for calculating VariancePop.
+ val isNull = left.isNull || right.isNull
+ covarPop.updateExpressions ++ varPop.updateExpressions.zip(varPop.aggBufferAttributes).map {
+ case (newValue, oldValue) => If(isNull, oldValue, newValue)
+ }
+ }
override lazy val mergeExpressions: Seq[Expression] =
covarPop.mergeExpressions ++ varPop.mergeExpressions
@@ -324,8 +330,14 @@ case class RegrIntercept(left: Expression, right: Expression) extends Declarativ
override lazy val initialValues: Seq[Expression] = covarPop.initialValues ++ varPop.initialValues
- override lazy val updateExpressions: Seq[Expression] =
- covarPop.updateExpressions ++ varPop.updateExpressions
+ override lazy val updateExpressions: Seq[Expression] = {
+ // RegrIntercept only handles paris where both y and x are non-empty, so we need additional
+ // judgment for calculating VariancePop.
+ val isNull = left.isNull || right.isNull
+ covarPop.updateExpressions ++ varPop.updateExpressions.zip(varPop.aggBufferAttributes).map {
+ case (newValue, oldValue) => If(isNull, oldValue, newValue)
+ }
+ }
override lazy val mergeExpressions: Seq[Expression] =
covarPop.mergeExpressions ++ varPop.mergeExpressions
diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/linear-regression.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/linear-regression.sql.out
index 7c91139921b5..a791a4f35e98 100644
--- a/sql/core/src/test/resources/sql-tests/analyzer-results/linear-regression.sql.out
+++ b/sql/core/src/test/resources/sql-tests/analyzer-results/linear-regression.sql.out
@@ -1,11 +1,11 @@
-- Automatically generated by SQLQueryTestSuite
-- !query
CREATE OR REPLACE TEMPORARY VIEW testRegression AS SELECT * FROM VALUES
-(1, 10, null), (2, 10, 11), (2, 20, 22), (2, 25, null), (2, 30, 35)
+(1, 10, null), (2, 10, 11), (2, 20, 22), (2, 25, null), (2, 30, 35), (2, null, 40)
AS testRegression(k, y, x)
-- !query analysis
CreateViewCommand `testRegression`, SELECT * FROM VALUES
-(1, 10, null), (2, 10, 11), (2, 20, 22), (2, 25, null), (2, 30, 35)
+(1, 10, null), (2, 10, 11), (2, 20, 22), (2, 25, null), (2, 30, 35), (2, null, 40)
AS testRegression(k, y, x), false, true, LocalTempView, true
+- Project [k#x, y#x, x#x]
+- SubqueryAlias testRegression
diff --git a/sql/core/src/test/resources/sql-tests/inputs/linear-regression.sql b/sql/core/src/test/resources/sql-tests/inputs/linear-regression.sql
index c7cb5bf1117a..df286d2a9b0a 100644
--- a/sql/core/src/test/resources/sql-tests/inputs/linear-regression.sql
+++ b/sql/core/src/test/resources/sql-tests/inputs/linear-regression.sql
@@ -1,6 +1,6 @@
-- Test data.
CREATE OR REPLACE TEMPORARY VIEW testRegression AS SELECT * FROM VALUES
-(1, 10, null), (2, 10, 11), (2, 20, 22), (2, 25, null), (2, 30, 35)
+(1, 10, null), (2, 10, 11), (2, 20, 22), (2, 25, null), (2, 30, 35), (2, null, 40)
AS testRegression(k, y, x);
-- SPARK-37613: Support ANSI Aggregate Function: regr_count
diff --git a/sql/core/src/test/resources/sql-tests/results/linear-regression.sql.out b/sql/core/src/test/resources/sql-tests/results/linear-regression.sql.out
index 1379713a9fb0..e511ea75aae5 100644
--- a/sql/core/src/test/resources/sql-tests/results/linear-regression.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/linear-regression.sql.out
@@ -1,7 +1,7 @@
-- Automatically generated by SQLQueryTestSuite
-- !query
CREATE OR REPLACE TEMPORARY VIEW testRegression AS SELECT * FROM VALUES
-(1, 10, null), (2, 10, 11), (2, 20, 22), (2, 25, null), (2, 30, 35)
+(1, 10, null), (2, 10, 11), (2, 20, 22), (2, 25, null), (2, 30, 35), (2, null, 40)
AS testRegression(k, y, x)
-- !query schema
struct<>
@@ -31,7 +31,7 @@ SELECT k, count(*), regr_count(y, x) FROM testRegression GROUP BY k
struct<k:int,count(1):bigint,regr_count(y, x):bigint>
-- !query output
1 1 0
-2 4 3
+2 5 3
-- !query
@@ -40,7 +40,7 @@ SELECT k, count(*) FILTER (WHERE x IS NOT NULL), regr_count(y, x) FROM testRegre
struct<k:int,count(1) FILTER (WHERE (x IS NOT NULL)):bigint,regr_count(y, x):bigint>
-- !query output
1 0 0
-2 3 3
+2 4 3
-- !query
@@ -99,7 +99,7 @@ SELECT k, avg(x), avg(y), regr_avgx(y, x), regr_avgy(y, x) FROM testRegression G
struct<k:int,avg(x):double,avg(y):double,regr_avgx(y, x):double,regr_avgy(y, x):double>
-- !query output
1 NULL 10.0 NULL NULL
-2 22.666666666666668 21.25 22.666666666666668 20.0
+2 27.0 21.25 22.666666666666668 20.0
-- !query
@@ -116,7 +116,7 @@ SELECT regr_sxx(y, x) FROM testRegression
-- !query schema
struct<regr_sxx(y, x):double>
-- !query output
-288.66666666666663
+288.6666666666667
-- !query
@@ -124,7 +124,7 @@ SELECT regr_sxx(y, x) FROM testRegression WHERE x IS NOT NULL AND y IS NOT NULL
-- !query schema
struct<regr_sxx(y, x):double>
-- !query output
-288.66666666666663
+288.6666666666667
-- !query
@@ -133,7 +133,7 @@ SELECT k, regr_sxx(y, x) FROM testRegression GROUP BY k
struct<k:int,regr_sxx(y, x):double>
-- !query output
1 NULL
-2 288.66666666666663
+2 288.6666666666667
-- !query
@@ -141,7 +141,7 @@ SELECT k, regr_sxx(y, x) FROM testRegression WHERE x IS NOT NULL AND y IS NOT NU
-- !query schema
struct<k:int,regr_sxx(y, x):double>
-- !query output
-2 288.66666666666663
+2 288.6666666666667
-- !query
@@ -215,7 +215,7 @@ SELECT regr_slope(y, x) FROM testRegression
-- !query schema
struct<regr_slope(y, x):double>
-- !query output
-0.8314087759815244
+0.8314087759815242
-- !query
@@ -223,7 +223,7 @@ SELECT regr_slope(y, x) FROM testRegression WHERE x IS NOT NULL AND y IS NOT NUL
-- !query schema
struct<regr_slope(y, x):double>
-- !query output
-0.8314087759815244
+0.8314087759815242
-- !query
@@ -232,7 +232,7 @@ SELECT k, regr_slope(y, x) FROM testRegression GROUP BY k
struct<k:int,regr_slope(y, x):double>
-- !query output
1 NULL
-2 0.8314087759815244
+2 0.8314087759815242
-- !query
@@ -240,7 +240,7 @@ SELECT k, regr_slope(y, x) FROM testRegression WHERE x IS NOT NULL AND y IS NOT
-- !query schema
struct<k:int,regr_slope(y, x):double>
-- !query output
-2 0.8314087759815244
+2 0.8314087759815242
-- !query
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org