/root/doris/be/src/olap/tablet_reader.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/tablet_reader.h" |
19 | | |
20 | | #include <gen_cpp/olap_file.pb.h> |
21 | | #include <gen_cpp/segment_v2.pb.h> |
22 | | #include <thrift/protocol/TDebugProtocol.h> |
23 | | |
24 | | #include <algorithm> |
25 | | #include <functional> |
26 | | #include <iterator> |
27 | | #include <memory> |
28 | | #include <numeric> |
29 | | #include <ostream> |
30 | | #include <shared_mutex> |
31 | | |
32 | | #include "common/compiler_util.h" // IWYU pragma: keep |
33 | | #include "common/config.h" |
34 | | #include "common/exception.h" |
35 | | #include "common/logging.h" |
36 | | #include "common/status.h" |
37 | | #include "exprs/bitmapfilter_predicate.h" |
38 | | #include "exprs/bloom_filter_func.h" |
39 | | #include "exprs/create_predicate_function.h" |
40 | | #include "exprs/hybrid_set.h" |
41 | | #include "olap/column_predicate.h" |
42 | | #include "olap/itoken_extractor.h" |
43 | | #include "olap/like_column_predicate.h" |
44 | | #include "olap/olap_common.h" |
45 | | #include "olap/olap_define.h" |
46 | | #include "olap/predicate_creator.h" |
47 | | #include "olap/row_cursor.h" |
48 | | #include "olap/rowset/segment_v2/bloom_filter.h" |
49 | | #include "olap/schema.h" |
50 | | #include "olap/tablet.h" |
51 | | #include "olap/tablet_meta.h" |
52 | | #include "olap/tablet_schema.h" |
53 | | #include "runtime/query_context.h" |
54 | | #include "runtime/runtime_predicate.h" |
55 | | #include "runtime/runtime_state.h" |
56 | | #include "vec/common/arena.h" |
57 | | #include "vec/core/block.h" |
58 | | |
59 | | namespace doris { |
60 | | using namespace ErrorCode; |
61 | | |
62 | 353 | void TabletReader::ReaderParams::check_validation() const { |
63 | 353 | if (UNLIKELY(version.first == -1 && is_segcompaction == false)) { |
64 | 0 | throw Exception(Status::FatalError("version is not set. tablet={}", tablet->tablet_id())); |
65 | 0 | } |
66 | 353 | } |
67 | | |
68 | 0 | std::string TabletReader::ReaderParams::to_string() const { |
69 | 0 | std::stringstream ss; |
70 | 0 | ss << "tablet=" << tablet->tablet_id() << " reader_type=" << int(reader_type) |
71 | 0 | << " aggregation=" << aggregation << " version=" << version |
72 | 0 | << " start_key_include=" << start_key_include << " end_key_include=" << end_key_include; |
73 | |
|
74 | 0 | for (const auto& key : start_key) { |
75 | 0 | ss << " keys=" << key; |
76 | 0 | } |
77 | |
|
78 | 0 | for (const auto& key : end_key) { |
79 | 0 | ss << " end_keys=" << key; |
80 | 0 | } |
81 | |
|
82 | 0 | for (auto& condition : conditions) { |
83 | 0 | ss << " conditions=" << apache::thrift::ThriftDebugString(condition.filter); |
84 | 0 | } |
85 | |
|
86 | 0 | return ss.str(); |
87 | 0 | } |
88 | | |
89 | 0 | std::string TabletReader::KeysParam::to_string() const { |
90 | 0 | std::stringstream ss; |
91 | 0 | ss << "start_key_include=" << start_key_include << " end_key_include=" << end_key_include; |
92 | |
|
93 | 0 | for (const auto& start_key : start_keys) { |
94 | 0 | ss << " keys=" << start_key.to_string(); |
95 | 0 | } |
96 | 0 | for (const auto& end_key : end_keys) { |
97 | 0 | ss << " end_keys=" << end_key.to_string(); |
98 | 0 | } |
99 | |
|
100 | 0 | return ss.str(); |
101 | 0 | } |
102 | | |
103 | 331 | void TabletReader::ReadSource::fill_delete_predicates() { |
104 | 331 | DCHECK_EQ(delete_predicates.size(), 0); |
105 | 1.05k | for (auto&& split : rs_splits) { |
106 | 1.05k | auto& rs_meta = split.rs_reader->rowset()->rowset_meta(); |
107 | 1.05k | if (rs_meta->has_delete_predicate()) { |
108 | 149 | delete_predicates.push_back(rs_meta); |
109 | 149 | } |
110 | 1.05k | } |
111 | 331 | } |
112 | | |
113 | 353 | TabletReader::~TabletReader() { |
114 | 353 | for (auto* pred : _col_predicates) { |
115 | 0 | delete pred; |
116 | 0 | } |
117 | 353 | for (auto* pred : _value_col_predicates) { |
118 | 0 | delete pred; |
119 | 0 | } |
120 | 353 | } |
121 | | |
122 | 353 | Status TabletReader::init(const ReaderParams& read_params) { |
123 | 353 | SCOPED_RAW_TIMER(&_stats.tablet_reader_init_timer_ns); |
124 | 353 | _predicate_arena = std::make_unique<vectorized::Arena>(); |
125 | | |
126 | 353 | Status res = _init_params(read_params); |
127 | 353 | if (!res.ok()) { |
128 | 0 | LOG(WARNING) << "fail to init reader when init params. res:" << res |
129 | 0 | << ", tablet_id:" << read_params.tablet->tablet_id() |
130 | 0 | << ", schema_hash:" << read_params.tablet->schema_hash() |
131 | 0 | << ", reader type:" << int(read_params.reader_type) |
132 | 0 | << ", version:" << read_params.version; |
133 | 0 | } |
134 | 353 | return res; |
135 | 353 | } |
136 | | |
137 | | // When only one rowset has data, and this rowset is nonoverlapping, we can read directly without aggregation |
138 | | bool TabletReader::_optimize_for_single_rowset( |
139 | 0 | const std::vector<RowsetReaderSharedPtr>& rs_readers) { |
140 | 0 | bool has_delete_rowset = false; |
141 | 0 | bool has_overlapping = false; |
142 | 0 | int nonoverlapping_count = 0; |
143 | 0 | for (const auto& rs_reader : rs_readers) { |
144 | 0 | if (rs_reader->rowset()->rowset_meta()->delete_flag()) { |
145 | 0 | has_delete_rowset = true; |
146 | 0 | break; |
147 | 0 | } |
148 | 0 | if (rs_reader->rowset()->rowset_meta()->num_rows() > 0) { |
149 | 0 | if (rs_reader->rowset()->rowset_meta()->is_segments_overlapping()) { |
150 | | // when there are overlapping segments, can not do directly read |
151 | 0 | has_overlapping = true; |
152 | 0 | break; |
153 | 0 | } else if (++nonoverlapping_count > 1) { |
154 | 0 | break; |
155 | 0 | } |
156 | 0 | } |
157 | 0 | } |
158 | |
|
159 | 0 | return !has_overlapping && nonoverlapping_count == 1 && !has_delete_rowset; |
160 | 0 | } |
161 | | |
162 | 331 | Status TabletReader::_capture_rs_readers(const ReaderParams& read_params) { |
163 | 331 | SCOPED_RAW_TIMER(&_stats.tablet_reader_capture_rs_readers_timer_ns); |
164 | 331 | if (read_params.rs_splits.empty()) { |
165 | 0 | return Status::InternalError("fail to acquire data sources. tablet={}", |
166 | 0 | _tablet->tablet_id()); |
167 | 0 | } |
168 | | |
169 | 331 | bool eof = false; |
170 | 331 | bool is_lower_key_included = _keys_param.start_key_include; |
171 | 331 | bool is_upper_key_included = _keys_param.end_key_include; |
172 | | |
173 | 331 | for (int i = 0; i < _keys_param.start_keys.size(); ++i) { |
174 | | // lower bound |
175 | 0 | RowCursor& start_key = _keys_param.start_keys[i]; |
176 | 0 | RowCursor& end_key = _keys_param.end_keys[i]; |
177 | |
|
178 | 0 | if (!is_lower_key_included) { |
179 | 0 | if (compare_row_key(start_key, end_key) >= 0) { |
180 | 0 | VLOG_NOTICE << "return EOF when lower key not include" |
181 | 0 | << ", start_key=" << start_key.to_string() |
182 | 0 | << ", end_key=" << end_key.to_string(); |
183 | 0 | eof = true; |
184 | 0 | break; |
185 | 0 | } |
186 | 0 | } else { |
187 | 0 | if (compare_row_key(start_key, end_key) > 0) { |
188 | 0 | VLOG_NOTICE << "return EOF when lower key include=" |
189 | 0 | << ", start_key=" << start_key.to_string() |
190 | 0 | << ", end_key=" << end_key.to_string(); |
191 | 0 | eof = true; |
192 | 0 | break; |
193 | 0 | } |
194 | 0 | } |
195 | | |
196 | 0 | _is_lower_keys_included.push_back(is_lower_key_included); |
197 | 0 | _is_upper_keys_included.push_back(is_upper_key_included); |
198 | 0 | } |
199 | | |
200 | 331 | if (eof) { |
201 | 0 | return Status::OK(); |
202 | 0 | } |
203 | | |
204 | 331 | bool need_ordered_result = true; |
205 | 331 | if (read_params.reader_type == ReaderType::READER_QUERY) { |
206 | 0 | if (_tablet_schema->keys_type() == DUP_KEYS) { |
207 | | // duplicated keys are allowed, no need to merge sort keys in rowset |
208 | 0 | need_ordered_result = false; |
209 | 0 | } |
210 | 0 | if (_tablet_schema->keys_type() == UNIQUE_KEYS && |
211 | 0 | _tablet->enable_unique_key_merge_on_write()) { |
212 | | // unique keys with merge on write, no need to merge sort keys in rowset |
213 | 0 | need_ordered_result = false; |
214 | 0 | } |
215 | 0 | if (_aggregation) { |
216 | | // compute engine will aggregate rows with the same key, |
217 | | // it's ok for rowset to return unordered result |
218 | 0 | need_ordered_result = false; |
219 | 0 | } |
220 | |
|
221 | 0 | if (_direct_mode) { |
222 | | // direct mode indicates that the storage layer does not need to merge, |
223 | | // it's ok for rowset to return unordered result |
224 | 0 | need_ordered_result = false; |
225 | 0 | } |
226 | |
|
227 | 0 | if (read_params.read_orderby_key) { |
228 | 0 | need_ordered_result = true; |
229 | 0 | } |
230 | 0 | } |
231 | | |
232 | 331 | _reader_context.reader_type = read_params.reader_type; |
233 | 331 | _reader_context.version = read_params.version; |
234 | 331 | _reader_context.tablet_schema = _tablet_schema; |
235 | 331 | _reader_context.need_ordered_result = need_ordered_result; |
236 | 331 | _reader_context.topn_filter_source_node_ids = read_params.topn_filter_source_node_ids; |
237 | 331 | _reader_context.topn_filter_target_node_id = read_params.topn_filter_target_node_id; |
238 | 331 | _reader_context.read_orderby_key_reverse = read_params.read_orderby_key_reverse; |
239 | 331 | _reader_context.read_orderby_key_limit = read_params.read_orderby_key_limit; |
240 | 331 | _reader_context.filter_block_conjuncts = read_params.filter_block_conjuncts; |
241 | 331 | _reader_context.return_columns = &_return_columns; |
242 | 331 | _reader_context.read_orderby_key_columns = |
243 | 331 | !_orderby_key_columns.empty() ? &_orderby_key_columns : nullptr; |
244 | 331 | _reader_context.predicates = &_col_predicates; |
245 | 331 | _reader_context.value_predicates = &_value_col_predicates; |
246 | 331 | _reader_context.lower_bound_keys = &_keys_param.start_keys; |
247 | 331 | _reader_context.is_lower_keys_included = &_is_lower_keys_included; |
248 | 331 | _reader_context.upper_bound_keys = &_keys_param.end_keys; |
249 | 331 | _reader_context.is_upper_keys_included = &_is_upper_keys_included; |
250 | 331 | _reader_context.delete_handler = &_delete_handler; |
251 | 331 | _reader_context.stats = &_stats; |
252 | 331 | _reader_context.use_page_cache = read_params.use_page_cache; |
253 | 331 | _reader_context.sequence_id_idx = _sequence_col_idx; |
254 | 331 | _reader_context.is_unique = tablet()->keys_type() == UNIQUE_KEYS; |
255 | 331 | _reader_context.merged_rows = &_merged_rows; |
256 | 331 | _reader_context.delete_bitmap = read_params.delete_bitmap; |
257 | 331 | _reader_context.enable_unique_key_merge_on_write = tablet()->enable_unique_key_merge_on_write(); |
258 | 331 | _reader_context.record_rowids = read_params.record_rowids; |
259 | 331 | _reader_context.rowid_conversion = read_params.rowid_conversion; |
260 | 331 | _reader_context.is_key_column_group = read_params.is_key_column_group; |
261 | 331 | _reader_context.remaining_conjunct_roots = read_params.remaining_conjunct_roots; |
262 | 331 | _reader_context.common_expr_ctxs_push_down = read_params.common_expr_ctxs_push_down; |
263 | 331 | _reader_context.output_columns = &read_params.output_columns; |
264 | 331 | _reader_context.push_down_agg_type_opt = read_params.push_down_agg_type_opt; |
265 | 331 | _reader_context.ttl_seconds = _tablet->ttl_seconds(); |
266 | | |
267 | 331 | return Status::OK(); |
268 | 331 | } |
269 | | |
270 | 0 | TabletColumn TabletReader::materialize_column(const TabletColumn& orig) { |
271 | 0 | if (!orig.is_variant_type()) { |
272 | 0 | return orig; |
273 | 0 | } |
274 | 0 | TabletColumn column_with_cast_type = orig; |
275 | 0 | auto cast_type = _reader_context.target_cast_type_for_variants.at(orig.name()); |
276 | 0 | FieldType filed_type = TabletColumn::get_field_type_by_type(cast_type.type); |
277 | 0 | if (filed_type == FieldType::OLAP_FIELD_TYPE_UNKNOWN) { |
278 | 0 | throw doris::Exception(ErrorCode::INTERNAL_ERROR, "Invalid type for variant column: {}", |
279 | 0 | cast_type.type); |
280 | 0 | } |
281 | 0 | column_with_cast_type.set_type(filed_type); |
282 | 0 | return column_with_cast_type; |
283 | 0 | } |
284 | | |
285 | 353 | Status TabletReader::_init_params(const ReaderParams& read_params) { |
286 | 353 | read_params.check_validation(); |
287 | | |
288 | 353 | _direct_mode = read_params.direct_mode; |
289 | 353 | _aggregation = read_params.aggregation; |
290 | 353 | _reader_type = read_params.reader_type; |
291 | 353 | _tablet = read_params.tablet; |
292 | 353 | _tablet_schema = read_params.tablet_schema; |
293 | 353 | _reader_context.runtime_state = read_params.runtime_state; |
294 | 353 | _reader_context.target_cast_type_for_variants = read_params.target_cast_type_for_variants; |
295 | | |
296 | 353 | RETURN_IF_ERROR(_init_conditions_param(read_params)); |
297 | | |
298 | 353 | Status res = _init_delete_condition(read_params); |
299 | 353 | if (!res.ok()) { |
300 | 0 | LOG(WARNING) << "fail to init delete param. res = " << res; |
301 | 0 | return res; |
302 | 0 | } |
303 | | |
304 | 353 | res = _init_return_columns(read_params); |
305 | 353 | if (!res.ok()) { |
306 | 0 | LOG(WARNING) << "fail to init return columns. res = " << res; |
307 | 0 | return res; |
308 | 0 | } |
309 | | |
310 | 353 | res = _init_keys_param(read_params); |
311 | 353 | if (!res.ok()) { |
312 | 0 | LOG(WARNING) << "fail to init keys param. res=" << res; |
313 | 0 | return res; |
314 | 0 | } |
315 | 353 | res = _init_orderby_keys_param(read_params); |
316 | 353 | if (!res.ok()) { |
317 | 0 | LOG(WARNING) << "fail to init orderby keys param. res=" << res; |
318 | 0 | return res; |
319 | 0 | } |
320 | 353 | if (_tablet_schema->has_sequence_col()) { |
321 | 8 | auto sequence_col_idx = _tablet_schema->sequence_col_idx(); |
322 | 8 | DCHECK_NE(sequence_col_idx, -1); |
323 | 16 | for (auto col : _return_columns) { |
324 | | // query has sequence col |
325 | 16 | if (col == sequence_col_idx) { |
326 | 4 | _sequence_col_idx = sequence_col_idx; |
327 | 4 | break; |
328 | 4 | } |
329 | 16 | } |
330 | 8 | } |
331 | | |
332 | 353 | return res; |
333 | 353 | } |
334 | | |
335 | 353 | Status TabletReader::_init_return_columns(const ReaderParams& read_params) { |
336 | 353 | SCOPED_RAW_TIMER(&_stats.tablet_reader_init_return_columns_timer_ns); |
337 | 353 | if (read_params.reader_type == ReaderType::READER_QUERY) { |
338 | 22 | _return_columns = read_params.return_columns; |
339 | 22 | _tablet_columns_convert_to_null_set = read_params.tablet_columns_convert_to_null_set; |
340 | 37 | for (auto id : read_params.return_columns) { |
341 | 37 | if (_tablet_schema->column(id).is_key()) { |
342 | 22 | _key_cids.push_back(id); |
343 | 22 | } else { |
344 | 15 | _value_cids.push_back(id); |
345 | 15 | } |
346 | 37 | } |
347 | 331 | } else if (read_params.return_columns.empty()) { |
348 | 0 | for (size_t i = 0; i < _tablet_schema->num_columns(); ++i) { |
349 | 0 | _return_columns.push_back(i); |
350 | 0 | if (_tablet_schema->column(i).is_key()) { |
351 | 0 | _key_cids.push_back(i); |
352 | 0 | } else { |
353 | 0 | _value_cids.push_back(i); |
354 | 0 | } |
355 | 0 | } |
356 | 0 | VLOG_NOTICE << "return column is empty, using full column as default."; |
357 | 331 | } else if ((read_params.reader_type == ReaderType::READER_CUMULATIVE_COMPACTION || |
358 | 331 | read_params.reader_type == ReaderType::READER_SEGMENT_COMPACTION || |
359 | 331 | read_params.reader_type == ReaderType::READER_BASE_COMPACTION || |
360 | 331 | read_params.reader_type == ReaderType::READER_FULL_COMPACTION || |
361 | 331 | read_params.reader_type == ReaderType::READER_COLD_DATA_COMPACTION || |
362 | 331 | read_params.reader_type == ReaderType::READER_ALTER_TABLE) && |
363 | 331 | !read_params.return_columns.empty()) { |
364 | 331 | _return_columns = read_params.return_columns; |
365 | 1.05k | for (auto id : read_params.return_columns) { |
366 | 1.05k | if (_tablet_schema->column(id).is_key()) { |
367 | 112 | _key_cids.push_back(id); |
368 | 943 | } else { |
369 | 943 | _value_cids.push_back(id); |
370 | 943 | } |
371 | 1.05k | } |
372 | 331 | } else if (read_params.reader_type == ReaderType::READER_CHECKSUM) { |
373 | 0 | _return_columns = read_params.return_columns; |
374 | 0 | for (auto id : read_params.return_columns) { |
375 | 0 | if (_tablet_schema->column(id).is_key()) { |
376 | 0 | _key_cids.push_back(id); |
377 | 0 | } else { |
378 | 0 | _value_cids.push_back(id); |
379 | 0 | } |
380 | 0 | } |
381 | 0 | } else { |
382 | 0 | return Status::Error<INVALID_ARGUMENT>( |
383 | 0 | "fail to init return columns. reader_type={}, return_columns_size={}", |
384 | 0 | int(read_params.reader_type), read_params.return_columns.size()); |
385 | 0 | } |
386 | | |
387 | 353 | std::sort(_key_cids.begin(), _key_cids.end(), std::greater<>()); |
388 | | |
389 | 353 | return Status::OK(); |
390 | 353 | } |
391 | | |
392 | 353 | Status TabletReader::_init_keys_param(const ReaderParams& read_params) { |
393 | 353 | SCOPED_RAW_TIMER(&_stats.tablet_reader_init_keys_param_timer_ns); |
394 | 353 | if (read_params.start_key.empty()) { |
395 | 353 | return Status::OK(); |
396 | 353 | } |
397 | | |
398 | 0 | _keys_param.start_key_include = read_params.start_key_include; |
399 | 0 | _keys_param.end_key_include = read_params.end_key_include; |
400 | |
|
401 | 0 | size_t start_key_size = read_params.start_key.size(); |
402 | | //_keys_param.start_keys.resize(start_key_size); |
403 | 0 | std::vector<RowCursor>(start_key_size).swap(_keys_param.start_keys); |
404 | |
|
405 | 0 | size_t scan_key_size = read_params.start_key.front().size(); |
406 | 0 | if (scan_key_size > _tablet_schema->num_columns()) { |
407 | 0 | return Status::Error<INVALID_ARGUMENT>( |
408 | 0 | "Input param are invalid. Column count is bigger than num_columns of schema. " |
409 | 0 | "column_count={}, schema.num_columns={}", |
410 | 0 | scan_key_size, _tablet_schema->num_columns()); |
411 | 0 | } |
412 | | |
413 | 0 | std::vector<uint32_t> columns(scan_key_size); |
414 | 0 | std::iota(columns.begin(), columns.end(), 0); |
415 | |
|
416 | 0 | std::shared_ptr<Schema> schema = std::make_shared<Schema>(_tablet_schema->columns(), columns); |
417 | |
|
418 | 0 | for (size_t i = 0; i < start_key_size; ++i) { |
419 | 0 | if (read_params.start_key[i].size() != scan_key_size) { |
420 | 0 | return Status::Error<INVALID_ARGUMENT>( |
421 | 0 | "The start_key.at({}).size={}, not equals the scan_key_size={}", i, |
422 | 0 | read_params.start_key[i].size(), scan_key_size); |
423 | 0 | } |
424 | | |
425 | 0 | Status res = _keys_param.start_keys[i].init_scan_key( |
426 | 0 | _tablet_schema, read_params.start_key[i].values(), schema); |
427 | 0 | if (!res.ok()) { |
428 | 0 | LOG(WARNING) << "fail to init row cursor. res = " << res; |
429 | 0 | return res; |
430 | 0 | } |
431 | 0 | res = _keys_param.start_keys[i].from_tuple(read_params.start_key[i]); |
432 | 0 | if (!res.ok()) { |
433 | 0 | LOG(WARNING) << "fail to init row cursor from Keys. res=" << res << "key_index=" << i; |
434 | 0 | return res; |
435 | 0 | } |
436 | 0 | } |
437 | | |
438 | 0 | size_t end_key_size = read_params.end_key.size(); |
439 | | //_keys_param.end_keys.resize(end_key_size); |
440 | 0 | std::vector<RowCursor>(end_key_size).swap(_keys_param.end_keys); |
441 | 0 | for (size_t i = 0; i < end_key_size; ++i) { |
442 | 0 | if (read_params.end_key[i].size() != scan_key_size) { |
443 | 0 | return Status::Error<INVALID_ARGUMENT>( |
444 | 0 | "The end_key.at({}).size={}, not equals the scan_key_size={}", i, |
445 | 0 | read_params.end_key[i].size(), scan_key_size); |
446 | 0 | } |
447 | | |
448 | 0 | Status res = _keys_param.end_keys[i].init_scan_key(_tablet_schema, |
449 | 0 | read_params.end_key[i].values(), schema); |
450 | 0 | if (!res.ok()) { |
451 | 0 | LOG(WARNING) << "fail to init row cursor. res = " << res; |
452 | 0 | return res; |
453 | 0 | } |
454 | | |
455 | 0 | res = _keys_param.end_keys[i].from_tuple(read_params.end_key[i]); |
456 | 0 | if (!res.ok()) { |
457 | 0 | LOG(WARNING) << "fail to init row cursor from Keys. res=" << res << " key_index=" << i; |
458 | 0 | return res; |
459 | 0 | } |
460 | 0 | } |
461 | | |
462 | | //TODO:check the valid of start_key and end_key.(eg. start_key <= end_key) |
463 | | |
464 | 0 | return Status::OK(); |
465 | 0 | } |
466 | | |
467 | 353 | Status TabletReader::_init_orderby_keys_param(const ReaderParams& read_params) { |
468 | 353 | SCOPED_RAW_TIMER(&_stats.tablet_reader_init_orderby_keys_param_timer_ns); |
469 | | // UNIQUE_KEYS will compare all keys as before |
470 | 353 | if (_tablet_schema->keys_type() == DUP_KEYS || (_tablet_schema->keys_type() == UNIQUE_KEYS && |
471 | 293 | _tablet->enable_unique_key_merge_on_write())) { |
472 | 293 | if (!_tablet_schema->cluster_key_uids().empty()) { |
473 | 0 | if (read_params.read_orderby_key_num_prefix_columns > |
474 | 0 | _tablet_schema->cluster_key_uids().size()) { |
475 | 0 | return Status::Error<ErrorCode::INTERNAL_ERROR>( |
476 | 0 | "read_orderby_key_num_prefix_columns={} > cluster_keys.size()={}", |
477 | 0 | read_params.read_orderby_key_num_prefix_columns, |
478 | 0 | _tablet_schema->cluster_key_uids().size()); |
479 | 0 | } |
480 | 0 | for (uint32_t i = 0; i < read_params.read_orderby_key_num_prefix_columns; i++) { |
481 | 0 | auto cid = _tablet_schema->cluster_key_uids()[i]; |
482 | 0 | auto index = _tablet_schema->field_index(cid); |
483 | 0 | if (index < 0) { |
484 | 0 | return Status::Error<ErrorCode::INTERNAL_ERROR>( |
485 | 0 | "could not find cluster key column with unique_id=" + |
486 | 0 | std::to_string(cid) + |
487 | 0 | " in tablet schema, tablet_id=" + std::to_string(_tablet->tablet_id())); |
488 | 0 | } |
489 | 0 | for (uint32_t idx = 0; idx < _return_columns.size(); idx++) { |
490 | 0 | if (_return_columns[idx] == index) { |
491 | 0 | _orderby_key_columns.push_back(idx); |
492 | 0 | break; |
493 | 0 | } |
494 | 0 | } |
495 | 0 | } |
496 | 293 | } else { |
497 | | // find index in vector _return_columns |
498 | | // for the read_orderby_key_num_prefix_columns orderby keys |
499 | 293 | for (uint32_t i = 0; i < read_params.read_orderby_key_num_prefix_columns; i++) { |
500 | 0 | for (uint32_t idx = 0; idx < _return_columns.size(); idx++) { |
501 | 0 | if (_return_columns[idx] == i) { |
502 | 0 | _orderby_key_columns.push_back(idx); |
503 | 0 | break; |
504 | 0 | } |
505 | 0 | } |
506 | 0 | } |
507 | 293 | } |
508 | 293 | if (read_params.read_orderby_key_num_prefix_columns != _orderby_key_columns.size()) { |
509 | 0 | return Status::Error<ErrorCode::INTERNAL_ERROR>( |
510 | 0 | "read_orderby_key_num_prefix_columns != _orderby_key_columns.size, " |
511 | 0 | "read_params.read_orderby_key_num_prefix_columns={}, " |
512 | 0 | "_orderby_key_columns.size()={}", |
513 | 0 | read_params.read_orderby_key_num_prefix_columns, _orderby_key_columns.size()); |
514 | 0 | } |
515 | 293 | } |
516 | | |
517 | 353 | return Status::OK(); |
518 | 353 | } |
519 | | |
520 | 353 | Status TabletReader::_init_conditions_param(const ReaderParams& read_params) { |
521 | 353 | SCOPED_RAW_TIMER(&_stats.tablet_reader_init_conditions_param_timer_ns); |
522 | 353 | std::vector<ColumnPredicate*> predicates; |
523 | | |
524 | 1.05k | auto parse_and_emplace_predicates = [this, &predicates](auto& params) { |
525 | 1.05k | for (const auto& param : params) { |
526 | 0 | ColumnPredicate* predicate = _parse_to_predicate({param.column_name, param.filter}); |
527 | 0 | predicate->attach_profile_counter(param.runtime_filter_id, param.filtered_rows_counter, |
528 | 0 | param.input_rows_counter); |
529 | 0 | predicates.emplace_back(predicate); |
530 | 0 | } |
531 | 1.05k | }; tablet_reader.cpp:_ZZN5doris12TabletReader22_init_conditions_paramERKNS0_12ReaderParamsEENK3$_0clIKSt6vectorINS_15FilterOlapParamISt10shared_ptrINS_19BloomFilterFuncBaseEEEESaISB_EEEEDaRT_ Line | Count | Source | 524 | 353 | auto parse_and_emplace_predicates = [this, &predicates](auto& params) { | 525 | 353 | for (const auto& param : params) { | 526 | 0 | ColumnPredicate* predicate = _parse_to_predicate({param.column_name, param.filter}); | 527 | 0 | predicate->attach_profile_counter(param.runtime_filter_id, param.filtered_rows_counter, | 528 | 0 | param.input_rows_counter); | 529 | 0 | predicates.emplace_back(predicate); | 530 | 0 | } | 531 | 353 | }; |
tablet_reader.cpp:_ZZN5doris12TabletReader22_init_conditions_paramERKNS0_12ReaderParamsEENK3$_0clIKSt6vectorINS_15FilterOlapParamISt10shared_ptrINS_20BitmapFilterFuncBaseEEEESaISB_EEEEDaRT_ Line | Count | Source | 524 | 353 | auto parse_and_emplace_predicates = [this, &predicates](auto& params) { | 525 | 353 | for (const auto& param : params) { | 526 | 0 | ColumnPredicate* predicate = _parse_to_predicate({param.column_name, param.filter}); | 527 | 0 | predicate->attach_profile_counter(param.runtime_filter_id, param.filtered_rows_counter, | 528 | 0 | param.input_rows_counter); | 529 | 0 | predicates.emplace_back(predicate); | 530 | 0 | } | 531 | 353 | }; |
tablet_reader.cpp:_ZZN5doris12TabletReader22_init_conditions_paramERKNS0_12ReaderParamsEENK3$_0clIKSt6vectorINS_15FilterOlapParamISt10shared_ptrINS_13HybridSetBaseEEEESaISB_EEEEDaRT_ Line | Count | Source | 524 | 353 | auto parse_and_emplace_predicates = [this, &predicates](auto& params) { | 525 | 353 | for (const auto& param : params) { | 526 | 0 | ColumnPredicate* predicate = _parse_to_predicate({param.column_name, param.filter}); | 527 | 0 | predicate->attach_profile_counter(param.runtime_filter_id, param.filtered_rows_counter, | 528 | 0 | param.input_rows_counter); | 529 | 0 | predicates.emplace_back(predicate); | 530 | 0 | } | 531 | 353 | }; |
|
532 | | |
533 | 353 | for (const auto& param : read_params.conditions) { |
534 | 0 | TCondition tmp_cond = param.filter; |
535 | 0 | RETURN_IF_ERROR(_tablet_schema->have_column(tmp_cond.column_name)); |
536 | | // The "column" parameter might represent a column resulting from the decomposition of a variant column. |
537 | | // Instead of using a "unique_id" for identification, we are utilizing a "path" to denote this column. |
538 | 0 | const auto& column = *DORIS_TRY(_tablet_schema->column(tmp_cond.column_name)); |
539 | 0 | const auto& mcolumn = materialize_column(column); |
540 | 0 | uint32_t index = _tablet_schema->field_index(tmp_cond.column_name); |
541 | 0 | ColumnPredicate* predicate = |
542 | 0 | parse_to_predicate(mcolumn, index, tmp_cond, _predicate_arena.get()); |
543 | | // record condition value into predicate_params in order to pushdown segment_iterator, |
544 | | // _gen_predicate_result_sign will build predicate result unique sign with condition value |
545 | 0 | predicate->attach_profile_counter(param.runtime_filter_id, param.filtered_rows_counter, |
546 | 0 | param.input_rows_counter); |
547 | 0 | predicates.emplace_back(predicate); |
548 | 0 | } |
549 | 353 | parse_and_emplace_predicates(read_params.bloom_filters); |
550 | 353 | parse_and_emplace_predicates(read_params.bitmap_filters); |
551 | 353 | parse_and_emplace_predicates(read_params.in_filters); |
552 | | |
553 | | // Function filter push down to storage engine |
554 | 353 | auto is_like_predicate = [](ColumnPredicate* _pred) { |
555 | 0 | return dynamic_cast<LikeColumnPredicate<TYPE_CHAR>*>(_pred) != nullptr || |
556 | 0 | dynamic_cast<LikeColumnPredicate<TYPE_STRING>*>(_pred) != nullptr; |
557 | 0 | }; |
558 | | |
559 | 353 | for (const auto& filter : read_params.function_filters) { |
560 | 0 | predicates.emplace_back(_parse_to_predicate(filter)); |
561 | 0 | auto* pred = predicates.back(); |
562 | |
|
563 | 0 | const auto& col = _tablet_schema->column(pred->column_id()); |
564 | 0 | const auto* tablet_index = _tablet_schema->get_ngram_bf_index(col.unique_id()); |
565 | 0 | if (is_like_predicate(pred) && tablet_index && config::enable_query_like_bloom_filter) { |
566 | 0 | std::unique_ptr<segment_v2::BloomFilter> ng_bf; |
567 | 0 | std::string pattern = pred->get_search_str(); |
568 | 0 | auto gram_bf_size = tablet_index->get_gram_bf_size(); |
569 | 0 | auto gram_size = tablet_index->get_gram_size(); |
570 | |
|
571 | 0 | RETURN_IF_ERROR(segment_v2::BloomFilter::create(segment_v2::NGRAM_BLOOM_FILTER, &ng_bf, |
572 | 0 | gram_bf_size)); |
573 | 0 | NgramTokenExtractor _token_extractor(gram_size); |
574 | |
|
575 | 0 | if (_token_extractor.string_like_to_bloom_filter(pattern.data(), pattern.length(), |
576 | 0 | *ng_bf)) { |
577 | 0 | pred->set_page_ng_bf(std::move(ng_bf)); |
578 | 0 | } |
579 | 0 | } |
580 | 0 | } |
581 | | |
582 | 353 | for (auto* predicate : predicates) { |
583 | 0 | auto column = _tablet_schema->column(predicate->column_id()); |
584 | 0 | if (column.aggregation() != FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE) { |
585 | 0 | _value_col_predicates.push_back(predicate); |
586 | 0 | } else { |
587 | 0 | _col_predicates.push_back(predicate); |
588 | 0 | } |
589 | 0 | } |
590 | | |
591 | 353 | for (int id : read_params.topn_filter_source_node_ids) { |
592 | 0 | auto& runtime_predicate = |
593 | 0 | read_params.runtime_state->get_query_ctx()->get_runtime_predicate(id); |
594 | 0 | RETURN_IF_ERROR(runtime_predicate.set_tablet_schema(read_params.topn_filter_target_node_id, |
595 | 0 | _tablet_schema)); |
596 | 0 | } |
597 | 353 | return Status::OK(); |
598 | 353 | } |
599 | | |
600 | | ColumnPredicate* TabletReader::_parse_to_predicate( |
601 | 0 | const std::pair<std::string, std::shared_ptr<BloomFilterFuncBase>>& bloom_filter) { |
602 | 0 | int32_t index = _tablet_schema->field_index(bloom_filter.first); |
603 | 0 | if (index < 0) { |
604 | 0 | return nullptr; |
605 | 0 | } |
606 | 0 | const TabletColumn& column = materialize_column(_tablet_schema->column(index)); |
607 | 0 | return create_column_predicate(index, bloom_filter.second, column.type(), &column); |
608 | 0 | } |
609 | | |
610 | | ColumnPredicate* TabletReader::_parse_to_predicate( |
611 | 0 | const std::pair<std::string, std::shared_ptr<HybridSetBase>>& in_filter) { |
612 | 0 | int32_t index = _tablet_schema->field_index(in_filter.first); |
613 | 0 | if (index < 0) { |
614 | 0 | return nullptr; |
615 | 0 | } |
616 | 0 | const TabletColumn& column = materialize_column(_tablet_schema->column(index)); |
617 | 0 | return create_column_predicate(index, in_filter.second, column.type(), &column); |
618 | 0 | } |
619 | | |
620 | | ColumnPredicate* TabletReader::_parse_to_predicate( |
621 | 0 | const std::pair<std::string, std::shared_ptr<BitmapFilterFuncBase>>& bitmap_filter) { |
622 | 0 | int32_t index = _tablet_schema->field_index(bitmap_filter.first); |
623 | 0 | if (index < 0) { |
624 | 0 | return nullptr; |
625 | 0 | } |
626 | 0 | const TabletColumn& column = materialize_column(_tablet_schema->column(index)); |
627 | 0 | return create_column_predicate(index, bitmap_filter.second, column.type(), &column); |
628 | 0 | } |
629 | | |
630 | 0 | ColumnPredicate* TabletReader::_parse_to_predicate(const FunctionFilter& function_filter) { |
631 | 0 | int32_t index = _tablet_schema->field_index(function_filter._col_name); |
632 | 0 | if (index < 0) { |
633 | 0 | return nullptr; |
634 | 0 | } |
635 | 0 | const TabletColumn& column = materialize_column(_tablet_schema->column(index)); |
636 | 0 | return create_column_predicate(index, std::make_shared<FunctionFilter>(function_filter), |
637 | 0 | column.type(), &column); |
638 | 0 | } |
639 | | |
640 | 353 | Status TabletReader::_init_delete_condition(const ReaderParams& read_params) { |
641 | 353 | SCOPED_RAW_TIMER(&_stats.tablet_reader_init_delete_condition_param_timer_ns); |
642 | | // If it's cumu and not allow do delete when cumu |
643 | 353 | if (read_params.reader_type == ReaderType::READER_SEGMENT_COMPACTION || |
644 | 353 | (read_params.reader_type == ReaderType::READER_CUMULATIVE_COMPACTION && |
645 | 353 | !config::enable_delete_when_cumu_compaction)) { |
646 | 0 | return Status::OK(); |
647 | 0 | } |
648 | 353 | bool cumu_delete = read_params.reader_type == ReaderType::READER_CUMULATIVE_COMPACTION && |
649 | 353 | config::enable_delete_when_cumu_compaction; |
650 | | // Delete sign could not be applied when delete on cumu compaction is enabled, bucause it is meant for delete with predicates. |
651 | | // If delete design is applied on cumu compaction, it will lose effect when doing base compaction. |
652 | | // `_delete_sign_available` indicates the condition where we could apply delete signs to data. |
653 | 353 | _delete_sign_available = ((read_params.reader_type == ReaderType::READER_BASE_COMPACTION && |
654 | 353 | config::enable_prune_delete_sign_when_base_compaction) || |
655 | 353 | read_params.reader_type == ReaderType::READER_COLD_DATA_COMPACTION || |
656 | 353 | read_params.reader_type == ReaderType::READER_CHECKSUM); |
657 | | |
658 | | // `_filter_delete` indicates the condition where we should execlude deleted tuples when reading data. |
659 | | // However, queries will not use this condition but generate special where predicates to filter data. |
660 | | // (Though a lille bit confused, it is how the current logic working...) |
661 | 353 | _filter_delete = _delete_sign_available || cumu_delete; |
662 | 353 | return _delete_handler.init(_tablet_schema, read_params.delete_predicates, |
663 | 353 | read_params.version.second); |
664 | 353 | } |
665 | | |
666 | | Status TabletReader::init_reader_params_and_create_block( |
667 | | TabletSharedPtr tablet, ReaderType reader_type, |
668 | | const std::vector<RowsetSharedPtr>& input_rowsets, |
669 | 0 | TabletReader::ReaderParams* reader_params, vectorized::Block* block) { |
670 | 0 | reader_params->tablet = tablet; |
671 | 0 | reader_params->reader_type = reader_type; |
672 | 0 | reader_params->version = |
673 | 0 | Version(input_rowsets.front()->start_version(), input_rowsets.back()->end_version()); |
674 | |
|
675 | 0 | ReadSource read_source; |
676 | 0 | for (const auto& rowset : input_rowsets) { |
677 | 0 | RowsetReaderSharedPtr rs_reader; |
678 | 0 | RETURN_IF_ERROR(rowset->create_reader(&rs_reader)); |
679 | 0 | read_source.rs_splits.emplace_back(std::move(rs_reader)); |
680 | 0 | } |
681 | 0 | read_source.fill_delete_predicates(); |
682 | 0 | reader_params->set_read_source(std::move(read_source)); |
683 | |
|
684 | 0 | std::vector<RowsetMetaSharedPtr> rowset_metas(input_rowsets.size()); |
685 | 0 | std::transform(input_rowsets.begin(), input_rowsets.end(), rowset_metas.begin(), |
686 | 0 | [](const RowsetSharedPtr& rowset) { return rowset->rowset_meta(); }); |
687 | 0 | TabletSchemaSPtr read_tablet_schema = |
688 | 0 | tablet->tablet_schema_with_merged_max_schema_version(rowset_metas); |
689 | 0 | TabletSchemaSPtr merge_tablet_schema = std::make_shared<TabletSchema>(); |
690 | 0 | merge_tablet_schema->copy_from(*read_tablet_schema); |
691 | | |
692 | | // Merge the columns in delete predicate that not in latest schema in to current tablet schema |
693 | 0 | for (auto& del_pred : reader_params->delete_predicates) { |
694 | 0 | merge_tablet_schema->merge_dropped_columns(*del_pred->tablet_schema()); |
695 | 0 | } |
696 | 0 | reader_params->tablet_schema = merge_tablet_schema; |
697 | 0 | if (tablet->enable_unique_key_merge_on_write()) { |
698 | 0 | reader_params->delete_bitmap = &tablet->tablet_meta()->delete_bitmap(); |
699 | 0 | } |
700 | |
|
701 | 0 | reader_params->return_columns.resize(read_tablet_schema->num_columns()); |
702 | 0 | std::iota(reader_params->return_columns.begin(), reader_params->return_columns.end(), 0); |
703 | 0 | reader_params->origin_return_columns = &reader_params->return_columns; |
704 | |
|
705 | 0 | *block = read_tablet_schema->create_block(); |
706 | |
|
707 | 0 | return Status::OK(); |
708 | 0 | } |
709 | | |
710 | | } // namespace doris |