Repository navigation
Fix INSERT ... SELECT FROM url()/s3() with parallel replicas inserting the data once per replica - #120488
Conversation
…nction inserting the data once per replica With parallel replicas enabled, `parallel_replicas_for_cluster_engines` converts a plain `url` / `s3` / ... table function into a cluster storage on the initiator, so `INSERT INTO <replicated table> SELECT * FROM url(...)` takes the distributed `parallel_distributed_insert_select` path and forwards the query to every replica of the cluster. The forwarded query text still named the plain function, and a replica running it as a secondary query created a plain storage that expanded the globs and read every file on its own instead of taking its share of the read tasks from the initiator, so N replicas inserted the data N times. `IStorageCluster::read` already rewrites the source for a plain `SELECT` via `updateQueryToSendIfNeeded` (the function becomes its `*Cluster` variant with the cluster name argument). Apply the same rewrite to the forwarded query in `InterpreterInsertQuery::distributedWriteIntoReplicatedMergeTreeOrDataLakeFromClusterStorage`. Closes: #120485 Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Workflow [PR], commit [b98fbce] Summary: ✅
AI ReviewSummaryThis PR fixes the forwarded-query rewrite for Findings
Final VerdictRequest changes until the delegated LLVM Coverage ReportMeasured on commit b98fbce.
Changed lines: Changed C/C++ lines covered: 35/36 (97.22%) · Uncovered code |
There was a problem hiding this comment.
Note
Copilot was unable to run its full agentic suite in this review.
Pull request overview
Fixes parallel-replicas INSERT ... SELECT FROM url()/s3() duplicating reads/inserts across replicas by rewriting the forwarded query to use *Cluster table functions (matching the existing SELECT rewrite path), and adds a stateless regression test to validate correct distribution and reads.
Changes:
- Make
IStorageCluster::updateQueryToSendIfNeededpublicly callable soInterpreterInsertQuerycan reuse the existing rewrite logic. - Rewrite the forwarded
INSERT ... SELECTquery text to use the*Clustertable function and include explicit structure/format args where needed. - Add a stateless test that validates row counts, forwarded query shape, and per-replica read distribution for
url()ands3().
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| tests/queries/0_stateless/05218_insert_select_auto_cluster_function_parallel_replicas.sh | Adds regression test covering url()/s3() distributed insert behavior with parallel replicas. |
| tests/queries/0_stateless/05218_insert_select_auto_cluster_function_parallel_replicas.reference | Adds expected output for the new regression test. |
| src/Storages/IStorageCluster.h | Exposes updateQueryToSendIfNeeded publicly to reuse rewrite logic outside read(). |
| src/Interpreters/InterpreterInsertQuery.cpp | Applies the cluster-function rewrite to the forwarded distributed INSERT ... SELECT query. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…uto-cluster-parallel-replicas
…ent run The test used `query_id`s that only varied with the database name and then looked the secondary queries up in `system.query_log` with an `event_date >= yesterday()` window. A re-run against the same server within that window could pick up the secondary `INSERT` rows of an earlier run, inflating `replicas`, `cluster_function_queries` and `rows_read_by_replicas`. The `query_id`s now carry a per-run unique suffix and both log lookups are bounded by `event_time >= now() - INTERVAL 10 MINUTE`. The forwarded-query assertion also matched a loose, case-sensitive `%Cluster(%`. It now checks the exact expected function of each section - `urlCluster` and `s3Cluster` - case-insensitively. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Pushed: merged AI review, major 1 — AI review, major 2 — query log isolation. Addressed in CI failures on
@groeneai, investigate the performance failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120488&sha=3ac0adba968efdf04b768ba39c687690be44e86c&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%206%2F6%29 and provide a fix in a separate pull request. If the fix is already in progress, link it here. |
|
|
Correction to my last paragraph: the 5/6 red already has a merged fixing PR, and nothing is owed on it.
Only the 6/6 measurement is still open on my side. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units58 translation units recompiled, 681 s compile time in total, 58 of them have a recent master baseline. |
|
The pull request for the 6/6 measurement: #121189. The cause sits upstream of both judges I described. That accounts for the entire report red at So the constant user-CPU offset I measured earlier is unchanged and still not your change; the learned thresholds are what used to absorb it. #121189 moves the timeout to the constant praktika's own CIDB reader already uses and makes both fail-open branches say so on the report instead of only in the log. #121090 is still needed for the dashboard half of the gate. |
`url('s3://...')`, `url('az://...')` and `url('hdfs://...')` are delegated to
the matching object-storage table function, but the query AST still carries the
surface name `url`. When `parallel_replicas_for_cluster_engines` made the
delegate build its `*Cluster` storage,
`StorageObjectStorageCluster::updateQueryToSendIfNeeded` derived the forwarded
function name from that surface name and sent
`urlCluster('parallel_replicas', 's3://...', ...)` to the replicas - a shape
`TableFunctionURL::parseArgumentsImpl` rejects for every non-HTTP scheme, and
carrying the argument grammar of the delegate rather than of `url`.
Reproduced on a local server with `url_scheme_mappers` pointing `s3://` at
MinIO: both `SELECT count() FROM url('s3://test/x.csv', 'CSV', 'a UInt32')` and
`INSERT INTO t SELECT * FROM url('s3://test/x.csv', ...)` failed with
`BAD_ARGUMENTS: The urlCluster table function does not support the 'S3' scheme`.
Scheme dispatch is now resolved on the node that received the query: the
delegate is executed with `parallel_replicas_for_cluster_engines` turned off, so
it builds its plain storage. A plain `http(s)` `url` is untouched and still
forwards `urlCluster(...)` to the replicas.
Adds `test_url_s3_scheme_with_parallel_replicas` to
`tests/integration/test_s3_style_link`, covering both the `SELECT` and the
distributed `INSERT ... SELECT` shapes, and documents the limitation next to the
existing `urlCluster` note.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…uto-cluster-parallel-replicas
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
Backport #120488 to 26.9: Fix `INSERT ... SELECT FROM url()`/`s3()` with parallel replicas inserting the data once per replica
|
🕵 Thank you — that closes out the The #121189 and #121090 are noted, including that neither clears For the record on this PR: it merged as 295f03a. The one review thread that was still open at merge — the |
Cherry pick #120488 to 26.8: Fix `INSERT ... SELECT FROM url()`/`s3()` with parallel replicas inserting the data once per replica
Cherry pick #120488 to 26.7: Fix `INSERT ... SELECT FROM url()`/`s3()` with parallel replicas inserting the data once per replica
Cherry pick #120488 to 26.3: Fix `INSERT ... SELECT FROM url()`/`s3()` with parallel replicas inserting the data once per replica
…ith parallel replicas inserting the data once per replica
…ith parallel replicas inserting the data once per replica
…ith parallel replicas inserting the data once per replica
Backport #120488 to 26.7: Fix `INSERT ... SELECT FROM url()`/`s3()` with parallel replicas inserting the data once per replica
Backport #120488 to 26.8: Fix `INSERT ... SELECT FROM url()`/`s3()` with parallel replicas inserting the data once per replica
…`/`s3()` with parallel replicas inserting the data once per replica
… a replicated destination Follow-up to ClickHouse#120488. `test_url_s3_scheme_with_parallel_replicas` created its `INSERT ... SELECT` destination as a plain `MergeTree`. `InterpreterInsertQuery::distributedWriteIntoReplicatedMergeTreeOrDataLakeFromClusterStorage` returns early unless the destination supports replication, so the `parallel_distributed_insert_select = 2` half of the test never got as far as looking at the source storage: it only exercised the local fallback and would have passed just the same before the fix. Create the destination as a `ReplicatedMergeTree` (and give the instance a ZooKeeper) so the distributed path is entered for real. It then declines the query solely because `TableFunctionURL::executeImpl` runs the delegate with `parallel_replicas_for_cluster_engines` turned off and the delegated `url('s3://...')` therefore resolves to a plain storage rather than to an `IStorageCluster` - which is the property under test. Should the delegate ever fan out again, the forwarded query would name `urlCluster('s3://...')` and every replica would read the whole file, and the row count now catches that. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…`INSERT ... SELECT` Passing `'auto'` through the structure and format helper still rewrote the arguments: it overwrote an explicit `structure = '...'` key-value argument with `'auto'`, so a node could infer a different type than the query gave. Skip the helper on the `INSERT ... SELECT` paths instead: only the rename to the `*Cluster` variant and the cluster name argument are applied, and the other arguments reach the nodes as written, as before #120488. Add type-sensitive test cases for an explicit structure, positional and as a key-value argument. Related: #120488 Related: #121070 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…er functions The forwarded `INSERT ... SELECT` used to carry the names-and-types structure of the source storage instead of `'auto'`, so the nodes did not take the structure from the insertion table and filled fields missing in the files with the default values of their types instead of the `DEFAULT` expressions of the destination table. Related: ClickHouse#120488 Related: ClickHouse#121070 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Closes: #120485
Related: #100146
Related: ClickHouse/ClickBench#2046
With parallel replicas enabled,
parallel_replicas_for_cluster_enginesconverts a plainurl/s3/ ... table function into a cluster storage on the initiator, soINSERT INTO <replicated table> SELECT * FROM url(...)takes the distributedparallel_distributed_insert_selectpath and forwards the query to every replica of the cluster. The forwarded query text still named the plain function, and a replica running it as a secondary query created a plain storage that expanded the globs and read every file on its own instead of taking its share of the read tasks from the initiator, so N replicas inserted the data N times. This is what broke the ClickBench load for 2- and 3-replica services after 26.6.IStorageCluster::readalready rewrites the source for a plainSELECTviaupdateQueryToSendIfNeeded(the function becomes its*Clustervariant with the cluster name argument, and the structure and format arguments are added). This PR applies the same rewrite to the forwarded query inInterpreterInsertQuery::distributedWriteIntoReplicatedMergeTreeOrDataLakeFromClusterStorage, and makesupdateQueryToSendIfNeededpublic for that.urlwith a non-HTTP scheme (url('s3://...'),url('az://...'),url('hdfs://...')) is delegated to another table function while the query text keeps the surface nameurl, so the same rewrite producedurlCluster('s3://...')- a shapeurlClusterrejects, and with the argument grammar of the delegate rather than ofurl. That was already the behaviour of a plainSELECT. Scheme dispatch is now resolved on the node that received the query: the delegate runs withparallel_replicas_for_cluster_enginesturned off and builds its plain storage.tests/integration/test_s3_style_link::test_url_s3_scheme_with_parallel_replicascovers it.The scope here is the source cluster storage of the forwarded query. The sibling
Distributed-destination path,StorageDistributed::distributedWriteFromClusterStorage, needs the same rewrite and is fixed separately in #121070.The new test
05218_insert_select_auto_cluster_function_parallel_replicasinserts three files of 10, 20 and 30 rows throughurland throughs3withtest_cluster_one_shard_three_replicas_localhostas the parallel replicas cluster, and checks the row count, that every replica ran the forwardedINSERT, that the forwarded query names the*Clusterfunction, and that the replicas together read every file exactly once. On an unfixed binary it reports 180 rows instead of 60, 0 cluster-function queries and 180 rows read by the replicas.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
INSERT INTO <replicated table> SELECT ... FROM url(...)(ands3,azureBlobStorage, ... ) with parallel replicas enabled inserting the data once per replica: the query forwarded to the replicas now names the*Clustertable function, so every replica reads only its share of the files. Closes #120485.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/120488]
Version info
26.10.1.326-master(included in26.10and later)26.9.1.1624,26.8.11.31,26.7.14.28