Skip to content

[Bug]: Decimal64.Scale crosses the signed coefficient boundary without overflow error #29594

Closed

Description

@XuPeng-SH

Confirmed internal arithmetic counterexample

Found during #29249 test quality QA on main-based local 27ad358c6c (base f1137a6cac72852be2ab2e5078b684b98f23d96c). Production decimal code is unchanged.

The real types.Decimal64.Scale method produces:

Decimal64(1).Scale(18) // 1000000000000000000, nil: valid nearest control
Decimal64(1).Scale(19) // -8446744073709551616, nil: sign-wrapped result

The mathematical coefficient 10^19 exceeds the maximum signed Decimal64 coefficient 9223372036854775807. Positive scaling must retain the mathematical sign or report representability failure; returning a negative coefficient without an error violates the existing scalar overflow contract. Scale accepts an int32 delta and already has overflow paths/tests for deltas 18/38; this is an internal representability claim, not yet a demonstrated SQL query/assignment failure.

A private overlay test called the actual method, checked the exact scale-18 control, and expected typed InvalidInput at scale 19. One test selected, terminal assertion failure on the returned nil error. No skipped or empty run is credited. The formal production/test tree was not changed to embed this diagnostic.

Owner and mechanism

Decimal64.Scale multiplies by Pow10[19] through Decimal64.Mul64. Decimal64 is physically uint64 with signed interpretation through the high bit. Mul64 checks unsigned multiplication wrap (z/x != y), which does not reject the unsigned-representable but signed-unrepresentable coefficient 10^19. The final scaling result consequently crosses the sign bit without a failure.

The same magnitude primitive is consumed by signed arithmetic callers. A fix must first establish result-sign/magnitude ownership and preserve valid negative-minimum behavior; blanket high-bit rejection in a shared magnitude helper may break those callers. Check positive/negative controls, zero, signed boundaries, direct final multiplication and 19-digit chunk multiplication.

Required validation / limits

  • Close signed representability at the existing arithmetic owner, retain typed overflow diagnostics and checked fallback behavior.
  • Add minimal independent bit/value/error regression coverage at that owner; reuse existing Scale tests.
  • Trace actual public consumers, especially scale alignment and widened division/multiplication, before asserting SQL impact or choosing BVT.
  • No production patch, running-server BVT, benchmark result, or claim of attribution is included here.

Refs #29249. Independent from #29593's planner DECIMAL/DOUBLE comparison proof defects.

Activity

  1. added
    kind/bug
    on Oct 3, 2026
  2. self-assigned this
    on Oct 4, 2026
  3. added a commit that references this issue on Oct 4, 2026
    08ddb7e
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