Skip to content

[Compatibility]: ADDTIME and SUBTIME keep TIMESTAMP type instead of returning DATETIME #28561

Closed

Description

@Ariznawlll

现象

当 ADDTIME() 或 SUBTIME() 的第一个参数是 TIMESTAMP 列时,MatrixOne 将函数结果解析为 TIMESTAMP;MySQL 8.0.46 将结果解析为 DATETIME。该类型差异会进入 view 和 CTAS 元数据,并使已经物化的 CTAS 值随后受到会话时区转换。

CREATE DATABASE addsub_ts_repro;
USE addsub_ts_repro;
SET time_zone = '+00:00';

CREATE TABLE src(
  id INT PRIMARY KEY,
  ts TIMESTAMP(6),
  dt DATETIME(6),
  tm TIME(6)
);

INSERT INTO src VALUES
  (1, '2024-01-01 01:02:03.123456',
      '2024-01-01 01:02:03.123456',
      '01:02:03.123456');

CREATE TABLE copied AS
SELECT
  ADDTIME(ts, '00:00:01.000001') AS add_ts,
  SUBTIME(ts, '00:00:01.000001') AS sub_ts,
  ADDTIME(dt, '00:00:01.000001') AS add_dt,
  ADDTIME(tm, '00:00:01.000001') AS add_tm
FROM src;

SELECT column_name, data_type, datetime_precision
FROM information_schema.columns
WHERE table_schema = 'addsub_ts_repro' AND table_name = 'copied'
ORDER BY ordinal_position;

SELECT CAST(add_ts AS CHAR), CAST(sub_ts AS CHAR),
       CAST(add_dt AS CHAR), CAST(add_tm AS CHAR)
FROM copied;

SET time_zone = '+08:00';

SELECT CAST(add_ts AS CHAR), CAST(sub_ts AS CHAR),
       CAST(add_dt AS CHAR), CAST(add_tm AS CHAR)
FROM copied;

MatrixOne 的 CTAS 元数据为:

add_ts  timestamp  6
sub_ts  timestamp  6
add_dt  datetime   6
add_tm  time       6

MySQL 8.0.46 的 CTAS 元数据为:

add_ts  datetime  6
sub_ts  datetime  6
add_dt  datetime  6
add_tm  time      6

在 +00:00 创建 CTAS 后切换到 +08:00,MatrixOne 中两个 *_ts 值变化 8 小时:

-- +00:00
2024-01-01 01:02:04.123457  2024-01-01 01:02:02.123455

-- +08:00
2024-01-01 09:02:04.123457  2024-01-01 09:02:02.123455

MySQL 的 CTAS 列为 DATETIME(6),切换会话时区后保持 01:02:04.123457 / 01:02:02.123455。DATETIME 和 TIME 输入列作为相邻控制,两端类型和值一致。

影响

  • view/CTAS 客户端读取到不同的列类型;
  • 基于 ADDTIME(TIMESTAMP, ...) 或 SUBTIME(TIMESTAMP, ...) 物化的数据,在 MatrixOne 中成为绝对时间并随读取会话时区变化,而 MySQL 将其保存为不做时区转换的 DATETIME;
  • 下游 schema inference、ETL 和跨时区查询会得到不同结果。

代码定位

  • pkg/sql/plan/function/list_builtIn.go 中 ADDTIME/SUBTIME 的 TIMESTAMP overload(4/5)将返回类型声明为 T_timestamp。
  • pkg/sql/plan/function/func_binary.go 的 addTimeToTimestamp / subTimeFromTimestamp 将结果重新转换为 types.Timestamp。
  • 相邻的 DATETIME 和 TIME overload 与 MySQL 一致,问题仅出现在 TIMESTAMP overload。

验证范围

  • MatrixOne: latest main ac660ae56ecd37bab3dd8c61193617a323377029
  • MySQL oracle: 8.0.46
  • ADDTIME / SUBTIME
  • 标量、view、CTAS、server-side prepared statement
  • TIMESTAMP(6)、DATETIME(6)、TIME(6) 类型控制
  • +00:00 创建并在 +08:00 读取
  • 3 个全新数据库重复,结果一致

MySQL 的时间函数文档:
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_addtime

Activity

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

    @jiangxinmeng1
    Contributor

    已在远程机器 [email protected] 对 clean main(a949c88af0ed)运行 SQL 复现:ADDTIME/SUBTIME 对 TIMESTAMP 的 CTAS 结果为 DATETIME(6),且时间值在时区切换下保持预期;该 issue 的类型行为已修复。转给 Ariznawlll 跟进。

  4. Ariznawlll commented on Oct 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:官方 main
    • commit:cca94b810ce87a38d1af16d7b418ea727eeda81c(包含已合入修复 fix remaining date/time compatibility issues #28932 的 merge commit)
    • 部署方式:本地隔离工作树,1 LOG / 1 TN / 2 CN;通过 CN0 的真实 MySQL TCP 黑盒测试
    • 测试日期:2026-10-22

    原问题验证

    ADDTIME/SUBTIME 作用于 TIMESTAMP(6) 后 CTAS 均为 DATETIME,结果分别为 2024-01-01 01:02:04.123457、2024-01-01 01:02:02.123455。 独立启动环境重复 3/3 轮通过,未用开发者自测代替 QA。

    Happy Path 覆盖

    1. DATETIME(6) 输入保持 DATETIME,TIME(6) 输入保持 TIME;切换 session 时区 +08 后物化 DATETIME 值不变。
    2. 新增成对 SQL 回归定向重复 3/3 通过,结果与独立 Go 黑盒断言核对。

    Unhappy Path 覆盖

    1. 未发生 TIMESTAMP 类型泄漏;未额外执行溢出 TIME 输入矩阵。
    2. 每轮在独立临时数据库执行并 DROP 清理;失败场景无残留表数据或错误 golden。

    白盒验证

    本轮未单独跑 Go UT / race;本问题为确定性 SQL 值或类型路径,以真实协议黑盒及 SQL 回归验收。完整 MatrixOne suite 未执行。

    回归测试

    • MOTR 汇总 PR #247,本项所在 t 用例 及同名 r 预期结果;定向 --repeat 3 通过。PR 当前仍 Open,尚未声称已合并或 CI 全绿。
    • 未运行完整 MOTR suite、TKE/集成 CI;不据此声称其他发布线通过。

    测试证据

    上述 PR 的成对 t/r、实际 main SHA,以及本地 go test ./pkg/tests/issues -run '^TestQATemporalScreenshot20261022$' -count=3 与 MOTR 定向 --repeat 3 均通过;黑盒临时测试仅用于本次独立验收,未作为持久产品代码提交。

    测试结论

    测试通过。

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