From ed49367fc750d0d50edaa4dde3cc7cb56598c305 Mon Sep 17 00:00:00 2001 From: Amos Bird Date: Sun, 14 Feb 2021 14:20:23 +0800 Subject: [PATCH] Fix global-with with subqueries --- src/Interpreters/InterpreterSelectWithUnionQuery.cpp | 8 ++++++-- src/Interpreters/InterpreterSelectWithUnionQuery.h | 3 ++- src/Interpreters/getTableExpressions.cpp | 2 +- .../0_stateless/01717_global_with_subquery_fix.reference | 0 .../0_stateless/01717_global_with_subquery_fix.sql | 1 + 5 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 tests/queries/0_stateless/01717_global_with_subquery_fix.reference create mode 100644 tests/queries/0_stateless/01717_global_with_subquery_fix.sql diff --git a/src/Interpreters/InterpreterSelectWithUnionQuery.cpp b/src/Interpreters/InterpreterSelectWithUnionQuery.cpp index e6610df43f..59fcff6193 100644 --- a/src/Interpreters/InterpreterSelectWithUnionQuery.cpp +++ b/src/Interpreters/InterpreterSelectWithUnionQuery.cpp @@ -329,7 +329,7 @@ InterpreterSelectWithUnionQuery::buildCurrentChildInterpreter(const ASTPtr & ast InterpreterSelectWithUnionQuery::~InterpreterSelectWithUnionQuery() = default; -Block InterpreterSelectWithUnionQuery::getSampleBlock(const ASTPtr & query_ptr_, const Context & context_) +Block InterpreterSelectWithUnionQuery::getSampleBlock(const ASTPtr & query_ptr_, const Context & context_, bool is_subquery) { auto & cache = context_.getSampleBlockCache(); /// Using query string because query_ptr changes for every internal SELECT @@ -339,7 +339,11 @@ Block InterpreterSelectWithUnionQuery::getSampleBlock(const ASTPtr & query_ptr_, return cache[key]; } - return cache[key] = InterpreterSelectWithUnionQuery(query_ptr_, context_, SelectQueryOptions().analyze()).getSampleBlock(); + if (is_subquery) + return cache[key] + = InterpreterSelectWithUnionQuery(query_ptr_, context_, SelectQueryOptions().subquery().analyze()).getSampleBlock(); + else + return cache[key] = InterpreterSelectWithUnionQuery(query_ptr_, context_, SelectQueryOptions().analyze()).getSampleBlock(); } diff --git a/src/Interpreters/InterpreterSelectWithUnionQuery.h b/src/Interpreters/InterpreterSelectWithUnionQuery.h index cd089a5197..f4062b2005 100644 --- a/src/Interpreters/InterpreterSelectWithUnionQuery.h +++ b/src/Interpreters/InterpreterSelectWithUnionQuery.h @@ -35,7 +35,8 @@ public: static Block getSampleBlock( const ASTPtr & query_ptr_, - const Context & context_); + const Context & context_, + bool is_subquery = false); virtual void ignoreWithTotals() override; diff --git a/src/Interpreters/getTableExpressions.cpp b/src/Interpreters/getTableExpressions.cpp index 766ce25753..a4e971c302 100644 --- a/src/Interpreters/getTableExpressions.cpp +++ b/src/Interpreters/getTableExpressions.cpp @@ -84,7 +84,7 @@ static NamesAndTypesList getColumnsFromTableExpression( if (table_expression.subquery) { const auto & subquery = table_expression.subquery->children.at(0); - names_and_type_list = InterpreterSelectWithUnionQuery::getSampleBlock(subquery, context).getNamesAndTypesList(); + names_and_type_list = InterpreterSelectWithUnionQuery::getSampleBlock(subquery, context, true).getNamesAndTypesList(); } else if (table_expression.table_function) { diff --git a/tests/queries/0_stateless/01717_global_with_subquery_fix.reference b/tests/queries/0_stateless/01717_global_with_subquery_fix.reference new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/queries/0_stateless/01717_global_with_subquery_fix.sql b/tests/queries/0_stateless/01717_global_with_subquery_fix.sql new file mode 100644 index 0000000000..14c4ac3e4c --- /dev/null +++ b/tests/queries/0_stateless/01717_global_with_subquery_fix.sql @@ -0,0 +1 @@ +WITH (SELECT count(distinct colU) from tabA) AS withA, (SELECT count(distinct colU) from tabA) AS withB SELECT withA / withB AS ratio FROM (SELECT date AS period, colX FROM (SELECT date, if(colA IN (SELECT colB FROM tabC), 0, colA) AS colX FROM tabB) AS tempB GROUP BY period, colX) AS main; -- {serverError 60} -- GitLab