Skip to content

[Bug]: interval time windows round fractional timestamps into the next bucket #26975

Closed

Description

@XuPeng-SH

Summary

MatrixOne's time-window operator interval(timestamp, n, unit) mis-buckets fractional-second timestamps. Values at or after .500000 seconds before a window boundary are rounded into the next window instead of being floored into the half-open window that contains the timestamp.

This is a blocking correctness issue for time-series feature acceptance: queries complete successfully and the server stays healthy, but daily/hourly/second aggregates are silently wrong.

Environment

  • Repository: matrixorigin/matrixone
  • Branch: main
  • Commit: c3ba9c7df14388956cb38d78681c6c35b75c42b8
  • Host: Debian x86_64, 10.222.1.55
  • SQL endpoint: 127.0.0.1:6001
  • Test database: ts_validation_20260812
  • Test artifact: mo-service with go version -m provenance for the commit above

Minimal reproduction

DROP TABLE IF EXISTS ts_min;
CREATE TABLE ts_min (
  id INT NOT NULL PRIMARY KEY,
  event_ts TIMESTAMP(6) NOT NULL,
  value INT NOT NULL
);

INSERT INTO ts_min VALUES
  (1, '2024-02-29 23:59:59.499999', 1),
  (2, '2024-02-29 23:59:59.500000', 2),
  (3, '2024-02-29 23:59:59.999999', 3);

-- All three rows are in the half-open day [Feb 29, Mar 1).
SELECT COUNT(*)
FROM ts_min
WHERE event_ts >= '2024-02-29 00:00:00.000000'
  AND event_ts <  '2024-03-01 00:00:00.000000';

SELECT _wstart, _wend, COUNT(*), SUM(value), MIN(id), MAX(id)
FROM ts_min
WHERE event_ts >= '2024-02-29 00:00:00.000000'
  AND event_ts <  '2024-03-01 00:00:00.000000'
INTERVAL(event_ts, 1, day);

Direct range result:

COUNT(*)
3

Actual interval result:

_wstart                    _wend                      COUNT  SUM  MIN  MAX
2024-02-29 00:00:00.000000 2024-03-01 00:00:00.000000      1    1    1    1
2024-03-01 00:00:00.000000 2024-03-02 00:00:00.000000      2    5    2    3

Expected result is one Feb 29 bucket with COUNT=3, SUM=6, MIN=1, MAX=3. The .500000 and .999999 rows are incorrectly placed in the Mar 1 bucket even though the predicate explicitly excludes Mar 1.

Additional validation

  • The same boundary behavior was reproduced with DATETIME(6).
  • interval(..., 1, hour) and interval(..., 1, second) also move .500000/later fractional values to the next bucket.
  • DATE_TRUNC(day/hour/second, event_ts) floors correctly, and raw half-open range predicates return the correct rows.
  • Inserts, reads, bulk writes, DML transactions/rollback, index scans, concurrent readers/writers, and restart durability all passed; the failure is isolated to time-window bucketing.
  • No panic, crash, deadlock, or server-log error occurred. This is a silent data-correctness failure.

Impact

Any time-series query using interval for rollups can report incorrect per-window counts and aggregates for normal sub-second event data. This affects daily/hourly/second rollups and can make dashboards, downsampling, and alert calculations wrong without an error.

Suspected area

pkg/sql/colexec/timewin/timewin.go computes window boundaries using integer modulo (val - val%interval). The observed threshold suggests the timestamp-to-window type conversion rounds fractional seconds before that modulo operation. This is a hypothesis to confirm during fixing.

Workaround

Use raw half-open range predicates or DATE_TRUNC-based grouping where applicable; these controls returned the expected bucket for the repro above.

