Skip to content

[Compatibility]: TIME functions accept and return values beyond the ±838:59:59 endpoint #28559

Closed

Description

@Ariznawlll

现象

MatrixOne 的多个 TIME 结果函数可以直接返回超出 MySQL TIME 合法范围的值。MySQL 的完整范围是 -838:59:59.000000 到 838:59:59.000000;相同输入会被截到端点并产生 1292 warning。

SET SESSION sql_mode = 'STRICT_TRANS_TABLES';

SELECT CAST(SEC_TO_TIME(3020399.000001) AS CHAR);

SELECT CAST(
  ADDTIME(CAST('838:59:58.999999' AS TIME(6)), '00:00:00.000002')
  AS CHAR
);

SELECT CAST(
  SUBTIME(CAST('-838:59:58.999999' AS TIME(6)), '00:00:00.000002')
  AS CHAR
);

SELECT CAST(
  TIMEDIFF(
    CAST('9999-12-31 23:59:59.999999' AS DATETIME(6)),
    CAST('0001-01-01 00:00:00.000000' AS DATETIME(6))
  ) AS CHAR
);

MatrixOne 返回:

838:59:59.000001
838:59:59.000001
-838:59:59.000001
87649415:59:59.999999

上述语句均没有 warning。MySQL 8.0.46 返回:

838:59:59.000000
838:59:59.000000
-838:59:59.000000
838:59:59.000000

并为越界结果产生 Warning 1292: Truncated incorrect time value。

影响

同一表达式在 SELECT、view 和 prepared statement 中能够返回非法 TIME,但写入 TIME(6) 或通过 CTAS 物化时又被 MatrixOne 的列范围检查拒绝。例如:

CREATE TABLE src(id INT PRIMARY KEY, t TIME(6));
INSERT INTO src VALUES (1, '838:59:58.999999');

SELECT CAST(ADDTIME(t, '00:00:00.000002') AS CHAR) FROM src;
-- 838:59:59.000001

CREATE TABLE copied AS
SELECT ADDTIME(t, '00:00:00.000002') AS t FROM src;
-- ERROR 1690: data out of range: data type time, value '838:59:59.000001'

这使查询结果、view/prepared 路径与物化路径使用了不同的值域契约。

代码定位

  • pkg/sql/plan/function/func_binary.go 的 addTimeToTime 和 subTimeFromTime 使用 types.ValidTime;该检查允许 MatrixOne 内部的约 25.6 亿小时范围,而不是 MySQL TIME 范围。
  • 同文件 timeDiff 也使用 types.ValidTime,溢出时会截到 types.MaxHourInTime。
  • secToTimeFromFloat64 使用 types.IsMySQLTimeFunctionResult;pkg/container/types/time.go 将函数端点定义为 838:59:59.999999,与 MySQL 文档和 MySQL 8.0.46 的实际结果不一致。
  • 另一方面,TIME 列赋值已经使用 MySQLTimeMax,因此会拒绝上述函数生成的非法值。

验证范围

  • MatrixOne: latest main ac660ae56ecd37bab3dd8c61193617a323377029
  • MySQL oracle: 8.0.46
  • sql_mode='' 与 STRICT_TRANS_TABLES 结果一致
  • 标量、表列、view、server-side prepared statement、CTAS/INSERT ... SELECT
  • 正负边界、精确端点、端点外 1 微秒、普通范围和超大 DATETIME 差值
  • 3 个全新数据库重复,现象一致

MySQL 文档说明 TIME 的完整范围为 -838:59:59.000000 到 838:59:59.000000,且 TIMEDIFF() 的结果受 TIME 范围限制:

