Skip to content

[Bug]: Prepared CTAS with TIMESTAMPADD(MICROSECOND) persists the wrong timestamp #30195

Closed

Description

@Ariznawlll

Summary

A prepared CTAS using TIMESTAMPADD(MICROSECOND, 1, ?) succeeds in MatrixOne but persists a different timestamp: the bound time-of-day and the interval increment are both lost. The same prepared expression returns the correct value when selected directly or used by INSERT ... SELECT; adding an explicit CAST(? AS DATETIME(6)) also makes the CTAS result correct. MySQL 8.0.46 persists the correct value without the cast.

This is a persisted-value correctness issue, distinct from #28562 (CTAS with a source-column TIMESTAMPADD fails during generated SQL reparsing) and from closed #27379 (microsecond result scale on ordinary temporal inputs).

Reproduction

CREATE DATABASE repro;
USE repro;
SET @d = '2024-01-31 12:34:56.000001';
PREPARE p FROM
  'CREATE TABLE c AS
   SELECT TIMESTAMPADD(MICROSECOND, 1, ?) AS v';
EXECUTE p USING @d;

SELECT v, CAST(v AS CHAR) FROM c;
  • MatrixOne stores 2024-01-31 00:00:00.000001.
  • MySQL 8.0.46 stores 2024-01-31 12:34:56.000002.

The MatrixOne behavior repeated with three input timestamps (including a leap-day value and a 23:59:59.999999 rollover case). Controls on the same MatrixOne commit:

  • PREPARE ... SELECT TIMESTAMPADD(MICROSECOND, 1, ?) returns the correct timestamp.
  • PREPARE ... INSERT INTO target SELECT TIMESTAMPADD(MICROSECOND, 1, ?) stores the correct timestamp.
  • Prepared CTAS with TIMESTAMPADD(MICROSECOND, 1, CAST(? AS DATETIME(6))) stores the correct timestamp.

Environment

  • MatrixOne: local main-derived commit 2ce4e58af6f03b943ee288ffad81012766cf830a, server reports 8.0.30-MatrixOne-v1.3.0
  • MySQL: 8.0.46
  • SQL-level server-side PREPARE / EXECUTE; temporary databases removed after comparison.

Activity

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

    @Ariznawlll
    CollaboratorAuthor

    补充参数日期部分的区间对照,输入均为 2024-01-31 12:34:56.123456:

    • TIMESTAMPADD(HOUR, 1, ?):MatrixOne CTAS 写入 2024-01-31 01:00:00;MySQL 写入 2024-01-31 13:34:56.123456。
    • TIMESTAMPADD(DAY, 1, ?):MatrixOne CTAS 写入 2024-02-01 00:00:00;MySQL 写入 2024-02-01 12:34:56.123456。

    这与正文的 MICROSECOND 用例一致:原始参数在 prepared CTAS 路径中没有按完整 datetime 保留。

  4. Ariznawlll commented on Oct 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充双 marker 用例:@n=1, @d='2024-01-31 12:34:56.123456' 时,标量 prepared SELECT TIMESTAMPADD(DAY, ?, ?) 在 MatrixOne 返回 2024-02-01 12:34:56,MySQL 返回 2024-02-01 12:34:56.123456;同一表达式放入 prepared CTAS 后,MatrixOne 仅写入 2024-02-01,MySQL 仍写入完整 datetime。说明 interval marker 与 datetime marker 同时出现时也会走到该 CTAS 数据差异。

  5. added
    ai-easy
    on Oct 19, 2026
  6. Ariznawlll commented on Oct 19, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:官方 main
    • commit:8e63625c4e9baeb1d28c1095c07732f056c04895
    • 修复:fix: preserve prepared temporal result types and values #30224 已 MERGED,merge commit d82e828b0a4df9ce635fe573c1e90c5cde978bf9 经 GitHub compare 确认为上述 main 的祖先。
    • 部署方式:隔离本地目录原生构建,1 CN / 1 TN / 1 LogService,MySQL 协议端口 6001;新建专用库,结束后删除。
    • 测试日期:2026-10-19 UTC。

    原问题验证

    原始 SQL PREPARE ... CREATE TABLE c AS SELECT TIMESTAMPADD(MICROSECOND,1,?) / EXECUTE USING @d 独立运行 3/3:输入 2024-01-31 12:34:56.000001,持久化实际值 2024-01-31 12:34:56.000002,列类型 datetime(6)、精度 6;不再出现旧错值 2024-01-31 00:00:00.000001。闰日末尾输入 2024-02-29 23:59:59.999999 时 3/3 持久化为 2024-03-01 00:00:00.000000。

    Happy Path 覆盖

    1. 双 marker prepared CTAS TIMESTAMPADD(DAY,?,?):3/3 为 2024-02-01 12:34:56.123456;HOUR CTAS:3/3 为 2024-01-31 13:34:56.123456。时间和小数秒未丢失。
    2. prepared 标量 SELECT 和 INSERT SELECT 对照各 3/3 为 2024-01-31 12:34:56.123457;INSERT 目标仅 1 行。

    Unhappy Path 覆盖

    1. prepared 标量执行 invalid 与 NULL 参数,各 3/3 得到 SQL NULL;随后同一 prepare 再执行有效参数,3/3 恢复为正确值。目标库清理后不存在;本缺陷不是失败注入/事务回滚问题。

    白盒验证

    同 SHA 的 pkg/sql/plan 与 pkg/sql/plan/function 定向 TestPreparedTemporalResultContracts|TestPreparedTemporalMarkerReuse|TestTemporalCompatibilityExecutionMatrix|TestDynamicTimeResultTypedCalendarAndMask,-count=3 PASS;含 prepared SELECT/CTAS 的类型与求值断言。

    回归测试

    • 新增 MOTR select without from clause is not supported #247 成对文件 t/issue_30195_30196_30197_prepared_temporal.test 与对应 r/*.result,覆盖三项 issue 的公开 SQL。独立核对期望值后定向重复 3/3,135/135 语句成功、0 skip;PR head 623ac129a7d011ae0d441cd4275d0100e5d8d854,尚未合入。
    • MatrixOne 原有 test/distributed/cases/prepare/prepared_temporal_results.sql 也覆盖此路径;本轮未运行该 BVT。MOTR 全 659 case suite 曾启动但因资源范围过大中断,遗留专用库已核对并清理;完整 suite/集成 CI 未完成。

    测试证据

    • MOTR #247 测试和期望结果;该文件定向运行摘要:total=3 success=3 failed=0 skipped=0 | stmts=135 ok=135 fail=0。
    • 上述本地 SQL 的实际值、列类型及专用库清理均在同一完整 SHA 上核对;未声称历史发布线已修复。

    测试结论

    测试通过。

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