Repository navigation
Fix an infinite loop in hop when subtracting the hop does not change the window end - #124570
Conversation
…e the window end `executeHop` finds the window end by subtracting the hop until the end is not after the time. ClickHouse#114607 made the loop throw when the subtraction makes the end larger (a wrap around zero), but not when it leaves the end unchanged, and then the loop never exits and cannot be cancelled. That happens for `Date` interval kinds when the bound saturates at the end of the calendar: `addQuarters`/`addMonths`/`addYears` clamp to `0000-01-01` (ExtendedDayNum -719528), which narrows to day 1368 in `UInt16`, and subtracting the hop from 1368 saturates to the same value again. The loop condition compares that day number with the time in seconds, so it stays true for every time below 1368 seconds. It also happens for time kinds when the span of the hop is a multiple of 2^32 seconds (a no-op in the wrapping `UInt32` arithmetic) and the window start computed by `ToStartOfTransform` is after the time, e.g. local midnight 39600 in `US/Samoa` with `toIntervalDay(33554432)`. The loop terminates only if every iteration strictly decreases the end, so require exactly that and throw the existing `Time overflow` error otherwise. Subtracting a positive hop from a representable value always decreases it; the only other inputs affected returned a window that does not contain the time. Found by `FunctionsStress` in `Unit tests (asan_ubsan, function_prop_fuzzer)` on PR ClickHouse#118495, query `hopEnd(CAST('1970-01-01 01:02:07' AS DateTime), ...)` with the test image time zone `Europe/Amsterdam`, i.e. raw time 127. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A hop that leaves the window end unchanged after the time used to return a window without the time; pin that it now throws, and make the comment say so. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the consequence line of the window-end guard comment; keep the strict-decrease invariant. 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; 2 rounds, 3 findings) plus my own cold review (1 nit).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261008-074000 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261008-062800 |
|
Workflow [PR], commit [157482d] Summary: ✅
AI ReviewSummaryThis PR hardens Final VerdictStatus: ✅ Approve The fix is small and targeted, the new guard matches the loop invariant it relies on, and the added regression queries cover both the original hang and the newly rejected no-progress/wrong-window case. LLVM Coverage ReportMeasured on commit 157482d.
Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 7 s compile time in total, 7 of them have a recent master baseline. |
Related: #114607
Related: #118495
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed an infinite, uncancellable loop in functions
hop,hopStartandhopEndwhen subtracting the hop interval does not move the window end, for examplehopEnd(toDateTime(0, 'UTC'), toIntervalQuarter(413844), toIntervalQuarter(413844)), where the date saturates at the calendar limit. Such arguments now throw theTime overflowerror.Description
On 26.9.11.2 (also 26.8.18.2, 26.7.22.4),
SELECT hopEnd(toDateTime(0, 'UTC'), toIntervalQuarter(413844), toIntervalQuarter(413844))never returns; neithermax_execution_timenorKILL QUERYstops it.Root cause.
executeHopsubtracts the hop from the window end until the end is not after the time. #114607 made the loop throw when the subtraction makes the end larger (a wrap), but not when it leaves it unchanged. That happens when aDatebound saturates:addQuartersclamps to0000-01-01, which narrows to day1368inUInt16, and subtracting the hop from1368saturates to the same value. It also happens for a hop spanning a multiple of 2^32 seconds when the window starts after the time (toIntervalDay(33554432)inUS/Samoa).Change. The in-loop check requires the end to strictly decrease (
>=instead of>), else it throws the existingTime overflowerror. If the unchanged end is already at or before the time, the old loop exited with a window that does not contain the time (a 128-second window in 1970 for a 2023 time with theDayhop above); such inputs now throw too, and no correct result changes.Validation. Four new lines in
04891_time_window_functions_wrapped_interval_overflowthrow with the fix. On master three hang, including the CI query (itsDateTimeis printed in the test image'sEurope/Amsterdamzone: raw127; in UTC it is3727, which does not loop), and the fourth returns the 1970 window above. 17280hop/hopStart/hopEndqueries over all interval kinds and five time zones give identical results before and after.Found by
FunctionsStressin Unit tests (asan_ubsan, function_prop_fuzzer) on #118495.A wrapped
Datebound that does not loop still returns a wrapped value (hopEnd(toDateTime(3727, 'UTC'), toIntervalQuarter(413844), toIntervalQuarter(413844))is2105-01-05); making it throw is a separate change, I can send it if wanted.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/124570]
Version info
26.10.1.1938-master(included in26.10and later)