Skip to content

[Compatibility]: TIMEDIFF subtracts mixed datetime and time strings as calendar values #28501

Closed

Description

@Ariznawlll

Problem

TIMEDIFF() in MatrixOne calculates a large TIME value when one string operand is a datetime and the other is a time. MySQL requires both operands to have the same temporal kind and returns NULL for a datetime/time mixture.

Tested on MatrixOne main commit f0c31cd4b830be32442cf329e0a3fb08aa9c16c3 and MySQL 8.3.0. Each MatrixOne reproduction was repeated three times.

Minimal reproduction

SELECT TIMEDIFF('2024-01-02 12:00:00','01:00:00');
SELECT TIMEDIFF('12:00:00','2024-01-01 01:00:00');

MatrixOne returns:

-23533:00:00.000000
 23579:00:00.000000

MySQL returns NULL for both expressions without a warning.

Row, prepared, and persistence coverage

The same values are produced when the operands come from mixed VARCHAR rows and when they are supplied through SQL PREPARE/EXECUTE.

A view preserves the invalid large values as TIME results. CTAS introduces an additional corruption step because the values exceed the persistable MySQL TIME range: MatrixOne emits range warnings and stores clamped values around -838:59:59 / 838:59:59, while MySQL stores NULL.

Controls

  • Two time strings return the same signed, microsecond-preserving difference in both systems.
  • Two full datetime strings return the same difference in both systems.
  • Invalid strings and NULL operands produce NULL in both systems.
  • MatrixOne remains healthy after the vector, prepared, view, and CTAS cases.

Code analysis

TimeDiffString in pkg/sql/plan/function/func_binary.go parses each string independently. If datetime parsing fails, it parses the value as TIME and immediately calls Time.ToDatetime, which attaches Today(time.UTC).

The function then subtracts the two DATETIME values without retaining or comparing their original temporal kinds. A datetime/time mixture therefore becomes a real calendar-date subtraction instead of an incompatible pair.

Expected behavior

TIMEDIFF() should only subtract operands of the same temporal kind. When string inputs resolve to a datetime/time mixture, it should return NULL consistently for literal, column, prepared, view, and CTAS paths.

Activity

  1. added this to the MO-202609 milestone on Sep 9, 2026
  2. added theissue type on Sep 9, 2026
  3. XuPeng-SH commented on Sep 27, 2026

    @XuPeng-SH
    Contributor

    QA handoff after merged PR #28851 (merge a99db843c7b4630a86883addc9424433d93fc2d2). This issue was a non-closing related report; the merge and shared BVT counts are not an independent PASS for this reproducer.

    Latest MatrixOne contract: C08, C19 in the merged temporal design. TIMEDIFF on mixed duration and calendar strings returns NULL; same-family operands use checked subtraction and a TIME result with the bound FSP.

    Please rerun the original reproducer on main at or after the merge and record the exact SHA, deployment, SQL mode/time zone, client path, observed rows/types/NULLs and active warnings or errors. Rerun both mixed directions, valid same-family controls, malformed/NULL inputs and metadata.

    Keep the issue open until the issue-specific acceptance is demonstrated with happy and unhappy controls. If any part still fails, post the exact observed-versus-expected result and return it to development; if it passes, record evidence and close the issue. @Ariznawlll

  4. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

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

    原问题验证

    按 issue 原始语义在上述 commit 重新执行并重复 3 次。TIMEDIFF 对 TIME 与 DATETIME 混合 family 返回 NULL;同 family 仍返回正确 TIME 差值。

    Happy Path 覆盖

    覆盖 TIME-TIME、DATETIME-DATETIME 与不同 FSP。

    Unhappy Path 覆盖

    覆盖两个混合方向、NULL 和越界;均受控返回,不再按日历错误相减。

    白盒验证

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

    回归测试

    测试证据

    • 精确测试版本:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 当前结果:TIMEDIFF 对 TIME 与 DATETIME 混合 family 返回 NULL;同 family 仍返回正确 TIME 差值。
    • 回归代码与 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

area/compatibilitykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions