Fix incorrect partition pruning for toWeek() function#99542
Merged
alexey-milovidov merged 2 commits intoClickHouse:masterfrom Mar 22, 2026
Merged
Fix incorrect partition pruning for toWeek() function#99542alexey-milovidov merged 2 commits intoClickHouse:masterfrom
alexey-milovidov merged 2 commits intoClickHouse:masterfrom
Conversation
toWeek() was incorrectly claiming monotonicity via its FactorTransform (ToStartOfYearImpl), which caused partition pruning to skip partitions containing December dates when filtering by week numbers 49-52. The issue was that toWeek() with certain week modes (e.g. mode 3, ISO weeks) can have week numbers that wrap at year boundaries (week 52 -> 1 in late December). The monotonicity assumption caused the optimizer to compute an invalid value range for the function output, leading to entire partitions being incorrectly pruned. Fix by disabling monotonicity information for ToWeekImpl, since week numbers are not monotonic due to year-boundary wrapping behavior that depends on the runtime week_mode parameter. Closes ClickHouse#90240
32b2ac9 to
26f4b80
Compare
Member
|
Could you please sign the CLA? |
Contributor
Author
|
@alexey-milovidov @yariks5s |
Contributor
Member
|
Check the test results. |
- Add missing `GROUP BY` to `03732_toweek_partition_pruning` test - Update `03789_to_year_week_monotonicity_key_condition` to verify that `toWeek` can no longer use primary key index (`INDEX_NOT_USED`)
Contributor
LLVM Coverage Report
PR changed lines: PR changed-lines coverage: 89.47% (17/19, 0 noise lines excluded) |
Contributor
Author
|
@alexey-milovidov Updated the tests. |
alexey-milovidov
approved these changes
Mar 22, 2026
robot-clickhouse
added a commit
that referenced
this pull request
Mar 22, 2026
This was referenced Mar 22, 2026
robot-clickhouse
added a commit
that referenced
this pull request
Mar 22, 2026
This was referenced Mar 22, 2026
robot-clickhouse
added a commit
that referenced
this pull request
Mar 22, 2026
This was referenced Mar 22, 2026
robot-clickhouse
added a commit
that referenced
this pull request
Mar 22, 2026
This was referenced Mar 22, 2026
robot-clickhouse
added a commit
that referenced
this pull request
Mar 22, 2026
This was referenced Mar 22, 2026
robot-clickhouse
added a commit
that referenced
this pull request
Mar 22, 2026
clickhouse-gh bot
added a commit
that referenced
this pull request
Mar 22, 2026
Backport #99542 to 26.2: Fix incorrect partition pruning for toWeek() function
clickhouse-gh bot
added a commit
that referenced
this pull request
Mar 22, 2026
Backport #99542 to 26.1: Fix incorrect partition pruning for toWeek() function
clickhouse-gh bot
added a commit
that referenced
this pull request
Mar 22, 2026
Backport #99542 to 26.3: Fix incorrect partition pruning for toWeek() function
clickhouse-gh bot
added a commit
that referenced
this pull request
Mar 22, 2026
Backport #99542 to 25.8: Fix incorrect partition pruning for toWeek() function
alexey-milovidov
added a commit
that referenced
this pull request
Mar 23, 2026
Backport #99542 to 25.12: Fix incorrect partition pruning for toWeek() function
alexey-milovidov
added a commit
that referenced
this pull request
Mar 23, 2026
Backport #99542 to 25.3: Fix incorrect partition pruning for toWeek() function
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.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fix incorrect partition pruning for
toWeek()that caused queries withWHERE toWeek(date, mode) = Nto return empty results for weeks 49-52 on tables partitioned bytoYYYYMM(date).Summary
toWeek()incorrectly claimed monotonicity via itsFactorTransform(ToStartOfYearImpl), which caused partition pruning to skip partitions containing December dates when filtering by week numbers 49-52.The root cause is that
toWeek()with certain week modes (e.g. mode 3, ISO weeks) can have week numbers that wrap at year boundaries (week 52 → 1 in late December). TheToStartOfYearImplfactor transform does not detect this wrapping because both December 1 and December 31 are in the same calendar year, so it incorrectly reports the function as monotonic. The optimizer then computes an invalid value range (e.g. [49, 1]) and prunes the partition entirely.No single
FactorTransformcan correctly handle all week modes because wrapping behavior depends on the runtimeweek_modeparameter and different modes wrap at different dates.Fix: Add a
hasMonotonicity()static method to each custom week Transform struct.ToWeekImplreturnsfalse; all other transforms returntrue.IFunctionCustomWeekdelegates toTransform::hasMonotonicity()instead of unconditionally returningtrue, following the same pattern used byhasPreimage()inDateTimeTransforms.h.Closes #90240