Skip to content

[Compatibility]: TIMESTAMPDIFF overcounts calendar periods at time-of-day boundaries #28569

Closed

Description

@Ariznawlll

现象

MatrixOne main(ac660ae56ecd37bab3dd8c61193617a323377029)的 TIMESTAMPDIFF() 在 MONTH、QUARTER 和 YEAR 单位下没有用完整的日期时间判断已完成的日历周期,导致同日但尚差 1 微秒的区间多算一个月,并导致小于一个月的负区间返回 -1 MONTH。

SELECT
  TIMESTAMPDIFF(MONTH,
    '2024-01-31 10:00:00.000001',
    '2024-03-31 10:00:00.000000') AS months,
  TIMESTAMPDIFF(QUARTER,
    '2024-01-31 10:00:00.000001',
    '2024-03-31 10:00:00.000000') AS quarters;

SELECT TIMESTAMPDIFF(MONTH,
  '2000-01-01 00:00:00.000000',
  '1999-12-31 23:59:59.999999');

SELECT
  TIMESTAMPDIFF(MONTH,
    '2000-02-29 12:00:00.123456',
    '2024-02-29 09:59:59.999999') AS months,
  TIMESTAMPDIFF(YEAR,
    '2000-02-29 12:00:00.123456',
    '2024-02-29 09:59:59.999999') AS years;

结果对比:

                                      MatrixOne   MySQL 8.0.46
少 1 微秒的两个月区间 (MONTH,QUARTER)  (2,0)       (1,0)
反向 1 微秒 (MONTH)                    -1          0
同月日但时刻尚未到 (MONTH,YEAR)        (288,24)    (287,23)

精确满两个月、精确满 24 年和多 1 微秒的控制组在两端一致;MICROSECOND/SECOND/MINUTE/HOUR/DAY/WEEK 的同批差分也一致。问题限定在按日历月计算并派生 QUARTER/YEAR 的路径。

期望

TIMESTAMPDIFF(MONTH) 应只计算完整经过的日历月,并按朝零方向截断负结果;比较月日边界时必须包含时分秒和微秒。QUARTER、YEAR 应基于正确的完整月数计算。

代码定位

pkg/container/types/datetime.go 的 Datetime.ConvertToMonth 只使用 Day() 计算 dayDiff,忽略两端的时间部分;当 dayDiff == 0 时直接返回名义月份差。负方向则无条件在 dayDiff < 0 时减 1,产生远离零的结果。DateTimeDiffWithUnit 的 QUARTER 和 YEAR 直接对该错误月数除以 3/12。

复现信息

  • MatrixOne main:ac660ae56ecd37bab3dd8c61193617a323377029
  • MySQL:8.0.46
  • 边界与控制组重复执行 3 次,结果一致

Activity

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

    @XuPeng-SH
    Contributor

    Triage routing: assigned to @jiangxinmeng1. Labels normalized to kind/bug + needs-triage (existing compatibility labels, when present, are retained).

    Recommended PR decomposition:

    • PR-T3 — calendar interval arithmetic (#28569 only): fix complete-month/quarter/year boundary calculation, including time-of-day and microseconds and negative intervals. Keep the arithmetic contract isolated from parsing and time-zone conversion.

    Suggested priority: P2; add boundary tests for exact, one-microsecond-before, and negative intervals.

  4. 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: C21 in the merged temporal design. TIMESTAMPDIFF YEAR/QUARTER/MONTH counts complete elapsed calendar periods using day and time remainders; valid reversed arguments negate the result.

    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 the original one-microsecond boundary and Jan31/Feb28/29 controls for typed and string inputs.

    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

  5. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

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

    原问题验证

    按 issue 原始语义在上述 commit 重新执行并重复 3 次。日期加减 MONTH/QUARTER/YEAR 在月末边界使用正确截断/进位规则。

    Happy Path 覆盖

    覆盖闰年、月末、季度和年份正负增量。

    Unhappy Path 覆盖

    覆盖 0001/9999 越界、NULL 与非法 interval;返回受控 NULL/诊断。

    白盒验证

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

    回归测试

    测试证据

    • 精确测试版本:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 当前结果:日期加减 MONTH/QUARTER/YEAR 在月末边界使用正确截断/进位规则。
    • 回归代码与 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