fix group by with totals/rollup/cube modifers and min/max functions over group by keys

This commit is contained in:
Anton Popov 2020-10-26 16:44:46 +03:00
parent 840d96255e
commit ab4c43cb81
3 changed files with 44 additions and 1 deletions

View File

@ -644,8 +644,13 @@ void TreeOptimizer::apply(ASTPtr & query, Aliases & aliases, const NameSet & sou
optimizeInjectiveFunctionsInsideUniq(query, context);
/// Eliminate min/max/any aggregators of functions of GROUP BY keys
if (settings.optimize_aggregators_of_group_by_keys)
if (settings.optimize_aggregators_of_group_by_keys
&& !select_query->group_by_with_totals
&& !select_query->group_by_with_rollup
&& !select_query->group_by_with_cube)
{
optimizeAggregateFunctionsOfGroupByKeys(select_query, query);
}
/// Remove duplicate items from ORDER BY.
optimizeDuplicatesInOrderBy(select_query);

View File

@ -0,0 +1,20 @@
totals
1 1 1
2 2 2
3 3 3
0 1 3
rollup
1 1 1
2 2 2
3 3 3
0 1 3
cube
1 1 1
2 2 2
3 3 3
0 1 3
=======
1 1 2 1
2 2 3 1
0 1 3 2

View File

@ -0,0 +1,18 @@
SELECT 'totals';
SELECT number % 3 + 1 AS n, min(n), max(n) FROM numbers(100) GROUP BY n WITH TOTALS;
SELECT 'rollup';
SELECT number % 3 + 1 AS n, min(n), max(n) FROM numbers(100) GROUP BY n WITH ROLLUP;
SELECT 'cube';
SELECT number % 3 + 1 AS n, min(n), max(n) FROM numbers(100) GROUP BY n WITH CUBE;
SELECT '=======';
SELECT
x,
min(x) AS lower,
max(x) + 1 AS upper,
upper - lower AS range
FROM
(
SELECT arrayJoin([1, 2]) AS x
)
GROUP BY x WITH ROLLUP;