From a34411f51eb8b6cb34123ea85967ce5288622d82 Mon Sep 17 00:00:00 2001 From: Nikolai Kochetov Date: Wed, 23 Dec 2020 17:48:14 +0300 Subject: [PATCH] Disable trivial_insert_select if query has aggregate functions. (cherry picked from commit 70ae043455a8e8c238394436a95f9aa876e24fc4) --- src/Interpreters/InterpreterInsertQuery.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Interpreters/InterpreterInsertQuery.cpp b/src/Interpreters/InterpreterInsertQuery.cpp index 8408b0ac5fc..5c91b41845b 100644 --- a/src/Interpreters/InterpreterInsertQuery.cpp +++ b/src/Interpreters/InterpreterInsertQuery.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -153,7 +154,18 @@ Block InterpreterInsertQuery::getSampleBlock( return res; } +static bool hasAggregateFunctions(const IAST * ast) +{ + if (const auto * func = typeid_cast(ast)) + if (AggregateFunctionFactory::instance().isAggregateFunctionName(func->name)) + return true; + for (const auto & child : ast->children) + if (hasAggregateFunctions(child.get())) + return true; + + return false; +} /** A query that just reads all data without any complex computations or filetering. * If we just pipe the result to INSERT, we don't have to use too many threads for read. */ @@ -186,7 +198,8 @@ static bool isTrivialSelect(const ASTPtr & select) && !select_query->groupBy() && !select_query->having() && !select_query->orderBy() - && !select_query->limitBy()); + && !select_query->limitBy() + && !hasAggregateFunctions(select_query)); } /// This query is ASTSelectWithUnionQuery subquery return false;