Merge pull request #40884 from TKaxe/fix_removeUnneededColumn

fix bug when remove unneeded columns in subquery
This commit is contained in:
Kseniia Sumarokova 2022-09-02 18:51:18 +02:00 committed by GitHub
commit 7dea71c83f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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));