Skip to content

sql: improve sqrdiff accuracy for large offsets - #174979

Open
Alignyx wants to merge 1 commit into
cockroachdb:masterfrom
Alignyx:fix-101588-sqrdiff-order-stability
Open

Alignyx wants to merge 1 commit into
cockroachdb:masterfrom
Alignyx:fix-101588-sqrdiff-order-stability

Conversation

@Alignyx

@Alignyx Alignyx commented Sep 10, 2026 •

Copy link
Copy Markdown

SQRDIFF can lose accuracy when its inputs have a large common offset and a much smaller spread. In #101588, the same four integer values produce different answers depending on the order selected by the join. For two copies each of 856809699799498753 and 856809699799531521, the squared deviations sum to 1073741824.

The decimal accumulator already uses Welford's update, but it maintains a mean near the original large values. Updating that mean in a finite-precision context loses digits that are needed for the small differences.

This change centers the inputs around the first value before accumulating. Translation preserves squared deviations, and the running mean now describes the small spread instead of the large offset. The integer implementation also benefits because it delegates to the decimal accumulator.

The partial-state combiner needs the same treatment. It uses the first partial mean as a common origin and computes sum - origin * count with exact multiplication and subtraction before dividing. That avoids first rounding two large means and then subtracting them. Both accumulators account for the additional decimal storage and clear it in Reset.

The regression runs all six orders of the two-value multiset through the direct and partial-state accumulators, with positive, zero and negative offsets. It also exercises NULL input and reuse after Reset. This improves the reported large-offset case; finite-precision arithmetic still applies to general decimal inputs.

Validation

  • bazel test //pkg/sql/sem/builtins:builtins_test --test_arg=-test.run=^TestSqrDiffLargeOffset$ passed.
  • The same test on unmodified 8812064a failed in both the direct and partial-state paths; one observed result was 1073741823.9989077333 instead of 1073741824.
  • bazel test //pkg/sql/sem/builtins:builtins_test passed, including the added regression.
  • Runs used two build jobs, disabled sharding, one Go-test parallel worker, and a fixed seed, with CockroachDB's randomized test knobs, test tenants and DRPC disabled.

The package tests exercise the partial-state implementation directly. They are not a separate multi-node SQL or exhaustive extreme-DECIMAL/window-function validation.

Fixes #101588

Release note (bug fix): Improved SQRDIFF accuracy for integer and decimal inputs with large common offsets. Changes in input order or the combination of partial aggregates could previously produce inaccurate results for these values.

Large common offsets consume precision in the decimal running mean and
can make SQRDIFF inaccurate for reordered inputs. Center direct inputs and
partial aggregate sums before updating the mean. Preserve the additional
decimal memory accounting and reset state between groups.

Add regressions for all orders of a two-value multiset in the direct and
partial-state paths, translated inputs, NULLs and Reset. They fail on the
unmodified baseline and pass with the change; the builtins suite passes.

Production patch generated with Codex CLI through MetamorphicRepair.

Fixes cockroachdb#101588

Release note (bug fix): Improved SQRDIFF accuracy for integer and decimal inputs with
large common offsets. Changes in input order or the combination of partial aggregates
could previously produce inaccurate results for these values.
@Alignyx
Alignyx requested a review from a team as a code owner September 10, 2026 11:39
@Alignyx
Alignyx requested review from spilchen and removed request for a team September 10, 2026 11:39
@blathers-crl

blathers-crl Bot commented Sep 10, 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 10, 2026

This branch has not been deployed

No deployments
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.

SQRDIFF has different results when the order of arguments changed

1 participant