Repository navigation
Description
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 returnsid = 1.- At
DECIMAL(39,1),DECIMAL(40,2), andDECIMAL(65,30), the same-spelling unquoted literal returns no row while the explicitCAST(... AS DECIMAL(M,D))control returnsid = 1. - A quoted literal containing the same 65-digit decimal spelling also returns
id = 1for theDECIMAL(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
- [Bug]: direct DECIMAL string comparisons lose precision through DOUBLE coercion #26897: text string literal DECIMAL coercion, fixed separately
- [Bug]: DECIMAL coercion and prepared runtime domains are inconsistent across expression consumers #27088: closed tracking issue for earlier DECIMAL coercion and prepared-runtime manifestations
Activity
Ariznawlll commentedon Sep 12, 2026
Additional scope on the same
main@4f9d170a22deaabc2eee6b239a51a8cb40a39f9ftwo-CN fixture:The truncation happens before expression evaluation, so it affects more than
=. With one storedDECIMAL(65,30)value equal to the complete literal, each result below was identical in 3/3 executions:v = bare_literalv < bare_literalv > bare_literalid=1v IN (bare_literal)v BETWEEN bare_literal AND bare_literalCAST(bare_literal AS CHAR).123bare_literal + 0.123CAST('full value' AS DECIMAL(65,30)) + 0This confirms an operand-value loss before comparison, range, list, formatting, or arithmetic operators run. The explicit
DECIMALcontrol remains exact; the affected surface is the bare numeric literal path that promotes beyondDECIMAL128.Ariznawlll commentedon Sep 12, 2026
DDL expression scope on the same
main@4f9d170a22deaabc2eee6b239a51a8cb40a39f9ftwo-CN baseline:DEFAULTandCHECKfollow different binding paths.The default stores the complete value and
SHOW CREATE TABLEpreserves the full spelling. Its column target supplies the required Decimal256 type.In contrast, this valid row is rejected in 3/3 runs:
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.Ariznawlll commentedon Sep 12, 2026
Generated-column scope on the same baseline:
In 3/3 runs,
gwas stored as the truncated value ending in.123000000000000000000000000000. Replacing only the generated-expression constant with an explicitCAST('<full value>' AS DECIMAL(65,30))stores all 30 fractional digits.SHOW CREATE TABLEprints 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 typedDEFAULTpath noted earlier.Ariznawlll commentedon Sep 12, 2026
Derived-object and write-path scope on the same baseline:
CREATE VIEW v AS SELECT <bare literal>.123althoughSHOW CREATE VIEWpreserves the full source spellingCREATE TABLE ct AS SELECT <bare literal>.123; CTAS column metadata isDECIMAL(65,3)INSERT INTO typed_target DECIMAL(65,30) SELECT <bare literal>.123000000000000000000000000000The last result is important: unlike a typed
DEFAULT, anINSERT … SELECTtarget 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, andINSERT … SELECTeven when the eventual destination column isDECIMAL(65,30).fix: support Decimal256 RANGE window bounds (#28825)
Ariznawlll commentedon Sep 21, 2026
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.