Repository navigation
Fix primary key analysis skipping rows for toMonth, toQuarter, toDayOfYear and toDayOfMonth over a DateTime before local 1970-01-01 - #124470
Conversation
`toQuarter`, `toMonth`, `toDayOfYear`, `toDayOfMonth`, `toHour` and `toTimeWithFixedDate` decided monotonicity on a key range with `toStartOfYear`, `toStartOfMonth` and `toDate` as factors. Their `DateTime` overloads saturate into `Date`, so a local 1969-12-31 (a `DateTime` in a zone west of UTC right after the epoch) got 1970-01-01, the start of the next day, month and year. A range crossing it was claimed monotonic and the key condition built a reversed range: `toMonth(dt) = 1` returned 0 instead of 744, and in debug builds `IN` threw `Invalid binary search result in MergeTreeSetIndex`. For `DateTime64` the `toDate` factor of `toHour` also narrowed the day number to `UInt16` and truncated pre-epoch fractions that `toHour` floors. Use the unclamped day number of the start of the day, month or year instead, as `ToDayOfWeekFactorImpl` already does. `toTimeWithFixedDate` truncates `DateTime64`, so its factor keeps a truncating `DecimalComponents` overload. Factor values are unchanged for `Date`, `Date32` and the `DateTime64` year/month cases. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merge the two-line ToStartOfPeriodFactorImpl comment into one invariant line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own cold review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261007-172200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261007-162800 |
|
Workflow [PR], commit [251ce5a] Summary: ❌
AI ReviewSummaryPR #124470, head Missing context / blind spots
Final VerdictStatus: ✅ Approve |
CI finish ledger - 0beec9eEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
All 149 check runs completed, Session id: cron:our-pr-ci-monitor:20261008-020432 |
…onicity-pre-epoch-datetime Master now gives toHour and toTimeWithFixedDate their own factors (ToClockPeriodStartFactorImpl, ToDayAndUTCOffsetFactorImpl), which already keep the local day 1969-12-31 unclamped, so take master's for both, drop this branch's ToTimeWithFixedDateFactorImpl and the now unused Day period of ToStartOfPeriodFactorImpl. Co-Authored-By: Claude Opus 5.5 <[email protected]>
toHour and toTimeWithFixedDate over a pre-epoch DateTime and DateTime64 are fixed on master by ClickHouse#124530 and ClickHouse#124474, so their queries pass there and no longer test this change. toTimeWithFixedDate also rounds a pre-epoch DateTime64 down to whole seconds on master now, so the expected row of the removed DateTime64 case no longer exists. Co-Authored-By: Claude Opus 5.5 <[email protected]>
toMonth, toDayOfMonth and toHour over a DateTime before local 1970-01-01toMonth, toQuarter, toDayOfYear and toDayOfMonth over a DateTime before local 1970-01-01
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units133 translation units recompiled, 765 s compile time in total, 133 of them have a recent master baseline. Median compile-time ratio to the baselines is ×0.94 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
CI finish ledger - 7b23e29Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261022-163012 |
|
Dear @yariks5s, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
|
@groeneai please update the branch with master |
…onicity-pre-epoch-datetime Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Merged master in 251ce5a (clean merge, the PR's own diff is unchanged). On the merged debug build |
CI finish ledger - 251ce5aEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261124-220031 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed primary key, partition key and skip index analysis that could skip matching rows for conditions on
toMonth,toQuarter,toDayOfYearandtoDayOfMonthover aDateTimecolumn in a time zone west of UTC holding times from local1969-12-31.Description
Related: #118721 (comment)
Related: #90461
On 26.3.42.3 and master, with
dt DateTime('America/New_York')as the sorting key filled bySELECT toDateTime(number * 3600) FROM numbers(2000),SELECT count() FROM t WHERE toMonth(dt) = 1returns 0 instead of 744.A date part is treated as monotonic on a key range when a factor is equal at both ends: the start of the year for
toMonth,toQuarterandtoDayOfYear, of the month fortoDayOfMonth. These factors weretoStartOfYearandtoStartOfMonth, which saturate aDateTimeintoDate, so aDateTimeon local 1969-12-31 got 1970-01-01, the start of the next month and year. A range crossing local 1970-01-01 was reported monotonic, the key condition got a reversed range and excluded matching granules. In debug builds the same range makesINthrowInvalid binary search result in MergeTreeSetIndex(for example withmerge_tree_coarse_index_granularity = 20).The four functions now use an unclamped start of month or year as the factor. Pruning for
Date,Date32,DateTime64and aDateTimein UTC is unchanged (granule counts compared with master); onlyDateTimeranges crossing a local period start lose the monotonicity claim.toHourandtoTimeWithFixedDate, which this PR also covered at first, are fixed on master by #124530 and #124474.Found while looking at the AST fuzzer failure on #121116; with #125327 on master, this PR also fixes its
Nullable(DateTime('America/New_York'))shape (master returns 96 of 144 rows).Workflow [PR]
Sync PR [sync-upstream/pr/124470]