Repository navigation
Cherry pick #105244 to 26.5: Fix Date32 overflow on toStartOfWeek - #106938
Merged
robot-ch-test-poll merged 10 commits intoJun 10, 2026
Merged
Conversation
`toStartOfWeek`, `toMonday`, `toLastDayOfWeek`, `toStartOfMonth`, `toLastDayOfMonth`, `toStartOfQuarter` and `toStartOfYear` now clamp pre-epoch rounding results to `1970-01-01` for the `Date` result type instead of overflowing through `UInt16`. Regenerate the reference files of the two pre-existing tests that encoded the old overflowed output. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105244&sha=fe0ab6b5fbaf7adc6250f0ee0f9d5306735a9818&name_0=PR&name_1=Fast%20test
The pre-epoch clamp added to `toMonday`, `toStartOfMonth` and `toStartOfYear` is correct for the returned `Date` value, but these impls are also the `FactorTransform` used by `getMonotonicityForRange` for `toDayOfWeek`, `toDayOfMonth`, `toDaysInMonth`, `toMonth`, `toQuarter` and `toDayOfYear`. Clamping the factor collapsed every pre-epoch week/month/year to `0`, so a range spanning several periods was wrongly reported as monotonic, which can make primary-key/partition pruning skip rows that actually match (e.g. `WHERE toDayOfWeek(date32_key) = 5`). Use the unclamped, order-preserving `executeExtendedResult` for the Date32 and DateTime64 factor comparisons (gated by `requires`, falling back to `execute` for factor transforms that do not provide it). The clamp stays on `execute`, so the returned values are unchanged. Adds 04305_date32_pre_epoch_monotonicity covering toDayOfWeek (Date32 and DateTime64), toMonth and toDayOfMonth.
…dResult
`TransformDateTime64::executeExtendedResult` truncated the fractional part of a
negative `DateTime64` towards zero, while `execute` rounds towards negative
infinity. A value in the last fractional second of a day was attributed to the
next day: `toStartOfMonth(toDateTime64('1969-12-31 23:59:59.5', 1))` with
`enable_extended_results_for_datetime_functions = 1` returned `1970-01-01`
while `toMonth` of the same value returns `12`.
This also matters for the monotonicity analysis, which now compares factor
transforms through `executeExtendedResult`: the factor must attribute boundary
values to the same period as the actual execution, otherwise a granule whose
key range crosses a pre-epoch period boundary could be wrongly declared
monotonic and pruned.
`std::max(res, 0)` only fixed the pre-epoch wrap of `toStartOfWeek`,
`toLastDayOfWeek`, `toMonday`, `toStartOfMonth`, `toLastDayOfMonth`,
`toStartOfQuarter` and `toStartOfYear`. A `Date32` value above `2149-06-06`
still wrapped through UInt16: `toStartOfYear(toDate32('2200-01-01'))` returned
`2020-07-27`. This also produced wrong query results through the same
monotonicity mechanism as #106419,
just in the upper range: a `WHERE toStartOfYear(c1) < toDate('1980-01-01')`
filter over a part with post-2149 values pruned rows that match per-row
evaluation.
Use `std::clamp(res, 0, DATE_LUT_MAX_DAY_NUM)` so out-of-range results
saturate to `2149-06-06`, consistent with `ToDateImpl` saturation, keeping the
functions monotonic over the whole `Date32` domain.
The original reproducer: a WHERE filter over `toStartOfYear` of a `Date32` column returned 0 rows when a merge-formed part contained a few pre-1970 values, while `countIf` of the identical expression counted them correctly. #106419
…th-Date32-overflow Fix Date32 overflow on toStartOfWeek
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.
Original pull-request #105244
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job