Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ case class RegrR2(y: Expression, x: Expression) extends PearsonCorrelation(y, x,
override def prettyName: String = "regr_r2"
override val evaluateExpression: Expression = {
val corr = ck / sqrt(xMk * yMk)
If(xMk === 0.0, Literal.create(null, DoubleType),
If(yMk === 0.0, Literal.create(1.0, DoubleType), corr * corr))
// In PearsonCorrelation, x and y are swapped, so here xMk refers to the dependent variable
// and yMk to the independent variable
If(yMk === 0.0, Literal.create(null, DoubleType),
If(xMk === 0.0, Literal.create(1.0, DoubleType), corr * corr))
}
override protected def withNewChildrenInternal(
newLeft: Expression, newRight: Expression): RegrR2 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,29 @@ Aggregate [k#x], [k#x, regr_intercept(cast(y#x as double), cast(x#x as double))
+- Project [k#x, y#x, x#x]
+- SubqueryAlias testRegression
+- LocalRelation [k#x, y#x, x#x]


-- !query
SELECT regr_r2(k, x) FROM testRegression where k=2
-- !query analysis
Aggregate [regr_r2(cast(k#x as double), cast(x#x as double)) AS regr_r2(k, x)#x]
+- Filter (k#x = 2)
+- SubqueryAlias testregression
+- View (`testRegression`, [k#x, y#x, x#x])
+- Project [cast(k#x as int) AS k#x, cast(y#x as int) AS y#x, cast(x#x as int) AS x#x]
+- Project [k#x, y#x, x#x]
+- SubqueryAlias testRegression
+- LocalRelation [k#x, y#x, x#x]


-- !query
SELECT regr_r2(y, k) FROM testRegression where k=2
-- !query analysis
Aggregate [regr_r2(cast(y#x as double), cast(k#x as double)) AS regr_r2(y, k)#x]
+- Filter (k#x = 2)
+- SubqueryAlias testregression
+- View (`testRegression`, [k#x, y#x, x#x])
+- Project [cast(k#x as int) AS k#x, cast(y#x as int) AS y#x, cast(x#x as int) AS x#x]
+- Project [k#x, y#x, x#x]
+- SubqueryAlias testRegression
+- LocalRelation [k#x, y#x, x#x]
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ SELECT regr_intercept(y, x) FROM testRegression;
SELECT regr_intercept(y, x) FROM testRegression WHERE x IS NOT NULL AND y IS NOT NULL;
SELECT k, regr_intercept(y, x) FROM testRegression GROUP BY k;
SELECT k, regr_intercept(y, x) FROM testRegression WHERE x IS NOT NULL AND y IS NOT NULL GROUP BY k;

-- SPARK-55969: regr_r2 should treat first param as dependent variable
SELECT regr_r2(k, x) FROM testRegression where k=2;
SELECT regr_r2(y, k) FROM testRegression where k=2;
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,19 @@ SELECT k, regr_intercept(y, x) FROM testRegression WHERE x IS NOT NULL AND y IS
struct<k:int,regr_intercept(y, x):double>
-- !query output
2 1.1547344110854496


-- !query
SELECT regr_r2(k, x) FROM testRegression where k=2
-- !query schema
struct<regr_r2(k, x):double>
-- !query output
1.0


-- !query
SELECT regr_r2(y, k) FROM testRegression where k=2
-- !query schema
struct<regr_r2(y, k):double>
-- !query output
NULL