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 | 60.0k | void init(const std::vector<DataTypePtr>& data_types, HashKeyType type) { |
81 | 60.0k | bool nullable = data_types.size() == 1 && data_types[0]->is_nullable(); |
82 | | |
83 | 60.0k | switch (type) { |
84 | 1 | case HashKeyType::without_key: |
85 | 1 | break; |
86 | 15.6k | case HashKeyType::serialized: |
87 | 15.6k | method_variant.emplace<MethodSerialized<AggregatedDataWithStringKey>>(); |
88 | 15.6k | break; |
89 | 2.92k | case HashKeyType::int8_key: |
90 | 2.92k | emplace_single<UInt8, AggData<UInt8>>(nullable); |
91 | 2.92k | break; |
92 | 764 | case HashKeyType::int16_key: |
93 | 764 | emplace_single<UInt16, AggData<UInt16>>(nullable); |
94 | 764 | break; |
95 | 2.79k | case HashKeyType::int32_key: |
96 | 2.79k | emplace_single<UInt32, AggData<UInt32>>(nullable); |
97 | 2.79k | break; |
98 | 9.77k | case HashKeyType::int32_key_phase2: |
99 | 9.77k | emplace_single<UInt32, AggregatedDataWithUInt32KeyPhase2>(nullable); |
100 | 9.77k | break; |
101 | 2.24k | case HashKeyType::int64_key: |
102 | 2.24k | emplace_single<UInt64, AggData<UInt64>>(nullable); |
103 | 2.24k | break; |
104 | 7.71k | case HashKeyType::int64_key_phase2: |
105 | 7.71k | emplace_single<UInt64, AggregatedDataWithUInt64KeyPhase2>(nullable); |
106 | 7.71k | break; |
107 | 443 | case HashKeyType::int128_key: |
108 | 443 | emplace_single<UInt128, AggData<UInt128>>(nullable); |
109 | 443 | break; |
110 | 31 | case HashKeyType::int256_key: |
111 | 31 | emplace_single<UInt256, AggData<UInt256>>(nullable); |
112 | 31 | break; |
113 | 3.29k | case HashKeyType::string_key: |
114 | 3.29k | if (nullable) { |
115 | 2.45k | method_variant.emplace<MethodSingleNullableColumn< |
116 | 2.45k | MethodStringNoCache<AggregatedDataWithNullableShortStringKey>>>(); |
117 | 2.45k | } else { |
118 | 845 | method_variant.emplace<MethodStringNoCache<AggregatedDataWithShortStringKey>>(); |
119 | 845 | } |
120 | 3.29k | break; |
121 | 1.00k | case HashKeyType::fixed64: |
122 | 1.00k | method_variant.emplace<MethodKeysFixed<AggData<UInt64>>>(get_key_sizes(data_types)); |
123 | 1.00k | break; |
124 | 1.09k | case HashKeyType::fixed72: |
125 | 1.09k | method_variant.emplace<MethodKeysFixed<AggData<UInt72>>>(get_key_sizes(data_types)); |
126 | 1.09k | break; |
127 | 675 | case HashKeyType::fixed96: |
128 | 675 | method_variant.emplace<MethodKeysFixed<AggData<UInt96>>>(get_key_sizes(data_types)); |
129 | 675 | break; |
130 | 1.21k | case HashKeyType::fixed104: |
131 | 1.21k | method_variant.emplace<MethodKeysFixed<AggData<UInt104>>>(get_key_sizes(data_types)); |
132 | 1.21k | break; |
133 | 208 | case HashKeyType::fixed128: |
134 | 208 | method_variant.emplace<MethodKeysFixed<AggData<UInt128>>>(get_key_sizes(data_types)); |
135 | 208 | break; |
136 | 2.32k | case HashKeyType::fixed136: |
137 | 2.32k | method_variant.emplace<MethodKeysFixed<AggData<UInt136>>>(get_key_sizes(data_types)); |
138 | 2.32k | break; |
139 | 7.91k | case HashKeyType::fixed256: |
140 | 7.91k | method_variant.emplace<MethodKeysFixed<AggData<UInt256>>>(get_key_sizes(data_types)); |
141 | 7.91k | break; |
142 | 1 | default: |
143 | 1 | throw Exception(ErrorCode::INTERNAL_ERROR, |
144 | 1 | "AggregatedDataVariants meet invalid key type, type={}", type); |
145 | 60.0k | } |
146 | 60.0k | } |
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 | 60.1k | : _size_of_key(size_of_key), _size_of_aggregate_states(size_of_aggregate_states) {} |
156 | | |
157 | 42.5k | int64_t memory_usage() const { return _arena_pool.size(); } |
158 | | |
159 | | template <typename KeyType> |
160 | 4.25M | AggregateDataPtr append_data(const KeyType& key) { |
161 | 4.25M | 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.25M | if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) { |
164 | 20.9k | _expand(); |
165 | 20.9k | } |
166 | | |
167 | 4.25M | *reinterpret_cast<KeyType*>(_current_keys) = key; |
168 | 4.25M | auto* aggregate_data = _current_agg_data; |
169 | 4.25M | ++_total_count; |
170 | 4.25M | ++_index_in_sub_container; |
171 | 4.25M | _current_agg_data += _size_of_aggregate_states; |
172 | 4.25M | _current_keys += _size_of_key; |
173 | 4.25M | return aggregate_data; |
174 | 4.25M | } _ZN5doris22AggregateDataContainer11append_dataINS_9StringRefEEEPcRKT_ Line | Count | Source | 160 | 143k | AggregateDataPtr append_data(const KeyType& key) { | 161 | 143k | 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 | 143k | if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) { | 164 | 5.55k | _expand(); | 165 | 5.55k | } | 166 | | | 167 | 143k | *reinterpret_cast<KeyType*>(_current_keys) = key; | 168 | 143k | auto* aggregate_data = _current_agg_data; | 169 | 143k | ++_total_count; | 170 | 143k | ++_index_in_sub_container; | 171 | 143k | _current_agg_data += _size_of_aggregate_states; | 172 | 143k | _current_keys += _size_of_key; | 173 | 143k | return aggregate_data; | 174 | 143k | } |
_ZN5doris22AggregateDataContainer11append_dataIhEEPcRKT_ Line | Count | Source | 160 | 3.71k | AggregateDataPtr append_data(const KeyType& key) { | 161 | 3.71k | DCHECK_EQ(sizeof(KeyType), _size_of_key); | 162 | | // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero | 163 | 3.71k | if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) { | 164 | 2.04k | _expand(); | 165 | 2.04k | } | 166 | | | 167 | 3.71k | *reinterpret_cast<KeyType*>(_current_keys) = key; | 168 | 3.71k | auto* aggregate_data = _current_agg_data; | 169 | 3.71k | ++_total_count; | 170 | 3.71k | ++_index_in_sub_container; | 171 | 3.71k | _current_agg_data += _size_of_aggregate_states; | 172 | 3.71k | _current_keys += _size_of_key; | 173 | 3.71k | return aggregate_data; | 174 | 3.71k | } |
_ZN5doris22AggregateDataContainer11append_dataItEEPcRKT_ Line | Count | Source | 160 | 683 | AggregateDataPtr append_data(const KeyType& key) { | 161 | 683 | 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 | 683 | if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) { | 164 | 379 | _expand(); | 165 | 379 | } | 166 | | | 167 | 683 | *reinterpret_cast<KeyType*>(_current_keys) = key; | 168 | 683 | auto* aggregate_data = _current_agg_data; | 169 | 683 | ++_total_count; | 170 | 683 | ++_index_in_sub_container; | 171 | 683 | _current_agg_data += _size_of_aggregate_states; | 172 | 683 | _current_keys += _size_of_key; | 173 | 683 | return aggregate_data; | 174 | 683 | } |
_ZN5doris22AggregateDataContainer11append_dataIjEEPcRKT_ Line | Count | Source | 160 | 2.47M | AggregateDataPtr append_data(const KeyType& key) { | 161 | 2.47M | 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.47M | if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) { | 164 | 6.99k | _expand(); | 165 | 6.99k | } | 166 | | | 167 | 2.47M | *reinterpret_cast<KeyType*>(_current_keys) = key; | 168 | 2.47M | auto* aggregate_data = _current_agg_data; | 169 | 2.47M | ++_total_count; | 170 | 2.47M | ++_index_in_sub_container; | 171 | 2.47M | _current_agg_data += _size_of_aggregate_states; | 172 | 2.47M | _current_keys += _size_of_key; | 173 | 2.47M | return aggregate_data; | 174 | 2.47M | } |
_ZN5doris22AggregateDataContainer11append_dataImEEPcRKT_ Line | Count | Source | 160 | 1.57M | AggregateDataPtr append_data(const KeyType& key) { | 161 | 1.57M | DCHECK_EQ(sizeof(KeyType), _size_of_key); | 162 | | // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero | 163 | 1.57M | if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) { | 164 | 2.55k | _expand(); | 165 | 2.55k | } | 166 | | | 167 | 1.57M | *reinterpret_cast<KeyType*>(_current_keys) = key; | 168 | 1.57M | auto* aggregate_data = _current_agg_data; | 169 | 1.57M | ++_total_count; | 170 | 1.57M | ++_index_in_sub_container; | 171 | 1.57M | _current_agg_data += _size_of_aggregate_states; | 172 | 1.57M | _current_keys += _size_of_key; | 173 | 1.57M | return aggregate_data; | 174 | 1.57M | } |
_ZN5doris22AggregateDataContainer11append_dataIN4wide7integerILm128EjEEEEPcRKT_ Line | Count | Source | 160 | 23.7k | AggregateDataPtr append_data(const KeyType& key) { | 161 | 23.7k | DCHECK_EQ(sizeof(KeyType), _size_of_key); | 162 | | // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero | 163 | 23.7k | if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) { | 164 | 280 | _expand(); | 165 | 280 | } | 166 | | | 167 | 23.7k | *reinterpret_cast<KeyType*>(_current_keys) = key; | 168 | 23.7k | auto* aggregate_data = _current_agg_data; | 169 | 23.7k | ++_total_count; | 170 | 23.7k | ++_index_in_sub_container; | 171 | 23.7k | _current_agg_data += _size_of_aggregate_states; | 172 | 23.7k | _current_keys += _size_of_key; | 173 | 23.7k | return aggregate_data; | 174 | 23.7k | } |
_ZN5doris22AggregateDataContainer11append_dataIN4wide7integerILm256EjEEEEPcRKT_ Line | Count | Source | 160 | 24.9k | AggregateDataPtr append_data(const KeyType& key) { | 161 | 24.9k | DCHECK_EQ(sizeof(KeyType), _size_of_key); | 162 | | // SUB_CONTAINER_CAPACITY should add a new sub container, and also expand when it is zero | 163 | 24.9k | if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) { | 164 | 720 | _expand(); | 165 | 720 | } | 166 | | | 167 | 24.9k | *reinterpret_cast<KeyType*>(_current_keys) = key; | 168 | 24.9k | auto* aggregate_data = _current_agg_data; | 169 | 24.9k | ++_total_count; | 170 | 24.9k | ++_index_in_sub_container; | 171 | 24.9k | _current_agg_data += _size_of_aggregate_states; | 172 | 24.9k | _current_keys += _size_of_key; | 173 | 24.9k | return aggregate_data; | 174 | 24.9k | } |
_ZN5doris22AggregateDataContainer11append_dataINS_6UInt72EEEPcRKT_ Line | Count | Source | 160 | 1.06k | AggregateDataPtr append_data(const KeyType& key) { | 161 | 1.06k | 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.06k | if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) { | 164 | 483 | _expand(); | 165 | 483 | } | 166 | | | 167 | 1.06k | *reinterpret_cast<KeyType*>(_current_keys) = key; | 168 | 1.06k | auto* aggregate_data = _current_agg_data; | 169 | 1.06k | ++_total_count; | 170 | 1.06k | ++_index_in_sub_container; | 171 | 1.06k | _current_agg_data += _size_of_aggregate_states; | 172 | 1.06k | _current_keys += _size_of_key; | 173 | 1.06k | return aggregate_data; | 174 | 1.06k | } |
_ZN5doris22AggregateDataContainer11append_dataINS_6UInt96EEEPcRKT_ Line | Count | Source | 160 | 8.96k | AggregateDataPtr append_data(const KeyType& key) { | 161 | 8.96k | 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.96k | if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) { | 164 | 592 | _expand(); | 165 | 592 | } | 166 | | | 167 | 8.96k | *reinterpret_cast<KeyType*>(_current_keys) = key; | 168 | 8.96k | auto* aggregate_data = _current_agg_data; | 169 | 8.96k | ++_total_count; | 170 | 8.96k | ++_index_in_sub_container; | 171 | 8.96k | _current_agg_data += _size_of_aggregate_states; | 172 | 8.96k | _current_keys += _size_of_key; | 173 | 8.96k | return aggregate_data; | 174 | 8.96k | } |
_ZN5doris22AggregateDataContainer11append_dataINS_7UInt104EEEPcRKT_ Line | Count | Source | 160 | 990 | AggregateDataPtr append_data(const KeyType& key) { | 161 | 990 | 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 | 990 | if (UNLIKELY(_index_in_sub_container % SUB_CONTAINER_CAPACITY == 0)) { | 164 | 473 | _expand(); | 165 | 473 | } | 166 | | | 167 | 990 | *reinterpret_cast<KeyType*>(_current_keys) = key; | 168 | 990 | auto* aggregate_data = _current_agg_data; | 169 | 990 | ++_total_count; | 170 | 990 | ++_index_in_sub_container; | 171 | 990 | _current_agg_data += _size_of_aggregate_states; | 172 | 990 | _current_keys += _size_of_key; | 173 | 990 | return aggregate_data; | 174 | 990 | } |
_ZN5doris22AggregateDataContainer11append_dataINS_7UInt136EEEPcRKT_ 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 | 910 | _expand(); | 165 | 910 | } | 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 | } |
|
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 | 60.1k | IteratorBase() = default; |
190 | | IteratorBase(Container* container_, uint32_t index_) |
191 | 4.42M | : container(container_), index(index_) { |
192 | 4.42M | sub_container_index = index / SUB_CONTAINER_CAPACITY; |
193 | 4.42M | index_in_sub_container = index - sub_container_index * SUB_CONTAINER_CAPACITY; |
194 | 4.42M | } |
195 | | |
196 | 57.0k | bool operator==(const IteratorBase& rhs) const { return index == rhs.index; } |
197 | 4.31M | bool operator!=(const IteratorBase& rhs) const { return index != rhs.index; } |
198 | | |
199 | 4.26M | Derived& operator++() { |
200 | 4.26M | index++; |
201 | 4.26M | index_in_sub_container++; |
202 | 4.26M | if (index_in_sub_container == SUB_CONTAINER_CAPACITY) { |
203 | 366 | index_in_sub_container = 0; |
204 | 366 | sub_container_index++; |
205 | 366 | } |
206 | 4.26M | return static_cast<Derived&>(*this); |
207 | 4.26M | } |
208 | | |
209 | | template <typename KeyType> |
210 | 4.26M | KeyType get_key() { |
211 | 4.26M | DCHECK_EQ(sizeof(KeyType), container->_size_of_key); |
212 | 4.26M | return ((KeyType*)(container->_key_containers[sub_container_index])) |
213 | 4.26M | [index_in_sub_container]; |
214 | 4.26M | } _ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_9StringRefEEET_v Line | Count | Source | 210 | 207k | KeyType get_key() { | 211 | | DCHECK_EQ(sizeof(KeyType), container->_size_of_key); | 212 | 207k | return ((KeyType*)(container->_key_containers[sub_container_index])) | 213 | 207k | [index_in_sub_container]; | 214 | 207k | } |
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIhEET_v Line | Count | Source | 210 | 3.81k | KeyType get_key() { | 211 | | DCHECK_EQ(sizeof(KeyType), container->_size_of_key); | 212 | 3.81k | return ((KeyType*)(container->_key_containers[sub_container_index])) | 213 | 3.81k | [index_in_sub_container]; | 214 | 3.81k | } |
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyItEET_v Line | Count | Source | 210 | 683 | KeyType get_key() { | 211 | | DCHECK_EQ(sizeof(KeyType), container->_size_of_key); | 212 | 683 | return ((KeyType*)(container->_key_containers[sub_container_index])) | 213 | 683 | [index_in_sub_container]; | 214 | 683 | } |
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIjEET_v Line | Count | Source | 210 | 2.38M | KeyType get_key() { | 211 | | DCHECK_EQ(sizeof(KeyType), container->_size_of_key); | 212 | 2.38M | return ((KeyType*)(container->_key_containers[sub_container_index])) | 213 | 2.38M | [index_in_sub_container]; | 214 | 2.38M | } |
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyImEET_v Line | Count | Source | 210 | 1.58M | KeyType get_key() { | 211 | | DCHECK_EQ(sizeof(KeyType), container->_size_of_key); | 212 | 1.58M | return ((KeyType*)(container->_key_containers[sub_container_index])) | 213 | 1.58M | [index_in_sub_container]; | 214 | 1.58M | } |
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIN4wide7integerILm128EjEEEET_v Line | Count | Source | 210 | 23.7k | KeyType get_key() { | 211 | | DCHECK_EQ(sizeof(KeyType), container->_size_of_key); | 212 | 23.7k | return ((KeyType*)(container->_key_containers[sub_container_index])) | 213 | 23.7k | [index_in_sub_container]; | 214 | 23.7k | } |
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyIN4wide7integerILm256EjEEEET_v Line | Count | Source | 210 | 46.3k | KeyType get_key() { | 211 | | DCHECK_EQ(sizeof(KeyType), container->_size_of_key); | 212 | 46.3k | return ((KeyType*)(container->_key_containers[sub_container_index])) | 213 | 46.3k | [index_in_sub_container]; | 214 | 46.3k | } |
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_6UInt72EEET_v Line | Count | Source | 210 | 1.06k | KeyType get_key() { | 211 | | DCHECK_EQ(sizeof(KeyType), container->_size_of_key); | 212 | 1.06k | return ((KeyType*)(container->_key_containers[sub_container_index])) | 213 | 1.06k | [index_in_sub_container]; | 214 | 1.06k | } |
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_6UInt96EEET_v Line | Count | Source | 210 | 8.98k | KeyType get_key() { | 211 | | DCHECK_EQ(sizeof(KeyType), container->_size_of_key); | 212 | 8.98k | return ((KeyType*)(container->_key_containers[sub_container_index])) | 213 | 8.98k | [index_in_sub_container]; | 214 | 8.98k | } |
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_7UInt104EEET_v Line | Count | Source | 210 | 989 | KeyType get_key() { | 211 | | DCHECK_EQ(sizeof(KeyType), container->_size_of_key); | 212 | 989 | return ((KeyType*)(container->_key_containers[sub_container_index])) | 213 | 989 | [index_in_sub_container]; | 214 | 989 | } |
_ZN5doris22AggregateDataContainer12IteratorBaseINS0_8IteratorELb0EE7get_keyINS_7UInt136EEET_v Line | Count | Source | 210 | 2.07k | KeyType get_key() { | 211 | | DCHECK_EQ(sizeof(KeyType), container->_size_of_key); | 212 | 2.07k | return ((KeyType*)(container->_key_containers[sub_container_index])) | 213 | 2.07k | [index_in_sub_container]; | 214 | 2.07k | } |
|
215 | | |
216 | 4.12M | AggregateDataPtr get_aggregate_data() { |
217 | 4.12M | return &(container->_value_containers[sub_container_index] |
218 | 4.12M | [container->_size_of_aggregate_states * |
219 | 4.12M | index_in_sub_container]); |
220 | 4.12M | } |
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 | 56.5k | Iterator begin() { return {this, 0}; } |
238 | | |
239 | | ConstIterator end() const { return {this, _total_count}; } |
240 | | ConstIterator cend() const { return end(); } |
241 | 4.37M | Iterator end() { return {this, _total_count}; } |
242 | | |
243 | 12 | [[nodiscard]] uint32_t total_count() const { return _total_count; } |
244 | | |
245 | 27 | size_t estimate_memory(size_t rows) const { |
246 | 27 | bool need_to_expand = false; |
247 | 27 | if (_total_count == 0) { |
248 | 1 | need_to_expand = true; |
249 | 26 | } else if ((_index_in_sub_container + rows) > SUB_CONTAINER_CAPACITY) { |
250 | 10 | need_to_expand = true; |
251 | 10 | rows -= (SUB_CONTAINER_CAPACITY - _index_in_sub_container); |
252 | 10 | } |
253 | | |
254 | 27 | if (!need_to_expand) { |
255 | 16 | return 0; |
256 | 16 | } |
257 | | |
258 | 11 | size_t count = (rows + SUB_CONTAINER_CAPACITY - 1) / SUB_CONTAINER_CAPACITY; |
259 | 11 | size_t size = _size_of_key * SUB_CONTAINER_CAPACITY; |
260 | 11 | size += _size_of_aggregate_states * SUB_CONTAINER_CAPACITY; |
261 | 11 | size *= count; |
262 | 11 | return size; |
263 | 27 | } |
264 | | |
265 | 58.4k | void init_once() { |
266 | 58.4k | if (_inited) { |
267 | 2.23k | return; |
268 | 2.23k | } |
269 | 56.1k | _inited = true; |
270 | 56.1k | iterator = begin(); |
271 | 56.1k | } |
272 | | Iterator iterator; |
273 | | |
274 | | private: |
275 | 20.9k | void _expand() { |
276 | 20.9k | _index_in_sub_container = 0; |
277 | 20.9k | _current_keys = nullptr; |
278 | 20.9k | _current_agg_data = nullptr; |
279 | 20.9k | try { |
280 | 20.9k | _current_keys = _arena_pool.alloc(_size_of_key * SUB_CONTAINER_CAPACITY); |
281 | 20.9k | _key_containers.emplace_back(_current_keys); |
282 | | |
283 | 20.9k | _current_agg_data = (AggregateDataPtr)_arena_pool.alloc(_size_of_aggregate_states * |
284 | 20.9k | SUB_CONTAINER_CAPACITY); |
285 | 20.9k | _value_containers.emplace_back(_current_agg_data); |
286 | 20.9k | } catch (...) { |
287 | 0 | if (_current_keys) { |
288 | 0 | _key_containers.pop_back(); |
289 | 0 | _current_keys = nullptr; |
290 | 0 | } |
291 | 0 | if (_current_agg_data) { |
292 | 0 | _value_containers.pop_back(); |
293 | 0 | _current_agg_data = nullptr; |
294 | 0 | } |
295 | 0 | throw; |
296 | 0 | } |
297 | 20.9k | } |
298 | | |
299 | | static constexpr uint32_t SUB_CONTAINER_CAPACITY = 8192; |
300 | | Arena _arena_pool; |
301 | | std::vector<char*> _key_containers; |
302 | | std::vector<AggregateDataPtr> _value_containers; |
303 | | AggregateDataPtr _current_agg_data = nullptr; |
304 | | char* _current_keys = nullptr; |
305 | | size_t _size_of_key {}; |
306 | | size_t _size_of_aggregate_states {}; |
307 | | uint32_t _index_in_sub_container {}; |
308 | | uint32_t _total_count {}; |
309 | | bool _inited = false; |
310 | | }; |
311 | | } // namespace doris |