Skip to content

Fix an infinite loop in hop when subtracting the hop does not change the window end - #124570

Merged
PedroTadim merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-hop-window-end-unchanged
Oct 9, 2026
Merged

PedroTadim merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-hop-window-end-unchanged

Conversation

@groeneai

@groeneai groeneai commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #114607
Related: #118495

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed an infinite, uncancellable loop in functions hop, hopStart and hopEnd when subtracting the hop interval does not move the window end, for example hopEnd(toDateTime(0, 'UTC'), toIntervalQuarter(413844), toIntervalQuarter(413844)), where the date saturates at the calendar limit. Such arguments now throw the Time overflow error.

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; neither max_execution_time nor KILL QUERY stops it.

Root cause. executeHop subtracts 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 a Date bound saturates: addQuarters clamps to 0000-01-01, which narrows to day 1368 in UInt16, and subtracting the hop from 1368 saturates 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) in US/Samoa).

Change. The in-loop check requires the end to strictly decrease (>= instead of >), else it throws the existing Time overflow error. 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 the Day hop above); such inputs now throw too, and no correct result changes.

Validation. Four new lines in 04891_time_window_functions_wrapped_interval_overflow throw with the fix. On master three hang, including the CI query (its DateTime is printed in the test image's Europe/Amsterdam zone: raw 127; in UTC it is 3727, which does not loop), and the fourth returns the 1970 window above. 17280 hop/hopStart/hopEnd queries over all interval kinds and five time zones give identical results before and after.

Found by FunctionsStress in Unit tests (asan_ubsan, function_prop_fuzzer) on #118495.

A wrapped Date bound that does not loop still returns a wrapped value (hopEnd(toDateTime(3727, 'UTC'), toIntervalQuarter(413844), toIntervalQuarter(413844)) is 2105-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

  • Merged into: 26.10.1.1938-master (included in 26.10 and later)

groeneai and others added 3 commits October 8, 2026 07:02
…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]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 8, 2026
@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator Author
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).

# Sev Finding Verdict Evidence / action
1 ❌ With a LowCardinality or Nullable time, the default adapters also evaluate the hidden zero time, which now throws for toIntervalQuarter(413844) while the plain function returns for a later time DISAGREE On master the same zero runs through the same loop and hangs (time 0 with toIntervalQuarter(413844) or toIntervalMonth(100000) never returns, also on 26.9.11.2), so such a query went from an uncancellable hang to an exception. An unchanged end on the first step depends only on the constant intervals, so it throws for every row, not just the hidden one. Skipping default arguments for hop/hopStart/hopEnd is already in my open #122356
2 ⚠️ The new error lines only covered the hang arm; nothing pinned that an unchanged end already before the time now throws instead of returning a window without the time AGREE, fixed test line with a 2023 time: master returns ('1970-01-01 23:57:52','1970-01-02 00:00:00'), the fix throws
3 💡 The in-loop comment said the loop would always spin forever, but with the end already before the time it exits AGREE, fixed comment names both cases
4 💡 26.8 still has windowID (executeHopSlice) with the same wend > wend_latest check noted, not blocking master has no windowID (#114747); relevant only to a 26.8 backport

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-slot-9:20261008-074000

@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. SELECT hopEnd(toDateTime('1970-01-01 01:02:07', 'Europe/Amsterdam'), toIntervalQuarter(413844), toIntervalQuarter(413844)) (the CI query, raw time 127) never returns on master; also toIntervalMonth(100000) with a materialized time, and toIntervalDay(33554432)/toIntervalDay(52543755) in US/Samoa. Killed by a 20 s timeout on every run.
b Root cause explained? The window-end loop in executeHop threw only when subtracting the hop made the end larger. A Date bound saturated at 0000-01-01 narrows to day 1368 in UInt16, and subtracting the hop from it saturates to the same value, so the end never changes and the loop never exits. A hop spanning a multiple of 2^32 seconds does the same for time kinds when the window starts after the time.
c Fix matches root cause? Yes. The loop terminates only if every iteration strictly decreases the end; the check now requires exactly that (>= instead of >) and throws the existing Time overflow error otherwise.
d Test intent preserved / new tests added? Yes. Four error lines and one sane Date-kind line added to 04891_time_window_functions_wrapped_interval_overflow; existing lines unchanged. 50/50 randomized runs pass.
e Both directions demonstrated? Yes. All four new lines throw with the fix; on a master build three hang and the fourth returns a window without the time; the edited test fails by timeout on a master server and passes with the fix.
f Fix is general across code paths? Yes. This loop is the only one of its kind left (windowID and the window view loops were removed earlier); all eight interval kinds and hop/hopStart/hopEnd go through it. tumble has no loop.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. Constant and column time arguments, Date and time interval kinds are tested. 17280 hop/hopStart/hopEnd queries over all kinds, five time zones and three time ranges give identical results before and after.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change. Only inputs that hung, or returned a window not containing the time, now throw the same error that #114607 introduced.
i Invariants and contracts preserved? Yes. The loop's strict-decrease invariant is now enforced; result columns and the error path (same code and message, thrown before any row is written) are unchanged.

Session id: cron:clickhouse-impl-slot-5:20261008-062800

@clickhouse-gh

clickhouse-gh Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [157482d]

Summary: ✅


AI Review

Summary

This PR hardens hop, hopStart, and hopEnd against the no-progress case in the backward window scan: when subtracting the hop leaves the window end unchanged or larger, the function now throws the existing Time overflow exception instead of hanging in an uncancellable loop or returning a window that does not contain the input time. I reviewed the changed loop, the wrapper entrypoints that reuse it, the prior PR discussion, and the added stateless regression coverage, and I do not see an unresolved correctness, safety, or contract issue in the current diff.

Final Verdict

Status: ✅ 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 Report

Measured on commit 157482d.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 8, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... label Oct 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 157482db9 with master 828144718 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of e689c35a8; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 7 s compile time in total, 7 of them have a recent master baseline.

Job report

@PedroTadim PedroTadim self-assigned this Oct 9, 2026
@PedroTadim
PedroTadim added this pull request to the merge queue Oct 9, 2026
Merged via the queue into ClickHouse:master with commit 54f24c7 Oct 9, 2026
152 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants