Skip to content

[Bug]: Server-side prepared overloaded functions reject valid non-integer bound values #27294

Closed

Description

@Ariznawlll

Description

When Connector/J uses a real server-side prepared statement (useServerPrepStmts=true), overloaded numeric functions are bound to an integer overload before the value type from COM_STMT_EXECUTE is available. Valid floating-point/decimal parameters are then rejected. This affects at least sleep(?) and abs(?).

The equivalent SQL literal, Connector/J client-side prepared statement, and server-side prepared statement with an explicit CAST(? AS DOUBLE) all succeed.

Environment

  • Branch: main
  • Commit: 99d03a52f78b9b42243c169e6d554fafd32f73c4
  • Deployment: local standalone launch, SQL port 26001
  • Client: MySQL Connector/J 8.0.33 and 8.0.15
  • Date: 2026-08-18

Steps to reproduce

Connect with:

jdbc:mysql://127.0.0.1:26001/?useServerPrepStmts=true&useSSL=false&serverTimezone=UTC

Then execute:

try (PreparedStatement ps = conn.prepareStatement("select sleep(?)")) {
    System.out.println(ps.getClass().getSimpleName()); // ServerPreparedStatement
    ps.setDouble(1, 0.05d);
    ps.executeQuery();
}

try (PreparedStatement ps = conn.prepareStatement("select abs(?)")) {
    ps.setDouble(1, -1.5d);
    ps.executeQuery();
}

Controls:

select sleep(0.05); -- succeeds and returns 0
select abs(-1.5);    -- succeeds and returns 1.5

The following JDBC controls also succeed:

// useServerPrepStmts=false
prepareStatement("select sleep(?)").setDouble(1, 0.05d);

// useServerPrepStmts=true
prepareStatement("select sleep(cast(? as double))").setDouble(1, 0.05d);
prepareStatement("select abs(cast(? as double))").setDouble(1, -1.5d);

Actual behavior

sleep(?) with setDouble, setFloat, setBigDecimal, or setString("0.05") fails:

SQLState=HY000 errorCode=20203
invalid argument cast to uint64, bad value 0.05

abs(?) with setDouble(-1.5) or setBigDecimal(-1.5) fails:

SQLState=HY000 errorCode=20203
invalid argument cast to int, bad value -1.5

Expected behavior

The server-side prepared forms should select or rebind the overload using the bound parameter's numeric type and produce the same result as the literal/client-prepared controls:

  • sleep(0.05) returns 0 after approximately 50 ms.
  • abs(-1.5) returns 1.5.

Stability and controls

  • Reproducer: 3/3 on the listed latest official main commit; identical result with Connector/J 8.0.33 and 8.0.15.
  • Normal controls: SQL literals, client-side Prepare, integer server-side Prepare, and explicit CAST(? AS DOUBLE) all pass.
  • Scope check: reproduced with two separate overloaded functions (sleep and abs), so this is not specific to sleep timing.
  • Post-failure state: the same connection and the same prepared statement remain reusable after rebinding an integer value; no connection corruption was observed.
  • Failure atomicity/data check: not applicable; the minimal reproducer is read-only.

Evidence

Fresh 3-round verification on 99d03a52f78b9b42243c169e6d554fafd32f73c4 produced the same matrix in every round:

PASS literal sql=select sleep(0.05) value=0
PASS client-double implementation=ClientPreparedStatement value=0
PASS server-int implementation=ServerPreparedStatement value=0
FAIL server-double implementation=ServerPreparedStatement state=HY000 code=20203 message=invalid argument cast to uint64, bad value 0.05
FAIL server-decimal implementation=ServerPreparedStatement state=HY000 code=20203 message=invalid argument cast to uint64, bad value 0.05
PASS server-cast-double implementation=ServerPreparedStatement value=0
PASS server-rebind int-double-int connection-and-statement-reusable
PASS literal sql=select abs(-1.5) value=1.5
FAIL abs-server-double implementation=ServerPreparedStatement state=HY000 code=20203 message=invalid argument cast to int, bad value -1.5
PASS abs-server-cast-double implementation=ServerPreparedStatement value=1.5

The public source commit used for verification is 99d03a52.

Code analysis

Confirmed observations:

  • baseBindParam initially represents a prepared parameter as T_text.
  • sleep has both uint64 and float64 overloads, with the integer overload listed first.
  • ParseExecuteData reads the binary protocol parameter type/value, but stores every value into a string vector.
  • ResetParamRefRule preserves the prepared expression's existing type when replacing the parameter value and only directly rebinds a function when one of its immediate arguments is a ParamRef.

