Skip to content

Fix wrong primary-key pruning for toStartOfDay and relative-number functions on out-of-range Date32 - #108018

Merged
nihalzp merged 6 commits into
ClickHouse:masterfrom
nihalzp:date32-zerotransform-saturation
Jun 24, 2026
Merged

nihalzp merged 6 commits into
ClickHouse:masterfrom
nihalzp:date32-zerotransform-saturation

Conversation

@nihalzp

@nihalzp nihalzp commented Jun 20, 2026 •

Copy link
Copy Markdown
Member

Continuation of

There are some remaining transforms that overflow on Date32. This PR fixes the remaining cases which allows us to remove the cautious monotonicity check on Date32 from src/Functions/IFunctionDateOrDateTime.h which, in turn, unblocks #101814.

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):

Fix wrong query results from incorrect primary key and partition pruning when toStartOfDay, toStartOfISOYear, toDaysSinceYearZero, toRelativeSecondNum, toRelativeMinuteNum, toRelativeHourNum, toRelativeWeekNum, toRelativeDayNum, toMonthNumSinceEpoch, or toYearNumSinceEpoch is applied to a Date32 key column containing dates outside the range the function can represent (in particular dates before 1970-01-01). These functions report themselves as monotonic to the primary index, but previously wrapped around for such arguments, so the index could prune granules that actually contained matching rows. They now saturate at the bounds of their result type and stay monotonic over the whole Date32 range, so pruning is correct.

Version info

  • Merged into: 26.7.1.41

@clickhouse-gh

clickhouse-gh Bot commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [750c55c]

Summary: ✅


AI Review

Summary
  • This PR makes the affected Date32 zero-factor date/time transforms saturate instead of wrapping, then removes the conservative Date32 monotonicity guard so primary-key and partition pruning can use those transforms again. The current head also covers the previously missing toStartOfISOYear and toDaysSinceYearZero boundary cases. I found no remaining correctness or safety issues in the reviewed diff.
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 20, 2026
Comment thread src/Functions/IFunctionDateOrDateTime.h Resolved
@nihalzp
nihalzp requested a review from yariks5s June 20, 2026 13:13
@clickhouse-gh

clickhouse-gh Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.20% 85.20% +0.00%
Functions 92.50% 92.50% +0.00%
Branches 77.50% 77.40% -0.10%

Changed lines: Changed C/C++ lines covered by tests: 21/25 (84.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@yariks5s yariks5s self-assigned this Jun 22, 2026
@nihalzp
nihalzp added this pull request to the merge queue Jun 24, 2026
Merged via the queue into ClickHouse:master with commit d1253bf Jun 24, 2026
327 of 328 checks passed
@nihalzp
nihalzp deleted the date32-zerotransform-saturation branch June 24, 2026 19:05
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 24, 2026
Blargian pushed a commit to Blargian/ClickHouse that referenced this pull request Aug 28, 2026
…nctions on out-of-range DateTime64

The ZeroTransform-factor date/time functions (toStartOfDay, toRelativeSecondNum,
toRelativeMinuteNum, toRelativeHourNum, toRelativeDayNum, toRelativeWeekNum,
toMonthNumSinceEpoch, toYearNumSinceEpoch) report themselves as always monotonic
to the primary index. Their standard-precision DateTime64 code paths narrowed the
result to UInt32/UInt16 without saturating, so for arguments outside that range
(before 1970 or beyond 2106) the value wrapped and the function was no longer
monotonic. This made primary-key pruning drop granules containing matching rows
(wrong count()) and tripped the exact_ranges assertion in the trivial-count
projection optimization (found by the AST fuzzer, amd_msan).

The DateTime64 execute overloads now saturate to [0, result-type max], mirroring
the earlier Date32 fix (PR ClickHouse#108018). Adds a regression test and updates the
references of two tests that asserted the previous wrapped standard-precision
results.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…m-saturation

Fix wrong primary-key pruning for `toStartOfDay` and relative-number functions on out-of-range `Date32`
shankar-iyer pushed a commit to shankar-iyer/ClickHouse that referenced this pull request Nov 9, 2026
…tive-number transforms

In a time zone east of UTC the Date 1970-01-01 (DayNum 0, the Date default value) starts before
the Unix epoch, e.g. at -3600 s in Europe/Berlin. The UInt16 overloads of ToRelativeHourNumImpl,
ToRelativeMinuteNumImpl, ToRelativeSecondNumImpl and ToRelativeSubsecondNumImpl ignored
ResultPrecision::Extended and narrowed that negative start to UInt32 (minute, hour) or went through
fromDayNum(DayNum), which saturates to the next day and is clamped to UInt32 (second, subsecond):

- dateDiff/age/timeDiff over a Date with units hour..nanosecond were off by 2^32 or by a whole day
  for 1970-01-01 east of UTC (Europe/Berlin: dateDiff('second', toDate(0), toDate(1)) = 0,
  'minute' = -4294965856), and second-based results were clamped for any Date after 2106-02-07.
- toRelativeMinuteNum (every east zone) and toRelativeHourNum (beyond +12:00) of that day wrapped
  to ~2^32 although FactorTransform = ZeroTransform claims always-monotonic, so primary key,
  minmax partition and statistics pruning dropped rows and reading in order returned them out of
  order.

Give the UInt16 overloads the shape the Int32 (Date32, ClickHouse#108018) and Int64 (DateTime64, ClickHouse#110344)
overloads already have: the extended precision returns the signed Int64 value (unsaturated
fromDayNum(ExtendedDayNum) for second/subsecond), and the standard minute/hour result is clamped
to [0, UInt32 max]. The standard second result was already monotonic and in range and is kept.

Measured over 10 time zones and 1,523 days including every boundary: values change only for
1970-01-01 east of UTC and for second-based dateDiff/age/timeDiff after 2106-02-07. For every Date
in 9 time zones, toRelativeHourNum, toRelativeMinuteNum and the hour..nanosecond dateDiff/age
results now equal the Date32 results for the same day. A stored key or column computed from the
old wrapped value keeps it until it is rebuilt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Development

Successfully merging this pull request may close these issues.

3 participants