Skip to content

[Bug]: FORMAT panics on a lone minus sign or an empty decimal-place string #28475

Closed

Description

@Ariznawlll

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 0

MySQL 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

  1. added this to the MO-202609 milestone on Sep 9, 2026
  2. added theissue type on Sep 9, 2026
  3. self-assigned this
    on Sep 9, 2026
  4. added a commit that references this issue on Sep 10, 2026
    1603316
  5. Ariznawlll commented on Sep 10, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:fc621e3616d229c7a29d0c80e73ed8eb1997459c
    • 部署方式:本地单机 1 Log / 1 TN / 1 CN
    • 测试日期:2026-09-10

    原问题验证

    原始 FORMAT('-',2) 和 FORMAT(1.25,'') 连续 3 次分别返回 0.00 和 1;均未出现内部 panic。

    Happy Path 覆盖

    1. VARCHAR 列的 '-'、空 scale 和普通 1234.56/2 分别返回 0.00 / 1 / 1,234.56。
    2. SQL PREPARE/EXECUTE 的 lone-minus 和 plus-scale 路径分别返回 0.00 / 1,234.56。

    Unhappy Path 覆盖

    1. 原来会触发 panic 的 lone-minus 与空 scale 现按数值 coercion 正常返回;执行后 fixture 仍为 3 行,确认无中断或数据破坏。
    2. 非数字 number/scale 分别返回 0.00 / 1,无内部错误。

    白盒验证

    修复 PR matrixone#28508 已合入;pkg/vectorize/format 定向单测通过。

    回归测试

    已加入 MOTR PR #153:issue_28213_28475_28476_28478_28480_format_compatibility.test,连续 3 次通过。

    测试证据

    • 本地构建二进制的 Git commit 为 fc621e3616。
    • 回归提交:1cc567ea。

    测试结论

    测试通过。

  6. added 3 commits that reference this issue on Sep 17, 2026
    ef793b0
    79163fe
    7a03d78
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/compatibilitykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions