Repository navigation
Add a stateless test for the trailing empty chunk in IMergingTransformBase - #117259
PedroTadim merged 3 commits into
Conversation
…mBase The reproducer is @ tiandiwonder's, supplied in ClickHouse#116805 (comment). It contradicts the note in ClickHouse#117048 that the trigger cannot be constructed deterministically from SQL. Mechanism. `InputPort::pullData` caches `is_finished` with no `HAS_DATA` term, so a consumer can pull a chunk from a port that already reads as finished. `BufferChunksTransform`, which `SortingStep::mergingSorted` inserts for a read-in-order merge of more than one stream when `read_in_order_use_buffering` is on, drains its buffer exactly that way. Before ClickHouse#117048 the guard in `IMergingTransformBase::prepare` was `!input_chunk.hasRows() && !virtual_row && !input.isFinished()`, so the `!input.isFinished()` term let a rowless chunk arriving from an already finished port fall through to `state.has_input = true` and be consumed. `MergingSortedAlgorithm::consume` reset the cursor from that zero-row chunk and pushed it into the batch sorting queue; `mergeBatchImpl` then computed `insertable_rows == 0` and called `MergedData::rowsToInsertBeforeFlush` with `max_rows = 0`. In the test the four per-partition FINAL streams each end in rows whose `vals` is empty, the ARRAY JOIN turns those into the zero-row chunk, and that is the last chunk the buffer pushes while the merge is still consuming the other three partitions. Oracle, and its limit. This test's oracle is the `chassert(max_rows > 0)` in `MergedData::rowsToInsertBeforeFlush`, which exists only in debug and sanitizer builds; there it goes through `abortOnFailedAssertion`, so the observation is a server SIGABRT rather than a query error. That is the whole of what this test covers. It does not cover the release-visible consequence: `SortingQueueImpl::push` (`src/Core/SortCursor.h`) calls `std::push_heap`, so the zero-row cursor is compared, reading row 0 of zero-row sort columns, before control reaches `rowsToInsertBeforeFlush` at all. That is the row loss ClickHouse#117048's own commit message reports, and it stays covered by that PR's `TwoInputsTrailingEmptyChunk` gtest, not by anything here. @ tiandiwonder observed correct results on a release build in the configurations he tried; that is an observation over those configurations. Every setting in the query is load-bearing. @ tiandiwonder's ablation, counting the rowless pulls that reach the merge: as committed 4 without FINAL 0 without do_not_merge_across_partitions_select_final 0 without optimize_read_in_order 0 without read_in_order_use_buffering 0 max_block_size = 64 instead of 1 0 max_threads = 4 instead of 1 0 They are pinned per query rather than with SET, so the test runner's settings randomization cannot move them. Validation. Reverting only ClickHouse#117048's hunk on a debug build (`git checkout 49f3609^ -- src/Processors/Merges/IMergingTransform.cpp`) reddens this test 10 times out of 10, each time with the assertion firing in `rowsToInsertBeforeFlush` reached from `mergeBatchImpl` on the batch sorting queue. On master it passes 50 out of 50 with randomized settings, and 24 out of 24 when run against itself in parallel, so it needs no `no-parallel` tag. The second query is @ tiandiwonder's too, kept after measuring it. It needs `query_plan_remove_redundant_sorting = 0`: without that, the inner ORDER BY feeding an order-insensitive aggregate is removed, and with no sort there is no merge for the rowless chunk to reach, which is why his own sweep saw the wrapped form reach the state 0 times out of 30. With the setting pinned it reaches the assertion 3 times out of 3 on the reverted build. `count()` and `sum()` are order-insensitive, so it detects lost or duplicated rows, not reordering. A third statement pins the shape the two queries depend on: `EXPLAIN PIPELINE` must show buffering in front of a sorted merge. `count()` and `sum()` are invariant under pipeline shape, so without it a planner change that stopped inserting `BufferChunksTransform` for this shape would leave the test green while it covered nothing. It asserts presence as a boolean rather than an exact processor count, so neither the number of parallel copies nor the rendered line count can redden it. This does not make `gtest_merging_sorted_empty_chunk.cpp` redundant. `SortingStep::mergingSorted` builds the merge only when there is more than one stream and hard-codes `SortingQueueStrategy::Batch`, so the gtest's single-input case and its default-strategy case are both out of reach of this query; other callers reach the default strategy (`Interpreters/SortedBlocksWriter.cpp`, `Storages/MergeTree/MergeTask.cpp`) but not through this test's pipeline. The gtest also builds its inputs by hand, where this test drives the real pipeline. Related: ClickHouse#117048 Related: ClickHouse#116805 Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review (2 rounds) - 6 findings adjudicatedBefore opening this PR I ran two rounds of independent cold review of the resulting code plus a Round 1
Round 2
On finding 2 the gate asked for an exact-count assertion on the four Noted and not acted on: the second boolean in that statement is redundant, since |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20260831-035500 |
|
Workflow [PR], commit [dc6aa78] Summary: ✅ AI ReviewSummaryThis PR adds a stateless SQL regression test for the trailing empty-chunk path in Final Verdict✅ No blocking issues found in the current revision. |
|
cc @davenger, could you review this? It adds SQL-level coverage for #117048, a read-in-order |
…s pipeline shape Reported by clickhouse-gh[bot] on ClickHouse#117259. The second statement kept its six load-bearing settings on its inner subquery. `clickhouse-test` injects randomized settings as client options with `--allow_repeated_settings`, and a client option beats a subquery-level `SETTINGS` clause, so a randomized `optimize_read_in_order = 0`, which the runner draws from `randint(0, 1)`, dropped `BufferChunksTransform` from that statement's pipeline while `count` and `sum` still returned the reference values: the statement passed without observing the merge it covers. Same defect and same fix as `c11b082a534ec09` on `04489_virtual_row_disabled_with_final`. The bare statement was never affected, its `SETTINGS` clause is already outermost. Each runtime statement now has a shape assertion over the exact query it runs, so neither can lose its coverage silently. Measured on a debug build: setting `read_in_order_use_buffering = 0` in the new assertion turns its output to `0 1`, and letting `query_plan_remove_redundant_sorting` default to 1 turns it to `0 0`, which is the case the report describes, while the counts stay at `3000 / 4498500 / 4498500` either way. Validation: 50 runs under default randomization, 8 concurrent runners times 3 runs, and 3 runs each under `allow_experimental_analyzer = 0`, `serialize_query_plan = 1` and parallel replicas, no failures. Style checks produce no output. Related: ClickHouse#117048 Related: ClickHouse#116805
…dget `Stateless tests (amd_asan_ubsan, flaky check)` failed this test on 4 of 50 runs with `Test runs too long (> 180s)`, while its 46 passes ran 148.7-178.8 s: a mean sitting 1.2 s under a hard per-file ceiling, not a non-deterministic test. The flaky check is the only mode that enables the thread fuzzer, which sleeps on every `pthread_mutex_lock` and `unlock`, and both runtime statements read at `max_block_size = 1`, so their cost tracks the number of one-row chunks pushed through the merge. Reading 400 rows instead of 4000 cuts it by the same factor: with the fuzzer enabled and one debug binary, 583.8 / 587.8 s before (a third run hit the 600 s per-test kill) against 82.5 / 84.0 / 88.9 s after, which scales the failing lane's measured maximum to about 37 s. The two runtime selects fell from 304 s and 288 s to 31 s and 30 s, while the two inserts stayed near 9 s each, an insert's cost under the fuzzer being independent of its row count. Shrinking this fixture is not free, and getting it wrong is silent. The state is reached only when all four partitions hold the same number of rows with values and the same number without, so the row count and the threshold must both be multiples of the partition count. Three otherwise-identical fixtures that break that (1000/750, 200/150, 100/75) pass on the reverted build while still printing correct results and correct pipeline shapes. The new first statement pins the split, so editing either number cannot leave the test covering nothing: with the partitions split unevenly and the row counts held constant it prints `0 0`. With ClickHouse#117048's hunk reverted the shipped file still reddens 10 times out of 10. On master it passes 10/10, 50/50 under randomized settings, 24/24 against itself in parallel, and 3/3 each under `allow_experimental_analyzer = 0`, `serialize_query_plan = 1` and parallel replicas. Style checks produce no output. Related: ClickHouse#117048 Related: ClickHouse#116805 Co-Authored-By: Claude Opus 5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Add SQL-level regression coverage for the trailing empty chunk in
IMergingTransformBase::prepare.Description
The reproducer in this test is @ tiandiwonder's, supplied in
#116805 (comment). I am adding it to the
suite, with the comments trimmed and one of his two queries settled by measurement.
Related: #117048
Related: #116805
#117048 fixed
IMergingTransformBase::prepare: an input that finishes its port in the same cycle itdelivers a trailing empty chunk had that chunk consumed by the merge. This test reaches that state
through a real read-in-order pipeline, where
BufferChunksTransformis the producer and the tail ofeach partition holds only empty arrays. #117048's description says the trigger "cannot be constructed
deterministically from SQL"; this shows that it can.
Verification: reverting only #117048's hunk on a debug build reddens the test 10 times out of 10,
each time as
Logical error: 'max_rows > 0'inMergedData::rowsToInsertBeforeFlushreached fromMergingSortedAlgorithm::mergeBatchImplwith the batch sorting queue, which aborts the server. Onmaster it passes 50/50 under randomized settings and 24/24 run against itself in parallel. The
pipeline-shape checks, one over each of the two runtime statements, also hold unchanged under
allow_experimental_analyzer = 0andserialize_query_plan = 1, the settings the old-analyzer anddistributed-plan test lanes install, and drop to 0 when buffering is disabled.
This does not make
gtest_merging_sorted_empty_chunk.cppredundant.SortingStep::mergingSortedbuilds the merge only when there is more than one stream, and hard-codes
SortingQueueStrategy::Batch, so the gtest's single-input case and its default-strategy case areboth out of reach of this query; the gtest also builds its inputs by hand, where this test drives the
real pipeline.
The assertion is the only oracle this test carries: the empty cursor is heapified and compared before
that assertion is reached, so the release-visible row loss stays covered by the gtest's
TwoInputsTrailingEmptyChunk.countandsumin the second query detect lost or duplicated rows,not reordering.
Workflow [PR]
Sync PR [sync-upstream/pr/117259]
Version info
26.9.1.473(included in26.9and later)