Skip to content

[Bug]: recursive CTE bypasses DECIMAL precision and stores out-of-range values #29137

Closed

Description

@Ariznawlll

Problem

A recursive CTE whose anchor is DECIMAL(5,2) can emit 1000.00, which is outside that type's valid range. The oversized value can then be stored in a declared DECIMAL(5,2) column, bypassing the range check that rejects the same value on a direct INSERT.

CREATE TABLE AS makes the invariant violation explicit: SHOW CREATE TABLE reports DECIMAL(5,2), while the table contains 1000.00.

Environment

  • MatrixOne commit: d8ddce92b1c5c172111b50aefe6b6b200b2589cb
  • Topology: local 1 LogService / 1 TN / 2 CN
  • CN SQL endpoints: 16001, 16002
  • sql_mode='STRICT_TRANS_TABLES'

Reproduction

create database cte_recursive_decimal_overflow;
use cte_recursive_decimal_overflow;
set session sql_mode='STRICT_TRANS_TABLES';

create table target(n decimal(5,2));

-- Control: correctly rejected as outside DECIMAL(5,2).
insert into target values(1000.00);

-- The same out-of-range value is accepted through a recursive CTE.
insert into target
with recursive r(n) as (
  select cast(999.99 as decimal(5,2))
  union all
  select n+0.01 from r where n<1000
)
select n from r;

select n from target order by n;

create table derived as
with recursive r(n) as (
  select cast(999.99 as decimal(5,2))
  union all
  select n+0.01 from r where n<1000
)
select n from r;

show create table derived;
select n from derived order by n;

Current behavior

The direct insert returns the expected range error:

1000.00 beyond the range, can't be converted to Decimal64(5,2)

Both recursive statements succeed:

target:  999.99, 1000.00
derived: 999.99, 1000.00

SHOW CREATE TABLE derived reports:

CREATE TABLE `derived` (
  `n` decimal(5,2) DEFAULT NULL
)

The issue was reproduced on both CNs in three independently created databases (6/6).

Expected behavior

The recursive member must be coerced to the anchor's DECIMAL(5,2) type. In strict mode, producing 1000.00 must return a range error and must not insert any partial rows or create a table containing a value outside its declared precision.

MySQL also fixes recursive CTE result-column types from the nonrecursive anchor:

https://dev.mysql.com/doc/refman/8.0/en/with.html

Code-level observation

bindRecursiveCTE intends to coerce every recursive member to the initial projection type (query_builder.go, around the makePlan2CastExpr call). That coercion is not enforcing the precision/range at execution for this numeric path, although the analogous recursive string-width assignment path has dedicated strict/non-strict handling.

Suggested regression coverage

Add BVT and executor UT coverage for recursive assignment to DECIMAL64 and DECIMAL128 anchors at positive and negative precision boundaries, in strict and non-strict modes. Cover plain SELECT, INSERT, CTAS, PreparedStatement reuse, multi-CN execution, and assert statement atomicity on overflow.

Activity

  1. added this to the MO-202609 milestone on Sep 20, 2026
  2. added
    kind/bug
    on Sep 20, 2026
  3. added theissue type on Sep 20, 2026
  4. added
    severity/s0
    and removed on Sep 24, 2026
  5. assigned and unassigned on Sep 24, 2026
  6. slyunnn commented on Sep 30, 2026

    @slyunnn

    Regression and extended verification for #29137 (2026-09-30)

    Verification Environment

    • Verified on MatrixOne origin/main commit 76862df5c89e8868bed5963c428f65b9eafb34b3 (Optimize leading ranges on composite sort keys (#29513)).
    • The running mo-service binary was built from that exact worktree. Its SHA-256 was 706b0f017fffc7a5d0e49f285dad22a5390885c678a88b169f6ca631384bb82b, matching the binary mounted in CN1.
    • Topology: 2 CN / 1 TN / 1 Log service / Proxy. The isolated MySQL endpoints were Proxy 6611 (service port 6009), CN1 16611 (service port 16001), and CN2 16612 (service port 16002). All three were reachable and completed health checks after the regression cases.
    • Client protocol: MySQL text protocol via mysql CLI. The three endpoint matrices ran in parallel with independent databases; no shared objects, session variables, fixed ports, or execution ordering were shared by the test cases.
    • Related fix: 3282f1c1e71d037a86710b8e79e387a7c800698e, fix: enforce recursive CTE decimal precision (#29280). The fix is an ancestor of the verified main commit.

    What Was Covered

    • OK The original strict-mode recursive DECIMAL(5,2) SELECT rejected 1000.00 with the expected range error through Proxy, CN1, and CN2.
    • OK Direct INSERT of 1000.00 and the original recursive INSERT were both rejected on all three endpoints; the target table remained at 0 rows, proving statement atomicity.
    • OK Failed recursive CTAS was rejected on all three endpoints and left no derived table in mo_catalog.
    • OK Negative DECIMAL(5,2) overflow (-1000.00) and DECIMAL(19,2) / DECIMAL128 positive overflow were rejected through all three endpoints.
    • OK Scale-growth overflow from DECIMAL(4,2) to the recursive anchor DECIMAL(5,4) was rejected through all three endpoints.
    • OK Happy-path boundary CTEs returned 999.98,999.99 and 0.0000,9.9900 through all three endpoints.
    • OK SQL-level PREPARE and EXECUTE of the original recursive CTE returned the expected range error on all three endpoints; a subsequent SELECT 1 succeeded.
    • OK With empty sql_mode, a direct two-row DML control and the equivalent recursive CTE both rejected the out-of-range value and left their tables empty on all three endpoints.
    • OK Focused unit tests passed with terminal status 0: TestParseDecimalRejectsBothPrecisionEndpoints, DECIMAL cast precision/scale tests, and TestRecursiveCteDecimalAnchorKeepsCheckedCast.
    • Not covered Binary COM_STMT_PREPARE / COM_STMT_EXECUTE; the issue is a server-side recursive CTE DECIMAL assignment contract, and connection-scoped SQL prepared execution directly covers reuse of the planned statement.
    • Not covered -race; the changed contract has no shared mutable state, goroutine lifecycle, or concurrency protocol, so race mode would not add a distinct proof.

    Findings

    Recursive CTE members are now validated against the anchor DECIMAL domain instead of using the former same-scale direct-copy path that could relabel an out-of-range value. The fix preserves precision checks for narrowed targets and validates scale growth against the full target precision. Strict and empty-sql_mode direct-DML controls agree with the recursive path, and rejected statements leave no partial DML or CTAS artifact.

    MOTR / BVT Coverage

    No new MOTR case was needed and no MOTR branch, recording, replay, commit, or PR was created. MatrixOne already contains the focused public BVT at test/distributed/cases/recursive_cte/recursive_cte_decimal_precision.sql with its result file, covering strict DECIMAL64 / DECIMAL128 boundaries, INSERT atomicity, CTAS cleanup, scale growth, legal controls, and SQL prepared execution. The exact SQL contract was also executed directly through the verified Proxy, CN1, and CN2 runtime above.

    Large-data regression is not required. The defect is determined by per-value DECIMAL conversion and statement atomicity at precision/scale boundaries; it does not depend on row count, block/batch boundaries, memory/spill, concurrency, data skew, network transfer, or resource scale. Minimum boundary inputs directly cover the relevant conversion states without adding unrelated CI cost.

    Final Conclusion

    Fixed. The related fix is present in current main; the original reproduction, direct-DML comparison, atomicity and CTAS assertions, strict/non-strict controls, precision/scale boundaries, prepared execution, and focused unit tests all passed through Proxy, CN1, and CN2. No large-data regression is needed.

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