Skip to content

Bug: lead/lag default value incorrect for DECIMAL type #24402

Closed

Description

@Ariznawlll

Bug Description

When using lead() or lag() window functions with a DECIMAL type column and an integer default value, the default value is not correctly scaled to match the DECIMAL precision. Instead of displaying the raw integer as a decimal value, it appears to be stored directly into the DECIMAL's raw internal storage without proper scaling.

Minimal Reproduction

select lead(cast(100.1234 as decimal(10,4)), 1, -1) over (order by a)
from (select 1 as a union all select 2 as a) t;

Expected Result

 100.1234
  -1.0000

Actual Result (MO)

 100.1234
  -0.0001

Analysis

DECIMAL(10,4) internally uses fixed-point integer storage (value * 10^4). The default value -1 should be stored as -10000 (displaying as -1.0000), but MO writes -1 directly into the raw storage, resulting in -1 / 10^4 = -0.0001.

Note: lag(col, 1, 0) with default 0 does not exhibit the issue since 0 is unaffected by scaling.

Environment

  • MO branch: 4.0-dev (nightly)
  • Discovered during big-data window function regression tests

Activity

  1. added
    kind/bug
    on May 15, 2026
  2. added theissue type on May 15, 2026
  3. self-assigned this
    on May 15, 2026
  4. added
    severity/s0
    on May 15, 2026
  5. daviszhen commented on May 18, 2026

    @daviszhen
    Contributor

    修改已经merge

  6. Ariznawlll commented on May 21, 2026

    @Ariznawlll
    CollaboratorAuthor

    Verified on latest main.

    Commit: 726bf23
    Server check: select git_version(); => 726bf23

    Test process:

    1. Built mo-service from this commit with make build.
    2. Started MO with ./mo-service -launch ./etc/launch/launch.toml.
    3. Connected through MySQL on 127.0.0.1:6001.

    Test SQL:

    select lead(cast(100.1234 as decimal(10,4)), 1, -1) over (order by a) as lead_decimal
    from (select 1 as a union all select 2 as a) t;
    
    select lag(cast(100.1234 as decimal(10,4)), 1, -1) over (order by a) as lag_decimal
    from (select 1 as a union all select 2 as a) t;

    Actual result:

    lead_decimal
    100.1234
    -1.0000
    
    lag_decimal
    -1.0000
    100.1234
    

    Result: PASS. The decimal default value is correctly scaled to -1.0000, not -0.0001.

  7. Ariznawlll commented on May 21, 2026

    @Ariznawlll
    CollaboratorAuthor

    3.0-dev verification update

    Verified on commit: 77e514a (origin/3.0-dev, git_version() = 77e514a)
    MO start method: built mo-service with Go 1.25.10, then started a clean local cluster via ./mo-service -launch /private/tmp/mo-launch-3dev/launch.toml, using isolated data-dir /private/tmp/mo-data-3dev.

    SQL case:

    • select lead(cast(100.1234 as decimal(10,4)), 1, -1) over (order by a) ...
    • select lag(cast(100.1234 as decimal(10,4)), 1, -1) over (order by a) ...

    Result:

    • lead output: 100.1234, -1.0000
    • lag output: -1.0000, 100.1234

    Conclusion: PASS on 3.0-dev. Decimal default value is preserved as -1.0000.

  8. Ariznawlll commented on May 21, 2026

    @Ariznawlll
    CollaboratorAuthor

    4.0-dev verification update

    Verified on commit: 3b24795 (origin/4.0-dev, git_version() = 3b24795)
    MO start method: built mo-service with Go 1.25.10, then started a clean local cluster via ./mo-service -launch /private/tmp/mo-launch-4dev/launch.toml, using isolated data-dir /private/tmp/mo-data-4dev-3b24795fa.

    SQL case:

    • select lead(cast(100.1234 as decimal(10,4)), 1, -1) over (order by a) ...
    • select lag(cast(100.1234 as decimal(10,4)), 1, -1) over (order by a) ...

    Result:

    • lead output: 100.1234, -1.0000
    • lag output: -1.0000, 100.1234

    Conclusion: PASS on 4.0-dev. Decimal default value is preserved as -1.0000.

  9. Ariznawlll commented on May 21, 2026

    @Ariznawlll
    CollaboratorAuthor

    Verified PASS on main, 3.0-dev, and 4.0-dev. Closing as fixed.

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions