Skip to content

[Bug]: prepared FIELD loses DECIMAL precision beside a fixed numeric peer #29378

Closed

Description

@XuPeng-SH

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Related issues are #28397 and #29317, but neither tracks this prepared statement shape: #28397's precision cases are fixed for direct expressions, while #29317 covers all operands being runtime parameters.

Branch Name

codex/fix-prepared-variadic-comparison

Commit ID

2c564748e34294e682dc337b92bbd0b80757d098

Other Environment Information

  • Darwin arm64 local CGo test environment.
  • Reproduced through the MySQL parser's PREPARE plan, FillValuesOfParamsInPlanWithSpecialization, and the expression executor. The follow-up used a package-level reproduction, not a fresh standalone SQL service.

Actual Behavior

With a DECIMAL SQL EXECUTE parameter and a fixed DECIMAL candidate, FIELD() rounds both distinct values to the same DOUBLE and returns candidate position 1. The equivalent direct expression returns 0.

At commit 2c564748e342, the focused planner reproduction returned 0 for direct SQL and 1 for the prepared execution. It also reproduces on base 7db5bee77ef4; the mismatch predates PR #29345.

Expected Behavior

The prepared execution should preserve the exact DECIMAL comparison domain and return 0, matching the equivalent direct expression. The all-parameter control also returns 0.

Steps to Reproduce

SELECT FIELD(
  CAST(9007199254740993 AS DECIMAL(20,0)),
  CAST(9007199254740992 AS DECIMAL(20,0))
);
-- 0

PREPARE p_field_decimal_peer FROM
  'SELECT FIELD(?, CAST(9007199254740992 AS DECIMAL(20,0)))';
SET @needle = CAST(9007199254740993 AS DECIMAL(20,0));
EXECUTE p_field_decimal_peer USING @needle;
-- Expected: 0; current prepared-plan reproduction: 1
DEALLOCATE PREPARE p_field_decimal_peer;

PREPARE p_field_decimal_control FROM 'SELECT FIELD(?, ?)';
SET @candidate = CAST(9007199254740992 AS DECIMAL(20,0));
EXECUTE p_field_decimal_control USING @needle, @candidate;
-- 0
DEALLOCATE PREPARE p_field_decimal_control;

The package-level reproduction prepares both queries, binds the marker with ParamValue{SourceType: DECIMAL(20,0)}, specializes the copied plan, and evaluates the resulting expression. The fixed candidate in the failing query retains the prepare-time FLOAT64 envelope; the all-parameter control restores both runtime DECIMAL domains.

Additional information

Activity

  1. added
    kind/bug
    on Sep 25, 2026
  2. self-assigned this
    on Sep 25, 2026
  3. XuPeng-SH commented on Sep 25, 2026

    @XuPeng-SH
    ContributorAuthor

    Follow-up verification: #29379 fixes the bare-marker example, but nested fixed-peer cases remain

    Deep review of exact PR head b85a90231da21c9c561cd3e88f52eab95004f50c: review and reproduction.

    Confirmed through a real MySQL connection to an embedded CN:

    SELECT FIELD(ABS(CAST(9007199254740993 AS DECIMAL(20,0))),
                 CAST(9007199254740992 AS DECIMAL(20,0))); -- 0
    PREPARE p FROM
      'SELECT FIELD(ABS(?), CAST(9007199254740992 AS DECIMAL(20,0)))';
    SET @n = CAST(9007199254740993 AS DECIMAL(20,0));
    EXECUTE p USING @n; -- still 1 on #29379
    DEALLOCATE PREPARE p;

    COALESCE(?, CAST(0 AS DECIMAL(20,0))), IF(true, ?, CAST(0 AS DECIMAL(20,0))), and placing ABS(?) in the candidate position also retain the same mismatch. Planner inspection shows the nested child is DECIMAL128 but FIELD still compares DOUBLE/DOUBLE because its fixed peer retains the provisional cast.

    The original bare-marker example now passes, as do additional exactness/explicit-cast/error-reuse controls. The four nested failures above also reproduce with the PR's two changed production files restored to base ce7f84dabc563fa5bb29c648e8b02aa6581b608b; they are remaining coverage of the original precision problem, not a new regression. Please preserve this remaining scope when deciding whether this issue can be closed, or explicitly move it to a linked follow-up.

    Related protocol gap, beyond the original SQL EXECUTE user-variable reproduction: COM_STMT_PREPARE/EXECUTE of SELECT FIELD(?, CAST(9007199254740992 AS DECIMAL(20,0))) with Go uint64(9007199254740993) also returns 1 instead of 0 on both base and PR head. The current SQL-source restoration excludes binary-protocol parameters. This also needs tracking if prepared FIELD precision is claimed across both protocols.

    Local evidence: /home/xupeng/mo-validation/pr29379-review-b85a/; 19 public SQL scenarios, 14 passing and the 5 cases above failing on PR head. Exact-head SCA and existing BVT pass but do not include these counterexamples.

  4. XuPeng-SH commented on Sep 25, 2026

    @XuPeng-SH
    ContributorAuthor

    Follow-up to my earlier review comment: PR #29379 has been rebased and updated at 90bde35db972e34f6fda74febdca3a52f3711f55. The nested ABS/COALESCE/IF fixed-peer precision failures and the typed numeric COM_STMT gap are fixed, including reverse operand order and prepared statement reuse. Independent public SQL counterexamples now pass 19/19 (old head: 14/19); planner package, focused embedded SQL test, and prepared BVT 48/48 twice pass. The full pkg/tests/issues package is not verified: a 20-minute overall timeout fired during an unrelated test; a longer rerun was stopped to avoid delaying delivery. This remains visible in the PR validation section. The prior comment documents the old head; these gaps are no longer known outstanding scope for #29378.

  5. added a commit that references this issue on Sep 26, 2026
    50c15c1
  6. Ariznawlll commented on Sep 27, 2026

    @Ariznawlll
    Collaborator

    测试版本

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

    原问题验证

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

    Happy Path 覆盖

    1. 精确 DECIMAL 值及嵌套 ABS 参数进入 prepared FIELD 后,均与直接执行一致,未发生 double 化精度丢失。

    Unhappy Path 覆盖

    1. 使用不相等的 2^53 边界整数,直接与 prepared 两条路径均返回 0;重复绑定后结果不漂移。

    白盒验证

    确认修复提交 50c15c1fd461fc3cd320d047b95ff3316170d468 是当前 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

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions