Repository navigation
[feature](be) Add aggregate state finalizers and batch merging #68312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HappenLee
wants to merge
6
commits into
apache:master
Choose a base branch
from
HappenLee:feature/agg-state-finalize
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e39631a
[feature](be) Add scalar aggregate state finalize functions
HappenLee c620bed
[doc](fe) Document deferred aggregate state alias support
HappenLee 6e81215
[fix](fe) Preserve value expressions when matching aggregate state ro…
HappenLee 476e0d5
[improvement](be) Reuse scalar aggregate finalize state storage
HappenLee 6fd8235
[improvement](be) Skip trivial aggregate finalize state lifecycle
HappenLee b9a7047
[improvement](be) Batch aggregate state unions and merges
HappenLee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstring> | ||
|
|
||
| #include "core/arena.h" | ||
| #include "core/block/block.h" | ||
| #include "core/column/column_nullable.h" | ||
| #include "exprs/aggregate/aggregate_function.h" | ||
| #include "exprs/function/function.h" | ||
| #include "util/defer_op.h" | ||
|
|
||
| namespace doris { | ||
|
|
||
| class FunctionAggStateFinalize : public IFunction { | ||
| public: | ||
| FunctionAggStateFinalize(DataTypePtr return_type, AggregateFunctionPtr agg_function) | ||
| : _return_type(std::move(return_type)), _agg_function(std::move(agg_function)) {} | ||
|
|
||
| static FunctionBasePtr create(const DataTypes& argument_types, const DataTypePtr& return_type, | ||
| const AggregateFunctionPtr& agg_function) { | ||
| return std::make_shared<DefaultFunction>( | ||
| std::make_shared<FunctionAggStateFinalize>(return_type, agg_function), | ||
| argument_types, return_type); | ||
| } | ||
|
|
||
| String get_name() const override { return _agg_function->get_name() + "_finalize"; } | ||
|
|
||
| size_t get_number_of_arguments() const override { return 1; } | ||
|
|
||
| // An outer NULL can have an empty, invalid serialized payload. Do not deserialize it. | ||
| bool use_default_implementation_for_nulls() const override { return false; } | ||
|
|
||
| DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { | ||
| return _return_type; | ||
| } | ||
|
|
||
| Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, | ||
| uint32_t result, size_t input_rows_count) const override { | ||
| const auto input = | ||
| block.get_by_position(arguments[0]).column->convert_to_full_column_if_const(); | ||
| const auto* nullable = check_and_get_column<ColumnNullable>(*input); | ||
| const auto& states = nullable ? nullable->get_nested_column() : *input; | ||
| auto output = _agg_function->get_return_type()->create_column(); | ||
| _agg_function->check_result_column_type(*output); | ||
| output->reserve(input_rows_count); | ||
| // Keep the reusable state storage separate from its per-row variable-length data. | ||
| Arena state_arena; | ||
| const auto state_size = _agg_function->size_of_data(); | ||
| auto* place = state_arena.aligned_alloc(state_size, _agg_function->align_of_data()); | ||
| Arena arena; | ||
| auto finalize_row = [&](size_t row) { | ||
| // Serialized states can use native columns as well as strings. | ||
| _agg_function->deserialize_and_merge_from_column_range(place, states, row, row, arena); | ||
| _agg_function->insert_result_into(place, *output); | ||
| }; | ||
| auto finalize_rows = [&]<bool is_trivial>() { | ||
| for (size_t row = 0; row < input_rows_count; ++row) { | ||
| if (nullable && nullable->is_null_at(row)) { | ||
| output->insert_default(); | ||
| continue; | ||
| } | ||
| if constexpr (is_trivial) { | ||
| // Trivial states support zero-init and need no destruction. | ||
| std::memset(place, 0, state_size); | ||
| finalize_row(row); | ||
| } else { | ||
| _agg_function->create(place); | ||
| DEFER(_agg_function->destroy(place)); | ||
| finalize_row(row); | ||
| } | ||
| // Destroy states before reclaiming any variable-length data they own. | ||
| arena.clear(); | ||
| } | ||
| }; | ||
| if (_agg_function->is_trivial()) { | ||
| finalize_rows.template operator()<true>(); | ||
| } else { | ||
| finalize_rows.template operator()<false>(); | ||
| } | ||
| ColumnPtr result_column = std::move(output); | ||
| if (_return_type->is_nullable()) { | ||
| result_column = wrap_in_nullable(result_column, block, arguments, input_rows_count); | ||
| } | ||
| block.replace_by_position(result, std::move(result_column)); | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| private: | ||
| DataTypePtr _return_type; | ||
| AggregateFunctionPtr _agg_function; | ||
| }; | ||
|
|
||
| } // namespace doris |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Preserve the effective decimal aggregate result type
This equality check rejects valid states because
DataTypeAggStatecannot always reconstruct the return type FE planned from argument subtypes alone. For example, withenable_decimal256=true,sum_finalize(sum_state(cast(1 as decimal(12,2))))and the AVG equivalent have Decimal256 FE return types, but BE rebuilds their narrow state subtype as Decimal128. The problem is not limited to that flag: FE gives decimalsum_map/avg_mapaMAP<K,DECIMAL(38,s)>result, while the state storesMAP<K,DECIMAL(12,s)>; sinceDataTypeAggStateonly widens a top-level DecimalV3 subtype, BE rebuilds the map's nested aggregate with the narrow decimal family. In both casesnested->get_return_type()differs and prepare returnsInternalError. Please preserve the effective nested result type in the state/reconstruction contract (or otherwise make FE and BE derive it identically), with end-to-end scalar and MAP decimal finalizer coverage.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enable_decimal256is disabled by default. Given that default configuration, I suggest treating the Decimal256-specific case as non-blocking for this PR and addressing Decimal256 AggState support separately.The runtime type-consistency check should remain in place. The decimal MAP case mentioned above is independent of this flag, so the default being disabled does not address that separate case.