Repository navigation
Streaming queries with cursors for Merge tables - #131815
alexey-milovidov wants to merge 7 commits into
Conversation
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]>
|
Workflow [PR], commit [46244b3] Summary: ✅
AI ReviewSummaryAdds experimental Cursor consumers traced: Findings❌ Blockers
Final VerdictStatus: ❌ Block Prevent cursor collisions from silently skipping unread sources. Correct lazy-source capability validation and cover the active-reader case. |
Build profile diff (arm_release)Commit See the job log for details. |
…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]>
…REAM from Merge Co-Authored-By: Claude Opus 5.5 <[email protected]>
…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()) |
There was a problem hiding this comment.
[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.
Support streaming queries (
SELECT ... FROM merge_table STREAM [BOUNDED] [CURSOR {...}]) forMergetables and themergetable function. Every table is read withSTREAM, so all of them must support it (ILLEGAL_STREAMotherwise). 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 (
narrowPipeconcatenates 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.WATERMARKand nestedMergetables are not supported.Test:
05770_streaming_queries_merge_cursors(bounded with and without a cursor, themergetable function, unbounded, a child withoutSTREAMsupport, 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
Mergetable 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, aMergeTreetable 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 by05770_streaming_queries_merge_cursors(three rounds) and05771_streaming_queries_merge_cursors_edge_cases(dotted names, a nestedMerge, 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::getStreamingCursoronly for an incremental refresh, which is what it exists for.Related: #42990
Related: #131769
Related: #131814
Related: #132145
Changelog category (leave one):
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 forMergetables.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/131815]