Repository navigation
Description
Is there an existing issue for the same bug?
- I have checked open and closed issues.
The open FORMAT correctness issue #28213 covers rounding, sign loss, and the scale upper bound. The older closed FORMAT panic issues used numeric table values or joins. None covers the two malformed character inputs below, which reach distinct unchecked string indexes in the current implementation.
Branch Name
main
Commit ID
f0c31cd4b830be32442cf329e0a3fb08aa9c16c3
Other Environment Information
- Local standalone MatrixOne built from the commit above
- Reference: MySQL 8.3.0
Actual Behavior
FORMAT() indexes its character arguments before checking their lengths. A one-byte minus sign as the number or an empty decimal-place string therefore produces an internal Go panic:
SELECT FORMAT('-', 2);
-- internal error: panic runtime error: index out of range [1] with length 1
SELECT FORMAT(1.25, '');
-- internal error: panic runtime error: index out of range [0] with length 0MySQL treats the malformed number as zero and the empty decimal-place string as zero:
FORMAT('-', 2) -> 0.00
FORMAT(1.25, '') -> 1
The same two panics occur when the arguments come from VARCHAR columns and from SQL PREPARE parameters. A mixed-row scan is aborted as soon as it reaches the malformed row, so valid rows from the same table cannot be returned.
Each constant, column, and prepared form was executed three times. The session and server remain usable after each error (SELECT COUNT(*) continues to return the expected row count), so this is an expression-evaluation panic surfaced as an internal query error rather than a process crash.
Expected Behavior
FORMAT() must validate argument lengths before indexing and apply its numeric-conversion rules without emitting an internal panic. For MySQL compatibility, '-' should format as zero and an empty decimal-place string should behave as zero decimal places.
Steps to Reproduce
DROP DATABASE IF EXISTS codex_format_malformed;
CREATE DATABASE codex_format_malformed;
USE codex_format_malformed;
SELECT FORMAT('-', 2);
SELECT FORMAT(1.25, '');
CREATE TABLE src(id INT PRIMARY KEY, n VARCHAR(20), d VARCHAR(20));
INSERT INTO src VALUES
(1, '-', '2'),
(2, '1.25', ''),
(3, '1234.56', '2');
SELECT id, FORMAT(n, d) FROM src WHERE id = 1;
SELECT id, FORMAT(n, d) FROM src WHERE id = 2;
SELECT id, FORMAT(n, d) FROM src ORDER BY id;
PREPARE p FROM 'SELECT FORMAT(?, ?)';
SET @n = '-', @d = 2;
EXECUTE p USING @n, @d;
SET @n = 1.25, @d = '';
EXECUTE p USING @n, @d;
DEALLOCATE PREPARE p;Controls
The following nearby inputs return normally in both engines: numeric values, 'abc', '+', '.', '-.', '-x', nonnumeric or negative nonempty scale strings, and NULL arguments.
Root Cause
pkg/vectorize/format/format.go reads scale[0] before checking whether scale is empty, and reads number[1] when the first character is '-' without first requiring len(number) >= 2. Constant folding and vector execution both call this routine, which explains the identical failure in literals, columns, and prepared statements.
Activity
fix(sql): harden FORMAT numeric and locale handling (#28508)
Ariznawlll commentedon Sep 10, 2026
测试版本
mainfc621e3616d229c7a29d0c80e73ed8eb1997459c1 Log / 1 TN / 1 CN原问题验证
原始
FORMAT('-',2)和FORMAT(1.25,'')连续 3 次分别返回0.00和1;均未出现内部 panic。Happy Path 覆盖
VARCHAR列的'-'、空 scale 和普通1234.56/2分别返回0.00 / 1 / 1,234.56。PREPARE/EXECUTE的 lone-minus 和 plus-scale 路径分别返回0.00 / 1,234.56。Unhappy Path 覆盖
0.00 / 1,无内部错误。白盒验证
修复 PR matrixone#28508 已合入;
pkg/vectorize/format定向单测通过。回归测试
已加入 MOTR PR #153:
issue_28213_28475_28476_28478_28480_format_compatibility.test,连续 3 次通过。测试证据
fc621e3616。测试结论
测试通过。
fix(sql): harden FORMAT numeric and locale handling (matrixorigin#28508)
fix(sql): harden FORMAT numeric and locale handling (matrixorigin#28508)
fix(sql): harden FORMAT numeric and locale handling (matrixorigin#28508)