Repository navigation
Fix wrong primary-key pruning for toStartOfDay and relative-number functions on out-of-range Date32 - #108018
Merged
nihalzp merged 6 commits intoJun 24, 2026
Conversation
Contributor
|
Workflow [PR], commit [750c55c] Summary: ✅
AI ReviewSummary
Final Verdict
|
…ce for `Date32` `toStartOfDay` saturation
…notonicity pruning test
Contributor
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 21/25 (84.00%) | Lost baseline coverage: none · Uncovered code |
yariks5s
approved these changes
Jun 24, 2026
Merged
via the queue into
ClickHouse:master
with commit Jun 24, 2026
d1253bf
327 of 328 checks passed
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]>
This was referenced Aug 28, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 onDate32fromsrc/Functions/IFunctionDateOrDateTime.hwhich, in turn, unblocks #101814.Changelog category (leave one):
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, ortoYearNumSinceEpochis applied to aDate32key column containing dates outside the range the function can represent (in particular dates before1970-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 wholeDate32range, so pruning is correct.Version info
26.7.1.41