Skip to content

[Bug]: unquoted DECIMAL256 literals lose fractional digits before evaluation #28765

Closed

Description

@Ariznawlll

Description

An unquoted decimal literal that requires the DECIMAL256 domain silently loses fractional digits before evaluation. The loss starts at 39 significant digits: a literal with the same spelling as a stored DECIMAL(39,1), DECIMAL(40,2), or DECIMAL(65,30) value does not compare equal to it.

This is distinct from #26897. That issue fixed exact coercion for text string constants paired with DECIMAL. Here the failing operand is an unquoted numeric literal; the text-literal and explicit-DECIMAL controls are correct on the same revision.

Environment

  • Branch: main
  • Commit: 4f9d170a22deaabc2eee6b239a51a8cb40a39f9f
  • Deployment: local one Log service, one TN service, two CN services, launched with etc/launch-multi-cn/launch.toml; direct MySQL-wire connection to CN on port 16001.

Steps to reproduce

CREATE DATABASE decimal256_unquoted_literal;
USE decimal256_unquoted_literal;

CREATE TABLE t (id INT PRIMARY KEY, v DECIMAL(65,30));
INSERT INTO t VALUES
  (1, 12345678901234567890123456789012345.123456789012345678901234567890);

SELECT 12345678901234567890123456789012345.123456789012345678901234567890;

SELECT id FROM t
WHERE v = 12345678901234567890123456789012345.123456789012345678901234567890;

SELECT id FROM t
WHERE v = CAST(
  '12345678901234567890123456789012345.123456789012345678901234567890'
  AS DECIMAL(65,30)
);

Actual behavior

The scalar literal is returned as:

12345678901234567890123456789012345.123

The equality predicate returns no rows. EXPLAIN records the already-truncated literal:

Filter Cond: (t.v = cast(12345678901234567890123456789012345.123 AS DECIMAL256))

The same outcome occurs with exact direct literals at DECIMAL(39,1), DECIMAL(40,2), and DECIMAL(65,30).

Expected behavior

The unquoted literal must preserve its complete numeric value. It has the same value as the inserted DECIMAL(65,30) value, so the equality predicate must return id = 1. Direct scalar evaluation must retain all 30 fractional digits.

Stability and controls

  • The full boundary matrix reproduced identically in 3/3 independent databases on the stated revision.
  • DECIMAL(38,18) is a passing control: its same-spelling unquoted literal returns id = 1.
  • At DECIMAL(39,1), DECIMAL(40,2), and DECIMAL(65,30), the same-spelling unquoted literal returns no row while the explicit CAST(... AS DECIMAL(M,D)) control returns id = 1.
  • A quoted literal containing the same 65-digit decimal spelling also returns id = 1 for the DECIMAL(65,30) table.
  • The statements are read-only after fixture insertion; rows remained unchanged after every repetition.

Evidence

The saved local matrix is evidence/types/decimal256_numeric_literal_boundary.sql; its three current-main outputs are decimal256_numeric_literal_boundary_main_4f9d170_run7.out through run9.out. After replacing only the isolated database names, all outputs are byte-identical.

Code analysis

The failing public path enters baseBinder.bindNumVal in pkg/sql/plan/base_binder.go for tree.P_decimal, then selects makePlan2DecimalExprWithType in pkg/sql/plan/make.go once types.Parse128 cannot represent the value. makePlan2DecimalExprWithType constructs a DECIMAL256 cast from the literal's original string, while direct Parse256 and explicit-cast controls retain the full value. The exact point at which this bind/execution path reduces the effective scale is not yet confirmed; the EXPLAIN output proves the reduction happens before the predicate is executed.

Regression coverage

Add a deterministic decimal planner/executor regression after the fix. It should assert scalar value and equality row set for 38-digit DECIMAL128 and 39-, 40-, and 65-digit DECIMAL256 unquoted literals, with explicit-CAST and text-literal controls.

Related

Activity

  1. added this to the MO-202609 milestone on Sep 12, 2026
  2. added
    kind/bug
    on Sep 12, 2026
  3. added theissue type on Sep 12, 2026
  4. Ariznawlll commented on Sep 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    Additional scope on the same main@4f9d170a22deaabc2eee6b239a51a8cb40a39f9f two-CN fixture:

    The truncation happens before expression evaluation, so it affects more than =. With one stored DECIMAL(65,30) value equal to the complete literal, each result below was identical in 3/3 executions:

    Expression Result
    v = bare_literal no row
    v < bare_literal no row
    v > bare_literal id=1
    v IN (bare_literal) no row
    v BETWEEN bare_literal AND bare_literal no row
    CAST(bare_literal AS CHAR) value ending in .123
    bare_literal + 0 value ending in .123
    CAST('full value' AS DECIMAL(65,30)) + 0 full 30-digit fractional value

    This confirms an operand-value loss before comparison, range, list, formatting, or arithmetic operators run. The explicit DECIMAL control remains exact; the affected surface is the bare numeric literal path that promotes beyond DECIMAL128.

  5. Ariznawlll commented on Sep 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    DDL expression scope on the same main@4f9d170a22deaabc2eee6b239a51a8cb40a39f9f two-CN baseline:

    DEFAULT and CHECK follow different binding paths.

    CREATE TABLE dflt(
      id INT PRIMARY KEY,
      v DECIMAL(65,30) DEFAULT <65-digit bare literal>
    );
    INSERT INTO dflt(id) VALUES (1);

    The default stores the complete value and SHOW CREATE TABLE preserves the full spelling. Its column target supplies the required Decimal256 type.

    In contrast, this valid row is rejected in 3/3 runs:

    CREATE TABLE check_bare(
      id INT PRIMARY KEY,
      v DECIMAL(65,30),
      CHECK(v = <the same 65-digit bare literal>)
    );
    INSERT INTO check_bare VALUES
      (1, CAST('<the complete value>' AS DECIMAL(65,30)));
    -- ERROR 3819: Check constraint is violated

    Replacing only the CHECK constant with CAST('<complete value>' AS DECIMAL(65,30)) accepts the row in all three runs. This extends the defect from read expressions to constraint enforcement: a correct DML row can be rejected because the untyped CHECK constant is truncated before the predicate runs. The default path is a passing control, not affected behavior.

  6. Ariznawlll commented on Sep 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    Generated-column scope on the same baseline:

    CREATE TABLE gen_bare(
      id INT PRIMARY KEY,
      v DECIMAL(65,30),
      g DECIMAL(65,30) GENERATED ALWAYS AS (v + <65-digit bare literal>) STORED
    );
    INSERT INTO gen_bare(id,v) VALUES (1, CAST('0.0' AS DECIMAL(65,30)));

    In 3/3 runs, g was stored as the truncated value ending in .123000000000000000000000000000. Replacing only the generated-expression constant with an explicit CAST('<full value>' AS DECIMAL(65,30)) stores all 30 fractional digits.

    SHOW CREATE TABLE prints the complete original bare-literal spelling in both cases. Therefore catalog DDL text is preserved, but the generated-expression execution path materializes the bare Decimal256 literal with the same fractional truncation. This is a persisted-data manifestation of the issue, distinct from the passing typed DEFAULT path noted earlier.

  7. Ariznawlll commented on Sep 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    Derived-object and write-path scope on the same baseline:

    Path Observed value / metadata, stable in 3/3
    CREATE VIEW v AS SELECT <bare literal> view evaluates to the value ending in .123 although SHOW CREATE VIEW preserves the full source spelling
    CREATE TABLE ct AS SELECT <bare literal> stored value ends in .123; CTAS column metadata is DECIMAL(65,3)
    INSERT INTO typed_target DECIMAL(65,30) SELECT <bare literal> stores .123000000000000000000000000000

    The last result is important: unlike a typed DEFAULT, an INSERT … SELECT target does not give the source literal an exact Decimal256 value before projection. It is truncated in the query expression, then converted to the target's scale. Consequently a valid source spelling can permanently lose fractional data through CTAS, View-backed queries, and INSERT … SELECT even when the eventual destination column is DECIMAL(65,30).

  8. self-assigned this
    on Sep 12, 2026
  9. added a commit that references this issue on Sep 20, 2026
    bc523de
  10. Ariznawlll commented on Sep 21, 2026

    @Ariznawlll
    CollaboratorAuthor

    Verified on current main 57c280e.\n\nThe unquoted 65-digit fractional decimal literal preserves its full spelling and value through scalar evaluation and arithmetic; no fractional digits are lost. The check was repeated three times.\n\nMOTR coverage was added in matrixorigin/motr#193:\nsuites/14_issue_regression/t/issue_28765_28694_28692_28222_compatibility.test.\n\nClosing 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/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions