Skip to content

[Compatibility]: TIMESTAMPADD SECOND rounds DECIMAL intervals to whole seconds #30203

Closed

Description

@Ariznawlll

现象

MatrixOne 将 TIMESTAMPADD(SECOND, decimal_value, datetime) 的 DECIMAL interval 换算成整数秒;MySQL 8.0.46 接受相同 DECIMAL 输入并保留秒的小数部分(最多 6 位)。差异在普通 SELECT 和 DECIMAL 列驱动执行中均出现,不依赖 CTAS 或 PREPARE。

CREATE TEMPORARY TABLE src(
  dt DATETIME(6),
  n DECIMAL(4,1)
);
INSERT INTO src VALUES
  ('2024-01-01 00:00:00.000000', 1.4),
  ('2024-01-01 00:00:00.000000', 1.5),
  ('2024-01-01 00:00:00.000000', 1.6),
  ('2024-01-01 00:00:00.000000',-1.4),
  ('2024-01-01 00:00:00.000000',-1.5),
  ('2024-01-01 00:00:00.000000',-1.6);
SELECT n, CAST(TIMESTAMPADD(SECOND,n,dt) AS CHAR)
FROM src ORDER BY n;

结果:

n      MatrixOne                    MySQL 8.0.46
-1.6   2023-12-31 23:59:58.000000  2023-12-31 23:59:58.400000
-1.5   2023-12-31 23:59:58.000000  2023-12-31 23:59:58.500000
-1.4   2023-12-31 23:59:59.000000  2023-12-31 23:59:58.600000
 1.4   2024-01-01 00:00:01.000000  2024-01-01 00:00:01.400000
 1.5   2024-01-01 00:00:02.000000  2024-01-01 00:00:01.500000
 1.6   2024-01-01 00:00:02.000000  2024-01-01 00:00:01.600000

TIMESTAMPADD(MICROSECOND, n, dt) 的 DECIMAL 小数控制在两端一致;目前差异集中在 SECOND unit 中小数秒被 MO 舍入为整数秒。

期望

DECIMAL 秒 interval 应保留 MySQL 8.0.46 的小数秒结果;正负 interval 均应使用相同精度规则。

与已有 issue 的关系

  • #30195 跟踪 Prepared CTAS 中源参数时间值的持久化错误;本复现是直接 SELECT 中 DECIMAL interval 的秒精度丢失。
  • #28119 跟踪 DATE_ADD/SUB 的 PREPARE 参数绑定限制;本复现不使用 PREPARE。

兼容性契约 README 和相关 issue 搜索未发现此 DECIMAL 秒 interval 限制。

环境

  • MatrixOne:2ce4e58af6f03b943ee288ffad81012766cf830a,8.0.30-MatrixOne-v1.3.0
  • MySQL:8.0.46
  • 测试日期:2026-10-14

Activity

  1. added this to the MO-202610 milestone on Oct 14, 2026
  2. added
    kind/bug
    on Oct 14, 2026
  3. added theissue type on Oct 14, 2026
  4. Ariznawlll commented on Oct 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充生成列 + 二级索引交叉:

    CREATE TABLE decimal_tsadd_generated(
      id INT PRIMARY KEY,
      dt DATETIME(6),
      n DECIMAL(2,1),
      calc DATETIME(6) AS (TIMESTAMPADD(SECOND,n,dt)) STORED,
      INDEX idx_calc(calc)
    );
    INSERT INTO decimal_tsadd_generated(id,dt,n) VALUES
      (1,'2024-01-01 00:00:00.000000',1.4),
      (2,'2024-01-01 00:00:00.000000',1.5),
      (3,'2024-01-01 00:00:00.000000',1.6);
    SELECT id,calc FROM decimal_tsadd_generated ORDER BY id;
    SELECT id FROM decimal_tsadd_generated
    WHERE calc='2024-01-01 00:00:01.500000';

    MatrixOne 保存 01.000000, 02.000000, 02.000000 秒,MySQL 8.0.46 保存 01.400000, 01.500000, 01.600000 秒。查询 calc='...01.500000' 在 MO 无结果、MySQL 返回 id=2;MO 执行计划使用 idx_calc。这确认 DECIMAL 秒小数丢失会进入 STORED generated value 和索引查找结果。

  5. Ariznawlll commented on Oct 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充 SQL PREPARE 复用验证:PREPARE ts_add FROM SELECT TIMESTAMPADD(SECOND, ?, CAST(... AS DATETIME(6))) 后分别以 DECIMAL(2,1) 的 1.5 和 -1.5 执行,MO 结果为 +2.000000s/-2.000000s,MySQL 8.0.46 为 +1.500000s/-1.500000s。直接 SELECT、DECIMAL 列、SQL PREPARE 三条路径同向复现。

  6. Ariznawlll commented on Oct 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充 interval 参数静态类型对照:

    SELECT CAST(TIMESTAMPADD(SECOND,CAST(1.5 AS DECIMAL(2,1)),
                             '2024-01-01 00:00:00.000000') AS CHAR);
    SELECT CAST(TIMESTAMPADD(SECOND,CAST(1.5 AS DOUBLE),
                             '2024-01-01 00:00:00.000000') AS CHAR);
    SELECT CAST(TIMESTAMPADD(SECOND,'1.5',
                             '2024-01-01 00:00:00.000000') AS CHAR);

    MO 分别返回 02.000000、01.000000、01.000000;MySQL 8.0.46 三种输入均返回 01.500000。因此 MO 的 DECIMAL 路径将小数秒舍入为整数秒,DOUBLE/字符串路径则截掉小数秒;MySQL 均保留到微秒。原 issue 中 DECIMAL 列结果与此一致。

  7. Ariznawlll commented on Oct 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    邻近函数控制:DATE_ADD/DATE_SUB 的 DECIMAL SECOND interval

    检查同一秒级 DECIMAL interval 在相邻 API 中的行为,发现 DATE_ADD/DATE_SUB/ADDDATE 当前保留小数秒,与 MySQL 一致:

    SELECT DATE_ADD(CAST('2024-01-01 00:00:00.000000' AS DATETIME(6)), INTERVAL 1.4 SECOND);
    SELECT DATE_SUB(CAST('2024-01-01 00:00:00.000000' AS DATETIME(6)), INTERVAL 1.4 SECOND);
    SELECT DATE_ADD(CAST('2024-01-01 00:00:00.000000' AS DATETIME(6)), INTERVAL 0.000001 SECOND);
    SELECT ADDDATE(CAST('2024-01-01 00:00:00.000000' AS DATETIME(6)), INTERVAL 1.5 SECOND);

    MatrixOne 8.0.30-MatrixOne-v1.3.0 与 MySQL 8.0.45 分别得到 00:00:01.400000、前一日 23:59:58.600000、00:00:00.000001 和 00:00:01.500000。这组控制把当前差异进一步限定在 TIMESTAMPADD(SECOND, DECIMAL, ...) 路径,并未发现 DATE_ADD/SUB 的同类值差异。

  8. added
    ai-easy
    on Oct 20, 2026
  9. slyunnn commented on Oct 22, 2026

    @slyunnn

    Regression and extended verification for #30203 (2026-10-22)

    Verification Environment

    • Source: latest origin/main, commit c232b6d2a1b5a94ac624c4954586569841c1e153, title Fix mixed JSON and numeric comparisons (#30707). The MatrixOne binary was built from this commit with Go 1.27.1.
    • Topology: 2 CN / 1 TN / 1 LogService / Proxy.
    • Proxy 6821, CN1 16121, and CN2 16122 were each reachable and tested directly.
    • Client: MySQL CLI over the MySQL client protocol. Prepared execution used server-side PREPARE/EXECUTE. Reference: MySQL 8.0.46. Comparison sessions used timezone +00:00 and matching sql_mode where applicable.

    What Was Covered

    • OK — Original DECIMAL-second behavior with positive and negative column values (1.4, 1.5, 1.6, -1.4, -1.5, -1.6) matches MySQL on Proxy, CN1, and CN2; fractional seconds are preserved.
    • OK — DECIMAL literal and server-side prepared execution with -1.5 match MySQL.
    • OK — NULL propagation, integer/DOUBLE/string controls, and the MICROSECOND control match MySQL.
    • OK — Boundary checks including 0.000001, sub-microsecond 0.0000009, and carry across the year boundary match MySQL.
    • OK — Out-of-range overflow returns NULL with warning code 1441 on both systems.
    • OK — STORED generated-column values match; the FORCE INDEX(added_idx) lookup returns id=2 on all MatrixOne endpoints and MySQL. MatrixOne's plan uses an Index Table Scan on added_idx.
    • Not covered — The complete distributed BVT harness was not run. The targeted SQL cases above were executed directly against the running service.

    Findings

    The associated fix is merged PR #30286, merge commit 743e749a1e33d6c845bb73999e8c82d297a2b657, titled fix(sql): preserve fractional TIMESTAMPADD SECOND intervals. It preserves fractional TIMESTAMPADD(SECOND, ...) values through typed normalization and execution paths.

    The current test/distributed/cases/function/func_datetime_timestampadd.result expects no row for the FORCE INDEX(added_idx) lookup, while direct execution on latest main and MySQL both return id=2. This is a stale expected-result artifact, not a remaining runtime failure. No MatrixOne source or test file was modified. No new MOTR case was needed because the existing distributed SQL test covers the behavior.

    Large-data regression is not needed: this is deterministic temporal arithmetic precision, independent of data volume.

    MOTR / BVT Coverage

    The relevant coverage exists in test/distributed/cases/function/func_datetime_timestampadd.test. Its targeted cases were exercised directly across Proxy/CN1/CN2, including the generated-column/index path. The full BVT harness was not run; the checked-in .result expectation for the forced-index lookup should be updated before claiming a full BVT pass. No MOTR test or commit was added.

    Final Conclusion

    Fixed — the reported fractional-second loss in TIMESTAMPADD(SECOND, DECIMAL, ...) is fixed on latest main and matches MySQL 8.0.46 across Proxy, CN1, and CN2. The only remaining item is the stale distributed-test expected result described above; it does not change the runtime conclusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-easyarea/compatibilitykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions