fix bug when remove unneeded columns in subquery

This commit is contained in:
luocongkai 2022-09-01 17:10:32 +08:00
parent e98ceb2575
commit 56e4179cad
3 changed files with 11 additions and 4 deletions

View File

@ -521,10 +521,15 @@ void removeUnneededColumnsFromSelectClause(ASTSelectQuery * select_query, const
++new_elements_size;
}
/// removing aggregation can change number of rows, so `count()` result in outer sub-query would be wrong
if (func && AggregateUtils::isAggregateFunction(*func) && !select_query->groupBy())
if (func && !select_query->groupBy())
{
new_elements[result_index] = elem;
++new_elements_size;
GetAggregatesVisitor::Data data = {};
GetAggregatesVisitor(data).visit(elem);
if (!data.aggregates.empty())
{
new_elements[result_index] = elem;
++new_elements_size;
}
}
}
}

View File

@ -1 +1,2 @@
select count(1) from (SELECT 1 AS a, count(1) FROM numbers(5))
select count(1) from (SELECT 1 AS a, count(1) FROM numbers(5));
select count(1) from (SELECT 1 AS a, count(1) + 1 FROM numbers(5));