Skip to content

Add STREAM BOUNDED modifier - #110653

Merged
SmitaRKulkarni merged 23 commits into
masterfrom
Add_bounded_stream_modifier
Aug 13, 2026
Merged

SmitaRKulkarni merged 23 commits into
masterfrom
Add_bounded_stream_modifier

Conversation

@SmitaRKulkarni

@SmitaRKulkarni SmitaRKulkarni commented Jul 16, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Add STREAM BOUNDED modifier, which read only the first snapshot of a streaming query, then finish instead of subscribing for updates.

cc @alesapin @Michicosun

Version info

  • Merged into: 26.8.1.1314 (included in 26.8 and later)

@clickhouse-gh

clickhouse-gh Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [073090f]

Summary: ❌

job_name test_name status info comment
Stress test (arm_debug) FAIL
Logical error: Sizes of nested column and null map of Nullable column are not equal after deserialization (null map size = A, nested column size = B) (STID: 6726-6673) FAIL cidb IGNORED

AI Review

Summary

This PR adds STREAM BOUNDED syntax and wires bounded reads to stop after the first completed snapshot instead of staying subscribed for updates. The parser / analyzer plumbing looks consistent, but one blocker remains in the execution path: bounded reads are still not latched to a specific enrichment round, so later data can leak into the supposedly first snapshot.

Findings

❌ Blockers

  • [src/Storages/MergeTree/Streaming/MergeTreeCommitOrderSequentialSource.cpp:404-455] STREAM BOUNDED still does not bind its result to the enrichment round that woke the processor. WakeupFd and updatesCount() are unversioned: if round N wakes at safe block 3 and round N+1 advances the same partition to 5 before work runs, work drains the stale wake at line 450, snapshots safe_block_numbers at line 455, builds [0,5], and line 404 then finishes after that single snapshot. Rows that were not part of the first completed enrichment can therefore leak into the bounded result, so the advertised "first snapshot" contract is still violated.
    Suggested fix: publish a round/epoch together with safe_block_numbers (or atomically snapshot {updates_count, safe_block_numbers}) and have bounded reads build the first snapshot from one specific round only.
Tests
  • ⚠️ Add a focused regression that lets two enrichment rounds complete before the first wake is drained and asserts that STREAM BOUNDED stops at the first round's safe_block_numbers, not the later one.
Final Verdict

Changes requested. No new inline review was posted because the remaining blocker is already tracked on an open review thread.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.00% 79.00% +0.00%

Changed lines: Changed C/C++ lines covered: 218/226 (96.46%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 16, 2026
@Michicosun Michicosun self-assigned this Jul 16, 2026
@EmeraldShift

EmeraldShift commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

I'm not sure if I understand, what is the purpose of a streaming query that doesn't subscribe? Is this expected to be passed some previous cursor argument in a polling loop?

@SmitaRKulkarni

Copy link
Copy Markdown
Member Author

@EmeraldShift
STREAM BOUNDED isn't meant for continuous consumption on its own; it's the building block for incremental refreshable materialized views.

Copilot AI left a comment

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.

Pull request overview

Adds a new STREAM BOUNDED modifier to ClickHouse streaming reads so a streaming query can read exactly the first fully-determined snapshot and then terminate (instead of staying subscribed for future updates). This integrates the new syntax through parsing/formatting and the analyzer, and adds bounded-specific subscription/enrichment semantics in the MergeTree streaming pipeline.

Changes:

  • Extend SQL syntax/AST/analyzer plumbing to support and preserve STREAM BOUNDED (including format round-trips and query tree hash/equality).
  • Implement bounded-stream behavior in MergeTree streaming sources by waiting for an atomically “determined” snapshot, reading it once, then finishing.
  • Add coverage via a new stateless query test and additional gtests for bounded subscription/enrichment behavior.

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/queries/0_stateless/04545_streaming_bounded.sql Adds a functional query test validating bounded streams terminate on empty and read the initial snapshot when populated.
tests/queries/0_stateless/04545_streaming_bounded.reference Expected output for the new bounded streaming stateless test.
src/Storages/MergeTree/Streaming/tests/gtest_bounds_subscription.cpp Expands unit tests to cover bounded subscription waking and “determined snapshot” semantics plus enrichment pending behavior.
src/Storages/MergeTree/Streaming/SubscriptionEnrichment.h Changes enrichment API to return a richer result (enriched + pending).
src/Storages/MergeTree/Streaming/SubscriptionEnrichment.cpp Implements the richer enrichment result including “pending” detection when promotion is blocked by gaps.
src/Storages/MergeTree/Streaming/MergeTreeCommitOrderSequentialSource.h Tracks whether the first bounded snapshot has been processed to stop after the initial read.
src/Storages/MergeTree/Streaming/MergeTreeCommitOrderSequentialSource.cpp Implements bounded behavior: wait for determined snapshot, read once, then finish; ensure atomic snapshot reads for bounded.
src/Storages/MergeTree/Streaming/MergeTreeBoundsSubscription.h Adds bounded mode, round lifecycle hooks, and atomic snapshotIfDetermined.
src/Storages/MergeTree/Streaming/MergeTreeBoundsSubscription.cpp Implements bounded wakeup rules and “determined snapshot” tracking.
src/Storages/MergeTree/MergeTreeData.cpp Wraps per-subscription enrichment in begin/end round calls and consumes the new enrichment result.
src/Processors/QueryPlan/ReadFromMergeTree.cpp Propagates bounded from parsed stream settings into MergeTreeBoundsSubscription.
src/Parsers/tests/gtest_stream_settings.cpp Adds parser/formatter tests for STREAM BOUNDED (with and without CURSOR).
src/Parsers/ParserStreamSettings.cpp Parses optional BOUNDED before optional CURSOR.
src/Parsers/CommonParsers.h Registers BOUNDED as a keyword token.
src/Parsers/ASTTablesInSelectQuery.cpp Ensures formatting prints stream settings when bounded is set even without a cursor.
src/Parsers/ASTStreamSettings.h Adds bounded to stream settings AST structure.
src/Parsers/ASTStreamSettings.cpp Formats BOUNDED (and preserves spacing when combined with CURSOR).
src/Analyzer/Utils.cpp Propagates bounded when converting analyzer stream settings into AST.
src/Analyzer/TableExpressionModifiers.h Adds bounded to analyzer-level StreamSettings and includes it in equality.
src/Analyzer/TableExpressionModifiers.cpp Includes bounded in dumps, hashes, and error-message formatting.
src/Analyzer/QueryTreeBuilder.cpp Propagates bounded from AST into analyzer stream settings.

@clickhouse-gh

clickhouse-gh Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 80.60% 86.20% +5.60%
Functions 91.00% 92.00% +1.00%
Branches 72.50% 78.30% +5.80%

Changed lines: Changed C/C++ lines covered: 289/319 (90.60%) · Uncovered code

Full report · Diff report

Comment thread src/Parsers/ASTStreamSettings.h Resolved
Comment thread src/Parsers/ASTStreamSettings.cpp Resolved
Comment thread src/Parsers/ASTTablesInSelectQuery.cpp Resolved

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 21 out of 21 changed files in this pull request and generated no new comments.

Suppressed comments (1)

src/Core/Streaming/Settings.h:26

  • The comment for subscribe_for_updates describes the inverted behavior (it says "If true" for bounded behavior, but the code uses subscribe_for_updates == false to mean bounded). This is likely to confuse future readers and can lead to incorrect usage.
    /// If true, read only the first snapshot and then finish (do not subscribe for updates).

@Michicosun Michicosun left a comment

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.

Overall looks great!

Left a couple of non-critical comments.

if (subscription->isDisabled())
return;

const auto safe_block_numbers = subscription->snapshot();

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.

STREAM BOUNDED still is not latched to a specific enrichment round here. WakeupFd is unversioned, so a stale wake from round N can be drained after round N+1 has already advanced safe_block_numbers; this line then snapshots the newer map and the bounded query emits data that was not part of the first completed enrichment, or a mixed-round subset if N+1 is still in flight.

Concrete trace: round N notifies at cursor 3, round N+1 advances the same partition to 5 before the source runs, this line reads 5, and the new finish condition at lines 403-409 stops after emitting [0,5]. That breaks the STREAM BOUNDED contract even under the redesign to “first enrichment”.

This needs a round/epoch on the wake, or an atomic publication of {updatesCount, safe_block_numbers} so work can build the snapshot for the same round it was woken for. Please add a regression where two enrichments happen before the first wake is drained.

@clickhouse-gh

clickhouse-gh Bot commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

No arm_release build profile data for commit 073090f - the build was skipped, reused from cache, or predates profile upload.

@Michicosun Michicosun left a comment

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.

LGTM

@SmitaRKulkarni

Copy link
Copy Markdown
Member Author

Unrelated fails :
Stress test (arm_debug) - #114259

@SmitaRKulkarni
SmitaRKulkarni added this pull request to the merge queue Aug 13, 2026
Merged via the queue into master with commit f8ff202 Aug 13, 2026
178 of 180 checks passed
@SmitaRKulkarni
SmitaRKulkarni deleted the Add_bounded_stream_modifier branch August 13, 2026 09:15
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 13, 2026
pull Bot pushed a commit to admariner/ClickHouse that referenced this pull request Aug 20, 2026
`MergeTreeData::scheduleStreamingJob` read the visible parts *before* it iterated
the registered subscriptions. A subscription registered in that window was
credited with an enrichment round computed from a parts snapshot taken before it
existed.

For a subscribing stream that is harmless — the next round fixes the cursor. For
a bounded stream it is not: `handleBoundedReconfiguration` treats the first
completed round as authoritative, so an empty stale round makes the source
finish immediately and the query silently returns fewer rows (usually none) than
were committed before it started.

This is what `04545_streaming_queries_bounded` hit on master: the first query in
the test (`SELECT ... FROM t_streaming_bounded STREAM BOUNDED` on the empty
table) triggers the background streaming job, the job snapshots an empty part
set, and by the time it reaches `executeOnEachSubscription` the two `INSERT`s and
the second query have already run, so the second query's subscription gets that
empty round and returns `all 0 0 0` instead of `all 10 45 450`.

Collect the subscriptions first and read the parts only afterwards. Everything
in the collected list was registered before the collect, so the parts snapshot
is at least as fresh as every subscription served from it. A subscription
registered after the collect is simply picked up by the next round: its own
`triggerStreamingSubscriptionEnrichment` re-schedules the job even while the job
is running (`BackgroundSchedulePoolTaskInfo::execute` re-queues a task that was
scheduled during execution).

`executeOnEachSubscription` is replaced by `collectSubscriptions`, which returns
strong pointers under the shared lock, because the callback form invited exactly
this ordering mistake.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=8d5be603139a198bdfd2ea225d2e998554316178&name_0=MasterCI&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29

Caused by: ClickHouse#110653
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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.

5 participants