/root/doris/be/src/olap/memtable.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include "olap/memtable.h" |
19 | | |
20 | | #include <fmt/format.h> |
21 | | #include <gen_cpp/olap_file.pb.h> |
22 | | #include <pdqsort.h> |
23 | | |
24 | | #include <algorithm> |
25 | | #include <limits> |
26 | | #include <string> |
27 | | #include <vector> |
28 | | |
29 | | #include "bvar/bvar.h" |
30 | | #include "common/config.h" |
31 | | #include "olap/memtable_memory_limiter.h" |
32 | | #include "olap/olap_define.h" |
33 | | #include "olap/tablet_schema.h" |
34 | | #include "runtime/descriptors.h" |
35 | | #include "runtime/exec_env.h" |
36 | | #include "runtime/thread_context.h" |
37 | | #include "tablet_meta.h" |
38 | | #include "util/debug_points.h" |
39 | | #include "util/runtime_profile.h" |
40 | | #include "util/stopwatch.hpp" |
41 | | #include "vec/aggregate_functions/aggregate_function_reader.h" |
42 | | #include "vec/aggregate_functions/aggregate_function_simple_factory.h" |
43 | | #include "vec/columns/column.h" |
44 | | |
45 | | namespace doris { |
46 | | |
47 | | bvar::Adder<int64_t> g_memtable_cnt("memtable_cnt"); |
48 | | |
49 | | using namespace ErrorCode; |
50 | | |
51 | | MemTable::MemTable(int64_t tablet_id, std::shared_ptr<TabletSchema> tablet_schema, |
52 | | const std::vector<SlotDescriptor*>* slot_descs, TupleDescriptor* tuple_desc, |
53 | | bool enable_unique_key_mow, PartialUpdateInfo* partial_update_info) |
54 | | : _mem_type(MemType::ACTIVE), |
55 | | _tablet_id(tablet_id), |
56 | | _enable_unique_key_mow(enable_unique_key_mow), |
57 | | _keys_type(tablet_schema->keys_type()), |
58 | | _tablet_schema(tablet_schema), |
59 | | _is_first_insertion(true), |
60 | | _agg_functions(tablet_schema->num_columns()), |
61 | | _offsets_of_aggregate_states(tablet_schema->num_columns()), |
62 | 15 | _total_size_of_aggregate_states(0) { |
63 | 15 | g_memtable_cnt << 1; |
64 | 15 | _query_thread_context.init_unlocked(); |
65 | 15 | _arena = std::make_unique<vectorized::Arena>(); |
66 | 15 | _vec_row_comparator = std::make_shared<RowInBlockComparator>(_tablet_schema); |
67 | 15 | _num_columns = _tablet_schema->num_columns(); |
68 | 15 | if (partial_update_info != nullptr) { |
69 | 15 | _is_partial_update = partial_update_info->is_partial_update; |
70 | 15 | if (_is_partial_update) { |
71 | 0 | _num_columns = partial_update_info->partial_update_input_columns.size(); |
72 | 0 | if (partial_update_info->is_schema_contains_auto_inc_column && |
73 | 0 | !partial_update_info->is_input_columns_contains_auto_inc_column) { |
74 | 0 | _is_partial_update_and_auto_inc = true; |
75 | 0 | _num_columns += 1; |
76 | 0 | } |
77 | 0 | } |
78 | 15 | } |
79 | | // TODO: Support ZOrderComparator in the future |
80 | 15 | _init_columns_offset_by_slot_descs(slot_descs, tuple_desc); |
81 | 15 | _mem_tracker = std::make_shared<MemTracker>(); |
82 | 15 | } |
83 | | |
84 | | void MemTable::_init_columns_offset_by_slot_descs(const std::vector<SlotDescriptor*>* slot_descs, |
85 | 15 | const TupleDescriptor* tuple_desc) { |
86 | 91 | for (auto slot_desc : *slot_descs) { |
87 | 91 | const auto& slots = tuple_desc->slots(); |
88 | 630 | for (int j = 0; j < slots.size(); ++j) { |
89 | 630 | if (slot_desc->id() == slots[j]->id()) { |
90 | 91 | _column_offset.emplace_back(j); |
91 | 91 | break; |
92 | 91 | } |
93 | 630 | } |
94 | 91 | } |
95 | 15 | if (_is_partial_update_and_auto_inc) { |
96 | 0 | _column_offset.emplace_back(_column_offset.size()); |
97 | 0 | } |
98 | 15 | } |
99 | | |
100 | 12 | void MemTable::_init_agg_functions(const vectorized::Block* block) { |
101 | 42 | for (uint32_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) { |
102 | 30 | vectorized::AggregateFunctionPtr function; |
103 | 30 | if (_keys_type == KeysType::UNIQUE_KEYS && _enable_unique_key_mow) { |
104 | | // In such table, non-key column's aggregation type is NONE, so we need to construct |
105 | | // the aggregate function manually. |
106 | 9 | function = vectorized::AggregateFunctionSimpleFactory::instance().get( |
107 | 9 | "replace_load", {block->get_data_type(cid)}, |
108 | 9 | block->get_data_type(cid)->is_nullable(), |
109 | 9 | BeExecVersionManager::get_newest_version()); |
110 | 21 | } else { |
111 | 21 | function = _tablet_schema->column(cid).get_aggregate_function( |
112 | 21 | vectorized::AGG_LOAD_SUFFIX, _tablet_schema->column(cid).get_be_exec_version()); |
113 | 21 | if (function == nullptr) { |
114 | 0 | LOG(WARNING) << "column get aggregate function failed, column=" |
115 | 0 | << _tablet_schema->column(cid).name(); |
116 | 0 | } |
117 | 21 | } |
118 | | |
119 | 30 | DCHECK(function != nullptr); |
120 | 30 | _agg_functions[cid] = function; |
121 | 30 | } |
122 | | |
123 | 42 | for (uint32_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) { |
124 | 30 | _offsets_of_aggregate_states[cid] = _total_size_of_aggregate_states; |
125 | 30 | _total_size_of_aggregate_states += _agg_functions[cid]->size_of_data(); |
126 | | |
127 | | // If not the last aggregate_state, we need pad it so that next aggregate_state will be aligned. |
128 | 30 | if (cid + 1 < _num_columns) { |
129 | 22 | size_t alignment_of_next_state = _agg_functions[cid + 1]->align_of_data(); |
130 | | |
131 | | /// Extend total_size to next alignment requirement |
132 | | /// Add padding by rounding up 'total_size_of_aggregate_states' to be a multiplier of alignment_of_next_state. |
133 | 22 | _total_size_of_aggregate_states = |
134 | 22 | (_total_size_of_aggregate_states + alignment_of_next_state - 1) / |
135 | 22 | alignment_of_next_state * alignment_of_next_state; |
136 | 22 | } |
137 | 30 | } |
138 | 12 | } |
139 | | |
140 | 15 | MemTable::~MemTable() { |
141 | 15 | SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_query_thread_context.query_mem_tracker); |
142 | 15 | { |
143 | 15 | SCOPED_CONSUME_MEM_TRACKER(_mem_tracker); |
144 | 15 | g_memtable_cnt << -1; |
145 | 15 | if (_keys_type != KeysType::DUP_KEYS) { |
146 | 35 | for (auto it = _row_in_blocks.begin(); it != _row_in_blocks.end(); it++) { |
147 | 20 | if (!(*it)->has_init_agg()) { |
148 | 20 | continue; |
149 | 20 | } |
150 | | // We should release agg_places here, because they are not released when a |
151 | | // load is canceled. |
152 | 0 | for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { |
153 | 0 | auto function = _agg_functions[i]; |
154 | 0 | DCHECK(function != nullptr); |
155 | 0 | function->destroy((*it)->agg_places(i)); |
156 | 0 | } |
157 | 0 | } |
158 | 15 | } |
159 | 15 | std::for_each(_row_in_blocks.begin(), _row_in_blocks.end(), |
160 | 15 | std::default_delete<RowInBlock>()); |
161 | | // Arena has to be destroyed after agg state, because some agg state's memory may be |
162 | | // allocated in arena. |
163 | 15 | _arena.reset(); |
164 | 15 | _vec_row_comparator.reset(); |
165 | 15 | _row_in_blocks.clear(); |
166 | 15 | _agg_functions.clear(); |
167 | 15 | _input_mutable_block.clear(); |
168 | 15 | _output_mutable_block.clear(); |
169 | 15 | } |
170 | 15 | if (_is_flush_success) { |
171 | | // If the memtable is flush success, then its memtracker's consumption should be 0 |
172 | 12 | if (_mem_tracker->consumption() != 0 && config::crash_in_memory_tracker_inaccurate) { |
173 | 0 | LOG(FATAL) << "memtable flush success but cosumption is not 0, it is " |
174 | 0 | << _mem_tracker->consumption(); |
175 | 0 | } |
176 | 12 | } |
177 | 15 | } |
178 | | |
179 | 6 | int RowInBlockComparator::operator()(const RowInBlock* left, const RowInBlock* right) const { |
180 | 6 | return _pblock->compare_at(left->_row_pos, right->_row_pos, _tablet_schema->num_key_columns(), |
181 | 6 | *_pblock, -1); |
182 | 6 | } |
183 | | |
184 | | Status MemTable::insert(const vectorized::Block* input_block, |
185 | 20 | const std::vector<uint32_t>& row_idxs) { |
186 | 20 | SCOPED_CONSUME_MEM_TRACKER(_mem_tracker); |
187 | 20 | if (_is_first_insertion) { |
188 | 12 | _is_first_insertion = false; |
189 | 12 | auto clone_block = input_block->clone_without_columns(&_column_offset); |
190 | 12 | _input_mutable_block = vectorized::MutableBlock::build_mutable_block(&clone_block); |
191 | 12 | _vec_row_comparator->set_block(&_input_mutable_block); |
192 | 12 | _output_mutable_block = vectorized::MutableBlock::build_mutable_block(&clone_block); |
193 | 12 | if (_keys_type != KeysType::DUP_KEYS) { |
194 | | // there may be additional intermediate columns in input_block |
195 | | // we only need columns indicated by column offset in the output |
196 | 12 | RETURN_IF_CATCH_EXCEPTION(_init_agg_functions(&clone_block)); |
197 | 12 | } |
198 | 12 | if (_tablet_schema->has_sequence_col()) { |
199 | 7 | if (_is_partial_update) { |
200 | | // for unique key partial update, sequence column index in block |
201 | | // may be different with the index in `_tablet_schema` |
202 | 0 | for (size_t i = 0; i < clone_block.columns(); i++) { |
203 | 0 | if (clone_block.get_by_position(i).name == SEQUENCE_COL) { |
204 | 0 | _seq_col_idx_in_block = i; |
205 | 0 | break; |
206 | 0 | } |
207 | 0 | } |
208 | 7 | } else { |
209 | 7 | _seq_col_idx_in_block = _tablet_schema->sequence_col_idx(); |
210 | 7 | } |
211 | 7 | } |
212 | 12 | } |
213 | | |
214 | 20 | auto num_rows = row_idxs.size(); |
215 | 20 | size_t cursor_in_mutableblock = _input_mutable_block.rows(); |
216 | 20 | RETURN_IF_ERROR(_input_mutable_block.add_rows(input_block, row_idxs.data(), |
217 | 20 | row_idxs.data() + num_rows, &_column_offset)); |
218 | 40 | for (int i = 0; i < num_rows; i++) { |
219 | 20 | _row_in_blocks.emplace_back(new RowInBlock {cursor_in_mutableblock + i}); |
220 | 20 | } |
221 | | |
222 | 20 | _stat.raw_rows += num_rows; |
223 | 20 | return Status::OK(); |
224 | 20 | } |
225 | | |
226 | | void MemTable::_aggregate_two_row_in_block(vectorized::MutableBlock& mutable_block, |
227 | 3 | RowInBlock* src_row, RowInBlock* dst_row) { |
228 | 3 | if (_tablet_schema->has_sequence_col() && _seq_col_idx_in_block >= 0) { |
229 | 3 | DCHECK_LT(_seq_col_idx_in_block, mutable_block.columns()); |
230 | 3 | auto col_ptr = mutable_block.mutable_columns()[_seq_col_idx_in_block].get(); |
231 | 3 | auto res = col_ptr->compare_at(dst_row->_row_pos, src_row->_row_pos, *col_ptr, -1); |
232 | | // dst sequence column larger than src, don't need to update |
233 | 3 | if (res > 0) { |
234 | 3 | return; |
235 | 3 | } |
236 | | // need to update the row pos in dst row to the src row pos when has |
237 | | // sequence column |
238 | 0 | dst_row->_row_pos = src_row->_row_pos; |
239 | 0 | } |
240 | | // dst is non-sequence row, or dst sequence is smaller |
241 | 0 | for (uint32_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) { |
242 | 0 | auto col_ptr = mutable_block.mutable_columns()[cid].get(); |
243 | 0 | _agg_functions[cid]->add(dst_row->agg_places(cid), |
244 | 0 | const_cast<const doris::vectorized::IColumn**>(&col_ptr), |
245 | 0 | src_row->_row_pos, _arena.get()); |
246 | 0 | } |
247 | 0 | } |
248 | 9 | Status MemTable::_put_into_output(vectorized::Block& in_block) { |
249 | 9 | SCOPED_RAW_TIMER(&_stat.put_into_output_ns); |
250 | 9 | std::vector<uint32_t> row_pos_vec; |
251 | 9 | DCHECK(in_block.rows() <= std::numeric_limits<int>::max()); |
252 | 9 | row_pos_vec.reserve(in_block.rows()); |
253 | 20 | for (int i = 0; i < _row_in_blocks.size(); i++) { |
254 | 11 | row_pos_vec.emplace_back(_row_in_blocks[i]->_row_pos); |
255 | 11 | } |
256 | 9 | return _output_mutable_block.add_rows(&in_block, row_pos_vec.data(), |
257 | 9 | row_pos_vec.data() + in_block.rows()); |
258 | 9 | } |
259 | | |
260 | 12 | size_t MemTable::_sort() { |
261 | 12 | SCOPED_RAW_TIMER(&_stat.sort_ns); |
262 | 12 | _stat.sort_times++; |
263 | 12 | size_t same_keys_num = 0; |
264 | | // sort new rows |
265 | 12 | Tie tie = Tie(_last_sorted_pos, _row_in_blocks.size()); |
266 | 43 | for (size_t i = 0; i < _tablet_schema->num_key_columns(); i++) { |
267 | 31 | auto cmp = [&](const RowInBlock* lhs, const RowInBlock* rhs) -> int { |
268 | 30 | return _input_mutable_block.compare_one_column(lhs->_row_pos, rhs->_row_pos, i, -1); |
269 | 30 | }; |
270 | 31 | _sort_one_column(_row_in_blocks, tie, cmp); |
271 | 31 | } |
272 | 12 | bool is_dup = (_keys_type == KeysType::DUP_KEYS); |
273 | | // sort extra round by _row_pos to make the sort stable |
274 | 12 | auto iter = tie.iter(); |
275 | 15 | while (iter.next()) { |
276 | 3 | pdqsort(std::next(_row_in_blocks.begin(), iter.left()), |
277 | 3 | std::next(_row_in_blocks.begin(), iter.right()), |
278 | 3 | [&is_dup](const RowInBlock* lhs, const RowInBlock* rhs) -> bool { |
279 | 3 | return is_dup ? lhs->_row_pos > rhs->_row_pos : lhs->_row_pos < rhs->_row_pos; |
280 | 3 | }); |
281 | 3 | same_keys_num += iter.right() - iter.left(); |
282 | 3 | } |
283 | | // merge new rows and old rows |
284 | 12 | _vec_row_comparator->set_block(&_input_mutable_block); |
285 | 12 | auto cmp_func = [this, is_dup, &same_keys_num](const RowInBlock* l, |
286 | 12 | const RowInBlock* r) -> bool { |
287 | 0 | auto value = (*(this->_vec_row_comparator))(l, r); |
288 | 0 | if (value == 0) { |
289 | 0 | same_keys_num++; |
290 | 0 | return is_dup ? l->_row_pos > r->_row_pos : l->_row_pos < r->_row_pos; |
291 | 0 | } else { |
292 | 0 | return value < 0; |
293 | 0 | } |
294 | 0 | }; |
295 | 12 | auto new_row_it = std::next(_row_in_blocks.begin(), _last_sorted_pos); |
296 | 12 | std::inplace_merge(_row_in_blocks.begin(), new_row_it, _row_in_blocks.end(), cmp_func); |
297 | 12 | _last_sorted_pos = _row_in_blocks.size(); |
298 | 12 | return same_keys_num; |
299 | 12 | } |
300 | | |
301 | 1 | Status MemTable::_sort_by_cluster_keys() { |
302 | 1 | SCOPED_RAW_TIMER(&_stat.sort_ns); |
303 | 1 | _stat.sort_times++; |
304 | | // sort all rows |
305 | 1 | vectorized::Block in_block = _output_mutable_block.to_block(); |
306 | 1 | vectorized::MutableBlock mutable_block = |
307 | 1 | vectorized::MutableBlock::build_mutable_block(&in_block); |
308 | 1 | auto clone_block = in_block.clone_without_columns(); |
309 | 1 | _output_mutable_block = vectorized::MutableBlock::build_mutable_block(&clone_block); |
310 | | |
311 | 1 | std::vector<RowInBlock*> row_in_blocks; |
312 | 1 | std::unique_ptr<int, std::function<void(int*)>> row_in_blocks_deleter((int*)0x01, [&](int*) { |
313 | 1 | std::for_each(row_in_blocks.begin(), row_in_blocks.end(), |
314 | 1 | std::default_delete<RowInBlock>()); |
315 | 1 | }); |
316 | 1 | row_in_blocks.reserve(mutable_block.rows()); |
317 | 5 | for (size_t i = 0; i < mutable_block.rows(); i++) { |
318 | 4 | row_in_blocks.emplace_back(new RowInBlock {i}); |
319 | 4 | } |
320 | 1 | Tie tie = Tie(0, mutable_block.rows()); |
321 | | |
322 | 2 | for (auto cid : _tablet_schema->cluster_key_idxes()) { |
323 | 2 | auto index = _tablet_schema->field_index(cid); |
324 | 2 | if (index == -1) { |
325 | 0 | return Status::InternalError("could not find cluster key column with unique_id=" + |
326 | 0 | std::to_string(cid) + " in tablet schema"); |
327 | 0 | } |
328 | 8 | auto cmp = [&](const RowInBlock* lhs, const RowInBlock* rhs) -> int { |
329 | 8 | return mutable_block.compare_one_column(lhs->_row_pos, rhs->_row_pos, index, -1); |
330 | 8 | }; |
331 | 2 | _sort_one_column(row_in_blocks, tie, cmp); |
332 | 2 | } |
333 | | |
334 | | // sort extra round by _row_pos to make the sort stable |
335 | 1 | auto iter = tie.iter(); |
336 | 1 | while (iter.next()) { |
337 | 0 | pdqsort(std::next(row_in_blocks.begin(), iter.left()), |
338 | 0 | std::next(row_in_blocks.begin(), iter.right()), |
339 | 0 | [](const RowInBlock* lhs, const RowInBlock* rhs) -> bool { |
340 | 0 | return lhs->_row_pos < rhs->_row_pos; |
341 | 0 | }); |
342 | 0 | } |
343 | | |
344 | 1 | in_block = mutable_block.to_block(); |
345 | 1 | SCOPED_RAW_TIMER(&_stat.put_into_output_ns); |
346 | 1 | std::vector<uint32_t> row_pos_vec; |
347 | 1 | DCHECK(in_block.rows() <= std::numeric_limits<int>::max()); |
348 | 1 | row_pos_vec.reserve(in_block.rows()); |
349 | 5 | for (int i = 0; i < row_in_blocks.size(); i++) { |
350 | 4 | row_pos_vec.emplace_back(row_in_blocks[i]->_row_pos); |
351 | 4 | } |
352 | 1 | return _output_mutable_block.add_rows(&in_block, row_pos_vec.data(), |
353 | 1 | row_pos_vec.data() + in_block.rows(), &_column_offset); |
354 | 1 | } |
355 | | |
356 | | void MemTable::_sort_one_column(std::vector<RowInBlock*>& row_in_blocks, Tie& tie, |
357 | 33 | std::function<int(const RowInBlock*, const RowInBlock*)> cmp) { |
358 | 33 | auto iter = tie.iter(); |
359 | 43 | while (iter.next()) { |
360 | 10 | pdqsort(std::next(row_in_blocks.begin(), iter.left()), |
361 | 10 | std::next(row_in_blocks.begin(), iter.right()), |
362 | 21 | [&cmp](auto lhs, auto rhs) -> bool { return cmp(lhs, rhs) < 0; }); |
363 | 10 | tie[iter.left()] = 0; |
364 | 27 | for (int i = iter.left() + 1; i < iter.right(); i++) { |
365 | 17 | tie[i] = (cmp(row_in_blocks[i - 1], row_in_blocks[i]) == 0); |
366 | 17 | } |
367 | 10 | } |
368 | 33 | } |
369 | | |
370 | | template <bool is_final> |
371 | | void MemTable::_finalize_one_row(RowInBlock* row, |
372 | | const vectorized::ColumnsWithTypeAndName& block_data, |
373 | 6 | int row_pos) { |
374 | | // move key columns |
375 | 18 | for (size_t i = 0; i < _tablet_schema->num_key_columns(); ++i) { |
376 | 12 | _output_mutable_block.get_column_by_position(i)->insert_from(*block_data[i].column.get(), |
377 | 12 | row->_row_pos); |
378 | 12 | } |
379 | 6 | if (row->has_init_agg()) { |
380 | | // get value columns from agg_places |
381 | 12 | for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { |
382 | 9 | auto function = _agg_functions[i]; |
383 | 9 | auto* agg_place = row->agg_places(i); |
384 | 9 | auto* col_ptr = _output_mutable_block.get_column_by_position(i).get(); |
385 | 9 | function->insert_result_into(agg_place, *col_ptr); |
386 | | |
387 | 9 | if constexpr (is_final) { |
388 | 0 | function->destroy(agg_place); |
389 | 0 | } else { |
390 | 0 | function->reset(agg_place); |
391 | 0 | } |
392 | 9 | } |
393 | | |
394 | 3 | if constexpr (is_final) { |
395 | 0 | row->remove_init_agg(); |
396 | 0 | } else { |
397 | 0 | for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { |
398 | 0 | auto function = _agg_functions[i]; |
399 | 0 | auto* agg_place = row->agg_places(i); |
400 | 0 | auto* col_ptr = _output_mutable_block.get_column_by_position(i).get(); |
401 | 0 | function->add(agg_place, const_cast<const doris::vectorized::IColumn**>(&col_ptr), |
402 | 0 | row_pos, _arena.get()); |
403 | 0 | } |
404 | 0 | } |
405 | 3 | } else { |
406 | | // move columns for rows do not need agg |
407 | 12 | for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { |
408 | 9 | _output_mutable_block.get_column_by_position(i)->insert_from( |
409 | 9 | *block_data[i].column.get(), row->_row_pos); |
410 | 9 | } |
411 | 3 | } |
412 | 6 | if constexpr (!is_final) { |
413 | 0 | row->_row_pos = row_pos; |
414 | 0 | } |
415 | 6 | } Unexecuted instantiation: _ZN5doris8MemTable17_finalize_one_rowILb0EEEvPNS_10RowInBlockERKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS6_EEi _ZN5doris8MemTable17_finalize_one_rowILb1EEEvPNS_10RowInBlockERKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS6_EEi Line | Count | Source | 373 | 6 | int row_pos) { | 374 | | // move key columns | 375 | 18 | for (size_t i = 0; i < _tablet_schema->num_key_columns(); ++i) { | 376 | 12 | _output_mutable_block.get_column_by_position(i)->insert_from(*block_data[i].column.get(), | 377 | 12 | row->_row_pos); | 378 | 12 | } | 379 | 6 | if (row->has_init_agg()) { | 380 | | // get value columns from agg_places | 381 | 12 | for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { | 382 | 9 | auto function = _agg_functions[i]; | 383 | 9 | auto* agg_place = row->agg_places(i); | 384 | 9 | auto* col_ptr = _output_mutable_block.get_column_by_position(i).get(); | 385 | 9 | function->insert_result_into(agg_place, *col_ptr); | 386 | | | 387 | 9 | if constexpr (is_final) { | 388 | 9 | function->destroy(agg_place); | 389 | 9 | } else { | 390 | 9 | function->reset(agg_place); | 391 | 9 | } | 392 | 9 | } | 393 | | | 394 | 3 | if constexpr (is_final) { | 395 | 3 | row->remove_init_agg(); | 396 | 3 | } else { | 397 | 3 | for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { | 398 | 3 | auto function = _agg_functions[i]; | 399 | 3 | auto* agg_place = row->agg_places(i); | 400 | 3 | auto* col_ptr = _output_mutable_block.get_column_by_position(i).get(); | 401 | 3 | function->add(agg_place, const_cast<const doris::vectorized::IColumn**>(&col_ptr), | 402 | 3 | row_pos, _arena.get()); | 403 | 3 | } | 404 | 3 | } | 405 | 3 | } else { | 406 | | // move columns for rows do not need agg | 407 | 12 | for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { | 408 | 9 | _output_mutable_block.get_column_by_position(i)->insert_from( | 409 | 9 | *block_data[i].column.get(), row->_row_pos); | 410 | 9 | } | 411 | 3 | } | 412 | 6 | if constexpr (!is_final) { | 413 | 6 | row->_row_pos = row_pos; | 414 | 6 | } | 415 | 6 | } |
|
416 | | |
417 | | template <bool is_final> |
418 | 3 | void MemTable::_aggregate() { |
419 | 3 | SCOPED_RAW_TIMER(&_stat.agg_ns); |
420 | 3 | _stat.agg_times++; |
421 | 3 | vectorized::Block in_block = _input_mutable_block.to_block(); |
422 | 3 | vectorized::MutableBlock mutable_block = |
423 | 3 | vectorized::MutableBlock::build_mutable_block(&in_block); |
424 | 3 | _vec_row_comparator->set_block(&mutable_block); |
425 | 3 | auto& block_data = in_block.get_columns_with_type_and_name(); |
426 | 3 | std::vector<RowInBlock*> temp_row_in_blocks; |
427 | 3 | temp_row_in_blocks.reserve(_last_sorted_pos); |
428 | 3 | RowInBlock* prev_row = nullptr; |
429 | 3 | int row_pos = -1; |
430 | | //only init agg if needed |
431 | 12 | for (int i = 0; i < _row_in_blocks.size(); i++) { |
432 | 9 | if (!temp_row_in_blocks.empty() && |
433 | 9 | (*_vec_row_comparator)(prev_row, _row_in_blocks[i]) == 0) { |
434 | 3 | if (!prev_row->has_init_agg()) { |
435 | 3 | prev_row->init_agg_places( |
436 | 3 | _arena->aligned_alloc(_total_size_of_aggregate_states, 16), |
437 | 3 | _offsets_of_aggregate_states.data()); |
438 | 12 | for (auto cid = _tablet_schema->num_key_columns(); cid < _num_columns; cid++) { |
439 | 9 | auto col_ptr = mutable_block.mutable_columns()[cid].get(); |
440 | 9 | auto data = prev_row->agg_places(cid); |
441 | 9 | _agg_functions[cid]->create(data); |
442 | 9 | _agg_functions[cid]->add( |
443 | 9 | data, const_cast<const doris::vectorized::IColumn**>(&col_ptr), |
444 | 9 | prev_row->_row_pos, _arena.get()); |
445 | 9 | } |
446 | 3 | } |
447 | 3 | _stat.merged_rows++; |
448 | 3 | _aggregate_two_row_in_block(mutable_block, _row_in_blocks[i], prev_row); |
449 | 6 | } else { |
450 | 6 | prev_row = _row_in_blocks[i]; |
451 | 6 | if (!temp_row_in_blocks.empty()) { |
452 | | // no more rows to merge for prev row, finalize it |
453 | 3 | _finalize_one_row<is_final>(temp_row_in_blocks.back(), block_data, row_pos); |
454 | 3 | } |
455 | 6 | temp_row_in_blocks.push_back(prev_row); |
456 | 6 | row_pos++; |
457 | 6 | } |
458 | 9 | } |
459 | 3 | if (!temp_row_in_blocks.empty()) { |
460 | | // finalize the last low |
461 | 3 | _finalize_one_row<is_final>(temp_row_in_blocks.back(), block_data, row_pos); |
462 | 3 | } |
463 | 3 | if constexpr (!is_final) { |
464 | | // if is not final, we collect the agg results to input_block and then continue to insert |
465 | 0 | _input_mutable_block.swap(_output_mutable_block); |
466 | | //TODO(weixang):opt here. |
467 | 0 | std::unique_ptr<vectorized::Block> empty_input_block = in_block.create_same_struct_block(0); |
468 | 0 | _output_mutable_block = |
469 | 0 | vectorized::MutableBlock::build_mutable_block(empty_input_block.get()); |
470 | 0 | _output_mutable_block.clear_column_data(); |
471 | 0 | _row_in_blocks = temp_row_in_blocks; |
472 | 0 | _last_sorted_pos = _row_in_blocks.size(); |
473 | 0 | } |
474 | 3 | } Unexecuted instantiation: _ZN5doris8MemTable10_aggregateILb0EEEvv _ZN5doris8MemTable10_aggregateILb1EEEvv Line | Count | Source | 418 | 3 | void MemTable::_aggregate() { | 419 | 3 | SCOPED_RAW_TIMER(&_stat.agg_ns); | 420 | 3 | _stat.agg_times++; | 421 | 3 | vectorized::Block in_block = _input_mutable_block.to_block(); | 422 | 3 | vectorized::MutableBlock mutable_block = | 423 | 3 | vectorized::MutableBlock::build_mutable_block(&in_block); | 424 | 3 | _vec_row_comparator->set_block(&mutable_block); | 425 | 3 | auto& block_data = in_block.get_columns_with_type_and_name(); | 426 | 3 | std::vector<RowInBlock*> temp_row_in_blocks; | 427 | 3 | temp_row_in_blocks.reserve(_last_sorted_pos); | 428 | 3 | RowInBlock* prev_row = nullptr; | 429 | 3 | int row_pos = -1; | 430 | | //only init agg if needed | 431 | 12 | for (int i = 0; i < _row_in_blocks.size(); i++) { | 432 | 9 | if (!temp_row_in_blocks.empty() && | 433 | 9 | (*_vec_row_comparator)(prev_row, _row_in_blocks[i]) == 0) { | 434 | 3 | if (!prev_row->has_init_agg()) { | 435 | 3 | prev_row->init_agg_places( | 436 | 3 | _arena->aligned_alloc(_total_size_of_aggregate_states, 16), | 437 | 3 | _offsets_of_aggregate_states.data()); | 438 | 12 | for (auto cid = _tablet_schema->num_key_columns(); cid < _num_columns; cid++) { | 439 | 9 | auto col_ptr = mutable_block.mutable_columns()[cid].get(); | 440 | 9 | auto data = prev_row->agg_places(cid); | 441 | 9 | _agg_functions[cid]->create(data); | 442 | 9 | _agg_functions[cid]->add( | 443 | 9 | data, const_cast<const doris::vectorized::IColumn**>(&col_ptr), | 444 | 9 | prev_row->_row_pos, _arena.get()); | 445 | 9 | } | 446 | 3 | } | 447 | 3 | _stat.merged_rows++; | 448 | 3 | _aggregate_two_row_in_block(mutable_block, _row_in_blocks[i], prev_row); | 449 | 6 | } else { | 450 | 6 | prev_row = _row_in_blocks[i]; | 451 | 6 | if (!temp_row_in_blocks.empty()) { | 452 | | // no more rows to merge for prev row, finalize it | 453 | 3 | _finalize_one_row<is_final>(temp_row_in_blocks.back(), block_data, row_pos); | 454 | 3 | } | 455 | 6 | temp_row_in_blocks.push_back(prev_row); | 456 | 6 | row_pos++; | 457 | 6 | } | 458 | 9 | } | 459 | 3 | if (!temp_row_in_blocks.empty()) { | 460 | | // finalize the last low | 461 | 3 | _finalize_one_row<is_final>(temp_row_in_blocks.back(), block_data, row_pos); | 462 | 3 | } | 463 | 3 | if constexpr (!is_final) { | 464 | | // if is not final, we collect the agg results to input_block and then continue to insert | 465 | 3 | _input_mutable_block.swap(_output_mutable_block); | 466 | | //TODO(weixang):opt here. | 467 | 3 | std::unique_ptr<vectorized::Block> empty_input_block = in_block.create_same_struct_block(0); | 468 | 3 | _output_mutable_block = | 469 | 3 | vectorized::MutableBlock::build_mutable_block(empty_input_block.get()); | 470 | 3 | _output_mutable_block.clear_column_data(); | 471 | 3 | _row_in_blocks = temp_row_in_blocks; | 472 | 3 | _last_sorted_pos = _row_in_blocks.size(); | 473 | 3 | } | 474 | 3 | } |
|
475 | | |
476 | 0 | void MemTable::shrink_memtable_by_agg() { |
477 | 0 | SCOPED_CONSUME_MEM_TRACKER(_mem_tracker); |
478 | 0 | if (_keys_type == KeysType::DUP_KEYS) { |
479 | 0 | return; |
480 | 0 | } |
481 | 0 | size_t same_keys_num = _sort(); |
482 | 0 | if (same_keys_num != 0) { |
483 | 0 | _aggregate<false>(); |
484 | 0 | } |
485 | 0 | } |
486 | | |
487 | 20 | bool MemTable::need_flush() const { |
488 | 20 | DBUG_EXECUTE_IF("MemTable.need_flush", { return true; }); |
489 | 20 | auto max_size = config::write_buffer_size; |
490 | 20 | if (_is_partial_update) { |
491 | 0 | auto update_columns_size = _num_columns; |
492 | 0 | max_size = max_size * update_columns_size / _tablet_schema->num_columns(); |
493 | 0 | max_size = max_size > 1048576 ? max_size : 1048576; |
494 | 0 | } |
495 | 20 | return memory_usage() >= max_size; |
496 | 20 | } |
497 | | |
498 | 20 | bool MemTable::need_agg() const { |
499 | 20 | if (_keys_type == KeysType::AGG_KEYS) { |
500 | 5 | auto max_size = config::write_buffer_size_for_agg; |
501 | 5 | return memory_usage() >= max_size; |
502 | 5 | } |
503 | 15 | return false; |
504 | 20 | } |
505 | | |
506 | 12 | Status MemTable::_to_block(std::unique_ptr<vectorized::Block>* res) { |
507 | 12 | size_t same_keys_num = _sort(); |
508 | 12 | if (_keys_type == KeysType::DUP_KEYS || same_keys_num == 0) { |
509 | 9 | if (_keys_type == KeysType::DUP_KEYS && _tablet_schema->num_key_columns() == 0) { |
510 | 0 | _output_mutable_block.swap(_input_mutable_block); |
511 | 9 | } else { |
512 | 9 | vectorized::Block in_block = _input_mutable_block.to_block(); |
513 | 9 | RETURN_IF_ERROR(_put_into_output(in_block)); |
514 | 9 | } |
515 | 9 | } else { |
516 | 3 | _aggregate<true>(); |
517 | 3 | } |
518 | 12 | if (_keys_type == KeysType::UNIQUE_KEYS && _enable_unique_key_mow && |
519 | 12 | !_tablet_schema->cluster_key_idxes().empty()) { |
520 | 1 | if (_is_partial_update) { |
521 | 0 | return Status::InternalError( |
522 | 0 | "Partial update for mow with cluster keys is not supported"); |
523 | 0 | } |
524 | 1 | RETURN_IF_ERROR(_sort_by_cluster_keys()); |
525 | 1 | } |
526 | 12 | _input_mutable_block.clear(); |
527 | 12 | *res = vectorized::Block::create_unique(_output_mutable_block.to_block()); |
528 | 12 | return Status::OK(); |
529 | 12 | } |
530 | | |
531 | 12 | Status MemTable::to_block(std::unique_ptr<vectorized::Block>* res) { |
532 | 12 | RETURN_IF_ERROR_OR_CATCH_EXCEPTION(_to_block(res)); |
533 | 12 | return Status::OK(); |
534 | 12 | } |
535 | | |
536 | | } // namespace doris |