Skip to content

Streaming queries with cursors for Merge tables - #131815

Open
alexey-milovidov wants to merge 7 commits into
masterfrom
stream-merge-tables
Open

alexey-milovidov wants to merge 7 commits into
masterfrom
stream-merge-tables

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Support streaming queries (SELECT ... FROM merge_table STREAM [BOUNDED] [CURSOR {...}]) for Merge tables and the merge table function. Every table is read with STREAM, so all of them must support it (ILLEGAL_STREAM otherwise). The cursor is {database: {table: <cursor of the table>}}, and a table without an entry is read from the beginning. Each child gets a copy of the stream settings with its part of the cursor.

A streaming read is never narrowed (narrowPipe concatenates the streams of the children, while a streaming child never finishes), read in order, or expanded for parallel replicas. The set of tables is determined at the start of the query. WATERMARK and nested Merge tables are not supported.

Test: 05770_streaming_queries_merge_cursors (bounded with and without a cursor, the merge table function, unbounded, a child without STREAM support, incremental refresh rejected).

The branch includes the commits of #131814 and #132145 (the infrastructure for an incremental refresh from tables other than MergeTree).

Incremental refresh: the bounded read of every table reports its cursor under the database and the table through a child holder, so a Merge table can be the source of an incremental refresh of a refreshable materialized view (APPEND INCREMENTAL). Every table is checked on every refresh, with the same checks as a direct source (validateIncrementalRefreshSource: it must report the cursor, a MergeTree table must be a plain one with persisted block columns), as the matching tables may change. The persisted cursor of a table is also keyed by its UUID, so a table that starts matching later, or is dropped and created again under the same name, is read from the beginning by the next refresh. Tested by 05770_streaming_queries_merge_cursors (three rounds) and 05771_streaming_queries_merge_cursors_edge_cases (dotted names, a nested Merge, parallel replicas, unsafe children, a recreated child).

This is a part of #131769, split by storage as requested in the review. Compared to it, the cursor format is handled by the table itself, no new virtual columns represent the cursor, and the read reports its final cursor through Context::getStreamingCursor only for an incremental refresh, which is what it exists for.

Related: #42990
Related: #131769
Related: #131814
Related: #132145

Changelog category (leave one):

  • Experimental Feature

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

Streaming queries (SELECT ... FROM t STREAM) with cursors are supported for Merge tables.

🤖 Generated with Claude Code


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

A streaming read of a `Merge` table reads every table with `STREAM`; the cursor
is `{database: {table: <cursor of the table>}}`, and a table without an entry is
read from the beginning. Each child gets a copy of the stream settings with its
part of the cursor. A streaming read is never narrowed (it concatenates the
streams of the children, and a streaming child never finishes), read in order or
expanded for parallel replicas. `WATERMARK` is not supported.

Related: #42990
Related: #131769

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

clickhouse-gh Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [46244b3]

Summary: ✅


AI Review

Summary

Adds experimental STREAM reads and incremental-refresh cursors for Merge tables and the merge table function. One new Major remains, and the author-deferred cursor-name ambiguity still causes silent row omission. Both issues were reproduced with the PR head's CI binary.

Cursor consumers traced: RefreshTask and CoordinationZnode persist/reload the flattened map; IcebergStorageSink and DataLakeRefreshCursorStore use the same representation; ASTStreamSettings and StreamSettings cloning also flatten keys; ReadState receives the extracted child subtree. The additional UUID component separates persisted incremental-refresh branches, while ordinary STREAM cursors retain the ambiguity below. Keeper and Iceberg persistence paths were reviewed statically.

Findings

❌ Blockers

  • src/Storages/StorageMerge.cpp:1912–1916: Distinct sources can share a cursor. [dismissed by author -- https://github.com/Streaming queries with cursors for Merge tables #131815#discussion_r4579164850] The author explicitly deferred preserving literal key boundaries. This remains a correctness defect: database review_a, table b.c, and database review_a.b, table c, both become the path review_a.b.c. In the reproduction, a cursor consuming the first source's row also suppresses the second source's unread row 100, although that source has no cursor entry and must start from the beginning. Preserve key boundaries or reject ambiguous source combinations before reading. Kept in this summary only; no new inline comment or thread reopening.

⚠️ Majors

  • src/Storages/StorageMaterializedView.cpp:219: An active reader can make a valid incremental source fail validation. StorageTableProxy forwards supportsStreaming but inherits reportsStreamingCursor = false. After reattaching a database with lazy_load_tables=1, an active STREAM reader keeps the loaded proxy in the catalog. Direct incremental-view creation then fails with does not report the cursor of a streaming read; refresh through a Merge source fails identically. Both succeed after releasing the reader. Forward the capability through the lazy stand-in or resolve it before validation, and add a regression with the source pinned during refresh.
Final Verdict

Status: ❌ Block

Prevent cursor collisions from silently skipping unread sources. Correct lazy-source capability validation and cover the active-reader case.

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label Nov 15, 2026
Comment thread src/Storages/StorageMerge.cpp Resolved
Comment thread src/Storages/StorageMerge.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-storage-merge Merge table engine (logical union over tables) and related mechanics. label Nov 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 46244b3f5bdc8bc15c0bbded0a08ce2a2e999bd0 was not compared: the CI logs cluster did not answer (the endpoint never became ready: the readiness probe timed out: HTTPSConnectionPool(host='t6h0zvqlg…).

See the job log for details.

@alexey-milovidov
alexey-milovidov marked this pull request as ready for review November 16, 2026 16:41
Comment thread src/Storages/StorageMerge.cpp Resolved
alexey-milovidov and others added 4 commits November 16, 2026 20:32
…l refresh

Instead of accepting only `MergeTree` sources, an incremental refresh accepts a
source whose bounded streaming read reports the cursor it advanced to
(`IStorage::reportsStreamingCursor`, true for `MergeTree` and
`ReplicatedMergeTree`); other engines enable it together with the reporting.

`StreamingCursor` gets `merge` and child holders, which place the cursor of a
part of a composite read (a table of `Merge`, a shard of `Distributed`) under its
key path. The trees are merged in the persisted form (a key with a dot is split
into a path), and the holder starts from the cursor of the previous refresh, so a
part that is not read this time keeps its position.

Related: #42990
Related: #131814 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A bounded read of every table of a `Merge` table reports its cursor under the
database and the table through a child holder, so a `Merge` table can be the
source of an incremental refresh. A table that does not report the cursor is
refused on every refresh, as the matching tables may change. The cursor of a
table with dots in the database or the table name is found by the parts of the
name, as the cursor tree splits the keys by dots.

Related: #131814 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Storages/StorageMerge.cpp Resolved
Comment thread src/Storages/StorageMerge.cpp Resolved
alexey-milovidov and others added 2 commits November 19, 2026 17:34
…bles

- Never expand a streaming `Merge` read for parallel replicas: the check is
  moved from `mayBeExpandedForParallelReplicas` to `computeExpandableReads`,
  which is shared by all the expansion paths.
- Reject a nested `Merge` table under a streaming `Merge` read: its `_database`
  and `_table` identify the leaf table, so the cursor could not be
  reconstructed from the result.
- Validate every table of a `Merge` source of an incremental refresh with the
  same checks as a direct source (`validateIncrementalRefreshSource`), on every
  refresh: a merging engine or a table without persisted block columns would
  silently lose or duplicate rows.
- Bind the cursor persisted by an incremental refresh to the UUID of the table,
  so that a table dropped and created again under the same name is read from
  the beginning instead of resuming from the cursor of its predecessor.
- Add a test, including table and database names with dots.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Conflict in `src/Storages/StorageMerge.cpp`: both sides added includes at the same place, keep both.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
/// The refresh resumes from the cursor that the read reports through `Context::getStreamingCursor`. A source
/// that supports STREAM but does not report the cursor would be read from the beginning on every refresh,
/// appending the same rows again.
if (!source->reportsStreamingCursor())

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.

[Major] Preserve cursor reporting through lazy table proxies

StorageTableProxy forwards supportsStreaming, but inherits the default false for reportsStreamingCursor. A loaded proxy remains in the catalog while another query holds it (replaceLoadedLazyTableUnlocked cannot replace an in-use proxy), so this check rejects a valid plain MergeTree source.

Reproduced with this head's CI binary: create an Atomic database with lazy_load_tables=1 and a child with both persisted block columns enabled; detach/attach the database, then keep SELECT a FROM child STREAM running. Direct APPEND INCREMENTAL view creation fails with does not report the cursor of a streaming read. Creating the view over a Merge source succeeds, but every refresh fails with the same exception while that reader runs. After cancelling the reader, the identical creation/refresh succeeds.

Forward this capability through the lazy stand-in, or resolve it before checking the capability. Add a regression that keeps the lazy source pinned while refreshing through Merge.

@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-storage-merge Merge table engine (logical union over tables) and related mechanics. labels Nov 22, 2026

This branch has not been deployed

No deployments
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-experimental Experimental Feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant