Skip to content

[Bug]: prepared FIELD miscompares mixed DECIMAL and string runtime operands #29317

Closed

Description

@Ariznawlll

Environment

  • MatrixOne main: 95b1d2bfd9d756d08320569858080cb16f230fff
  • Local standalone launch configuration

Problem

FIELD() returns a different position when all operands are prepared parameters and their runtime domains mix DECIMAL, integer, character, and NULL values. The equivalent direct expression returns the matching position.

The presence of a character candidate after the matching integer candidate changes the cached all-parameter statement to character comparison. DECIMAL 2.0 is then compared as text with integer 2 and no match is reported, even though mixed FIELD() operands use numeric comparison.

Reproduction

SELECT FIELD(2.0,1,2,'x',NULL);

PREPARE p FROM 'SELECT FIELD(?,?,?,?,?)';
SET @needle=2.0,@a=1,@b=2,@c='x',@n=NULL;
EXECUTE p USING @needle,@a,@b,@c,@n;

Results:

direct expression: 2
prepared expression: 0

CAST(2 AS DECIMAL(10,1)) reproduces the same prepared result.

Controls

  • Repeated the full matrix three times with identical results.
  • FIELD(?,1,2,'x',NULL) returns 2 for the same DECIMAL runtime value; the issue requires all operands to be parameter markers.
  • All-parameter calls return 2 when all runtime operands are DECIMAL, all are DOUBLE, or all are integers.
  • The mixed all-parameter call returns 2 when the needle is DOUBLE or integer, but returns 0 when it is DECIMAL.
  • Rebinding the same cached statement from DECIMAL to integer returns 2, so the statement remains reusable.

Code path

fieldCheck in pkg/sql/plan/function/func_binary.go treats T_any as compatible while trying the string overload first. A plan whose operands are all parameter markers can therefore bind FIELD to FieldString before runtime domains are known. Prepared specialization does not re-resolve the variadic common comparison domain, so DECIMAL 2.0 and integer 2 reach byte comparison as different strings.

Direct binding sees the real operand domains and selects the numeric comparison path.

Activity

  1. added this to the MO-202609 milestone on Sep 24, 2026
  2. added
    kind/bug
    on Sep 24, 2026
  3. added theissue type on Sep 24, 2026
  4. Ariznawlll commented on Sep 24, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充一个同属“全参数 FIELD() prepared 运行时比较域选择”的二进制错误场景,建议与本 issue 一并修复和回归,不另开重复 issue。

    环境

    • MatrixOne main:95b1d2bfd9d756d08320569858080cb16f230fff
    • 本地单机部署,2026-09-24
    • 对照:MySQL 8.0.45

    复现

    SELECT FIELD(x'0062',x'61',x'0062');
    
    SET @needle=x'0062',@first=x'61',@second=x'0062';
    SELECT FIELD(@needle,@first,@second);
    
    PREPARE p FROM 'SELECT FIELD(?,?,?)';
    EXECUTE p USING @needle,@first,@second;
    DEALLOCATE PREPARE p;

    具体问题

    • MatrixOne 直接表达式:2
    • MatrixOne 用户变量表达式:2
    • MatrixOne prepared 全参数表达式:1
    • MySQL 8.0.45 三种路径均为:2

    x'0062' 与第一个候选 x'61' 不相等,只与第二个候选 x'0062' 相等;prepared 路径返回 1,说明比较了错误的值/运行时域。结果连续 3 次一致;新建 prepared statement、重复执行以及先绑定文本值再重新绑定二进制值都能复现。

    现有 issue 的 DECIMAL/字符串场景返回 0,这里的二进制场景返回错误的非零位置 1,会让调用方误认为命中了第一个候选。建议修复后同时加入以下回归:

    1. 全参数 FIELD(?,?,?) 绑定带前导零字节的 BINARY/VARBINARY;
    2. 同一 prepared statement 在文本与二进制参数之间重复绑定;
    3. 直接表达式、用户变量表达式和 prepared 表达式结果一致。

    本地证据:evidence/function_exploration_20260924/field_prepared_binary_repro.sql 及三轮输出。

  5. self-assigned this
    on Sep 24, 2026
  6. added a commit that references this issue on Sep 25, 2026
    426cf10
  7. Ariznawlll commented on Sep 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:5be6cd90b501da225be591ffffe6000bf01f515e
    • 部署方式:本地单机(1 CN / 1 TN / 1 LogService)
    • 测试日期:2026-09-27

    原问题验证

    在上述最新 main 上按 issue 原始复现步骤执行,并独立重复 3 次;3/3 均满足修复后的预期。当前 main 已包含修复提交 426cf10b000b6a828e8c69f1af26082f26036ed3。

    Happy Path 覆盖

    1. prepared FIELD 的数字/字符串混合参数及二进制参数与直接 SQL 结果一致。

    Unhappy Path 覆盖

    1. 覆盖 NULL 与前导零二进制值,重复绑定未出现字符串化误比较。

    白盒验证

    确认修复提交 426cf10b000b6a828e8c69f1af26082f26036ed3 是当前 main 的祖先,并核对对应 MatrixOne BVT/实现路径;本次另以公开 SQL 协议执行黑盒验收。

    回归测试

    已加入 MOTR PR #204 的 issue_29317_29318_29378_prepared_comparison.test,MOTR 提交 2b4462f2;本地基线比对连续 3 次通过。

    测试证据

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions