Repository navigation
Description
Summary
CTAS truncates fractional seconds from DATE_ADD/DATE_SUB expressions. A TIMESTAMP(6) or DATETIME(6) source and a direct SELECT retain microseconds, but the CTAS target is declared without fractional precision and stores a whole-second value.
Environment
- MatrixOne official
main:99ed717b769e261b842c2f17345bd65865fc1470 - Clean local single-CN build and isolated data directory
- Reproduced identically in 3/3 runs
Reproduction
CREATE TABLE src (
id INT PRIMARY KEY,
ts TIMESTAMP(6) NOT NULL,
dt DATETIME(6) NOT NULL
);
INSERT INTO src VALUES
(1, '2024-03-10 06:30:00.123456', '2024-03-10 06:30:00.123456');
SELECT DATE_ADD(ts, INTERVAL 1 HOUR), DATE_ADD(dt, INTERVAL 1 HOUR) FROM src;
-- 2024-03-10 07:30:00.123456 | 2024-03-10 07:30:00.123456
CREATE TABLE ctas_ts AS SELECT DATE_ADD(ts, INTERVAL 1 HOUR) AS v FROM src;
CREATE TABLE ctas_dt AS SELECT DATE_ADD(dt, INTERVAL 1 HOUR) AS v FROM src;
SHOW CREATE TABLE ctas_ts;
SHOW CREATE TABLE ctas_dt;
SELECT v FROM ctas_ts;
SELECT v FROM ctas_dt;Actual result:
CREATE TABLE `ctas_ts` (`v` timestamp NOT NULL)
CREATE TABLE `ctas_dt` (`v` datetime NOT NULL)
2024-03-10 07:30:00
2024-03-10 07:30:00
For comparison, CREATE TABLE ctas_plain AS SELECT ts AS v FROM src creates timestamp(6) and preserves the source precision.
Expected behavior
CTAS should derive a target temporal scale that preserves the evaluated DATE_ADD/DATE_SUB result's microseconds. It must not silently change .123456 to a whole-second value merely because the expression becomes a persisted CTAS column.
Preliminary code observation
The runtime date-add evaluator derives the result scale from its temporal input, but the static overload metadata for the DATE_ADD/DATE_SUB TIMESTAMP and DATETIME cases advertises the unscaled type. CTAS uses that metadata to synthesize the target schema, so the subsequent insert is coerced to scale 0. This is a diagnosis hypothesis, not a proposed fix.
Suggested regression coverage
Cover CTAS and an equivalent INSERT ... SELECT target with TIMESTAMP(6) and DATETIME(6) inputs, DATE_ADD and DATE_SUB, and a plain temporal CTAS control.
Activity
Ariznawlll commentedon Sep 5, 2026
补充最新
main(e7bb0572235ec4bac81eb098eb0ad8900f0065ab)上的行为。当前现象已经不再只是“落表后值统一变成整秒”,而是 CTAS 目标列的声明精度与已有数据的实际精度不一致:DATE_SUB的行为相同。由 CTAS 创建的行实际保留了微秒,普通查询协议显示却按DATETIME(0)隐藏小数部分;同一列后续写入带微秒的值时又按 scale 0 四舍五入。于是同一列中,CTAS 已有行和后续插入行具有不同的有效精度语义。对照 MySQL 8.3,目标列为
DATETIME(6),元数据、显示结果、MICROSECOND()与后续写入行为保持一致。代码侧仍可看到
DATE_ADD/DATE_SUB的DATETIMEoverload 静态返回类型使用无 scale 的types.T_datetime.ToType(),而执行器会依据输入动态把结果向量设为带 scale 的DATETIME。这解释了 CTAS 规划类型与实际生成向量之间的不一致。fix temporal precision, prepared binding, and boundary semantics (#28316
jiangxinmeng1 commentedon Sep 14, 2026
fixed by #28316
Ariznawlll commentedon Sep 14, 2026
测试版本
mainbe30a70fe50eac4e8c78e5117fdd69179ba853e0mo-service,独立数据目录原问题验证
源表含
TIMESTAMP(6)与DATETIME(6)的2024-03-10 06:30:00.123456。连续 3 次执行DATE_ADD/DATE_SUBCTAS 后,ctas_add.v=DATETIME(6)、ctas_sub.v=TIMESTAMP(6),物化值均保留123456微秒。Happy Path 覆盖
DATE_ADD(dt, INTERVAL 1 HOUR)结果为2024-03-10 07:30:00.123456。DATE_SUB(ts, INTERVAL 1 HOUR)结果为2024-03-10 05:30:00.123456。2031-07-08 09:10:11.654321后仍保留654321,声明精度、既有数据和后续写入一致。Unhappy Path 覆盖
不适用:问题是合法 CTAS 结果的元数据/物化精度不一致,不涉及应失败的 SQL。通过后续写入验证排除了“旧行有微秒、新行按 scale 0 四舍五入”的不一致状态。
白盒验证
执行
go test -vet=off ./pkg/sql/plan -run 'TestCreateTableAsSelectWithTemporalFractionalSeconds|TestCreateTableAsSelectTemporalInsertKeepsTargetScale' -count=1,通过。回归测试
issue_28121_28123_28124_28122_28144_temporal_ctas.test./motr --repeat 3 14_issue_regression.issue_28121_28123_28124_28122_28144_temporal_ctas:3/3 通过,66/66 statements。测试证据
定向结果与预期基线见 MOTR PR #170。该 case 在
14_issue_regression套件实跑中为 22/22 statements 通过;套件内其他既有 case 的失败未修改。测试结论
测试通过。
fix temporal precision, prepared binding, and boundary semantics (mat…
fix temporal precision, prepared binding, and boundary semantics (mat…
fix temporal precision, prepared binding, and boundary semantics (mat…