Skip to content

[Compatibility]: DATE_ADD, DATE_SUB, and TIMESTAMPADD preserve TIMESTAMP type instead of DATETIME #28563

Description

@Ariznawlll

现象

DATE_ADD()、DATE_SUB() 和 TIMESTAMPADD() 的输入是 TIMESTAMP 列时,MatrixOne 将结果继续解析为 TIMESTAMP;MySQL 8.0.46 将这类日期时间算术结果解析为 DATETIME。该差异会进入 view/CTAS 元数据,并改变物化结果在会话时区切换后的值。

下面使用 TIMESTAMP(0) 隔离类型问题;DATE_ADD/DATE_SUB 的小数秒精度问题已由 #28122 单独跟踪。

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

CREATE TABLE src(id INT PRIMARY KEY, ts TIMESTAMP(0), dt DATETIME(0));
INSERT INTO src VALUES
  (1, '2024-01-01 01:02:03', '2024-01-01 01:02:03');

CREATE VIEW v AS
SELECT
  DATE_ADD(ts, INTERVAL 1 SECOND) AS da_ts,
  DATE_SUB(ts, INTERVAL 1 DAY) AS ds_ts,
  TIMESTAMPADD(SECOND, 1, ts) AS ta_ts,
  DATE_ADD(dt, INTERVAL 1 SECOND) AS da_dt
FROM src;

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

MatrixOne view 元数据:

da_ts  timestamp  0
ds_ts  timestamp  0
ta_ts  timestamp  0
da_dt  datetime   0

MySQL 8.0.46:

da_ts  datetime  0
ds_ts  datetime  0
ta_ts  datetime  0
da_dt  datetime  0

DATE_ADD/DATE_SUB 还可直接物化出不同语义:

CREATE TABLE copied AS
SELECT
  DATE_ADD(ts, INTERVAL 1 SECOND) AS da_ts,
  DATE_SUB(ts, INTERVAL 1 DAY) AS ds_ts,
  DATE_ADD(dt, INTERVAL 1 SECOND) AS da_dt
FROM src;

SET time_zone = '+08:00';

SELECT CAST(da_ts AS CHAR), CAST(ds_ts AS CHAR), CAST(da_dt AS CHAR)
FROM copied;

MatrixOne 将两个 TIMESTAMP 派生列从创建时的 01:02:04 / 01:02:03 转换为 09:02:04 / 09:02:03;DATETIME 控制列不变。MySQL 的三个 CTAS 列均为 DATETIME,切换时区后都保持创建时的墙钟值。

TIMESTAMPADD() 的 CTAS 当前另受 #28562 阻塞,但 view 元数据已经能独立确认其返回类型差异。

影响

  • view/CTAS schema inference 与 MySQL 不一致;
  • MatrixOne 将日期时间算术的物化结果保存为绝对时间,跨会话时区读取时发生转换;
  • MySQL 将结果保存为 DATETIME 墙钟值,不随读取会话时区改变。

代码定位

pkg/sql/plan/function/list_builtIn.go 中以下 overload 将 TIMESTAMP 输入的返回类型声明为 T_timestamp:

  • DATE_ADD overload 4;
  • DATE_SUB overload 4;
  • TIMESTAMPADD overload 2/6。

对应执行路径也返回 types.Timestamp。相邻的 DATETIME 输入返回 DATETIME,符合 MySQL。

验证范围

  • MatrixOne: latest main ac660ae56ecd37bab3dd8c61193617a323377029
  • MySQL oracle: 8.0.46
  • DATE_ADD / DATE_SUB / TIMESTAMPADD
  • TIMESTAMP 与 DATETIME 控制
  • view 和 CTAS 元数据、+00:00 创建后在 +08:00 读取
  • 3 个全新数据库重复,结果一致

MySQL 文档明确说明 DATE_ADD/DATE_SUB 的首参数为 DATETIME 或 TIMESTAMP 时返回 DATETIME:
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add

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

    返回类型差异在 DST 春季跳时附近会进一步改变算术结果。在最新 main ac660ae56ecd37bab3dd8c61193617a323377029,会话时区设为 America/New_York:

    CREATE TABLE src(id INT PRIMARY KEY, ts TIMESTAMP(6));
    INSERT INTO src VALUES (1, '2024-03-10 01:30:00.123456');
    
    SELECT
      CAST(ts AS CHAR),
      CAST(TIMESTAMPADD(MINUTE, 30, ts) AS CHAR),
      UNIX_TIMESTAMP(TIMESTAMPADD(MINUTE, 30, ts)),
      CAST(TIMESTAMPADD(HOUR, 1, ts) AS CHAR),
      UNIX_TIMESTAMP(TIMESTAMPADD(HOUR, 1, ts))
    FROM src;

    MatrixOne:

    原值                         +30 分钟                     epoch             +1 小时                      epoch
    2024-03-10 01:30:00.123456  2024-03-10 01:00:00.123456  1710050400.123456 2024-03-10 01:30:00.123456  1710052200.123456
    

    MySQL 8.0.46:

    原值                         +30 分钟                     epoch             +1 小时                      epoch
    2024-03-10 01:30:00.123456  2024-03-10 02:00:00.123456  1710054000.123456 2024-03-10 02:30:00.123456  1710054000.123456
    

    也就是说,MatrixOne 的 +30 MINUTE 反而倒退 30 分钟,+1 HOUR 则与原值完全相同。DATE_ADD(ts, INTERVAL 1 HOUR) 和 DATE_SUB(ts, INTERVAL -1 HOUR) 走相同路径。跳时后的值、秋季重复小时和 +2 HOUR 控制组与 MySQL 一致。

    这与本 issue 的类型传播直接相关:MySQL 将这些表达式作为 DATETIME 的墙钟算术结果;MatrixOne 保留为 TIMESTAMP 后又把 DST gap 中不存在的中间墙钟值转换回瞬时,触发运行时归一化。完整场景重复 3 次一致。

  4. jiangxinmeng1 commented on Oct 22, 2026

    @jiangxinmeng1
    Contributor

    在最新 main(a949c88af0)核查后,DATE_ADD/DATE_SUB/TIMESTAMPADD 的返回类型已是 DATETIME;但 issue 中组合使用的 UNIX_TIMESTAMP(TIMESTAMPADD(...)) 在 DST 间隙仍会因 DATETIME 的隐式转换报错。修复已提交 PR #30793。按约定只在 main 验证,新 PR 尚未验证,等待合并后再在 main 复核。

  5. XuPeng-SH commented on Oct 23, 2026

    @XuPeng-SH
    Contributor

    PR #30793 synchronization update — head 669d8b451e0dc49c5e66f2b5f7a02c0401ec9a25

    The latest commit adds a native-DATETIME-only UNIX_TIMESTAMP overload selection and a legacy binding-verification mode for old persisted DATETIME expression trees. This appears to address the PR-specific UNIX_TIMESTAMP(DATE_ADD(...)) binding path and the previously reported backup/restore regression caused by changing the durable expression tree. Static review only; I have not independently run the tests.

    No PR-triggered workflow runs or combined status checks are currently associated with this exact SHA. The exact-head regression and CI results are still needed before considering this issue validated. It remains open and assigned to jiangxinmeng1; no status or assignment change.

  6. XuPeng-SH commented on Oct 23, 2026

    @XuPeng-SH
    Contributor

    PR #30793 exact-head verification — 9a8630c5549e64141d630f5e0d73d7b0f6fee1da

    The remaining linked behavior—UNIX_TIMESTAMP(DATE_ADD(...)) failing when DATE_ADD produces a typed DATETIME in a DST gap—is reported fixed on this head. XuPeng-SH and aptend both submitted APPROVED reviews. Exact-head evidence reported by the reviewers includes typed-DATETIME/DST public SQL checks, prepared timezone changes, CTAS metadata and focused function/planner tests; aptend additionally reports the original exact-base control still reproduces the failure. The overload selection is restricted to native DATETIME, so TIME remains rejected.

    This evidence is from the reviewers; I did not run the tests locally. The reviews validate planner/verifier behavior and public SQL on an embedded cluster, not a physical cross-version archive restore. Keeping this issue open for tester confirmation and assigning it to Ariznawlll.

  7. added a commit that references this issue on Oct 23, 2026
    2daeb49
  8. XuPeng-SH commented on Oct 24, 2026

    @XuPeng-SH
    Contributor

    Supplemental local SQL validation for merged PR #30793

    The loopback SQL challenge exercised the DST-gap composition described here: UNIX_TIMESTAMP(DATE_ADD(... DATETIME ...)), UNIX_TIMESTAMP(TIMESTAMPADD(... DATETIME ...)), and the DATE_SUB control all returned 1710054000 in America/New_York. Fractional-second, exact-boundary, NULL, valid-time, TIME-rejection, and concurrent New York/UTC session-isolation checks also passed; both disposable schemas were removed and read back as absent.

    Target merge SHA: 2daeb49e5abcf2b1eaaddb84d309049ec7c0486c. The single-node local service binary source was 6f773c556d4f3177f5373eb193cd576e080b662f, SHA256 51489b59a60310e79eddcf1182c582685b2c60f800f35a0a625eb5359469349b. Refreshed origin/main is 0abc1d317aea608f3a228297d68e1ae4e619a3a2; the binary was not rebuilt from that SHA, though the seven relevant planner/function/verifier source files are byte-identical at the tested source and refreshed main.

    This does not independently test the issue's TIMESTAMP-input return-type metadata, view/CTAS materialization contract, or physical cross-version DUMP/LOAD. It is scoped SQL evidence and does not claim full issue acceptance.

    Evidence: /workspace/qa-challenge/validation-main-6f773-30793.json and /workspace/qa-challenge/evidence/30793/. The issue remains open and was already assigned to Ariznawlll; no assignment change was made.

  9. XuPeng-SH commented on Oct 24, 2026

    @XuPeng-SH
    Contributor

    Additional type-metadata and CTAS check for merged PR #30793

    The prior local note covered the DST-gap composition but explicitly left the issue's timestamp-arithmetic return-type and CTAS path untested. I exercised those on the same isolated service.

    • Target merge: 2daeb49e5abcf2b1eaaddb84d309049ec7c0486c.
    • Service binary source: 6f773c556d4f3177f5373eb193cd576e080b662f, SHA256 51489b59a60310e79eddcf1182c582685b2c60f800f35a0a625eb5359469349b; relevant datetime/planner/function source files are blob-identical at pinned main 0abc1d317aea608f3a228297d68e1ae4e619a3a2. The service was not rebuilt from 0abc.
    • A view over TIMESTAMP(0) and DATETIME(0) reported datetime(0) for DATE_ADD(ts), DATE_SUB(ts), TIMESTAMPADD(..., ts), and the DATETIME control.
    • CTAS materialized both DATE_ADD(ts) and DATE_ADD(dt) as DATETIME. Reading after changing the session zone from UTC to +08:00 left both wall-clock values at 2024-01-01 01:02:04.
    • The disposable database was dropped and read back absent.

    This confirms the scoped view/CTAS behavior on the tested descendant binary; physical historical DUMP/LOAD archive compatibility and broader suites were not exercised. Issue remains open and is already assigned to Ariznawlll; assignment was left unchanged.

    Evidence: /workspace/qa-challenge/validation-main-6f773-30793.json.

  10. slyunnn commented on Oct 26, 2026

    @slyunnn

    Regression and extended verification for #28563 (2026-10-26)

    Verification Environment

    • MatrixOne latest main: 861a2c4cb68961d8b6b01f18e0aa6ac52a3a505f — Fix decimal rounding and MySQL text-to-float casts (#31191). The running binary reported git_version() = 861a2c4cb6.
    • Local topology: 2 CN / 1 TN / 1 LogService / Proxy; built from the pinned source with Go 1.27.1.
    • Proxy 16833, CN1 26151, and CN2 26152 were reachable. The original view/CTAS test was created through Proxy; metadata and materialized values were then read through all three endpoints.
    • Client: MySQL CLI over TCP using the MySQL wire protocol.

    What Was Covered

    • OK Recreated the issue's TIMESTAMP(0) and DATETIME(0) table/view case. DATE_ADD, DATE_SUB, and TIMESTAMPADD view columns all reported DATETIME(0).
    • OK CTAS materialized the TIMESTAMP arithmetic results as DATETIME(0); after changing the read session from +00:00 to +08:00, values retained their stored wall-clock representation.
    • OK Confirmed matching view metadata and CTAS readback through Proxy, CN1, and CN2.
    • OK Exercised the DST-gap composition in America/New_York: UNIX_TIMESTAMP(DATE_ADD(ts, INTERVAL 30 MINUTE)) and UNIX_TIMESTAMP(TIMESTAMPADD(HOUR, 1, ts)) returned 1710054000.123456; no conversion error occurred.
    • Not covered Physical cross-version DUMP/LOAD archive restore or the full distributed BVT suite.

    Findings

    The issue's return-type, view-metadata, and CTAS wall-clock behaviors are fixed on current main. The return-type/temporal compatibility changes are included in merged PR #28932, merge commit 0c3f7e035d90fbcf205b3164c560eda2df38335e. The DST-gap UNIX_TIMESTAMP(DATETIME) follow-up is merged as PR #30793, merge commit 2daeb49e5abcf2b1eaaddb84d309049ec7c0486c.

    MOTR / BVT Coverage

    No new MOTR case was added in this run. Focused multi-CN SQL was executed directly through the real MySQL protocol; the full distributed BVT suite was not rerun.

    Final Conclusion

    Fixed for the issue's reported return-type, view/CTAS metadata, and timezone-stable materialization behavior on the pinned latest main. Large-data regression is not needed: this is temporal result typing and timezone-conversion behavior, not a row-count or resource-boundary issue.

  11. Ariznawlll commented on Oct 26, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:官方 main
    • commit:28e8760905a7f39dba2ce436357ea75492f30512;实际双 CN 服务从该完整 SHA 构建,均返回 git_version()=28e8760905。
    • 修复:PR #28932 merge commit 0c3f7e035d90fbcf205b3164c560eda2df38335e 与 PR #30793 merge commit 2daeb49e5abcf2b1eaaddb84d309049ec7c0486c,均在加深 Git 历史后确认属于该 main 的祖先链。
    • 部署方式:本机隔离 2 CN / 1 TN / 1 LogService,经实际 MySQL 协议连接;每轮新建并删除独立测试库。
    • 测试日期:2026-10-26。

    原问题验证

    重复 3/3 次创建 issue 原始的 TIMESTAMP(0)/DATETIME(0) 源表和 VIEW。DATE_ADD(ts)、DATE_SUB(ts)、TIMESTAMPADD(...,ts) 及 DATETIME 对照列在 information_schema.columns 中均为 datetime(0),不再把前三列错误标成 timestamp。在 UTC 时区 CTAS 物化后,另一 CN 切到 +08:00 读回仍为 2024-01-01 01:02:04、2023-12-31 01:02:03、2024-01-01 01:02:04,没有 8 小时漂移。

    Happy Path 覆盖

    1. 原始 VIEW 四列类型和 CTAS 墙钟值:双 CN 交叉读回 3/3 一致。
    2. America/New_York 春季 DST gap:UNIX_TIMESTAMP(DATE_ADD(ts, INTERVAL 30 MINUTE)) 与 UNIX_TIMESTAMP(TIMESTAMPADD(HOUR,1,ts)) 均返回 1710054000.123456,3/3;没有原先的隐式转换错误。
    3. DATE_ADD(CAST(NULL AS DATETIME), INTERVAL 1 SECOND) 维持 SQL NULL;测试库删除后另一个 CN 查询已不存在。3/3。

    Unhappy Path 覆盖

    本 issue 的原始故障是合法日期时间运算返回类型及跨时区物化语义,不涉及写入拒绝或失败原子性;无应拒绝的输入可按原验收标准执行。未将合法 NULL/DST 边界伪装为错误路径。物化源表和 CTAS 均在自有数据库内创建并清理。

    白盒验证

    本轮以真实 SQL/协议确认公共行为;未新增白盒 fault 注入或 -race,因为原问题不是内部并发/资源所有权路径。未进行真实旧版本物理 DUMP/LOAD 升级。

    回归测试

    测试证据

    MOTR PR 的 SQL 与逐行期望值可复跑;隔离脚本 qa-current/verify_28563.py 在 /private/tmp/mo-six-qa-20261026 保留了 3 轮实际值及清理核查。之前 tester 的补充验证也记录在本 issue 评论中;本评论只陈述本轮独立执行结果。

    测试结论

    测试通过。

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