Coverage Report

Created: 2026-07-09 03:26

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
using AggregatedDataWithNullableStringKey = DataWithNullKey<AggregatedDataWithStringKey>;
51
52
/// Parameterized method variant for aggregation hash tables.
53
/// StringData / NullableStringData control which hash map is used for string keys:
54
///   - AggregatedDataVariants uses StringHashMap (AggregatedDataWithShortStringKey)
55
///   - BucketedAggDataVariants uses PHHashMap<StringRef> (AggregatedDataWithStringKey)
56
///     to avoid StringHashMap's sub-table complexity and unify the emplace interface.
57
template <typename StringData, typename NullableStringData>
58
using AggMethodVariantsBase = std::variant<
59
        std::monostate, MethodSerialized<AggregatedDataWithStringKey>,
60
        MethodOneNumber<UInt8, AggData<UInt8>>, MethodOneNumber<UInt16, AggData<UInt16>>,
61
        MethodOneNumber<UInt32, AggData<UInt32>>, MethodOneNumber<UInt64, AggData<UInt64>>,
62
        MethodStringNoCache<StringData>, MethodOneNumber<UInt128, AggData<UInt128>>,
63
        MethodOneNumber<UInt256, AggData<UInt256>>,
64
        MethodOneNumber<UInt32, AggregatedDataWithUInt32KeyPhase2>,
65
        MethodOneNumber<UInt64, AggregatedDataWithUInt64KeyPhase2>,
66
        MethodSingleNullableColumn<MethodOneNumber<UInt8, AggDataNullable<UInt8>>>,
67
        MethodSingleNullableColumn<MethodOneNumber<UInt16, AggDataNullable<UInt16>>>,
68
        MethodSingleNullableColumn<MethodOneNumber<UInt32, AggDataNullable<UInt32>>>,
69
        MethodSingleNullableColumn<MethodOneNumber<UInt64, AggDataNullable<UInt64>>>,
70
        MethodSingleNullableColumn<
71
                MethodOneNumber<UInt32, AggregatedDataWithNullableUInt32KeyPhase2>>,
72
        MethodSingleNullableColumn<
73
                MethodOneNumber<UInt64, AggregatedDataWithNullableUInt64KeyPhase2>>,
74
        MethodSingleNullableColumn<MethodOneNumber<UInt128, AggDataNullable<UInt128>>>,
75
        MethodSingleNullableColumn<MethodOneNumber<UInt256, AggDataNullable<UInt256>>>,
76
        MethodSingleNullableColumn<MethodStringNoCache<NullableStringData>>,
77
        MethodKeysFixed<AggData<UInt64>>, MethodKeysFixed<AggData<UInt72>>,
78
        MethodKeysFixed<AggData<UInt96>>, MethodKeysFixed<AggData<UInt104>>,
79
        MethodKeysFixed<AggData<UInt128>>, MethodKeysFixed<AggData<UInt136>>,
80
        MethodKeysFixed<AggData<UInt256>>>;
81
82
using AggregatedMethodVariants = AggMethodVariantsBase<AggregatedDataWithShortStringKey,
83
                                                       AggregatedDataWithNullableShortStringKey>;
84
85
/// Bucketed agg uses PHHashMap<StringRef> for string keys instead of StringHashMap.
86
/// This avoids StringHashMap's sub-table complexity and unifies the emplace interface
87
/// (3-arg PHHashMap::emplace), while still using HashMethodString for correct
88
/// single-column string key extraction.
89
using BucketedAggMethodVariants =
90
        AggMethodVariantsBase<AggregatedDataWithStringKey, AggregatedDataWithNullableStringKey>;
91
92
/// Intermediate base that adds the shared init logic for aggregation data
93
/// variants.  Only the string_key case differs between AggregatedDataVariants
94
/// and BucketedAggDataVariants; all other key types are identical.  The
95
/// StringData/NullableStringData template parameters control which hash map
96
/// type is emplaced for string_key.
97
template <typename MethodVariants, typename StringData, typename NullableStringData>
98
struct AggDataVariantsBase : public DataVariants<MethodVariants, MethodSingleNullableColumn,
99
                                                 MethodOneNumber, DataWithNullKey> {
100
145k
    void init_agg_data(const std::vector<DataTypePtr>& data_types, HashKeyType type) {
101
145k
        bool nullable = data_types.size() == 1 && data_types[0]->is_nullable();
102
103
145k
        switch (type) {
104
1
        case HashKeyType::without_key:
105
1
            break;
106
26.4k
        case HashKeyType::serialized:
107
26.4k
            this->method_variant.template emplace<MethodSerialized<AggregatedDataWithStringKey>>();
108
26.4k
            break;
109
5.49k
        case HashKeyType::int8_key:
110
5.49k
            this->template emplace_single<UInt8, AggData<UInt8>>(nullable);
111
5.49k
            break;
112
8.37k
        case HashKeyType::int16_key:
113
8.37k
            this->template emplace_single<UInt16, AggData<UInt16>>(nullable);
114
8.37k
            break;
115
14.4k
        case HashKeyType::int32_key:
116
14.4k
            this->template emplace_single<UInt32, AggData<UInt32>>(nullable);
117
14.4k
            break;
118
12.9k
        case HashKeyType::int32_key_phase2:
119
12.9k
            this->template emplace_single<UInt32, AggregatedDataWithUInt32KeyPhase2>(nullable);
120
12.9k
            break;
121
32.0k
        case HashKeyType::int64_key:
122
32.0k
            this->template emplace_single<UInt64, AggData<UInt64>>(nullable);
123
32.0k
            break;
124
3.38k
        case HashKeyType::int64_key_phase2:
125
3.38k
            this->template emplace_single<UInt64, AggregatedDataWithUInt64KeyPhase2>(nullable);
126
3.38k
            break;
127
618
        case HashKeyType::int128_key:
128
618
            this->template emplace_single<UInt128, AggData<UInt128>>(nullable);
129
618
            break;
130
5
        case HashKeyType::int256_key:
131
5
            this->template emplace_single<UInt256, AggData<UInt256>>(nullable);
132
5
            break;
133
13.7k
        case HashKeyType::string_key:
134
13.7k
            if (nullable) {
135
6.46k
                this->method_variant.template emplace<
136
6.46k
                        MethodSingleNullableColumn<MethodStringNoCache<NullableStringData>>>();
137
7.23k
            } else {
138
7.23k
                this->method_variant.template emplace<MethodStringNoCache<StringData>>();
139
7.23k
            }
140
13.7k
            break;
141
1.26k
        case HashKeyType::fixed64:
142
1.26k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt64>>>(
143
1.26k
                    get_key_sizes(data_types));
144
1.26k
            break;
145
1.20k
        case HashKeyType::fixed72:
146
1.20k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt72>>>(
147
1.20k
                    get_key_sizes(data_types));
148
1.20k
            break;
149
766
        case HashKeyType::fixed96:
150
766
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt96>>>(
151
766
                    get_key_sizes(data_types));
152
766
            break;
153
1.46k
        case HashKeyType::fixed104:
154
1.46k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt104>>>(
155
1.46k
                    get_key_sizes(data_types));
156
1.46k
            break;
157
127
        case HashKeyType::fixed128:
158
127
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt128>>>(
159
127
                    get_key_sizes(data_types));
160
127
            break;
161
18.5k
        case HashKeyType::fixed136:
162
18.5k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt136>>>(
163
18.5k
                    get_key_sizes(data_types));
164
18.5k
            break;
165
4.76k
        case HashKeyType::fixed256:
166
4.76k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt256>>>(
167
4.76k
                    get_key_sizes(data_types));
168
4.76k
            break;
169
1
        default:
170
1
            throw Exception(ErrorCode::INTERNAL_ERROR, "meet invalid agg key type, type={}", type);
171
145k
        }
172
145k
    }
_ZN5doris19AggDataVariantsBaseISt7variantIJSt9monostateNS_16MethodSerializedI9PHHashMapINS_9StringRefEPc11DefaultHashIS5_vEEEENS_15MethodOneNumberIhS4_IhS6_9HashCRC32IhEEEENSB_ItS4_ItS6_SC_ItEEEENSB_IjS4_IjS6_SC_IjEEEENSB_ImS4_ImS6_SC_ImEEEENS_19MethodStringNoCacheINS_13StringHashMapIS6_NS_9AllocatorILb1ELb1ELb0ENS_22DefaultMemoryAllocatorELb1EEEEEEENSB_IN4wide7integerILm128EjEES4_ISY_S6_SC_ISY_EEEENSB_INSX_ILm256EjEES4_IS12_S6_SC_IS12_EEEENSB_IjS4_IjS6_14HashMixWrapperIjSJ_EEEENSB_ImS4_ImS6_S16_ImSM_EEEENS_26MethodSingleNullableColumnINSB_IhNS_15DataWithNullKeyISE_EEEEEENS1D_INSB_ItNS1E_ISH_EEEEEENS1D_INSB_IjNS1E_ISK_EEEEEENS1D_INSB_ImNS1E_ISN_EEEEEENS1D_INSB_IjNS1E_IS18_EEEEEENS1D_INSB_ImNS1E_IS1B_EEEEEENS1D_INSB_ISY_NS1E_IS10_EEEEEENS1D_INSB_IS12_NS1E_IS14_EEEEEENS1D_INSP_INS1E_ISU_EEEEEENS_15MethodKeysFixedISN_EENS26_IS4_INS_6UInt72ES6_SC_IS28_EEEENS26_IS4_INS_6UInt96ES6_SC_IS2C_EEEENS26_IS4_INS_7UInt104ES6_SC_IS2G_EEEENS26_IS10_EENS26_IS4_INS_7UInt136ES6_SC_IS2L_EEEENS26_IS14_EEEESU_S23_E13init_agg_dataERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS2W_EENS_11HashKeyTypeE
Line
Count
Source
100
45.6k
    void init_agg_data(const std::vector<DataTypePtr>& data_types, HashKeyType type) {
101
45.6k
        bool nullable = data_types.size() == 1 && data_types[0]->is_nullable();
102
103
45.6k
        switch (type) {
104
1
        case HashKeyType::without_key:
105
1
            break;
106
8.42k
        case HashKeyType::serialized:
107
8.42k
            this->method_variant.template emplace<MethodSerialized<AggregatedDataWithStringKey>>();
108
8.42k
            break;
109
2.27k
        case HashKeyType::int8_key:
110
2.27k
            this->template emplace_single<UInt8, AggData<UInt8>>(nullable);
111
2.27k
            break;
112
480
        case HashKeyType::int16_key:
113
480
            this->template emplace_single<UInt16, AggData<UInt16>>(nullable);
114
480
            break;
115
3.03k
        case HashKeyType::int32_key:
116
3.03k
            this->template emplace_single<UInt32, AggData<UInt32>>(nullable);
117
3.03k
            break;
118
12.9k
        case HashKeyType::int32_key_phase2:
119
12.9k
            this->template emplace_single<UInt32, AggregatedDataWithUInt32KeyPhase2>(nullable);
120
12.9k
            break;
121
1.34k
        case HashKeyType::int64_key:
122
1.34k
            this->template emplace_single<UInt64, AggData<UInt64>>(nullable);
123
1.34k
            break;
124
3.38k
        case HashKeyType::int64_key_phase2:
125
3.38k
            this->template emplace_single<UInt64, AggregatedDataWithUInt64KeyPhase2>(nullable);
126
3.38k
            break;
127
618
        case HashKeyType::int128_key:
128
618
            this->template emplace_single<UInt128, AggData<UInt128>>(nullable);
129
618
            break;
130
5
        case HashKeyType::int256_key:
131
5
            this->template emplace_single<UInt256, AggData<UInt256>>(nullable);
132
5
            break;
133
3.31k
        case HashKeyType::string_key:
134
3.31k
            if (nullable) {
135
2.28k
                this->method_variant.template emplace<
136
2.28k
                        MethodSingleNullableColumn<MethodStringNoCache<NullableStringData>>>();
137
2.28k
            } else {
138
1.02k
                this->method_variant.template emplace<MethodStringNoCache<StringData>>();
139
1.02k
            }
140
3.31k
            break;
141
1.26k
        case HashKeyType::fixed64:
142
1.26k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt64>>>(
143
1.26k
                    get_key_sizes(data_types));
144
1.26k
            break;
145
1.20k
        case HashKeyType::fixed72:
146
1.20k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt72>>>(
147
1.20k
                    get_key_sizes(data_types));
148
1.20k
            break;
149
766
        case HashKeyType::fixed96:
150
766
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt96>>>(
151
766
                    get_key_sizes(data_types));
152
766
            break;
153
1.46k
        case HashKeyType::fixed104:
154
1.46k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt104>>>(
155
1.46k
                    get_key_sizes(data_types));
156
1.46k
            break;
157
127
        case HashKeyType::fixed128:
158
127
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt128>>>(
159
127
                    get_key_sizes(data_types));
160
127
            break;
161
2.06k
        case HashKeyType::fixed136:
162
2.06k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt136>>>(
163
2.06k
                    get_key_sizes(data_types));
164
2.06k
            break;
165
2.97k
        case HashKeyType::fixed256:
166
2.97k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt256>>>(
167
2.97k
                    get_key_sizes(data_types));
168
2.97k
            break;
169
1
        default:
170
1
            throw Exception(ErrorCode::INTERNAL_ERROR, "meet invalid agg key type, type={}", type);
171
45.6k
        }
172
45.6k
    }
_ZN5doris19AggDataVariantsBaseISt7variantIJSt9monostateNS_16MethodSerializedI9PHHashMapINS_9StringRefEPc11DefaultHashIS5_vEEEENS_15MethodOneNumberIhS4_IhS6_9HashCRC32IhEEEENSB_ItS4_ItS6_SC_ItEEEENSB_IjS4_IjS6_SC_IjEEEENSB_ImS4_ImS6_SC_ImEEEENS_19MethodStringNoCacheIS9_EENSB_IN4wide7integerILm128EjEES4_IST_S6_SC_IST_EEEENSB_INSS_ILm256EjEES4_ISX_S6_SC_ISX_EEEENSB_IjS4_IjS6_14HashMixWrapperIjSJ_EEEENSB_ImS4_ImS6_S11_ImSM_EEEENS_26MethodSingleNullableColumnINSB_IhNS_15DataWithNullKeyISE_EEEEEENS18_INSB_ItNS19_ISH_EEEEEENS18_INSB_IjNS19_ISK_EEEEEENS18_INSB_ImNS19_ISN_EEEEEENS18_INSB_IjNS19_IS13_EEEEEENS18_INSB_ImNS19_IS16_EEEEEENS18_INSB_IST_NS19_ISV_EEEEEENS18_INSB_ISX_NS19_ISZ_EEEEEENS18_INSP_INS19_IS9_EEEEEENS_15MethodKeysFixedISN_EENS21_IS4_INS_6UInt72ES6_SC_IS23_EEEENS21_IS4_INS_6UInt96ES6_SC_IS27_EEEENS21_IS4_INS_7UInt104ES6_SC_IS2B_EEEENS21_ISV_EENS21_IS4_INS_7UInt136ES6_SC_IS2G_EEEENS21_ISZ_EEEES9_S1Y_E13init_agg_dataERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS2R_EENS_11HashKeyTypeE
Line
Count
Source
100
100k
    void init_agg_data(const std::vector<DataTypePtr>& data_types, HashKeyType type) {
101
100k
        bool nullable = data_types.size() == 1 && data_types[0]->is_nullable();
102
103
100k
        switch (type) {
104
0
        case HashKeyType::without_key:
105
0
            break;
106
18.0k
        case HashKeyType::serialized:
107
18.0k
            this->method_variant.template emplace<MethodSerialized<AggregatedDataWithStringKey>>();
108
18.0k
            break;
109
3.22k
        case HashKeyType::int8_key:
110
3.22k
            this->template emplace_single<UInt8, AggData<UInt8>>(nullable);
111
3.22k
            break;
112
7.89k
        case HashKeyType::int16_key:
113
7.89k
            this->template emplace_single<UInt16, AggData<UInt16>>(nullable);
114
7.89k
            break;
115
11.3k
        case HashKeyType::int32_key:
116
11.3k
            this->template emplace_single<UInt32, AggData<UInt32>>(nullable);
117
11.3k
            break;
118
0
        case HashKeyType::int32_key_phase2:
119
0
            this->template emplace_single<UInt32, AggregatedDataWithUInt32KeyPhase2>(nullable);
120
0
            break;
121
30.6k
        case HashKeyType::int64_key:
122
30.6k
            this->template emplace_single<UInt64, AggData<UInt64>>(nullable);
123
30.6k
            break;
124
0
        case HashKeyType::int64_key_phase2:
125
0
            this->template emplace_single<UInt64, AggregatedDataWithUInt64KeyPhase2>(nullable);
126
0
            break;
127
0
        case HashKeyType::int128_key:
128
0
            this->template emplace_single<UInt128, AggData<UInt128>>(nullable);
129
0
            break;
130
0
        case HashKeyType::int256_key:
131
0
            this->template emplace_single<UInt256, AggData<UInt256>>(nullable);
132
0
            break;
133
10.3k
        case HashKeyType::string_key:
134
10.3k
            if (nullable) {
135
4.17k
                this->method_variant.template emplace<
136
4.17k
                        MethodSingleNullableColumn<MethodStringNoCache<NullableStringData>>>();
137
6.21k
            } else {
138
6.21k
                this->method_variant.template emplace<MethodStringNoCache<StringData>>();
139
6.21k
            }
140
10.3k
            break;
141
0
        case HashKeyType::fixed64:
142
0
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt64>>>(
143
0
                    get_key_sizes(data_types));
144
0
            break;
145
0
        case HashKeyType::fixed72:
146
0
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt72>>>(
147
0
                    get_key_sizes(data_types));
148
0
            break;
149
0
        case HashKeyType::fixed96:
150
0
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt96>>>(
151
0
                    get_key_sizes(data_types));
152
0
            break;
153
0
        case HashKeyType::fixed104:
154
0
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt104>>>(
155
0
                    get_key_sizes(data_types));
156
0
            break;
157
0
        case HashKeyType::fixed128:
158
0
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt128>>>(
159
0
                    get_key_sizes(data_types));
160
0
            break;
161
16.5k
        case HashKeyType::fixed136:
162
16.5k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt136>>>(
163
16.5k
                    get_key_sizes(data_types));
164
16.5k
            break;
165
1.78k
        case HashKeyType::fixed256:
166
1.78k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt256>>>(
167
1.78k
                    get_key_sizes(data_types));
168
1.78k
            break;
169
0
        default:
170
0
            throw Exception(ErrorCode::INTERNAL_ERROR, "meet invalid agg key type, type={}", type);
171
100k
        }
172
100k
    }
173
};
174
175
struct AggregatedDataVariants
176
        : public AggDataVariantsBase<AggregatedMethodVariants, AggregatedDataWithShortStringKey,
