sql: stabilize decimal VARIANCE with large offsets - #174717
Open
Alignyx wants to merge 1 commit into
Open
Conversation
Decimal VARIANCE rounded a large initial value while updating its Welford mean, then multiplied the rounded residual by the original-scale delta. Distributed plans accumulated one such error per partial aggregation state. Center local inputs around the first value. During final aggregation, center each exact partial sum around one common offset before deriving its mean. This preserves translation-invariant differences without changing the distributed aggregate interface. Fixes cockroachdb#173063 Release note (bug fix): Fixed VARIANCE and related statistical aggregates returning negative or plan-dependent results for high-precision DECIMAL values with a large common offset.
|
Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR. My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI. 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
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.
Fixes #173063.
Root cause
The DECIMAL variance accumulator applied Welford's algorithm directly to absolute input values with the 25-significant-digit intermediate context. When a value had more leading digits than that context retained, the first mean was rounded. A later equal value then produced a nonzero residual against the rounded mean, and the residual product could make the squared-difference state negative. Distributed aggregation repeated the same loss while combining partial means, which amplified the error.
How I tracked it down
I reproduced the report in both local and multi-node distributed execution, then compared it with translation-equivalent and scaling-equivalent inputs whose variance is unchanged. The aggregate and scan paths remained the same while the translated cases returned zero, and row and vectorized execution agreed on the failure. Reducing the input to a singleton exposed the first invalid state before finalization: the internal squared-difference value was already
-1E+10. Varying the number of significant digits put the boundary at the intermediate context's precision. I also kept a nearby nonzero-variance case as a control; that ruled out clamping negative results at finalization because the implementation still needs to distinguish a real small variance from numerical error.Fix
The local accumulator now stores the first exact input as an offset and runs Welford's updates on centered values. The final accumulator uses the first partial mean as a common offset and reconstructs each centered partial sum with exact decimal arithmetic before combining states. This keeps the existing
(squared difference, sum, count)partial-state interface unchanged while preventing high-order digits that cancel out from consuming the working precision.Test coverage
363636.36363636363636, and two partial states whose final variance is5E-21.