Repository navigation
Read object storage tables (S3, Iceberg, DeltaLake, ...) with parallel replicas - #127138
alexey-milovidov merged 17 commits into
Conversation
A table created with `ENGINE = S3/AzureBlobStorage/HDFS/Iceberg*/DeltaLake*/Hudi/Paimon*` was always read by the initiator alone, while the table functions and `DataLakeCatalog` tables of the same engines are read over the replicas of `cluster_for_parallel_replicas` when `parallel_replicas_for_cluster_engines` is enabled. A persistent table's storage is created once, at `CREATE`/`ATTACH`, so it cannot make that decision itself. The analyzer now makes it per query: when the `FROM` of a query is such a table alone (possibly with `ARRAY JOIN`), the table node reads through a transient `StorageObjectStorageCluster` that shares the table's configuration and metadata, so the existing `IStorageCluster` path handles the read (processing stage, row policies, task iterator pruning). The replicas receive `SELECT ... FROM db.t` and read their own `db.t` with the initiator's read tasks; a replica recognizes that from the query being the secondary query of a cluster read whose whole `FROM` is that table, not from settings, because the initiator may change the parallel replicas settings after the analysis. A replica without the table, or one older than the new cluster function protocol version 13 (which would read all of the files), is not used. A query is read locally, as before, for a row policy, a `SQL SECURITY` view body, a materialized view source, an explicit `PREWHERE`, a correlated subquery, a `JOIN`, a partition strategy other than hive, a Delta Lake CDF read, and tables of the `system` database. The new setting `parallel_replicas_for_object_storage_tables` (default `true`, `false` for `compatibility` 26.10 and older) turns the feature off. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nitial-query counters The integration test reads a persistent `S3` table with parallel replicas over a cluster of two current servers and one at `CLICKHOUSE_CI_MIN_TESTED_VERSION`: the older server is not used (it would read every file), and neither is a server without the table. With parallel replicas enabled server-side (the `ParallelReplicas` stateless job) six tests now read their object storage table on the replicas, so the `ProfileEvents` and `EXPLAIN` of the initial query they assert on no longer describe the file reads; their results are unchanged. Tag them `no-parallel-replicas`. Write the protocol version 13 entry of the native protocol specification as its own sentence. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ITY NONE view For a lazily loaded table the analyzer builds the cluster storage from the loaded table and a snapshot taken after the metadata refresh, but recorded the lazy stand-in and the pre-refresh snapshot for the rollback of a correlated query, so planning could see other metadata than the one the columns were resolved against. Record the storage and snapshot the replacement was built from instead. Skip the refresh for a lazily loaded table that is not an object storage table, which saves a discarded storage snapshot per query (e.g. a MergeTree table in a `lazy_load_tables` database with parallel replicas enabled). The test now also checks that the body of a `SQL SECURITY NONE` view reads the table locally. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`parallel_replicas_prefer_local_replica = 0` with `max_parallel_replicas = 1` passes `canReplaceClusterEngineWithClusterVariant`, but `ReadFromCluster` caps the replicas only when `max_parallel_replicas > 1`, so the cluster read went to every replica of `cluster_for_parallel_replicas` instead of reading locally. Return no cluster storage for such a query, and cover it in 05441. Also reword the identity-guard comment: without an interserver secret the replicas authenticate as the cluster's configured user, not as the initial one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
On a replica, `isReadOnReplicaForInitiator` took any `QueryNode` that is not a subquery or a CTE for the root of the query the initiator sent. `UNION` arms are built with `is_subquery = false` and every nested interpreter has a root of its own, so an `IN` subquery with `UNION ALL` arms over the table pulled files from the initiator's task stream: a cluster read returned a wrong count, and under MergeTree parallel replicas the follower's `ReadTaskRequest` failed with `BAD_ARGUMENTS`. `executeQuery` now marks the query it receives from an initiator with `SelectQueryOptions::is_secondary_query_root`, which `subquery()` clears and the planner copies into `SelectQueryInfo`; the follower rule tests that flag. 05441 covers both shapes. The MergeTree table has no primary key, so the `IN` set does not prune it and the followers read it too. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A replica forwards the whole query of the swapped table, including its IN and scalar subqueries, and the initiator answers every read-task request on that connection from this table's iterator. A cluster table function, a DataLakeCatalog table, or a view or table over other tables in the forwarded query then takes this table's files on the replica, so `t IN (SELECT a FROM s3Cluster(...))` returned a wrong count. The table functions avoid this shape by counting table functions before resolution, which never sees a named table. The post-resolution rollback now also restores the swapped tables when the resolved tree has another table or table function whose storage is a cluster read, a view, reads other tables, or is not loaded yet. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The r4 rollback enumerated the storage kinds that may send read-task requests on a replica (cluster reads, views, tables over other tables), but any storage that runs a query of its own during read escapes such a list: an `Executable` table whose input query is `SELECT a FROM s3Cluster(...)` took the swapped table's files on the replicas, so `t IN (SELECT a FROM e WHERE a < 50)` returned 0 instead of 50. The rule is now closed: a query that also reads a table function or any table that is not one of the swapped ones reads the swapped tables itself. A table inside a subquery is never swapped (the query is distributed once its root table is), so a subquery that reads a table now keeps the read local as well. A scalar subquery is executed while it is resolved, before the rollback at the end of `resolve`, so `SELECT (SELECT count() FROM t WHERE a IN vc)` with `vc` a view over `s3Cluster` still forwarded the view. The rollback now also runs for the subquery right before `evaluateScalarSubqueryIfNeeded` executes it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop five self-describing comments on new declarations and shorten the isClusterVariantDisabledForQuery doc to one line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 11 rounds) plus my own review (6 notes).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261021-080500 (rows 14-17: cron:clickhouse-maint-slot-13:20261021-143400; rows 15, 18-20: cron:clickhouse-maint-slot-14:20261023-002000; rows 21-22: cron:clickhouse-maint-slot-12:20261023-040500; row 23: cron:clickhouse-maint-slot-12:20261023-060500) |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261021-071900 |
|
Workflow [PR], commit [808baeb] Summary: ✅
AI ReviewSummaryPR #127138 enables eligible persistent object storage tables to use parallel replicas, with an opt-out setting and checks for replica availability and protocol support. Reviewed head Final VerdictStatus: ✅ Approve |
|
cc @devcrafter @novikd @SmitaRKulkarni, could you review this and add the |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes388 object files changed (+275.57 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units1055 translation units recompiled, 7734 s compile time in total, 1055 of them have a recent master baseline. |
The test started about 80 `clickhouse client` processes, three per check (EXPLAIN, the query with parallel replicas, the query without them). Under ASan a client start costs ~0.4 s, so starting clients was most of the run time, and the ASan flaky check failed it with "Test runs too long (> 180s)" (p50 161 s, max 213 s). Each check now sends its three queries, byte for byte the same, to one client and splits the output on a separator line. That leaves 36 client starts. With an ASan client against a local server the run time drops from ~44 s to ~21 s (24 runs, 12 in parallel); the old and the new check print the same output for differing, multi-line, empty and ReadFromCluster results. Also retitle two sections whose headings still described a replica read, while their EXPLAIN lines show the table is read locally. Co-Authored-By: Claude Opus 5.5 <[email protected]>
It runs a dozen parallel replicas queries over S3 and Iceberg tables and takes more than 180 seconds in the flaky check under ASan with S3 storage. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…bject-storage-tables # Conflicts: # src/Storages/ObjectStorage/StorageObjectStorage.h # src/Storages/ObjectStorage/registerStorageObjectStorage.cpp
With parallel replicas a read of a persistent Iceberg table is now distributed, so the counters of the initial query change while the results do not: - `05339_iceberg_top_k_query_condition_cache`: the replicas use the query condition cache, so the initial query records no lookups (the same as the three other query condition cache tests already tagged). - `05407_iceberg_metadata_s3_requests`: the initiator resolves the table again when `IStorageCluster::read` analyzes the query it sends to the replicas, so the table read makes 2 LIST requests instead of 1. Both pass with `parallel_replicas_for_object_storage_tables = 0` and in the other configurations. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Two cases where reading a persistent object storage table with parallel replicas changed something it should not: - The cluster read marks the query distributed as soon as its table is replaced, and a subquery inherits that state. So a plain `s3` in a subquery with its own `SETTINGS` (whose context the table function uses) was no longer converted to `s3Cluster`, while the query was then rolled back to a local read because it has another source. The same holds for a `DataLakeCatalog` table in a subquery or on the right side of `IN`, which is resolved with that context too. A query with a subquery or with a table on the right side of `IN` always reads its table itself, so do not replace the table in such a query at all, instead of replacing it and rolling it back. Measured with `SELECT count() FROM t WHERE a IN (SELECT a FROM s3(...) WHERE a < 50 SETTINGS max_threads = 2)`: 0 remote queries before, 3 now, the same as with `parallel_replicas_for_object_storage_tables = 0`. - `parallel_replicas_for_object_storage_tables` is stripped from the text of a distributed DDL query with the other initiator-only settings, so `CREATE VIEW ... ON CLUSTER` stored the view body without it, and the view was then read with parallel replicas despite the opt-out. Keep it in the DDL text: in a view body it decides how the view reads its table. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous commit kept the table of every query with a subquery or with an identifier on the right side of `IN`, which also kept local the queries that read nothing else, e.g. `WHERE a > (SELECT 1)`, `exists(SELECT 1)` or `WITH (1, 2) AS s ... WHERE a IN s`, which read the table with parallel replicas before. Only a subquery that reads a source is affected by the distributed state it inherits, and a query with such a subquery is read locally in any case, so refuse the replacement only for it. The seven shapes measured before and after this commit read the same way, and the `s3` in a subquery with its own `SETTINGS` is still read with parallel replicas. 05444 now also reads the views, and checks that the one created ON CLUSTER with the opt-out reads its table itself while the one without it does not. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lacing the table The table is replaced while the FROM is resolved, and the replacement marks the query context distributed. A source the query resolves afterwards inherits that state, so its own cluster read is disabled (`canReplaceClusterEngineWithClusterVariant`), and the final rollback restores only the replaced table. The check before the replacement saw subqueries in the query tree only, but `resolveQuery` has already moved the CTEs and the `WITH` aliases out of it, and `x IN name` is an identifier until it is resolved. So each of these lost an existing parallel read as soon as `parallel_replicas_for_object_storage_tables` was enabled (measured, 3 replicas, secondary queries with the setting on vs off; results equal): - `(a, b) IN catalog.ids`, a DataLakeCatalog (Iceberg REST) table: 0 vs 3 - `WITH c AS (SELECT a FROM s3(...) SETTINGS max_threads = 2) ... a IN c`: 0 vs 3 - a CTE over the catalog table, `a IN c`: 0 vs 3 - `WITH (SELECT max(a) FROM s3(...) SETTINGS ...) AS m ... a <= m`: the s3 read became local and `t` was read with parallel replicas instead Follow the identifiers of the query to the CTEs and expression aliases of the scope chain and check their nodes the same way, and treat an `IN` over a name that is neither an alias, a CTE nor a column of the table as a table. All the shapes above now read like with the setting disabled. Shapes without another source keep the parallel read: `WITH (1, 2) AS s ... IN s`, `WITH 5 AS x`, `b IN b`, `(SELECT 1)`, `exists(SELECT 1)`, `IN (1, 2)`. Reported by the automated review on ClickHouse#127138. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`WITH ids AS s SELECT ... WHERE x IN s` resolves the body of the alias as a table expression, so with a DataLakeCatalog database as the current database the catalog table lost its parallel read the same way as `x IN ids` did before the previous commit (measured, 3 replicas: 0 secondary queries with `parallel_replicas_for_object_storage_tables = 1` vs 3 with 0; the same for a chain `ids AS s, s AS s2`). An alias to a column (`WITH b AS s ... b IN s`) still keeps the parallel read of the table. Found by the second-model review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reading the table with parallel replicas marked the query context distributed as soon as the table was replaced, and everything the query resolves afterwards inherits that state, so another source of the query lost its own cluster read (`canReplaceClusterEngineWithClusterVariant`) while the query was then rolled back to a local read anyway. The check before the replacement tried to find such sources in advance, but every expansion the analyzer does later hides one: after the subqueries, CTEs, aliases and `IN` table names of the previous commits, the automated review found a `WITH` lambda and a SQL UDF (measured on 7131a0b, 3 replicas, `Shards` of the initial query with `parallel_replicas_for_object_storage_tables` 1 vs 0, results equal): - `WITH x -> x IN (SELECT a FROM s3(...) SETTINGS max_threads = 2) AS f SELECT count() FROM t WHERE f(a)`: 0 vs 3 - the same lambda passed to `arrayExists(f, [a])`: 0 vs 3 - a SQL UDF with that body, and a UDF calling it: 0 vs 3 - a lambda over a DataLakeCatalog (Iceberg REST) table: 0 vs 3 A replaced table is now read like the table itself during analysis: it does not mark the query distributed, so the other sources resolve exactly as without the setting, and the final rollback keeps deciding on the fully resolved tree. The check before the replacement is removed. A subquery of a query with a replaced table still reads its tables itself, as before. All the shapes above now read like with the setting disabled, and so do the subquery/CTE/alias/`IN` shapes of the previous commits (also over the catalog table). As for MergeTree parallel replicas, server constants of such a query are now computed on the initiator, so `SELECT hostName(), count() FROM t GROUP BY 1` returns the same row as a local read instead of one row per replica. Marking the query distributed only once the replacement is kept was tried first and does not work: the constants are already folded then, and the initiator keeps the folded `hostName()` as a GROUP BY key while the shipped query drops it (`NUMBER_OF_COLUMNS_DOESNT_MATCH`). A scalar subquery over another source no longer prevents the parallel read: it is executed during analysis, so `WITH (SELECT max(a) FROM s3(...)) AS m SELECT count() FROM t WHERE a <= m` now reads both with parallel replicas (05441 line 82: 0 -> 1). 05441 adds the lambda and the SQL function shapes, which fail without this commit, and a server constant in GROUP BY. Reported by the automated review on ClickHouse#127138. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| if (table_node->isTemporaryTable() || !scope.context->canReplaceClusterEngineWithClusterVariant()) | ||
| return; | ||
|
|
||
| /// The cluster read does not support `PREWHERE`. |
There was a problem hiding this comment.
Because the cluster read rejects it: IStorageCluster does not override supportsPrewhere() (false in IStorage), so the planner throws ILLEGAL_PREWHERE on the swapped storage. Without this check, SELECT ... FROM t PREWHERE ... that works today would fail once parallel replicas are enabled.
Master already behaves this way for the table functions (measured on 44cf9c0, Parquet source):
SELECT count() FROM s3('.../data.parquet', 'NOSIGN', 'Parquet', 'a UInt64') PREWHERE a > 10
-- OK: ReadFromObjectStorage, Prewhere filter a > 10
-- same query + SETTINGS enable_parallel_replicas = 1, cluster_for_parallel_replicas = 'pr', parallel_replicas_for_cluster_engines = 1
-- Code: 182. Storage S3 (table _table_function.s3) does not support PREWHERE. (ILLEGAL_PREWHERE)
s3Cluster(...) PREWHERE fails the same way. The replicas read through StorageObjectStorage, which supports PREWHERE, so the cluster read could forward it instead. That would fix both; I can do it in a separate PR and then remove this check.
alexey-milovidov
left a comment
There was a problem hiding this comment.
While the replacement is rather mechanical, it is inline with automatic cluster table functions, and this is a good change overall.
Since ClickHouse#127138 the `ParallelReplicas` job reads S3 engine tables with parallel replicas. The S3 reads of the test then run on the replicas, which consult the query condition cache and skip the row blocks as expected (run 2: one hit, 82497 rows read), but the initial query's `ProfileEvents` that the test asserts on stay 0. Same tag and reason as the tests ClickHouse#127138 itself tagged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d with parallel replicas After #127138, a table with an object storage engine is read over the replicas of `cluster_for_parallel_replicas` through a transient `StorageObjectStorageCluster`. That read does not go through `ReadFromObjectStorageStep`, so the objects it consumed were not recorded into `QueryConsumedObjectSets` and nothing marked a pruned, unpinned or zero-object read. The finalization check fell back to a fresh listing and cached results that must not be cached, which broke `04545`, `04822`, `04846` and `05411` in the `ParallelReplicas` stateless tests. Read the table itself when the query has a consumed-object capture, so the consistent cache keeps working, and fail closed in `getTaskIteratorExtension` of a parallel replicas table read if a capture appears after the analysis (a subquery-level cache). https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=108721&sha=e10b04b3fc1dd20759dca60d95d7e439b2628c9a&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…llel replicas Since ClickHouse#127138 a persistent `S3` table is read over the replicas when parallel replicas are enabled, so in the `ParallelReplicas` job the `ProfileEvents` of the initial query no longer count the query condition cache lookups and the skipped row groups of the `t_05339_s3` and `t_05339_s3_glob` reads, and four lines of the reference read 0. Pin `enable_parallel_replicas = 0` for every server read of the test, as the `IcebergLocal` reads already did, instead of tagging the whole test `no-parallel-replicas`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #127052
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A table with the
S3,AzureBlobStorage,HDFS,Iceberg*,DeltaLake,DeltaLakeS3,DeltaLakeAzure,Hudi,Paimon,PaimonS3,PaimonAzureorPaimonHDFSengine is now read over the replicas ofcluster_for_parallel_replicaswhen parallel replicas are enabled, as the table functions of these engines already are withparallel_replicas_for_cluster_engines. A replica takes part only if it has the table. The new settingparallel_replicas_for_object_storage_tablesturns it off. Closes #127052.Description
With parallel replicas enabled,
icebergS3(...)is read over the replicas, but a table created withENGINE = IcebergS3(...)was always read by the initiator alone: the storage of a persistent table is created once atCREATE/ATTACH, so it cannot decide per query.Change: when the
FROMof a query is such a table alone (possibly withARRAY JOIN), the analyzer reads it through a transientStorageObjectStorageClusterthat shares the table's configuration and metadata, so the existing cluster read handles it. The swap is in the analyzer, not inread: the planner picks the processing stage and collects the_path/_filepruning predicate from the storage type beforereadruns. The replicas receiveSELECT ... FROM db.tand read their owndb.twith the initiator's read tasks. The query is analyzed as a local read, sohostName()and other server constants are computed on the initiator. A replica without the table, or one older than the new cluster function protocol version13(it would read every file), is not used. The read stays local for a row policy, aSQL SECURITY DEFINER/NONEview body, an explicitPREWHERE, a correlated subquery, aJOIN, a materialized view source,max_parallel_replicas = 1, thesystemdatabase, and a query that also reads a table function or a table not read this way (on a replica it could take this table's read tasks).No speed-up is claimed (#126036, #127051, #127060).
ProfileEvents['ParallelReplicasUsedCount']stays 0 for these reads, as foricebergS3(): it counts only MergeTree reads.Validation: the new stateless tests and the integration test (skipped replicas) fail without the change and pass with it. Eight existing tests assert on
ProfileEventsorEXPLAINof the initial query and are taggedno-parallel-replicas.Protocol version
13is new here; please tell me if the private repository already uses it.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/127138]
Version info
26.11.1.713-master(included in26.11and later)