Repository navigation
Description
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:
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
fix: enforce recursive CTE decimal precision (#29280)
slyunnn commentedon Sep 30, 2026
Regression and extended verification for #29137 (2026-09-30)
Verification Environment
origin/maincommit76862df5c89e8868bed5963c428f65b9eafb34b3(Optimize leading ranges on composite sort keys (#29513)).mo-servicebinary was built from that exact worktree. Its SHA-256 was706b0f017fffc7a5d0e49f285dad22a5390885c678a88b169f6ca631384bb82b, matching the binary mounted in CN1.2 CN / 1 TN / 1 Log service / Proxy. The isolated MySQL endpoints were Proxy6611(service port6009), CN116611(service port16001), and CN216612(service port16002). All three were reachable and completed health checks after the regression cases.mysqlCLI. 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.3282f1c1e71d037a86710b8e79e387a7c800698e,fix: enforce recursive CTE decimal precision(#29280). The fix is an ancestor of the verifiedmaincommit.What Was Covered
DECIMAL(5,2)SELECTrejected1000.00with the expected range error through Proxy, CN1, and CN2.INSERTof1000.00and the original recursiveINSERTwere both rejected on all three endpoints; the target table remained at0rows, proving statement atomicity.derivedtable inmo_catalog.DECIMAL(5,2)overflow (-1000.00) andDECIMAL(19,2)/DECIMAL128positive overflow were rejected through all three endpoints.DECIMAL(4,2)to the recursive anchorDECIMAL(5,4)was rejected through all three endpoints.999.98,999.99and0.0000,9.9900through all three endpoints.PREPAREandEXECUTEof the original recursive CTE returned the expected range error on all three endpoints; a subsequentSELECT 1succeeded.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.0:TestParseDecimalRejectsBothPrecisionEndpoints, DECIMAL cast precision/scale tests, andTestRecursiveCteDecimalAnchorKeepsCheckedCast.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.-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_modedirect-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.sqlwith its result file, covering strictDECIMAL64/DECIMAL128boundaries,INSERTatomicity, 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.