Repository navigation
Fix distributed INSERT ... SELECT FROM cluster functions ignoring column DEFAULT expressions - #127657
Conversation
…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: #120488 Related: #121070 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
Workflow [PR], commit [f3abf4e] Summary: ✅
AI ReviewSummaryThis PR restores destination Findings❌ Blockers
Tests
Missing context / blind spots
Final VerdictStatus: ❌ Block. Preserve initiator-side argument evaluation and add the focused regression above before merging. |
There was a problem hiding this comment.
🟡 Changes recommended
Forwarded S3 inserts overwrite explicit key-value structures with auto, changing source parsing and default-value semantics.
1 open issue
What changed in this PR
This PR restores destination-column DEFAULT expressions for distributed INSERT ... SELECT from cluster table functions, addressing regressions introduced by #120488 and #121070.
Changes:
- Adds
forward_structureto distinguish reads requiring a shared schema from independently executed inserts. - Keeps automatic structure resolution for forwarded inserts while preserving cluster-function rewriting.
- Adds stateless regression coverage for S3 and URL sources with replicated and
Distributeddestinations.
| File | Description |
|---|---|
| tests/queries/0_stateless/05446_insert_select_cluster_function_column_defaults.sh | Tests missing-field defaults across distributed insert paths. |
| tests/queries/0_stateless/05446_insert_select_cluster_function_column_defaults.reference | Records expected default and explicit field values. |
| src/Storages/StorageURLCluster.h | Extends the URL rewrite interface. |
| src/Storages/StorageURLCluster.cpp | Makes URL structure forwarding conditional. |
| src/Storages/StorageFileCluster.h | Extends the file rewrite interface. |
| src/Storages/StorageFileCluster.cpp | Makes file structure forwarding conditional. |
| src/Storages/StorageDistributed.cpp | Disables structure forwarding for distributed inserts. |
| src/Storages/ObjectStorage/StorageObjectStorageCluster.h | Extends the object-storage rewrite interface. |
| src/Storages/ObjectStorage/StorageObjectStorageCluster.cpp | Makes object-storage structure forwarding conditional. |
| src/Storages/IStorageCluster.h | Defines and documents the structure-forwarding flag. |
| src/Storages/IStorageCluster.cpp | Keeps structure forwarding enabled for reads. |
| src/Interpreters/InterpreterInsertQuery.cpp | Disables structure forwarding for replicated-table and data-lake inserts. |
Review effort: Balanced
|
|
||
| ASTs & args = expression_list->children; | ||
| const auto & structure = storage_snapshot->metadata->getColumns().getAll().toNamesAndTypesDescription(); | ||
| const String structure = forward_structure ? storage_snapshot->metadata->getColumns().getAll().toNamesAndTypesDescription() : "auto"; |
groeneai
left a comment
There was a problem hiding this comment.
I reproduced the line 389 finding on a local debug build of bbc1d71, and it is a regression against master: with parallel_distributed_insert_select = 2, a key-value structure = '...' reaches the nodes as structure = 'auto'. Only the key-value branch of the S3 helper overwrites an explicit structure; its positional branches, the named-collection branch, Azure and HDFS keep it.
- With the 05446 files, the case below gives
q = 7(the destination'sDEFAULT) for{"id":1}. Master (44cf9c0),parallel_distributed_insert_select = 0and plains3give0. An auto-converteds3into aDistributedtable behaves the same way. - A headerless CSV read with
structure = 'a UInt32, b UInt32'andSELECT toString(a + b)now fails on the nodes withUNKNOWN_IDENTIFIER, since they inferc1, c2. Master inserts3and7.
With configuration->structure instead of "auto" (inline suggestion), both behave as on master and 05446 still matches its reference. The case, which fails on bbc1d71 and passes with the suggestion:
$CLICKHOUSE_CLIENT -q "
INSERT INTO dst_05446 (id, q) SELECT id, q FROM s3Cluster('${CLUSTER}', '${S3_GLOB}', 'JSONEachRow', structure = 'id UInt32, q UInt32')
SETTINGS parallel_distributed_insert_select = 2"
check dst_05446 "s3Cluster, key-value structure"--- s3Cluster, key-value structure ---
1 2000-01-01 0 unknown 10
2 2000-01-01 5 unknown 20
The rest of the change looks right to me.
|
|
||
| ASTs & args = expression_list->children; | ||
| const auto & structure = storage_snapshot->metadata->getColumns().getAll().toNamesAndTypesDescription(); | ||
| const String structure = forward_structure ? storage_snapshot->metadata->getColumns().getAll().toNamesAndTypesDescription() : "auto"; |
There was a problem hiding this comment.
configuration->structure is the structure as the user wrote it ('auto' unless given), so an explicit key-value structure survives and everything else still gets 'auto'.
| const String structure = forward_structure ? storage_snapshot->metadata->getColumns().getAll().toNamesAndTypesDescription() : "auto"; | |
| const String structure = forward_structure | |
| ? storage_snapshot->metadata->getColumns().getAll().toNamesAndTypesDescription() | |
| : configuration->structure; |
Build profile diff (arm_release)Commit See the job log for details. |
…`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]>
| if (add_structure_and_format) | ||
| configuration->addStructureAndFormatToArgsIfNeeded(args, structure, configuration->format, context, /*with_structure=*/true); |
There was a problem hiding this comment.
❌ [P1] Keep initiator-side argument evaluation when skipping schema injection
This helper also materializes positional argument expressions: the S3 implementation calls StorageURL::evalArgsAndCollectHeaders, which replaces them with literals. Skipping it changes which source the workers open. The earlier argument parsing does not preserve those evaluated values in the forwarded AST because TableFunctionObjectStorage::parseArguments parses a clone.
For example, consider a distributed insert whose source is:
s3Cluster('cluster', getMacro('ingest_url'), 'JSONEachRow', 'id UInt32')If the initiator's ingest_url is http://s3/bucket_a/file.jsonl and a worker's is http://s3/bucket_b/file.jsonl, previously the forwarded URL was the initiator's literal URL. Now the worker evaluates getMacro again and constructs its storage for bucket_b, while the initiator assigns it the key file.jsonl from bucket_a. The task carries the object path, not the S3 connection configuration, so this can silently insert rows from the wrong bucket; an initiator-only macro instead makes the insert fail. Both distributed insert callers pass false, and the auto-converted plain-function branch has the same regression.
Separate argument materialization from structure/format injection, retaining initiator-side evaluation on both branches while leaving omitted/'auto' structures available for destination defaults. Add a regression with different source-URL macros on the initiator and worker.
A `DEFAULT now64(3)` column missing in the files, with a TTL on it, inserted through `s3Cluster` with `src AS dest` aliases, query parameters and `NOSIGN`: with the default values of the type, the rows got `1970-01-01` and the TTL deleted them. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
It is not caused by this PR. The only failing row in The check compares against the previous release, which became |
groeneai
left a comment
There was a problem hiding this comment.
Re-reviewed 759cfb0 (merged into master 06be024, debug build) on a real 3-process cluster (one shard, three replicas, MinIO), parallel_distributed_insert_select = 2 into a ReplicatedMergeTree.
-
c345e9e fixes the key-value
structurefinding:structure = 'id UInt32, v String'reaches the nodes as written and gives1.50. 05446 (pointed at my MinIO) matches its reference, while master still writes1970-01-01,0and''in it. -
The bot's P1 (r4367065709) is real, but only reachable with a node-dependent argument.
addStructureAndFormatToArgsIfNeededS3materializes the positional arguments throughStorageURL::evalArgsAndCollectHeaders, so skipping it makes every node evaluate them again. Macrosrc= bucket-a on the initiator and bucket-b on the two other nodes,INSERT INTO dst SELECT id, tcpPort() FROM s3Cluster('pr3', concat(getMacro('src'), '/part_{1,2,3,4,5,6}.jsonl'), ..., 'JSONEachRow', 'id UInt32'):
| build | rows of the two other nodes | macro defined on the initiator only | missing field with DEFAULT 7 |
|---|---|---|---|
| master 06be024 | bucket-a | inserts | 0 |
| 26.8.28.14 | bucket-a | inserts | 0 |
| this PR | bucket-b (101..105) |
Code: 139 ... No macro init_only in config |
7 |
| 26.8.2.7 | bucket-b | Code: 139 |
7 |
The initiator's own share still comes from bucket-a, so one INSERT mixes both buckets. This is the behavior from before #120488 (26.8.2.7 predates its backport), and with pr-must-backport it would also come back on 26.7 to 26.10. A plain SELECT from the same s3Cluster reads bucket-a on every build, since IStorageCluster::read still materializes the arguments. urlCluster/fileCluster are not involved: their helper edits a copy of the arguments.
If you want to keep the evaluation on the initiator: calling the helper with the structure as written instead of skipping it (add_structure_and_format ? structure : configuration->structure, my earlier suggestion, at both call sites) gives bucket-a, inserts with the initiator-only macro, DEFAULT 7 and 1.50, and 05446 matches its reference. The flag then only selects the structure, so its name and the comment in IStorageCluster.h would need an update.
| if (add_structure_and_format) | ||
| configuration->addStructureAndFormatToArgsIfNeeded(args, structure, configuration->format, context, /*with_structure=*/true); |
There was a problem hiding this comment.
| if (add_structure_and_format) | |
| configuration->addStructureAndFormatToArgsIfNeeded(args, structure, configuration->format, context, /*with_structure=*/true); | |
| configuration->addStructureAndFormatToArgsIfNeeded( | |
| args, add_structure_and_format ? structure : configuration->structure, configuration->format, context, /*with_structure=*/true); |
| if (add_structure_and_format) | ||
| configuration->addStructureAndFormatToArgsIfNeeded(args, structure, configuration->format, context, /*with_structure=*/true); |
There was a problem hiding this comment.
| if (add_structure_and_format) | |
| configuration->addStructureAndFormatToArgsIfNeeded(args, structure, configuration->format, context, /*with_structure=*/true); | |
| configuration->addStructureAndFormatToArgsIfNeeded( | |
| args, add_structure_and_format ? structure : configuration->structure, configuration->format, context, /*with_structure=*/true); |
skuznetsov-clickhouse
left a comment
There was a problem hiding this comment.
Looks good for a majority of cases.
For possible edge cases I created an issue #127792
Backport #127657 to 26.10: Fix distributed `INSERT ... SELECT FROM` cluster functions ignoring column `DEFAULT` expressions
Keep the release branch older cluster API and carry the fix at its existing distributed INSERT path. Forward the source arguments as written for INSERT so destination DEFAULT expressions remain available; SELECT continues adding structure and format. Omit the test Distributed-destination case, whose newer source rewrite is absent on this branch. Related: #127657 Related: ClickHouse/support-escalation#8987 Verification: git diff --check against the PR base; bash -n on the 05446 test. Build and runtime test remain for CI.
Cherry pick #127657 to 26.8: Fix distributed `INSERT ... SELECT FROM` cluster functions ignoring column `DEFAULT` expressions
…uster functions ignoring column `DEFAULT` expressions
Backport #127657 to 26.8: Fix distributed `INSERT ... SELECT FROM` cluster functions ignoring column `DEFAULT` expressions
Cherry pick #127657 to 26.9: Fix distributed `INSERT ... SELECT FROM` cluster functions ignoring column `DEFAULT` expressions
…uster functions ignoring column `DEFAULT` expressions
Backport #127657 to 26.9: Fix distributed `INSERT ... SELECT FROM` cluster functions ignoring column `DEFAULT` expressions
An old server (the public `clickhouse/clickhouse-server:26.6` image) creates a `DataLakeCatalog` database over a local REST catalog, an `IcebergS3` table through a named collection, and views and materialized views over them, then is restarted on the binary under test with the same metadata. An old and a new server also run parallel replicas over a catalog table, `icebergS3Cluster`, cross-version Iceberg writes and a distributed `INSERT ... SELECT FROM s3Cluster` that must apply the destination `DEFAULT` expressions (ClickHouse#127657). Single-node clusters make each version read every file for a query initiated on the other one.
…uster functions ignoring column `DEFAULT` expressions
…uster functions ignoring column `DEFAULT` expressions
…luster functions ignoring column `DEFAULT` expressions

Related: #120488
Related: #121070
A distributed
INSERT ... SELECT FROM s3Cluster(...)(and the other object storage cluster functions, or a plains3converted byparallel_replicas_for_cluster_engines) into a replicated table withparallel_distributed_insert_select = 2(the default), or into aDistributedtable, silently wrote0,''or1970-01-01instead of theDEFAULTexpressions of the destination table for fields missing in the source files. No error is raised. When a TTL depends on such a column, the rows are expired right after insertion.The cause is #120488, which started to apply
IStorageCluster::updateQueryToSendIfNeededto the forwardedINSERT(#121070 did the same for aDistributeddestination). Besides renaming the function to its*Clustervariant, which is what fixed the duplicate inserts, it adds the structure of the source storage to the arguments in place of an omitted or'auto'structure. That structure has names and types only. Before, the forwarded query was sent as written, and every node took the structure from the insertion table (use_structure_from_insertion_table_in_table_functions), which carries theDEFAULTexpressions, so the missing fields got their defaults.updateQueryToSendIfNeedednow has anadd_structure_and_formatargument.IStorageCluster::readstill adds the structure and format, because the initiator merges the streams of the nodes and needs them to produce its header. The twoINSERT ... SELECTpaths skip that step: only the rename to the*Clustervariant and the cluster name argument are applied, and all other arguments reach the nodes as written, as before #120488. Every node inserts on its own and only reports progress, so there is no header to agree on.An earlier version of this PR passed
'auto'as the structure instead. That still rewrote the arguments and replaced an explicitstructure = '...'key-value argument with'auto', so a node could infer a different type than the query gave; skipping the step entirely avoids changing anything that was not changed before #120488.Notes
urlClusterandfileClusterwere not affected:ITableFunctionCluster::updateStructureAndFormatArgumentsIfNeededmodifies a copy of the arguments, so no structure was ever added for them (verified through the forwarded query insystem.query_log). They take the new argument only to keep the interface uniform.system.query_log:1970-01-01,0and''in thes3Cluster, auto-converteds3andDistributedcases, and the forwarded query carries'`id` UInt32, `d` Date, ...';s3Cluster(..., 'JSONEachRow')is forwarded without a structure, a positional orkey = valuestructure is forwarded as written, and theDEFAULTexpressions are applied;05218_insert_select_auto_cluster_function_parallel_replicasand05219_insert_select_auto_cluster_function_distributed(from FixINSERT ... SELECT FROM url()/s3()with parallel replicas inserting the data once per replica #120488 and FixINSERT INTO <Distributed table> SELECT FROM url()/s3()with parallel replicas: duplicated data and bypassed sharding key #121070) still pass.INSERT ... SELECT FROM url()/s3()with parallel replicas inserting the data once per replica #120488 and FixINSERT INTO <Distributed table> SELECT FROM url()/s3()with parallel replicas: duplicated data and bypassed sharding key #121070 only. Withparallel_distributed_insert_select = 0, theDEFAULTexpressions are still lost, because only theSELECTis distributed and it has always been sent with names and types only. That behavior predates FixINSERT ... SELECT FROM url()/s3()with parallel replicas inserting the data once per replica #120488 and is left unchanged here.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
INSERT INTO <replicated or Distributed table> SELECT ... FROM s3Cluster(...)(and other object storage cluster table functions, ors3converted tos3Clusterby parallel replicas) writing the default values of the column types (0, empty string,1970-01-01) instead of theDEFAULTexpressions of the destination table for fields missing in the source files.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/127657]
Version info
26.11.1.782-master(included in26.11and later)26.10.3.7,26.9.26.20,26.8.35.16