Skip to content

[Bug]: Prepared ADDTIME/SUBTIME mishandle DATETIME parameters #30197

Closed

Description

@Ariznawlll

Summary

ADDTIME / SUBTIME do not handle dynamic parameters whose values are datetimes consistently with MySQL 8.0.46:

  1. With a string-bound datetime crossing midnight, MatrixOne returns an unwrapped TIME duration (or a negative duration), while MySQL returns the clock portion of the date-arithmetic result.
  2. With a DATETIME-typed user variable, MatrixOne returns ERROR 20101 due to panic wrong type for FunctionResultWrapper; MySQL executes the same prepared statements successfully.

The typed-parameter panic was reproducible twice for each function. It is distinct from #28151 (fixed panic on fractional composite TIME strings) and #28561 (result type for TIMESTAMP-column inputs).

Value mismatch reproduction

SET @d = '2024-02-29 23:30:00.123456';
PREPARE p FROM 'SELECT ADDTIME(?, ''02:00:00.000001'')';
EXECUTE p USING @d;
  • MatrixOne: 25:30:00.123457
  • MySQL 8.0.46: 01:30:00.123457

A matching negative-boundary probe (@d='2024-02-29 00:30:00.123456' and ADDTIME(?, '-02:00:00.000001')) returns -01:29:59.876545 in MatrixOne and 22:30:00.123455 in MySQL. Away from the day boundary, the returned values matched.

Panic reproduction

SET @d = CAST('2024-02-29 23:30:00.123456' AS DATETIME(6));
PREPARE p FROM 'SELECT ADDTIME(?, ''02:00:00.000001'')';
EXECUTE p USING @d;

PREPARE p FROM 'SELECT SUBTIME(?, ''02:00:00.000001'')';
EXECUTE p USING @d;

MatrixOne returns ERROR 20101 (HY000): internal error: panic wrong type for FunctionResultWrapper for both statements (stacks point to addTimeToDatetime / subTimeFromDatetime in func_binary.go). MySQL returns 01:30:00.123457 for ADDTIME and 21:30:00.123455 for SUBTIME.

Casting the marker explicitly inside the expression (ADDTIME(CAST(? AS DATETIME(6)), ...)) succeeds on both engines and returns the datetime result; this changes the resolved expression type and does not cover the uncast dynamic-parameter form.

MySQL documents that from 8.0.28, when the first argument is a dynamic parameter, ADDTIME / SUBTIME resolve to TIME: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html

Environment

  • MatrixOne: local main-derived commit 2ce4e58af6f03b943ee288ffad81012766cf830a, server reports 8.0.30-MatrixOne-v1.3.0
  • MySQL: 8.0.46
  • SQL-level server-side PREPARE / EXECUTE; no test files changed.

Activity

  1. added this to the MO-202610 milestone on Oct 14, 2026
  2. added theissue type on Oct 14, 2026
  3. added
    ai-easy
    on Oct 19, 2026
  4. Ariznawlll commented on Oct 19, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:官方 main
    • commit:8e63625c4e9baeb1d28c1095c07732f056c04895
    • 修复:fix: preserve prepared temporal result types and values #30224 MERGED,merge commit d82e828b0a4df9ce635fe573c1e90c5cde978bf9 已进入该 main。
    • 部署方式:隔离本地原生构建,1 CN / 1 TN / 1 LogService、MySQL 6001;SQL 协议真实 PREPARE/EXECUTE。
    • 测试日期:2026-10-19 UTC。

    原问题验证

    原始 prepared ADDTIME(?, '02:00:00.000001'),字符串绑定 2024-02-29 23:30:00.123456:3/3 实际 01:30:00.123457,不再错误返回 25:30:00.123457。同样输入改成 CAST(... AS DATETIME(6)) 绑定:3/3 也返回 01:30:00.123457,不再报 panic wrong type for FunctionResultWrapper。prepared SUBTIME typed 参数 3/3 返回 21:30:00.123455,没有 panic。

    Happy Path 覆盖

    1. 字符串日期 00:30:00.123456 加正两小时 3/3 为 02:30:00.123457;ADDTIME(?, '-02:00:00.000001') 越过前一日 3/3 为 22:30:00.123455;SUBTIME 同边界 3/3 也是 22:30:00.123455。
    2. 真 TIME 文本 23:30:00.123456 + 两小时的对照 3/3 为合法持续时间 25:30:00.123457,未把持续时间误转成时钟。

    Unhappy Path 覆盖

    1. 同一 prepared ADDTIME 使用 NULL、无效文本 invalid 参数,均 3/3 返回 NULL;切回有效参数后 3/3 仍正常执行。此 SELECT 路径无持久写入,未观测到会话损坏或 panic。

    白盒验证

    同 SHA 的 TestPreparedTemporalResultContracts|TestPreparedTemporalMarkerReuse|TestTemporalCompatibilityExecutionMatrix|TestDynamicTimeResultTypedCalendarAndMask 在 pkg/sql/plan/... -count=3 PASS,含 typed DATETIME 与动态参数的类型/值断言。

    回归测试

    MOTR #247 新增成对 t/issue_30195_30196_30197_prepared_temporal.test / r/*.result,当前 head 623ac129a7d011ae0d441cd4275d0100e5d8d854;定向 3/3,135/135 语句成功、0 skip,PR 未合入。MatrixOne 原有 test/distributed/cases/prepare/prepared_temporal_results.sql 也含动态参数与 NULL 断言,本轮未单独跑该 BVT。完整 659 case suite 尝试启动后中断,遗留专用库已清理;完整 suite/集成 CI 未完成。

    测试证据

    • MOTR #247 成对回归文件;定向摘要:total=3 success=3 failed=0 skipped=0 | stmts=135 ok=135 fail=0。
    • 上述越界时钟、typed 参数、NULL/错误值对照为同一完整 SHA 的本地隔离服务真实 SQL 输出;未声称历史发布线已修复。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

ai-easyarea/compatibilitykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions