diff --git a/src/Interpreters/ClusterProxy/SelectStreamFactory.cpp b/src/Interpreters/ClusterProxy/SelectStreamFactory.cpp index 7cb55f32162..f0692923ff2 100644 --- a/src/Interpreters/ClusterProxy/SelectStreamFactory.cpp +++ b/src/Interpreters/ClusterProxy/SelectStreamFactory.cpp @@ -160,7 +160,7 @@ void SelectStreamFactory::createForShard( const auto & settings = context->getSettingsRef(); - if (settings.prefer_localhost_replica && shard_info.isLocal()) + if ((settings.prefer_localhost_replica && shard_info.isLocal()) || !shard_info.hasRemoteConnections()) { StoragePtr main_table_storage; diff --git a/tests/queries/0_stateless/01813_use_local_shard_if_no_remote_connections.reference b/tests/queries/0_stateless/01813_use_local_shard_if_no_remote_connections.reference new file mode 100644 index 00000000000..aa47d0d46d4 --- /dev/null +++ b/tests/queries/0_stateless/01813_use_local_shard_if_no_remote_connections.reference @@ -0,0 +1,2 @@ +0 +0 diff --git a/tests/queries/0_stateless/01813_use_local_shard_if_no_remote_connections.sql b/tests/queries/0_stateless/01813_use_local_shard_if_no_remote_connections.sql new file mode 100644 index 00000000000..9bd80435ca9 --- /dev/null +++ b/tests/queries/0_stateless/01813_use_local_shard_if_no_remote_connections.sql @@ -0,0 +1,19 @@ +DROP DATABASE IF EXISTS 01813_db; +CREATE DATABASE 01813_db; + +DROP TABLE IF EXISTS 01813_db.data; +CREATE TABLE 01813_db.data (a Int64, b Int64) ENGINE = TinyLog(); + +DROP TABLE IF EXISTS 01813_db.data_distributed; +CREATE TABLE 01813_db.data_distributed (a Int64, b Int64) ENGINE = Distributed(test_shard_localhost, '01813_db', data); + +INSERT INTO 01813_db.data VALUES (0, 0); + +SET prefer_localhost_replica = 1; +SELECT a / (SELECT sum(number) FROM numbers(10)) FROM 01813_db.data_distributed; + +SET prefer_localhost_replica = 0; +SELECT a / (SELECT sum(number) FROM numbers(10)) FROM 01813_db.data_distributed; + +DROP TABLE 01813_db.data_distributed; +DROP TABLE 01813_db.data;