Activity

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

    @Ariznawlll
    CollaboratorAuthor

    补充同一 TIME 输入边界在双参数 TIMESTAMP() 中的传播:

    SELECT CAST(TIMESTAMP('2024-01-01', '839:00:00') AS CHAR);

    MatrixOne latest main ac660ae56 返回:

    2024-02-04 23:00:00
    

    且没有 warning。MySQL 8.0.46 先把第二参数限制到 TIME 上界,返回:

    2024-02-04 22:59:59
    Warning 1292: Truncated incorrect time value: '839:00:00'
    

    相邻控制 TIMESTAMP('2024-01-01', '838:59:59') 两端均返回 2024-02-04 22:59:59。

    对应代码是 pkg/sql/plan/function/func_timestamp_pair.go 的 newTimestampPairTimeReader 字符串分支:它直接使用内部宽范围的 types.ParseTime 结果,没有应用 MySQL TIME 输入范围限制。因此 #28559 修复时还需要覆盖双参数 TIMESTAMP(),否则其他 TIME 结果函数修正后仍可从这里绕过范围契约。

  4. changed the title [-][Compatibility]: TIME-producing functions return values beyond the ±838:59:59 endpoint[/-] [+][Compatibility]: TIME functions accept and return values beyond the ±838:59:59 endpoint[/+] on Sep 10, 2026
  5. Ariznawlll commented on Sep 10, 2026

    @Ariznawlll
    CollaboratorAuthor

    继续扩展后确认,#28559 不只影响“生成 TIME 结果”的函数;多个接收 TIME-like 字符串的函数也绕过相同边界契约。sql_mode='' 与 STRICT_TRANS_TABLES 下均一致,3/3 重复:

    SELECT CAST(TIME('839:00:00') AS CHAR),
           CAST(TIME('-839:00:00') AS CHAR);
    
    SELECT TIME_TO_SEC('839:00:00'),
           TIME_TO_SEC('-839:00:00');
    
    SELECT TIME_FORMAT('839:00:00', '%H:%i:%s.%f'),
           TIME_FORMAT('-839:00:00', '%H:%i:%s.%f');

    MatrixOne latest main ac660ae56:

    839:00:00 | -839:00:00
    3020400   | -3020400
    839:00:00.000000 | -839:00:00.000000
    

    均无 warning。MySQL 8.0.46 先限制到正负端点:

    838:59:59 | -838:59:59
    3020399   | -3020399
    838:59:59.000000 | -838:59:59.000000
    

    每个越界参数均产生 Warning 1292。HOUR('839:00:00') 两端数值碰巧同为 838,但 MatrixOne 同样缺少越界 warning。

    另外,MAKETIME(838,59,59.000001) 和 MAKETIME(839,0,0) 已能截到正确端点,但 MatrixOne 没有 MySQL 的 1292 warning;因此修复范围还应包含“结果已经 clamp、诊断仍丢失”的路径。

    这些路径与 issue 本文的 ADDTIME/SUBTIME/TIMEDIFF/SEC_TO_TIME 共同说明:内部宽范围 Time 表示被直接暴露给多个 SQL 函数,SQL 层尚未统一应用 MySQL TIME 的输入/输出边界和诊断规则。

  6. Ariznawlll commented on Sep 10, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充生成列、二级索引、ODKU 和 ALTER 的持久化影响。非严格模式下 3/3 重复:

    SET SESSION sql_mode = '';
    
    CREATE TABLE g(
      id INT PRIMARY KEY,
      raw VARCHAR(32),
      sec BIGINT GENERATED ALWAYS AS (TIME_TO_SEC(raw)) STORED,
      INDEX idx_sec(sec)
    );
    
    INSERT INTO g(id,raw) VALUES
      (1,'838:59:59'),(2,'839:00:00'),(3,'-839:00:00');
    
    SELECT id,raw,sec FROM g ORDER BY id;
    SELECT id FROM g WHERE sec=3020400;

    MatrixOne:

    1 |  838:59:59 |  3020399
    2 |  839:00:00 |  3020400
    3 | -839:00:00 | -3020400
    
    sec=3020400 -> id 2
    

    执行计划使用 Index Table Scan on g.idx_sec,因此越界解释已经进入持久化生成列和二级索引键。

    MySQL 8.0.46 会将两个越界输入限制到正负端点并产生 1292 warning,生成列保存 ±3020399;sec=3020400 不返回行。

    以下路径继续保持同一差异:

    INSERT INTO g(id,raw) VALUES(1,'839:00:00')
    ON DUPLICATE KEY UPDATE raw=VALUES(raw);
    
    ALTER TABLE g ADD COLUMN fmt VARCHAR(64)
    GENERATED ALWAYS AS (TIME_FORMAT(raw,'%H:%i:%s.%f')) STORED;

    MatrixOne 的 ODKU 将 sec 更新为 3020400;ALTER 回填 fmt='839:00:00.000000'。MySQL 分别保存 3020399 和 838:59:59.000000 并产生越界 warning。

    这说明 #28559 不只是临时 SELECT 显示差异;错误值可以通过生成列写入存储、建立索引,并直接改变索引等值查询结果。

  7. 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: C02, C03, C32–C33 in the merged temporal design. The public TIME endpoint is ±838:59:59. Arithmetic ADDTIME/SUBTIME/TIMEDIFF beyond it returns row NULL plus warning 1441; explicit TIME conversion clamps with warning 1292; assignment follows strict/non-strict rules (including 1264/1265). Do not use MySQL arithmetic saturation as the oracle.

    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. Retest SELECT, CTAS/view, prepared execution, positive/negative endpoints and one-microsecond overflow. SEC_TO_TIME is a separate API: confirm it publishes no out-of-domain TIME, and keep this issue open if any original path remains inconsistent.

    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

  8. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

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

    原问题验证

    按 issue 原始语义在上述 commit 重新执行并重复 3 次。SEC_TO_TIME 显式转换按端点 clamp;ADDTIME/SUBTIME/TIMEDIFF 溢出返回 NULL 和 warning 1441。

    Happy Path 覆盖

    覆盖端点内、恰好端点和正负秒值。

    Unhappy Path 覆盖

    覆盖端点外、极大值、NULL 及 arithmetic overflow;区分 conversion clamp 与 arithmetic NULL。

    白盒验证

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

    回归测试

    测试证据

    • 精确测试版本:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 当前结果:SEC_TO_TIME 显式转换按端点 clamp;ADDTIME/SUBTIME/TIMEDIFF 溢出返回 NULL 和 warning 1441。
    • 回归代码与 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