Repository navigation
fix(aggexec): widen AVG sum state to Decimal256 for DECIMAL(29..34) - #32488
Conversation
AVG over a DECIMAL(p,s) column with p in 29..34 promoted its result to DECIMAL(p+4,s+4), which still fits Decimal128, so aggregate dispatch selected sumDecimal128FastExec, whose single Decimal128 sum state overflowed past 2^127-1 at about 1.7*10^(38-p) rows although the exact mean always fits the declared result. At p=34, 17015 copies of 10^34-1 failed with 20301 Decimal128 Add overflow while the exact mean fits the declared DECIMAL(38,4) result. Route such AVG to the existing sumAvgDecExec[Decimal128, Decimal256] and finalize through the new narrowAvgResult mode: compute the exact mean at the declared result scale in the Decimal256 domain, then narrow to the unchanged public Decimal128 result type. SUM, widths <= 28, and widths >= 35 keep their existing dispatch. Add focused UTs (boundary, grouped/distinct/window, dispatch controls) and the avg_decimal34_boundary BVT pair with an oracle-reviewed result. Related to matrixorigin#32429
CI coverage gate fell to 69.70% on sumavg2.go modified lines because the new dispatch helper's promoted branch and narrowDecimal256Avg's guard ladder had no direct tests. Add focused UTs for both.
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? |
|
Queue requested — the merge queue status continues in this comment ↓. |
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 6150dc49dfc96a7f8654de7676674dc5ba5c99cb
Assessment: approve
The fix reuses Decimal256 aggregation and preserves Decimal128 AVG results with bounded, accounted finalization. No actionable defects found.
Model: gpt-6.1-sol / max
Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0
Validation:
-
Full aggexec suite passed: 3,177 test/subtest passes, no failures or skips (aggexec-full-ready.stdout.jsonl; checks/1794331458372230142-3.json).
-
Codec round-trips, window snapshots and all 27 injected flush-allocation failures passed (agg-probes.stdout.jsonl; checks/1794331743270066858-3.json).
-
All 17 SQL fixture statements passed twice through MySQL, including exact values, result metadata, CTAS type and cleanup (public-probe.stdout.jsonl; checks/1794331746576725845-3.json).
-
Formatting, independent arithmetic oracles, go vet and configured incremental lint passed. validation-audit.json reconciles receipts, logs, unchanged source and complete discussion coverage.
Limitations:
- Local SQL replay used one embedded CN. Normal mo-tester passes were author-reported and were not independently rerun. Pending CI was not counted as a pass.
✅ Merged into
|
What type of PR is this?
Which issue(s) this PR fixes:
Related to #32429
What this PR does / why we need it:
AVG over a valid DECIMAL(34,0) column failed with
20301 Decimal128 Add overflowat 17015 identical rows of10^34 - 1, although the exact average9999999999999999999999999999999999.0000fits the declared DECIMAL(38,4) result, while casting the same values to DECIMAL(35,0) succeeded.Root cause.
AvgReturnTypepromotes AVG over DECIMAL(p,s) to DECIMAL(p+4,s+4), which still fits Decimal128 storage for p = 29..34, so aggregate dispatch selectedsumDecimal128FastExec, whose single Decimal128 sum state is checked only against the Decimal128 domain. The exact mean of any in-range group is bounded by the input magnitude and always fits the declared result, but the intermediate sum accumulator passes2^127 - 1after about1.7 * 10^(38-p)rows (17015 rows at p = 34), raising the intermediate overflow. The widened-state executor existed but was only dispatched when the AVG result itself was Decimal256 (p >= 35); no executor covered the band where the result fits Decimal128 while the intermediate sum does not.What changed.
pkg/sql/colexec/aggexec/sumavg2.go: the newavgDecimal128UsesWideSumStateroutes AVG (not SUM) over DECIMAL(29..34) to the existing genericsumAvgDecExec[types.Decimal128, types.Decimal256]; widths <= 28 keep the fast Decimal128 executor (overflow there needs roughly 17 billion rows in one group, the already-accepted bound of the fast path), and width >= 35 plus all SUM dispatches are unchanged.narrowAvgResultfinalization computes the exact mean at the declared result scale in the Decimal256 domain (decimal256AvgAtScaleSigned) and narrows it into the unchanged public Decimal128 result type through the existing representability checks. It is wired into the distinct and grouped flush paths (the grouped path emits into a fresh Decimal128 result vector and frees the wide intermediates) and the window/sliding consumers; zero-count groups stay NULL. There is no result-type, wire-format, or persisted-state change.Issue-to-test proof.
TestAvgDecimal128WideSumStateBoundary/TestAvgDecimal128WideSumStateDispatch(param decimal(34,0), result decimal128(38,4), value = 34 nines) fails on base code with the issue's exact errorDecimal128 Add overflow: 168959999999999999999999999999999983104+1189999999999999999999999999999999881at 17015 rows while the 17014-row control passes, and passes after the fix. The suite also covers grouped input (including NULL and all-NULL groups), negative inputs, partial-state merge across the reported add pair, opposite-half cancellation, DISTINCT across the boundary, window sliding, and the dispatch counterexamples (width 28 keeps the fast executor; widths 29/30/34 select the wide executor with the unchanged result type).SUM = 170149999999999999999999999999999982985) and every AVG returns9999999999999999999999999999999999.0000, including the previously failing 17015-rowSELECT AVG(d) FROM tand the promotedDECIMAL(35,0)control.test/distributed/cases/dtype/avg_decimal34_boundary.test/.resultcovers the boundary at user level (both the 17014-row and 17015-row predicates,AVG(-d),AVG(DISTINCT d), two 17015-row groups viaGROUP BY mod(id,2), the promotedDECIMAL(35,0)control, and a CTAS check that the public result staysdecimal(38,4)). The generated result was reviewed row-by-row against an independent arbitrary-precision oracle before comparison, and two consecutive mo-tester comparison passes were green (17/17 statements, 100%, empty error.txt).CI coverage follow-up (commit 6150dc4). The first CI run failed the
Matrixone Coverage execution / Coveragegate: modified-line coverage ofsumavg2.gowas 69.70% (23/33) against the 75% threshold, because the promoted branch ofavgDecimal128UsesWideSumStateand mostnarrowDecimal256Avgguard clauses had no direct tests. Five focused UTs were added covering exactly those lines: the promoted-result dispatch (widths 35..38 and the helper's promotion/width classification), the promoted boundary execution (17015 rows through the Decimal256-result path), all five invalid-result-type guards, the wide-domain scale/Div/precision overflow ladder, and a promoted DISTINCT success case. Local line-level coverage on the CI-analyzed modified lines is now 79.17% (57/72) with the package's full suite; the remaining lines are the mutually-exclusiveAppendFixed/allocation error returns insideFlush.Test commands.
.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=900s -run 'TestAvgDecimal128WideSumState(Boundary|Dispatch)' ./pkg/sql/colexec/aggexec/- base: exit 1 with the reported overflow (the 17014-row control passes); fixed: exit 0..agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=3600s ./pkg/sql/colexec/aggexec/- exit 0 (whole package).test/distributed/cases/dtype/avg_decimal34_boundary.testvia mo-tester (method=genrsto generate, then twomethod=runcomparison passes)..agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=900s -run 'TestAvgDecimal128WideSumStateDispatchPromotedResult|TestAvgDecimal128WideSumStateBoundaryPromotedResult|TestNarrowDecimal256AvgRejectsInvalidResultType|TestNarrowDecimal256AvgWideningAndPrecisionOverflow|TestAvgDecimal128WideSumStateDistinctErrorPropagates' ./pkg/sql/colexec/aggexec/- exit 0 (coverage follow-up UTs).gofmt -l <changed files>clean;GOWORK=off go vet -mod=readonly ./pkg/sql/colexec/aggexec(with the documented CGo include flags) clean;golangci-lint run -c .golangci.yml --new-from-rev <merge-base> ./pkg/sql/colexec/aggexecreports 0 issues.Residual risks.