177
                                     AggregatedDataWithNullableShortStringKey> {
178
    AggregatedDataWithoutKey without_key = nullptr;
179
180
    bool is_fixed_key = true;
181
182
45.7k
    void init(const std::vector<DataTypePtr>& data_types, HashKeyType type) {
183
45.7k
        is_fixed_key = !(type == HashKeyType::without_key || type == HashKeyType::EMPTY ||
184
45.7k
                         type == HashKeyType::serialized || type == HashKeyType::string_key);
185
45.7k
        this->init_agg_data(data_types, type);
186
45.7k
    }
187
};
188
189
using AggregatedDataVariantsUPtr = std::unique_ptr<AggregatedDataVariants>;
190
using ArenaUPtr = std::unique_ptr<Arena>;
191
192
/// Data variants for bucketed hash aggregation.
193
/// Uses BucketedAggMethodVariants (PHHashMap for string keys).
194
struct BucketedAggDataVariants
195
        : public AggDataVariantsBase<BucketedAggMethodVariants, AggregatedDataWithStringKey,
196
                                     AggregatedDataWithNullableStringKey> {
197
103k
    void init(const std::vector<DataTypePtr>& data_types, HashKeyType type) {
198
103k
        this->init_agg_data(data_types, type);
199
103k
    }
200
};
201
202
using BucketedAggDataVariantsUPtr = std::unique_ptr<BucketedAggDataVariants>;
203
204
struct AggregateDataContainer {
205
public:
206
    AggregateDataContainer(size_t size_of_key, size_t size_of_aggregate_states)
207
45.3k
            : _size_of_key(size_of_key), _size_of_aggregate_states(size_of_aggregate_states) {}
208
209
45.5k
    int64_t memory_usage() const { return _arena_pool.size(); }
210
211
    template <typename KeyType>
212
2.81M
    AggregateDataPtr append_data(const KeyType& key) {
213
2.81M
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
214
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
215
2.81M
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
20.8k
            _expand();
217
20.8k
        }
218
219
2.81M
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
2.81M
        auto* aggregate_data = _current_agg_data;
221
2.81M
        ++_total_count;
222
2.81M
        ++_index_in_sub_container;
223
2.81M
        _current_agg_data += _size_of_aggregate_states;
224
2.81M
        _current_keys += _size_of_key;
225
2.81M
        return aggregate_data;
226
2.81M
    }
