Coverage Report

Created: 2026-03-16 11:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/common/agg_utils.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <variant>
21
#include <vector>
22
23
#include "core/arena.h"
24
#include "exec/common/hash_table/hash_map_context.h"
25
#include "exec/common/hash_table/hash_map_util.h"
26
#include "exec/common/hash_table/ph_hash_map.h"
27
#include "exec/common/hash_table/string_hash_map.h"
28
29
namespace doris {
30
31
template <typename T>
32
using AggData = PHHashMap<T, AggregateDataPtr, HashCRC32<T>>;
33
template <typename T>
34
using AggDataNullable = DataWithNullKey<AggData<T>>;
35
36
using AggregatedDataWithoutKey = AggregateDataPtr;
37
using AggregatedDataWithStringKey = PHHashMap<StringRef, AggregateDataPtr>;
38
using AggregatedDataWithShortStringKey = StringHashMap<AggregateDataPtr>;
39
40
using AggregatedDataWithUInt32KeyPhase2 =
41
        PHHashMap<UInt32, AggregateDataPtr, HashMixWrapper<UInt32>>;
42
using AggregatedDataWithUInt64KeyPhase2 =
43
        PHHashMap<UInt64, AggregateDataPtr, HashMixWrapper<UInt64>>;
44
45
using AggregatedDataWithNullableUInt32KeyPhase2 =
46
        DataWithNullKey<AggregatedDataWithUInt32KeyPhase2>;
47
using AggregatedDataWithNullableUInt64KeyPhase2 =
48
        DataWithNullKey<AggregatedDataWithUInt64KeyPhase2>;
49
using AggregatedDataWithNullableShortStringKey = DataWithNullKey<AggregatedDataWithShortStringKey>;
50
51
using AggregatedMethodVariants = std::variant<
52
        std::monostate, MethodSerialized<AggregatedDataWithStringKey>,
53
        MethodOneNumber<UInt8, AggData<UInt8>>, MethodOneNumber<UInt16, AggData<UInt16>>,
54
        MethodOneNumber<UInt32, AggData<UInt32>>, MethodOneNumber<UInt64, AggData<UInt64>>,
55
        MethodStringNoCache<AggregatedDataWithShortStringKey>,
56
        MethodOneNumber<UInt128, AggData<UInt128>>, MethodOneNumber<UInt256, AggData<UInt256>>,
57
        MethodOneNumber<UInt32, AggregatedDataWithUInt32KeyPhase2>,
58
        MethodOneNumber<UInt64, AggregatedDataWithUInt64KeyPhase2>,
59
        MethodSingleNullableColumn<MethodOneNumber<UInt8, AggDataNullable<UInt8>>>,
60
        MethodSingleNullableColumn<MethodOneNumber<UInt16, AggDataNullable<UInt16>>>,
61
        MethodSingleNullableColumn<MethodOneNumber<UInt32, AggDataNullable<UInt32>>>,
62
        MethodSingleNullableColumn<MethodOneNumber<UInt64, AggDataNullable<UInt64>>>,
63
        MethodSingleNullableColumn<
64
                MethodOneNumber<UInt32, AggregatedDataWithNullableUInt32KeyPhase2>>,
65
        MethodSingleNullableColumn<
66
                MethodOneNumber<UInt64, AggregatedDataWithNullableUInt64KeyPhase2>>,
67
        MethodSingleNullableColumn<MethodOneNumber<UInt128, AggDataNullable<UInt128>>>,
68
        MethodSingleNullableColumn<MethodOneNumber<UInt256, AggDataNullable<UInt256>>>,
69
        MethodSingleNullableColumn<MethodStringNoCache<AggregatedDataWithNullableShortStringKey>>,
70
        MethodKeysFixed<AggData<UInt64>>, MethodKeysFixed<AggData<UInt72>>,
71
        MethodKeysFixed<AggData<UInt96>>, MethodKeysFixed<AggData<UInt104>>,
72
        MethodKeysFixed<AggData<UInt128>>, MethodKeysFixed<AggData<UInt136>>,
73
        MethodKeysFixed<AggData<UInt256>>>;
74
75
struct AggregatedDataVariants
76
        : public DataVariants<AggregatedMethodVariants, MethodSingleNullableColumn, MethodOneNumber,
77
                              DataWithNullKey> {
78
    AggregatedDataWithoutKey without_key = nullptr;
79
80
63.6k
    void init(const std::vector<DataTypePtr>& data_types, HashKeyType type) {
81
63.6k
        bool nullable = data_types.size() == 1 && data_types[0]->is_nullable();
82
83
63.6k
        switch (type) {
84
1
        case HashKeyType::without_key:
85
1
            break;
86
15.9k
        case HashKeyType::serialized:
87
15.9k
            method_variant.emplace<MethodSerialized<AggregatedDataWithStringKey>>();
88
15.9k
            break;
89
2.12k
        case HashKeyType::int8_key:
90
2.12k
            emplace_single<UInt8, AggData<UInt8>>(nullable);
91
2.12k
            break;
92
1.16k
        case HashKeyType::int16_key:
93
1.16k
            emplace_single<UInt16, AggData<UInt16>>(nullable);
94
1.16k
            break;
95
2.95k
        case HashKeyType::int32_key:
96
2.95k
            emplace_single<UInt32, AggData<UInt32>>(nullable);
97
2.95k
            break;
98
10.3k
        case HashKeyType::int32_key_phase2:
99
10.3k
            emplace_single<UInt32, AggregatedDataWithUInt32KeyPhase2>(nullable);
100
10.3k
            break;
101
2.11k
        case HashKeyType::int64_key:
102
2.11k
            emplace_single<UInt64, AggData<UInt64>>(nullable);
103
2.11k
            break;
104
7.26k
        case HashKeyType::int64_key_phase2:
105
7.26k
            emplace_single<UInt64, AggregatedDataWithUInt64KeyPhase2>(nullable);
106
7.26k
            break;
107
523
        case HashKeyType::int128_key:
108
523
            emplace_single<UInt128, AggData<UInt128>>(nullable);
109
523
            break;
110
13
        case HashKeyType::int256_key:
111
13
            emplace_single<UInt256, AggData<UInt256>>(nullable);
112
13
            break;
113
4.15k
        case HashKeyType::string_key:
114
4.15k
            if (nullable) {
115
2.98k
                method_variant.emplace<MethodSingleNullableColumn<
116
2.98k
                        MethodStringNoCache<AggregatedDataWithNullableShortStringKey>>>();
117
2.98k
            } else {
118
1.16k
                method_variant.emplace<MethodStringNoCache<AggregatedDataWithShortStringKey>>();
119
1.16k
            }
120
4.15k
            break;
121
1.49k
        case HashKeyType::fixed64:
122
1.49k
            method_variant.emplace<MethodKeysFixed<AggData<UInt64>>>(get_key_sizes(data_types));
123
1.49k
            break;
124
1.61k
        case HashKeyType::fixed72:
125
1.61k
            method_variant.emplace<MethodKeysFixed<AggData<UInt72>>>(get_key_sizes(data_types));
126
1.61k
            break;
127
893
        case HashKeyType::fixed96:
128
893
            method_variant.emplace<MethodKeysFixed<AggData<UInt96>>>(get_key_sizes(data_types));
129
893
            break;
130
1.96k
        case HashKeyType::fixed104:
131
1.96k
            method_variant.emplace<MethodKeysFixed<AggData<UInt104>>>(get_key_sizes(data_types));
132
1.96k
            break;
133
226
        case HashKeyType::fixed128:
134
226
            method_variant.emplace<MethodKeysFixed<AggData<UInt128>>>(get_key_sizes(data_types));
135
226
            break;
136
2.99k
        case HashKeyType::fixed136:
137
2.99k
            method_variant.emplace<MethodKeysFixed<AggData<UInt136>>>(get_key_sizes(data_types));
138
2.99k
            break;
139
7.85k
        case HashKeyType::fixed256:
140
7.85k
            method_variant.emplace<MethodKeysFixed<AggData<UInt256>>>(get_key_sizes(data_types));
141
7.85k
            break;
142
1
        default:
143
1
            throw Exception(ErrorCode::INTERNAL_ERROR,
144
1
                            "AggregatedDataVariants meet invalid key type, type={}", type);
145
63.6k
        }
146
63.6k
    }
147
};
148
149
using AggregatedDataVariantsUPtr = std::unique_ptr<AggregatedDataVariants>;
150
using ArenaUPtr = std::unique_ptr<Arena>;
151
152
struct AggregateDataContainer {
153
public:
154
    AggregateDataContainer(size_t size_of_key, size_t size_of_aggregate_states)
155
69.9k
            : _size_of_key(size_of_key), _size_of_aggregate_states(size_of_aggregate_states) {}
156
157
88.2k
    int64_t memory_usage() const { return _arena_pool.size(); }
158
159
    template <typename KeyType>
160
4.71M
    AggregateDataPtr append_data(const KeyType& key) {
161
4.71M
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
162
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
163
4.71M
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
25.9k
            _expand();
165
25.9k
        }
166
167
4.71M
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
4.71M
        auto* aggregate_data = _current_agg_data;
169
4.71M
        ++_total_count;
170
4.71M
        ++_index_in_sub_container;
171
4.71M
        _current_agg_data += _size_of_aggregate_states;
172
4.71M
        _current_keys += _size_of_key;
173
4.71M
        return aggregate_data;
174
4.71M
    }
_ZN5doris22AggregateDataContainer11append_dataINS_9StringRefEEEPcRKT_
Line
Count
Source
160
88.5k
    AggregateDataPtr append_data(const KeyType& key) {
161
88.5k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
162
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
163
88.5k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
5.36k
            _expand();
165
5.36k
        }
166
167
88.5k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
88.5k
        auto* aggregate_data = _current_agg_data;
169
88.5k
        ++_total_count;
170
88.5k
        ++_index_in_sub_container;
171
88.5k
        _current_agg_data += _size_of_aggregate_states;
172
88.5k
        _current_keys += _size_of_key;
173
88.5k
        return aggregate_data;
174
88.5k
    }
_ZN5doris22AggregateDataContainer11append_dataIhEEPcRKT_
Line
Count
Source
160
3.86k
    AggregateDataPtr append_data(const KeyType& key) {
161
3.86k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
162
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
163
3.86k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
1.45k
            _expand();
165
1.45k
        }
166
167
3.86k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
3.86k
        auto* aggregate_data = _current_agg_data;
169
3.86k
        ++_total_count;
170
3.86k
        ++_index_in_sub_container;
171
3.86k
        _current_agg_data += _size_of_aggregate_states;
172
3.86k
        _current_keys += _size_of_key;
173
3.86k
        return aggregate_data;
174
3.86k
    }
_ZN5doris22AggregateDataContainer11append_dataItEEPcRKT_
Line
Count
Source
160
2.65k
    AggregateDataPtr append_data(const KeyType& key) {
161
2.65k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
162
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
163
2.65k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
552
            _expand();
165
552
        }
166
167
2.65k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
2.65k
        auto* aggregate_data = _current_agg_data;
169
2.65k
        ++_total_count;
170
2.65k
        ++_index_in_sub_container;
171
2.65k
        _current_agg_data += _size_of_aggregate_states;
172
2.65k
        _current_keys += _size_of_key;
173
2.65k
        return aggregate_data;
174
2.65k
    }
_ZN5doris22AggregateDataContainer11append_dataIjEEPcRKT_
Line
Count
Source
160
4.05M
    AggregateDataPtr append_data(const KeyType& key) {
161
4.05M
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
162
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
163
4.05M
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
9.46k
            _expand();
165
9.46k
        }
166
167
4.05M
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
4.05M
        auto* aggregate_data = _current_agg_data;
169
4.05M
        ++_total_count;
170
4.05M
        ++_index_in_sub_container;
171
4.05M
        _current_agg_data += _size_of_aggregate_states;
172
4.05M
        _current_keys += _size_of_key;
173
4.05M
        return aggregate_data;
174
4.05M
    }
_ZN5doris22AggregateDataContainer11append_dataImEEPcRKT_
Line
Count
Source
160
518k
    AggregateDataPtr append_data(const KeyType& key) {
161
518k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
162
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
163
518k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
5.02k
            _expand();
165
5.02k
        }
166
167
518k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
518k
        auto* aggregate_data = _current_agg_data;
169
518k
        ++_total_count;
170
518k
        ++_index_in_sub_container;
171
518k
        _current_agg_data += _size_of_aggregate_states;
172
518k
        _current_keys += _size_of_key;
173
518k
        return aggregate_data;
174
518k
    }
_ZN5doris22AggregateDataContainer11append_dataIN4wide7integerILm128EjEEEEPcRKT_
Line
Count
Source
160
26.7k
    AggregateDataPtr append_data(const KeyType& key) {
161
26.7k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
162
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
163
26.7k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
709
            _expand();
165
709
        }
166
167
26.7k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
26.7k
        auto* aggregate_data = _current_agg_data;
169
26.7k
        ++_total_count;
170
26.7k
        ++_index_in_sub_container;
171
26.7k
        _current_agg_data += _size_of_aggregate_states;
172
26.7k
        _current_keys += _size_of_key;
173
26.7k
        return aggregate_data;
174
26.7k
    }
_ZN5doris22AggregateDataContainer11append_dataIN4wide7integerILm256EjEEEEPcRKT_
Line
Count
Source
160
14.5k
    AggregateDataPtr append_data(const KeyType& key) {
161
14.5k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
162
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
163
14.5k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
674
            _expand();
165
674
        }
166
167
14.5k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
14.5k
        auto* aggregate_data = _current_agg_data;
169
14.5k
        ++_total_count;
170
14.5k
        ++_index_in_sub_container;
171
14.5k
        _current_agg_data += _size_of_aggregate_states;
172
14.5k
        _current_keys += _size_of_key;
173
14.5k
        return aggregate_data;
174
14.5k
    }
_ZN5doris22AggregateDataContainer11append_dataINS_6UInt72EEEPcRKT_
Line
Count
Source
160
1.09k
    AggregateDataPtr append_data(const KeyType& key) {
161
1.09k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
162
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
163
1.09k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
547
            _expand();
165
547
        }
166
167
1.09k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
1.09k
        auto* aggregate_data = _current_agg_data;
169
1.09k
        ++_total_count;
170
1.09k
        ++_index_in_sub_container;
171
1.09k
        _current_agg_data += _size_of_aggregate_states;
172
1.09k
        _current_keys += _size_of_key;
173
1.09k
        return aggregate_data;
174
1.09k
    }
_ZN5doris22AggregateDataContainer11append_dataINS_6UInt96EEEPcRKT_
Line
Count
Source
160
8.91k
    AggregateDataPtr append_data(const KeyType& key) {
161
8.91k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
162
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
163
8.91k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
615
            _expand();
165
615
        }
166
167
8.91k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
8.91k
        auto* aggregate_data = _current_agg_data;
169
8.91k
        ++_total_count;
170
8.91k
        ++_index_in_sub_container;
171
8.91k
        _current_agg_data += _size_of_aggregate_states;
172
8.91k
        _current_keys += _size_of_key;
173
8.91k
        return aggregate_data;
174
8.91k
    }
_ZN5doris22AggregateDataContainer11append_dataINS_7UInt104EEEPcRKT_
Line
Count
Source
160
1.01k
    AggregateDataPtr append_data(const KeyType& key) {
161
1.01k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
162
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
163
1.01k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
530
            _expand();
165
530
        }
166
167
1.01k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
1.01k
        auto* aggregate_data = _current_agg_data;
169
1.01k
        ++_total_count;
170
1.01k
        ++_index_in_sub_container;
171
1.01k
        _current_agg_data += _size_of_aggregate_states;
172
1.01k
        _current_keys += _size_of_key;
173
1.01k
        return aggregate_data;
174
1.01k
    }
_ZN5doris22AggregateDataContainer11append_dataINS_7UInt136EEEPcRKT_
Line
Count
Source
160
2.09k
    AggregateDataPtr append_data(const KeyType& key) {
161
2.09k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
162
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
163
2.09k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
1.03k
            _expand();
165
1.03k
        }
166
167
2.09k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
2.09k
        auto* aggregate_data = _current_agg_data;
169
2.09k
        ++_total_count;
170
2.09k
        ++_index_in_sub_container;
171
2.09k
        _current_agg_data += _size_of_aggregate_states;
172
2.09k
        _current_keys += _size_of_key;
173
2.09k
        return aggregate_data;
174
2.09k
    }
175
176
    template <typename Derived, bool IsConst>
177
    class IteratorBase {
178
        using Container =
179
                std::conditional_t<IsConst, const AggregateDataContainer, AggregateDataContainer>;
180
181
        Container* container = nullptr;
182
        uint32_t index;
183
        uint32_t sub_container_index;
184
        uint32_t index_in_sub_container;
185
186
        friend class HashTable;
187
188
    public:
189
69.9k
        IteratorBase() = default;
190
        IteratorBase(Container* container_, uint32_t index_)
191
4.92M
                : container(container_), index(index_) {
192
4.92M
            sub_container_index = index / SUB_CONTAINER_CAPACITY;
193
4.92M
            index_in_sub_container = index - sub_container_index * SUB_CONTAINER_CAPACITY;
194
4.92M
        }
195
196
64.6k
        bool operator==(const IteratorBase& rhs) const { return index == rhs.index; }
197
4.79M
        bool operator!=(const IteratorBase& rhs) const { return index != rhs.index; }
198
199
4.73M
        Derived& operator++() {
200
4.73M
            index++;
201
4.73M
            index_in_sub_container++;
202
4.73M
            if (index_in_sub_container == SUB_CONTAINER_CAPACITY) {
203
387
                index_in_sub_container = 0;
204
387
                sub_container_index++;
205
387
            }
206
4.73M
            return static_cast<Derived&>(*this);
207
4.73M
        }
208
209
        template <typename KeyType>
210
4.73M
        KeyType get_key() {
211
4.73M
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
4.73M
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
4.73M
                    [index_in_sub_container];
214
4.73M
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_9StringRefEEET_v
Line
Count
Source
210
91.8k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
91.8k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
91.8k
                    [index_in_sub_container];
214
91.8k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIhEET_v
Line
Count
Source
210
3.91k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
3.91k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
3.91k
                    [index_in_sub_container];
214
3.91k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyItEET_v
Line
Count
Source
210
2.65k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
2.65k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
2.65k
                    [index_in_sub_container];
214
2.65k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIjEET_v
Line
Count
Source
210
4.04M
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
4.04M
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
4.04M
                    [index_in_sub_container];
214
4.04M
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyImEET_v
Line
Count
Source
210
526k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
526k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
526k
                    [index_in_sub_container];
214
526k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIN4wide7integerILm128EjEEEET_v
Line
Count
Source
210
26.8k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
26.8k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
26.8k
                    [index_in_sub_container];
214
26.8k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIN4wide7integerILm256EjEEEET_v
Line
Count
Source
210
23.8k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
23.8k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
23.8k
                    [index_in_sub_container];
214
23.8k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_6UInt72EEET_v
Line
Count
Source
210
1.09k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
1.09k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
1.09k
                    [index_in_sub_container];
214
1.09k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_6UInt96EEET_v
Line
Count
Source
210
8.88k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
8.88k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
8.88k
                    [index_in_sub_container];
214
8.88k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_7UInt104EEET_v
Line
Count
Source
210
1.01k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
1.01k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
1.01k
                    [index_in_sub_container];
214
1.01k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_7UInt136EEET_v
Line
Count
Source
210
2.09k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
2.09k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
2.09k
                    [index_in_sub_container];
214
2.09k
        }
215
216
4.69M
        AggregateDataPtr get_aggregate_data() {
217
4.69M
            return &(container->_value_containers[sub_container_index]
218
4.69M
                                                 [container->_size_of_aggregate_states *
219
4.69M
                                                  index_in_sub_container]);
220
4.69M
        }
221
    };
222
223
    class Iterator : public IteratorBase<Iterator, false> {
224
    public:
225
        using IteratorBase<Iterator, false>::IteratorBase;
226
    };
227
228
    class ConstIterator : public IteratorBase<ConstIterator, true> {
229
    public:
230
        using IteratorBase<ConstIterator, true>::IteratorBase;
231
    };
232
233
    ConstIterator begin() const { return {this, 0}; }
234
235
    ConstIterator cbegin() const { return begin(); }
236
237
66.2k
    Iterator begin() { return {this, 0}; }
238
239
    ConstIterator end() const { return {this, _total_count}; }
240
    ConstIterator cend() const { return end(); }
241
4.86M
    Iterator end() { return {this, _total_count}; }
242
243
2.14k
    [[nodiscard]] uint32_t total_count() const { return _total_count; }
244
245
4.06k
    size_t estimate_memory(size_t rows) const {
246
4.06k
        bool need_to_expand = false;
247
4.06k
        if (_total_count == 0) {
248
1
            need_to_expand = true;
249
4.05k
        } else if ((_index_in_sub_container + rows) > SUB_CONTAINER_CAPACITY) {
250
2
            need_to_expand = true;
251
2
            rows -= (SUB_CONTAINER_CAPACITY - _index_in_sub_container);
252
2
        }
253
254
4.06k
        if (!need_to_expand) {
255
4.05k
            return 0;
256
4.05k
        }
257
258
3
        size_t count = (rows + SUB_CONTAINER_CAPACITY - 1) / SUB_CONTAINER_CAPACITY;
259
3
        size_t size = _size_of_key * SUB_CONTAINER_CAPACITY;
260
3
        size += _size_of_aggregate_states * SUB_CONTAINER_CAPACITY;
261
3
        size *= count;
262
3
        return size;
263
4.06k
    }
264
265
72.1k
    void init_once() {
266
72.1k
        if (_inited) {
267
6.12k
            return;
268
6.12k
        }
269
66.0k
        _inited = true;
270
66.0k
        iterator = begin();
271
66.0k
    }
272
    Iterator iterator;
273
274
private:
275
25.9k
    void _expand() {
276
25.9k
        _index_in_sub_container = 0;
277
25.9k
        _current_keys = nullptr;
278
25.9k
        _current_agg_data = nullptr;
279
25.9k
        try {
280
25.9k
            _current_keys = _arena_pool.alloc(_size_of_key * SUB_CONTAINER_CAPACITY);
281
25.9k
            _key_containers.emplace_back(_current_keys);
282
283
25.9k
            _current_agg_data = (AggregateDataPtr)_arena_pool.alloc(_size_of_aggregate_states *
284
25.9k
                                                                    SUB_CONTAINER_CAPACITY);
285
25.9k
            _value_containers.emplace_back(_current_agg_data);
286
25.9k
        } catch (...) {
287
0
            if (_current_keys) {
288
0
                _key_containers.pop_back();
289
0
                _current_keys = nullptr;
290
0
            }
291
0
            if (_current_agg_data) {
292
0
                _value_containers.pop_back();
293
0
                _current_agg_data = nullptr;
294
0
            }
295
0
            throw;
296
0
        }
297
25.9k
    }
298
299
    static constexpr uint32_t SUB_CONTAINER_CAPACITY = 8192;
300
    Arena _arena_pool;
301
    std::vector<char*> _key_containers;
302
    std::vector<AggregateDataPtr> _value_containers;
303
    AggregateDataPtr _current_agg_data = nullptr;
304
    char* _current_keys = nullptr;
305
    size_t _size_of_key {};
306
    size_t _size_of_aggregate_states {};
307
    uint32_t _index_in_sub_container {};
308
    uint32_t _total_count {};
309
    bool _inited = false;
310
};
311
} // namespace doris