Skip to content

fix(decimal): handle Decimal256 wide multiplication scale reduction - #28878

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/issue-28875-d256-mul-scale
Sep 14, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/issue-28875-d256-mul-scale

Conversation

@XuPeng-SH

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 #28875
Fixes #28876

What this PR does / why we need it:

d256Mul previously rejected a raw 256-bit product before applying the
target-scale reduction. High-scale values can have a wider unscaled product
while their rounded result is representable; this caused the deterministic
Decimal256 failures exposed by #28825. The same dispatch also indexed the
two-limb scalePow10Factors helper out of bounds for valid scale reductions
greater than 38 (for example DECIMAL(76,76)), which could panic the process.

This change keeps the existing int32/int64 and ordinary 256-bit paths for the
supported scale range. Raw-overflow recovery and reductions above 38 use an
allocation-free 8-limb (512-bit) product, complete discarded-remainder
accumulation, one round-half-up, and a final signed Decimal256 range check.
Failure returns the existing controlled Decimal256 overflow error and does not
overwrite the destination. Mul256's raw integer contract is unchanged.

Test plan

  • ./.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -run '^TestD256Mul(ScaledRawOverflow|ScaledFallbackChunkBoundaries|HighScaleSmallValues|WideMatchesBigInt)' ./pkg/sql/plan/function
  • Same focused matrix with -race.
  • Full ./.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=220s ./pkg/sql/plan/function.
  • Independent math/big oracle covers positive/negative values, below/exact/above-half and carry-to-overflow, 38/39/57/76 scale chunks, raw-overflow/final-fit and final-overflow, vector/vector and broadcast layouts, partial/all NULL rows, and 256 random 512-bit products.
  • go vet ./pkg/sql/plan/function with the repository CGo include/link flags, golangci-lint run ./pkg/sql/plan/function, license-eye header check, git diff --check.
  • Existing benchmark medians for the ≤38 fast/scaled paths remained within run-to-run noise on the same host; the new wide path is only selected for raw-overflow recovery or scale reductions above 38.

Validation was run after rebasing onto origin/main 60248a0895. CI was not awaited per the task instruction.

@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 force-pushed the codex/issue-28875-d256-mul-scale branch from 5579166 to beb53b3 Compare September 14, 2026 13:57
@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Rebased the base-defect fix (Decimal256 wide multiplication scale reduction, including the related high-scale guard) onto latest main at c61ed374a8; new head beb53b3136fac496bb4098401c0f6c72a2cd1446. Existing focused/full/race evidence was recorded before this base-only rebase; this worktree lacks source-matched native CGo artifacts for a redundant rerun. Only the personal feature branch was force-with-lease updated; CI intentionally not awaited.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Follow-up after GPT-6 medium review:

  • Enforced the logical DECIMAL(65, scale) result range in the wide fallback, including the asymmetric signed boundary (-10^65 allowed, +10^65 rejected).
  • Added an execution-level SQL/frontend regression for DECIMAL(65,65) multiplication, asserting successful evaluation to non-NULL Decimal256 zero and preserving (65,65) metadata.
  • Validation: native CGo-linked go test -mod=readonly ./pkg/sql/plan/function -count=1 (12.8s), ./pkg/sql/plan -count=1 (4.7s), go vet, gofmt, and git diff --check all passed.
  • GPT-6 medium final review: PASS, no P0-P2 findings.

Pushed as 8125376.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

GPT-6 medium deep review at exact head 8125376.

I reviewed the Decimal256 scale-reduction path and its logical-range guard; I found no verified correctness blocker on this head. The exact-head CI is green. The available artifact does not list individual timings for the new tests, so I cannot certify their per-test time against the 45-second limit.

@XuPeng-SH
XuPeng-SH merged commit 3e89220 into matrixorigin:main Sep 14, 2026
20 checks passed
@XuPeng-SH
XuPeng-SH deleted the codex/issue-28875-d256-mul-scale branch September 14, 2026 18:12
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…atrixorigin#28878)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes matrixorigin#28875
Fixes matrixorigin#28876

## What this PR does / why we need it:

`d256Mul` previously rejected a raw 256-bit product before applying the
target-scale reduction. High-scale values can have a wider unscaled
product
while their rounded result is representable; this caused the
deterministic
Decimal256 failures exposed by matrixorigin#28825. The same dispatch also indexed
the
two-limb `scalePow10Factors` helper out of bounds for valid scale
reductions
greater than 38 (for example `DECIMAL(76,76)`), which could panic the
process.

This change keeps the existing int32/int64 and ordinary 256-bit paths
for the
supported scale range. Raw-overflow recovery and reductions above 38 use
an
allocation-free 8-limb (512-bit) product, complete discarded-remainder
accumulation, one round-half-up, and a final signed Decimal256 range
check.
Failure returns the existing controlled Decimal256 overflow error and
does not
overwrite the destination. `Mul256`'s raw integer contract is unchanged.

## Test plan