_ZN5doris22AggregateDataContainer11append_dataINS_9StringRefEEEPcRKT_
Line
Count
Source
212
114k
    AggregateDataPtr append_data(const KeyType& key) {
213
114k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
214
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
215
114k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
4.74k
            _expand();
217
4.74k
        }
218
219
114k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
114k
        auto* aggregate_data = _current_agg_data;
221
114k
        ++_total_count;
222
114k
        ++_index_in_sub_container;
223
114k
        _current_agg_data += _size_of_aggregate_states;
224
114k
        _current_keys += _size_of_key;
225
114k
        return aggregate_data;
226
114k
    }
_ZN5doris22AggregateDataContainer11append_dataIhEEPcRKT_
Line
Count
Source
212
3.03k
    AggregateDataPtr append_data(const KeyType& key) {
213
3.03k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
214
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
215
3.03k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
1.47k
            _expand();
217
1.47k
        }
218
219
3.03k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
3.03k
        auto* aggregate_data = _current_agg_data;
221
3.03k
        ++_total_count;
222
3.03k
        ++_index_in_sub_container;
223
3.03k
        _current_agg_data += _size_of_aggregate_states;
224
3.03k
        _current_keys += _size_of_key;
225
3.03k
        return aggregate_data;
226
3.03k
    }
_ZN5doris22AggregateDataContainer11append_dataItEEPcRKT_
Line
Count
Source
212
430
    AggregateDataPtr append_data(const KeyType& key) {
213
430
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
214
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
215
430
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
219
            _expand();
217
219
        }
218
219
430
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
430
        auto* aggregate_data = _current_agg_data;
221
430
        ++_total_count;
222
430
        ++_index_in_sub_container;
223
430
        _current_agg_data += _size_of_aggregate_states;
224
430
        _current_keys += _size_of_key;
225
430
        return aggregate_data;
226
430
    }
_ZN5doris22AggregateDataContainer11append_dataIjEEPcRKT_
Line
Count
Source
212
2.22M
    AggregateDataPtr append_data(const KeyType& key) {
213
2.22M
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
214
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
215
2.22M
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
9.29k
            _expand();
217
9.29k
        }
218
219
2.22M
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
2.22M
        auto* aggregate_data = _current_agg_data;
221
2.22M
        ++_total_count;
222
2.22M
        ++_index_in_sub_container;
223
2.22M
        _current_agg_data += _size_of_aggregate_states;
224
2.22M
        _current_keys += _size_of_key;
225
2.22M
        return aggregate_data;
226
2.22M
    }
_ZN5doris22AggregateDataContainer11append_dataImEEPcRKT_
Line
Count
Source
212
417k
    AggregateDataPtr append_data(const KeyType& key) {
213
417k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
214
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
215
417k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
1.74k
            _expand();
217
1.74k
        }
218
219
417k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
417k
        auto* aggregate_data = _current_agg_data;
221
417k
        ++_total_count;
222
417k
        ++_index_in_sub_container;
223
417k
        _current_agg_data += _size_of_aggregate_states;
224
417k
        _current_keys += _size_of_key;
225
417k
        return aggregate_data;
226
417k
    }
_ZN5doris22AggregateDataContainer11append_dataIN4wide7integerILm128EjEEEEPcRKT_
Line
Count
Source
212
19.0k
    AggregateDataPtr append_data(const KeyType& key) {
213
19.0k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
214
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
215
19.0k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
241
            _expand();
217
241
        }
218
219
19.0k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
19.0k
        auto* aggregate_data = _current_agg_data;
221
19.0k
        ++_total_count;
222
19.0k
        ++_index_in_sub_container;
223
19.0k
        _current_agg_data += _size_of_aggregate_states;
224
19.0k
        _current_keys += _size_of_key;
225
19.0k
        return aggregate_data;
226
19.0k
    }
_ZN5doris22AggregateDataContainer11append_dataIN4wide7integerILm256EjEEEEPcRKT_
Line
Count
Source
212
18.5k
    AggregateDataPtr append_data(const KeyType& key) {
213
18.5k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
214
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
215
18.5k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
676
            _expand();
217
676
        }
218
219
18.5k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
18.5k
        auto* aggregate_data = _current_agg_data;
221
18.5k
        ++_total_count;
222
18.5k
        ++_index_in_sub_container;
223
18.5k
        _current_agg_data += _size_of_aggregate_states;
224
18.5k
        _current_keys += _size_of_key;
225
18.5k
        return aggregate_data;
226
18.5k
    }
_ZN5doris22AggregateDataContainer11append_dataINS_6UInt72EEEPcRKT_
Line
Count
Source
212
1.00k
    AggregateDataPtr append_data(const KeyType& key) {
213
1.00k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
214
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
215
1.00k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
478
            _expand();
217
478
        }
218
219
1.00k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
1.00k
        auto* aggregate_data = _current_agg_data;
221
1.00k
        ++_total_count;
222
1.00k
        ++_index_in_sub_container;
223
1.00k
        _current_agg_data += _size_of_aggregate_states;
224
1.00k
        _current_keys += _size_of_key;
225
1.00k
        return aggregate_data;
226
1.00k
    }
_ZN5doris22AggregateDataContainer11append_dataINS_6UInt96EEEPcRKT_
Line
Count
Source
212
8.89k
    AggregateDataPtr append_data(const KeyType& key) {
213
8.89k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
214
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
215
8.89k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
604
            _expand();
217
604
        }
