Skip to content

[Bug]: Prepared DECIMAL arguments in COALESCE/GREATEST/LEAST lose precision and NULL rebinding panics #26879

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

I searched open/closed MatrixOne issues and pull requests for prepared DECIMAL with COALESCE/GREATEST/LEAST, precision loss, NULL rebinding, strToFloat, and the observed index-out-of-range panic. No exact duplicate was found.

Related but distinct:

Branch Name

main

Commit ID

23bf597e6bcaccb6cc8331bd9f6478ca77c2b59e

Other Environment Information

  • Local mo-service, 2 CN / 1 TN / 1 Log service / Proxy
  • Reproduced through both SQL PREPARE/EXECUTE and go-sql-driver/mysql binary protocol
  • MySQL 8.3.0 was run locally as the behavior oracle
  • Non-partitioned table; no distributed transaction

Actual Behavior

Prepared string parameters combined with DECIMAL128 columns inside COALESCE, GREATEST, and LEAST are coerced through DOUBLE. Distinct values above 2^53 become equal and queries silently return extra rows.

For parameter 9007199254740992.0000000002:

expression                    MatrixOne raw   explicit DECIMAL cast / MySQL 8.3
COALESCE(?,d)=d               1,2,3           2
GREATEST(d,?)=d               1,2,3,4         2,3,4
LEAST(d,?)=d                  1,2,3           1,2
NULLIF(d,?) IS NULL           2               2

There is also a deterministic reuse panic. A fresh statement executed with NULL first works. But after the same prepared COALESCE statement has executed once with a non-NULL string, rebinding it to NULL over two or more input rows fails with:

internal error: panic runtime error: index out of range [1] with length 1
... vector.(*FunctionResult[...]).Append
... function.strToFloat
... function.strTypeToOthers
... function.newCast

The sequence value -> NULL -> value -> NULL produces wrong rows -> panic -> wrong rows -> panic in every run. One input row does not panic; two and four rows reproduce 3/3. The server remains available and the statement can execute a non-NULL value after the error.

MySQL 8.3 returns exact results for all function cases and returns all rows for the NULL COALESCE execution without an error.

Expected Behavior

When a prepared parameter shares a common type with a DECIMAL operand in these functions, it must retain exact DECIMAL semantics. Parameterized and explicitly cast forms must return the same rows. Rebinding any prepared parameter from non-NULL to NULL must never panic.

Steps to Reproduce

DROP DATABASE IF EXISTS prepared_decimal_function_repro;
CREATE DATABASE prepared_decimal_function_repro;
USE prepared_decimal_function_repro;

CREATE TABLE t(id INT PRIMARY KEY, d DECIMAL(38,10));
INSERT INTO t VALUES
  (1,9007199254740992.0000000001),
  (2,9007199254740992.0000000002),
  (3,9007199254740992.0000000003),
  (4,9007199254740993.0000000001);

PREPARE pc FROM
  'SELECT id FROM t WHERE COALESCE(?,d)=d ORDER BY id';
SET @p='9007199254740992.0000000002';
EXECUTE pc USING @p;
-- Wrong: 1,2,3. Expected: 2.

SET @p=NULL;
EXECUTE pc USING @p;
-- Panic. Expected: 1,2,3,4.

SET @p='9007199254740992.0000000002';
EXECUTE pc USING @p;
-- Statement recovers, but still returns wrong 1,2,3.

SET @p=NULL;
EXECUTE pc USING @p;
-- Panics again.

-- Explicit typing is correct.
PREPARE pc_typed FROM
  'SELECT id FROM t
   WHERE COALESCE(CAST(? AS DECIMAL(38,10)),d)=d
   ORDER BY id';
SET @p='9007199254740992.0000000002';
EXECUTE pc_typed USING @p;
-- 2

PREPARE pg FROM
  'SELECT id FROM t WHERE GREATEST(d,?)=d ORDER BY id';
EXECUTE pg USING @p;
-- Wrong: 1,2,3,4. Expected: 2,3,4.

PREPARE pl FROM
  'SELECT id FROM t WHERE LEAST(d,?)=d ORDER BY id';
EXECUTE pl USING @p;
-- Wrong: 1,2,3. Expected: 1,2.

Additional Information

PR #26849 added normalizeDecimalParamComparisonArgs, but intentionally invokes it only for direct comparison operators. The function overloads here still choose DOUBLE as the common type for the prepared TEXT transport and DECIMAL operand. That explains the precision collapse and sends COALESCE through strToFloat; after a non-NULL execution, NULL rebinding exposes a result-vector cardinality mismatch in that cast path.

Please retain:

  • SQL and binary prepared protocols;
  • DECIMAL64 and DECIMAL128 values around 2^53;
  • explicit-cast and MySQL controls;
  • NULL-first and value-to-NULL reuse;
  • one-row and multi-row batches;
  • recovery after the panic.

Activity

  1. added
    kind/bug
    on Aug 9, 2026
  2. added this to the MO-202608 milestone on Aug 9, 2026
  3. added theissue type on Aug 9, 2026
  4. added
    severity/s0
    and removed on Aug 10, 2026
  5. ck89119 commented on Aug 28, 2026

    @ck89119
    Contributor

    Verified on the latest main (e5f26b66195373f9bff512ac9f428788d1c07e8c). This issue is no longer reproducible after #27483 (3bb6b15c93).

    I ran the prepared-protocol regression test on the latest main:

    TestIssue27088PreparedDecimalCommonType
    PASS
    

    The passing coverage includes exact DECIMAL behavior above 2^53, common-value-function handling, both COM_STMT and SQL PREPARE/EXECUTE paths, and non-NULL-to-NULL prepared parameter reuse without a panic.

  6. Ariznawlll commented on Aug 28, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main SQL PREPARE 复测 3/3:COALESCE、GREATEST、LEAST 的高精度 DECIMAL 正确;value→NULL→value 重绑无 panic;非法文本后重绑也可恢复。稳定 SQL 回归已加入 https://github.com/matrixorigin/motr/pull/131,并行三轮通过。二进制协议维度保留现有 Go 回归覆盖。无需 big-data、PITR/snapshot、stability 或 chaos。结论:关闭。

  7. Ariznawlll commented on Aug 28, 2026

    @Ariznawlll
    CollaboratorAuthor
  8. LeftHandCold commented on Sep 26, 2026

    @LeftHandCold
    Collaborator

    Main 再次观察到 GREATEST/LEAST Prepared DECIMAL 错行(2026-09-26)

    核查了两轮真实 MOTR Artifact、构建 receipt、固定测试 revision、原始 SQL/golden/diff,以及本 Issue 的关闭验收。结论:本 Issue 中 GREATEST/LEAST 两个子场景再次出现相同错行,属于高可信的回归信号;不是单凭 Job failure 或 Case 名称归类。建议重新检查本 Issue 的关闭状态,不另建重复 Issue。

    1. Case 级 pass → fail

    项目 前一轮:该 Case 通过 后一轮:该 Case 失败
    Run / Job 36089256712 / 108001303486 36152217640 / 108239266183
    Job 时间,UTC 2026-09-25 08:30:28–09:57:27 2026-09-25 20:28:53–22:06:25
    MatrixOne SHA 7ce783464806490263c184f22aaf5d307225adef 71c415705c909e524abd21dd0cf3e31b1c18e941
    Case 结果 success; stmts=20 ok=20 fail=0 failed; stmts=20 ok=20 fail=0

    两轮共同条件:

    注意:两个 Job 整体都因多项用例失败而为 failure;这里的 pass→fail 仅指该 Case。 后一轮 20 条 SQL 均执行完毕,但输出比较失败,所以 ok=20 不能解释为正确性通过。

    2. 原始结果证据

    后一轮 Artifact tke-family-weekly-shadow-1x2-motr-36152217640-1 中,failures.txt 的 14_issue_regression.issue_26879_prepared_decimal_functions 节包含完整 SQL、Expected、Actual 和 diff;两轮 summary_parallel.txt 分别记录该 Case 的通过/失败。

    查询 Golden / 原关闭验收预期 ID 本轮实际 ID
    GREATEST(d,?)=d 2,3,4 1,2,3,4
    LEAST(d,?)=d 1,2 1,2,3

    差异是整数主键行集合增加,不是 DECIMAL 显示格式或排序差异。两个查询均有 ORDER BY id。本轮 COALESCE(?,d)=d 的 value→NULL→value→NULL 四次执行仍符合 Golden,不能把本次描述为原 Issue 全部症状复发,也没有证据宣称 NULL 重绑 panic 再现。

    最小复现(在独立测试 database 中执行):

    CREATE TABLE t(id INT PRIMARY KEY, d DECIMAL(38,10));
    INSERT INTO t VALUES
      (1,9007199254740992.0000000001),
      (2,9007199254740992.0000000002),
      (3,9007199254740992.0000000003),
      (4,9007199254740993.0000000001);
    
    SET @p='9007199254740992.0000000002';
    PREPARE pg FROM 'SELECT id FROM t WHERE GREATEST(d,?)=d ORDER BY id';
    EXECUTE pg USING @p;
    -- expected: 2,3,4; observed in this MOTR run: 1,2,3,4
    
    PREPARE pl FROM 'SELECT id FROM t WHERE LEAST(d,?)=d ORDER BY id';
    EXECUTE pl USING @p;
    -- expected: 1,2; observed in this MOTR run: 1,2,3

    这里是 已定型 DECIMAL 列 + prepared marker,不是把 SQL 字符串字面量简单代入函数。MySQL 的 PREPARE 类型规则区分推导类型与执行时参数类型;字符串参数通常按推导类型隐式转换。MySQL 8.0.36 的 min/max 类型解析也先从非参数操作数推导参数类型。这是独立的文档/源码依据,不冒充本轮 MySQL 实测。

    3. 为什么补在本 Issue,而不是 #29318 或新建

    4. 根因候选与未完成的验证

    两个被测 SHA 的比较为单向前进 9 个提交。

    #29345(426cf10b000b6a828e8c69f1af26082f26036ed3)位于该区间。其 variadicSource 分支和 runtime source 重建会在 GREATEST/LEAST 上重新引入 SQL user variable 的 source type;这与本次 typed DECIMAL 参数路径高度相关,值得优先核查。

    这仍是源码支持的调查方向,不是已经证明 #29345 为引入者。 本轮没有完成该 PR 前后同环境 A/B,也没有在审查时最新 Main 3f0a68bd8034d37e098765920f328985c308c9ff 上重新运行。因此确认范围限定为上述真实 CI subject,不宣称当前 HEAD 必然仍失败,也不外推到二进制 prepared 协议或 4.2。

    建议下一步只复跑这个四行 Case:对比 #29345 parent fdfbea4fb7125d538d8675d725ace17811760023、merge 426cf10b000b6a828e8c69f1af26082f26036ed3 和最新 Main;补充函数返回类型/计划及显式 DECIMAL CAST 对照。修复时同时保留 COALESCE/NULL 重绑和 #29318 all-parameter mixed-domain 对照,避免修复一端再次破坏另一端。无需重跑整套 TKE/Weekly。

  9. XuPeng-SH commented on Sep 28, 2026

    @XuPeng-SH
    Contributor

    Independent QA found two additional prepared common-value gaps on clean main b9013dc6bb058ec74935a62ac3a35f57d13c2f98 (local 1 CN / 1 TN / 1 Log service), using MySQL 8.0.44 as a SQL oracle. Both are also present in the local #29428 worktree; they are not introduced by that PR. #29428 fixes the original #29389 GREATEST(d,?) / LEAST(d,?) regression and some typed/nested peers, but does not claim these extended cases are solved.

    Reproducer (run in a scratch database):

    CREATE TABLE t(id INT PRIMARY KEY, d DECIMAL(38,10));
    INSERT INTO t VALUES
      (1,9007199254740992.0000000001),
      (2,9007199254740992.0000000002),
      (3,9007199254740992.0000000003);
    SET @p='9007199254740992.0000000002';
    
    PREPARE p1 FROM 'SELECT id FROM t WHERE GREATEST(d,COALESCE(?,?))=d ORDER BY id';
    EXECUTE p1 USING @p,@p;
    -- main and #29428: 1,2,3; MySQL: 2,3
    
    PREPARE p2 FROM 'SELECT id FROM t WHERE GREATEST(?,IFNULL(?,d))=d ORDER BY id';
    EXECUTE p2 USING @p,@p;
    -- main and #29428: 1,2,3; MySQL: 2
    
    PREPARE p3 FROM 'SELECT id FROM t WHERE COALESCE(?,NULL,d)=d ORDER BY id';
    EXECUTE p3 USING @p;
    -- main and #29428: invalid argument operator cast [VARCHAR ANY]; MySQL: 1,2,3
    
    PREPARE p4 FROM 'SELECT id FROM t WHERE COALESCE(?,d,NULL)=d ORDER BY id';
    EXECUTE p4 USING @p;
    -- main and #29428: same cast error; MySQL: 2
    
    PREPARE p5 FROM 'SELECT id FROM t WHERE COALESCE(?,CAST(NULL AS DECIMAL(38,10)),d)=d ORDER BY id';
    EXECUTE p5 USING @p;
    -- main and #29428: 1,2,3; MySQL: 2

    Changing @p to NULL or a numeric-prefix string also reproduces the untyped-NULL cast error. GREATEST(d,COALESCE(?,?)) and GREATEST(?,IFNULL(?,d)) show that the outer DECIMAL peer is not propagated through an inner result whose arguments are unresolved markers. The NULL forms have an additional type/order boundary; a local attempt to special-case literal NULL did not fix public SQL and was discarded. They need a common-type/provenance design, with direct SQL string and actual FLOAT controls, rather than another result-specific cast.

    This extends the still-open prepared common-value issue. It is separate from #29318's resolved all-parameter mixed-domain case. Please retain these exact SQL and MySQL row expectations as acceptance cases for the follow-up fix.

  10. XuPeng-SH commented on Sep 28, 2026

    @XuPeng-SH
    Contributor

    Independent QA found another prepared NULL rebinding case on clean main b9013dc6bb058ec74935a62ac3a35f57d13c2f98. The same result occurs on PR #29428's candidate build; the PR did not introduce it. This belongs to the still-open NULL/common-value follow-up here.

    CREATE TABLE t(id INT PRIMARY KEY, d DECIMAL(38,10));
    INSERT INTO t VALUES (1,9007199254740992.0000000001);
    SET @p=NULL;
    
    PREPARE p FROM 'SELECT id FROM t WHERE ABS(COALESCE(?,d))=d';
    EXECUTE p USING @p;
    -- ERROR 20203: invalid argument cast to int, bad value 9007199254740992.0000000001
    
    SELECT id FROM t WHERE ABS(COALESCE(NULL,d))=d;
    -- id 1

    This fails on the first execution with NULL, so it does not depend on a prior cached non-NULL plan. Wrapping the inner expression in GREATEST(?,ABS(COALESCE(?,d))) or COALESCE(?,ABS(COALESCE(?,d))) reproduces the same error; GREATEST(?,COALESCE(?,d)+0) does not. The failure is already present in the single ABS(COALESCE(?,d)) child, which separates it from #29428's fixed outer DECIMAL precision path. The exact prepare-time cast/binder fault still needs investigation. A future fix should test fresh NULL, value→NULL reuse, direct-literal control, and DECIMAL values above 2^53.

  11. Ariznawlll commented on Oct 8, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    MatrixOne 最新 main 1a0088790c8823bd3fe824ad9ff2ef1ce130eb31;本地独立数据目录、单 CN/TN 服务;MOTR PR #218 已推送,Python 3.10/3.12/3.13 与 Go 离线检查均通过。

    原问题验证

    原 DECIMAL(38,10) 高精度参数在 COALESCE/GREATEST/LEAST 不再误匹配;非 NULL→NULL 重绑不再 panic。

    正常场景覆盖

    四行 >2^53 数据中,原 COALESCE 只返回 id=2,GREATEST 返回 2/3/4,LEAST 返回 1/2;嵌套 GREATEST/IFNULL 与 ABS(COALESCE) 结果符合预期。

    边界与异常场景覆盖

    对同一 prepared 语句执行值→NULL→值→NULL;新补的非定型 NULL、CAST(NULL AS DECIMAL) 与嵌套函数路径均稳定,源表仍 4 行。

    白盒验证

    现有 pkg/sql/plan/issue_26879_common_value_test.go 覆盖参数类型推断;本轮补真实服务 SQL/多行扫描。

    回归测试

    扩充 MOTR 14_issue_regression.issue_26879_prepared_decimal_functions,3/3 通过(48 条语句/轮)。

    测试证据

    MOTR PR #218 的原路径、补充评论路径与 golden;已含 p2–p5 和 NULL 首次执行。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions