Skip to content

Cherry pick #120488 to 26.8: Fix INSERT ... SELECT FROM url()/s3() with parallel replicas inserting the data once per replica - #121296

Merged
PedroTadim merged 12 commits into
backport/26.8/120488from
cherrypick/26.8/120488
Sep 23, 2026
Merged

PedroTadim merged 12 commits into
backport/26.8/120488from
cherrypick/26.8/120488

Conversation

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Original pull-request #120488

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

alexey-milovidov and others added 11 commits September 16, 2026 23:46
…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]>
…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]>
`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]>
…tation`

The note about a dispatched `url` call not being fanned out by
`parallel_replicas_for_cluster_engines` was added directly inside the
`AUTOGENERATED` region of `docs/reference/functions/table-functions/url.mdx`,
which fails the docs check and leaves the source of truth stale.

Move it into the `FunctionDocumentation` block of
`src/TableFunctions/TableFunctionURL.cpp`, next to the existing `urlCluster`
scheme-dispatch note, and revert the generated page.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ster-parallel-replicas

Fix `INSERT ... SELECT FROM url()`/`s3()` with parallel replicas inserting the data once per replica
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Sep 21, 2026
Only the condition analysis block conflicted: this branch still builds
`condition_ast` from `prewhere`/`where` and re-acquires the metadata and the
snapshot inside the `try`, where master reads them off the hoisted
`src_snapshot`. That body is kept as it is.

The change's own hoisting of `updateExternalDynamicMetadataIfExists` and of
`src_metadata_snapshot` is taken, because `updateQueryToSendIfNeeded` needs the
snapshot before the forwarded query text is built, and the late duplicate
declaration of `src_metadata_snapshot` is dropped with it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@PedroTadim
PedroTadim merged commit 5f7d1e7 into backport/26.8/120488 Sep 23, 2026
1 check passed
@PedroTadim
PedroTadim deleted the cherrypick/26.8/120488 branch September 23, 2026 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants