Skip to content

Reject a non-finite WITH FILL bound - #118434

Merged
yakov-olkhovskiy merged 10 commits into
masterfrom
with-fill-to-non-finite-118122
Sep 29, 2026
Merged

yakov-olkhovskiy merged 10 commits into
masterfrom
with-fill-to-non-finite-118122

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

A NaN or Inf value is not usable as any WITH FILL parameter, and each one fails in its own way.
Towards the fill direction, NaN compares greater than every value in the totalized Field order, so the
loop's termination test in FillingRow::next is always true and the query generated fill rows until it hit
a time limit, and forever without one:

SELECT count() FROM (SELECT toFloat64(number) AS x FROM numbers(3) ORDER BY x WITH FILL TO nan STEP 1);
-- never returns; with max_execution_time = 3 it dies with TIMEOUT_EXCEEDED

FillingRow::next guards the fill cursor and the data rows against NaN/Inf (the fixes for #69261), but
compares them against the loop constraint unguarded, and validation accepted the bound because
less(0, nan) reads as a positive fill direction. A LIMIT does not stop it either. STALENESS reaches
the same constraint: updateConstraintsWithStalenessRow derives it from the staleness value exactly like
from TO, and writes it into the same array.
A TO bound the fill runs away from does not hang, it is passed at once and nothing is filled. A
non-finite FROM or STEP makes the cursor itself non-finite, which is where the guards inside
FillingRow::next stop the fill, so nothing is filled either — and for FROM the bound has by then been
emitted as a row, replacing the fill a perfectly good TO asked for. Measured on 26.10.1.65 over the rows
0.0 and 1.0:

ORDER BY x WITH FILL FROM -inf TO 3 STEP 1    ->  -inf, 0, 1
ORDER BY x WITH FILL FROM 0    TO 4 STEP nan  ->  0, 1
ORDER BY x WITH FILL FROM -1   TO 3 STEP 1    ->  -1, 0, 1, 2

All four parameters are rejected now with INVALID_WITH_FILL_EXPRESSION, like the other unusable
WITH FILL combinations. The check lives in checkFillDescription, so it covers the planner and the query
plan deserializer alike.
Of the eighteen rejected shapes, nine hung before and nine returned rows, so this is a small
backward-incompatible tightening for values that could never produce a correct fill. The same precedent
already exists for a vacuous TO beyond FROM.
Closes: #118122
Related: #118438

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 to CHANGELOG.md):

A non-finite FROM, TO, STEP or STALENESS value in ORDER BY ... WITH FILL (nan, inf or -inf)
is now rejected with INVALID_WITH_FILL_EXPRESSION. Previously such a value either made the query generate
fill rows endlessly (TO nan, TO inf ascending, TO -inf descending, and the same values for
STALENESS) or produced a wrong result: a non-finite FROM emitted the bound itself as a row and dropped
the fill rows that TO asked for, while a non-finite STEP, or a TO bound the fill runs away from,
returned the rows unfilled. Pass a finite value instead, or omit it.


Workflow [PR]
Sync PR [sync-upstream/pr/118434]

Version info

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

`ORDER BY x WITH FILL TO nan` (and `TO inf`) generated fill rows until the query hit a time limit, and
forever without one: `FillingRow::next` guards the fill cursor and the data rows against `NaN`/`Inf`,
but compares them against the `TO` bound unguarded, and `NaN` compares greater than every value in the
totalized `Field` order, so the loop's termination test was always true. A `LIMIT` does not stop it
either, and validation accepted the bound because `less(0, nan)` reads as a positive fill direction.

Such a bound is rejected now with `INVALID_WITH_FILL_EXPRESSION`, like the other unusable `WITH FILL`
parameter combinations, on both analyzers. `FROM` and `STEP` are left alone: a non-finite value there is
already handled by the run-time guards in the fill loop and those queries return correct results.

Closes: #118122

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e667913]

Summary: ✅


AI Review

Summary

This PR closes the non-finite WITH FILL family by rejecting NaN/Inf in FROM, TO, STEP, and STALENESS through the shared checkFillDescription path, so both SQL analyzers and the serialized-plan deserializer enforce the same contract. I re-checked the previously discussed gaps against the current code, traced the values through FillingRow and FillingTransform, and did not find a remaining correctness hole in the user-reachable paths covered by this change. The Bug Fix changelog metadata is appropriate, the changelog entry is specific and user-readable, and the current PR CI is all green.

Final Verdict

✅ No new findings in the current diff.

LLVM Coverage Report

Measured on commit e667913.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.40% 79.30% -0.10%

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

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. labels Sep 6, 2026
Comment thread src/Planner/PlannerSorting.cpp Resolved
@zlareb1

zlareb1 commented Sep 6, 2026

