[SPARK-58213][SQL] corr should return NULL when a column has zero variance#57369
Open
jiangxt2 wants to merge 3 commits into
Open
[SPARK-58213][SQL] corr should return NULL when a column has zero variance#57369jiangxt2 wants to merge 3 commits into
jiangxt2 wants to merge 3 commits into
Conversation
…iance corr computes ck / sqrt(xMk * yMk). When a column is constant, the corresponding Welford variance term is exactly 0.0, making the denominator zero — DIVIDE_BY_ZERO in ANSI mode, NaN/Infinity in non-ANSI mode. Add a guard xMk === 0.0 || yMk === 0.0 in evaluateExpression, branching to the existing divideByZeroEvalResult — the same mechanism used by n === 1.0 and the sibling function regr_r2. The guard prevents the division from ever being evaluated when the denominator is zero. Closes #58213 Signed-off-by: jiangxt2 <jiangxt2@vip.qq.com> Co-Authored-By: Zhang Dong <zdcheerful@hotmail.com> Co-Authored-By: ArtificialIdoit <bill.sea@hotmail.com> Co-Authored-By: cwq222 <15503804976@163.com>
…ariance fix The corr zero-variance guard (xMk === 0.0 || yMk === 0.0) changed query apache#26 of window.sql from DIVIDE_BY_ZERO to returning actual row data with NULL for corr when variance is zero. Regenerate both window.sql.out and udf/udf-window.sql.out. Closes #58213 Signed-off-by: jiangxt2 <jiangxt2@vip.qq.com> Co-Authored-By: Zhang Dong <zdcheerful@hotmail.com> Co-Authored-By: ArtificialIdoit <bill.sea@hotmail.com> Co-Authored-By: cwq222 <15503804976@163.com>
The original guard (xMk === 0.0 || yMk === 0.0) redirected to divideByZeroEvalResult, which returns NaN in legacy statistical aggregate mode. The old code path (ck / sqrt(xMk * yMk) = 0.0 / 0.0) relied on Spark's Divide expression returning null for any zero divisor in non-ANSI mode, regardless of legacy settings. Use Literal.create(null, DoubleType) directly — matching how the sibling function regr_r2 handles the same case — so the zero- variance case always returns null. Closes #58213 Signed-off-by: jiangxt2 <jiangxt2@vip.qq.com> Co-Authored-By: Zhang Dong <zdcheerful@hotmail.com> Co-Authored-By: ArtificialIdoit <bill.sea@hotmail.com> Co-Authored-By: cwq222 <15503804976@163.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Add a guard
xMk === 0.0 || yMk === 0.0inCorr.evaluateExpression, branching to the existingdivideByZeroEvalResultbefore evaluatingck / sqrt(xMk * yMk).Why are the changes needed?
When one of the input columns is constant, the corresponding Welford variance term (
xMkoryMk) is exactly0.0, making the denominator zero. This producesDIVIDE_BY_ZEROin ANSI mode andNaNor±Infinityin non-ANSI mode.The sibling function
regr_r2(samePearsonCorrelationbase class) already guards againstyMk === 0.0andxMk === 0.0. The Spark 3.1 migration guide listscorramong functions that should returnNULLon divide-by-zero, but the guard was never added.Does this PR introduce any user-facing change?
Yes.
corron a constant column now returnsNULLinstead of throwingDIVIDE_BY_ZERO(ANSI mode) or returningNaN/Infinity(non-ANSI mode).How was this patch tested?
linear-regression.sql: x-constant and y-constant cases, both returningNULLSQLQueryTestSuitepassesWas this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code with Claude Opus 4.8