218
219
8.89k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
8.89k
        auto* aggregate_data = _current_agg_data;
221
8.89k
        ++_total_count;
222
8.89k
        ++_index_in_sub_container;
223
8.89k
        _current_agg_data += _size_of_aggregate_states;
224
8.89k
        _current_keys += _size_of_key;
225
8.89k
        return aggregate_data;
226
8.89k
    }
_ZN5doris22AggregateDataContainer11append_dataINS_7UInt104EEEPcRKT_
Line
Count
Source
212
860
    AggregateDataPtr append_data(const KeyType& key) {
213
860
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
214
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
215
860
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
451
            _expand();
217
451
        }
218
219
860
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
860
        auto* aggregate_data = _current_agg_data;
221
860
        ++_total_count;
222
860
        ++_index_in_sub_container;
223
860
        _current_agg_data += _size_of_aggregate_states;
224
860
        _current_keys += _size_of_key;
225
860
        return aggregate_data;
226
860
    }
_ZN5doris22AggregateDataContainer11append_dataINS_7UInt136EEEPcRKT_
Line
Count
Source
212
2.43k
    AggregateDataPtr append_data(const KeyType& key) {
213
2.43k
        DCHECK_EQ(sizeof(KeyType), _size_of_key);
214
        // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero
215
2.43k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
869
            _expand();
217
869
        }
218
219
2.43k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
2.43k
        auto* aggregate_data = _current_agg_data;
221
2.43k
        ++_total_count;
222
2.43k
        ++_index_in_sub_container;
223
2.43k
        _current_agg_data += _size_of_aggregate_states;
224
2.43k
        _current_keys += _size_of_key;
225
2.43k
        return aggregate_data;
226
2.43k
    }
227
228
    template <typename Derived, bool IsConst>
229
    class IteratorBase {
230
        using Container =
231
                std::conditional_t<IsConst, const AggregateDataContainer, AggregateDataContainer>;
232
233
        Container* container = nullptr;
234
        uint32_t index;
235
        uint32_t sub_container_index;
236
        uint32_t index_in_sub_container;
237
238
        friend class HashTable;
239
240
    public:
241
45.3k
        IteratorBase() = default;
242
        IteratorBase(Container* container_, uint32_t index_)
243
2.45M
                : container(container_), index(index_) {
244
2.45M
            sub_container_index = index / SUB_CONTAINER_CAPACITY;
245
2.45M
            index_in_sub_container = index - sub_container_index * SUB_CONTAINER_CAPACITY;
246
2.45M
        }
247
248
43.3k
        bool operator==(const IteratorBase& rhs) const { return index == rhs.index; }
249
2.35M
        bool operator!=(const IteratorBase& rhs) const { return index != rhs.index; }
250
251
2.31M
        Derived& operator++() {
252
2.31M
            index++;
253
2.31M
            index_in_sub_container++;
254
2.31M
            if (index_in_sub_container == SUB_CONTAINER_CAPACITY) {
255
253
                index_in_sub_container = 0;
256
253
                sub_container_index++;
257
253
            }
258
2.31M
            return static_cast<Derived&>(*this);
259
2.31M
        }
260
261
        template <typename KeyType>
262
2.29M
        KeyType get_key() {
263
2.29M
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
2.29M
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
2.29M
                    [index_in_sub_container];
266
2.29M
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_9StringRefEEET_v
Line
Count
Source
262
158k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
158k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
158k
                    [index_in_sub_container];
266
158k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIhEET_v
Line
Count
Source
262
3.10k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
3.10k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
3.10k
                    [index_in_sub_container];
266
3.10k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyItEET_v
Line
Count
Source
262
430
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
430
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
430
                    [index_in_sub_container];
266
430
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIjEET_v
Line
Count
Source
262
1.75M
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
1.75M
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
1.75M
                    [index_in_sub_container];
266
1.75M
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyImEET_v
Line
Count
Source
262
313k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
313k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
313k
                    [index_in_sub_container];
266
313k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIN4wide7integerILm128EjEEEET_v
Line
Count
Source
262
18.9k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
18.9k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
18.9k
                    [index_in_sub_container];
266
18.9k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIN4wide7integerILm256EjEEEET_v
Line
Count
Source
262
32.9k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
32.9k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
32.9k
                    [index_in_sub_container];
266
32.9k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_6UInt72EEET_v
Line
Count
Source
262
1.00k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
1.00k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
1.00k
                    [index_in_sub_container];
266
1.00k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_6UInt96EEET_v
Line
Count
Source
262
8.84k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
8.84k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
8.84k
                    [index_in_sub_container];
266
8.84k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_7UInt104EEET_v
Line
Count
Source
262
839
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
839
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
839
                    [index_in_sub_container];
266
839
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_7UInt136EEET_v
Line
Count
Source
262
2.43k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
2.43k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
2.43k
                    [index_in_sub_container];
266
2.43k
        }
267
268
2.21M
        AggregateDataPtr get_aggregate_data() {
269
2.21M
            return &(container->_value_containers[sub_container_index]
270
2.21M
                                                 [container->_size_of_aggregate_states *
271
2.21M
                                                  index_in_sub_container]);
272
2.21M
        }
273
    };
