Skip to content

Optimize Stream Aligner - #116706

Merged
Michicosun merged 37 commits into
masterfrom
optimize_stream_aligner
Sep 21, 2026
Merged

Michicosun merged 37 commits into
masterfrom
optimize_stream_aligner

Conversation

@Michicosun

@Michicosun Michicosun commented Aug 27, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

This patch slightly optimizes the AlignStreams streaming plan step.


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

Version info

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

@clickhouse-gh

clickhouse-gh Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [12d94a0]

Summary: ❌

job_name test_name status info comment
Build profile diff ERROR

AI Review

Summary

This PR splits STREAM ... WATERMARK reads into a narrow metadata subplan plus a filtered data subplan, aligns them with the new AlignStreamsStep, materializes _time_attribute as a reader virtual, and rejects UNORDERED watermark streams during analysis. The _time_attribute cleanup is useful, but the new split still changes watermark semantics under realistic filters/chunking, and the new filter placement reopens a row-policy security gap, so I do not think this is ready to merge.

Findings

❌ Blockers

  • [src/Storages/MergeTree/Streaming/ReadingPlan/buildReadRoundPipeline.cpp:131-155] Moving row_level_filter and PREWHERE directly above the ReadFromMergeTree child makes tryMergeFilters/optimizePrewhere eligible again, but makeStreamingSelectQueryInfo has already cleared query_info.row_level_filter. Once the merged condition is pushed back into the source it comes back only as plain prewhere_info, so hidden rows can still execute the user predicate before the row policy removes them. A query like ... STREAM PREWHERE intDiv(1, secret_col) = 0 under a policy tenant_id = 42 can therefore leak protected rows through exceptions or timing again. Keep the row policy inside the read step (and only externalize the user filter), or explicitly disable PREWHERE pushdown on the streaming path when a row policy is present.
  • [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/116706#discussion_r3881237906] [src/Storages/MergeTree/Streaming/ReadingPlan/AlignStreams.cpp:124-167] The new aligner still emits rows past an earlier watermark whenever the metadata and data subplans cut different chunk boundaries. canReleaseData only waits for the last queued watermark to cover right_cursor, while canReleaseWatermark only checks held_data->left_cursor > boundary, so a single data chunk spanning rows before and after W1 is released before W1. That still changes late-data/window semantics for out-of-order event times. The fix needs either row-level splitting at watermark boundaries or a proven invariant that both subplans produce identical chunking.
  • [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/116706#discussion_r3881774914] [src/Storages/MergeTree/Streaming/ReadingPlan/buildReadRoundPipeline.cpp:131-159] The data-side progress signal still disappears when an entire interval is filtered out. ExtractBoundariesTransform stamps only surviving data chunks, and AlignStreamsProcessor advances watermark release only from held_data / data_progress, so metadata can emit a watermark for rows 1..100 while a PREWHERE drops that whole range and the marker waits until some later surviving row or end-of-round. That makes watermark firing depend on filter selectivity instead of the unfiltered source. Preserve cursor advancement through filtered-out ranges, for example by stamping boundaries before the filters or by carrying explicit progress markers from the unfiltered read path.

⚠️ Majors

  • [src/Storages/MergeTree/Streaming/ReadingPlan/buildReadRoundPipeline.cpp:135] A direct cursor-column predicate now removes a column that later streaming steps still require. For ... STREAM BOUNDED PREWHERE _block_number, this FilterStep honors remove_prewhere_column = true and erases _block_number before StampPartitionCursorsStep/AlignStreamsStep, so the pipeline fails on getPositionByName(_block_number) instead of executing the query. Force remove_column = false for _block_number / _block_offset once filters move ahead of cursor stamping, or reject those predicates explicitly.
Final Verdict

Changes requested. The current split-stream pipeline still has correctness/security gaps around watermark ordering and filter handling that should be fixed before merge.

LLVM Coverage Report

Measured on commit 12d94a0.

Metric Baseline Current Δ
Lines 88.50% 88.50% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 80.80% 80.70% -0.10%

Changed lines: Changed C/C++ lines covered: 239/257 (93.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Aug 27, 2026
@Michicosun
Michicosun force-pushed the optimize_stream_aligner branch from e7f26cc to 9f69462 Compare August 27, 2026 13:19
@clickhouse-gh

clickhouse-gh Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing commit 12d94a0e9d276b27d61ceefdd70458a0d9d64168 with master failed: RuntimeError: CI logs cluster query failed: SELECT file, symbol, sumIf(size, side = 'pr') AS pr_size, sumIf(size, side = 'base') AS base_size, toInt64(pr_size) - toInt64(base_size) AS delta FROM ( SELECT 'pr' AS side, file, replaceRegexpAll(replaceRegexpAll(symbol, ' ?\\[clone \\.llvm\\.[0-9]+\\]', ''), '\\.llvm\\.[0-9]+', '') AS symbol, size FROM binary_symbols WHERE date >= today() - 7 AND (repo = 'ClickHouse/ClickHouse' OR repo = '') AND pull_request_number = 116706 AND commit_sha = '12d9...(+747 chars elided)...a' AND check_name = 'arm_release' AND check_start_time = '2026-09-20 18:23:52' AND instance_id = 'i-0c799da5be0c96901' AND file IN ('./ci/tmp/build/programs/clickhouse', './ci/tmp/build/programs/clickhouse-keeper') AND size >= 1024 ) GROUP BY file, symbol HAVING abs(delta) >= 65536 ORDER BY abs(delta) DESC LIMIT 20.

See the job log for details.

@Michicosun
Michicosun enabled auto-merge August 28, 2026 08:36
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Sep 4, 2026
plan->addStep(std::make_unique<FilterStep>(plan->getCurrentHeader(), filter->actions.clone(), filter->column_name, filter->do_remove_column));

if (const auto & filter = reading_context.prewhere_filter)
plan->addStep(std::make_unique<FilterStep>(plan->getCurrentHeader(), filter->actions.clone(), filter->column_name, filter->do_remove_column));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This external FilterStep now runs before StampPartitionCursorsStep, but it still honors the original remove_prewhere_column flag. A legal query like SELECT count() FROM mt STREAM BOUNDED PREWHERE _block_number therefore erases _block_number here, and the later cursor stamper/aligner immediately fails on getPositionByName(_block_number) instead of executing the stream.

Can we force remove_column = false for _block_number / _block_offset once filters move ahead of cursor stamping, or reject those predicates explicitly?

@Michicosun
Michicosun added this pull request to the merge queue Sep 21, 2026
Merged via the queue into master with commit d148412 Sep 21, 2026
346 of 350 checks passed
@Michicosun
Michicosun deleted the optimize_stream_aligner branch September 21, 2026 04:27
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 21, 2026
thevar1able pushed a commit to groeneai/ClickHouse that referenced this pull request Oct 27, 2026
leshikus pushed a commit to leshikus/ClickHouse that referenced this pull request Oct 27, 2026
@clickhouse-gh clickhouse-gh Bot added comp-streaming-queries Streaming queries: queries that keep reading a table as data arrives - cursors, subscriptions to ... and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-streaming-queries Streaming queries: queries that keep reading a table as data arrives - cursors, subscriptions to ... pr-not-for-changelog This PR should not be mentioned in the changelog 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.

3 participants