Repository navigation
fix: stabilize variance aggregates at large offsets - #27549
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
fcd7648 to
913ab79
Compare
ea43a09 to
482be5f
Compare
aptend
left a comment
There was a problem hiding this comment.
Re-review of exact head 0cea946. The rewrite closes my previous DECIMAL(38,20) subtraction-overflow case, and the normalized state plus v30/legacy protocol gate close the later M2-overflow and mixed-version state-layout findings. One finite-result intermediate overflow remains.
In pkg/sql/colexec/aggexec/vardev2.go:300, the carried normalized variance is multiplied before it is divided. With FLOAT64 rows [0, 2e154, 1e154], after two rows the mean is 1e154 and VAR_POP state is 1e308. The third row equals the mean, so its increment is zero and the exact next VAR_POP is 1e308 * 2 / 3 = 6.666666666666667e307, which is finite. The current left-associative expression evaluates 1e308 * 2 as +Inf first, then divides by 3, so BulkFill followed by Flush returns +Inf. I reproduced this with an operator-level regression; require.False(math.IsInf(result, 0)) fails.
Please evaluate the count ratio before multiplying (or use the scaled helper) and retain this counterexample as a regression.
XuPeng-SH
left a comment
There was a problem hiding this comment.
Reviewed exact head 0cea946. This head does fix the previously reported Decimal128 deviation, unnormalized-M2 overflow, and mixed-version state-layout problems, but blocking numeric edges remain.
[P1] The old normalized variance is still multiplied before it is divided at pkg/sql/colexec/aggexec/vardev2.go:300. For the finite input [1.3e154, -1.3e154, 0], the first two rows produce a finite state variance of 1.69e308. The third row evaluates 1.69e308 * 2 before / 3, stores +Inf, and returns +Inf although VAR_POP is the representable 1.1266666666666666e308. A temporary focused test failed deterministically. Apply the same scaled product/quotient treatment to the existing-variance rescale and add this three-row regression.
[P1] A raw variance state cannot preserve a representable STDDEV when only the squared result exceeds float64. STDDEV_POP over [1e200, -1e200] is the finite 1e200, but updateVarianceState stores +Inf and getResult returns sqrt(+Inf). The same issue affects resident, DISTINCT, and merged execution. The shared VAR/STDDEV state needs an exponent-scaled representation or a STDDEV-safe equivalent, with resident and merge tests.
Performance: the unconditional Frexp/Ldexp path is now in every non-DISTINCT variance row. On this head, a focused M4 microbenchmark measured 8.58 ns/update versus 5.63 ns for the arithmetic path, about 52 percent slower in the recurrence itself. Keep the overflow-safe fallback, but use a finite direct-product fast path for normal values.
Please also revert or split the unrelated HNSW BVT edits. They remove the pre-delta f32 ANN oracle and add an unconditional sleep(20), deterministically increasing the suite while explicitly avoiding a stale-cache behavior unrelated to #27543. The existing variance-focused tests pass, but they do not cover the two counterexamples above.
XuPeng-SH
left a comment
There was a problem hiding this comment.
Re-review after the rebase: the variance implementation and its tests are byte-for-byte unchanged from 0cea9464, so the blockers from my previous review remain on this head.
- The normalized update still multiplies before dividing (
variance * count / nextCount).[1.3e154, -1.3e154, 0]therefore returns+Infalthough the finite VAR_POP result is about1.1266666666666666e308. - STDDEV still stores raw variance and loses finite answers when only the square overflows:
STDDEV_POP([1e200, -1e200])returns+Inf, while the correct result is1e200. - The unconditional Frexp/Ldexp path remains roughly 52% slower in the focused update microbenchmark; use a finite direct arithmetic fast path with exponent scaling only near overflow/underflow.
- The PR still contains unrelated HNSW BVT edits; keep those out of this numerical fix.
Please address the finite-result counterexamples and add regression coverage before approval.
XuPeng-SH
left a comment
There was a problem hiding this comment.
Deep re-review of exact head a1dd839498bbb584644b4e4db3a7a7b08d4f6d13 against merge base 77c9ae710c991b219d1aabfb245cb9109dc5cd3c completed. The scaled normalized-variance representation closes the remaining finite-result overflow cases, including the multiply-before-divide counterexamples and finite STDDEV with unrepresentable squared variance. The common path now stays on direct arithmetic and only falls back to Frexp/Ldexp near overflow or underflow; the unrelated HNSW edits are gone. Decimal deviation handling, resident/DISTINCT/merge execution, sample scaling, and the v30 rolling-upgrade gate/legacy state layout have focused regression coverage. I found no remaining correctness, performance, compatibility, or unhappy-path blocker. Exact-head CI is green. Local focused binaries compiled but could not run in this checkout because the host lacks thirdparties/install/lib/libmo.dylib; that is an environment limitation, not a PR failure.
aunjgr
left a comment
There was a problem hiding this comment.
Approved at exact head a1dd839498bbb584644b4e4db3a7a7b08d4f6d13 against base 77c9ae710c991b219d1aabfb245cb9109dc5cd3c.
The normalized Welford/Chan representation restores translation stability without retaining an overflow-prone M2. The exponent sidecar closes finite VAR results whose intermediate square/weighted sum overflows and finite STDDEV results whose variance itself exceeds float64; ordinary values remain on the direct arithmetic fast path. Decimal inputs retain an exact origin and convert only deviations, including the exact-subtraction overflow fallback. Resident, DISTINCT, merge, population/sample, overflow, underflow, and large-offset cases are covered.
The aggregate-state layout change is capability-gated: pre-v30 remote execution uses the legacy three-vector implementation, the coordinator prevents incompatible multi-stage/shuffle placement, and remote validation fails closed. Current-format spill and mixed-version remote ownership are therefore separated. I found no remaining correctness, performance, compatibility, cleanup, or growth blocker.
…arge-offset # Conflicts: # pkg/defines/const.go
aptend
left a comment
There was a problem hiding this comment.
Deep re-reviewed the complete diff and all historical reviews/threads at exact head c93d071, including the delta after my prior review of 0cea946 and the v32 protocol rebase.
The earlier Decimal128 subtraction, normalized-variance rescaling, exponent-scaled STDDEV, and mixed-version state-layout blockers are closed. The current v32 wire round trips and fails closed across mismatched layouts; resident, DISTINCT, merge, sample/population, spill-facing state serialization, and rolling-upgrade gates were checked. Full aggexec/group/compile tests, focused race suites, vet, build, and diff check pass.
One supported exact-numeric large-offset case remains silently wrong: integer inputs are converted to float64 before the new stable recurrence, so unit differences above 2^53 disappear. The inline operator-level counterexample returns zero instead of the exact representable variance 1.25.
…arge-offset # Conflicts: # pkg/sql/compile/compile.go
…arge-offset # Conflicts: # pkg/defines/const.go
aptend
left a comment
There was a problem hiding this comment.
Deep re-review of exact head 28a1ac8, including the complete diff, all historical reviews/threads, and the delta since my previous review at c93d071. The exact native origin added for BIGINT, BIGINT UNSIGNED, and BIT closes the remaining >2^53 unit-offset failure in resident, DISTINCT, merge, and serialized-state continuation paths. The earlier decimal-deviation, scaled finite VAR/STDDEV, and rolling-upgrade state-layout findings remain closed behind the v35 gate. Owning-package build, vet, full tests, focused race tests (20 iterations), benchmark, and diff check pass; exact-head CI is green. I found no remaining blocker.
|
@Mergifyio queue |
Merge Queue Status
This pull request spent 25 minutes 42 seconds in the queue, with no time running CI. Waiting for
All conditions
ReasonPull request #27549 has been dequeued Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.
Failing checks:
HintYou should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it. Tick the box to put this pull request back in the merge queue (same as
|
What type of PR is this?
Which issue(s) this PR fixes:
Fixes #27543
What this PR does / why we need it:
Replace the unstable sum-of-squares variance calculation with Welford online state and Chan parallel merging. DECIMAL aggregates retain an exact origin and convert only the deviation, preventing loss of small variance around large offsets.
Validation
go test ./pkg/sql/colexec/aggexec -count=1go test -race ./pkg/sql/colexec/aggexecgo test ./pkg/sql/colexec/group -count=1go vet ./pkg/sql/colexec/...aggexeccoverage: 79.0%