Skip to content

fix: widen decimal SUM accumulation - #28624

Merged
XuPeng-SH merged 37 commits into
matrixorigin:mainfrom
daviszhen:codex/sum-decimal256-widen
Sep 15, 2026
Merged

XuPeng-SH merged 37 commits into
matrixorigin:mainfrom
daviszhen:codex/sum-decimal256-widen

Conversation

@daviszhen

@daviszhen daviszhen commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #28585

What this PR does / why we need it:

  • Derives DECIMAL SUM precision with aggregate headroom instead of pinning Decimal128 inputs to precision 38.
  • Selects a Decimal256 accumulator when the derived SUM result exceeds Decimal128.
  • Keeps AVG physical state aligned with its existing executor.
  • Versions the widened DECIMAL SUM partial-state layout with MORPC v73; main owns v69-v72 for bounded RANK, CONV bases, checked integer arithmetic, and corrected IP semantics.
  • Uses the pre-v73 [Decimal128 sum, int64 count] layout on both remote partial and coordinator MergeGroup while a cluster has mixed protocol versions; a new coordinator converts the compatible legacy state to its planned Decimal256 result.
  • Covers old-partial/new-merge and new-partial/old-merge wire compatibility for wide Decimal64 and Decimal128, along with overflow, cancellation/order independence, DISTINCT, and return-type behavior.
  • Merges the latest main.

Testing

  • go test ./pkg/sql/colexec/aggexec ./pkg/sql/colexec/group ./pkg/sql/colexec/window ./pkg/sql/colexec/partition ./pkg/sql/colexec/hashjoin ./pkg/sql/compile ./pkg/sql/plan ./pkg/sql/plan/function ./pkg/sql/plan/substrait ./pkg/vm/process ./pkg/fulltext2 -count=1
  • make build

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APPROVE at d0f3347 against main e2580f9. Both previously reported rolling-upgrade blockers are closed; no new concrete blocker found.

The final-result fix is on the worker, not merely on the upgraded coordinator: decodeScope installs RemoteRunContext on both process contexts and restores the serialized NeedEval flag. Group.Prepare now selects legacyDecimalSumResult for remote final Groups below v65. The wide Decimal64/Decimal128 legacy constructors then retain their physical Decimal128 return type and do not enable widening. This closes the old-coordinator -> new-worker value-1 counterexample even though that coordinator never runs the new shuffle gate. A new coordinator still disables affected final shuffle below v65 and its local MergeGroup explicitly keeps legacyDecimalSumResult=false, widening the compatible partial accumulator to its own planned Decimal256 result. At v65 the widened representation is enabled normally.

Revalidated the bidirectional pre-v65 [Decimal128 sum, int64 count] state selection, constructor flag propagation, ordinary/DISTINCT/null Flush paths and allocation-error ownership, precision headroom/cap, AVG state separation, window behavior, metadata changes and Substrait eligibility fallback. Mixed-version operation intentionally retains old accumulator range until the common protocol advances. Widening has bounded per-group state/output cost; the 12 TPC-H Substrait exclusions are an explicit offload/performance tradeoff, not silently successful exports.

Evidence: complete current 19-file change map and review/comment history, direct tracing of remote decode -> Prepare -> legacy constructor -> Flush, and inspection of TestRemoteFinalDecimalSumPreservesOldCoordinatorResult (both input widths, v64/v65, actual Group execution/value/type), bidirectional state tests and compiler gate controls. The six core SUM/Group files are byte-identical to those independently reviewed earlier in the integrated #28540 head c824051, so that analysis remains valid. Reused the author's reported owning-package, focused race and build results; I did not rerun native suites, deploy a mixed-CN cluster, benchmark, or wait for CI. Live head/base were rechecked unchanged before submission.

@aptend aptend left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed the complete diff, all reviews, inline replies and resolved thread state, and the delta since my prior review at bfe9669 against exact head 5b644f8. Both rolling-upgrade blockers are closed. Below the current v67 gate, partial Group and coordinator MergeGroup select the same legacy two-vector Decimal128 state; a remotely decoded final Group also preserves the old coordinator Decimal128 result, while an upgraded local finalizer converts the compatible accumulator to its planned Decimal256 output. At v67 the widened one-vector state and result are enabled together. I also traced ordinary, DISTINCT, null, window, spill/rebuild and allocation-error paths, the precision cap, shuffle fallback, and the explicit Substrait eligibility tradeoff; no new blocking issue found. Exact-head required CI, Ubuntu UT, SCA, coverage and exercised BVT jobs are terminal-success. Static diff and formatting checks are clean; I reused exact-head CI and the prior focused compatibility/race evidence rather than rebuilding native dependencies locally.

…6-widen

# Conflicts:
#	pkg/defines/const.go
#	pkg/sql/colexec/group/exec2.go
#	pkg/sql/colexec/group/mergeGroup.go
#	pkg/sql/colexec/group/types2.go
@daviszhen

