Skip to content

[Bug]: FIELD loses DECIMAL precision through DOUBLE coercion #28397

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

No open or closed issue was found for FIELD() losing DECIMAL comparison precision.

Branch Name

main

Commit ID

f72ca9efbeb3a7c673701e4a135cc6670c33cfde

Other Environment Information

  • Local standalone MatrixOne
  • Reference: MySQL 8.3.0

Actual Behavior

FIELD() converts DECIMAL arguments to DOUBLE before comparing them. Distinct exact values that collapse to the same floating-point representation therefore match the wrong candidate.

SELECT FIELD(
  CAST(9007199254740993 AS DECIMAL(20,0)),
  CAST(9007199254740992 AS DECIMAL(20,0)),
  CAST(9007199254740993 AS DECIMAL(20,0))
);
MatrixOne: 1
MySQL 8.3: 2

The same false first match occurs at other exact-numeric boundaries:

SELECT FIELD(
  CAST('99999999999999999999999999999999999999' AS DECIMAL(38,0)),
  CAST('99999999999999999999999999999999999998' AS DECIMAL(38,0)),
  CAST('99999999999999999999999999999999999999' AS DECIMAL(38,0))
);

SELECT FIELD(
  CAST(1.0000000000000001 AS DECIMAL(20,16)),
  CAST(1.0000000000000000 AS DECIMAL(20,16)),
  CAST(1.0000000000000001 AS DECIMAL(20,16))
);

MatrixOne returns 1 for both statements; MySQL returns 2.

The issue reproduces with table columns:

CREATE TABLE t(
  search DECIMAL(38,0),
  candidate1 DECIMAL(38,0),
  candidate2 DECIMAL(38,0)
);
INSERT INTO t VALUES
  (99999999999999999999999999999999999999,
   99999999999999999999999999999999999998,
   99999999999999999999999999999999999999),
  (9007199254740993,9007199254740992,9007199254740993),
  (123,122,123);

SELECT search, FIELD(search,candidate1,candidate2) FROM t ORDER BY search;

MatrixOne returns indexes 2, 1, 1; MySQL returns 2, 2, 2.

Each constant expression was repeated three times.

Expected Behavior

When all FIELD() operands are DECIMAL, comparisons should retain exact DECIMAL precision and return the index of the actually equal value. Distinct exact values must not become equal through an intermediate DOUBLE conversion.

Controls

  • CASE WHEN search=candidate1 THEN 1 WHEN search=candidate2 THEN 2 ELSE 0 END returns 2,2,2 in MatrixOne for the same table.
  • Ordinary integers and low-precision decimals such as 123/122 and 1.25/1.24 return the correct index.
  • Equal DECIMAL values with different declared scales compare as equal in both engines.

Root cause

fieldCheck() has direct overloads for integer and floating types, but not DECIMAL. When the input types do not match a direct overload, it selects overload 10 and requests FLOAT64 casts for every operand. FieldNumber[float64] then compares the already rounded values.

Mixed signed/unsigned integer inputs also take the fallback conversion path and do not follow MySQL's all-integer comparison rule, for example FIELD(18446744073709551615,-1,18446744073709551615) returns 2 in MatrixOne and 1 in MySQL.

Activity

  1. added this to the MO-202609 milestone on Sep 8, 2026
  2. added
    kind/bug
    on Sep 8, 2026
  3. added theissue type on Sep 8, 2026
  4. added
    severity/s0
    and removed on Sep 8, 2026
  5. Ariznawlll commented on Sep 9, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:e8ae1e51f2b0f04b6d69d37ff6ccf8166736c9e6
    • 部署方式:本地单机嵌入式集群与 MOTR MySQL 协议黑盒
    • 测试日期:2026-09-09

    原问题验证

    对 FIELD() 执行 DECIMAL 精确比较:9007199254740993 与相邻值、DECIMAL(38,0) 极值、DECIMAL(20,16) 小数位边界均返回目标位置 2,未再发生经 DOUBLE 转换导致的误匹配。连续 3 轮一致。

    Happy Path 覆盖

    1. 整数精度边界、DECIMAL128 边界和小数 scale 边界均精确命中第二个候选值。
    2. 等值但不同 decimal scale 的候选值返回第一个匹配位置。
    3. UNSIGNED 最大值与负数混合比较返回正确位置。
    4. 表列 DECIMAL(38,0) 三行读取均返回 2。

    Unhappy Path 覆盖

    1. 含 NULL 候选项的 signed/unsigned 比较仍正确跳过 NULL 并命中第二个候选项。
    2. NULL 搜索值返回 0;无异常、无错误结果类型或连接状态污染。

    白盒验证

    go test ./pkg/tests/issues -run '^TestIssue28397FieldKeepsExactNumericComparison$' 连续 3 轮通过,覆盖上述常量与表列路径。

    回归测试

    测试证据

    • MOTR PR:matrixorigin/motr#152,提交 156c477b1d8757b1de4f5784532b5ddd75d8744e。
    • 所有精度边界断言在三轮中保持一致。

    测试结论

    测试通过。

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