Copy link
Copy Markdown
Member

TL;DR: the check closes the TO route into constraints[], but the same non-finite value still enters constraints[] through STALENESS, and the resulting infinite loop is live at pure defaults. One more all-finite non-termination in the same loop is out of this PR's stated scope and filed separately as #118438.

  1. STALENESS nan (and inf) still loops forever, same signature as the defect this PR fixes: updateConstraintsWithStalenessRow (src/Interpreters/FillingRow.cpp, around lines 325-337) writes base_value + staleness into the same constraints[] array that FillingRow::next compares unguarded, and the staleness value is accepted by validation because accurateLess(nan, 0) is false. Verified live (26.8.2.7, 26.9.1.1, master 26.9.1.840):
SELECT count()
FROM (SELECT toFloat64(number) AS x FROM numbers(3) ORDER BY x WITH FILL STEP 1 STALENESS nan)
SETTINGS max_execution_time = 3;  -- TIMEOUT_EXCEEDED; runs forever at the default 0

Extending the same finiteness check to fill_staleness in both analyzers would close the family.

  1. For completeness of the enumeration: three previously-terminating vacuous shapes now throw (ASC ... TO -inf STEP 1, DESC ... TO inf STEP -1, DESC ... TO nan STEP -1 each returned the data rows unchanged before this change). That looks consistent with the existing precedent of rejecting a vacuous TO < FROM, so probably fine — noting it in case any of them appears in the wild.

  2. Related but a different mechanism (all values finite, so no validation of this kind can fully catch it): a STEP below the ulp of the cursor value makes cursor + step == cursor and the loop never advances — filed as WITH FILL with an all-finite Float64 range loops forever at default settings when STEP is below the cursor's ulp (cursor + step == cursor, no progress guard) #118438 with reproducers.

@clickhouse-gh

clickhouse-gh Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing e6679138e with master e96f004b9 (stripped binary size, per-symbol sizes and ThinLTO time; 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, 9 s compile time in total, 7 of them have a recent master baseline.

Job report

alexey-milovidov and others added 2 commits September 11, 2026 08:18
…nite-118122

# Conflicts:
#	src/Interpreters/InterpreterSelectQuery.cpp
#	src/Planner/PlannerSorting.cpp
…on moved to `checkFillDescription`

Master unified the `WITH FILL` validation of both analyzers into `checkFillDescription`, so the finiteness
check lives there now and covers both analyzers at once.

`STALENESS` reaches the same fill loop as `TO`: `updateConstraintsWithStalenessRow` derives the loop
constraint from it exactly like from `TO`, and `FillingRow::next` compares that constraint unguarded, so
`ORDER BY x WITH FILL STEP 1 STALENESS nan` (also `inf`, and `-inf` with `DESC`) kept generating the suffix
forever, the same defect this change fixes for `TO`. Both are rejected with `INVALID_WITH_FILL_EXPRESSION`
now, and the stateless test covers the `STALENESS` shapes on both analyzers.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Updated: merged (which had unified the validation of both analyzers into checkFillDescription) and extended the finiteness check there to cover fill_staleness as well as fill_to, with STALENESS nan / inf / DESC ... -inf cases added to 05112_with_fill_to_non_finite and a positive control that a finite STALENESS 2 still fills. Thanks @zlareb1 — that closes the family; the ulp-sized STEP non-termination (#118438) is a different mechanism and stays out of scope here.

On the two CI reds of the previous commit:

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 (fixing the two words the shell ate in the first sentence above: merged master, which had unified the WITH FILL validation of both analyzers into checkFillDescription.)

@groeneai

Copy link
Copy Markdown
Collaborator

It is already in progress, and it is yours: #118094 (default memory_worker_correct_memory_tracker to 1, so an upward drift stops being permanent), #117738 (the one-shot correction re-baselined to jemalloc stats.allocated instead of resident), and the draft #117995 (stop naming this row a deadlock). All three took commits yesterday, so I am not opening anything beside them.

The open issue for the signature is your own #117681 (the usage side far above real usage, every query refused), plus #113060 for the per-user tracker. Neither is reachable from the CI row: the row is named Hung check failed, possible deadlock found and the fault has no name of its own, which is what #117995 fixes. My own root-cause note for this signature is a comment on #113408 rather than an issue, so it is not findable either.

The run you linked is that class: would use frozen at 160.97/160.98 GiB while current RSS moves underneath it (1.71 to 1.73 GiB) against maximum: 47.65 GiB, with attempt to allocate chunk of 0.00 B. Same shape as the master run in your table.

Re-measured today over 30 days, test_name = 'Hung check failed, possible deadlock found' carrying a memory-limit message: 25 rows, of which 13 are this drift shape (12 PRs plus one master run), 8 are ordinary pressure and 4 have no parseable RSS. Five of the 13 postdate the last row of your table, the largest being 2026-09-10 on Stress test (arm_debug) at would use 228.12 GiB against current RSS: 5.17 GiB (report).

Two notes on the part that is not in progress, where the steps come from:

  • Your "a multiple of about 32 GiB" is sharper than "about". 12 of the 13 drifts are within 0.08 of an integer multiple of 32 GiB, worst residual 2.4 GiB (k = 2 in seven rows, 5 in three, 3 and 7 once each). The exception, at k = 2.80, is also the row with the highest RSS (10.85 GiB), and RSS keeps moving while the message repeats.
  • MemoryAllocatedWithoutCheck and MemoryAllocatedWithoutCheckBytes cannot attribute a step. Their only call site is MemoryTracker.cpp:553 in commitAllocation, and the MemoryTrackerBlockerInThread branch at :281-303 does its own amount.fetch_add and returns before reaching it. Bytes entering under a blocker land in MemoryTracking but read 0 in those two events.

One thing worth deciding: #117738 says Closes: #117681, while #118094 says #117681 tracks the unknown origin. When #117738 merges, the origin loses its tracker and the signature is back to having no open issue.

I will take the origin of the steps and report on #117681, unless you would rather I stayed off it.

…nite-118122

# Conflicts:
#	src/Core/SortDescription.cpp

Co-authored-by: yakov <[email protected]>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ alexey-milovidov
❌ cursoragent
You have signed the CLA already but the status is still pending? Let us recheck it.

@yakov-olkhovskiy

Copy link
Copy Markdown
Member

FROM and STEP are left alone: a non-finite value there is
already handled by the run-time guards in the fill loop and those queries return correct results.

this is not correct - ORDER BY x WITH FILL FROM -inf TO 3 STEP 1 produces -inf, 0, 1
I'm taking over this PR

cursoragent and others added 2 commits September 18, 2026 03:52
`FROM` was left out of the finiteness check on the grounds that the run-time
guards in `FillingRow::next` already handle it and the query returns correct
results. They do not: a non-finite `FROM` puts the fill cursor exactly where
those guards stop the fill, so the bound itself is emitted as a row and the
fill rows a finite `TO` asks for are silently dropped. Measured on 26.10.1.65:

    ORDER BY x WITH FILL FROM -inf TO 3 STEP 1  over {0.0, 1.0}  ->  -inf, 0, 1
    ORDER BY x WITH FILL FROM -1   TO 3 STEP 1  over {0.0, 1.0}  ->  -1, 0, 1, 2
    ORDER BY x DESC WITH FILL FROM inf STEP -1  over {0,1,2}     ->  inf, 2, 1, 0

Wrong results are worse than the hang this change started from, so `FROM` is
rejected on the same rule as `TO` and `STALENESS`.

Also replace the local lambda with `Field::isNaN` and `Field::isInf`, which are
the same predicate and are what `FillingRow::next` already uses for the row side
of this invariant, and drop the `NaNUtils.h` include that the lambda needed.

Co-authored-by: yakov <[email protected]>
Three things were missing or stale:

* The shapes that do not hang but fill nothing (`ASC ... TO -inf`, `DESC ... TO
  nan`, `DESC ... TO inf`) returned their rows before and throw now. They are
  the only user-visible regression risk here, so they are pinned rather than
  left as an unstated side effect of the check.
* `SETTINGS enable_analyzer = 0` no longer selects the old analyzer: since v26.9
  the setting is frozen at `1` and disabling it is rejected with
  `SETTING_CONSTRAINT_VIOLATION`, so those cases asserted the wrong error after
  merging `master`. Dropped, along with the `FROM`/`STEP` claim they backed.
* A bound reached through an expression or a cast (`TO -log(0)`,
  `TO CAST('inf', 'Float32')`) is checked after evaluation, not only as a
  literal, and both hung before.

Renamed off `..._with_fill_to_non_finite`, which named only the `TO` bound, and
renumbered to the next free prefix, 05112 having gone stale.

Co-authored-by: yakov <[email protected]>
@cursor cursor Bot changed the title Reject a WITH FILL TO bound that the fill loop can never pass Reject a non-finite WITH FILL bound Sep 18, 2026
`STEP` was the last parameter left accepting `NaN`/`Inf`, and it fails the same
way `FROM` does: the first step makes the cursor non-finite, which is where the
guards inside `FillingRow::next` stop the fill, so the query silently returns
its rows unfilled. Measured on 26.10.1.65 over the rows 0.0 and 1.0:

    ORDER BY x WITH FILL FROM 0 TO 4 STEP nan  ->  0, 1
    ORDER BY x WITH FILL FROM 0 TO 4 STEP 1    ->  0, 1, 2, 3

Rejecting it leaves no `WITH FILL` parameter where a non-finite value is taken
and quietly ignored, which is the point of the check: a bound that cannot
produce a fill should say so instead of returning a plausible-looking result.

An `INTERVAL` step is unaffected - `step_kind` implies an integer or decimal
value, which is never non-finite.

Co-authored-by: yakov <[email protected]>
Comment thread src/Core/SortDescription.cpp Resolved
alexey-milovidov and others added 2 commits September 18, 2026 11:48
`deserializeSortDescription` is a client-facing entrypoint of its own: a forged query plan reaches
`checkFillDescription` without passing through the parser or either analyzer, so the stateless test
`05227_with_fill_non_finite_bound` does not prove that a serialized `NaN`/`Inf` bound is rejected there.
Add the missing `expectRejected` cases for `fill_from`, `fill_to`, `fill_step` and `fill_staleness`,
asserting the finiteness message so that a regression cannot silently shift the rejection to another
check, and let `expectRejected` assert the message text.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Updated. @yakov-olkhovskiy — noted that you are taking over; this pass only did the mechanical work, nothing that conflicts with your commits.

  • Merged master (the branch was 68 commits behind and CONFLICTING; the merge was clean, e7ea795adda7). That also clears the three Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) reds — 03262_filter_push_down_view, 01414_push_predicate_when_contains_with_clause and 03174_exact_rows_before_aggregation. They are not caused by this change: on master itself (pull_request_number = 0) the same three tests fail in the same single shard of that job, 15 times each, starting 2026-09-17 19:36 and last at 2026-09-18 08:10, and every run from 2026-09-18 09:35 onwards is green again — a master regression that was fixed there, so merging pulls the fix in.
  • Fixed the Finish Workflow red (ci/jobs/scripts/workflow_hooks/pr_body_check.py). The description had no blank line between the changelog category and the ### Changelog entry heading, and get_category reads the line after the category as a second category: More than one changelog category specified. Only the blank line was added; the text is unchanged, and the hook passes against the new body locally.
  • Addressed the one unresolved review thread (the serialized-plan gtest matrix) in 7c8c69c4c99b: RejectsNonFiniteFillDescription walks NaN, Inf and -Inf through fill_from, fill_to, fill_step and fill_staleness at the deserializeSortDescription boundary, asserting the ... must be finite message so the rejection cannot silently move to another check. 05227_with_fill_non_finite_bound and all 5 tests of the gtest suite pass locally; of the 55 with_fill stateless tests, 45 pass and the 10 failures are all CLUSTER_DOESNT_EXIST / ALL_CONNECTION_TRIES_FAILED from the bare local config, not from this change.

One thing that needs a human: the CLA check is red because fbe2245ad4f1, c41d19abb1f1, 7684dbdf3465 and 07dbae1f3fad are authored by Cursor Agent <[email protected]>, which has not signed the CLA. Since we do not rewrite published history here, that has to be resolved by you — either sign for that identity or re-author those four commits.

Performance Comparison (arm_release, master_head, 5/6) flagged limit_by #4 as slower; this change only rejects a query at analysis time and touches no execution path, so that is re-measured by the fresh run rather than acted on here.

@yakov-olkhovskiy
yakov-olkhovskiy added this pull request to the merge queue Sep 29, 2026
Merged via the queue into master with commit f72b2e0 Sep 29, 2026
348 of 350 checks passed
@yakov-olkhovskiy
yakov-olkhovskiy deleted the with-fill-to-non-finite-118122 branch September 29, 2026 02:13
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 29, 2026
alexey-milovidov added a commit that referenced this pull request Oct 1, 2026
The previous commit left every generated value to the runtime checks in
`FillingRow::next`. That is exact, but for a fill with `FROM` it reports a
certain wraparound only after generating every value up to the boundary of
the column type: `WITH FILL FROM 0 TO 4294967297` over a `DateTime` column
first generates 2^32 rows. A fill with `FROM` generates the same sequence for
any data (even an empty input), so judging it up front is exact as well, and
it fails fast - restore those checks (`TO` against the last generated value,
the `INTERVAL` walk, the `Int64` carrier wrap).

Only the up-front check of fills without `FROM` stays removed: it had to
approximate the data-anchored sequence and rejected queries that never wrap -
an empty input filled `TO 1025` over `UInt8`, and
`ORDER BY d DESC WITH FILL TO -5 STEP INTERVAL -1 YEAR` over `Date`, which
stops at `1970-01-01` because the calendar arithmetic saturates there.

Also adapt the test to #118434,
merged from master, which rejects a non-finite `TO` up front.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. 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

7 participants