Repository navigation
Conversation
…w that sorts earlier
When `FillingRow::next` advances a fill key onto the value of the next
original row, it resets the later keys to their `FROM` values (or to the
next row's values for keys without `FROM`) and decides whether to insert the
row. It accepted the row if ANY later key was less than the next row's, an
OR over the keys instead of a lexicographic comparison. For
SELECT * FROM values('i Int64, j Int64, k Int64', (0, 0, 1), (1, 1, 2))
ORDER BY i WITH FILL, j WITH FILL FROM 2, k WITH FILL FROM 1
the reset row (1, 2, 1) has j = 2 > 1 but k = 1 < 2, so it was emitted
before (1, 1, 2), and an outer ORDER BY over it returned the same rows
because the input is advertised as sorted. Inserted while still greater
than the previous original row, the same row was then inserted again by the
pending-row placement at the next original row (a duplicate).
`FillingRow::shift` (the STALENESS prefix path) has the identical block.
Both now call one helper, `resetKeysAfter`, which keeps the assignments and
the per-key test and lets the first key that differs from the next original
row decide; a row equal to it is not less. This only refuses rows: a refused
row is placed by the existing pending-row logic after the original row, or is
not emitted when nothing follows (like the `FROM` row of a single key above
the data). Also no longer emitted: a generated row identical to the next
original row (`Nullable` keys, NULL equals NULL), and one whose deciding key
equals its exclusive `TO` (`FROM 2 TO 2`).
Related: ClickHouse#118077 (comment)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
… do not sort first
The previous commit replaced the OR over the reset keys by the decision of
the first differing key alone. That also refused rows that sort before the
next original row when the deciding key has an empty `FROM = TO` range
(the constraint conjunct), which master inserts in order and continues
from. The fill cursor then stayed at the refused row, and the next gap
added rows master never emits, e.g. (1, 2, 2) after (1, 3, 2) for
SELECT * FROM values('i Int64, j Int64, k Int64', (0, 0, 1), (1, 3, 2), (1, 3, 5))
ORDER BY i WITH FILL, j WITH FILL FROM 2 TO 2, k WITH FILL FROM 1
Now the original OR decides as before and the row is additionally refused
when it does not sort before the next original row (first differing later
key decides), so every accepted row is one master accepts too.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ecides the order
The previous commit refused a reset row whenever its first differing later key
was not less than the next original row's. `less` orders NaN by direction only,
while the sort places it by `NULLS FIRST/LAST` (last by default, for both
directions), so a row that sorts first was dropped, e.g. (1, 0, 1) for
SELECT * FROM values('i Int64, j Float64, k Int64', (0, 0, 0), (1, nan, 2))
ORDER BY i WITH FILL, j DESC WITH FILL FROM 0, k WITH FILL FROM 1
It also ignored the keys before the advanced one, which can already make the row
sort first (a key at its `TO`, or a NULL marker).
Now the row is refused only when it equals the next original row up to the
advanced key and the first differing later key is a non-NULL, non-NaN value that
is not less; every other row is decided as before. The test gains two blocks
pinned to the existing output: an empty `FROM = TO` range and a NaN under `DESC`.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test gains a block where a key before the advanced one sits at its `TO`, so the row sorts first by that key and master's decision is kept. The declaration comment of `resetKeysAfter` states the NULL/NaN exception. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the resetKeysAfter header comment to two lines (now also states the all-equal refusal) and drop the duplicated contract from the .cpp comment, keeping only the NULL/NaN ordering 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; 8 findings over four rounds) plus my own cold review.
Also noticed, not in this PR (separate code, unchanged): a fill key sitting at its Severity: ❌ blocker / |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261117-021403 |
|
Workflow [PR], commit [d426cb4] Summary: ✅
AI ReviewSummaryPR #132201 fixes out-of-order and duplicate generated rows when later Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units9 translation units recompiled, 25 s compile time in total, 9 of them have a recent master baseline. |
With FROM on a fill column the change of the first commit made results unsorted where master returns them sorted. It lets the later columns be filled inside a group whose earlier key is past its TO, and there the reset of the following columns to their FROM can put the filling row behind an original row (the defect fixed separately in ClickHouse#132201): - over (3,0,0,1),(3,1,1,2),(3,1,2,3) `ORDER BY i WITH FILL TO 1, j WITH FILL, k WITH FILL FROM 2, l WITH FILL FROM 1` it returned (3,1,2,1) before (3,1,1,2), and `SELECT DISTINCT i, j, k` over it aborted a debug build; - over (0,0,0,1),(2,-3,2,0),(2,1,0,3) `ORDER BY a WITH FILL TO 0 STEP 2 STALENESS 3, b WITH FILL STEP 4, c WITH FILL FROM 0 TO 0, d WITH FILL TO 2` the STALENESS reset left c at its FROM 0 and it returned (2,-3,0,1) after (2,-3,2,0). Both were found by the AI review on the PR. A fill column with FROM now keeps the previous comparison like a Nullable or floating-point one. Also measured a narrower alternative that changes only a key exactly at its TO and keeps the early return for a key past it: it returns 25 of 18000 random cases unsorted that master returns sorted (the walk of the later columns then starts from a filling row left behind by that early return), so it was not used. Measured over random 2-4 key cases (Int64, random ASC/DESC, TO, FROM, STEP, STALENESS, a sorting prefix; Nullable(Int64) with NULLs; Float64 with inf/-inf/nan), output sorted under the ORDER BY and every input row present, 30000 cases: none is unsorted where master is sorted; every case with a FROM, a Nullable or a floating-point column (13063) is identical to master; the unsorted master results without them (plain 173/3000, STEP 109, STEP with STALENESS 64, sorting prefix 37) are all sorted now. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #118077 (comment)
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
ORDER BY ... WITH FILLwith several fill keys emitting a generated row before an original row that sorts earlier, or emitting it twice, when a later fill key restarts from itsFROMvalue. Such a row is now emitted after that original row or not at all.Description
On 26.10.3.7 this query returns
(1, 2, 1)before(1, 1, 2), violating its ownORDER BY:An outer
ORDER BY i, j, kkeeps that order: its sort is elided. With a third input row it is emitted twice.When
FillingRow::next(orFillingRow::shiftfor STALENESS) advances a key onto the next original row, it resets the later keys toFROMand inserts the row if ANY later key is less than the next row's. The row is now refused when it equals the next row up to the advanced key and the first later key that differs is not less.The change only stops emitting rows: a refused row stays pending and is emitted after the original row, or dropped when nothing follows (as for a single key's
FROMabove the data), and a generated row identical to the next original row is no longer emitted. Every other row is decided as before, also when the deciding key isNULLorNaN. Unchanged:NULLS FIRSTon a later fill key (#118096 fixes one key) and a non-fill key between fill keys (#118077).Validation: the new test fails on master in the five bug cases and keeps master's output for an empty
FROM = TOrange, aNaNunderDESCand an earlier key at itsTO. The 115 existingWITH FILLtests pass. A randomized A/B sweep against master (4,431 multi-key shapes,TOon any key, emptyFROM = TOranges) and aNaNgrid found no new ordering or duplicate violation and no row master does not emit.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/132201]