From adca27cb45c70a14c15e3f7f51e289d317197331 Mon Sep 17 00:00:00 2001 From: Artem Zuikov Date: Thu, 26 Mar 2020 12:07:10 +0300 Subject: [PATCH] Fix PREWHERE with distributed IN (#9871) fix PREWHERE with distributed IN (local mode) --- .../InJoinSubqueriesPreprocessor.cpp | 4 ++-- dbms/src/Parsers/ASTIdentifier.cpp | 9 +++++++ dbms/src/Parsers/ASTIdentifier.h | 2 ++ .../01102_distributed_local_in_bug.reference | 0 .../01102_distributed_local_in_bug.sql | 24 +++++++++++++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 dbms/tests/queries/0_stateless/01102_distributed_local_in_bug.reference create mode 100644 dbms/tests/queries/0_stateless/01102_distributed_local_in_bug.sql diff --git a/dbms/src/Interpreters/InJoinSubqueriesPreprocessor.cpp b/dbms/src/Interpreters/InJoinSubqueriesPreprocessor.cpp index 843f443eeef..389d6c825b0 100644 --- a/dbms/src/Interpreters/InJoinSubqueriesPreprocessor.cpp +++ b/dbms/src/Interpreters/InJoinSubqueriesPreprocessor.cpp @@ -98,8 +98,8 @@ private: throw Exception("Distributed table should have an alias when distributed_product_mode set to local.", ErrorCodes::DISTRIBUTED_IN_JOIN_SUBQUERY_DENIED); - database_and_table = createTableIdentifier(database, table); - database_and_table->setAlias(alias); + auto & identifier = database_and_table->as(); + identifier.resetTable(database, table); } else throw Exception("InJoinSubqueriesPreprocessor: unexpected value of 'distributed_product_mode' setting", diff --git a/dbms/src/Parsers/ASTIdentifier.cpp b/dbms/src/Parsers/ASTIdentifier.cpp index 3da683dd186..c9d1ca328df 100644 --- a/dbms/src/Parsers/ASTIdentifier.cpp +++ b/dbms/src/Parsers/ASTIdentifier.cpp @@ -101,6 +101,15 @@ void ASTIdentifier::appendColumnNameImpl(WriteBuffer & ostr) const writeString(name, ostr); } +void ASTIdentifier::resetTable(const String & database_name, const String & table_name) +{ + auto ast = createTableIdentifier(database_name, table_name); + auto & ident = ast->as(); + name.swap(ident.name); + name_parts.swap(ident.name_parts); + uuid = ident.uuid; +} + ASTPtr createTableIdentifier(const String & database_name, const String & table_name) { assert(database_name != "_temporary_and_external_tables"); diff --git a/dbms/src/Parsers/ASTIdentifier.h b/dbms/src/Parsers/ASTIdentifier.h index 8ca75aca4ff..9e28a1461ca 100644 --- a/dbms/src/Parsers/ASTIdentifier.h +++ b/dbms/src/Parsers/ASTIdentifier.h @@ -49,6 +49,8 @@ public: return name; } + void resetTable(const String & database_name, const String & table_name); + protected: void formatImplWithoutAlias(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override; void appendColumnNameImpl(WriteBuffer & ostr) const override; diff --git a/dbms/tests/queries/0_stateless/01102_distributed_local_in_bug.reference b/dbms/tests/queries/0_stateless/01102_distributed_local_in_bug.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/dbms/tests/queries/0_stateless/01102_distributed_local_in_bug.sql b/dbms/tests/queries/0_stateless/01102_distributed_local_in_bug.sql new file mode 100644 index 00000000000..ea32f6ffc7d --- /dev/null +++ b/dbms/tests/queries/0_stateless/01102_distributed_local_in_bug.sql @@ -0,0 +1,24 @@ +DROP TABLE IF EXISTS hits; +DROP TABLE IF EXISTS visits; +DROP TABLE IF EXISTS hits_layer; +DROP TABLE IF EXISTS visits_layer; + +CREATE TABLE visits(StartDate Date) ENGINE MergeTree ORDER BY(StartDate); +CREATE TABLE hits(EventDate Date, WatchID UInt8) ENGINE MergeTree ORDER BY(EventDate); + +CREATE TABLE visits_layer(StartDate Date) ENGINE Distributed(test_cluster_two_shards_localhost, currentDatabase(), 'visits'); +CREATE TABLE hits_layer(EventDate Date, WatchID UInt8) ENGINE Distributed(test_cluster_two_shards_localhost, currentDatabase(), 'hits'); + +SET distributed_product_mode = 'local'; + +SELECT 0 FROM hits_layer AS hl +PREWHERE WatchID IN +( + SELECT 0 FROM visits_layer AS vl +) +WHERE 0; + +DROP TABLE hits; +DROP TABLE visits; +DROP TABLE hits_layer; +DROP TABLE visits_layer;