Copy link
Copy Markdown
Contributor Author

Merged current main at 6987fda and resolved the MORPC collision by retaining v67 for named process timezone identity and assigning v68 to widened Decimal SUM state. Group and MergeGroup preserve both timezone and Decimal compatibility state. Targeted tests and make build pass on head 8004426.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Resolved the latest main conflict in eaed65e by merging upstream/main at 07fdd4a.

Main now owns MORPC v68 for complete strict-write GROUP_CONCAT cut reporting, so widened DECIMAL SUM moves to v69. I updated both producer/coordinator gates and the old/new wire-compatibility tests to preserve the pre-v69 layout during mixed-version operation.

Local validation passed for aggexec, group, window, compile, substrait, and process, followed by make build. Fresh CI is expected from this push.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Resolved the latest main conflict in cf0d541 by merging upstream/main at f9a4444.

Main owns MORPC v69 for exact bounded per-partition RANK with boundary ties, so widened DECIMAL SUM now uses v70. I updated the protocol constant, remote producer/coordinator gates, legacy result handling, and old/new wire-compatibility tests; the main RANK v69 path remains unchanged.

Local validation passed for aggexec, group, window, partition, hashjoin, compile, plan, plan/function, substrait, process, and fulltext2, followed by make build. Fresh CI is expected from this push.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Resolved the latest main conflict at dd52272 after preserving the remote branch updates and merging upstream/main through 2ea4ec2.

Main now owns MORPC v69-v72. Widened DECIMAL SUM therefore moves to v73, with the producer, coordinator, legacy result handling, and old/new wire-compatibility tests updated together.

Local aggregate/group/window/compile/plan/substrait/process regression tests passed, followed by the complete native-provenance-aware make build.

@aunjgr aunjgr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed exact head dd52272 against base 2ea4ec2. Our prior unversioned SUM partial-state blocker is closed.

The v73 boundary now selects the legacy [Decimal128 sum, int64 count] state for both partial Group and coordinator MergeGroup below the boundary. An upgraded local finalizer widens that compatible accumulator to its planned Decimal256 output; a remotely decoded final Group instead preserves an old coordinator's Decimal128 result contract. I traced RemoteRunContext/NeedEval restoration, constructor flags, ordinary/DISTINCT Flush, partial-result cleanup, and the shuffle gate. The bidirectional value-1 tests and remote-final Group tests cover both Decimal64 and Decimal128 inputs.

Same-version widened accumulation and precision headroom remain intact; mixed-version mode intentionally retains the old accumulator range. Main currently ends at v72, so v73 is a fresh allocation. #28540 contains the same core SUM implementation and must follow this PR.

No remaining confirmed blocker. Reused green current-head CI, earlier source review, and revision-scoped independent compatibility evidence; no new native build, SQL server or mixed-CN deployment run. The twelve TPC-H Substrait exclusions remain an explicit offload tradeoff, not a claim of all-22 offload compatibility.

@mergify

mergify Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-15 09:01 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ❌ Checks failed · in-place
  • 🚫 Left the queue — 2026-09-15 10:35 UTC · at 0e0eb88dec7e9bdd5ddb6ffce489d7f1631eb043

This pull request spent 1 hour 33 minutes 52 seconds in the queue, with no time running CI.

Waiting for any of
  • check-neutral = CI Required
  • check-skipped = CI Required
  • check-success = CI Required
All conditions

Reason

The merge conditions cannot be satisfied due to failing checks

  • Mergify Merge Protections
  • CI Required

Hint

You may have to fix your CI before adding the pull request to the queue again.
If you update this pull request, to fix the CI, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Requeued — the merge queue status continues in this comment ↓.

@daviszhen

daviszhen commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Follow-up on the Ubuntu/x86 UT failure: run attempt 2 produced no failed test assertion. make ut hit its global timeout after 72m16s (Error 124) while Test_CheckpointChaos2 had been active for only about 9 seconds; all preceding reported packages/tests passed and the cgroup diagnostics recorded no OOM kill. A contemporaneous main-based control run completed the same unsharded race suite in 51m39s, so this was runner/cache throughput rather than a product-code failure.

The PR was behind main. I merged current upstream/main without conflicts and pushed exact head 200b654691, which starts a clean CI run:

@mergify

mergify Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-09-15 14:15 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-09-15 15:05 UTC · at fd313b92909ca27360bf19360e2f4a0557254040

This pull request spent 50 minutes in the queue, with no time running CI.

Waiting for any of
  • check-neutral = CI Required
  • check-skipped = CI Required
  • check-success = CI Required
All conditions

Reason

Pull request #28624 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.

The author needs to enable "Allow edits from maintainers" on this pull request.

Hint

You 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.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: SUM does not widen DECIMAL(38) accumulation beyond Decimal128

5 participants