/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 "util/runtime_profile.h" |
38 | | #include "util/stopwatch.hpp" |
39 | | #include "vec/aggregate_functions/aggregate_function_reader.h" |
40 | | #include "vec/aggregate_functions/aggregate_function_simple_factory.h" |
41 | | #include "vec/columns/column.h" |
42 | | |
43 | | namespace doris { |
44 | | |
45 | | bvar::Adder<int64_t> g_memtable_cnt("memtable_cnt"); |
46 | | bvar::Adder<int64_t> g_memtable_input_block_allocated_size("memtable_input_block_allocated_size"); |
47 | | |
48 | | using namespace ErrorCode; |
49 | | |
50 | | MemTable::MemTable(int64_t tablet_id, std::shared_ptr<TabletSchema> tablet_schema, |
51 | | const std::vector<SlotDescriptor*>* slot_descs, TupleDescriptor* tuple_desc, |
52 | | bool enable_unique_key_mow, PartialUpdateInfo* partial_update_info) |
53 | | : _mem_type(MemType::ACTIVE), |
54 | | _tablet_id(tablet_id), |
55 | | _enable_unique_key_mow(enable_unique_key_mow), |
56 | | _keys_type(tablet_schema->keys_type()), |
57 | | _tablet_schema(tablet_schema), |
58 | | _is_first_insertion(true), |
59 | | _agg_functions(tablet_schema->num_columns()), |
60 | | _offsets_of_aggregate_states(tablet_schema->num_columns()), |
61 | 12 | _total_size_of_aggregate_states(0) { |
62 | 12 | g_memtable_cnt << 1; |
63 | 12 | _query_thread_context.init_unlocked(); |
64 | 12 | _arena = std::make_unique<vectorized::Arena>(); |
65 | 12 | _vec_row_comparator = std::make_shared<RowInBlockComparator>(_tablet_schema); |
66 | 12 | _num_columns = _tablet_schema->num_columns(); |
67 | 12 | if (partial_update_info != nullptr) { |
68 | 12 | _partial_update_mode = partial_update_info->update_mode(); |
69 | 12 | if (_partial_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS) { |
70 | 0 | _num_columns = partial_update_info->partial_update_input_columns.size(); |
71 | 0 | if (partial_update_info->is_schema_contains_auto_inc_column && |
72 | 0 | !partial_update_info->is_input_columns_contains_auto_inc_column) { |
73 | 0 | _is_partial_update_and_auto_inc = true; |
74 | 0 | _num_columns += 1; |
75 | 0 | } |
76 | 0 | } |
77 | 12 | } |
78 | | // TODO: Support ZOrderComparator in the future |
79 | 12 | _init_columns_offset_by_slot_descs(slot_descs, tuple_desc); |
80 | 12 | _mem_tracker = std::make_shared<MemTracker>(); |
81 | 12 | } |
82 | | |
83 | | void MemTable::_init_columns_offset_by_slot_descs(const std::vector<SlotDescriptor*>* slot_descs, |
84 | 12 | const TupleDescriptor* tuple_desc) { |
85 | 88 | for (auto slot_desc : *slot_descs) { |
86 | 88 | const auto& slots = tuple_desc->slots(); |
87 | 627 | for (int j = 0; j < slots.size(); ++j) { |
88 | 627 | if (slot_desc->id() == slots[j]->id()) { |
89 | 88 | _column_offset.emplace_back(j); |
90 | 88 | break; |
91 | 88 | } |
92 | 627 | } |
93 | 88 | } |
94 | 12 | if (_is_partial_update_and_auto_inc) { |
95 | 0 | _column_offset.emplace_back(_column_offset.size()); |
96 | 0 | } |
97 | 12 | } |
98 | | |
99 | 9 | void MemTable::_init_agg_functions(const vectorized::Block* block) { |
100 | 39 | for (uint32_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) { |
101 | 30 | vectorized::AggregateFunctionPtr function; |
102 | 30 | if (_keys_type == KeysType::UNIQUE_KEYS && _enable_unique_key_mow) { |
103 | | // In such table, non-key column's aggregation type is NONE, so we need to construct |
104 | | // the aggregate function manually. |
105 | 9 | if (_skip_bitmap_col_idx != cid) { |
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 | 9 | } else { |
111 | 0 | function = vectorized::AggregateFunctionSimpleFactory::instance().get( |
112 | 0 | "bitmap_intersect", {block->get_data_type(cid)}, false, |
113 | 0 | BeExecVersionManager::get_newest_version()); |
114 | 0 | } |
115 | 21 | } else { |
116 | 21 | function = _tablet_schema->column(cid).get_aggregate_function( |
117 | 21 | vectorized::AGG_LOAD_SUFFIX, _tablet_schema->column(cid).get_be_exec_version()); |
118 | 21 | if (function == nullptr) { |
119 | 0 | LOG(WARNING) << "column get aggregate function failed, column=" |
120 | 0 | << _tablet_schema->column(cid).name(); |
121 | 0 | } |
122 | 21 | } |
123 | | |
124 | 30 | DCHECK(function != nullptr); |
125 | 30 | _agg_functions[cid] = function; |
126 | 30 | } |
127 | | |
128 | 39 | for (uint32_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) { |
129 | 30 | _offsets_of_aggregate_states[cid] = _total_size_of_aggregate_states; |
130 | 30 | _total_size_of_aggregate_states += _agg_functions[cid]->size_of_data(); |
131 | | |
132 | | // If not the last aggregate_state, we need pad it so that next aggregate_state will be aligned. |
133 | 30 | if (cid + 1 < _num_columns) { |
134 | 22 | size_t alignment_of_next_state = _agg_functions[cid + 1]->align_of_data(); |
135 | | |
136 | | /// Extend total_size to next alignment requirement |
137 | | /// Add padding by rounding up 'total_size_of_aggregate_states' to be a multiplier of alignment_of_next_state. |
138 | 22 | _total_size_of_aggregate_states = |
139 | 22 | (_total_size_of_aggregate_states + alignment_of_next_state - 1) / |
140 | 22 | alignment_of_next_state * alignment_of_next_state; |
141 | 22 | } |
142 | 30 | } |
143 | 9 | } |
144 | | |
145 | 12 | MemTable::~MemTable() { |
146 | 12 | SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_query_thread_context.query_mem_tracker); |
147 | 12 | if (_is_flush_success) { |
148 | | // If the memtable is flush success, then its memtracker's consumption should be 0 |
149 | 9 | if (_mem_tracker->consumption() != 0 && config::crash_in_memory_tracker_inaccurate) { |
150 | 0 | LOG(FATAL) << "memtable flush success but cosumption is not 0, it is " |
151 | 0 | << _mem_tracker->consumption(); |
152 | 0 | } |
153 | 9 | } |
154 | 12 | g_memtable_input_block_allocated_size << -_input_mutable_block.allocated_bytes(); |
155 | 12 | g_memtable_cnt << -1; |
156 | 12 | if (_keys_type != KeysType::DUP_KEYS) { |
157 | 29 | for (auto it = _row_in_blocks.begin(); it != _row_in_blocks.end(); it++) { |
158 | 17 | if (!(*it)->has_init_agg()) { |
159 | 17 | continue; |
160 | 17 | } |
161 | | // We should release agg_places here, because they are not released when a |
162 | | // load is canceled. |
163 | 0 | for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { |
164 | 0 | auto function = _agg_functions[i]; |
165 | 0 | DCHECK(function != nullptr); |
166 | 0 | function->destroy((*it)->agg_places(i)); |
167 | 0 | } |
168 | 0 | } |
169 | 12 | } |
170 | 12 | std::for_each(_row_in_blocks.begin(), _row_in_blocks.end(), std::default_delete<RowInBlock>()); |
171 | 12 | _arena.reset(); |
172 | 12 | _vec_row_comparator.reset(); |
173 | 12 | _row_in_blocks.clear(); |
174 | 12 | _agg_functions.clear(); |
175 | 12 | _input_mutable_block.clear(); |
176 | 12 | _output_mutable_block.clear(); |
177 | 12 | } |
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 | 17 | const std::vector<uint32_t>& row_idxs) { |
186 | 17 | SCOPED_CONSUME_MEM_TRACKER(_mem_tracker); |
187 | | |
188 | 17 | if (_is_first_insertion) { |
189 | 9 | _is_first_insertion = false; |
190 | 9 | auto clone_block = input_block->clone_without_columns(&_column_offset); |
191 | 9 | _input_mutable_block = vectorized::MutableBlock::build_mutable_block(&clone_block); |
192 | 9 | _vec_row_comparator->set_block(&_input_mutable_block); |
193 | 9 | _output_mutable_block = vectorized::MutableBlock::build_mutable_block(&clone_block); |
194 | 9 | if (_tablet_schema->has_sequence_col()) { |
195 | 7 | if (_partial_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS) { |
196 | | // for unique key fixed partial update, sequence column index in block |
197 | | // may be different with the index in `_tablet_schema` |
198 | 0 | for (size_t i = 0; i < clone_block.columns(); i++) { |
199 | 0 | if (clone_block.get_by_position(i).name == SEQUENCE_COL) { |
200 | 0 | _seq_col_idx_in_block = i; |
201 | 0 | break; |
202 | 0 | } |
203 | 0 | } |
204 | 7 | } else { |
205 | 7 | _seq_col_idx_in_block = _tablet_schema->sequence_col_idx(); |
206 | 7 | } |
207 | 7 | } |
208 | 9 | if (_partial_update_mode == UniqueKeyUpdateModePB::UPDATE_FLEXIBLE_COLUMNS && |
209 | 9 | _tablet_schema->has_skip_bitmap_col()) { |
210 | | // init of _skip_bitmap_col_idx must be before _init_agg_functions() |
211 | 0 | _skip_bitmap_col_idx = _tablet_schema->skip_bitmap_col_idx(); |
212 | 0 | if (_seq_col_idx_in_block != -1) { |
213 | 0 | _seq_col_unique_id = _tablet_schema->column(_seq_col_idx_in_block).unique_id(); |
214 | 0 | } |
215 | 0 | } |
216 | 9 | if (_keys_type != KeysType::DUP_KEYS) { |
217 | | // there may be additional intermediate columns in input_block |
218 | | // we only need columns indicated by column offset in the output |
219 | 9 | RETURN_IF_CATCH_EXCEPTION(_init_agg_functions(&clone_block)); |
220 | 9 | } |
221 | 9 | } |
222 | | |
223 | 17 | auto num_rows = row_idxs.size(); |
224 | 17 | size_t cursor_in_mutableblock = _input_mutable_block.rows(); |
225 | 17 | auto block_size0 = _input_mutable_block.allocated_bytes(); |
226 | 17 | RETURN_IF_ERROR(_input_mutable_block.add_rows(input_block, row_idxs.data(), |
227 | 17 | row_idxs.data() + num_rows, &_column_offset)); |
228 | 17 | auto block_size1 = _input_mutable_block.allocated_bytes(); |
229 | 17 | g_memtable_input_block_allocated_size << block_size1 - block_size0; |
230 | 34 | for (int i = 0; i < num_rows; i++) { |
231 | 17 | _row_in_blocks.emplace_back(new RowInBlock {cursor_in_mutableblock + i}); |
232 | 17 | } |
233 | | |
234 | 17 | _stat.raw_rows += num_rows; |
235 | 17 | return Status::OK(); |
236 | 17 | } |
237 | | |
238 | | template <bool has_skip_bitmap_col> |
239 | | void MemTable::_aggregate_two_row_in_block(vectorized::MutableBlock& mutable_block, |
240 | 3 | RowInBlock* src_row, RowInBlock* dst_row) { |
241 | | // for flexible partial update, the caller must guarantees that either src_row and dst_row |
242 | | // both specify the sequence column, or src_row and dst_row both don't specify the |
243 | | // sequence column |
244 | 3 | if (_tablet_schema->has_sequence_col() && _seq_col_idx_in_block >= 0) { |
245 | 3 | DCHECK_LT(_seq_col_idx_in_block, mutable_block.columns()); |
246 | 3 | auto col_ptr = mutable_block.mutable_columns()[_seq_col_idx_in_block].get(); |
247 | 3 | auto res = col_ptr->compare_at(dst_row->_row_pos, src_row->_row_pos, *col_ptr, -1); |
248 | | // dst sequence column larger than src, don't need to update |
249 | 3 | if (res > 0) { |
250 | 3 | return; |
251 | 3 | } |
252 | | // need to update the row pos in dst row to the src row pos when has |
253 | | // sequence column |
254 | 0 | dst_row->_row_pos = src_row->_row_pos; |
255 | 0 | } |
256 | | // dst is non-sequence row, or dst sequence is smaller |
257 | 0 | if constexpr (!has_skip_bitmap_col) { |
258 | 0 | DCHECK(_skip_bitmap_col_idx == -1); |
259 | 0 | for (uint32_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) { |
260 | 0 | auto* col_ptr = mutable_block.mutable_columns()[cid].get(); |
261 | 0 | _agg_functions[cid]->add(dst_row->agg_places(cid), |
262 | 0 | const_cast<const doris::vectorized::IColumn**>(&col_ptr), |
263 | 0 | src_row->_row_pos, _arena.get()); |
264 | 0 | } |
265 | 0 | } else { |
266 | 0 | DCHECK(_skip_bitmap_col_idx != -1); |
267 | 0 | DCHECK_LT(_skip_bitmap_col_idx, mutable_block.columns()); |
268 | 0 | const BitmapValue& skip_bitmap = |
269 | 0 | assert_cast<vectorized::ColumnBitmap*, TypeCheckOnRelease::DISABLE>( |
270 | 0 | mutable_block.mutable_columns()[_skip_bitmap_col_idx].get()) |
271 | 0 | ->get_data()[src_row->_row_pos]; |
272 | 0 | for (uint32_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) { |
273 | 0 | const auto& col = _tablet_schema->column(cid); |
274 | 0 | if (cid != _skip_bitmap_col_idx && skip_bitmap.contains(col.unique_id())) { |
275 | 0 | continue; |
276 | 0 | } |
277 | 0 | auto* col_ptr = mutable_block.mutable_columns()[cid].get(); |
278 | 0 | _agg_functions[cid]->add(dst_row->agg_places(cid), |
279 | 0 | const_cast<const doris::vectorized::IColumn**>(&col_ptr), |
280 | 0 | src_row->_row_pos, _arena.get()); |
281 | 0 | } |
282 | 0 | } |
283 | 0 | } _ZN5doris8MemTable27_aggregate_two_row_in_blockILb0EEEvRNS_10vectorized12MutableBlockEPNS_10RowInBlockES6_ Line | Count | Source | 240 | 3 | RowInBlock* src_row, RowInBlock* dst_row) { | 241 | | // for flexible partial update, the caller must guarantees that either src_row and dst_row | 242 | | // both specify the sequence column, or src_row and dst_row both don't specify the | 243 | | // sequence column | 244 | 3 | if (_tablet_schema->has_sequence_col() && _seq_col_idx_in_block >= 0) { | 245 | 3 | DCHECK_LT(_seq_col_idx_in_block, mutable_block.columns()); | 246 | 3 | auto col_ptr = mutable_block.mutable_columns()[_seq_col_idx_in_block].get(); | 247 | 3 | auto res = col_ptr->compare_at(dst_row->_row_pos, src_row->_row_pos, *col_ptr, -1); | 248 | | // dst sequence column larger than src, don't need to update | 249 | 3 | if (res > 0) { | 250 | 3 | return; | 251 | 3 | } | 252 | | // need to update the row pos in dst row to the src row pos when has | 253 | | // sequence column | 254 | 0 | dst_row->_row_pos = src_row->_row_pos; | 255 | 0 | } | 256 | | // dst is non-sequence row, or dst sequence is smaller | 257 | 0 | if constexpr (!has_skip_bitmap_col) { | 258 | 0 | DCHECK(_skip_bitmap_col_idx == -1); | 259 | 0 | for (uint32_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) { | 260 | 0 | auto* col_ptr = mutable_block.mutable_columns()[cid].get(); | 261 | 0 | _agg_functions[cid]->add(dst_row->agg_places(cid), | 262 | 0 | const_cast<const doris::vectorized::IColumn**>(&col_ptr), | 263 | 0 | src_row->_row_pos, _arena.get()); | 264 | 0 | } | 265 | 0 | } else { | 266 | 0 | DCHECK(_skip_bitmap_col_idx != -1); | 267 | 0 | DCHECK_LT(_skip_bitmap_col_idx, mutable_block.columns()); | 268 | 0 | const BitmapValue& skip_bitmap = | 269 | 0 | assert_cast<vectorized::ColumnBitmap*, TypeCheckOnRelease::DISABLE>( | 270 | 0 | mutable_block.mutable_columns()[_skip_bitmap_col_idx].get()) | 271 | 0 | ->get_data()[src_row->_row_pos]; | 272 | 0 | for (uint32_t cid = _tablet_schema->num_key_columns(); cid < _num_columns; ++cid) { | 273 | 0 | const auto& col = _tablet_schema->column(cid); | 274 | 0 | if (cid != _skip_bitmap_col_idx && skip_bitmap.contains(col.unique_id())) { | 275 | 0 | continue; | 276 | 0 | } | 277 | 0 | auto* col_ptr = mutable_block.mutable_columns()[cid].get(); | 278 | 0 | _agg_functions[cid]->add(dst_row->agg_places(cid), | 279 | 0 | const_cast<const doris::vectorized::IColumn**>(&col_ptr), | 280 | 0 | src_row->_row_pos, _arena.get()); | 281 | 0 | } | 282 | 0 | } | 283 | 0 | } |
Unexecuted instantiation: _ZN5doris8MemTable27_aggregate_two_row_in_blockILb1EEEvRNS_10vectorized12MutableBlockEPNS_10RowInBlockES6_ |
284 | 6 | Status MemTable::_put_into_output(vectorized::Block& in_block) { |
285 | 6 | SCOPED_RAW_TIMER(&_stat.put_into_output_ns); |
286 | 6 | std::vector<uint32_t> row_pos_vec; |
287 | 6 | DCHECK(in_block.rows() <= std::numeric_limits<int>::max()); |
288 | 6 | row_pos_vec.reserve(in_block.rows()); |
289 | 14 | for (int i = 0; i < _row_in_blocks.size(); i++) { |
290 | 8 | row_pos_vec.emplace_back(_row_in_blocks[i]->_row_pos); |
291 | 8 | } |
292 | 6 | return _output_mutable_block.add_rows(&in_block, row_pos_vec.data(), |
293 | 6 | row_pos_vec.data() + in_block.rows()); |
294 | 6 | } |
295 | | |
296 | 9 | size_t MemTable::_sort() { |
297 | 9 | SCOPED_RAW_TIMER(&_stat.sort_ns); |
298 | 9 | _stat.sort_times++; |
299 | 9 | size_t same_keys_num = 0; |
300 | | // sort new rows |
301 | 9 | Tie tie = Tie(_last_sorted_pos, _row_in_blocks.size()); |
302 | 37 | for (size_t i = 0; i < _tablet_schema->num_key_columns(); i++) { |
303 | 30 | auto cmp = [&](const RowInBlock* lhs, const RowInBlock* rhs) -> int { |
304 | 30 | return _input_mutable_block.compare_one_column(lhs->_row_pos, rhs->_row_pos, i, -1); |
305 | 30 | }; |
306 | 28 | _sort_one_column(_row_in_blocks, tie, cmp); |
307 | 28 | } |
308 | 9 | bool is_dup = (_keys_type == KeysType::DUP_KEYS); |
309 | | // sort extra round by _row_pos to make the sort stable |
310 | 9 | auto iter = tie.iter(); |
311 | 12 | while (iter.next()) { |
312 | 3 | pdqsort(std::next(_row_in_blocks.begin(), iter.left()), |
313 | 3 | std::next(_row_in_blocks.begin(), iter.right()), |
314 | 3 | [&is_dup](const RowInBlock* lhs, const RowInBlock* rhs) -> bool { |
315 | 3 | return is_dup ? lhs->_row_pos > rhs->_row_pos : lhs->_row_pos < rhs->_row_pos; |
316 | 3 | }); |
317 | 3 | same_keys_num += iter.right() - iter.left(); |
318 | 3 | } |
319 | | // merge new rows and old rows |
320 | 9 | _vec_row_comparator->set_block(&_input_mutable_block); |
321 | 9 | auto cmp_func = [this, is_dup, &same_keys_num](const RowInBlock* l, |
322 | 9 | const RowInBlock* r) -> bool { |
323 | 0 | auto value = (*(this->_vec_row_comparator))(l, r); |
324 | 0 | if (value == 0) { |
325 | 0 | same_keys_num++; |
326 | 0 | return is_dup ? l->_row_pos > r->_row_pos : l->_row_pos < r->_row_pos; |
327 | 0 | } else { |
328 | 0 | return value < 0; |
329 | 0 | } |
330 | 0 | }; |
331 | 9 | auto new_row_it = std::next(_row_in_blocks.begin(), _last_sorted_pos); |
332 | 9 | std::inplace_merge(_row_in_blocks.begin(), new_row_it, _row_in_blocks.end(), cmp_func); |
333 | 9 | _last_sorted_pos = _row_in_blocks.size(); |
334 | 9 | return same_keys_num; |
335 | 9 | } |
336 | | |
337 | 1 | Status MemTable::_sort_by_cluster_keys() { |
338 | 1 | SCOPED_RAW_TIMER(&_stat.sort_ns); |
339 | 1 | _stat.sort_times++; |
340 | | // sort all rows |
341 | 1 | vectorized::Block in_block = _output_mutable_block.to_block(); |
342 | 1 | vectorized::MutableBlock mutable_block = |
343 | 1 | vectorized::MutableBlock::build_mutable_block(&in_block); |
344 | 1 | auto clone_block = in_block.clone_without_columns(); |
345 | 1 | _output_mutable_block = vectorized::MutableBlock::build_mutable_block(&clone_block); |
346 | | |
347 | 1 | std::vector<RowInBlock*> row_in_blocks; |
348 | 1 | std::unique_ptr<int, std::function<void(int*)>> row_in_blocks_deleter((int*)0x01, [&](int*) { |
349 | 1 | std::for_each(row_in_blocks.begin(), row_in_blocks.end(), |
350 | 1 | std::default_delete<RowInBlock>()); |
351 | 1 | }); |
352 | 1 | row_in_blocks.reserve(mutable_block.rows()); |
353 | 5 | for (size_t i = 0; i < mutable_block.rows(); i++) { |
354 | 4 | row_in_blocks.emplace_back(new RowInBlock {i}); |
355 | 4 | } |
356 | 1 | Tie tie = Tie(0, mutable_block.rows()); |
357 | | |
358 | 2 | for (auto cid : _tablet_schema->cluster_key_idxes()) { |
359 | 2 | auto index = _tablet_schema->field_index(cid); |
360 | 2 | if (index == -1) { |
361 | 0 | return Status::InternalError("could not find cluster key column with unique_id=" + |
362 | 0 | std::to_string(cid) + " in tablet schema"); |
363 | 0 | } |
364 | 8 | auto cmp = [&](const RowInBlock* lhs, const RowInBlock* rhs) -> int { |
365 | 8 | return mutable_block.compare_one_column(lhs->_row_pos, rhs->_row_pos, index, -1); |
366 | 8 | }; |
367 | 2 | _sort_one_column(row_in_blocks, tie, cmp); |
368 | 2 | } |
369 | | |
370 | | // sort extra round by _row_pos to make the sort stable |
371 | 1 | auto iter = tie.iter(); |
372 | 1 | while (iter.next()) { |
373 | 0 | pdqsort(std::next(row_in_blocks.begin(), iter.left()), |
374 | 0 | std::next(row_in_blocks.begin(), iter.right()), |
375 | 0 | [](const RowInBlock* lhs, const RowInBlock* rhs) -> bool { |
376 | 0 | return lhs->_row_pos < rhs->_row_pos; |
377 | 0 | }); |
378 | 0 | } |
379 | | |
380 | 1 | in_block = mutable_block.to_block(); |
381 | 1 | SCOPED_RAW_TIMER(&_stat.put_into_output_ns); |
382 | 1 | std::vector<uint32_t> row_pos_vec; |
383 | 1 | DCHECK(in_block.rows() <= std::numeric_limits<int>::max()); |
384 | 1 | row_pos_vec.reserve(in_block.rows()); |
385 | 5 | for (int i = 0; i < row_in_blocks.size(); i++) { |
386 | 4 | row_pos_vec.emplace_back(row_in_blocks[i]->_row_pos); |
387 | 4 | } |
388 | 1 | std::vector<int> column_offset; |
389 | 6 | for (int i = 0; i < _column_offset.size(); ++i) { |
390 | 5 | column_offset.emplace_back(i); |
391 | 5 | } |
392 | 1 | return _output_mutable_block.add_rows(&in_block, row_pos_vec.data(), |
393 | 1 | row_pos_vec.data() + in_block.rows(), &column_offset); |
394 | 1 | } |
395 | | |
396 | | void MemTable::_sort_one_column(std::vector<RowInBlock*>& row_in_blocks, Tie& tie, |
397 | 30 | std::function<int(const RowInBlock*, const RowInBlock*)> cmp) { |
398 | 30 | auto iter = tie.iter(); |
399 | 40 | while (iter.next()) { |
400 | 10 | pdqsort(std::next(row_in_blocks.begin(), iter.left()), |
401 | 10 | std::next(row_in_blocks.begin(), iter.right()), |
402 | 21 | [&cmp](auto lhs, auto rhs) -> bool { return cmp(lhs, rhs) < 0; }); |
403 | 10 | tie[iter.left()] = 0; |
404 | 27 | for (int i = iter.left() + 1; i < iter.right(); i++) { |
405 | 17 | tie[i] = (cmp(row_in_blocks[i - 1], row_in_blocks[i]) == 0); |
406 | 17 | } |
407 | 10 | } |
408 | 30 | } |
409 | | |
410 | | template <bool is_final> |
411 | | void MemTable::_finalize_one_row(RowInBlock* row, |
412 | | const vectorized::ColumnsWithTypeAndName& block_data, |
413 | 6 | int row_pos) { |
414 | | // move key columns |
415 | 18 | for (size_t i = 0; i < _tablet_schema->num_key_columns(); ++i) { |
416 | 12 | _output_mutable_block.get_column_by_position(i)->insert_from(*block_data[i].column.get(), |
417 | 12 | row->_row_pos); |
418 | 12 | } |
419 | 6 | if (row->has_init_agg()) { |
420 | | // get value columns from agg_places |
421 | 12 | for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { |
422 | 9 | auto function = _agg_functions[i]; |
423 | 9 | auto* agg_place = row->agg_places(i); |
424 | 9 | auto* col_ptr = _output_mutable_block.get_column_by_position(i).get(); |
425 | 9 | function->insert_result_into(agg_place, *col_ptr); |
426 | | |
427 | 9 | if constexpr (is_final) { |
428 | 0 | function->destroy(agg_place); |
429 | 0 | } else { |
430 | 0 | function->reset(agg_place); |
431 | 0 | } |
432 | 9 | } |
433 | | |
434 | 3 | _arena->clear(); |
435 | | |
436 | 3 | if constexpr (is_final) { |
437 | 0 | row->remove_init_agg(); |
438 | 0 | } else { |
439 | 0 | for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { |
440 | 0 | auto function = _agg_functions[i]; |
441 | 0 | auto* agg_place = row->agg_places(i); |
442 | 0 | auto* col_ptr = _output_mutable_block.get_column_by_position(i).get(); |
443 | 0 | function->add(agg_place, const_cast<const doris::vectorized::IColumn**>(&col_ptr), |
444 | 0 | row_pos, _arena.get()); |
445 | 0 | } |
446 | 0 | } |
447 | 3 | } else { |
448 | | // move columns for rows do not need agg |
449 | 12 | for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { |
450 | 9 | _output_mutable_block.get_column_by_position(i)->insert_from( |
451 | 9 | *block_data[i].column.get(), row->_row_pos); |
452 | 9 | } |
453 | 3 | } |
454 | 6 | if constexpr (!is_final) { |
455 | 0 | row->_row_pos = row_pos; |
456 | 0 | } |
457 | 6 | } Unexecuted instantiation: _ZN5doris8MemTable17_finalize_one_rowILb0EEEvPNS_10RowInBlockERKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS6_EEi _ZN5doris8MemTable17_finalize_one_rowILb1EEEvPNS_10RowInBlockERKSt6vectorINS_10vectorized21ColumnWithTypeAndNameESaIS6_EEi Line | Count | Source | 413 | 6 | int row_pos) { | 414 | | // move key columns | 415 | 18 | for (size_t i = 0; i < _tablet_schema->num_key_columns(); ++i) { | 416 | 12 | _output_mutable_block.get_column_by_position(i)->insert_from(*block_data[i].column.get(), | 417 | 12 | row->_row_pos); | 418 | 12 | } | 419 | 6 | if (row->has_init_agg()) { | 420 | | // get value columns from agg_places | 421 | 12 | for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { | 422 | 9 | auto function = _agg_functions[i]; | 423 | 9 | auto* agg_place = row->agg_places(i); | 424 | 9 | auto* col_ptr = _output_mutable_block.get_column_by_position(i).get(); | 425 | 9 | function->insert_result_into(agg_place, *col_ptr); | 426 | | | 427 | 9 | if constexpr (is_final) { | 428 | 9 | function->destroy(agg_place); | 429 | 9 | } else { | 430 | 9 | function->reset(agg_place); | 431 | 9 | } | 432 | 9 | } | 433 | | | 434 | 3 | _arena->clear(); | 435 | | | 436 | 3 | if constexpr (is_final) { | 437 | 3 | row->remove_init_agg(); | 438 | 3 | } else { | 439 | 3 | for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { | 440 | 3 | auto function = _agg_functions[i]; | 441 | 3 | auto* agg_place = row->agg_places(i); | 442 | 3 | auto* col_ptr = _output_mutable_block.get_column_by_position(i).get(); | 443 | 3 | function->add(agg_place, const_cast<const doris::vectorized::IColumn**>(&col_ptr), | 444 | 3 | row_pos, _arena.get()); | 445 | 3 | } | 446 | 3 | } | 447 | 3 | } else { | 448 | | // move columns for rows do not need agg | 449 | 12 | for (size_t i = _tablet_schema->num_key_columns(); i < _num_columns; ++i) { | 450 | 9 | _output_mutable_block.get_column_by_position(i)->insert_from( | 451 | 9 | *block_data[i].column.get(), row->_row_pos); | 452 | 9 | } | 453 | 3 | } | 454 | 6 | if constexpr (!is_final) { | 455 | 6 | row->_row_pos = row_pos; | 456 | 6 | } | 457 | 6 | } |
|
458 | | |
459 | | template <bool is_final, bool has_skip_bitmap_col> |
460 | 3 | void MemTable::_aggregate() { |
461 | 3 | SCOPED_RAW_TIMER(&_stat.agg_ns); |
462 | 3 | _stat.agg_times++; |
463 | 3 | vectorized::Block in_block = _input_mutable_block.to_block(); |
464 | 3 | vectorized::MutableBlock mutable_block = |
465 | 3 | vectorized::MutableBlock::build_mutable_block(&in_block); |
466 | 3 | _vec_row_comparator->set_block(&mutable_block); |
467 | 3 | auto& block_data = in_block.get_columns_with_type_and_name(); |
468 | 3 | std::vector<RowInBlock*> temp_row_in_blocks; |
469 | 3 | temp_row_in_blocks.reserve(_last_sorted_pos); |
470 | 3 | RowInBlock* prev_row = nullptr; |
471 | 3 | int row_pos = -1; |
472 | | //only init agg if needed |
473 | | |
474 | 3 | auto init_for_agg = [&](RowInBlock* row) { |
475 | 3 | row->init_agg_places(_arena->aligned_alloc(_total_size_of_aggregate_states, 16), |
476 | 3 | _offsets_of_aggregate_states.data()); |
477 | 12 | for (auto cid = _tablet_schema->num_key_columns(); cid < _num_columns; cid++) { |
478 | 9 | auto* col_ptr = mutable_block.mutable_columns()[cid].get(); |
479 | 9 | auto* data = prev_row->agg_places(cid); |
480 | 9 | _agg_functions[cid]->create(data); |
481 | 9 | _agg_functions[cid]->add(data, const_cast<const doris::vectorized::IColumn**>(&col_ptr), |
482 | 9 | prev_row->_row_pos, _arena.get()); |
483 | 9 | } |
484 | 3 | }; Unexecuted instantiation: _ZZN5doris8MemTable10_aggregateILb0ELb0EEEvvENKUlPNS_10RowInBlockEE_clES3_ Unexecuted instantiation: _ZZN5doris8MemTable10_aggregateILb0ELb1EEEvvENKUlPNS_10RowInBlockEE_clES3_ _ZZN5doris8MemTable10_aggregateILb1ELb0EEEvvENKUlPNS_10RowInBlockEE_clES3_ Line | Count | Source | 474 | 3 | auto init_for_agg = [&](RowInBlock* row) { | 475 | 3 | row->init_agg_places(_arena->aligned_alloc(_total_size_of_aggregate_states, 16), | 476 | 3 | _offsets_of_aggregate_states.data()); | 477 | 12 | for (auto cid = _tablet_schema->num_key_columns(); cid < _num_columns; cid++) { | 478 | 9 | auto* col_ptr = mutable_block.mutable_columns()[cid].get(); | 479 | 9 | auto* data = prev_row->agg_places(cid); | 480 | 9 | _agg_functions[cid]->create(data); | 481 | 9 | _agg_functions[cid]->add(data, const_cast<const doris::vectorized::IColumn**>(&col_ptr), | 482 | 9 | prev_row->_row_pos, _arena.get()); | 483 | 9 | } | 484 | 3 | }; |
Unexecuted instantiation: _ZZN5doris8MemTable10_aggregateILb1ELb1EEEvvENKUlPNS_10RowInBlockEE_clES3_ |
485 | | |
486 | 3 | if (!has_skip_bitmap_col || _seq_col_idx_in_block == -1) { |
487 | 9 | for (RowInBlock* cur_row : _row_in_blocks) { |
488 | 9 | if (!temp_row_in_blocks.empty() && (*_vec_row_comparator)(prev_row, cur_row) == 0) { |
489 | 3 | if (!prev_row->has_init_agg()) { |
490 | 3 | init_for_agg(prev_row); |
491 | 3 | } |
492 | 3 | _stat.merged_rows++; |
493 | 3 | _aggregate_two_row_in_block<has_skip_bitmap_col>(mutable_block, cur_row, prev_row); |
494 | 6 | } else { |
495 | 6 | prev_row = cur_row; |
496 | 6 | if (!temp_row_in_blocks.empty()) { |
497 | | // no more rows to merge for prev row, finalize it |
498 | 3 | _finalize_one_row<is_final>(temp_row_in_blocks.back(), block_data, row_pos); |
499 | 3 | } |
500 | 6 | temp_row_in_blocks.push_back(prev_row); |
501 | 6 | row_pos++; |
502 | 6 | } |
503 | 9 | } |
504 | 3 | if (!temp_row_in_blocks.empty()) { |
505 | | // finalize the last low |
506 | 3 | _finalize_one_row<is_final>(temp_row_in_blocks.back(), block_data, row_pos); |
507 | 3 | } |
508 | 3 | } else { |
509 | | // For flexible partial update and the table has sequence column, considering the following situation: |
510 | | // there are multiple rows with the same keys in memtable, some of them specify the sequence column, |
511 | | // some of them don't. We can't do the de-duplication in memtable becasue we can only know the value |
512 | | // of the sequence column of the row which don't specify seqeuence column in SegmentWriter after we |
513 | | // probe the historical data. So at here we can only merge rows that have sequence column together and |
514 | | // merge rows without sequence column together, and finally, perform deduplication on them in SegmentWriter. |
515 | | |
516 | | // !!ATTENTION!!: there may be rows with the same keys after MemTable::_aggregate() in this situation. |
517 | 0 | RowInBlock* row_with_seq_col = nullptr; |
518 | 0 | int row_pos_with_seq = -1; |
519 | 0 | RowInBlock* row_without_seq_col = nullptr; |
520 | 0 | int row_pos_without_seq = -1; |
521 | |
|
522 | 0 | auto finalize_rows = [&]() { |
523 | 0 | if (row_with_seq_col != nullptr) { |
524 | 0 | _finalize_one_row<is_final>(row_with_seq_col, block_data, row_pos_with_seq); |
525 | 0 | row_with_seq_col = nullptr; |
526 | 0 | } |
527 | 0 | if (row_without_seq_col != nullptr) { |
528 | 0 | _finalize_one_row<is_final>(row_without_seq_col, block_data, row_pos_without_seq); |
529 | 0 | row_without_seq_col = nullptr; |
530 | 0 | } |
531 | 0 | }; Unexecuted instantiation: _ZZN5doris8MemTable10_aggregateILb0ELb1EEEvvENKUlvE_clEv Unexecuted instantiation: _ZZN5doris8MemTable10_aggregateILb1ELb1EEEvvENKUlvE_clEv |
532 | 0 | auto add_row = [&](RowInBlock* row, bool with_seq_col) { |
533 | 0 | temp_row_in_blocks.push_back(row); |
534 | 0 | row_pos++; |
535 | 0 | if (with_seq_col) { |
536 | 0 | row_with_seq_col = row; |
537 | 0 | row_pos_with_seq = row_pos; |
538 | 0 | } else { |
539 | 0 | row_without_seq_col = row; |
540 | 0 | row_pos_without_seq = row_pos; |
541 | 0 | } |
542 | 0 | }; Unexecuted instantiation: _ZZN5doris8MemTable10_aggregateILb0ELb1EEEvvENKUlPNS_10RowInBlockEbE_clES3_b Unexecuted instantiation: _ZZN5doris8MemTable10_aggregateILb1ELb1EEEvvENKUlPNS_10RowInBlockEbE_clES3_b |
543 | 0 | auto& skip_bitmaps = assert_cast<vectorized::ColumnBitmap*>( |
544 | 0 | mutable_block.mutable_columns()[_skip_bitmap_col_idx].get()) |
545 | 0 | ->get_data(); |
546 | 0 | for (auto* cur_row : _row_in_blocks) { |
547 | 0 | const BitmapValue& skip_bitmap = skip_bitmaps[cur_row->_row_pos]; |
548 | 0 | bool with_seq_col = !skip_bitmap.contains(_seq_col_unique_id); |
549 | | // compare keys, the keys of row_with_seq_col and row_with_seq_col is the same, |
550 | | // choose any of them if it's valid |
551 | 0 | prev_row = (row_with_seq_col == nullptr) ? row_without_seq_col : row_with_seq_col; |
552 | 0 | if (prev_row != nullptr && (*_vec_row_comparator)(prev_row, cur_row) == 0) { |
553 | 0 | prev_row = (with_seq_col ? row_with_seq_col : row_without_seq_col); |
554 | 0 | if (prev_row == nullptr) { |
555 | 0 | add_row(cur_row, with_seq_col); |
556 | 0 | continue; |
557 | 0 | } |
558 | 0 | if (!prev_row->has_init_agg()) { |
559 | 0 | init_for_agg(prev_row); |
560 | 0 | } |
561 | 0 | _stat.merged_rows++; |
562 | 0 | _aggregate_two_row_in_block<has_skip_bitmap_col>(mutable_block, cur_row, prev_row); |
563 | 0 | } else { |
564 | | // no more rows to merge for prev rows, finalize them |
565 | 0 | finalize_rows(); |
566 | 0 | add_row(cur_row, with_seq_col); |
567 | 0 | } |
568 | 0 | } |
569 | | // finalize the last lows |
570 | 0 | finalize_rows(); |
571 | 0 | } |
572 | 3 | if constexpr (!is_final) { |
573 | | // if is not final, we collect the agg results to input_block and then continue to insert |
574 | 0 | _input_mutable_block.swap(_output_mutable_block); |
575 | | //TODO(weixang):opt here. |
576 | 0 | std::unique_ptr<vectorized::Block> empty_input_block = in_block.create_same_struct_block(0); |
577 | 0 | _output_mutable_block = |
578 | 0 | vectorized::MutableBlock::build_mutable_block(empty_input_block.get()); |
579 | 0 | _output_mutable_block.clear_column_data(); |
580 | 0 | _row_in_blocks = temp_row_in_blocks; |
581 | 0 | _last_sorted_pos = _row_in_blocks.size(); |
582 | 0 | } |
583 | 3 | } Unexecuted instantiation: _ZN5doris8MemTable10_aggregateILb0ELb0EEEvv Unexecuted instantiation: _ZN5doris8MemTable10_aggregateILb0ELb1EEEvv _ZN5doris8MemTable10_aggregateILb1ELb0EEEvv Line | Count | Source | 460 | 3 | void MemTable::_aggregate() { | 461 | 3 | SCOPED_RAW_TIMER(&_stat.agg_ns); | 462 | 3 | _stat.agg_times++; | 463 | 3 | vectorized::Block in_block = _input_mutable_block.to_block(); | 464 | 3 | vectorized::MutableBlock mutable_block = | 465 | 3 | vectorized::MutableBlock::build_mutable_block(&in_block); | 466 | 3 | _vec_row_comparator->set_block(&mutable_block); | 467 | 3 | auto& block_data = in_block.get_columns_with_type_and_name(); | 468 | 3 | std::vector<RowInBlock*> temp_row_in_blocks; | 469 | 3 | temp_row_in_blocks.reserve(_last_sorted_pos); | 470 | 3 | RowInBlock* prev_row = nullptr; | 471 | 3 | int row_pos = -1; | 472 | | //only init agg if needed | 473 | | | 474 | 3 | auto init_for_agg = [&](RowInBlock* row) { | 475 | 3 | row->init_agg_places(_arena->aligned_alloc(_total_size_of_aggregate_states, 16), | 476 | 3 | _offsets_of_aggregate_states.data()); | 477 | 3 | for (auto cid = _tablet_schema->num_key_columns(); cid < _num_columns; cid++) { | 478 | 3 | auto* col_ptr = mutable_block.mutable_columns()[cid].get(); | 479 | 3 | auto* data = prev_row->agg_places(cid); | 480 | 3 | _agg_functions[cid]->create(data); | 481 | 3 | _agg_functions[cid]->add(data, const_cast<const doris::vectorized::IColumn**>(&col_ptr), | 482 | 3 | prev_row->_row_pos, _arena.get()); | 483 | 3 | } | 484 | 3 | }; | 485 | | | 486 | 3 | if (!has_skip_bitmap_col || _seq_col_idx_in_block == -1) { | 487 | 9 | for (RowInBlock* cur_row : _row_in_blocks) { | 488 | 9 | if (!temp_row_in_blocks.empty() && (*_vec_row_comparator)(prev_row, cur_row) == 0) { | 489 | 3 | if (!prev_row->has_init_agg()) { | 490 | 3 | init_for_agg(prev_row); | 491 | 3 | } | 492 | 3 | _stat.merged_rows++; | 493 | 3 | _aggregate_two_row_in_block<has_skip_bitmap_col>(mutable_block, cur_row, prev_row); | 494 | 6 | } else { | 495 | 6 | prev_row = cur_row; | 496 | 6 | if (!temp_row_in_blocks.empty()) { | 497 | | // no more rows to merge for prev row, finalize it | 498 | 3 | _finalize_one_row<is_final>(temp_row_in_blocks.back(), block_data, row_pos); | 499 | 3 | } | 500 | 6 | temp_row_in_blocks.push_back(prev_row); | 501 | 6 | row_pos++; | 502 | 6 | } | 503 | 9 | } | 504 | 3 | if (!temp_row_in_blocks.empty()) { | 505 | | // finalize the last low | 506 | 3 | _finalize_one_row<is_final>(temp_row_in_blocks.back(), block_data, row_pos); | 507 | 3 | } | 508 | 3 | } else { | 509 | | // For flexible partial update and the table has sequence column, considering the following situation: | 510 | | // there are multiple rows with the same keys in memtable, some of them specify the sequence column, | 511 | | // some of them don't. We can't do the de-duplication in memtable becasue we can only know the value | 512 | | // of the sequence column of the row which don't specify seqeuence column in SegmentWriter after we | 513 | | // probe the historical data. So at here we can only merge rows that have sequence column together and | 514 | | // merge rows without sequence column together, and finally, perform deduplication on them in SegmentWriter. | 515 | | | 516 | | // !!ATTENTION!!: there may be rows with the same keys after MemTable::_aggregate() in this situation. | 517 | 0 | RowInBlock* row_with_seq_col = nullptr; | 518 | 0 | int row_pos_with_seq = -1; | 519 | 0 | RowInBlock* row_without_seq_col = nullptr; | 520 | 0 | int row_pos_without_seq = -1; | 521 | |
| 522 | 0 | auto finalize_rows = [&]() { | 523 | 0 | if (row_with_seq_col != nullptr) { | 524 | 0 | _finalize_one_row<is_final>(row_with_seq_col, block_data, row_pos_with_seq); | 525 | 0 | row_with_seq_col = nullptr; | 526 | 0 | } | 527 | 0 | if (row_without_seq_col != nullptr) { | 528 | 0 | _finalize_one_row<is_final>(row_without_seq_col, block_data, row_pos_without_seq); | 529 | 0 | row_without_seq_col = nullptr; | 530 | 0 | } | 531 | 0 | }; | 532 | 0 | auto add_row = [&](RowInBlock* row, bool with_seq_col) { | 533 | 0 | temp_row_in_blocks.push_back(row); | 534 | 0 | row_pos++; | 535 | 0 | if (with_seq_col) { | 536 | 0 | row_with_seq_col = row; | 537 | 0 | row_pos_with_seq = row_pos; | 538 | 0 | } else { | 539 | 0 | row_without_seq_col = row; | 540 | 0 | row_pos_without_seq = row_pos; | 541 | 0 | } | 542 | 0 | }; | 543 | 0 | auto& skip_bitmaps = assert_cast<vectorized::ColumnBitmap*>( | 544 | 0 | mutable_block.mutable_columns()[_skip_bitmap_col_idx].get()) | 545 | 0 | ->get_data(); | 546 | 0 | for (auto* cur_row : _row_in_blocks) { | 547 | 0 | const BitmapValue& skip_bitmap = skip_bitmaps[cur_row->_row_pos]; | 548 | 0 | bool with_seq_col = !skip_bitmap.contains(_seq_col_unique_id); | 549 | | // compare keys, the keys of row_with_seq_col and row_with_seq_col is the same, | 550 | | // choose any of them if it's valid | 551 | 0 | prev_row = (row_with_seq_col == nullptr) ? row_without_seq_col : row_with_seq_col; | 552 | 0 | if (prev_row != nullptr && (*_vec_row_comparator)(prev_row, cur_row) == 0) { | 553 | 0 | prev_row = (with_seq_col ? row_with_seq_col : row_without_seq_col); | 554 | 0 | if (prev_row == nullptr) { | 555 | 0 | add_row(cur_row, with_seq_col); | 556 | 0 | continue; | 557 | 0 | } | 558 | 0 | if (!prev_row->has_init_agg()) { | 559 | 0 | init_for_agg(prev_row); | 560 | 0 | } | 561 | 0 | _stat.merged_rows++; | 562 | 0 | _aggregate_two_row_in_block<has_skip_bitmap_col>(mutable_block, cur_row, prev_row); | 563 | 0 | } else { | 564 | | // no more rows to merge for prev rows, finalize them | 565 | 0 | finalize_rows(); | 566 | 0 | add_row(cur_row, with_seq_col); | 567 | 0 | } | 568 | 0 | } | 569 | | // finalize the last lows | 570 | 0 | finalize_rows(); | 571 | 0 | } | 572 | 3 | if constexpr (!is_final) { | 573 | | // if is not final, we collect the agg results to input_block and then continue to insert | 574 | 3 | _input_mutable_block.swap(_output_mutable_block); | 575 | | //TODO(weixang):opt here. | 576 | 3 | std::unique_ptr<vectorized::Block> empty_input_block = in_block.create_same_struct_block(0); | 577 | 3 | _output_mutable_block = | 578 | 3 | vectorized::MutableBlock::build_mutable_block(empty_input_block.get()); | 579 | 3 | _output_mutable_block.clear_column_data(); | 580 | 3 | _row_in_blocks = temp_row_in_blocks; | 581 | 3 | _last_sorted_pos = _row_in_blocks.size(); | 582 | 3 | } | 583 | 3 | } |
Unexecuted instantiation: _ZN5doris8MemTable10_aggregateILb1ELb1EEEvv |
584 | | |
585 | 0 | void MemTable::shrink_memtable_by_agg() { |
586 | 0 | SCOPED_CONSUME_MEM_TRACKER(_mem_tracker); |
587 | 0 | if (_keys_type == KeysType::DUP_KEYS) { |
588 | 0 | return; |
589 | 0 | } |
590 | 0 | size_t same_keys_num = _sort(); |
591 | 0 | if (same_keys_num != 0) { |
592 | 0 | (_skip_bitmap_col_idx == -1) ? _aggregate<false, false>() : _aggregate<false, true>(); |
593 | 0 | } |
594 | 0 | } |
595 | | |
596 | 17 | bool MemTable::need_flush() const { |
597 | 17 | auto max_size = config::write_buffer_size; |
598 | 17 | if (_partial_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS) { |
599 | 0 | auto update_columns_size = _num_columns; |
600 | 0 | max_size = max_size * update_columns_size / _tablet_schema->num_columns(); |
601 | 0 | max_size = max_size > 1048576 ? max_size : 1048576; |
602 | 0 | } |
603 | 17 | return memory_usage() >= max_size; |
604 | 17 | } |
605 | | |
606 | 17 | bool MemTable::need_agg() const { |
607 | 17 | if (_keys_type == KeysType::AGG_KEYS) { |
608 | 2 | auto max_size = config::write_buffer_size_for_agg; |
609 | 2 | return memory_usage() >= max_size; |
610 | 2 | } |
611 | 15 | return false; |
612 | 17 | } |
613 | | |
614 | 9 | Status MemTable::_to_block(std::unique_ptr<vectorized::Block>* res) { |
615 | 9 | size_t same_keys_num = _sort(); |
616 | 9 | if (_keys_type == KeysType::DUP_KEYS || same_keys_num == 0) { |
617 | 6 | if (_keys_type == KeysType::DUP_KEYS && _tablet_schema->num_key_columns() == 0) { |
618 | 0 | _output_mutable_block.swap(_input_mutable_block); |
619 | 6 | } else { |
620 | 6 | vectorized::Block in_block = _input_mutable_block.to_block(); |
621 | 6 | RETURN_IF_ERROR(_put_into_output(in_block)); |
622 | 6 | } |
623 | 6 | } else { |
624 | 3 | (_skip_bitmap_col_idx == -1) ? _aggregate<true, false>() : _aggregate<true, true>(); |
625 | 3 | } |
626 | 9 | if (_keys_type == KeysType::UNIQUE_KEYS && _enable_unique_key_mow && |
627 | 9 | !_tablet_schema->cluster_key_idxes().empty()) { |
628 | 1 | if (_partial_update_mode != UniqueKeyUpdateModePB::UPSERT) { |
629 | 0 | return Status::InternalError( |
630 | 0 | "Partial update for mow with cluster keys is not supported"); |
631 | 0 | } |
632 | 1 | RETURN_IF_ERROR(_sort_by_cluster_keys()); |
633 | 1 | } |
634 | 9 | g_memtable_input_block_allocated_size << -_input_mutable_block.allocated_bytes(); |
635 | 9 | _input_mutable_block.clear(); |
636 | | // After to block, all data in arena is saved in the block |
637 | 9 | _arena.reset(); |
638 | 9 | *res = vectorized::Block::create_unique(_output_mutable_block.to_block()); |
639 | 9 | return Status::OK(); |
640 | 9 | } |
641 | | |
642 | 9 | Status MemTable::to_block(std::unique_ptr<vectorized::Block>* res) { |
643 | 9 | RETURN_IF_ERROR_OR_CATCH_EXCEPTION(_to_block(res)); |
644 | 9 | return Status::OK(); |
645 | 9 | } |
646 | | |
647 | | } // namespace doris |