Coverage Report

Created: 2026-03-13 12:15

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
3.84k
    void init(const std::vector<DataTypePtr>& data_types, HashKeyType type) {
81
3.84k
        bool nullable = data_types.size() == 1 && data_types[0]->is_nullable();
82
83
3.84k
        switch (type) {
84
1
        case HashKeyType::without_key:
85
1
            break;
86
1.32k
        case HashKeyType::serialized:
87
1.32k
            method_variant.emplace<MethodSerialized<AggregatedDataWithStringKey>>();
88
1.32k
            break;
89
65
        case HashKeyType::int8_key:
90
65
            emplace_single<UInt8, AggData<UInt8>>(nullable);
91
65
            break;
92
47
        case HashKeyType::int16_key:
93
47
            emplace_single<UInt16, AggData<UInt16>>(nullable);
94
47
            break;
95
197
        case HashKeyType::int32_key:
96
197
            emplace_single<UInt32, AggData<UInt32>>(nullable);
97
197
            break;
98
764
        case HashKeyType::int32_key_phase2:
99
764
            emplace_single<UInt32, AggregatedDataWithUInt32KeyPhase2>(nullable);
100
764
            break;
101
199
        case HashKeyType::int64_key:
102
199
            emplace_single<UInt64, AggData<UInt64>>(nullable);
103
199
            break;
104
557
        case HashKeyType::int64_key_phase2:
105
557
            emplace_single<UInt64, AggregatedDataWithUInt64KeyPhase2>(nullable);
106
557
            break;
107
132
        case HashKeyType::int128_key:
108
132
            emplace_single<UInt128, AggData<UInt128>>(nullable);
109
132
            break;
110
1
        case HashKeyType::int256_key:
111
1
            emplace_single<UInt256, AggData<UInt256>>(nullable);
112
1
            break;
113
376
        case HashKeyType::string_key:
114
376
            if (nullable) {
115
169
                method_variant.emplace<MethodSingleNullableColumn<
116
169
                        MethodStringNoCache<AggregatedDataWithNullableShortStringKey>>>();
117
207
            } else {
118
207
                method_variant.emplace<MethodStringNoCache<AggregatedDataWithShortStringKey>>();
119
207
            }
120
376
            break;
121
111
        case HashKeyType::fixed64:
122
111
            method_variant.emplace<MethodKeysFixed<AggData<UInt64>>>(get_key_sizes(data_types));
123
111
            break;
124
0
        case HashKeyType::fixed72:
125
0
            method_variant.emplace<MethodKeysFixed<AggData<UInt72>>>(get_key_sizes(data_types));
126
0
            break;
127
7
        case HashKeyType::fixed96:
128
7
            method_variant.emplace<MethodKeysFixed<AggData<UInt96>>>(get_key_sizes(data_types));
129
7
            break;
130
8
        case HashKeyType::fixed104:
131
8
            method_variant.emplace<MethodKeysFixed<AggData<UInt104>>>(get_key_sizes(data_types));
132
8
            break;
133
18
        case HashKeyType::fixed128:
134
18
            method_variant.emplace<MethodKeysFixed<AggData<UInt128>>>(get_key_sizes(data_types));
135
18
            break;
136
21
        case HashKeyType::fixed136:
137
21
            method_variant.emplace<MethodKeysFixed<AggData<UInt136>>>(get_key_sizes(data_types));
138
21
            break;
139
10
        case HashKeyType::fixed256:
140
10
            method_variant.emplace<MethodKeysFixed<AggData<UInt256>>>(get_key_sizes(data_types));
141
10
            break;
142
1
        default:
143
1
            throw Exception(ErrorCode::INTERNAL_ERROR,
144
1
                            "AggregatedDataVariants meet invalid key type, type={}", type);
145
3.84k
        }
146
3.84k
    }
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
6.76k
            : _size_of_key(size_of_key), _size_of_aggregate_states(size_of_aggregate_states) {}
156
157
25.6k
    int64_t memory_usage() const { return _arena_pool.size(); }
158
159
    template <typename KeyType>
160
1.96M
    AggregateDataPtr append_data(const KeyType& key) {
161
1.96M
        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.96M
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
4.78k
            _expand();
165
4.78k
        }
166
167
1.96M
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
1.96M
        auto* aggregate_data = _current_agg_data;
169
1.96M
        ++_total_count;
170
1.96M
        ++_index_in_sub_container;
171
1.96M
        _current_agg_data += _size_of_aggregate_states;
172
1.96M
        _current_keys += _size_of_key;
173
1.96M
        return aggregate_data;
174
1.96M
    }
_ZN5doris22AggregateDataContainer11append_dataINS_9StringRefEEEPcRKT_
Line
Count
Source
160
43.2k
    AggregateDataPtr append_data(const KeyType& key) {
161
43.2k
        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
43.2k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
592
            _expand();
165
592
        }
166
167
43.2k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
43.2k
        auto* aggregate_data = _current_agg_data;
169
43.2k
        ++_total_count;
170
43.2k
        ++_index_in_sub_container;
171
43.2k
        _current_agg_data += _size_of_aggregate_states;
172
43.2k
        _current_keys += _size_of_key;
173
43.2k
        return aggregate_data;
174
43.2k
    }
_ZN5doris22AggregateDataContainer11append_dataIhEEPcRKT_
Line
Count
Source
160
1.10k
    AggregateDataPtr append_data(const KeyType& key) {
161
1.10k
        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.10k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
240
            _expand();
165
240
        }
166
167
1.10k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
1.10k
        auto* aggregate_data = _current_agg_data;
169
1.10k
        ++_total_count;
170
1.10k
        ++_index_in_sub_container;
171
1.10k
        _current_agg_data += _size_of_aggregate_states;
172
1.10k
        _current_keys += _size_of_key;
173
1.10k
        return aggregate_data;
174
1.10k
    }
_ZN5doris22AggregateDataContainer11append_dataItEEPcRKT_
Line
Count
Source
160
1.22k
    AggregateDataPtr append_data(const KeyType& key) {
161
1.22k
        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.22k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
30
            _expand();
165
30
        }
166
167
1.22k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
1.22k
        auto* aggregate_data = _current_agg_data;
169
1.22k
        ++_total_count;
170
1.22k
        ++_index_in_sub_container;
171
1.22k
        _current_agg_data += _size_of_aggregate_states;
172
1.22k
        _current_keys += _size_of_key;
173
1.22k
        return aggregate_data;
174
1.22k
    }
_ZN5doris22AggregateDataContainer11append_dataIjEEPcRKT_
Line
Count
Source
160
1.57M
    AggregateDataPtr append_data(const KeyType& key) {
161
1.57M
        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.57M
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
1.38k
            _expand();
165
1.38k
        }
166
167
1.57M
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
1.57M
        auto* aggregate_data = _current_agg_data;
169
1.57M
        ++_total_count;
170
1.57M
        ++_index_in_sub_container;
171
1.57M
        _current_agg_data += _size_of_aggregate_states;
172
1.57M
        _current_keys += _size_of_key;
173
1.57M
        return aggregate_data;
174
1.57M
    }
_ZN5doris22AggregateDataContainer11append_dataImEEPcRKT_
Line
Count
Source
160
317k
    AggregateDataPtr append_data(const KeyType& key) {
161
317k
        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
317k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
2.44k
            _expand();
165
2.44k
        }
166
167
317k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
317k
        auto* aggregate_data = _current_agg_data;
169
317k
        ++_total_count;
170
317k
        ++_index_in_sub_container;
171
317k
        _current_agg_data += _size_of_aggregate_states;
172
317k
        _current_keys += _size_of_key;
173
317k
        return aggregate_data;
174
317k
    }
_ZN5doris22AggregateDataContainer11append_dataIN4wide7integerILm128EjEEEEPcRKT_
Line
Count
Source
160
24.0k
    AggregateDataPtr append_data(const KeyType& key) {
161
24.0k
        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
24.0k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
74
            _expand();
165
74
        }
166
167
24.0k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
24.0k
        auto* aggregate_data = _current_agg_data;
169
24.0k
        ++_total_count;
170
24.0k
        ++_index_in_sub_container;
171
24.0k
        _current_agg_data += _size_of_aggregate_states;
172
24.0k
        _current_keys += _size_of_key;
173
24.0k
        return aggregate_data;
174
24.0k
    }
_ZN5doris22AggregateDataContainer11append_dataIN4wide7integerILm256EjEEEEPcRKT_
Line
Count
Source
160
94
    AggregateDataPtr append_data(const KeyType& key) {
161
94
        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
94
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
4
            _expand();
165
4
        }
166
167
94
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
94
        auto* aggregate_data = _current_agg_data;
169
94
        ++_total_count;
170
94
        ++_index_in_sub_container;
171
94
        _current_agg_data += _size_of_aggregate_states;
172
94
        _current_keys += _size_of_key;
173
94
        return aggregate_data;
174
94
    }
Unexecuted instantiation: _ZN5doris22AggregateDataContainer11append_dataINS_6UInt72EEEPcRKT_
_ZN5doris22AggregateDataContainer11append_dataINS_6UInt96EEEPcRKT_
Line
Count
Source
160
1.21k
    AggregateDataPtr append_data(const KeyType& key) {
161
1.21k
        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.21k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
7
            _expand();
165
7
        }
166
167
1.21k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
1.21k
        auto* aggregate_data = _current_agg_data;
169
1.21k
        ++_total_count;
170
1.21k
        ++_index_in_sub_container;
171
1.21k
        _current_agg_data += _size_of_aggregate_states;
172
1.21k
        _current_keys += _size_of_key;
173
1.21k
        return aggregate_data;
174
1.21k
    }
_ZN5doris22AggregateDataContainer11append_dataINS_7UInt104EEEPcRKT_
Line
Count
Source
160
6
    AggregateDataPtr append_data(const KeyType& key) {
161
6
        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
6
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
5
            _expand();
165
5
        }
166
167
6
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
6
        auto* aggregate_data = _current_agg_data;
169
6
        ++_total_count;
170
6
        ++_index_in_sub_container;
171
6
        _current_agg_data += _size_of_aggregate_states;
172
6
        _current_keys += _size_of_key;
173
6
        return aggregate_data;
174
6
    }
_ZN5doris22AggregateDataContainer11append_dataINS_7UInt136EEEPcRKT_
Line
Count
Source
160
6
    AggregateDataPtr append_data(const KeyType& key) {
161
6
        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
6
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
6
            _expand();
165
6
        }
166
167
6
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
6
        auto* aggregate_data = _current_agg_data;
169
6
        ++_total_count;
170
6
        ++_index_in_sub_container;
171
6
        _current_agg_data += _size_of_aggregate_states;
172
6
        _current_keys += _size_of_key;
173
6
        return aggregate_data;
174
6
    }
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
6.76k
        IteratorBase() = default;
190
        IteratorBase(Container* container_, uint32_t index_)
191
1.95M
                : container(container_), index(index_) {
192
1.95M
            sub_container_index = index / SUB_CONTAINER_CAPACITY;
193
1.95M
            index_in_sub_container = index - sub_container_index * SUB_CONTAINER_CAPACITY;
194
1.95M
        }
195
196
5.74k
        bool operator==(const IteratorBase& rhs) const { return index == rhs.index; }
197
1.93M
        bool operator!=(const IteratorBase& rhs) const { return index != rhs.index; }
198
199
1.93M
        Derived& operator++() {
200
1.93M
            index++;
201
1.93M
            index_in_sub_container++;
202
1.93M
            if (index_in_sub_container == SUB_CONTAINER_CAPACITY) {
203
183
                index_in_sub_container = 0;
204
183
                sub_container_index++;
205
183
            }
206
1.93M
            return static_cast<Derived&>(*this);
207
1.93M
        }
208
209
        template <typename KeyType>
210
1.93M
        KeyType get_key() {
211
1.93M
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
1.93M
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
1.93M
                    [index_in_sub_container];
214
1.93M
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_9StringRefEEET_v
Line
Count
Source
210
43.2k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
43.2k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
43.2k
                    [index_in_sub_container];
214
43.2k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIhEET_v
Line
Count
Source
210
1.10k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
1.10k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
1.10k
                    [index_in_sub_container];
214
1.10k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyItEET_v
Line
Count
Source
210
1.22k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
1.22k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
1.22k
                    [index_in_sub_container];
214
1.22k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIjEET_v
Line
Count
Source
210
1.55M
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
1.55M
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
1.55M
                    [index_in_sub_container];
214
1.55M
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyImEET_v
Line
Count
Source
210
309k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
309k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
309k
                    [index_in_sub_container];
214
309k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIN4wide7integerILm128EjEEEET_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_keyIN4wide7integerILm256EjEEEET_v
Line
Count
Source
210
94
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
94
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
94
                    [index_in_sub_container];
214
94
        }
Unexecuted instantiation: _ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_6UInt72EEET_v
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_6UInt96EEET_v
Line
Count
Source
210
1.15k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
1.15k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
1.15k
                    [index_in_sub_container];
214
1.15k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_7UInt104EEET_v
Line
Count
Source
210
6
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
6
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
6
                    [index_in_sub_container];
214
6
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_7UInt136EEET_v
Line
Count
Source
210
6
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
6
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
6
                    [index_in_sub_container];
214
6
        }
215
216
1.93M
        AggregateDataPtr get_aggregate_data() {
217
1.93M
            return &(container->_value_containers[sub_container_index]
218
1.93M
                                                 [container->_size_of_aggregate_states *
219
1.93M
                                                  index_in_sub_container]);
220
1.93M
        }
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
6.71k
    Iterator begin() { return {this, 0}; }
238
239
    ConstIterator end() const { return {this, _total_count}; }
240
    ConstIterator cend() const { return end(); }
241
1.94M
    Iterator end() { return {this, _total_count}; }
242
243
1.02k
    [[nodiscard]] uint32_t total_count() const { return _total_count; }
244
245
1.86k
    size_t estimate_memory(size_t rows) const {
246
1.86k
        bool need_to_expand = false;
247
1.86k
        if (_total_count == 0) {
248
1
            need_to_expand = true;
249
1.85k
        } 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
1.86k
        if (!need_to_expand) {
255
1.85k
            return 0;
256
1.85k
        }
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
1.86k
    }
264
265
8.90k
    void init_once() {
266
8.90k
        if (_inited) {
267
2.21k
            return;
268
2.21k
        }
269
6.68k
        _inited = true;
270
6.68k
        iterator = begin();
271
6.68k
    }
272
    Iterator iterator;
273
274
private:
275
4.78k
    void _expand() {
276
4.78k
        _index_in_sub_container = 0;
277
4.78k
        _current_keys = nullptr;
278
4.78k
        _current_agg_data = nullptr;
279
4.78k
        try {
280
4.78k
            _current_keys = _arena_pool.alloc(_size_of_key * SUB_CONTAINER_CAPACITY);
281
4.78k
            _key_containers.emplace_back(_current_keys);
282
283
4.78k
            _current_agg_data = (AggregateDataPtr)_arena_pool.alloc(_size_of_aggregate_states *
284
4.78k
                                                                    SUB_CONTAINER_CAPACITY);
285
4.78k
            _value_containers.emplace_back(_current_agg_data);
286
4.78k
        } 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
4.78k
    }
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