Skip to content

Fix index analysis of date and time functions over a Nullable(DateTime('tz')) key - #125327

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-nullable-datetime-key-monotonicity-timezone
Oct 15, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-nullable-datetime-key-monotonicity-timezone

Conversation

@groeneai

@groeneai groeneai commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed primary key and skip index analysis that could skip or over-count rows for conditions on date and time functions such as toHour, toDayOfMonth, timezoneOffset or toTimeWithFixedDate over a Nullable(DateTime) or LowCardinality(Nullable(DateTime)) key whose time zone differs from the session time zone.

Description

Closes: #125306
Closes: #121179
Related: #124474
Related: #121265

On 26.9.7.6 and 26.3.33.24, with dt Nullable(DateTime('America/New_York')) as the sorting key (allow_nullable_key = 1) and session_timezone = 'UTC', SELECT count() FROM t WHERE toHour(dt) = 23 over rows 15 minutes apart returns 2 instead of 12. On master, after #124474, timezoneOffset(dt) = -14400 on the same data also returns 105 instead of 120.

IFunctionDateOrDateTime::getMonotonicityForRange took the time zone from dynamic_cast<const TimezoneMixin *>(&type) before unwrapping LowCardinality and Nullable. The wrappers carry no time zone, so the factor was compared in the session time zone while the function runs in the column's one, and ranges were declared monotonic where they are not. The cast now runs on the unwrapped type, as in IFunctionCustomWeek since #118655. This covers every function of the class with a non-trivial factor (toHour, toMinute, toDayOfMonth, toMonth, timezoneOffset, toTimeWithFixedDate, ...), for primary keys and skip indexes; non-wrapped types are unchanged.

#121265 by @ DeviousCardi has the same change to this file (its IFunctionCustomWeek half is already on master through #118655).

The test extends 05339_timezone_offset_monotonicity_dst with count() vs countIf lines for Nullable, LowCardinality(Nullable) and Nullable(DateTime64) keys and a minmax skip index, and a max_rows_to_read line checking that a Nullable key is still pruned; every new line fails on master. Locally, a matrix over four time zones, two session zones, five functions and =, <, > went from 720 wrong counts out of 4260 to 0.

toString over Nullable(DateTime('tz')) and Nullable(Enum) keys has a similar defect in ToStringMonotonicity (#119557, fix in #121081); it is not changed here.


Workflow [PR]
Sync PR [sync-upstream/pr/125327]

Version info

  • Merged into: 26.10.1.2438-master (included in 26.10 and later)

groeneai and others added 2 commits October 12, 2026 05:48
…ullable keys

`IFunctionDateOrDateTime::getMonotonicityForRange` picked the lookup table with
`dynamic_cast<const TimezoneMixin *>(&type)` before unwrapping `LowCardinality`
and `Nullable`. The wrappers are not `TimezoneMixin`, so for a
`Nullable(DateTime('tz'))` key the factor was compared in the session time zone
while the function itself runs in the column's time zone. A range whose factor
is equal at both ends in the session zone but not in the column zone was
declared monotonic, and primary key / skip index analysis skipped matching
granules or over-counted rows.

This is the long-standing ClickHouse#121179 (`toHour`, `toDayOfMonth`, ... over such keys);
ClickHouse#124474 changed the `timezoneOffset` factor to (local day, UTC offset), which in
a UTC lookup table is constant over a whole UTC day, so sub-daily `Nullable`
`timezoneOffset` conditions started losing rows on master (ClickHouse#125306).

Take the time zone from the unwrapped type, as `IFunctionCustomWeek` does since
ClickHouse#118655. ClickHouse#121265 contains the same change to this file.

Closes ClickHouse#125306
Closes ClickHouse#121179

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The changelog entry names skip index analysis, so the test also needs a
skip index over a `Nullable(DateTime('America/New_York'))` column with
`ORDER BY tuple()`, where only the index can prune. On master the
`toHour(dt) = 23` line returns 8 rows instead of 12.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-issue PR origin: picked up an external GitHub issue labels Oct 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 rounds, 1 finding) plus my own cold review (1 nit).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test has no skip-index case although the entry names skip index analysis; most wrapper/function paths have no pruning assertion AGREE (skip index), fixed @ 0b4f52e Added a minmax skip-index count() vs countIf line over a Nullable(DateTime('America/New_York')) column (master 8 vs 12). Per-path pruning lines not added: the change is one time zone pointer shared by every function and type branch, and the existing max_rows_to_read line fails a "never monotonic for wrapped types" variant (25 rows read instead of 2).
2 💡 The LowCardinality(Nullable) line reaches the function as Nullable, because key analysis strips LowCardinality first noted Kept as end-to-end coverage of the reported shape (master 105 vs 120).

Re-review of the fix commit: 0 findings.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261012-063300

@groeneai
groeneai requested review from nihalzp and yariks5s October 12, 2026 07:01
@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0b4f52e]

Summary: ✅


AI Review

Summary

This PR fixes IFunctionDateOrDateTime::getMonotonicityForRange so Nullable(DateTime('tz')), LowCardinality(Nullable(DateTime('tz'))), and the corresponding DateTime64 wrappers use the nested type's timezone instead of the session timezone during index analysis. The code change is narrow, matches the execution-time timezone selection for the 1-argument forms, and the added stateless coverage exercises correctness for nullable keys plus both primary-key and minmax skip-index paths.

Final Verdict

✅ No blocking issues found in the current diff.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... labels Oct 12, 2026
Comment thread src/Functions/IFunctionDateOrDateTime.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 0b4f52e1a with master 371b23a4b (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

54 translation units recompiled, 171 s compile time in total, 54 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×0.95 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost -8.1 s (-5%) in total before that adjustment.

Job report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trivial. Thanks!

@alexey-milovidov alexey-milovidov self-assigned this Oct 15, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 15, 2026
Merged via the queue into ClickHouse:master with commit f43b7f1 Oct 15, 2026
177 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... groeneai-origin-issue PR origin: picked up an external GitHub issue pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

3 participants