Skip to content

Fix logical error Logical error: Trying to extract chunk from ChunkBuffer before all inputs are finished in correlated subqueries - #95080

Merged
alexey-milovidov merged 6 commits into
masterfrom
fix-chunk-buffer-double-onfinish
Feb 4, 2026
Merged

alexey-milovidov merged 6 commits into
masterfrom
fix-chunk-buffer-double-onfinish

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jan 25, 2026 •

Copy link
Copy Markdown
Member

The ISimpleTransform::prepare method can call onFinish from two different code paths: when the output port finishes and when the input port finishes. This caused SaveSubqueryResultToBufferTransform::onFinish to call chunk_buffer->onInputFinish multiple times, leading to an underflow of unfinished_inputs counter in ChunkBuffer.

Added a finished flag to ensure onInputFinish is only called once, and added a chassert in ChunkBuffer::onInputFinish to catch underflow issues in debug builds.

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

See https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=95068&sha=bdb2c3ebf0d614b433e0d2861f7d32951a43ab79&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29&name_1=AST%20fuzzer%20%28amd_debug%29
#95068

Fixes #93832.

Version info

  • Merged into: 26.2.1.233

The `ISimpleTransform::prepare` method can call `onFinish` from two
different code paths: when the output port finishes and when the input
port finishes. This caused `SaveSubqueryResultToBufferTransform::onFinish`
to call `chunk_buffer->onInputFinish` multiple times, leading to an
underflow of `unfinished_inputs` counter in `ChunkBuffer`.

Added a `finished` flag to ensure `onInputFinish` is only called once,
and added a `chassert` in `ChunkBuffer::onInputFinish` to catch
underflow issues in debug builds.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jan 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7232670]

Summary: ❌

job_name test_name status info comment
Stress test (amd_tsan) failure
Logical error: Block structure mismatch in A stream: different number of columns: (STID: 0993-2da1) FAIL cidb, issue
Stress test (amd_ubsan) failure
Logical error: Unexpected return type from A. Expected B. Got C. Action: (STID: 1611-3d19) FAIL cidb, issue
Upgrade check (amd_asan) failure
Error message in clickhouse-server.log (see upgrade_error_messages.txt) FAIL cidb
Upgrade check (amd_msan) failure
Error message in clickhouse-server.log (see upgrade_error_messages.txt) FAIL cidb

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jan 25, 2026
@alexey-milovidov

alexey-milovidov commented Jan 25, 2026 •

Copy link
Copy Markdown
Member Author

The fix itself could be better, let's ask @novikd

alexey-milovidov and others added 3 commits January 25, 2026 20:06
…streams

The previous commit added a `finished` flag to prevent double `onFinish`
calls, but the root cause was that `addSimpleTransform` creates transforms
for main streams plus totals/extremes streams, while `setInputsNumber`
was only counting main streams. This caused a mismatch that led to
underflow of the `unfinished_inputs` counter.

This commit fixes the root cause by using the `StreamType` overload of
`addSimpleTransform` to only create transforms for main streams. The
totals/extremes streams will pass through unchanged.

Note: Correlated subqueries with WITH TOTALS will return NULL for the
totals row. This is a known limitation of the common subplan buffering
optimization when combined with totals.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
The correlated subquery pattern in this test requires the new analyzer
(the old analyzer doesn't support referencing outer columns like `t0.c0`
in correlated subqueries).

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Comment on lines +58 to +62
size_t streams = pipeline.getNumStreams();
if (pipeline.hasTotals())
--streams;
if (pipeline.hasExtremes())
--streams;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added a correct calculation of the number of inputs.

@novikd novikd self-assigned this Jan 26, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Ok, but now the test fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci 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.

Logical error: Trying to extract chunk from ChunkBuffer before all inputs are finished

3 participants