Skip to content

[Bug]: Uppercase scientific-notation DECIMAL CAST silently saturates valid values #29550

Closed

Description

@XuPeng-SH

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Searched open and closed DECIMAL/scientific/exponent/CAST reports. #2299 is the historical general scientific-notation support request; lowercase e already works. This report concerns uppercase E being silently treated as overflow and persisted as the DECIMAL bound. No existing report for this specific failure was found.

Branch Name

main

Commit ID

79fa30e10c0b298567eb073cdfd88bdc91ab09b9 (final re-confirmation). Initially reproduced at e784c47e112756612cbdc792340e75161aab36d1.

Other Environment Information

  • Debian Linux x86_64, Go 1.26.4; CPU make -j8 build at the pinned commit.
  • Isolated standalone CN/TN/logservice, DISK-V2; PyMySQL text/SQL PREPARE and mysql-connector-python COM_STMT execution.
  • Independent reference: an isolated MySQL 8.0.44 instance and Python Decimal.
  • Found during QA covering the latest 150 main commits plus the subsequent admission-diagnostics commit; introduced-by revision is not claimed.

Actual Behavior

Valid uppercase scientific strings saturate to the DECIMAL upper/lower bound instead of converting to their numeric value. Even zero saturates:

Expression MatrixOne MySQL / exact expected
CAST('1E-2' AS DECIMAL(5,2)) 999.99 0.01
CAST('1E2' AS DECIMAL(5,2)) 999.99 100.00
CAST('-1E-2' AS DECIMAL(5,2)) -999.99 -0.01
CAST('0E2' AS DECIMAL(5,2)) 999.99 0.00
CAST('1e-2' AS DECIMAL(5,2)) 0.01 0.01

The problem is reproduced for Decimal64/128/256 result types: DECIMAL(5,2), (20,8), (38,18), and (65,10), across positive/negative/zero/leading-plus inputs. It affects literal and VARCHAR-column CAST, SQL PREPARE, and binary prepared CAST. INSERT ... SELECT CAST(...) stores the wrong value without warnings. A seeded multiplication challenge initially exposed enormous products; reduction proved CAST, not multiplication, is the first failing owner.

Expected Behavior

E and e spellings of an otherwise identical, representable scientific number must yield the same DECIMAL value. A lexical/parser failure must not be mistaken for a proven range overflow and silently converted to a type bound.

Steps to Reproduce

DROP DATABASE IF EXISTS qa_decimal_scientific;
CREATE DATABASE qa_decimal_scientific;
USE qa_decimal_scientific;

SELECT CAST('1E-2' AS DECIMAL(5,2)),
       CAST('1e-2' AS DECIMAL(5,2)),
       CAST('0E2' AS DECIMAL(5,2));
-- Actual:   999.99, 0.01, 999.99
-- Expected:   0.01, 0.01,   0.00

CREATE TABLE src(id INT PRIMARY KEY, v VARCHAR(20));
INSERT INTO src VALUES (1,'1E-2'),(2,'1e-2'),(3,'0E2');
SELECT id,CAST(v AS DECIMAL(5,2)) FROM src ORDER BY id;
SHOW WARNINGS;
-- Wrong rows 1 and 3; no warnings.

CREATE TABLE dst(id INT PRIMARY KEY, v DECIMAL(5,2));
INSERT INTO dst SELECT id,CAST(v AS DECIMAL(5,2)) FROM src;
SELECT * FROM dst ORDER BY id;
-- Wrong conversions are persisted, not merely displayed incorrectly.

PREPARE p FROM 'SELECT CAST(? AS DECIMAL(5,2))';
SET @v='1E-2';
EXECUTE p USING @v;
-- Actual 999.99, expected 0.01.
DEALLOCATE PREPARE p;
DROP DATABASE qa_decimal_scientific;

Additional information

Root-cause chain at the pinned commit:

  1. parseCastNumericToken and prefixedDigitsToDecimalString preserve uppercase E for base-10 strings (pkg/sql/plan/function/func_cast.go).
  2. The Decimal64/128/256 lexical parsers in pkg/container/types/decimal.go recognize lowercase e, so uppercase spelling produces a lexical conversion error.
  3. strToDecimal64/128/256 catches any explicit-cast parsing error and tries clampDecimal*CastString. decimalCastStringSign uses big.ParseFloat, which accepts uppercase E, but that path checks only the sign and unconditionally returns the bound. It does not prove overflow. Thus a valid value, including zero, silently becomes an extremum.
  4. mysqlDecimalPrefix already normalizes E elsewhere; its successful numeric-prefix path does not close this explicit CAST path.

Suggested priority: P1 because SQL can persist grossly incorrect numeric data without error/warning. Fix at the existing conversion/parser owner across all three storage widths; preserve legitimate overflow, signed bounds, lowercase scientific inputs, prefixes, NULL, and explicit/assignment contracts. Regression acceptance should include literal/column/SQL+binary PREPARE, INSERT SELECT, signs/zero, both exponent spellings, and actual out-of-range controls.

Activity

  1. self-assigned this
    on Oct 1, 2026
  2. added
    kind/bug
    on Oct 1, 2026
  3. added a commit that references this issue on Oct 2, 2026
    7a169dd
  4. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • Commit:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 部署方式:本地全新单 CN mo-service,独立数据目录;MySQL 文本协议与 server-side PREPARE
    • 测试日期:2026-10-02

    原问题验证

    按 issue 原始语义在上述 commit 重新执行并重复 3 次。科学计数文本大小写均解析为 0.01,零与负数分别为 0、-0.01。

    Happy Path 覆盖

    覆盖 e/E、正负 exponent、leading sign 和 prepared 重绑。

    Unhappy Path 覆盖

    覆盖 malformed exponent、溢出、下溢和 NULL;无 prefix 静默误读。

    白盒验证

    关联修复 #29556 已进入当前 main。同时将本 issue 的可公开观察契约固化为独立回归,避免只依赖实现细节或共享测试间接判断。

    回归测试

    测试证据

    • 精确测试版本:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 当前结果:科学计数文本大小写均解析为 0.01,零与负数分别为 0、-0.01。
    • 回归代码与 golden result 已提交到 MOTR #209;本地全量 14_issue_regression 中观察到的旧 GOROOT/历史 golden/并发锁冲突属于既有无关失败,以上新增用例单独重跑均稳定通过。

    测试结论

    测试通过。

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

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions