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;