-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-58291][SQL] Guard statistical-aggregate merge against empty-buffer overflow to NaN #57460
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
71 changes: 71 additions & 0 deletions
71
...est/scala/org/apache/spark/sql/catalyst/expressions/aggregate/MergeEmptyBufferSuite.scala
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.spark.sql.catalyst.expressions.aggregate | ||
|
|
||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions.{AttributeReference, JoinedRow} | ||
| import org.apache.spark.sql.types.DoubleType | ||
|
|
||
| /** | ||
| * Focused regression coverage for the empty-buffer guards in the `merge` expressions of the | ||
| * statistical aggregates. Merging an empty buffer into a populated one (or vice versa) is an | ||
| * identity, but without the guards a large average makes the intermediate `delta * deltaN` term | ||
| * overflow to `Infinity` before it is multiplied by the zero count, and `Infinity * 0 = NaN` | ||
| * corrupts the moments. The end-to-end `DataFrameAggregateSuite` cases use `repartition(1)`, which | ||
| * only produces the empty-left merge; here both the `isEmptyLeft` and `isEmptyRight` branches are | ||
| * exercised directly by driving the merger projection with the empty buffer on each side, under | ||
| * both interpreted and generated execution. | ||
| */ | ||
| class MergeEmptyBufferSuite extends TestWithAndWithoutCodegen { | ||
| private val x = AttributeReference("x", DoubleType, nullable = true)() | ||
| private val y = AttributeReference("y", DoubleType, nullable = true)() | ||
|
|
||
| // Merge the empty buffer on each side and assert the populated buffer is returned unchanged (an | ||
| // empty-buffer merge is an identity), which also proves no term overflowed to Infinity/NaN. | ||
| private def checkMergeWithEmptyIsIdentity( | ||
| evaluator: DeclarativeAggregateEvaluator, populated: InternalRow): Unit = { | ||
| val empty = evaluator.initialize() | ||
| val joiner = new JoinedRow | ||
| // isEmptyLeft: empty buffer as the left (accumulator) operand. | ||
| assert(evaluator.merger(joiner(empty, populated)).copy() === populated) | ||
| // isEmptyRight: empty buffer as the right (input) operand. | ||
| assert(evaluator.merger(joiner(populated, empty)).copy() === populated) | ||
| } | ||
|
|
||
| testBothCodegenAndInterpreted("SPARK-58291: var_pop empty-buffer merge is overflow-safe") { | ||
| val evaluator = DeclarativeAggregateEvaluator(VariancePop(x, nullOnDivideByZero = true), Seq(x)) | ||
| // Two equal, very large finite values: a populated buffer with a huge average but zero | ||
| // variance, which is exactly the shape that overflowed `delta * deltaN`. | ||
| val populated = evaluator.update(InternalRow(1.0e155), InternalRow(1.0e155)) | ||
| checkMergeWithEmptyIsIdentity(evaluator, populated) | ||
| } | ||
|
|
||
| testBothCodegenAndInterpreted("SPARK-58291: covar_pop empty-buffer merge is overflow-safe") { | ||
| val evaluator = | ||
| DeclarativeAggregateEvaluator(CovPopulation(x, y, nullOnDivideByZero = true), Seq(x, y)) | ||
| val populated = evaluator.update(InternalRow(1.0e155, 1.0e155), InternalRow(1.0e155, 1.0e155)) | ||
| checkMergeWithEmptyIsIdentity(evaluator, populated) | ||
| } | ||
|
|
||
| testBothCodegenAndInterpreted("SPARK-58291: corr empty-buffer merge is overflow-safe") { | ||
| val evaluator = DeclarativeAggregateEvaluator(Corr(x, y, nullOnDivideByZero = true), Seq(x, y)) | ||
| val populated = evaluator.update( | ||
| InternalRow(1.0e155, 1.0e-150), | ||
| InternalRow(1.000000000000001e155, 2.0e-150)) | ||
| checkMergeWithEmptyIsIdentity(evaluator, populated) | ||
| } | ||
| } |
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
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.
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.
[P3][non-blocking] Consider directly exercising both empty-buffer merge directions.
These end-to-end cases reproduce the empty-left final-merge failure, but
repartition(1)does not independently exercise the newly addedisEmptyRightbranch. A focused Catalyst regression usingDeclarativeAggregateEvaluator.mergewith a populated buffer and an empty buffer in both orders, ideally under both interpreted and generated execution, would protect both branches without changing the production fix.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.
Thanks, good call. Added
MergeEmptyBufferSuite(Catalyst) that drives the merger projection directly with the empty buffer on each side --merger(joiner(empty, populated))andmerger(joiner(populated, empty))-- forvar_pop,covar_pop, andcorr, under bothCODEGEN_ONLYandNO_CODEGENviaTestWithAndWithoutCodegen. Merging an empty buffer is an identity, so it asserts the populated buffer is returned unchanged, which also proves nothing overflowed toInfinity/NaN.Mutation-tested both ways: reverting the guard fails all six cases; isolating just the
isEmptyRightassertion (the branchrepartition(1)never reaches) still fails on the reverted code (xMk/m2come backNaN), confirming that branch is now independently covered.I kept the fix in a separate commit and added the tests in a follow-up commit so the diff is easy to re-review.