You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: prepared FIELD miscompares mixed DECIMAL and string runtime operands #29317
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.
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;
Activity
Ariznawlll commentedon Sep 24, 2026
补充一个同属“全参数
FIELD()prepared 运行时比较域选择”的二进制错误场景,建议与本 issue 一并修复和回归,不另开重复 issue。环境
main:95b1d2bfd9d756d08320569858080cb16f230fff复现
具体问题
2212x'0062'与第一个候选x'61'不相等,只与第二个候选x'0062'相等;prepared 路径返回1,说明比较了错误的值/运行时域。结果连续 3 次一致;新建 prepared statement、重复执行以及先绑定文本值再重新绑定二进制值都能复现。现有 issue 的 DECIMAL/字符串场景返回
0,这里的二进制场景返回错误的非零位置1,会让调用方误认为命中了第一个候选。建议修复后同时加入以下回归:FIELD(?,?,?)绑定带前导零字节的 BINARY/VARBINARY;本地证据:
evidence/function_exploration_20260924/field_prepared_binary_repro.sql及三轮输出。fix: preserve prepared variadic comparison domains (#29345)
Ariznawlll commentedon Sep 27, 2026
测试版本
main5be6cd90b501da225be591ffffe6000bf01f515e原问题验证
在上述最新 main 上按 issue 原始复现步骤执行,并独立重复 3 次;3/3 均满足修复后的预期。当前 main 已包含修复提交
426cf10b000b6a828e8c69f1af26082f26036ed3。Happy Path 覆盖
Unhappy Path 覆盖
白盒验证
确认修复提交
426cf10b000b6a828e8c69f1af26082f26036ed3是当前 main 的祖先,并核对对应 MatrixOne BVT/实现路径;本次另以公开 SQL 协议执行黑盒验收。回归测试
已加入 MOTR PR #204 的
issue_29317_29318_29378_prepared_comparison.test,MOTR 提交2b4462f2;本地基线比对连续 3 次通过。测试证据
5be6cd90b501da225be591ffffe6000bf01f515e测试结论
测试通过。