Skip to content

[Bug]: CTAS truncates fractional seconds from FROM_UNIXTIME #28124

Closed

Description

@Ariznawlll

Summary

CTAS truncates fractional seconds produced by FROM_UNIXTIME. The source epoch value has six fractional digits and direct evaluation returns DATETIME(6)-equivalent data, but the CTAS target is created as unscaled DATETIME and stores only whole seconds.

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);
INSERT INTO src VALUES (1, '2024-03-10 06:30:00.123456');

SELECT UNIX_TIMESTAMP(ts), FROM_UNIXTIME(UNIX_TIMESTAMP(ts)) FROM src;
-- 1710052200.123456 | 2024-03-10 06:30:00.123456

CREATE TABLE ctas_from_unix AS
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(ts)) AS v FROM src;
SHOW CREATE TABLE ctas_from_unix;
SELECT v FROM ctas_from_unix;

Actual result:

CREATE TABLE `ctas_from_unix` (`v` datetime NOT NULL)
2024-03-10 06:30:00

Scope control

CREATE TABLE ... AS SELECT UNIX_TIMESTAMP(ts) creates DECIMAL(38,6) and stores 1710052200.123456 correctly. A view over FROM_UNIXTIME(UNIX_TIMESTAMP(ts)) also returns 2024-03-10 06:30:00.123456. The loss is therefore specific to CTAS schema derivation for fractional FROM_UNIXTIME results.

Expected behavior

CTAS should create a temporal target with sufficient fractional scale and retain .123456, rather than silently coercing the result to whole seconds.

Preliminary code observation

The FROM_UNIXTIME evaluator has separate decimal/float paths that construct a datetime with nanoseconds, but the corresponding static overload return metadata is unscaled DATETIME. CTAS derives its output schema from that metadata and the insert then truncates the fractional component. This is a diagnosis hypothesis, not a proposed fix.

Suggested regression coverage

Cover CTAS for decimal and float fractional epochs, a UNIX_TIMESTAMP(timestamp(6)) round trip, and a view/direct-query control that preserves microseconds.

Activity

  1. added
    kind/bug
    on Sep 4, 2026
  2. added this to the MO-202609 milestone on Sep 4, 2026
  3. added theissue type on Sep 4, 2026
  4. jiangxinmeng1 commented on Sep 14, 2026

    @jiangxinmeng1
    Contributor

    fixed by #28316

  5. Ariznawlll commented on Sep 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:be30a70fe50eac4e8c78e5117fdd69179ba853e0
    • 部署方式:本地全新单 CN mo-service,独立数据目录
    • 测试日期:2026-09-14

    原问题验证

    以 TIMESTAMP(6) '2024-03-10 06:30:00.123456' 作为源表数据执行 CREATE TABLE ... AS SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(ts))。连续 3 次创建出的目标列均为 DATETIME(6),查询值为 2024-03-10 06:30:00.123456,不再落为整秒。

    Happy Path 覆盖

    1. 直接 UNIX_TIMESTAMP(ts) / FROM_UNIXTIME(...) 往返保留 123456 微秒。
    2. CTAS 目标的 COLUMN_TYPE=DATETIME(6)、DATETIME_PRECISION=6,与实际值一致。

    Unhappy Path 覆盖

    不适用:此 issue 的风险是合法时间值在 CTAS 中被静默截断,并非非法输入拒绝。已用声明类型、物化值和微秒提取三项断言防止静默精度丢失。

    白盒验证

    执行 go test -vet=off ./pkg/sql/plan -run 'TestCreateTableAsSelectPreservesUnixTimePrecision|TestCreateTableAsSelectWithTemporalFractionalSeconds' -count=1,通过。

    回归测试

    测试证据

    定向结果与预期基线见 MOTR PR #170。该 case 在 14_issue_regression 套件实跑中为 22/22 statements 通过;套件内其他既有 case 的失败未修改。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions