Repository navigation
Description
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-vActual 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 - part2For 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
fix: stabilize variance aggregates at large offsets (#27549)
slyunnn commentedon Sep 8, 2026
Regression and extended verification for #27543 (2026-09-08)
Verification Environment
origin/mainatffabe501da007d526544192e1c280cf0ce2c7022—fix(auto-increment): honor session and ordered insert semantics (#28349).fix: stabilize variance aggregates at large offsets, merge commit02af906c367b3ef7e530336651ba033f8c14861c; it is an ancestor of the verifiedorigin/main.1 LogService + 1 TN + 1 CNdeployment, with no Proxy and one CN frontend at127.0.0.1:26001.mysqlclient at127.0.0.1:26001; MOTR record client at127.0.0.1:26001; MOTR replay client (run twice) at127.0.0.1:26001. They intentionally resolve to the same physical CN because this is a single-CN topology.mysqlCLI and MOTR/PyMySQL), authenticated asdump.What Was Covered
DOUBLEandDECIMAL(30,6)values near1000000000000;VAR_POPreturned4andSTDDEV_POPreturned2for both types.VAR_POP,STDDEV_POP,VARIANCE,VAR_SAMP, andSTDDEV_SAMP.BIGINTvalues above2^53and near-maximumBIGINT UNSIGNEDvalues.DOUBLE/DECIMALaggregation confirmed finite, non-NaNresults for independent aggregate states.NULLinputs are ignored; one non-NULLvalue gives population variance0andNULLsample variance/deviation.100000-row, five-group regression near1e12; allVAR_POP/STDDEV_POPchecks forDOUBLEandDECIMALremained in the expected range and non-NaN.Findings
The original precision-loss regression is not reproducible on the current mainline. The checked original query returned
4.0,2.0,4.000000000000, and2.000000000000forDOUBLE/DECIMALpopulation 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, andTestCompileShuffleGroupGatesVarianceByProtocolVersion.MOTR / BVT Coverage
Added and recorded:
suites/14_issue_regression/t/issue_27543_variance_offset.testsuites/14_issue_regression/r/issue_27543_variance_offset.resultThe case was recorded once and replayed twice successfully:
20statements per replay,20passed,0failed. The regression commit is4d69730b66fce52957a931ccd9017f06a760d652(test: add MOTR regression for #27543) ontest/issue-27543-variance-offset, pushed tomatrixorigin/motrvia 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 the100000-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
DOUBLEandDECIMALcases, extended aggregate/type/boundary cases, and the100000-row grouped regression. Multi-CN/Proxy partial-state transport remains explicitly not covered by this single-CN verification topology.fix: stabilize variance aggregates at large offsets (matrixorigin#27549)
fix: stabilize variance aggregates at large offsets (matrixorigin#27549)
fix: stabilize variance aggregates at large offsets (matrixorigin#27549)