Hypothesis: the initial T_text parameter makes fixedTypeMatch choose the first/lowest-cost integer overload and insert an implicit cast. That cast wraps the ParamRef, so execute-time replacement does not rebind the outer overloaded function using the actual COM_STMT_EXECUTE numeric type. The bound decimal value is consequently forced through the previously selected integer cast.

Regression coverage

Add a protocol/integration regression using a real COM_STMT_PREPARE + COM_STMT_EXECUTE path (not SQL PREPARE/EXECUTE text commands):

  1. sleep(?) with integer, float/double, decimal and string representations.
  2. abs(?) with negative integer, double and decimal values.
  3. Rebind one statement across integer → double/decimal → integer.
  4. Assert literal and explicit-cast controls, exact values, error recovery, and statement/connection reuse.

Related

Activity

  1. added this to the MO-202608 milestone on Aug 18, 2026
  2. added
    kind/bug
    on Aug 18, 2026
  3. added theissue type on Aug 18, 2026
  4. Ariznawlll commented on Aug 23, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在最新 main(cdf7888344df65136f6d0c9c35ead4940d69a793)验证修复。

    验证内容:

    • Connector/J 8.0.33 真实 server-prepared 下,sleep(?) 的 DOUBLE、DECIMAL 与数字字符串参数均可执行并返回 0。
    • abs(?) 覆盖整数、DOUBLE、DECIMAL 及同一 PreparedStatement 的整数 -> 小数 -> 整数复用,结果正确;会话保持可用。
    • 连续 3 轮通过。

    已补充 MOTR 回归:matrixorigin/motr#117(issue_27294_prepared_numeric_overloads),使用真实 COM_STMT_PREPARE/COM_STMT_EXECUTE 覆盖 sleep/abs 的浮点与数字文本绑定及重用;PR 内 7 个场景连续 3 轮共 21/21 通过。

  5. Ariznawlll commented on Aug 24, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:53f6aa6f0bbdc59f924072e823cdcd48ff43a981
    • 部署方式:TKE nightly MOTR Regression Test Suite
    • 测试日期:2026-08-24

    原问题验证

    该 issue 曾在较早的 main commit cdf7888344df65136f6d0c9c35ead4940d69a793 上关闭。当前 nightly 在更新后的 main 上重新执行同一 binary-protocol regression,COM_STMT_EXECUTE 绑定小数参数 0.01 到 SLEEP(?) 仍失败:

    Error 20203 (HY000): invalid argument cast to uint64, bad value 0.01
    

    文本 SQL/显式 CAST(? AS DOUBLE) 可工作,但 server-side prepared statement 仍把实际小数参数走整数转换。这是 prepared overload 重新绑定/类型处理仍未修复,不能通过修改 MOTR result 掩盖。

    Happy Path 覆盖

    本次回归覆盖的正常控制为文本 SQL 与显式 DOUBLE CAST;失败后连接和测试进程没有出现 panic、重启或中断。

    Unhappy Path 覆盖

    不适用:本次验证为只读的参数类型兼容性校验。

    白盒验证

    不适用:该结论来自真实 binary protocol 回归执行,当前需要研发定位 overload 选择与 execute-time 参数类型重绑定路径。

    回归测试

    MOTR:14_issue_regression.issue_27294_prepared_numeric_overloads,已在本次 nightly 执行。

    测试证据

    测试结论

    测试不通过。

  6. Ariznawlll commented on Sep 7, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:bcd5ed833b5400fe95f322e4028adb2bde3de0a4
    • 部署方式:本地 standalone MatrixOne,MySQL binary protocol 端口 6001
    • 测试日期:2026-09-07

    原问题验证

    1. 使用真实 COM_STMT_PREPARE/COM_STMT_EXECUTE,通过同一物理连接执行 sleep(?) 的整数、浮点和数值字符串绑定。
    2. 重用 abs(?) prepared handle,在整数和小数参数之间切换,核对返回类型和值。
    3. 连续执行场景并在每组操作后检查连接可用性。

    Happy Path 覆盖

    1. sleep(?) 正确接受整数、浮点和 numeric-text 绑定并返回预期结果。
    2. abs(?) 在整数/小数重用时重新选择正确重载,数值不被截断。

    Unhappy Path 覆盖

    1. 参数类型跨执行切换,未复用错误的旧重载或返回错误类型。
    2. 每次执行后连接仍可复用,无 EOF、panic 或 prepared 状态污染。

    白盒验证

    不适用。本轮验证真实 binary protocol prepared 行为,未使用内部故障注入。

    回归测试

    已纳入 MOTR PR #145:script/14_issue_regression/issue_27294_prepared_numeric_overloads.sh 及对应 scenario。

    测试证据

    测试结论

    测试通过。

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