Repository navigation
Description
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 ENDreturns2,2,2in MatrixOne for the same table.- Ordinary integers and low-precision decimals such as
123/122and1.25/1.24return 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
fix(function): preserve exact FIELD numeric comparisons (#28429)
Ariznawlll commentedon Sep 9, 2026
测试版本
maine8ae1e51f2b0f04b6d69d37ff6ccf8166736c9e6原问题验证
对
FIELD()执行 DECIMAL 精确比较:9007199254740993与相邻值、DECIMAL(38,0) 极值、DECIMAL(20,16) 小数位边界均返回目标位置 2,未再发生经 DOUBLE 转换导致的误匹配。连续 3 轮一致。Happy Path 覆盖
UNSIGNED最大值与负数混合比较返回正确位置。Unhappy Path 覆盖
白盒验证
go test ./pkg/tests/issues -run '^TestIssue28397FieldKeepsExactNumericComparison$'连续 3 轮通过,覆盖上述常量与表列路径。回归测试
issue_28397_field_decimal_precision.test已加入 PR paritial data type #152,覆盖高精度、scale、signed/unsigned、NULL 与表列读取。测试证据
156c477b1d8757b1de4f5784532b5ddd75d8744e。测试结论
测试通过。
fix(function): preserve exact FIELD numeric comparisons (matrixorigin…
fix(function): preserve exact FIELD numeric comparisons (matrixorigin…
fix(function): preserve exact FIELD numeric comparisons (matrixorigin…
fix(sql): reject incompatible binary casts in regexp expressions (#29524