Skip to content

sql: stabilize decimal VARIANCE with large offsets - #174717

Open
Alignyx wants to merge 1 commit into
cockroachdb:masterfrom
Alignyx:fix-173063-decimal-variance-precision
Open

sql: stabilize decimal VARIANCE with large offsets#174717
Alignyx wants to merge 1 commit into
cockroachdb:masterfrom
Alignyx:fix-173063-decimal-variance-precision

Conversation

@Alignyx

@Alignyx Alignyx commented Sep 5, 2026

Copy link
Copy Markdown

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

  • Added aggregate unit cases for equal large-offset values returning zero, a nearby nonzero dataset returning 363636.36363636363636, and two partial states whose final variance is 5E-21.
  • Added the reported high-offset SQL regression with NULL input, an equal-value zero result, a translated zero oracle, a nearby nonzero control, and forced distributed/vectorized execution.
  • Ran the focused regression across all aggregate logic-test configurations, the complete local and fake-distributed aggregate suites, the full builtin aggregate package, and the five-node distributed aggregation suite.

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.
@Alignyx
Alignyx requested a review from a team as a code owner September 5, 2026 01:22
@Alignyx
Alignyx requested review from shghasemi and removed request for a team September 5, 2026 01:22
@blathers-crl

blathers-crl Bot commented Sep 5, 2026

Copy link
Copy Markdown

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.

@blathers-crl blathers-crl Bot added the O-community Originated from the community label Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-community Originated from the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: VARIANCE function returns negative value for large DECIMAL when all non-NULL values are equal, with error amplified under distributed aggregation

1 participant