Skip to content

[Bug]: VAR_POP / STDDEV_POP lose small variance under large numeric offsets #27543

Closed

Description

@Lundomn

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

Local executed service and source:

0543eb93978de5551e0c07f871924e7db1cec893.

The current remote main head inspected on 2026-08-24 is
ba8954d543af0bdfb1948a8b89f77f5235ac9259. I did not build a fresh binary
at that commit, but its pkg/sql/colexec/aggexec/vardev2.go still has the
same accumulation and sumSq / n - (sum / n)^2 finalization path described
below.

Other Environment Information

- Hardware parameters: local development machine
- OS type: macOS
- Deployment: local standalone MatrixOne, 1 CN / 1 TN / 1 Log service
- Client: MySQL text protocol at 127.0.0.1:6001
- Server version: 8.0.30-MatrixOne-v

Actual Behavior

VAR_POP / VARIANCE, STDDEV_POP, VAR_SAMP, and STDDEV_SAMP lose
small, non-zero variance when values have a large common numeric offset.

For seven exactly representable values 10^12 ... 10^12+6, the population
variance is exactly 4 and the population standard deviation is exactly 2.
MatrixOne returns zero for both DOUBLE and DECIMAL(30,6). The equivalent
translated expression returns the correct result.

Three consecutive executions of the minimal query returned the same tuple:

VAR_POP(x)  STDDEV_POP(x)  VAR_POP(y)        STDDEV_POP(y)     shifted VAR_POP/STDDEV_POP
0           0              0.000000000000    0.000000000000    4 / 2

The same result occurs for BIGINT and BIGINT UNSIGNED.

Large-data confirmation used three independent databases derived from a
250,000-row Kafka-loaded source. Each had 241,379 non-NULL values in the
range 10^12 ... 10^12+6. Every database produced a grossly incorrect
unshifted variance (about 10^12), while the shifted expression returned
4.000029000020.... With GROUP BY, some groups produced negative variance
and STDDEV_POP = NaN. Repeating after an index was created did not change
the incorrect result.

This is different from #25482 / #26392, which concern enabling the
VARIANCE alias in window syntax. It is also different from the fixed
single-row VAR_SAMP / STDDEV_SAMP NULL behavior.

Expected Behavior

Variance is translation invariant:

VAR_POP(x) = VAR_POP(x - C)
STDDEV_POP(x) = STDDEV_POP(x - C)

For the seven-row input below, MatrixOne must return:

VAR_POP(x)      = 4
STDDEV_POP(x)   = 2
VAR_POP(y)      = 4.000000000000
STDDEV_POP(y)   = 2.000000000000

The result must never be negative for a variance, and standard deviation must
not become NaN for finite non-NULL inputs.

Steps to Reproduce

CREATE DATABASE variance_offset_repro;
USE variance_offset_repro;

CREATE TABLE t (
  x DOUBLE NOT NULL,
  y DECIMAL(30,6) NOT NULL
);

INSERT INTO t VALUES
  (1000000000000.0, 1000000000000),
  (1000000000001.0, 1000000000001),
  (1000000000002.0, 1000000000002),
  (1000000000003.0, 1000000000003),
  (1000000000004.0, 1000000000004),
  (1000000000005.0, 1000000000005),
  (1000000000006.0, 1000000000006);

SELECT
  VAR_POP(x), STDDEV_POP(x),
  VAR_POP(y), STDDEV_POP(y)
FROM t;

-- Control: subtracting the common offset should not alter variance.
SELECT
  VAR_POP(x - 1000000000000.0),
  STDDEV_POP(x - 1000000000000.0),
  VAR_POP(y - CAST(1000000000000 AS DECIMAL(30,6))),
  STDDEV_POP(y - CAST(1000000000000 AS DECIMAL(30,6)))
FROM t;

Actual output:

0, 0, 0.000000000000, 0.000000000000
4, 2, 4.000000000000, 2.000000000000

Possible Root Cause

pkg/sql/colexec/aggexec/vardev2.go converts all supported numeric inputs,
including decimal values, to float64; it independently accumulates sum
and sumSq, then calculates variance as:

part1 := sumSq / n
part2 := (sum / n) * (sum / n)
variance := part1 - part2

For values around 10^12, both terms are around 10^24, so subtracting
them destroys the true variance of 4. The existing ULP-based near-zero
clamp then turns the lost result into zero; parallel/grouped accumulation can
instead retain a large signed cancellation error. A numerically stable
online/mergeable algorithm (for example Welford/Chan), or an equivalent
compensated implementation, is needed.

