diff --git a/src/Interpreters/SelectQueryOptions.h b/src/Interpreters/SelectQueryOptions.h index b7312971942..709ecdc239c 100644 --- a/src/Interpreters/SelectQueryOptions.h +++ b/src/Interpreters/SelectQueryOptions.h @@ -138,8 +138,6 @@ struct SelectQueryOptions shard_count = shard_count_; return *this; } - - bool hasShardInfo() const { return shard_num.has_value() || shard_count.has_value(); } }; } diff --git a/src/Interpreters/TreeRewriter.cpp b/src/Interpreters/TreeRewriter.cpp index 884e07791a8..a1b74fcd7a6 100644 --- a/src/Interpreters/TreeRewriter.cpp +++ b/src/Interpreters/TreeRewriter.cpp @@ -976,7 +976,8 @@ TreeRewriterResultPtr TreeRewriter::analyzeSelect( result.required_source_columns_before_expanding_alias_columns = result.required_source_columns.getNames(); /// rewrite filters for select query, must go after getArrayJoinedColumns - if (settings.optimize_respect_aliases && result.metadata_snapshot && !select_options.hasShardInfo()) + bool is_initiator = getContext()->getClientInfo().distributed_depth == 0; + if (settings.optimize_respect_aliases && result.metadata_snapshot && is_initiator) { /// If query is changed, we need to redo some work to correct name resolution. if (replaceAliasColumnsInQuery(query, result.metadata_snapshot->getColumns(), result.array_join_result_to_source, getContext())) diff --git a/tests/queries/0_stateless/02008_aliased_column_distributed_bug.reference b/tests/queries/0_stateless/02008_aliased_column_distributed_bug.reference index e69de29bb2d..406c915e7d7 100644 --- a/tests/queries/0_stateless/02008_aliased_column_distributed_bug.reference +++ b/tests/queries/0_stateless/02008_aliased_column_distributed_bug.reference @@ -0,0 +1,20 @@ +0 +0 +0 +0 +0 +0 +6 +7 +8 +9 +0 +0 +0 +0 +0 +0 +6 +7 +8 +9 diff --git a/tests/queries/0_stateless/02008_aliased_column_distributed_bug.sql b/tests/queries/0_stateless/02008_aliased_column_distributed_bug.sql index 1b3e01fb98e..92cc30c38dc 100644 --- a/tests/queries/0_stateless/02008_aliased_column_distributed_bug.sql +++ b/tests/queries/0_stateless/02008_aliased_column_distributed_bug.sql @@ -1,10 +1,15 @@ DROP TABLE IF EXISTS click_storage; DROP TABLE IF EXISTS click_storage_dst; -CREATE TABLE click_storage ( `PhraseID` UInt64, `PhraseProcessedID` UInt64 ALIAS if(PhraseID > 0, PhraseID, 0) ) ENGINE = MergeTree() ORDER BY tuple(); +CREATE TABLE click_storage ( `PhraseID` UInt64, `PhraseProcessedID` UInt64 ALIAS if(PhraseID > 5, PhraseID, 0) ) ENGINE = MergeTree() ORDER BY tuple(); +INSERT INTO click_storage SELECT number AS PhraseID from numbers(10); CREATE TABLE click_storage_dst ( `PhraseID` UInt64, `PhraseProcessedID` UInt64 ) ENGINE = Distributed(test_shard_localhost, currentDatabase(), 'click_storage'); +SET prefer_localhost_replica = 1; +SELECT materialize(PhraseProcessedID) FROM click_storage_dst; + +SET prefer_localhost_replica = 0; SELECT materialize(PhraseProcessedID) FROM click_storage_dst; DROP TABLE IF EXISTS click_storage;