diff --git a/src/Databases/DatabaseReplicated.cpp b/src/Databases/DatabaseReplicated.cpp index 1f2d21d7f6a..1c13b0e8fcb 100644 --- a/src/Databases/DatabaseReplicated.cpp +++ b/src/Databases/DatabaseReplicated.cpp @@ -1206,6 +1206,13 @@ void DatabaseReplicated::recoverLostReplica(const ZooKeeperPtr & current_zookeep query_context->setSetting("database_replicated_allow_explicit_uuid", 3); query_context->setSetting("database_replicated_allow_replicated_engine_arguments", 3); + /// We apply the flatten_nested setting after writing the CREATE query to the DDL log, + /// but before writing metadata to ZooKeeper. So we have to apply the setting on secondary replicas, but not in recovery mode. + /// Set it to false, so it will do nothing on recovery. The metadata in ZooKeeper should be used as is. + /// Same for data_type_default_nullable. + query_context->setSetting("flatten_nested", false); + query_context->setSetting("data_type_default_nullable", false); + auto txn = std::make_shared(current_zookeeper, zookeeper_path, false, ""); query_context->initZooKeeperMetadataTransaction(txn); return query_context; diff --git a/tests/integration/test_replicated_database/test.py b/tests/integration/test_replicated_database/test.py index 533eb601ad6..dd06665b648 100644 --- a/tests/integration/test_replicated_database/test.py +++ b/tests/integration/test_replicated_database/test.py @@ -103,11 +103,27 @@ def test_flatten_nested(started_cluster): "CREATE MATERIALIZED VIEW create_replicated_table.mv ENGINE=ReplicatedMergeTree ORDER BY tuple() AS select d, cast([(k, toString(i32))] as Nested(a UInt64, b String)) from create_replicated_table.replicated_table" ) - assert main_node.query( - "show create create_replicated_table.mv" - ) == dummy_node.query("show create create_replicated_table.mv") + main_node.query( + "CREATE TABLE create_replicated_table.no_flatten (n Nested(a UInt64, b String)) ENGINE=ReplicatedMergeTree ORDER BY tuple();", + settings={"flatten_nested": 0}, + ) + + snapshot_recovering_node.query( + "CREATE DATABASE create_replicated_table ENGINE = Replicated('/test/create_replicated_table', 'shard1', 'replica3');" + ) + snapshot_recovering_node.query( + "SYSTEM SYNC DATABASE REPLICA create_replicated_table" + ) + + for node in [dummy_node, snapshot_recovering_node]: + for table in ["replicated_table", "mv", "no_flatten"]: + assert main_node.query( + f"show create create_replicated_table.{table}" + ) == node.query(f"show create create_replicated_table.{table}") + main_node.query("DROP DATABASE create_replicated_table SYNC") dummy_node.query("DROP DATABASE create_replicated_table SYNC") + snapshot_recovering_node.query("DROP DATABASE create_replicated_table SYNC") def test_create_replicated_table(started_cluster):