Repository navigation
Do not lose the held back rows of a preliminary DISTINCT when its input finishes concurrently - #132147
Do not lose the held back rows of a preliminary DISTINCT when its input finishes concurrently#132147groeneai wants to merge 1 commit into
DISTINCT when its input finishes concurrently#132147Conversation
…ut finishes concurrently A preliminary `DistinctTransform` that coalesces its output (ClickHouse#114550) emits the rows it still holds once its input is exhausted, from `prepare`. It checked `input.isFinished()` and then called `ISimpleTransform::prepare`, which checks the port again and finishes the output when the input is finished. The upstream finishes the shared port from its own `prepare` on another executor thread, so the port could become finished between the two checks: the first one saw an open input and kept the rows, the second one finished the output with the rows still held, and the later port update of the finished node is ignored. A plain `SELECT DISTINCT` read by several streams then returned fewer rows (05401 on `Stateless tests (arm_asan_ubsan, azure, targeted)` lost exactly the 133 keys of one stream's end-of-input chunk). While rows are held, `prepare` now observes the input once: it flushes when the input is finished, otherwise it requests data and returns `NeedData` when there is none, and delegates to the base class only when the base class cannot see the input finish (the flushed rows are pushed before the input is looked at, or the input has a chunk that only this processor can pull). `PortFull` / `NeedData` and the `setNeeded` / `setNotNeeded` calls mirror what the base class does in the same states. Validated by widening the window with a 500 us sleep at the equivalent point of the old and the new code (8 concurrent `clickhouse local` loops of the 05401 query with the CI settings and a 6-stream read): old code 139 of 6400 results wrong, each one a lost end-of-input chunk (including the CI result byte for byte); new code 0 of 6400. 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 (1 nit, the same sibling).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261116-213601 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20261116-210201 |
|
Workflow [PR], commit [ad6eb05] Summary: ❌
AI ReviewSummaryThe change prevents preliminary Final VerdictStatus: ✅ Approve |
CI finish ledger - ad6eb05Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261117-003016 |
Related: #114550
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Not required.
Description
05401_aggregation_nullable_fixed_keys_batch_packgot 17013 instead of 17146 rows from a plainSELECT DISTINCT a, nbinStateless tests (arm_asan_ubsan, azure, targeted), twice, byte-identical (1, 2). On a remote disk the table is read by 6 streams, so theDISTINCTruns in parallel with one preliminaryDistinctTransformper stream, which coalesces its output (#114550) and emits the rows it still holds when its input is exhausted.DistinctTransform::preparecheckedinput.isFinished()and then calledISimpleTransform::prepare, which checks the port again and finishes the output when the input is finished. The upstream finishes the port from its ownprepareon another thread, so the input can become finished between the two checks: the first keeps the rows, the second finishes the output without them. The 133 missing keys are exactly the end-of-input chunk of the second stream.While rows are held,
preparenow looks at the input once: it flushes when the input is finished, otherwise it requests data, and it calls the base class only in states where the base class cannot see the input finish.FillingTransform::prepareandBuildRuntimeFilterTransform::preparecheck the input before delegating in the same way; they are not changed here.Validation: with a 500 us sleep at the equivalent point of the old and the new code, 8 concurrent
clickhouse localloops of the 05401 query with the CI settings return 139 wrong results of 6400 on master, each one a lost end-of-input chunk (one of them the CI result), and 0 of 6400 with the fix. No new test: the race needs a thread switch between two loads in one call;05401stays the coverage. The code is master-only (not in 26.10).Workflow [PR]
Sync PR [sync-upstream/pr/132147]