Skip to content

[Bug]: prepared composite primary-key comparison converts encoded keys to DOUBLE and returns incorrect rows #29463

Closed

Description

@XuPeng-SH

Branch Name

main; also reproduced while reviewing PR #29462.

Commit ID

Reproduced on the clean pre-PR regression revision a99db843c7b4630a86883addc9424433d93fc2d2 and PR head f5b8e9f33588f7e0c7945f305c7d2de2f901a1ae in separate fresh local data directories. This is not introduced by #29462.

Environment

Linux amd64, local single-CN launch, fresh database and table; SQL PREPARE/EXECUTE. The PR review also observed the same result through JDBC server-side prepared statements.

Actual Behavior

A composite primary-key lookup with a nonnumeric text parameter returns every row for the first key component. It emits warnings for converting the encoded internal compound keys to DOUBLE. A NULL parameter also emits two such internal-key warnings. The equivalent component comparison using k2 + 0 correctly returns no rows and does not expose encoded-key warnings.

Expected Behavior

Comparisons must preserve the logical numeric column domain across compound-key optimization and runtime parameter specialization. With rows (1,2) and (1,3), neither k2 = 'invalid' (numeric prefix 0) nor k2 = NULL matches any row. Diagnostics must concern the SQL conversion; internal serialized keys must never be treated as numeric text.

Steps to Reproduce

create database composite_prepare_review;
use composite_prepare_review;
create table t(k1 int, k2 int, v varchar(12), primary key(k1,k2));
insert into t values (1,2,'two'),(1,3,'three');
prepare p from 'select v from t where k1=? and k2=? order by v';
prepare control from 'select v from t where k1=? and k2+0=? order by v';
set @a=1; set @b='invalid';
execute p using @a,@b;       -- actual: three, two; expected: empty
show warnings;             -- actual: 4 warnings, 3 mention internal encoded keys
execute control using @a,@b; -- empty, one conversion warning
set @b=null;
execute p using @a,@b;      -- empty, but two internal-key warnings
show warnings;
execute control using @a,@b; -- empty, no warnings
show warnings;
deallocate prepare p;
deallocate prepare control;
drop database composite_prepare_review;

Additional information

Discovered during the deep review of #29462. Its new issue_29429_prepared_filter.result currently accepts this incorrect behavior and must be corrected rather than used as a correctness oracle. Investigation points to composition of mergeFiltersOnCompositeKey and prepared numeric-comparison fallback in ResetParamRefRule: the outer serialized-key comparison gets converted into a DOUBLE comparison after a nested parameter conversion changes domain. Related PR: #29462; related original performance issue: #29429.

Review follow-up

A minimal change that only stops the outer DOUBLE coercion was rejected by counterexamples: k2 > '2.5' still returned the row with key 2, and later bindings such as '2' could miss matching rows. The parameter rebinder also changes the types of serialized components.

The earlier per-execution rebuild with indexes disabled was rejected: it was incomplete for VARCHAR keys and reduced TPCC throughput to about 20% of the direct-parent baseline. It is no longer the proposed fix.

The correction published in commit 74d6811d3d in #29462 binds original SQL parameter source types before key/index lowering. One bounded runtime cache holds the resulting parameterized plan and its preoptimization diagnostic candidates; QUERY retry uses the same entry. An unsafe type domain keeps scalar predicates through normal optimization, while safe integer domains retain encoded-key lookup without per-execution planning.

On 2026-09-29 the real two-CN COM_STMT_EXECUTE and SQL EXECUTE test passed equality/IN/ranges, no key/composite primary key/secondary index, invalid strings with an actual zero-key row, NULL, fractional strings/numbers, suffix strings and return to integer bindings on the same handle, including warning counts and writes. Final repeated two-CN BVT passed 500/500; three clean NVMe 10-warehouse/10-client/two-minute pairs reached 102.59% of the direct parent mean with zero errors. Final GPT-6 Astra/xhigh review passed with zero blockers. The fix is published in #29462 at 74d6811d3ddae92e1841efab3892bf841588e16f; new-head CI is tracked separately.

Activity

  1. self-assigned this
    on Sep 29, 2026
  2. added a commit that references this issue on Sep 29, 2026
    a36da7f
  3. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • Commit:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 部署方式:本地全新单 CN mo-service,独立数据目录;MySQL 文本协议与 server-side PREPARE
    • 测试日期:2026-10-02

    原问题验证

    按 issue 原始语义在上述 commit 重新执行并重复 3 次。ELT/FIELD 类索引在 invalid、NULL、2.5 时不返回错误行,整数 2 精确返回 two;warning 可观测。

    Happy Path 覆盖

    覆盖合法整数索引、字符串参数和 prepared 重绑。

    Unhappy Path 覆盖

    覆盖小数、负数、越界、NULL 与非法文本;诊断受控且 statement 可复用。

    白盒验证

    关联修复 #29462、#29466 已进入当前 main。同时将本 issue 的可公开观察契约固化为独立回归,避免只依赖实现细节或共享测试间接判断。

    回归测试

    测试证据

    • 精确测试版本:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 当前结果:ELT/FIELD 类索引在 invalid、NULL、2.5 时不返回错误行,整数 2 精确返回 two;warning 可观测。
    • 回归代码与 golden result 已提交到 MOTR #209;本地全量 14_issue_regression 中观察到的旧 GOROOT/历史 golden/并发锁冲突属于既有无关失败,以上新增用例单独重跑均稳定通过。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions