Zero copy final aggregation for any/anyFirst/anyLast #84428
+176
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Reduce memory allocation and memory copy when select from an aggregating merge tree table with FINAL when the table has columns with type
SimpleAggregateFunction(anyLast)
.Technical details
We have many tables like this:
The motivation is to be able to add an initial row to the table first (some values can be missing, thus they are NULL) and later update the the missing values (a.k.a. coalescing merge tree).
The query with FINAL is significantly slower than
ReplacingMergeTree
. We identify two places of the problem:a. Lock contention in arena allocation (more details in #79056 (comment))
b. Overhead in
memcpy
when merging rows for each column.Merging of type
SimpleAggregateFunction(anyLast, Nullable(String))
in AggregatingSortedAlgorithm is done by:The proposal is to keep the pointer to the original row (column pointer and row number) instead of copying the row to intermediate state. It works well because in the end we need to copy the intermediate state to the final row anyway. This way, we avoid:
Some result in our case: