This commit is contained in:
kssenii 2021-07-03 07:45:37 +00:00
parent 1776b06ef8
commit 02681019f8
6 changed files with 12 additions and 13 deletions

View File

@ -231,7 +231,6 @@ void ExpressionAnalyzer::analyzeAggregation()
if (has_aggregation)
{
/// Find out aggregation keys.
if (select_query)
{
@ -252,6 +251,8 @@ void ExpressionAnalyzer::analyzeAggregation()
/// Constant expressions have non-null column pointer at this stage.
if (node->column && isColumnConst(*node->column))
{
select_query->group_by_with_constant_keys = true;
/// But don't remove last key column if no aggregate functions, otherwise aggregation will not work.
if (!aggregate_descriptions.empty() || size > 1)
{
@ -288,6 +289,10 @@ void ExpressionAnalyzer::analyzeAggregation()
else
aggregated_columns = temp_actions->getNamesAndTypesList();
/// Constant expressions are already removed during first 'analyze' run.
/// So for second `analyze` information is taken from select_query.
has_const_aggregation_keys = select_query->group_by_with_constant_keys;
for (const auto & desc : aggregate_descriptions)
aggregated_columns.emplace_back(desc.column_name, desc.function->getReturnType());
}

View File

@ -65,6 +65,7 @@ struct ExpressionAnalyzerData
bool has_aggregation = false;
NamesAndTypesList aggregation_keys;
bool has_const_aggregation_keys = false;
AggregateDescriptions aggregate_descriptions;
WindowDescriptions window_descriptions;
@ -309,6 +310,7 @@ public:
bool hasTableJoin() const { return syntax->ast_join; }
const NamesAndTypesList & aggregationKeys() const { return aggregation_keys; }
bool hasConstAggregationKeys() const { return has_const_aggregation_keys; }
const AggregateDescriptions & aggregates() const { return aggregate_descriptions; }
const PreparedSets & getPreparedSets() const { return prepared_sets; }

View File

@ -2035,7 +2035,7 @@ void InterpreterSelectQuery::executeAggregation(QueryPlan & query_plan, const Ac
settings.group_by_two_level_threshold,
settings.group_by_two_level_threshold_bytes,
settings.max_bytes_before_external_group_by,
settings.empty_result_for_aggregation_by_empty_set,
settings.empty_result_for_aggregation_by_empty_set || (keys.empty() && query_analyzer->hasConstAggregationKeys()),
context->getTemporaryVolume(),
settings.max_threads,
settings.min_free_disk_space_for_temporary_data);

View File

@ -44,6 +44,7 @@ public:
bool group_by_with_totals = false;
bool group_by_with_rollup = false;
bool group_by_with_cube = false;
bool group_by_with_constant_keys = false;
bool limit_with_ties = false;
ASTPtr & refSelect() { return getExpression(Expression::SELECT); }

View File

@ -1,5 +1 @@
1 0
1 0
1 0
2 1 0
D 0
0

View File

@ -1,7 +1,2 @@
SELECT 1 as a, count() FROM numbers(10) WHERE 0 GROUP BY a;
SELECT materialize(1) as a, count() FROM numbers(10) WHERE 0 GROUP BY a;
SELECT materialize(1) as a, count() FROM numbers(10) WHERE 0 ORDER BY a;
SELECT 2 as b, less(1, b) as a, count() FROM numbers(10) WHERE 0 GROUP BY a;
SELECT upper('d') as a, count() FROM numbers(10) WHERE 0 GROUP BY a;
SELECT count() FROM numbers(10) WHERE 0