Coverage Report

Created: 2026-05-11 11:01

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
156k
    void init_agg_data(const std::vector<DataTypePtr>& data_types, HashKeyType type) {
101
156k
        bool nullable = data_types.size() == 1 && data_types[0]->is_nullable();
102
103
156k
        switch (type) {
104
1
        case HashKeyType::without_key:
105
1
            break;
106
18.7k
        case HashKeyType::serialized:
107
18.7k
            this->method_variant.template emplace<MethodSerialized<AggregatedDataWithStringKey>>();
108
18.7k
            break;
109
15.7k
        case HashKeyType::int8_key:
110
15.7k
            this->template emplace_single<UInt8, AggData<UInt8>>(nullable);
111
15.7k
            break;
112
7.35k
        case HashKeyType::int16_key:
113
7.35k
            this->template emplace_single<UInt16, AggData<UInt16>>(nullable);
114
7.35k
            break;
115
10.2k
        case HashKeyType::int32_key:
116
10.2k
            this->template emplace_single<UInt32, AggData<UInt32>>(nullable);
117
10.2k
            break;
118
16.9k
        case HashKeyType::int32_key_phase2:
119
16.9k
            this->template emplace_single<UInt32, AggregatedDataWithUInt32KeyPhase2>(nullable);
120
16.9k
            break;
121
8.08k
        case HashKeyType::int64_key:
122
8.08k
            this->template emplace_single<UInt64, AggData<UInt64>>(nullable);
123
8.08k
            break;
124
6.28k
        case HashKeyType::int64_key_phase2:
125
6.28k
            this->template emplace_single<UInt64, AggregatedDataWithUInt64KeyPhase2>(nullable);
126
6.28k
            break;
127
641
        case HashKeyType::int128_key:
128
641
            this->template emplace_single<UInt128, AggData<UInt128>>(nullable);
129
641
            break;
130
7
        case HashKeyType::int256_key:
131
7
            this->template emplace_single<UInt256, AggData<UInt256>>(nullable);
132
7
            break;
133
22.7k
        case HashKeyType::string_key:
134
22.7k
            if (nullable) {
135
6.10k
                this->method_variant.template emplace<
136
6.10k
                        MethodSingleNullableColumn<MethodStringNoCache<NullableStringData>>>();
137
16.6k
            } else {
138
16.6k
                this->method_variant.template emplace<MethodStringNoCache<StringData>>();
139
16.6k
            }
140
22.7k
            break;
141
888
        case HashKeyType::fixed64:
142
888
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt64>>>(
143
888
                    get_key_sizes(data_types));
144
888
            break;
145
1.00k
        case HashKeyType::fixed72:
146
1.00k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt72>>>(
147
1.00k
                    get_key_sizes(data_types));
148
1.00k
            break;
149
2.32k
        case HashKeyType::fixed96:
150
2.32k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt96>>>(
151
2.32k
                    get_key_sizes(data_types));
152
2.32k
            break;
153
791
        case HashKeyType::fixed104:
154
791
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt104>>>(
155
791
                    get_key_sizes(data_types));
156
791
            break;
157
159
        case HashKeyType::fixed128:
158
159
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt128>>>(
159
159
                    get_key_sizes(data_types));
160
159
            break;
161
39.3k
        case HashKeyType::fixed136:
162
39.3k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt136>>>(
163
39.3k
                    get_key_sizes(data_types));
164
39.3k
            break;
165
5.40k
        case HashKeyType::fixed256:
166
5.40k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt256>>>(
167
5.40k
                    get_key_sizes(data_types));
168
5.40k
            break;
169
1
        default:
170
1
            throw Exception(ErrorCode::INTERNAL_ERROR, "meet invalid agg key type, type={}", type);
171
156k
        }
172
156k
    }
_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
70.6k
    void init_agg_data(const std::vector<DataTypePtr>& data_types, HashKeyType type) {
101
70.6k
        bool nullable = data_types.size() == 1 && data_types[0]->is_nullable();
102
103
70.6k
        switch (type) {
104
1
        case HashKeyType::without_key:
105
1
            break;
106
8.48k
        case HashKeyType::serialized:
107
8.48k
            this->method_variant.template emplace<MethodSerialized<AggregatedDataWithStringKey>>();
108
8.48k
            break;
109
13.2k
        case HashKeyType::int8_key:
110
13.2k
            this->template emplace_single<UInt8, AggData<UInt8>>(nullable);
111
13.2k
            break;
112
556
        case HashKeyType::int16_key:
113
556
            this->template emplace_single<UInt16, AggData<UInt16>>(nullable);
114
556
            break;
115
4.66k
        case HashKeyType::int32_key:
116
4.66k
            this->template emplace_single<UInt32, AggData<UInt32>>(nullable);
117
4.66k
            break;
118
16.9k
        case HashKeyType::int32_key_phase2:
119
16.9k
            this->template emplace_single<UInt32, AggregatedDataWithUInt32KeyPhase2>(nullable);
120
16.9k
            break;
121
3.31k
        case HashKeyType::int64_key:
122
3.31k
            this->template emplace_single<UInt64, AggData<UInt64>>(nullable);
123
3.31k
            break;
124
6.28k
        case HashKeyType::int64_key_phase2:
125
6.28k
            this->template emplace_single<UInt64, AggregatedDataWithUInt64KeyPhase2>(nullable);
126
6.28k
            break;
127
641
        case HashKeyType::int128_key:
128
641
            this->template emplace_single<UInt128, AggData<UInt128>>(nullable);
129
641
            break;
130
7
        case HashKeyType::int256_key:
131
7
            this->template emplace_single<UInt256, AggData<UInt256>>(nullable);
132
7
            break;
133
6.32k
        case HashKeyType::string_key:
134
6.32k
            if (nullable) {
135
5.12k
                this->method_variant.template emplace<
136
5.12k
                        MethodSingleNullableColumn<MethodStringNoCache<NullableStringData>>>();
137
5.12k
            } else {
138
1.20k
                this->method_variant.template emplace<MethodStringNoCache<StringData>>();
139
1.20k
            }
140
6.32k
            break;
141
888
        case HashKeyType::fixed64:
142
888
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt64>>>(
143
888
                    get_key_sizes(data_types));
144
888
            break;
145
1.00k
        case HashKeyType::fixed72:
146
1.00k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt72>>>(
147
1.00k
                    get_key_sizes(data_types));
148
1.00k
            break;
149
2.07k
        case HashKeyType::fixed96:
150
2.07k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt96>>>(
151
2.07k
                    get_key_sizes(data_types));
152
2.07k
            break;
153
791
        case HashKeyType::fixed104:
154
791
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt104>>>(
155
791
                    get_key_sizes(data_types));
156
791
            break;
157
159
        case HashKeyType::fixed128:
158
159
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt128>>>(
159
159
                    get_key_sizes(data_types));
160
159
            break;
161
1.72k
        case HashKeyType::fixed136:
162
1.72k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt136>>>(
163
1.72k
                    get_key_sizes(data_types));
164
1.72k
            break;
165
3.68k
        case HashKeyType::fixed256:
166
3.68k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt256>>>(
167
3.68k
                    get_key_sizes(data_types));
168
3.68k
            break;
169
1
        default:
170
1
            throw Exception(ErrorCode::INTERNAL_ERROR, "meet invalid agg key type, type={}", type);
171
70.6k
        }
172
70.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
86.1k
    void init_agg_data(const std::vector<DataTypePtr>& data_types, HashKeyType type) {
101
86.1k
        bool nullable = data_types.size() == 1 && data_types[0]->is_nullable();
102
103
86.1k
        switch (type) {
104
0
        case HashKeyType::without_key:
105
0
            break;
106
10.2k
        case HashKeyType::serialized:
107
10.2k
            this->method_variant.template emplace<MethodSerialized<AggregatedDataWithStringKey>>();
108
10.2k
            break;
109
2.50k
        case HashKeyType::int8_key:
110
2.50k
            this->template emplace_single<UInt8, AggData<UInt8>>(nullable);
111
2.50k
            break;
112
6.79k
        case HashKeyType::int16_key:
113
6.79k
            this->template emplace_single<UInt16, AggData<UInt16>>(nullable);
114
6.79k
            break;
115
5.59k
        case HashKeyType::int32_key:
116
5.59k
            this->template emplace_single<UInt32, AggData<UInt32>>(nullable);
117
5.59k
            break;
118
0
        case HashKeyType::int32_key_phase2:
119
0
            this->template emplace_single<UInt32, AggregatedDataWithUInt32KeyPhase2>(nullable);
120
0
            break;
121
4.77k
        case HashKeyType::int64_key:
122
4.77k
            this->template emplace_single<UInt64, AggData<UInt64>>(nullable);
123
4.77k
            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
16.3k
        case HashKeyType::string_key:
134
16.3k
            if (nullable) {
135
985
                this->method_variant.template emplace<
136
985
                        MethodSingleNullableColumn<MethodStringNoCache<NullableStringData>>>();
137
15.4k
            } else {
138
15.4k
                this->method_variant.template emplace<MethodStringNoCache<StringData>>();
139
15.4k
            }
140
16.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
256
        case HashKeyType::fixed96:
150
256
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt96>>>(
151
256
                    get_key_sizes(data_types));
152
256
            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
37.6k
        case HashKeyType::fixed136:
162
37.6k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt136>>>(
163
37.6k
                    get_key_sizes(data_types));
164
37.6k
            break;
165
1.72k
        case HashKeyType::fixed256:
166
1.72k
            this->method_variant.template emplace<MethodKeysFixed<AggData<UInt256>>>(
167
1.72k
                    get_key_sizes(data_types));
168
1.72k
            break;
169
0
        default:
170
0
            throw Exception(ErrorCode::INTERNAL_ERROR, "meet invalid agg key type, type={}", type);
171
86.1k
        }
172
86.1k
    }
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
70.7k
    void init(const std::vector<DataTypePtr>& data_types, HashKeyType type) {
183
70.7k
        is_fixed_key = !(type == HashKeyType::without_key || type == HashKeyType::EMPTY ||
184
70.7k
                         type == HashKeyType::serialized || type == HashKeyType::string_key);
185
70.7k
        this->init_agg_data(data_types, type);
186
70.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
84.8k
    void init(const std::vector<DataTypePtr>& data_types, HashKeyType type) {
198
84.8k
        this->init_agg_data(data_types, type);
199
84.8k
    }
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
68.2k
            : _size_of_key(size_of_key), _size_of_aggregate_states(size_of_aggregate_states) {}
208
209
54.4k
    int64_t memory_usage() const { return _arena_pool.size(); }
210
211
    template <typename KeyType>
212
3.28M
    AggregateDataPtr append_data(const KeyType& key) {
213
3.28M
        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.28M
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
26.0k
            _expand();
217
26.0k
        }
218
219
3.28M
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
3.28M
        auto* aggregate_data = _current_agg_data;
221
3.28M
        ++_total_count;
222
3.28M
        ++_index_in_sub_container;
223
3.28M
        _current_agg_data += _size_of_aggregate_states;
224
3.28M
        _current_keys += _size_of_key;
225
3.28M
        return aggregate_data;
226
3.28M
    }
_ZN5doris22AggregateDataContainer11append_dataINS_9StringRefEEEPcRKT_
Line
Count
Source
212
174k
    AggregateDataPtr append_data(const KeyType& key) {
213
174k
        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
174k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
5.19k
            _expand();
217
5.19k
        }
218
219
174k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
174k
        auto* aggregate_data = _current_agg_data;
221
174k
        ++_total_count;
222
174k
        ++_index_in_sub_container;
223
174k
        _current_agg_data += _size_of_aggregate_states;
224
174k
        _current_keys += _size_of_key;
225
174k
        return aggregate_data;
226
174k
    }
_ZN5doris22AggregateDataContainer11append_dataIhEEPcRKT_
Line
Count
Source
212
3.17k
    AggregateDataPtr append_data(const KeyType& key) {
213
3.17k
        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.17k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
2.03k
            _expand();
217
2.03k
        }
218
219
3.17k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
3.17k
        auto* aggregate_data = _current_agg_data;
221
3.17k
        ++_total_count;
222
3.17k
        ++_index_in_sub_container;
223
3.17k
        _current_agg_data += _size_of_aggregate_states;
224
3.17k
        _current_keys += _size_of_key;
225
3.17k
        return aggregate_data;
226
3.17k
    }
_ZN5doris22AggregateDataContainer11append_dataItEEPcRKT_
Line
Count
Source
212
1.77k
    AggregateDataPtr append_data(const KeyType& key) {
213
1.77k
        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.77k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
397
            _expand();
217
397
        }
218
219
1.77k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
1.77k
        auto* aggregate_data = _current_agg_data;
221
1.77k
        ++_total_count;
222
1.77k
        ++_index_in_sub_container;
223
1.77k
        _current_agg_data += _size_of_aggregate_states;
224
1.77k
        _current_keys += _size_of_key;
225
1.77k
        return aggregate_data;
226
1.77k
    }
_ZN5doris22AggregateDataContainer11append_dataIjEEPcRKT_
Line
Count
Source
212
2.26M
    AggregateDataPtr append_data(const KeyType& key) {
213
2.26M
        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.26M
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
10.6k
            _expand();
217
10.6k
        }
218
219
2.26M
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
2.26M
        auto* aggregate_data = _current_agg_data;
221
2.26M
        ++_total_count;
222
2.26M
        ++_index_in_sub_container;
223
2.26M
        _current_agg_data += _size_of_aggregate_states;
224
2.26M
        _current_keys += _size_of_key;
225
2.26M
        return aggregate_data;
226
2.26M
    }
_ZN5doris22AggregateDataContainer11append_dataImEEPcRKT_
Line
Count
Source
212
774k
    AggregateDataPtr append_data(const KeyType& key) {
213
774k
        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
774k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
3.81k
            _expand();
217
3.81k
        }
218
219
774k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
774k
        auto* aggregate_data = _current_agg_data;
221
774k
        ++_total_count;
222
774k
        ++_index_in_sub_container;
223
774k
        _current_agg_data += _size_of_aggregate_states;
224
774k
        _current_keys += _size_of_key;
225
774k
        return aggregate_data;
226
774k
    }
_ZN5doris22AggregateDataContainer11append_dataIN4wide7integerILm128EjEEEEPcRKT_
Line
Count
Source
212
17.3k
    AggregateDataPtr append_data(const KeyType& key) {
213
17.3k
        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
17.3k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
395
            _expand();
217
395
        }
218
219
17.3k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
17.3k
        auto* aggregate_data = _current_agg_data;
221
17.3k
        ++_total_count;
222
17.3k
        ++_index_in_sub_container;
223
17.3k
        _current_agg_data += _size_of_aggregate_states;
224
17.3k
        _current_keys += _size_of_key;
225
17.3k
        return aggregate_data;
226
17.3k
    }
_ZN5doris22AggregateDataContainer11append_dataIN4wide7integerILm256EjEEEEPcRKT_
Line
Count
Source
212
36.9k
    AggregateDataPtr append_data(const KeyType& key) {
213
36.9k
        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
36.9k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
746
            _expand();
217
746
        }
218
219
36.9k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
36.9k
        auto* aggregate_data = _current_agg_data;
221
36.9k
        ++_total_count;
222
36.9k
        ++_index_in_sub_container;
223
36.9k
        _current_agg_data += _size_of_aggregate_states;
224
36.9k
        _current_keys += _size_of_key;
225
36.9k
        return aggregate_data;
226
36.9k
    }
_ZN5doris22AggregateDataContainer11append_dataINS_6UInt72EEEPcRKT_
Line
Count
Source
212
1.02k
    AggregateDataPtr append_data(const KeyType& key) {
213
1.02k
        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.02k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
440
            _expand();
217
440
        }
218
219
1.02k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
1.02k
        auto* aggregate_data = _current_agg_data;
221
1.02k
        ++_total_count;
222
1.02k
        ++_index_in_sub_container;
223
1.02k
        _current_agg_data += _size_of_aggregate_states;
224
1.02k
        _current_keys += _size_of_key;
225
1.02k
        return aggregate_data;
226
1.02k
    }
_ZN5doris22AggregateDataContainer11append_dataINS_6UInt96EEEPcRKT_
Line
Count
Source
212
8.92k
    AggregateDataPtr append_data(const KeyType& key) {
213
8.92k
        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.92k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
1.30k
            _expand();
217
1.30k
        }
218
219
8.92k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
8.92k
        auto* aggregate_data = _current_agg_data;
221
8.92k
        ++_total_count;
222
8.92k
        ++_index_in_sub_container;
223
8.92k
        _current_agg_data += _size_of_aggregate_states;
224
8.92k
        _current_keys += _size_of_key;
225
8.92k
        return aggregate_data;
226
8.92k
    }
_ZN5doris22AggregateDataContainer11append_dataINS_7UInt104EEEPcRKT_
Line
Count
Source
212
790
    AggregateDataPtr append_data(const KeyType& key) {
213
790
        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
790
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
304
            _expand();
217
304
        }
218
219
790
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
790
        auto* aggregate_data = _current_agg_data;
221
790
        ++_total_count;
222
790
        ++_index_in_sub_container;
223
790
        _current_agg_data += _size_of_aggregate_states;
224
790
        _current_keys += _size_of_key;
225
790
        return aggregate_data;
226
790
    }
_ZN5doris22AggregateDataContainer11append_dataINS_7UInt136EEEPcRKT_
Line
Count
Source
212
1.83k
    AggregateDataPtr append_data(const KeyType& key) {
213
1.83k
        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.83k
        if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) {
216
758
            _expand();
217
758
        }
218
219
1.83k
        *reinterpret_cast<KeyType*>(_current_keys) = key;
220
1.83k
        auto* aggregate_data = _current_agg_data;
221
1.83k
        ++_total_count;
222
1.83k
        ++_index_in_sub_container;
223
1.83k
        _current_agg_data += _size_of_aggregate_states;
224
1.83k
        _current_keys += _size_of_key;
225
1.83k
        return aggregate_data;
226
1.83k
    }
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
68.1k
        IteratorBase() = default;
242
        IteratorBase(Container* container_, uint32_t index_)
243
3.45M
                : container(container_), index(index_) {
244
3.45M
            sub_container_index = index / SUB_CONTAINER_CAPACITY;
245
3.45M
            index_in_sub_container = index - sub_container_index * SUB_CONTAINER_CAPACITY;
246
3.45M
        }
247
248
63.0k
        bool operator==(const IteratorBase& rhs) const { return index == rhs.index; }
249
3.31M
        bool operator!=(const IteratorBase& rhs) const { return index != rhs.index; }
250
251
3.28M
        Derived& operator++() {
252
3.28M
            index++;
253
3.28M
            index_in_sub_container++;
254
3.28M
            if (index_in_sub_container == SUB_CONTAINER_CAPACITY) {
255
256
                index_in_sub_container = 0;
256
256
                sub_container_index++;
257
256
            }
258
3.28M
            return static_cast<Derived&>(*this);
259
3.28M
        }
260
261
        template <typename KeyType>
262
3.26M
        KeyType get_key() {
263
3.26M
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
3.26M
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
3.26M
                    [index_in_sub_container];
266
3.26M
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_9StringRefEEET_v
Line
Count
Source
262
246k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
246k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
246k
                    [index_in_sub_container];
266
246k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIhEET_v
Line
Count
Source
262
3.36k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
3.36k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
3.36k
                    [index_in_sub_container];
266
3.36k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyItEET_v
Line
Count
Source
262
1.77k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
1.77k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
1.77k
                    [index_in_sub_container];
266
1.77k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIjEET_v
Line
Count
Source
262
2.14M
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
2.14M
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
2.14M
                    [index_in_sub_container];
266
2.14M
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyImEET_v
Line
Count
Source
262
770k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
770k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
770k
                    [index_in_sub_container];
266
770k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIN4wide7integerILm128EjEEEET_v
Line
Count
Source
262
17.4k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
17.4k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
17.4k
                    [index_in_sub_container];
266
17.4k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIN4wide7integerILm256EjEEEET_v
Line
Count
Source
262
70.2k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
70.2k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
70.2k
                    [index_in_sub_container];
266
70.2k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_6UInt72EEET_v
Line
Count
Source
262
1.02k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
1.02k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
1.02k
                    [index_in_sub_container];
266
1.02k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_6UInt96EEET_v
Line
Count
Source
262
8.93k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
8.93k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
8.93k
                    [index_in_sub_container];
266
8.93k
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_7UInt104EEET_v
Line
Count
Source
262
790
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
790
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
790
                    [index_in_sub_container];
266
790
        }
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_7UInt136EEET_v
Line
Count
Source
262
1.83k
        KeyType get_key() {
263
            DCHECK_EQ(sizeof(KeyType), container->_size_of_key);
264
1.83k
            return ((KeyType*)(container->_key_containers[sub_container_index]))
265
1.83k
                    [index_in_sub_container];
266
1.83k
        }
267
268
3.09M
        AggregateDataPtr get_aggregate_data() {
269
3.09M
            return &(container->_value_containers[sub_container_index]
270
3.09M
                                                 [container->_size_of_aggregate_states *
271
3.09M
                                                  index_in_sub_container]);
272
3.09M
        }
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
63.3k
    Iterator begin() { return {this, 0}; }
290
291
    ConstIterator end() const { return {this, _total_count}; }
292
    ConstIterator cend() const { return end(); }
293
3.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
63.2k
    void init_once() {
318
63.2k
        if (_inited) {
319
302
            return;
320
302
        }
321
62.9k
        _inited = true;
322
62.9k
        iterator = begin();
323
62.9k
    }
324
    Iterator iterator;
325
326
private:
327
26.0k
    void _expand() {
328
26.0k
        _index_in_sub_container = 0;
329
26.0k
        _current_keys = nullptr;
330
26.0k
        _current_agg_data = nullptr;
331
26.0k
        try {
332
26.0k
            _current_keys = _arena_pool.alloc(_size_of_key * SUB_CONTAINER_CAPACITY);
333
26.0k
            _key_containers.emplace_back(_current_keys);
334
335
26.0k
            _current_agg_data = (AggregateDataPtr)_arena_pool.alloc(_size_of_aggregate_states *
336
26.0k
                                                                    SUB_CONTAINER_CAPACITY);
337
26.0k
            _value_containers.emplace_back(_current_agg_data);
338
26.0k
        } 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
26.0k
    }
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