Merge pull request #24062 from amosbird/projection-fix1

Fix empty key projection query analysis
This commit is contained in:
Nikolai Kochetov 2021-05-16 07:37:03 +03:00 committed by GitHub
commit 093b7e120a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 6 deletions

View File

@ -1894,9 +1894,12 @@ void NO_INLINE Aggregator::mergeWithoutKeyStreamsImpl(
res = place;
}
/// Adding Values
for (size_t i = 0; i < params.aggregates_size; ++i)
aggregate_functions[i]->merge(res + offsets_of_aggregate_states[i], (*aggregate_columns[i])[0], result.aggregates_pool);
if (block.rows() > 0)
{
/// Adding Values
for (size_t i = 0; i < params.aggregates_size; ++i)
aggregate_functions[i]->merge(res + offsets_of_aggregate_states[i], (*aggregate_columns[i])[0], result.aggregates_pool);
}
/// Early release memory.
block.clear();

View File

@ -546,7 +546,12 @@ void AggregatingTransform::initGenerate()
/// If there was no data, and we aggregate without keys, and we must return single row with the result of empty aggregation.
/// To do this, we pass a block with zero rows to aggregate.
if (variants.empty() && params->params.keys_size == 0 && !params->params.empty_result_for_aggregation_by_empty_set)
params->aggregator.executeOnBlock(getInputs().front().getHeader(), variants, key_columns, aggregate_columns, no_more_keys);
{
if (params->only_merge)
params->aggregator.mergeBlock(getInputs().front().getHeader(), variants, no_more_keys);
else
params->aggregator.executeOnBlock(getInputs().front().getHeader(), variants, key_columns, aggregate_columns, no_more_keys);
}
double elapsed_seconds = watch.elapsedSeconds();
size_t rows = variants.sizeWithoutOverflowRow();

View File

@ -4041,7 +4041,7 @@ bool MergeTreeData::getQueryProcessingStageWithAggregateProjection(
auto required_columns = candidate.before_aggregation->foldActionsByProjection(keys, projection.sample_block_for_keys);
// std::cerr << fmt::format("before_aggregation = \n{}", candidate.before_aggregation->dumpDAG()) << std::endl;
// std::cerr << fmt::format("aggregate_required_columns = \n{}", fmt::join(required_columns, ", ")) << std::endl;
if (required_columns.empty())
if (required_columns.empty() && !keys.empty())
continue;
if (analysis_result.optimize_aggregation_in_order)

View File

@ -161,7 +161,11 @@ QueryPlanPtr MergeTreeDataSelectExecutor::read(
query_info.merge_tree_data_select_cache.get());
}
LOG_DEBUG(log, "Choose projection {}", query_info.projection->desc->name);
LOG_DEBUG(
log,
"Choose {} projection {}",
ProjectionDescription::typeToString(query_info.projection->desc->type),
query_info.projection->desc->name);
if (query_info.projection->merge_tree_data_select_base_cache->sum_marks
+ query_info.projection->merge_tree_data_select_projection_cache->sum_marks

View File

@ -3,3 +3,5 @@
2020-10-24 00:00:00 1.3619605237696326 0.16794469697335793 0.7637956767025532 0.8899329799574005 0.6227685185389797 0.30795997278638165 0.7637956767025532
2020-10-24 00:00:00 19 -1.9455094931672063 0.7759802460082872 0.6 0
2020-10-24 00:00:00 852 894
2 -1
999

View File

@ -38,4 +38,14 @@ select toStartOfMinute(datetime) dt_m, domain, sum(retry_count) / sum(duration),
select toStartOfHour(toStartOfMinute(datetime)) dt_h, uniqHLL12(x_id), uniqHLL12(y_id) from projection_test group by dt_h order by dt_h;
-- found by fuzzer
SELECT 2, -1 FROM projection_test PREWHERE domain_alias = 1. WHERE domain = NULL GROUP BY -9223372036854775808 ORDER BY countIf(first_time = 0) / count(-2147483649) DESC NULLS LAST, 1048576 DESC NULLS LAST;
drop table if exists projection_test;
drop table if exists projection_without_key;
create table projection_without_key (key UInt32, PROJECTION x (SELECT max(key))) engine MergeTree order by key;
insert into projection_without_key select number from numbers(1000);
set force_optimize_projection = 1, allow_experimental_projection_optimization = 1;
select max(key) from projection_without_key;
drop table projection_without_key;