Activity

  1. added
    kind/bug
    severity/s0
    on Aug 11, 2026
  2. XuPeng-SH commented on Aug 11, 2026

    @XuPeng-SH
    ContributorAuthor

    Follow-up validation with a standard IoT/TSBS-like telemetry workload on commit c3ba9c7df14388956cb38d78681c6c35b75c42b8 (10.222.1.55):

    • 50 devices, 5 sites, 2 metrics (cpu_usage, temperature), one-minute samples over 24 hours: 144,000 rows.
    • Added 5,000 append-only rows, then inserted three out-of-order/late TIMESTAMP(6) points for device-001/cpu_usage, including 08:59:59.500000 and 08:59:59.999999.
    • The half-open raw range [08:00,09:00) returned 63 rows; DATE_TRUNC('hour', event_ts) kept all 63 in the 08:00 bucket.
    • The equivalent interval(event_ts, 1, hour) query returned 61 rows in 08:00 and moved the two fractional boundary points to 09:00.
    • Standard latest-value/window, tag-filter, hourly rollup, gap-fill (fill(null)/fill(prev)), sliding-window, concurrent ingest/read, upsert, and restart-durability checks otherwise passed.

    This confirms the issue is reproducible in a normal telemetry workload, not only in the three-row minimal repro.

  3. XuPeng-SH commented on Aug 11, 2026

    @XuPeng-SH
    ContributorAuthor

    Additional boundary matrix on the same commit:

    • The mis-bucketing is not limited to positive dates: for 1969-12-31 23:59:59.499999, .500000, .999999, a raw range and DATE_TRUNC return all 3 rows in the same second/day, while interval(...,1,second/day) returns 1 row in the prior bucket and 2 rows in the next bucket.
    • The same threshold appears for minute windows: in [00:00,00:01), DATE_TRUNC('minute', ...) returns all 5 fractional rows together, while interval(...,1,minute) returns 3 in 00:00 and 2 in 00:01.
    • This was reproduced in a 50-device/2-metric telemetry workload with late data, not only synthetic boundary tables.
  4. added a commit that references this issue on Aug 13, 2026
  5. Ariznawlll commented on Aug 14, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • commit:de40fe49fd105bd4d77bde8989369cec1fe65c76
    • 部署方式:本地隔离部署,2 CN / 1 TN / 1 Log,直连 CN,UTC 会话时区
    • 测试日期:2026-08-14

    原问题验证

    复跑 Issue 的闰日 TIMESTAMP(6) 边界:23:59:59.000000/.499999/.500000/.999999 均保留在 2 月 29 日的半开区间内,日窗口精确为 COUNT=4, SUM=10, MIN(id)=1, MAX(id)=4,00:00:00.000000 单独进入 3 月 1 日,不再发生 .500000 后被舍入到下一桶的问题。完整矩阵及 MOTR 定向回归连续执行 3 轮均通过。

    Happy Path 覆盖

    1. 覆盖 TIMESTAMP(6) 的秒、分钟、天窗口,结果分别与原始半开范围及 DATE_TRUNC oracle 一致。
    2. 覆盖闰日跨天边界和精确窗口边界 .000000。
    3. 覆盖 1970 年前 DATETIME(6),.499999/.500000/.999999 均正确 floor 到前一秒。

    Unhappy Path 覆盖

    1. 本 Issue 的核心输入均是合法时间边界,不把合法边界误归类为异常输入;使用跨 epoch 的负时间值作为高风险边界对照。
    2. 所有边界查询后两张源表均保持 5 行、SUM(value)=15,同一会话继续可用。

    白盒验证

    exact main 上 TestQueryBuilder_bindTimeWindowTruncatePreservesFractionalScale 连续 3 轮通过;该修复为确定性时间精度/类型转换路径,本轮不单独增加 race gate。

    回归测试

    已加入 MOTR PR #109:issue_26975_time_window_fractional_boundary.test。定向 --repeat 3 为 3/3 通过;完整 14_issue_regression 中本用例通过。该缺陷由数值精度转换触发,与数据规模无关,不需要大数据量回归。

    测试证据

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions