提交 9b0da245 编写于 作者: A Alexey Milovidov

dbms: allowed to GROUP BY constants with no aggregates [#METR-18298].

上级 7fcae3dd
......@@ -211,13 +211,17 @@ void ExpressionAnalyzer::analyzeAggregation()
/// constant expressions have non-null column pointer at this stage
if (const auto is_constexpr = col.column)
{
if (i < group_asts.size() - 1)
group_asts[i] = std::move(group_asts.back());
/// but don't remove last key column if no aggregate functions, otherwise aggregation will not work
if (!aggregate_descriptions.empty() || group_asts.size() > 1)
{
if (i < group_asts.size() - 1)
group_asts[i] = std::move(group_asts.back());
group_asts.pop_back();
i -= 1;
group_asts.pop_back();
i -= 1;
continue;
continue;
}
}
NameAndTypePair key{column_name, col.type};
......@@ -781,7 +785,26 @@ void ExpressionAnalyzer::optimizeGroupBy()
}
if (group_exprs.empty())
select_query->group_expression_list = nullptr;
{
/** Нельзя полностью убирать GROUP BY. Потому что если при этом даже агрегатных функций не было, то получится, что не будет агрегации.
* Вместо этого оставим GROUP BY const.
* Далее см. удаление констант в методе analyzeAggregation.
*/
/// Нужно вставить константу, которая не является именем столбца таблицы. Такой случай редкий, но бывает.
UInt64 unused_column = 0;
String unused_column_name = toString(unused_column);
while (columns.end() != std::find_if(columns.begin(), columns.end(),
[&unused_column_name](const NameAndTypePair & name_type) { return name_type.name == unused_column_name; }))
{
++unused_column;
unused_column_name = toString(unused_column);
}
select_query->group_expression_list = new ASTExpressionList;
select_query->group_expression_list->children.push_back(new ASTLiteral(StringRange(), UInt64(unused_column)));
}
}
......
40
41
2 42
43
11
40
40
41
41
2 42
2 42
43
43
11
11
11
11
1
1
2
2
select 40 as z from (select * from system.numbers limit 3) group by z;
select 41 as z from remote('127.0.0.{1,2}', system.one) group by z;
select count(), 42 AS z from remote('127.0.0.{1,2}', system.one) group by z;
select 43 AS z from remote('127.0.0.{1,2}', system.one) group by 42, 43, 44;
select 11 AS z from (SELECT 2 UNION ALL SELECT 3) group by 42, 43, 44;
select 40 as z from (select * from system.numbers limit 3) group by z WITH TOTALS;
select 41 as z from remote('127.0.0.{1,2}', system.one) group by z WITH TOTALS;
select count(), 42 AS z from remote('127.0.0.{1,2}', system.one) group by z WITH TOTALS;
select 43 AS z from remote('127.0.0.{1,2}', system.one) group by 42, 43, 44 WITH TOTALS;
select 11 AS z from (SELECT 1 UNION ALL SELECT 2) group by 42, 43, 44 WITH TOTALS;
select 11 AS z from (SELECT 2 UNION ALL SELECT 3) group by 42, 43, 44 WITH TOTALS;
SELECT count() WITH TOTALS;
SELECT count() FROM remote('127.0.0.{1,2}', system.one) WITH TOTALS;
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册