274
275
    class Iterator : public IteratorBase<Iterator, false> {
276
    public:
277
        using IteratorBase<Iterator, false>::IteratorBase;
278
    };
279
280
    class ConstIterator : public IteratorBase<ConstIterator, true> {
281
    public:
282
        using IteratorBase<ConstIterator, true>::IteratorBase;
283
    };
284
285
    ConstIterator begin() const { return {this, 0}; }
286
287
    ConstIterator cbegin() const { return begin(); }
288
289
43.4k
    Iterator begin() { return {this, 0}; }
290
291
    ConstIterator end() const { return {this, _total_count}; }
292
    ConstIterator cend() const { return end(); }
293
2.40M
    Iterator end() { return {this, _total_count}; }
294
295
10
    [[nodiscard]] uint32_t total_count() const { return _total_count; }
296
297
19
    size_t estimate_memory(size_t rows) const {
298
19
        bool need_to_expand = false;
299
19
        if (_total_count == 0) {
300
1
            need_to_expand = true;
301
18
        } else if ((_index_in_sub_container + rows) > SUB_CONTAINER_CAPACITY) {
302
2
            need_to_expand = true;
303
2
            rows -= (SUB_CONTAINER_CAPACITY - _index_in_sub_container);
304
2
        }
305
306
19
        if (!need_to_expand) {
307
16
            return 0;
308
16
        }
309
310
3
        size_t count = (rows + SUB_CONTAINER_CAPACITY - 1) / SUB_CONTAINER_CAPACITY;
311
3
        size_t size = _size_of_key * SUB_CONTAINER_CAPACITY;
312
3
        size += _size_of_aggregate_states * SUB_CONTAINER_CAPACITY;
313
3
        size *= count;
314
3
        return size;
315
19
    }
316
317
43.5k
    void init_once() {
318
43.5k
        if (_inited) {
319
348
            return;
320
348
        }
321
43.2k
        _inited = true;
322
43.2k
        iterator = begin();
323
43.2k
    }
324
    Iterator iterator;
325
326
private:
327
20.8k
    void _expand() {
328
20.8k
        _index_in_sub_container = 0;
329
20.8k
        _current_keys = nullptr;
330
20.8k
        _current_agg_data = nullptr;
331
20.8k
        try {
332
20.8k
            _current_keys = _arena_pool.alloc(_size_of_key * SUB_CONTAINER_CAPACITY);
333
20.8k
            _key_containers.emplace_back(_current_keys);
334
335
20.8k
            _current_agg_data = (AggregateDataPtr)_arena_pool.alloc(_size_of_aggregate_states *
336
20.8k
                                                                    SUB_CONTAINER_CAPACITY);
337
20.8k
            _value_containers.emplace_back(_current_agg_data);
338
20.8k
        } catch (...) {
339
0
            if (_current_keys) {
340
0
                _key_containers.pop_back();
341
0
                _current_keys = nullptr;
342
0
            }
343
0
            if (_current_agg_data) {
344
0
                _value_containers.pop_back();
345
0
                _current_agg_data = nullptr;
346
0
            }
347
0
            throw;
348
0
        }
349
20.8k
    }
350
351
    static constexpr uint32_t SUB_CONTAINER_CAPACITY = 8192;
352
    Arena _arena_pool;
353
    std::vector<char*> _key_containers;
354
    std::vector<AggregateDataPtr> _value_containers;
355
    AggregateDataPtr _current_agg_data = nullptr;
356
    char* _current_keys = nullptr;
357
    size_t _size_of_key {};
358
    size_t _size_of_aggregate_states {};
359
    uint32_t _index_in_sub_container {};
360
    uint32_t _total_count {};
361
    bool _inited = false;
362
};
363
} // namespace doris