- `./.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -run
'^TestD256Mul(ScaledRawOverflow|ScaledFallbackChunkBoundaries|HighScaleSmallValues|WideMatchesBigInt)'
./pkg/sql/plan/function`
- Same focused matrix with `-race`.
- Full `./.agents/skills/mo-dev/scripts/mo-cgo-test -count=1
-timeout=220s ./pkg/sql/plan/function`.
- Independent `math/big` oracle covers positive/negative values,
below/exact/above-half and carry-to-overflow, 38/39/57/76 scale chunks,
raw-overflow/final-fit and final-overflow, vector/vector and broadcast
layouts, partial/all NULL rows, and 256 random 512-bit products.
- `go vet ./pkg/sql/plan/function` with the repository CGo include/link
flags, `golangci-lint run ./pkg/sql/plan/function`, `license-eye header
check`, `git diff --check`.
- Existing benchmark medians for the ≤38 fast/scaled paths remained
within run-to-run noise on the same host; the new wide path is only
selected for raw-overflow recovery or scale reductions above 38.

Validation was run after rebasing onto `origin/main` `60248a0895`. CI
was not awaited per the task instruction.
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…atrixorigin#28878)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes matrixorigin#28875
Fixes matrixorigin#28876

## What this PR does / why we need it:

`d256Mul` previously rejected a raw 256-bit product before applying the
target-scale reduction. High-scale values can have a wider unscaled
product
while their rounded result is representable; this caused the
deterministic
Decimal256 failures exposed by matrixorigin#28825. The same dispatch also indexed
the
two-limb `scalePow10Factors` helper out of bounds for valid scale
reductions
greater than 38 (for example `DECIMAL(76,76)`), which could panic the
process.

This change keeps the existing int32/int64 and ordinary 256-bit paths
for the
supported scale range. Raw-overflow recovery and reductions above 38 use
an
allocation-free 8-limb (512-bit) product, complete discarded-remainder
accumulation, one round-half-up, and a final signed Decimal256 range
check.
Failure returns the existing controlled Decimal256 overflow error and
does not
overwrite the destination. `Mul256`'s raw integer contract is unchanged.

## Test plan

- `./.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -run
'^TestD256Mul(ScaledRawOverflow|ScaledFallbackChunkBoundaries|HighScaleSmallValues|WideMatchesBigInt)'
./pkg/sql/plan/function`
- Same focused matrix with `-race`.
- Full `./.agents/skills/mo-dev/scripts/mo-cgo-test -count=1
-timeout=220s ./pkg/sql/plan/function`.
- Independent `math/big` oracle covers positive/negative values,
below/exact/above-half and carry-to-overflow, 38/39/57/76 scale chunks,
raw-overflow/final-fit and final-overflow, vector/vector and broadcast
layouts, partial/all NULL rows, and 256 random 512-bit products.
- `go vet ./pkg/sql/plan/function` with the repository CGo include/link
flags, `golangci-lint run ./pkg/sql/plan/function`, `license-eye header
check`, `git diff --check`.
- Existing benchmark medians for the ≤38 fast/scaled paths remained
within run-to-run noise on the same host; the new wide path is only
selected for raw-overflow recovery or scale reductions above 38.

Validation was run after rebasing onto `origin/main` `60248a0895`. CI
was not awaited per the task instruction.
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…atrixorigin#28878)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes matrixorigin#28875
Fixes matrixorigin#28876

## What this PR does / why we need it:

`d256Mul` previously rejected a raw 256-bit product before applying the
target-scale reduction. High-scale values can have a wider unscaled
product
while their rounded result is representable; this caused the
deterministic
Decimal256 failures exposed by matrixorigin#28825. The same dispatch also indexed
the
two-limb `scalePow10Factors` helper out of bounds for valid scale
reductions
greater than 38 (for example `DECIMAL(76,76)`), which could panic the
process.

This change keeps the existing int32/int64 and ordinary 256-bit paths
for the
supported scale range. Raw-overflow recovery and reductions above 38 use
an
allocation-free 8-limb (512-bit) product, complete discarded-remainder
accumulation, one round-half-up, and a final signed Decimal256 range
check.
Failure returns the existing controlled Decimal256 overflow error and
does not
overwrite the destination. `Mul256`'s raw integer contract is unchanged.

## Test plan

- `./.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -run
'^TestD256Mul(ScaledRawOverflow|ScaledFallbackChunkBoundaries|HighScaleSmallValues|WideMatchesBigInt)'
./pkg/sql/plan/function`
- Same focused matrix with `-race`.
- Full `./.agents/skills/mo-dev/scripts/mo-cgo-test -count=1
-timeout=220s ./pkg/sql/plan/function`.
- Independent `math/big` oracle covers positive/negative values,
below/exact/above-half and carry-to-overflow, 38/39/57/76 scale chunks,
raw-overflow/final-fit and final-overflow, vector/vector and broadcast
layouts, partial/all NULL rows, and 256 random 512-bit products.
- `go vet ./pkg/sql/plan/function` with the repository CGo include/link
flags, `golangci-lint run ./pkg/sql/plan/function`, `license-eye header
check`, `git diff --check`.
- Existing benchmark medians for the ≤38 fast/scaled paths remained
within run-to-run noise on the same host; the new wide path is only
selected for raw-overflow recovery or scale reductions above 38.

Validation was run after rebasing onto `origin/main` `60248a0895`. CI
was not awaited per the task instruction.

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

2 participants