Activity

  1. self-assigned this
    on Aug 24, 2026
  2. added a commit that references this issue on Aug 28, 2026
    02af906
  3. reopened this on Sep 4, 2026
  4. assigned and unassigned on Sep 4, 2026
  5. slyunnn commented on Sep 8, 2026

    @slyunnn

    Regression and extended verification for #27543 (2026-09-08)

    Verification Environment

    • MatrixOne baseline: latest origin/main at ffabe501da007d526544192e1c280cf0ce2c7022 — fix(auto-increment): honor session and ordered insert semantics (#28349).
    • Related fix: PR #27549, fix: stabilize variance aggregates at large offsets, merge commit 02af906c367b3ef7e530336651ba033f8c14861c; it is an ancestor of the verified origin/main.
    • Topology: fresh local 1 LogService + 1 TN + 1 CN deployment, with no Proxy and one CN frontend at 127.0.0.1:26001.
    • Three test endpoints/entry points: direct mysql client at 127.0.0.1:26001; MOTR record client at 127.0.0.1:26001; MOTR replay client (run twice) at 127.0.0.1:26001. They intentionally resolve to the same physical CN because this is a single-CN topology.
    • Client protocol: MySQL TCP text protocol (mysql CLI and MOTR/PyMySQL), authenticated as dump.

    What Was Covered

    • OK: Original reproduction with seven consecutive DOUBLE and DECIMAL(30,6) values near 1000000000000; VAR_POP returned 4 and STDDEV_POP returned 2 for both types.
    • OK: Happy path for VAR_POP, STDDEV_POP, VARIANCE, VAR_SAMP, and STDDEV_SAMP.
    • OK: Compatibility/control types for BIGINT values above 2^53 and near-maximum BIGINT UNSIGNED values.
    • OK: Grouped DOUBLE/DECIMAL aggregation confirmed finite, non-NaN results for independent aggregate states.
    • OK: Boundary/unhappy behavior: NULL inputs are ignored; one non-NULL value gives population variance 0 and NULL sample variance/deviation.
    • OK: Deterministic 100000-row, five-group regression near 1e12; all VAR_POP / STDDEV_POP checks for DOUBLE and DECIMAL remained in the expected range and non-NaN.
    • Not covered: multi-CN or Proxy execution, including wire-level partial-state transfer between independent CNs; the verified topology has one CN and no Proxy.

    Findings

    The original precision-loss regression is not reproducible on the current mainline. The checked original query returned 4.0, 2.0, 4.000000000000, and 2.000000000000 for DOUBLE/DECIMAL population variance and standard deviation respectively.

    Focused current-main aggregation tests passed, including large-offset state/merge, decimal merge, exact integer variance, sample single-value, and protocol-version aggregation coverage:

    TestVarianceStateLargeOffset, TestMergeVarianceStateLargeOffset, TestVarStdDevExecLargeOffset, TestVarPopExecMergeLargeOffset, TestVarPopDecimalLargeOffsetWithNull, TestVarPopDecimalMergeLargeOffset, TestExactIntegerVarianceAtTypeLimits, TestVarSampleSingleNonNullValueReturnsNull, TestRemoteVarianceUsesLegacyStateBeforeProtocolV35, and TestCompileShuffleGroupGatesVarianceByProtocolVersion.

    MOTR / BVT Coverage

    Added and recorded:

    • suites/14_issue_regression/t/issue_27543_variance_offset.test
    • suites/14_issue_regression/r/issue_27543_variance_offset.result

    The case was recorded once and replayed twice successfully: 20 statements per replay, 20 passed, 0 failed. The regression commit is 4d69730b66fce52957a931ccd9017f06a760d652 (test: add MOTR regression for #27543) on test/issue-27543-variance-offset, pushed to matrixorigin/motr via SSH.

    Large-data regression: required and executed. This issue concerns numerically stable incremental and merged aggregate state, so a deterministic 100000-row input was used as the minimum stable scale to cross input-batch boundaries and exercise grouped states. A larger workload was not added because the 100000-row test already covers the relevant numerical/batch dimension without unrelated CI cost.

    Final Conclusion

    Fixed. The related mainline fix preserves the small variance under large numeric offsets for the reproduced DOUBLE and DECIMAL cases, extended aggregate/type/boundary cases, and the 100000-row grouped regression. Multi-CN/Proxy partial-state transport remains explicitly not covered by this single-CN verification topology.

  6. added 3 commits that reference this issue on Sep 17, 2026
    5b219b1
    dd30fd7
    b4e0a15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions