Coverage Report

Created: 2026-03-30 12:42

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
65.7k
    void init(const std::vector<DataTypePtr>& data_types, HashKeyType type) {
81
65.7k
        bool nullable = data_types.size() == 1 && data_types[0]->is_nullable();
82
83
65.7k
        switch (type) {
84
1
        case HashKeyType::without_key:
85
1
            break;
86
11.8k
        case HashKeyType::serialized:
87
11.8k
            method_variant.emplace<MethodSerialized<AggregatedDataWithStringKey>>();
88
11.8k
            break;
89
4.34k
        case HashKeyType::int8_key:
90
4.34k
            emplace_single<UInt8, AggData<UInt8>>(nullable);
91
4.34k
            break;
92
1.09k
        case HashKeyType::int16_key:
93
1.09k
            emplace_single<UInt16, AggData<UInt16>>(nullable);
94
1.09k
            break;
95
5.75k
        case HashKeyType::int32_key:
96
5.75k
            emplace_single<UInt32, AggData<UInt32>>(nullable);
97
5.75k
            break;
98
13.0k
        case HashKeyType::int32_key_phase2:
99
13.0k
            emplace_single<UInt32, AggregatedDataWithUInt32KeyPhase2>(nullable);
100
13.0k
            break;
101
4.10k
        case HashKeyType::int64_key:
102
4.10k
            emplace_single<UInt64, AggData<UInt64>>(nullable);
103
4.10k
            break;
104
7.37k
        case HashKeyType::int64_key_phase2:
105
7.37k
            emplace_single<UInt64, AggregatedDataWithUInt64KeyPhase2>(nullable);
106
7.37k
            break;
107
662
        case HashKeyType::int128_key:
108
662
            emplace_single<UInt128, AggData<UInt128>>(nullable);
109
662
            break;
110
23
        case HashKeyType::int256_key:
111
23
            emplace_single<UInt256, AggData<UInt256>>(nullable);
112
23
            break;
113
6.11k
        case HashKeyType::string_key:
114
6.11k
            if (nullable) {
115
5.12k
                method_variant.emplace<MethodSingleNullableColumn<
116
5.12k
                        MethodStringNoCache<AggregatedDataWithNullableShortStringKey>>>();
117
5.12k
            } else {
118
996
                method_variant.emplace<MethodStringNoCache<AggregatedDataWithShortStringKey>>();
119
996
            }
120
6.11k
            break;
121
1.33k
        case HashKeyType::fixed64:
122
1.33k
            method_variant.emplace<MethodKeysFixed<AggData<UInt64>>>(get_key_sizes(data_types));
123
1.33k
            break;
124
1.57k
        case HashKeyType::fixed72:
125
1.57k
            method_variant.emplace<MethodKeysFixed<AggData<UInt72>>>(get_key_sizes(data_types));
126
1.57k
            break;
127
757
        case HashKeyType::fixed96:
128
757
            method_variant.emplace<MethodKeysFixed<AggData<UInt96>>>(get_key_sizes(data_types));
129
757
            break;
130
1.53k
        case HashKeyType::fixed104:
131
1.53k
            method_variant.emplace<MethodKeysFixed<AggData<UInt104>>>(get_key_sizes(data_types));
132
1.53k
            break;
133
109
        case HashKeyType::fixed128:
134
109
            method_variant.emplace<MethodKeysFixed<AggData<UInt128>>>(get_key_sizes(data_types));
135
109
            break;
136
2.82k
        case HashKeyType::fixed136:
137
2.82k
            method_variant.emplace<MethodKeysFixed<AggData<UInt136>>>(get_key_sizes(data_types));
138
2.82k
            break;
139
3.19k
        case HashKeyType::fixed256:
140
3.19k
            method_variant.emplace<MethodKeysFixed<AggData<UInt256>>>(get_key_sizes(data_types));
141
3.19k
            break;
142
1
        default:
143
1
            throw Exception(ErrorCode::INTERNAL_ERROR,
144
1
                            "AggregatedDataVariants meet invalid key type, type={}", type);
145
65.7k
        }
146
65.7k
    }
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
63.1k
            : _size_of_key(size_of_key), _size_of_aggregate_states(size_of_aggregate_states) {}
156
157
57.6k
    int64_t memory_usage() const { return _arena_pool.size(); }
158
159
    template <typename KeyType>
160
4.51M
    AggregateDataPtr append_data(const KeyType& key) {
161
4.51M
        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.51M
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
26.4k
            _expand();
165
26.4k
        }
166
167
4.51M
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
4.51M
        auto* aggregate_data = _current_agg_data;
169
4.51M
        ++_total_count;
170
4.51M
        ++_index_in_sub_container;
171
4.51M
        _current_agg_data += _size_of_aggregate_states;
172
4.51M
        _current_keys += _size_of_key;
173
4.51M
        return aggregate_data;
174
4.51M
    }
_ZN5doris22AggregateDataContainer11append_dataINS_9StringRefEEEPcRKT_
Line
Count
Source
160
252k
    AggregateDataPtr append_data(const KeyType& key) {
161
252k
        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
252k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
5.64k
            _expand();
165
5.64k
        }
166
167
252k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
252k
        auto* aggregate_data = _current_agg_data;
169
252k
        ++_total_count;
170
252k
        ++_index_in_sub_container;
171
252k
        _current_agg_data += _size_of_aggregate_states;
172
252k
        _current_keys += _size_of_key;
173
252k
        return aggregate_data;
174
252k
    }
_ZN5doris22AggregateDataContainer11append_dataIhEEPcRKT_
Line
Count
Source
160
4.14k
    AggregateDataPtr append_data(const KeyType& key) {
161
4.14k
        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.14k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
2.33k
            _expand();
165
2.33k
        }
166
167
4.14k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
4.14k
        auto* aggregate_data = _current_agg_data;
169
4.14k
        ++_total_count;
170
4.14k
        ++_index_in_sub_container;
171
4.14k
        _current_agg_data += _size_of_aggregate_states;
172
4.14k
        _current_keys += _size_of_key;
173
4.14k
        return aggregate_data;
174
4.14k
    }
_ZN5doris22AggregateDataContainer11append_dataItEEPcRKT_
Line
Count
Source
160
2.08k
    AggregateDataPtr append_data(const KeyType& key) {
161
2.08k
        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.08k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
545
            _expand();
165
545
        }
166
167
2.08k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
2.08k
        auto* aggregate_data = _current_agg_data;
169
2.08k
        ++_total_count;
170
2.08k
        ++_index_in_sub_container;
171
2.08k
        _current_agg_data += _size_of_aggregate_states;
172
2.08k
        _current_keys += _size_of_key;
173
2.08k
        return aggregate_data;
174
2.08k
    }
_ZN5doris22AggregateDataContainer11append_dataIjEEPcRKT_
Line
Count
Source
160
2.48M
    AggregateDataPtr append_data(const KeyType& key) {
161
2.48M
        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.48M
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
9.28k
            _expand();
165
9.28k
        }
166
167
2.48M
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
2.48M
        auto* aggregate_data = _current_agg_data;
169
2.48M
        ++_total_count;
170
2.48M
        ++_index_in_sub_container;
171
2.48M
        _current_agg_data += _size_of_aggregate_states;
172
2.48M
        _current_keys += _size_of_key;
173
2.48M
        return aggregate_data;
174
2.48M
    }
_ZN5doris22AggregateDataContainer11append_dataImEEPcRKT_
Line
Count
Source
160
1.74M
    AggregateDataPtr append_data(const KeyType& key) {
161
1.74M
        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.74M
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
5.10k
            _expand();
165
5.10k
        }
166
167
1.74M
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
1.74M
        auto* aggregate_data = _current_agg_data;
169
1.74M
        ++_total_count;
170
1.74M
        ++_index_in_sub_container;
171
1.74M
        _current_agg_data += _size_of_aggregate_states;
172
1.74M
        _current_keys += _size_of_key;
173
1.74M
        return aggregate_data;
174
1.74M
    }
_ZN5doris22AggregateDataContainer11append_dataIN4wide7integerILm128EjEEEEPcRKT_
Line
Count
Source
160
17.0k
    AggregateDataPtr append_data(const KeyType& key) {
161
17.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
17.0k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
366
            _expand();
165
366
        }
166
167
17.0k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
17.0k
        auto* aggregate_data = _current_agg_data;
169
17.0k
        ++_total_count;
170
17.0k
        ++_index_in_sub_container;
171
17.0k
        _current_agg_data += _size_of_aggregate_states;
172
17.0k
        _current_keys += _size_of_key;
173
17.0k
        return aggregate_data;
174
17.0k
    }
_ZN5doris22AggregateDataContainer11append_dataIN4wide7integerILm256EjEEEEPcRKT_
Line
Count
Source
160
6.93k
    AggregateDataPtr append_data(const KeyType& key) {
161
6.93k
        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.93k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
629
            _expand();
165
629
        }
166
167
6.93k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
6.93k
        auto* aggregate_data = _current_agg_data;
169
6.93k
        ++_total_count;
170
6.93k
        ++_index_in_sub_container;
171
6.93k
        _current_agg_data += _size_of_aggregate_states;
172
6.93k
        _current_keys += _size_of_key;
173
6.93k
        return aggregate_data;
174
6.93k
    }
_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
501
            _expand();
165
501
        }
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.95k
    AggregateDataPtr append_data(const KeyType& key) {
161
8.95k
        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.95k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
600
            _expand();
165
600
        }
166
167
8.95k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
8.95k
        auto* aggregate_data = _current_agg_data;
169
8.95k
        ++_total_count;
170
8.95k
        ++_index_in_sub_container;
171
8.95k
        _current_agg_data += _size_of_aggregate_states;
172
8.95k
        _current_keys += _size_of_key;
173
8.95k
        return aggregate_data;
174
8.95k
    }
_ZN5doris22AggregateDataContainer11append_dataINS_7UInt104EEEPcRKT_
Line
Count
Source
160
872
    AggregateDataPtr append_data(const KeyType& key) {
161
872
        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
872
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
423
            _expand();
165
423
        }
166
167
872
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
872
        auto* aggregate_data = _current_agg_data;
169
872
        ++_total_count;
170
872
        ++_index_in_sub_container;
171
872
        _current_agg_data += _size_of_aggregate_states;
172
872
        _current_keys += _size_of_key;
173
872
        return aggregate_data;
174
872
    }
_ZN5doris22AggregateDataContainer11append_dataINS_7UInt136EEEPcRKT_
Line
Count
Source
160
1.94k
    AggregateDataPtr append_data(const KeyType& key) {
161
1.94k
        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.94k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
164
974
            _expand();
165
974
        }
166
167
1.94k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
168
1.94k
        auto* aggregate_data = _current_agg_data;
169
1.94k
        ++_total_count;
170
1.94k
        ++_index_in_sub_container;
171
1.94k
        _current_agg_data += _size_of_aggregate_states;
172
1.94k
        _current_keys += _size_of_key;
173
1.94k
        return aggregate_data;
174
1.94k
    }
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
63.1k
        IteratorBase() = default;
190
        IteratorBase(Container* container_, uint32_t index_)
191
4.66M
                : container(container_), index(index_) {
192
4.66M
            sub_container_index = index / SUB_CONTAINER_CAPACITY;
193
4.66M
            index_in_sub_container = index - sub_container_index * SUB_CONTAINER_CAPACITY;
194
4.66M
        }
195
196
58.4k
        bool operator==(const IteratorBase& rhs) const { return index == rhs.index; }
197
4.55M
        bool operator!=(const IteratorBase& rhs) const { return index != rhs.index; }
198
199
4.49M
        Derived& operator++() {
200
4.49M
            index++;
201
4.49M
            index_in_sub_container++;
202
4.49M
            if (index_in_sub_container == SUB_CONTAINER_CAPACITY) {
203
358
                index_in_sub_container = 0;
204
358
                sub_container_index++;
205
358
            }
206
4.49M
            return static_cast<Derived&>(*this);
207
4.49M
        }
208
209
        template <typename KeyType>
210
4.49M
        KeyType get_key() {
211
4.49M
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
4.49M
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
4.49M
                    [index_in_sub_container];
214
4.49M
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_9StringRefEEET_v
Line
Count
Source
210
348k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
348k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
348k
                    [index_in_sub_container];
214
348k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIhEET_v
Line
Count
Source
210
4.14k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
4.14k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
4.14k
                    [index_in_sub_container];
214
4.14k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyItEET_v
Line
Count
Source
210
2.08k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
2.08k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
2.08k
                    [index_in_sub_container];
214
2.08k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIjEET_v
Line
Count
Source
210
2.36M
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
2.36M
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
2.36M
                    [index_in_sub_container];
214
2.36M
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyImEET_v
Line
Count
Source
210
1.74M
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
1.74M
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
1.74M
                    [index_in_sub_container];
214
1.74M
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIN4wide7integerILm128EjEEEET_v
Line
Count
Source
210
16.8k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
16.8k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
16.8k
                    [index_in_sub_container];
214
16.8k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIN4wide7integerILm256EjEEEET_v
Line
Count
Source
210
11.4k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
11.4k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
11.4k
                    [index_in_sub_container];
214
11.4k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_6UInt72EEET_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_keyINS_6UInt96EEET_v
Line
Count
Source
210
8.97k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
8.97k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
8.97k
                    [index_in_sub_container];
214
8.97k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_7UInt104EEET_v
Line
Count
Source
210
872
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
872
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
872
                    [index_in_sub_container];
214
872
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_7UInt136EEET_v
Line
Count
Source
210
1.94k
        KeyType get_key() {
211
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
212
1.94k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
213
1.94k
                    [index_in_sub_container];
214
1.94k
        }
215
216
4.30M
        AggregateDataPtr get_aggregate_data() {
217
4.30M
            return &(container->_value_containers[sub_container_index]
218
4.30M
                                                 [container->_size_of_aggregate_states *
219
4.30M
                                                  index_in_sub_container]);
220
4.30M
        }
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
57.6k
    Iterator begin() { return {this, 0}; }
238
239
    ConstIterator end() const { return {this, _total_count}; }
240
    ConstIterator cend() const { return end(); }
241
4.60M
    Iterator end() { return {this, _total_count}; }
242
243
10
    [[nodiscard]] uint32_t total_count() const { return _total_count; }
244
245
19
    size_t estimate_memory(size_t rows) const {
246
19
        bool need_to_expand = false;
247
19
        if (_total_count == 0) {
248
1
            need_to_expand = true;
249
18
        } 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
19
        if (!need_to_expand) {
255
16
            return 0;
256
16
        }
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
19
    }
264
265
60.7k
    void init_once() {
266
60.7k
        if (_inited) {
267
3.39k
            return;
268
3.39k
        }
269
57.3k
        _inited = true;
270
57.3k
        iterator = begin();
271
57.3k
    }
272
    Iterator iterator;
273
274
private:
275
26.4k
    void _expand() {
276
26.4k
        _index_in_sub_container = 0;
277
26.4k
        _current_keys = nullptr;
278
26.4k
        _current_agg_data = nullptr;
279
26.4k
        try {
280
26.4k
            _current_keys = _arena_pool.alloc(_size_of_key * SUB_CONTAINER_CAPACITY);
281
26.4k
            _key_containers.emplace_back(_current_keys);
282
283
26.4k
            _current_agg_data = (AggregateDataPtr)_arena_pool.alloc(_size_of_aggregate_states *
284
26.4k
                                                                    SUB_CONTAINER_CAPACITY);
285
26.4k
            _value_containers.emplace_back(_current_agg_data);
286
26.4k
        } 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
26.4k
    }
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