-
Notifications
You must be signed in to change notification settings - Fork 28.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-48719][SQL] Fix the calculation bug of RegrSlope
& RegrIntercept
when the first parameter is null
#47105
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
} | ||
} | ||
|
||
override lazy val mergeExpressions: Seq[Expression] = | ||
covarPop.mergeExpressions ++ varPop.mergeExpressions | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A tuple is added with the value of |
||
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,15 +116,15 @@ SELECT regr_sxx(y, x) FROM testRegression | |
-- !query schema | ||
struct<regr_sxx(y, x):double> | ||
-- !query output | ||
288.66666666666663 | ||
288.6666666666667 | ||
|
||
|
||
-- !query | ||
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,15 +133,15 @@ 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 | ||
SELECT k, regr_sxx(y, x) FROM testRegression WHERE x IS NOT NULL AND y IS NOT NULL GROUP BY k | ||
-- !query schema | ||
struct<k:int,regr_sxx(y, x):double> | ||
-- !query output | ||
2 288.66666666666663 | ||
2 288.6666666666667 | ||
|
||
|
||
-- !query | ||
|
@@ -215,15 +215,15 @@ SELECT regr_slope(y, x) FROM testRegression | |
-- !query schema | ||
struct<regr_slope(y, x):double> | ||
-- !query output | ||
0.8314087759815244 | ||
0.8314087759815242 | ||
|
||
|
||
-- !query | ||
SELECT regr_slope(y, x) FROM testRegression WHERE x IS NOT NULL AND y IS NOT NULL | ||
-- !query schema | ||
struct<regr_slope(y, x):double> | ||
-- !query output | ||
0.8314087759815244 | ||
0.8314087759815242 | ||
|
||
|
||
-- !query | ||
|
@@ -232,15 +232,15 @@ 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 | ||
SELECT k, regr_slope(y, x) FROM testRegression WHERE x IS NOT NULL AND y IS NOT NULL GROUP BY k | ||
-- !query schema | ||
struct<k:int,regr_slope(y, x):double> | ||
-- !query output | ||
2 0.8314087759815244 | ||
2 0.8314087759815242 | ||
|
||
|
||
-- !query | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's add some code comments to explain the null check