be/src/exec/scan/file_scanner_v2.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "exec/scan/file_scanner_v2.h" |
19 | | |
20 | | #include <gen_cpp/Exprs_types.h> |
21 | | #include <gen_cpp/PlanNodes_types.h> |
22 | | |
23 | | #include <algorithm> |
24 | | #include <map> |
25 | | #include <memory> |
26 | | #include <optional> |
27 | | #include <string> |
28 | | #include <utility> |
29 | | |
30 | | #include "common/cast_set.h" |
31 | | #include "common/config.h" |
32 | | #include "common/consts.h" |
33 | | #include "common/metrics/doris_metrics.h" |
34 | | #include "common/status.h" |
35 | | #include "core/assert_cast.h" |
36 | | #include "core/block/column_with_type_and_name.h" |
37 | | #include "core/column/column.h" |
38 | | #include "core/data_type/data_type.h" |
39 | | #include "core/data_type/data_type_nullable.h" |
40 | | #include "core/data_type_serde/data_type_serde.h" |
41 | | #include "core/string_ref.h" |
42 | | #include "exec/common/util.hpp" |
43 | | #include "exec/operator/scan_operator.h" |
44 | | #include "exec/scan/access_path_parser.h" |
45 | | #include "exec/scan/file_scan_io_context.h" |
46 | | #include "exprs/runtime_filter_expr.h" |
47 | | #include "exprs/vexpr.h" |
48 | | #include "exprs/vexpr_context.h" |
49 | | #include "exprs/vslot_ref.h" |
50 | | #include "format/format_common.h" |
51 | | #include "format_v2/column_mapper.h" |
52 | | #include "format_v2/jni/iceberg_sys_table_reader.h" |
53 | | #include "format_v2/jni/jdbc_reader.h" |
54 | | #include "format_v2/jni/max_compute_jni_reader.h" |
55 | | #include "format_v2/jni/trino_connector_jni_reader.h" |
56 | | #include "format_v2/table/hive_reader.h" |
57 | | #include "format_v2/table/hudi_reader.h" |
58 | | #include "format_v2/table/iceberg_reader.h" |
59 | | #include "format_v2/table/paimon_reader.h" |
60 | | #include "format_v2/table/remote_doris_reader.h" |
61 | | #include "format_v2/table_reader.h" |
62 | | #include "io/fs/file_meta_cache.h" |
63 | | #include "io/io_common.h" |
64 | | #include "runtime/descriptors.h" |
65 | | #include "runtime/exec_env.h" |
66 | | #include "runtime/runtime_state.h" |
67 | | #include "service/backend_options.h" |
68 | | #include "storage/id_manager.h" |
69 | | |
70 | | namespace doris { |
71 | | namespace { |
72 | | |
73 | 37 | std::string table_format_name(const TFileRangeDesc& range) { |
74 | 37 | return range.__isset.table_format_params ? range.table_format_params.table_format_type |
75 | 37 | : "NotSet"; |
76 | 37 | } |
77 | | |
78 | | TFileFormatType::type get_range_format_type(const TFileScanRangeParams& params, |
79 | 41 | const TFileRangeDesc& range) { |
80 | 41 | return range.__isset.format_type ? range.format_type : params.format_type; |
81 | 41 | } |
82 | | |
83 | 31 | bool is_supported_table_format(const TFileRangeDesc& range) { |
84 | 31 | const auto table_format = table_format_name(range); |
85 | 31 | if (table_format == "hudi" && range.__isset.table_format_params && |
86 | 31 | range.table_format_params.__isset.hudi_params && |
87 | 31 | range.table_format_params.hudi_params.__isset.delta_logs && |
88 | 31 | !range.table_format_params.hudi_params.delta_logs.empty()) { |
89 | | // Hudi MOR splits need log-file merge semantics and must stay on the existing JNI path. |
90 | | // FileScannerV2 currently supports native Parquet data files only. |
91 | 1 | return false; |
92 | 1 | } |
93 | 30 | return table_format == "NotSet" || table_format == "tvf" || table_format == "hive" || |
94 | 30 | table_format == "iceberg" || table_format == "paimon" || table_format == "hudi"; |
95 | 31 | } |
96 | | |
97 | 3 | bool is_supported_arrow_table_format(const TFileRangeDesc& range) { |
98 | 3 | return table_format_name(range) == "remote_doris"; |
99 | 3 | } |
100 | | |
101 | 3 | bool is_supported_jni_table_format(const TFileRangeDesc& range) { |
102 | 3 | const auto table_format = table_format_name(range); |
103 | 3 | if (table_format == "paimon") { |
104 | 0 | return range.__isset.table_format_params && |
105 | 0 | range.table_format_params.__isset.paimon_params && |
106 | 0 | range.table_format_params.paimon_params.__isset.reader_type && |
107 | 0 | range.table_format_params.paimon_params.reader_type == TPaimonReaderType::PAIMON_JNI; |
108 | 0 | } |
109 | 3 | return table_format == "jdbc" || table_format == "iceberg" || table_format == "hudi" || |
110 | 3 | table_format == "max_compute" || table_format == "trino_connector"; |
111 | 3 | } |
112 | | |
113 | 19 | bool is_csv_format(TFileFormatType::type format_type) { |
114 | 19 | switch (format_type) { |
115 | 2 | case TFileFormatType::FORMAT_CSV_PLAIN: |
116 | 3 | case TFileFormatType::FORMAT_CSV_GZ: |
117 | 4 | case TFileFormatType::FORMAT_CSV_BZ2: |
118 | 5 | case TFileFormatType::FORMAT_CSV_LZ4FRAME: |
119 | 6 | case TFileFormatType::FORMAT_CSV_LZ4BLOCK: |
120 | 7 | case TFileFormatType::FORMAT_CSV_LZOP: |
121 | 8 | case TFileFormatType::FORMAT_CSV_DEFLATE: |
122 | 9 | case TFileFormatType::FORMAT_CSV_SNAPPYBLOCK: |
123 | 10 | case TFileFormatType::FORMAT_PROTO: |
124 | 10 | return true; |
125 | 9 | default: |
126 | 9 | return false; |
127 | 19 | } |
128 | 19 | } |
129 | | |
130 | 9 | bool is_text_format(TFileFormatType::type format_type) { |
131 | 9 | return format_type == TFileFormatType::FORMAT_TEXT; |
132 | 9 | } |
133 | | |
134 | 7 | bool is_json_format(TFileFormatType::type format_type) { |
135 | 7 | return format_type == TFileFormatType::FORMAT_JSON; |
136 | 7 | } |
137 | | |
138 | 5 | bool is_native_format(TFileFormatType::type format_type) { |
139 | 5 | return format_type == TFileFormatType::FORMAT_NATIVE; |
140 | 5 | } |
141 | | |
142 | 6 | bool is_partition_slot(const TFileScanSlotInfo& slot_info, const std::string& column_name) { |
143 | 6 | if (column_name.starts_with(BeConsts::GLOBAL_ROWID_COL) || |
144 | 6 | column_name == BeConsts::ICEBERG_ROWID_COL) { |
145 | 2 | return false; |
146 | 2 | } |
147 | 4 | return slot_info.__isset.category ? slot_info.category == TColumnCategory::PARTITION_KEY |
148 | 4 | : !slot_info.is_file_slot; |
149 | 6 | } |
150 | | |
151 | 8 | bool is_data_file_slot(const TFileScanSlotInfo& slot_info, const std::string& column_name) { |
152 | 8 | if (column_name.starts_with(BeConsts::GLOBAL_ROWID_COL) || |
153 | 8 | column_name == BeConsts::ICEBERG_ROWID_COL) { |
154 | 2 | return false; |
155 | 2 | } |
156 | | // CSV and other non-self-describing formats need FE slot descriptors for only the columns that |
157 | | // are physically read from the file. Partition/default/virtual columns stay in TableReader's |
158 | | // mapping layer and are materialized after the file-local block is read. New FE provides an |
159 | | // explicit category; old FE falls back to `is_file_slot`. |
160 | 6 | if (slot_info.__isset.category) { |
161 | 4 | return slot_info.category == TColumnCategory::REGULAR || |
162 | 4 | slot_info.category == TColumnCategory::GENERATED; |
163 | 4 | } |
164 | 2 | return slot_info.is_file_slot; |
165 | 6 | } |
166 | | |
167 | | Status rewrite_slot_refs_to_global_index( |
168 | | VExprSPtr* expr, |
169 | 7 | const std::unordered_map<int32_t, format::GlobalIndex>& slot_id_to_global_index) { |
170 | 7 | DORIS_CHECK(expr != nullptr); |
171 | 7 | if (*expr == nullptr) { |
172 | 0 | return Status::OK(); |
173 | 0 | } |
174 | 7 | if (auto* runtime_filter = dynamic_cast<RuntimeFilterExpr*>(expr->get()); |
175 | 7 | runtime_filter != nullptr) { |
176 | 1 | auto impl = runtime_filter->get_impl(); |
177 | 1 | DORIS_CHECK(impl != nullptr); |
178 | 1 | RETURN_IF_ERROR(rewrite_slot_refs_to_global_index(&impl, slot_id_to_global_index)); |
179 | 1 | runtime_filter->set_impl(std::move(impl)); |
180 | 1 | return Status::OK(); |
181 | 1 | } |
182 | 6 | if ((*expr)->is_slot_ref()) { |
183 | 4 | const auto* slot_ref = assert_cast<const VSlotRef*>(expr->get()); |
184 | 4 | const auto global_index_it = slot_id_to_global_index.find(slot_ref->slot_id()); |
185 | 4 | if (global_index_it == slot_id_to_global_index.end()) { |
186 | 1 | DORIS_CHECK(slot_ref->slot_id() >= 0); |
187 | 1 | const auto global_index = format::GlobalIndex(cast_set<size_t>(slot_ref->slot_id())); |
188 | 1 | *expr = VSlotRef::create_shared(cast_set<int>(global_index.value()), |
189 | 1 | cast_set<int>(global_index.value()), -1, |
190 | 1 | slot_ref->data_type(), slot_ref->column_name()); |
191 | 1 | RETURN_IF_ERROR(expr->get()->prepare(nullptr, RowDescriptor(), nullptr)); |
192 | 1 | return Status::OK(); |
193 | 1 | } |
194 | 3 | const auto global_index = global_index_it->second; |
195 | 3 | *expr = VSlotRef::create_shared(cast_set<int>(global_index.value()), |
196 | 3 | cast_set<int>(global_index.value()), -1, |
197 | 3 | slot_ref->data_type(), slot_ref->column_name()); |
198 | 3 | RETURN_IF_ERROR(expr->get()->prepare(nullptr, RowDescriptor(), nullptr)); |
199 | 3 | return Status::OK(); |
200 | 3 | } |
201 | 2 | auto children = (*expr)->children(); |
202 | 2 | for (auto& child : children) { |
203 | 2 | if (child == nullptr) { |
204 | 0 | continue; |
205 | 0 | } |
206 | 2 | RETURN_IF_ERROR(rewrite_slot_refs_to_global_index(&child, slot_id_to_global_index)); |
207 | 2 | } |
208 | 2 | (*expr)->set_children(std::move(children)); |
209 | 2 | return Status::OK(); |
210 | 2 | } |
211 | | |
212 | | } // namespace |
213 | | |
214 | | #ifdef BE_TEST |
215 | | Status FileScannerV2::TEST_to_file_format(TFileFormatType::type format_type, |
216 | 16 | format::FileFormat* file_format) { |
217 | 16 | return _to_file_format(format_type, file_format); |
218 | 16 | } |
219 | | |
220 | | bool FileScannerV2::TEST_is_partition_slot(const TFileScanSlotInfo& slot_info, |
221 | 6 | const std::string& column_name) { |
222 | 6 | return is_partition_slot(slot_info, column_name); |
223 | 6 | } |
224 | | |
225 | | bool FileScannerV2::TEST_is_data_file_slot(const TFileScanSlotInfo& slot_info, |
226 | 8 | const std::string& column_name) { |
227 | 8 | return is_data_file_slot(slot_info, column_name); |
228 | 8 | } |
229 | | |
230 | | Status FileScannerV2::TEST_rewrite_slot_refs_to_global_index( |
231 | | VExprSPtr* expr, |
232 | 4 | const std::unordered_map<int32_t, format::GlobalIndex>& slot_id_to_global_index) { |
233 | 4 | return rewrite_slot_refs_to_global_index(expr, slot_id_to_global_index); |
234 | 4 | } |
235 | | |
236 | | FileScannerV2::RealtimeCounterDeltas FileScannerV2::TEST_collect_realtime_counter_deltas( |
237 | | const io::FileReaderStats& file_reader_stats, |
238 | | const io::FileCacheStatistics& file_cache_statistics, |
239 | | UncachedReaderBytesStorage uncached_reader_bytes_storage, int64_t* last_read_bytes, |
240 | | int64_t* last_read_rows, int64_t* last_bytes_read_from_local, |
241 | 7 | int64_t* last_bytes_read_from_remote) { |
242 | 7 | return _collect_realtime_counter_deltas(file_reader_stats, file_cache_statistics, |
243 | 7 | uncached_reader_bytes_storage, last_read_bytes, |
244 | 7 | last_read_rows, last_bytes_read_from_local, |
245 | 7 | last_bytes_read_from_remote); |
246 | 7 | } |
247 | | #endif |
248 | | |
249 | 41 | bool FileScannerV2::is_supported(const TFileScanRangeParams& params, const TFileRangeDesc& range) { |
250 | 41 | const auto format_type = get_range_format_type(params, range); |
251 | 41 | if (format_type == TFileFormatType::FORMAT_PARQUET) { |
252 | 16 | return is_supported_table_format(range); |
253 | 25 | } else if (format_type == TFileFormatType::FORMAT_ARROW) { |
254 | 3 | return is_supported_arrow_table_format(range); |
255 | 22 | } else if (format_type == TFileFormatType::FORMAT_JNI) { |
256 | 3 | return is_supported_jni_table_format(range); |
257 | 19 | } else if (is_csv_format(format_type) || is_text_format(format_type) || |
258 | 19 | is_json_format(format_type) || is_native_format(format_type)) { |
259 | 15 | return is_supported_table_format(range); |
260 | 15 | } else { |
261 | 4 | LOG(WARNING) << "Unsupported file format type " << format_type << " for file scanner v2"; |
262 | 4 | return false; |
263 | 4 | } |
264 | 41 | } |
265 | | |
266 | | FileScannerV2::FileScannerV2(RuntimeState* state, FileScanLocalState* local_state, int64_t limit, |
267 | | std::shared_ptr<SplitSourceConnector> split_source, |
268 | | RuntimeProfile* profile, ShardedKVCache* kv_cache, |
269 | | const std::unordered_map<std::string, int>* colname_to_slot_id) |
270 | 0 | : Scanner(state, local_state, limit, profile), |
271 | 0 | _split_source(std::move(split_source)), |
272 | 0 | _kv_cache(kv_cache) { |
273 | 0 | (void)colname_to_slot_id; |
274 | 0 | if (state->get_query_ctx() != nullptr && |
275 | 0 | state->get_query_ctx()->file_scan_range_params_map.count(local_state->parent_id()) > 0) { |
276 | 0 | _params = &(state->get_query_ctx()->file_scan_range_params_map[local_state->parent_id()]); |
277 | 0 | } else { |
278 | 0 | _params = _split_source->get_params(); |
279 | 0 | } |
280 | 0 | } |
281 | | |
282 | 0 | Status FileScannerV2::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) { |
283 | 0 | RETURN_IF_ERROR(Scanner::init(state, conjuncts)); |
284 | 0 | _get_block_timer = |
285 | 0 | ADD_TIMER_WITH_LEVEL(_local_state->scanner_profile(), "FileScannerV2GetBlockTime", 1); |
286 | 0 | _file_counter = |
287 | 0 | ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), "FileNumber", TUnit::UNIT, 1); |
288 | 0 | _file_read_bytes_counter = ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), |
289 | 0 | "FileReadBytes", TUnit::BYTES, 1); |
290 | 0 | _file_read_calls_counter = ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), |
291 | 0 | "FileReadCalls", TUnit::UNIT, 1); |
292 | 0 | _file_read_time_counter = |
293 | 0 | ADD_TIMER_WITH_LEVEL(_local_state->scanner_profile(), "FileReadTime", 1); |
294 | 0 | _adaptive_batch_predicted_rows_counter = ADD_COUNTER_WITH_LEVEL( |
295 | 0 | _local_state->scanner_profile(), "AdaptiveBatchPredictedRows", TUnit::UNIT, 1); |
296 | 0 | _adaptive_batch_actual_bytes_counter = ADD_COUNTER_WITH_LEVEL( |
297 | 0 | _local_state->scanner_profile(), "AdaptiveBatchActualBytes", TUnit::BYTES, 1); |
298 | 0 | _adaptive_batch_probe_count_counter = ADD_COUNTER_WITH_LEVEL( |
299 | 0 | _local_state->scanner_profile(), "AdaptiveBatchProbeCount", TUnit::UNIT, 1); |
300 | 0 | _file_cache_statistics = std::make_unique<io::FileCacheStatistics>(); |
301 | 0 | _file_reader_stats = std::make_unique<io::FileReaderStats>(); |
302 | 0 | RETURN_IF_ERROR(_init_io_ctx()); |
303 | 0 | _io_ctx->file_cache_stats = _file_cache_statistics.get(); |
304 | 0 | _io_ctx->file_reader_stats = _file_reader_stats.get(); |
305 | 0 | _io_ctx->is_disposable = _state->query_options().disable_file_cache; |
306 | 0 | return Status::OK(); |
307 | 0 | } |
308 | | |
309 | 0 | Status FileScannerV2::_open_impl(RuntimeState* state) { |
310 | 0 | RETURN_IF_CANCELLED(state); |
311 | 0 | RETURN_IF_ERROR(Scanner::_open_impl(state)); |
312 | 0 | RETURN_IF_ERROR(_split_source->get_next(&_first_scan_range, &_current_range)); |
313 | 0 | if (_first_scan_range) { |
314 | 0 | RETURN_IF_ERROR(_create_table_reader_for_format(_current_range, &_table_reader)); |
315 | 0 | DORIS_CHECK(_table_reader != nullptr); |
316 | 0 | RETURN_IF_ERROR(_init_expr_ctxes()); |
317 | 0 | RETURN_IF_ERROR(_init_table_reader(_current_range)); |
318 | 0 | } |
319 | 0 | return Status::OK(); |
320 | 0 | } |
321 | | |
322 | 0 | Status FileScannerV2::_get_block_impl(RuntimeState* state, Block* block, bool* eof) { |
323 | 0 | while (true) { |
324 | 0 | RETURN_IF_CANCELLED(state); |
325 | 0 | if (!_has_prepared_split) { |
326 | 0 | RETURN_IF_ERROR(_prepare_next_split(eof)); |
327 | 0 | if (*eof) { |
328 | 0 | return Status::OK(); |
329 | 0 | } |
330 | 0 | } |
331 | | |
332 | 0 | { |
333 | 0 | SCOPED_TIMER(_get_block_timer); |
334 | 0 | if (_should_run_adaptive_batch_size()) { |
335 | 0 | _table_reader->set_batch_size(_predict_reader_batch_rows()); |
336 | 0 | } |
337 | 0 | RETURN_IF_ERROR(_table_reader->get_block(block, eof)); |
338 | 0 | } |
339 | 0 | if (*eof) { |
340 | 0 | _state->update_num_finished_scan_range(1); |
341 | 0 | _has_prepared_split = false; |
342 | 0 | *eof = false; |
343 | 0 | continue; |
344 | 0 | } |
345 | 0 | _update_adaptive_batch_size(*block); |
346 | 0 | return Status::OK(); |
347 | 0 | } |
348 | 0 | } |
349 | | |
350 | 0 | Status FileScannerV2::_prepare_next_split(bool* eos) { |
351 | 0 | bool has_next = _first_scan_range; |
352 | 0 | if (!_first_scan_range) { |
353 | 0 | RETURN_IF_ERROR(_split_source->get_next(&has_next, &_current_range)); |
354 | 0 | } |
355 | 0 | _first_scan_range = false; |
356 | 0 | if (!has_next || _should_stop) { |
357 | 0 | *eos = true; |
358 | 0 | return Status::OK(); |
359 | 0 | } |
360 | 0 | DORIS_CHECK(_table_reader != nullptr); |
361 | 0 | _current_range_path = _current_range.path; |
362 | 0 | _init_adaptive_batch_size_state(get_range_format_type(*_params, _current_range)); |
363 | 0 | RETURN_IF_ERROR(_prepare_table_reader_split(_current_range)); |
364 | 0 | COUNTER_UPDATE(_file_counter, 1); |
365 | 0 | _has_prepared_split = true; |
366 | 0 | *eos = false; |
367 | 0 | return Status::OK(); |
368 | 0 | } |
369 | | |
370 | 0 | Status FileScannerV2::_init_table_reader(const TFileRangeDesc& range) { |
371 | 0 | const auto format_type = get_range_format_type(*_params, range); |
372 | 0 | format::FileFormat file_format; |
373 | 0 | RETURN_IF_ERROR(_to_file_format(format_type, &file_format)); |
374 | 0 | DORIS_CHECK(_table_reader != nullptr); |
375 | |
|
376 | 0 | VExprContextSPtrs table_conjuncts; |
377 | 0 | RETURN_IF_ERROR(_build_table_conjuncts(&table_conjuncts)); |
378 | 0 | RETURN_IF_ERROR(_table_reader->init({ |
379 | 0 | .projected_columns = _projected_columns, |
380 | 0 | .conjuncts = std::move(table_conjuncts), |
381 | 0 | .format = file_format, |
382 | 0 | .scan_params = const_cast<TFileScanRangeParams*>(_params), |
383 | 0 | .io_ctx = _io_ctx, |
384 | 0 | .runtime_state = _state, |
385 | 0 | .scanner_profile = _local_state->scanner_profile(), |
386 | 0 | .file_slot_descs = &_file_slot_descs, |
387 | 0 | .push_down_agg_type = _local_state->get_push_down_agg_type(), |
388 | 0 | .condition_cache_digest = _local_state->get_condition_cache_digest(), |
389 | 0 | })); |
390 | 0 | return Status::OK(); |
391 | 0 | } |
392 | | |
393 | | Status FileScannerV2::_create_table_reader_for_format( |
394 | 0 | const TFileRangeDesc& range, std::unique_ptr<format::TableReader>* reader) const { |
395 | 0 | DORIS_CHECK(reader != nullptr); |
396 | 0 | const auto table_format = table_format_name(range); |
397 | 0 | if (table_format == "NotSet" || table_format == "tvf") { |
398 | 0 | *reader = std::make_unique<format::TableReader>(); |
399 | 0 | } else if (table_format == "hive") { |
400 | 0 | *reader = format::hive::HiveReader::create_unique(); |
401 | 0 | } else if (table_format == "iceberg") { |
402 | 0 | if (get_range_format_type(*_params, range) == TFileFormatType::FORMAT_JNI) { |
403 | 0 | *reader = std::make_unique<format::iceberg::IcebergSysTableJniReader>(); |
404 | 0 | } else { |
405 | 0 | *reader = std::make_unique<format::iceberg::IcebergTableReader>(); |
406 | 0 | } |
407 | 0 | } else if (table_format == "paimon") { |
408 | 0 | *reader = std::make_unique<format::paimon::PaimonHybridReader>(); |
409 | 0 | } else if (table_format == "hudi") { |
410 | 0 | *reader = std::make_unique<format::hudi::HudiHybridReader>(); |
411 | 0 | } else if (table_format == "jdbc") { |
412 | 0 | *reader = std::make_unique<format::jdbc::JdbcJniReader>(); |
413 | 0 | } else if (table_format == "max_compute") { |
414 | 0 | const auto* mc_desc = |
415 | 0 | static_cast<const MaxComputeTableDescriptor*>(_output_tuple_desc->table_desc()); |
416 | 0 | RETURN_IF_ERROR(mc_desc->init_status()); |
417 | 0 | *reader = std::make_unique<format::max_compute::MaxComputeJniReader>(mc_desc); |
418 | 0 | } else if (table_format == "trino_connector") { |
419 | 0 | *reader = std::make_unique<format::trino_connector::TrinoConnectorJniReader>(); |
420 | 0 | } else if (table_format == "remote_doris") { |
421 | 0 | *reader = std::make_unique<format::remote_doris::RemoteDorisReader>(); |
422 | 0 | } else { |
423 | 0 | return Status::NotSupported("FileScannerV2 does not support table format {}", table_format); |
424 | 0 | } |
425 | 0 | return Status::OK(); |
426 | 0 | } |
427 | | |
428 | 0 | Status FileScannerV2::_prepare_table_reader_split(const TFileRangeDesc& range) { |
429 | 0 | std::map<std::string, Field> partition_values; |
430 | 0 | RETURN_IF_ERROR(_generate_partition_values(range, &partition_values)); |
431 | 0 | format::FileFormat current_split_format; |
432 | 0 | RETURN_IF_ERROR(_to_file_format(get_range_format_type(*_params, range), ¤t_split_format)); |
433 | 0 | RETURN_IF_ERROR(_table_reader->prepare_split({ |
434 | 0 | .partition_values = std::move(partition_values), |
435 | 0 | .cache = _kv_cache, |
436 | 0 | .current_range = range, |
437 | 0 | .current_split_format = current_split_format, |
438 | 0 | .global_rowid_context = _create_global_rowid_context(range), |
439 | 0 | })); |
440 | 0 | return Status::OK(); |
441 | 0 | } |
442 | | |
443 | 0 | bool FileScannerV2::_should_enable_file_meta_cache() const { |
444 | 0 | return ExecEnv::GetInstance()->file_meta_cache()->enabled() && |
445 | 0 | _split_source->num_scan_ranges() < config::max_external_file_meta_cache_num / 3; |
446 | 0 | } |
447 | | |
448 | | std::optional<format::GlobalRowIdContext> FileScannerV2::_create_global_rowid_context( |
449 | 0 | const TFileRangeDesc& range) const { |
450 | 0 | if (!_need_global_rowid_column) { |
451 | 0 | return std::nullopt; |
452 | 0 | } |
453 | 0 | auto& id_file_map = _state->get_id_file_map(); |
454 | 0 | DORIS_CHECK(id_file_map != nullptr); |
455 | 0 | const auto file_id = id_file_map->get_file_mapping_id( |
456 | 0 | std::make_shared<FileMapping>(_local_state->cast<FileScanLocalState>().parent_id(), |
457 | 0 | range, _should_enable_file_meta_cache())); |
458 | 0 | return format::GlobalRowIdContext { |
459 | 0 | .version = IdManager::ID_VERSION, |
460 | 0 | .backend_id = BackendOptions::get_backend_id(), |
461 | 0 | .file_id = file_id, |
462 | 0 | }; |
463 | 0 | } |
464 | | |
465 | | Status FileScannerV2::_generate_partition_values( |
466 | 0 | const TFileRangeDesc& range, std::map<std::string, Field>* partition_values) const { |
467 | 0 | DORIS_CHECK(partition_values != nullptr); |
468 | 0 | partition_values->clear(); |
469 | 0 | if (!range.__isset.columns_from_path_keys || !range.__isset.columns_from_path) { |
470 | 0 | return Status::OK(); |
471 | 0 | } |
472 | 0 | DORIS_CHECK(range.columns_from_path_keys.size() == range.columns_from_path.size()); |
473 | 0 | for (size_t idx = 0; idx < range.columns_from_path_keys.size(); ++idx) { |
474 | 0 | const auto& key = range.columns_from_path_keys[idx]; |
475 | 0 | const auto it = _partition_slot_descs.find(key); |
476 | 0 | if (it == _partition_slot_descs.end()) { |
477 | 0 | continue; |
478 | 0 | } |
479 | 0 | const auto& value = range.columns_from_path[idx]; |
480 | 0 | const bool is_null = range.__isset.columns_from_path_is_null && |
481 | 0 | idx < range.columns_from_path_is_null.size() && |
482 | 0 | range.columns_from_path_is_null[idx]; |
483 | 0 | Field field; |
484 | 0 | DORIS_CHECK(it->second.slot_desc != nullptr); |
485 | 0 | RETURN_IF_ERROR(_parse_partition_value(it->second.slot_desc, value, is_null, &field)); |
486 | 0 | partition_values->emplace(it->second.canonical_name, std::move(field)); |
487 | 0 | } |
488 | 0 | return Status::OK(); |
489 | 0 | } |
490 | | |
491 | | Status FileScannerV2::_parse_partition_value(const SlotDescriptor* slot_desc, |
492 | | const std::string& value, bool is_null, |
493 | 0 | Field* field) const { |
494 | 0 | DORIS_CHECK(slot_desc != nullptr); |
495 | 0 | DORIS_CHECK(field != nullptr); |
496 | 0 | if (is_null) { |
497 | 0 | *field = Field::create_field<TYPE_NULL>(Null()); |
498 | 0 | return Status::OK(); |
499 | 0 | } |
500 | 0 | const auto data_type = remove_nullable(slot_desc->get_data_type_ptr()); |
501 | 0 | auto column = data_type->create_column(); |
502 | 0 | auto serde = data_type->get_serde(); |
503 | 0 | DataTypeSerDe::FormatOptions options; |
504 | 0 | options.converted_from_string = true; |
505 | 0 | StringRef ref(value.data(), value.size()); |
506 | 0 | RETURN_IF_ERROR(serde->from_string(ref, *column, options)); |
507 | 0 | DORIS_CHECK(column->size() == 1); |
508 | 0 | *field = (*column)[0]; |
509 | 0 | return Status::OK(); |
510 | 0 | } |
511 | | |
512 | 0 | Status FileScannerV2::_init_expr_ctxes() { |
513 | 0 | _slot_id_to_desc.clear(); |
514 | 0 | _slot_id_to_global_index.clear(); |
515 | 0 | _partition_slot_descs.clear(); |
516 | 0 | _file_slot_descs.clear(); |
517 | 0 | for (const auto* slot_desc : _output_tuple_desc->slots()) { |
518 | 0 | _slot_id_to_desc.emplace(slot_desc->id(), slot_desc); |
519 | 0 | } |
520 | 0 | DORIS_CHECK(_table_reader != nullptr); |
521 | 0 | RETURN_IF_ERROR(_build_projected_columns(*_table_reader)); |
522 | 0 | return Status::OK(); |
523 | 0 | } |
524 | | |
525 | 0 | Status FileScannerV2::_build_projected_columns(const format::TableReader& table_reader) { |
526 | 0 | _projected_columns.clear(); |
527 | 0 | _projected_columns.reserve(_params->required_slots.size()); |
528 | 0 | _need_global_rowid_column = false; |
529 | 0 | format::ProjectedColumnBuildContext build_context { |
530 | 0 | .scan_params = _params, |
531 | 0 | .range = &_current_range, |
532 | 0 | .runtime_state = _state, |
533 | 0 | }; |
534 | |
|
535 | 0 | for (size_t slot_idx = 0; slot_idx < _params->required_slots.size(); ++slot_idx) { |
536 | 0 | const auto& slot_info = _params->required_slots[slot_idx]; |
537 | 0 | const auto it = _slot_id_to_desc.find(slot_info.slot_id); |
538 | 0 | if (it == _slot_id_to_desc.end()) { |
539 | 0 | return Status::InternalError("Unknown source slot descriptor, slot_id={}", |
540 | 0 | slot_info.slot_id); |
541 | 0 | } |
542 | 0 | auto column = _build_table_column(it->second); |
543 | 0 | if (column.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) { |
544 | 0 | _need_global_rowid_column = true; |
545 | 0 | } |
546 | 0 | RETURN_IF_ERROR(_build_default_expr(slot_info, &column.default_expr)); |
547 | 0 | build_context.schema_column.reset(); |
548 | 0 | RETURN_IF_ERROR(table_reader.annotate_projected_column(slot_info, &build_context, &column)); |
549 | | // Build nested children from access paths generated by the slot's access-path |
550 | | // expressions. A projected column can therefore contain only a subset of the schema |
551 | | // column's nested children. |
552 | 0 | RETURN_IF_ERROR(AccessPathParser::build_nested_children( |
553 | 0 | &column, it->second, |
554 | 0 | build_context.schema_column.has_value() ? &*build_context.schema_column : nullptr)); |
555 | 0 | if (is_partition_slot(slot_info, column.name)) { |
556 | 0 | column.is_partition_key = true; |
557 | 0 | _partition_slot_descs.emplace( |
558 | 0 | column.name, |
559 | 0 | PartitionSlotInfo {.slot_desc = it->second, .canonical_name = column.name}); |
560 | 0 | for (const auto& alias : column.name_mapping) { |
561 | 0 | _partition_slot_descs.emplace( |
562 | 0 | alias, |
563 | 0 | PartitionSlotInfo {.slot_desc = it->second, .canonical_name = column.name}); |
564 | 0 | } |
565 | 0 | } else if (is_data_file_slot(slot_info, column.name)) { |
566 | 0 | _file_slot_descs.push_back(const_cast<SlotDescriptor*>(it->second)); |
567 | 0 | } |
568 | 0 | const auto global_index = format::GlobalIndex(slot_idx); |
569 | 0 | _slot_id_to_global_index.emplace(slot_info.slot_id, global_index); |
570 | 0 | _projected_columns.push_back(std::move(column)); |
571 | 0 | } |
572 | 0 | RETURN_IF_ERROR(table_reader.validate_projected_columns(build_context)); |
573 | 0 | return Status::OK(); |
574 | 0 | } |
575 | | |
576 | | Status FileScannerV2::_build_default_expr(const TFileScanSlotInfo& slot_info, |
577 | 0 | VExprContextSPtr* ctx) const { |
578 | 0 | DORIS_CHECK(ctx != nullptr); |
579 | 0 | if (slot_info.__isset.default_value_expr && !slot_info.default_value_expr.nodes.empty()) { |
580 | 0 | return VExpr::create_expr_tree(slot_info.default_value_expr, *ctx); |
581 | 0 | } |
582 | | |
583 | 0 | if (_params->__isset.default_value_of_src_slot) { |
584 | 0 | const auto it = _params->default_value_of_src_slot.find(slot_info.slot_id); |
585 | 0 | if (it != _params->default_value_of_src_slot.end() && !it->second.nodes.empty()) { |
586 | 0 | return VExpr::create_expr_tree(it->second, *ctx); |
587 | 0 | } |
588 | 0 | } |
589 | 0 | return Status::OK(); |
590 | 0 | } |
591 | | |
592 | 0 | format::ColumnDefinition FileScannerV2::_build_table_column(const SlotDescriptor* slot_desc) { |
593 | 0 | DORIS_CHECK(slot_desc != nullptr); |
594 | 0 | format::ColumnDefinition column; |
595 | | // TODO(gabriel): why always BY_NAME here? |
596 | 0 | column.identifier = Field::create_field<TYPE_STRING>(slot_desc->col_name()); |
597 | 0 | column.name = slot_desc->col_name(); |
598 | 0 | column.type = slot_desc->get_data_type_ptr(); |
599 | 0 | return column; |
600 | 0 | } |
601 | | |
602 | 0 | Status FileScannerV2::_build_table_conjuncts(VExprContextSPtrs* conjuncts) const { |
603 | 0 | DORIS_CHECK(conjuncts != nullptr); |
604 | 0 | conjuncts->clear(); |
605 | 0 | conjuncts->reserve(_conjuncts.size()); |
606 | 0 | for (const auto& conjunct : _conjuncts) { |
607 | 0 | VExprSPtr root; |
608 | 0 | RETURN_IF_ERROR(format::clone_table_expr_tree(conjunct->root(), &root)); |
609 | 0 | RETURN_IF_ERROR(rewrite_slot_refs_to_global_index(&root, _slot_id_to_global_index)); |
610 | 0 | conjuncts->push_back(VExprContext::create_shared(std::move(root))); |
611 | 0 | } |
612 | 0 | return Status::OK(); |
613 | 0 | } |
614 | | |
615 | 0 | TFileFormatType::type FileScannerV2::_get_current_format_type() const { |
616 | 0 | return get_range_format_type(*_params, _current_range); |
617 | 0 | } |
618 | | |
619 | | Status FileScannerV2::_to_file_format(TFileFormatType::type format_type, |
620 | 16 | format::FileFormat* file_format) { |
621 | 16 | DORIS_CHECK(file_format != nullptr); |
622 | 16 | switch (format_type) { |
623 | 1 | case TFileFormatType::FORMAT_PARQUET: |
624 | 1 | *file_format = format::FileFormat::PARQUET; |
625 | 1 | return Status::OK(); |
626 | 1 | case TFileFormatType::FORMAT_JNI: |
627 | 1 | *file_format = format::FileFormat::JNI; |
628 | 1 | return Status::OK(); |
629 | 1 | case TFileFormatType::FORMAT_CSV_PLAIN: |
630 | 2 | case TFileFormatType::FORMAT_CSV_GZ: |
631 | 3 | case TFileFormatType::FORMAT_CSV_BZ2: |
632 | 4 | case TFileFormatType::FORMAT_CSV_LZ4FRAME: |
633 | 5 | case TFileFormatType::FORMAT_CSV_LZ4BLOCK: |
634 | 6 | case TFileFormatType::FORMAT_CSV_LZOP: |
635 | 7 | case TFileFormatType::FORMAT_CSV_DEFLATE: |
636 | 8 | case TFileFormatType::FORMAT_CSV_SNAPPYBLOCK: |
637 | 9 | case TFileFormatType::FORMAT_PROTO: |
638 | 9 | *file_format = format::FileFormat::CSV; |
639 | 9 | return Status::OK(); |
640 | 1 | case TFileFormatType::FORMAT_TEXT: |
641 | 1 | *file_format = format::FileFormat::TEXT; |
642 | 1 | return Status::OK(); |
643 | 1 | case TFileFormatType::FORMAT_JSON: |
644 | 1 | *file_format = format::FileFormat::JSON; |
645 | 1 | return Status::OK(); |
646 | 1 | case TFileFormatType::FORMAT_NATIVE: |
647 | 1 | *file_format = format::FileFormat::NATIVE; |
648 | 1 | return Status::OK(); |
649 | 1 | case TFileFormatType::FORMAT_ARROW: |
650 | 1 | *file_format = format::FileFormat::ARROW; |
651 | 1 | return Status::OK(); |
652 | 1 | default: |
653 | 1 | return Status::NotSupported("FileScannerV2 does not support file format {}", |
654 | 1 | to_string(format_type)); |
655 | 16 | } |
656 | 16 | } |
657 | | |
658 | 0 | Status FileScannerV2::_init_io_ctx() { |
659 | 0 | _io_ctx = create_file_scan_io_context(_state); |
660 | 0 | return Status::OK(); |
661 | 0 | } |
662 | | |
663 | 0 | void FileScannerV2::_reset_adaptive_batch_size_state() { |
664 | 0 | _block_size_predictor.reset(); |
665 | 0 | COUNTER_SET(_adaptive_batch_predicted_rows_counter, int64_t(0)); |
666 | 0 | COUNTER_SET(_adaptive_batch_actual_bytes_counter, int64_t(0)); |
667 | 0 | } |
668 | | |
669 | 0 | void FileScannerV2::_init_adaptive_batch_size_state(TFileFormatType::type format_type) { |
670 | 0 | _reset_adaptive_batch_size_state(); |
671 | 0 | if (!_should_enable_adaptive_batch_size(format_type)) { |
672 | 0 | return; |
673 | 0 | } |
674 | | |
675 | | // V2 native file readers do not have reliable row-width hints before the first batch. Start |
676 | | // every split with a small probe, then learn bytes-per-row from the materialized table block |
677 | | // and keep later batches close to RuntimeState::preferred_block_size_bytes(). |
678 | 0 | _block_size_predictor = std::make_unique<AdaptiveBlockSizePredictor>( |
679 | 0 | _state->preferred_block_size_bytes(), 0.0, ADAPTIVE_BATCH_INITIAL_PROBE_ROWS, |
680 | 0 | _state->batch_size()); |
681 | 0 | } |
682 | | |
683 | 0 | bool FileScannerV2::_should_enable_adaptive_batch_size(TFileFormatType::type format_type) const { |
684 | 0 | if (!config::enable_adaptive_batch_size) { |
685 | 0 | return false; |
686 | 0 | } |
687 | 0 | switch (format_type) { |
688 | 0 | case TFileFormatType::FORMAT_PARQUET: |
689 | 0 | case TFileFormatType::FORMAT_ORC: |
690 | 0 | case TFileFormatType::FORMAT_CSV_PLAIN: |
691 | 0 | case TFileFormatType::FORMAT_CSV_GZ: |
692 | 0 | case TFileFormatType::FORMAT_CSV_BZ2: |
693 | 0 | case TFileFormatType::FORMAT_CSV_LZ4FRAME: |
694 | 0 | case TFileFormatType::FORMAT_CSV_LZ4BLOCK: |
695 | 0 | case TFileFormatType::FORMAT_CSV_LZOP: |
696 | 0 | case TFileFormatType::FORMAT_CSV_DEFLATE: |
697 | 0 | case TFileFormatType::FORMAT_CSV_SNAPPYBLOCK: |
698 | 0 | case TFileFormatType::FORMAT_PROTO: |
699 | 0 | case TFileFormatType::FORMAT_TEXT: |
700 | 0 | case TFileFormatType::FORMAT_JSON: |
701 | 0 | case TFileFormatType::FORMAT_JNI: |
702 | 0 | return true; |
703 | 0 | default: |
704 | 0 | return false; |
705 | 0 | } |
706 | 0 | } |
707 | | |
708 | 0 | bool FileScannerV2::_should_run_adaptive_batch_size() const { |
709 | | // COUNT pushdown emits synthetic rows from file metadata and does not materialize file columns, |
710 | | // so there is no useful row-width sample to learn from. |
711 | 0 | return _block_size_predictor != nullptr && |
712 | 0 | _local_state->get_push_down_agg_type() != TPushAggOp::type::COUNT; |
713 | 0 | } |
714 | | |
715 | 0 | size_t FileScannerV2::_predict_reader_batch_rows() { |
716 | 0 | DORIS_CHECK(_block_size_predictor != nullptr); |
717 | | // Before history exists this returns the probe row count; after update(), it returns roughly |
718 | | // preferred_block_size_bytes / EWMA(bytes_per_row), capped by RuntimeState::batch_size(). |
719 | 0 | const size_t predicted_rows = _block_size_predictor->predict_next_rows(); |
720 | 0 | COUNTER_SET(_adaptive_batch_predicted_rows_counter, static_cast<int64_t>(predicted_rows)); |
721 | 0 | return predicted_rows; |
722 | 0 | } |
723 | | |
724 | 0 | void FileScannerV2::_update_adaptive_batch_size(const Block& block) { |
725 | 0 | if (!_should_run_adaptive_batch_size()) { |
726 | 0 | return; |
727 | 0 | } |
728 | 0 | COUNTER_SET(_adaptive_batch_actual_bytes_counter, static_cast<int64_t>(block.bytes())); |
729 | 0 | if (block.rows() == 0) { |
730 | 0 | return; |
731 | 0 | } |
732 | | // The sample is taken after TableReader has finalized file-local columns to table columns. |
733 | | // This matches the memory shape seen by upstream operators and catches very wide nested |
734 | | // columns, such as map/string payloads, after the first probe batch. |
735 | 0 | if (!_block_size_predictor->has_history()) { |
736 | 0 | COUNTER_UPDATE(_adaptive_batch_probe_count_counter, 1); |
737 | 0 | } |
738 | 0 | _block_size_predictor->update(block); |
739 | 0 | } |
740 | | |
741 | 0 | Status FileScannerV2::close(RuntimeState* state) { |
742 | 0 | if (!_try_close()) { |
743 | 0 | return Status::OK(); |
744 | 0 | } |
745 | 0 | if (_table_reader != nullptr) { |
746 | 0 | RETURN_IF_ERROR(_table_reader->close()); |
747 | 0 | _report_condition_cache_profile(); |
748 | 0 | _table_reader.reset(); |
749 | 0 | } |
750 | 0 | return Scanner::close(state); |
751 | 0 | } |
752 | | |
753 | 0 | void FileScannerV2::try_stop() { |
754 | 0 | Scanner::try_stop(); |
755 | 0 | if (_io_ctx) { |
756 | 0 | _io_ctx->should_stop = true; |
757 | 0 | } |
758 | 0 | } |
759 | | |
760 | 0 | void FileScannerV2::update_realtime_counters() { |
761 | 0 | if (_file_reader_stats == nullptr) { |
762 | 0 | return; |
763 | 0 | } |
764 | 0 | DORIS_CHECK(_file_cache_statistics != nullptr); |
765 | 0 | const int64_t bytes_read = cast_set<int64_t>(_file_reader_stats->read_bytes); |
766 | 0 | auto* local_state = static_cast<FileScanLocalState*>(_local_state); |
767 | 0 | const auto file_type = |
768 | 0 | _current_range.__isset.file_type |
769 | 0 | ? _current_range.file_type |
770 | 0 | : (_params != nullptr && _params->__isset.file_type ? _params->file_type |
771 | 0 | : TFileType::FILE_LOCAL); |
772 | 0 | const auto deltas = _collect_realtime_counter_deltas( |
773 | 0 | *_file_reader_stats, *_file_cache_statistics, _uncached_reader_bytes_storage(file_type), |
774 | 0 | &_last_read_bytes, &_last_read_rows, &_last_bytes_read_from_local, |
775 | 0 | &_last_bytes_read_from_remote); |
776 | |
|
777 | 0 | COUNTER_UPDATE(local_state->_scan_bytes, deltas.scan_bytes); |
778 | 0 | COUNTER_UPDATE(local_state->_scan_rows, deltas.scan_rows); |
779 | |
|
780 | 0 | _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_rows(deltas.scan_rows); |
781 | 0 | _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes(deltas.scan_bytes); |
782 | 0 | _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes_from_local_storage( |
783 | 0 | deltas.scan_bytes_from_local_storage); |
784 | 0 | _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes_from_remote_storage( |
785 | 0 | deltas.scan_bytes_from_remote_storage); |
786 | |
|
787 | 0 | COUNTER_SET(_file_read_bytes_counter, bytes_read); |
788 | 0 | COUNTER_SET(_file_read_calls_counter, cast_set<int64_t>(_file_reader_stats->read_calls)); |
789 | 0 | COUNTER_SET(_file_read_time_counter, cast_set<int64_t>(_file_reader_stats->read_time_ns)); |
790 | |
|
791 | 0 | DorisMetrics::instance()->query_scan_bytes->increment(deltas.scan_bytes); |
792 | 0 | DorisMetrics::instance()->query_scan_rows->increment(deltas.scan_rows); |
793 | 0 | DorisMetrics::instance()->query_scan_bytes_from_local->increment( |
794 | 0 | deltas.scan_bytes_from_local_storage); |
795 | 0 | DorisMetrics::instance()->query_scan_bytes_from_remote->increment( |
796 | 0 | deltas.scan_bytes_from_remote_storage); |
797 | 0 | } |
798 | | |
799 | | FileScannerV2::RealtimeCounterDeltas FileScannerV2::_collect_realtime_counter_deltas( |
800 | | const io::FileReaderStats& file_reader_stats, |
801 | | const io::FileCacheStatistics& file_cache_statistics, |
802 | | UncachedReaderBytesStorage uncached_reader_bytes_storage, int64_t* last_read_bytes, |
803 | | int64_t* last_read_rows, int64_t* last_bytes_read_from_local, |
804 | 7 | int64_t* last_bytes_read_from_remote) { |
805 | 7 | DORIS_CHECK(last_read_bytes != nullptr); |
806 | 7 | DORIS_CHECK(last_read_rows != nullptr); |
807 | 7 | DORIS_CHECK(last_bytes_read_from_local != nullptr); |
808 | 7 | DORIS_CHECK(last_bytes_read_from_remote != nullptr); |
809 | | |
810 | 7 | const int64_t read_bytes = cast_set<int64_t>(file_reader_stats.read_bytes); |
811 | 7 | const int64_t read_rows = cast_set<int64_t>(file_reader_stats.read_rows); |
812 | 7 | const int64_t bytes_read_from_local = file_cache_statistics.bytes_read_from_local; |
813 | 7 | const int64_t bytes_read_from_remote = file_cache_statistics.bytes_read_from_remote; |
814 | 7 | DORIS_CHECK(read_bytes >= *last_read_bytes); |
815 | 7 | DORIS_CHECK(read_rows >= *last_read_rows); |
816 | 7 | DORIS_CHECK(bytes_read_from_local >= *last_bytes_read_from_local); |
817 | 7 | DORIS_CHECK(bytes_read_from_remote >= *last_bytes_read_from_remote); |
818 | | |
819 | 7 | RealtimeCounterDeltas deltas; |
820 | 7 | deltas.scan_rows = read_rows - *last_read_rows; |
821 | 7 | deltas.scan_bytes = read_bytes - *last_read_bytes; |
822 | | // Peer cache is a known cache source, but it is not remote object storage. |
823 | 7 | const bool has_cache_source_stats = file_cache_statistics.num_local_io_total != 0 || |
824 | 7 | file_cache_statistics.num_remote_io_total != 0 || |
825 | 7 | file_cache_statistics.num_peer_io_total != 0 || |
826 | 7 | bytes_read_from_local != 0 || bytes_read_from_remote != 0 || |
827 | 7 | file_cache_statistics.bytes_read_from_peer != 0; |
828 | 7 | if (!has_cache_source_stats) { |
829 | 4 | switch (uncached_reader_bytes_storage) { |
830 | 1 | case UncachedReaderBytesStorage::LOCAL: |
831 | 1 | deltas.scan_bytes_from_local_storage = deltas.scan_bytes; |
832 | 1 | break; |
833 | 3 | case UncachedReaderBytesStorage::REMOTE: |
834 | 3 | deltas.scan_bytes_from_remote_storage = deltas.scan_bytes; |
835 | 3 | break; |
836 | 0 | case UncachedReaderBytesStorage::NONE: |
837 | 0 | break; |
838 | 4 | } |
839 | 4 | } else { |
840 | 3 | deltas.scan_bytes_from_local_storage = bytes_read_from_local - *last_bytes_read_from_local; |
841 | 3 | deltas.scan_bytes_from_remote_storage = |
842 | 3 | bytes_read_from_remote - *last_bytes_read_from_remote; |
843 | 3 | } |
844 | | |
845 | 7 | *last_read_bytes = read_bytes; |
846 | 7 | *last_read_rows = read_rows; |
847 | 7 | *last_bytes_read_from_local = bytes_read_from_local; |
848 | 7 | *last_bytes_read_from_remote = bytes_read_from_remote; |
849 | 7 | return deltas; |
850 | 7 | } |
851 | | |
852 | | FileScannerV2::UncachedReaderBytesStorage FileScannerV2::_uncached_reader_bytes_storage( |
853 | 0 | TFileType::type file_type) { |
854 | 0 | switch (file_type) { |
855 | 0 | case TFileType::FILE_LOCAL: |
856 | 0 | return UncachedReaderBytesStorage::LOCAL; |
857 | 0 | case TFileType::FILE_STREAM: |
858 | 0 | return UncachedReaderBytesStorage::NONE; |
859 | 0 | case TFileType::FILE_BROKER: |
860 | 0 | case TFileType::FILE_S3: |
861 | 0 | case TFileType::FILE_HDFS: |
862 | 0 | case TFileType::FILE_NET: |
863 | 0 | case TFileType::FILE_HTTP: |
864 | 0 | return UncachedReaderBytesStorage::REMOTE; |
865 | 0 | } |
866 | 0 | DORIS_CHECK(false) << "unknown file type: " << file_type; |
867 | 0 | return UncachedReaderBytesStorage::NONE; |
868 | 0 | } |
869 | | |
870 | 0 | void FileScannerV2::_collect_profile_before_close() { |
871 | 0 | _report_file_reader_predicate_filtered_rows(); |
872 | 0 | Scanner::_collect_profile_before_close(); |
873 | 0 | if (_file_reader_stats != nullptr) { |
874 | 0 | COUNTER_SET(_file_read_bytes_counter, cast_set<int64_t>(_file_reader_stats->read_bytes)); |
875 | 0 | COUNTER_SET(_file_read_calls_counter, cast_set<int64_t>(_file_reader_stats->read_calls)); |
876 | 0 | COUNTER_SET(_file_read_time_counter, cast_set<int64_t>(_file_reader_stats->read_time_ns)); |
877 | 0 | } |
878 | | // Query profiles can be collected before Scanner::close() runs. Publish condition-cache |
879 | | // counters here as well, using deltas so this method and close() cannot double count. |
880 | 0 | _report_condition_cache_profile(); |
881 | 0 | } |
882 | | |
883 | 0 | bool FileScannerV2::_should_update_load_counters() const { |
884 | 0 | if (_is_load) { |
885 | 0 | return true; |
886 | 0 | } |
887 | | // TVF based loads (e.g. http_stream, group commit relay) plan the load source as a |
888 | | // tvf query scan without src tuple desc, so _is_load is false. But rows filtered by |
889 | | // the load's WHERE clause still need to be reported as unselected rows. FILE_STREAM |
890 | | // is only reachable from such load entries, never from normal queries, so use it to |
891 | | // identify these scanners. |
892 | 0 | return (_params != nullptr && _params->__isset.file_type && |
893 | 0 | _params->file_type == TFileType::FILE_STREAM) || |
894 | 0 | (_current_range.__isset.file_type && _current_range.file_type == TFileType::FILE_STREAM); |
895 | 0 | } |
896 | | |
897 | 0 | void FileScannerV2::_report_file_reader_predicate_filtered_rows() { |
898 | 0 | const int64_t filtered_rows = _io_ctx != nullptr ? _io_ctx->predicate_filtered_rows : 0; |
899 | 0 | const int64_t filtered_delta = filtered_rows - _reported_predicate_filtered_rows; |
900 | 0 | if (filtered_delta > 0) { |
901 | | // File readers can evaluate localized conjuncts before a block reaches Scanner. Count |
902 | | // those rows as scanner-level unselected rows so load statistics stay identical no matter |
903 | | // whether a predicate is pushed down or evaluated by Scanner::_filter_output_block(). |
904 | 0 | _counter.num_rows_unselected += filtered_delta; |
905 | 0 | _reported_predicate_filtered_rows = filtered_rows; |
906 | 0 | } |
907 | 0 | } |
908 | | |
909 | 0 | void FileScannerV2::_report_condition_cache_profile() { |
910 | 0 | auto* local_state = static_cast<FileScanLocalState*>(_local_state); |
911 | 0 | const int64_t hit_count = |
912 | 0 | _table_reader != nullptr ? _table_reader->condition_cache_hit_count() : 0; |
913 | 0 | const int64_t hit_delta = hit_count - _reported_condition_cache_hit_count; |
914 | 0 | if (hit_delta > 0) { |
915 | 0 | COUNTER_UPDATE(local_state->_condition_cache_hit_counter, hit_delta); |
916 | 0 | _reported_condition_cache_hit_count = hit_count; |
917 | 0 | } |
918 | 0 | const int64_t filtered_rows = _io_ctx != nullptr ? _io_ctx->condition_cache_filtered_rows : 0; |
919 | 0 | const int64_t filtered_delta = filtered_rows - _reported_condition_cache_filtered_rows; |
920 | 0 | if (filtered_delta > 0) { |
921 | 0 | COUNTER_UPDATE(local_state->_condition_cache_filtered_rows_counter, filtered_delta); |
922 | 0 | _reported_condition_cache_filtered_rows = filtered_rows; |
923 | 0 | } |
924 | 0 | } |
925 | | |
926 | | } // namespace doris |