Skip to content

Cherry pick #105244 to 26.4: Fix Date32 overflow on toStartOfWeek - #106936

Merged
robot-ch-test-poll merged 10 commits into
backport/26.4/105244from
cherrypick/26.4/105244
Jun 10, 2026
Merged

robot-ch-test-poll merged 10 commits into
backport/26.4/105244from
cherrypick/26.4/105244

Conversation

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

yariks5s and others added 10 commits May 18, 2026 15:37
`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
@robot-ch-test-poll robot-ch-test-poll added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Jun 10, 2026
@robot-ch-test-poll
robot-ch-test-poll merged commit bb7284c into backport/26.4/105244 Jun 10, 2026
@robot-ch-test-poll
robot-ch-test-poll deleted the cherrypick/26.4/105244 branch June 10, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants