Wrong result: strict comparison of a DateTime64 key column with a DateTime constant prunes the last second (regression in 26.3)
Describe what's wrong
A strict comparison between a DateTime64(3) column in the primary key or partition key and a DateTime constant returns too few rows. This covers col < toDateTime(X) and col > toDateTime(X).
Index analysis appears to handle the bound with whole-second semantics. EXPLAIN indexes = 1 shows time < toDateTime(X) becoming time in (-Inf, X - 1]. That is correct for a DateTime column. For DateTime64(3) it excludes the whole last second before X, so every part or granule whose values all fall inside that second is pruned. The rows are still there: row-level evaluation (countIf) returns the correct count, and so do <=, >= and a DateTime64 constant.
It returns wrong results silently, with no error. We hit it through clickhouse-go, which binds time.Time parameters as toDateTime('…'). A plain WHERE time >= ? AND time < ? over freshly inserted data therefore dropped the rows written in the final second of the window.
Does it reproduce on the most recent release?
Yes
How to reproduce
clickhouse local, default settings. On 26.8 it also reproduces with enable_analyzer = 0.
| Version | Result |
|---|---|
| 24.8.14.39, 25.8.33.6, 26.1.12.23, 26.2.19.43 | correct |
| 26.3.34.136, 26.5.7.64, 26.7.15.52, 26.8.10.6, 26.9.4.3 | wrong |
CREATE TABLE repro (d Int64, time DateTime64(3, 'UTC'))
ENGINE = MergeTree ORDER BY (d, time) PARTITION BY toYYYYMM(time);
-- Two inserts, so two parts, both inside 10:05:09.xxx
INSERT INTO repro VALUES (1, '2026-09-28 10:05:09.110');
INSERT INTO repro VALUES (1, '2026-09-28 10:05:09.599');
SELECT count() FROM repro WHERE d = 1 AND time < toDateTime('2026-09-28 10:05:10', 'UTC'); -- 0, expected 2
SELECT count() FROM repro WHERE time < toDateTime('2026-09-28 10:05:10', 'UTC'); -- 0, expected 2 (min-max index only)
SELECT count() FROM repro WHERE d = 1 AND time > toDateTime('2026-09-28 10:05:09', 'UTC'); -- 0, expected 2
SELECT count() FROM repro WHERE d = 1 AND time < toDateTime64('2026-09-28 10:05:10', 3, 'UTC'); -- 2
SELECT count() FROM repro WHERE d = 1 AND time < toDateTime64('2026-09-28 10:05:10', 0, 'UTC'); -- 2
SELECT count() FROM repro WHERE d = 1 AND time <= toDateTime('2026-09-28 10:05:10', 'UTC'); -- 2
SELECT countIf(time < toDateTime('2026-09-28 10:05:10', 'UTC')) FROM repro; -- 2EXPLAIN indexes = 1 SELECT count() FROM repro WHERE d = 1 AND time < toDateTime('2026-09-28 10:05:10', 'UTC') on 26.9.4.3:
└──ReadFromMergeTree (default.repro)
Parts: 0 | Granules: 0
Indexes:
Min-Max
Keys:
time
Condition: (time in (-Inf, 1790589909])
Parts: 0/2
Granules: 0/2
...
PrimaryKey
Keys:
d
time
Condition: and((time in (-Inf, 1790589909]), (d in [1, 1]))1790589910 is 2026-09-28 10:05:10, so the strict < became <= 10:05:09.000, which excludes 10:05:09.110 and 10:05:09.599.
Expected behavior
Every query above returns 2. A DateTime64(3) column compared with a DateTime constant should compare at the column's precision: 10:05:09.599 < 10:05:10 is true. It does not today, which is inconsistent with row-level evaluation of the same expression.
Error message and/or stacktrace
None. The results are wrong without any error.
Additional context
Workaround: cast the constant to the column's type, e.g. time < toDateTime64(?, 3, 'UTC'), or bind the parameter with sub-second precision.
Source: ClickHouse/ClickHouse