Skip to content

[Bug]: Decimal256 multiplication panics when scale reduction exceeds 38 digits #28876

Closed

Description

@XuPeng-SH

Summary

d256Mul panics for valid Decimal256 operands when the scale reduction is more
than 38 digits. The multiplication fast tiers call scalePow10Factors, which
indexes types.Pow10 past its 20-entry range.

Reproduction

On origin/main at f9a44447743fff827dab01300837d27a3d9811f0:

left := []types.Decimal256{{B0_63: 1}}
right := []types.Decimal256{{B0_63: 1}}
result := make([]types.Decimal256, 1)
_ = d256Mul(left, right, result, 76, 76, nulls.NewWithSize(1))

The call panics with:

runtime error: index out of range [57] with length 20
github.com/matrixorigin/matrixone/pkg/sql/plan/function.scalePow10Factors

DECIMAL(76,76) is a valid Decimal256 type, and 1 * 1 should produce a
zero/one value after the target-scale reduction. A user query can therefore
crash the process instead of returning a result or a controlled overflow.

Root cause

scalePow10Factors(n) supports only the two-limb representation for
n <= 38, but the Decimal256 multiplication path can receive scale-down
values up to 76. The int32/int64 fast tiers do not guard that range before
indexing types.Pow10[n-19].

Expected fix

Use a bounded generic scale-down path for n > 38 (or reject it with a normal
arithmetic error), without indexing types.Pow10 out of bounds. Preserve the
existing int32/int64 fast paths for their supported range and add tests for
all vector/broadcast layouts, NULL rows, scale 38/39/57/76, and overflow/error
semantics.

Related: #28875 (the raw-overflow Decimal256 multiplication fallback).

Activity

  1. self-assigned this
    on Sep 14, 2026
  2. added
    kind/bug
    on Sep 14, 2026
  3. XuPeng-SH commented on Sep 14, 2026

    @XuPeng-SH
    ContributorAuthor

    Fix queued in PR #28878: #28878. The unified high-scale dispatch prevents scalePow10Factors out-of-range access while preserving the <=38 fast paths.

  4. added a commit that references this issue on Sep 14, 2026
    3e89220
  5. Ariznawlll commented on Sep 15, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • MatrixOne main:0c3a04f390adaf6281fd592a49778ea5b1155e67
    • 本地 standalone(1 CN / 1 TN / 1 LogService),2026-09-15。

    原问题验证

    复现高 scale Decimal256 乘法的缩 scale 分支;不再 panic,结果可正常返回。

    Happy Path 覆盖

    覆盖 raw product 经缩 scale 后可表示、边界 scale 以及小值高 scale 乘法。

    Unhappy Path 覆盖

    覆盖超过 Decimal256 可表示范围的输入,确认返回受控溢出错误而非 panic。

    白盒验证

    pkg/sql/plan/function 中 TestD256MulScaledRawOverflow、TestD256MulScaledRawOverflowPreservesOverflow、TestD256MulScaledFallbackChunkBoundaries、TestD256MulHighScaleSmallValues 连续 3 次通过。

    回归测试

    该问题属于内部 Decimal256 宽算术内核;公开 SQL 不提供可用于 MOTR 的 DECIMAL256 声明,故以已有 MatrixOne 定向 UT 作为回归门禁,不强行加入 MOTR。

    测试证据

    定向 Go UT:3/3 通过;测试命令为 go test -vet=off ./pkg/sql/plan/function -count=3 -run '^(TestD256MulScaledRawOverflow|TestD256MulScaledRawOverflowPreservesOverflow|TestD256MulScaledFallbackChunkBoundaries|TestD256MulHighScaleSmallValues)$'。

    测试结论

    测试通过。

  6. added 3 commits that reference this issue on Sep 17, 2026
    9e8907e
    1779c03
    f5f7146
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions