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 "exprs/runtime_filter_expr.h" |
46 | | #include "exprs/vexpr.h" |
47 | | #include "exprs/vexpr_context.h" |
48 | | #include "exprs/vslot_ref.h" |
49 | | #include "format/format_common.h" |
50 | | #include "format_v2/column_mapper.h" |
51 | | #include "format_v2/jni/iceberg_sys_table_reader.h" |
52 | | #include "format_v2/jni/jdbc_reader.h" |
53 | | #include "format_v2/jni/max_compute_jni_reader.h" |
54 | | #include "format_v2/jni/trino_connector_jni_reader.h" |
55 | | #include "format_v2/table/hive_reader.h" |
56 | | #include "format_v2/table/hudi_reader.h" |
57 | | #include "format_v2/table/iceberg_reader.h" |
58 | | #include "format_v2/table/paimon_reader.h" |
59 | | #include "format_v2/table/remote_doris_reader.h" |
60 | | #include "format_v2/table_reader.h" |
61 | | #include "io/cache/block_file_cache_profile.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 | 99.9k | std::string table_format_name(const TFileRangeDesc& range) { |
74 | 99.9k | return range.__isset.table_format_params ? range.table_format_params.table_format_type |
75 | 99.9k | : "NotSet"; |
76 | 99.9k | } |
77 | | |
78 | | TFileFormatType::type get_range_format_type(const TFileScanRangeParams& params, |
79 | 236k | const TFileRangeDesc& range) { |
80 | 236k | return range.__isset.format_type ? range.format_type : params.format_type; |
81 | 236k | } |
82 | | |
83 | 60.5k | bool is_supported_table_format(const TFileRangeDesc& range) { |
84 | 60.5k | const auto table_format = table_format_name(range); |
85 | 60.5k | if (table_format == "hudi" && range.__isset.table_format_params && |
86 | 60.5k | range.table_format_params.__isset.hudi_params && |
87 | 60.5k | range.table_format_params.hudi_params.__isset.delta_logs && |
88 | 60.5k | !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 | 60.5k | return table_format == "NotSet" || table_format == "tvf" || table_format == "hive" || |
94 | 60.5k | table_format == "iceberg" || table_format == "paimon" || table_format == "hudi"; |
95 | 60.5k | } |
96 | | |
97 | 101 | bool is_supported_arrow_table_format(const TFileRangeDesc& range) { |
98 | 101 | return table_format_name(range) == "remote_doris"; |
99 | 101 | } |
100 | | |
101 | 5 | bool is_supported_jni_table_format(const TFileRangeDesc& range) { |
102 | 5 | const auto table_format = table_format_name(range); |
103 | 5 | if (table_format == "paimon") { |
104 | 2 | return range.__isset.table_format_params && |
105 | 2 | range.table_format_params.__isset.paimon_params && |
106 | 2 | range.table_format_params.paimon_params.__isset.reader_type && |
107 | 2 | range.table_format_params.paimon_params.reader_type == TPaimonReaderType::PAIMON_JNI; |
108 | 2 | } |
109 | 3 | return table_format == "jdbc" || table_format == "iceberg" || table_format == "hudi" || |
110 | 3 | table_format == "max_compute" || table_format == "trino_connector"; |
111 | 5 | } |
112 | | |
113 | 6.28k | bool is_csv_format(TFileFormatType::type format_type) { |
114 | 6.28k | switch (format_type) { |
115 | 959 | case TFileFormatType::FORMAT_CSV_PLAIN: |
116 | 960 | case TFileFormatType::FORMAT_CSV_GZ: |
117 | 961 | case TFileFormatType::FORMAT_CSV_BZ2: |
118 | 962 | case TFileFormatType::FORMAT_CSV_LZ4FRAME: |
119 | 963 | case TFileFormatType::FORMAT_CSV_LZ4BLOCK: |
120 | 964 | case TFileFormatType::FORMAT_CSV_LZOP: |
121 | 965 | case TFileFormatType::FORMAT_CSV_DEFLATE: |
122 | 966 | case TFileFormatType::FORMAT_CSV_SNAPPYBLOCK: |
123 | 967 | case TFileFormatType::FORMAT_PROTO: |
124 | 967 | return true; |
125 | 5.31k | default: |
126 | 5.31k | return false; |
127 | 6.28k | } |
128 | 6.28k | } |
129 | | |
130 | 5.31k | bool is_text_format(TFileFormatType::type format_type) { |
131 | 5.31k | return format_type == TFileFormatType::FORMAT_TEXT; |
132 | 5.31k | } |
133 | | |
134 | 604 | bool is_json_format(TFileFormatType::type format_type) { |
135 | 604 | return format_type == TFileFormatType::FORMAT_JSON; |
136 | 604 | } |
137 | | |
138 | 6 | bool is_native_format(TFileFormatType::type format_type) { |
139 | 6 | return format_type == TFileFormatType::FORMAT_NATIVE; |
140 | 6 | } |
141 | | |
142 | 236k | bool is_partition_slot(const TFileScanSlotInfo& slot_info, const std::string& column_name) { |
143 | 236k | if (column_name.starts_with(BeConsts::GLOBAL_ROWID_COL) || |
144 | 236k | column_name == BeConsts::ICEBERG_ROWID_COL) { |
145 | 4.47k | return false; |
146 | 4.47k | } |
147 | 231k | return slot_info.__isset.category ? slot_info.category == TColumnCategory::PARTITION_KEY |
148 | 231k | : !slot_info.is_file_slot; |
149 | 236k | } |
150 | | |
151 | 224k | bool is_data_file_slot(const TFileScanSlotInfo& slot_info, const std::string& column_name) { |
152 | 224k | if (column_name.starts_with(BeConsts::GLOBAL_ROWID_COL) || |
153 | 224k | column_name == BeConsts::ICEBERG_ROWID_COL) { |
154 | 4.47k | return false; |
155 | 4.47k | } |
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 | 219k | if (slot_info.__isset.category) { |
161 | 219k | return slot_info.category == TColumnCategory::REGULAR || |
162 | 219k | slot_info.category == TColumnCategory::GENERATED; |
163 | 219k | } |
164 | 2 | return slot_info.is_file_slot; |
165 | 219k | } |
166 | | |
167 | | Status rewrite_slot_refs_to_global_index( |
168 | | VExprSPtr* expr, |
169 | 358k | const std::unordered_map<int32_t, format::GlobalIndex>& slot_id_to_global_index) { |
170 | 358k | DORIS_CHECK(expr != nullptr); |
171 | 358k | if (*expr == nullptr) { |
172 | 0 | return Status::OK(); |
173 | 0 | } |
174 | 358k | if (auto* runtime_filter = dynamic_cast<RuntimeFilterExpr*>(expr->get()); |
175 | 358k | runtime_filter != nullptr) { |
176 | 14.9k | auto impl = runtime_filter->get_impl(); |
177 | 14.9k | DORIS_CHECK(impl != nullptr); |
178 | 14.9k | RETURN_IF_ERROR(rewrite_slot_refs_to_global_index(&impl, slot_id_to_global_index)); |
179 | 14.9k | runtime_filter->set_impl(std::move(impl)); |
180 | 14.9k | return Status::OK(); |
181 | 14.9k | } |
182 | 343k | if ((*expr)->is_slot_ref()) { |
183 | 109k | const auto* slot_ref = assert_cast<const VSlotRef*>(expr->get()); |
184 | 109k | const auto global_index_it = slot_id_to_global_index.find(slot_ref->slot_id()); |
185 | 109k | 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 | 109k | const auto global_index = global_index_it->second; |
195 | 109k | *expr = VSlotRef::create_shared(cast_set<int>(global_index.value()), |
196 | 109k | cast_set<int>(global_index.value()), -1, |
197 | 109k | slot_ref->data_type(), slot_ref->column_name()); |
198 | 109k | RETURN_IF_ERROR(expr->get()->prepare(nullptr, RowDescriptor(), nullptr)); |
199 | 109k | return Status::OK(); |
200 | 109k | } |
201 | 233k | auto children = (*expr)->children(); |
202 | 237k | for (auto& child : children) { |
203 | 237k | if (child == nullptr) { |
204 | 0 | continue; |
205 | 0 | } |
206 | 237k | RETURN_IF_ERROR(rewrite_slot_refs_to_global_index(&child, slot_id_to_global_index)); |
207 | 237k | } |
208 | 233k | (*expr)->set_children(std::move(children)); |
209 | 233k | return Status::OK(); |
210 | 233k | } |
211 | | |
212 | | } // namespace |
213 | | |
214 | | #ifdef BE_TEST |
215 | | FileScannerV2::FileScannerV2(RuntimeState* state, RuntimeProfile* profile, |
216 | | std::unique_ptr<format::TableReader> table_reader) |
217 | | : Scanner(state, profile), _table_reader(std::move(table_reader)) {} |
218 | | |
219 | | Status FileScannerV2::TEST_validate_scan_range(const TFileScanRangeParams& params, |
220 | | const TFileRangeDesc& range) { |
221 | | return _validate_scan_range(params, range); |
222 | | } |
223 | | |
224 | | Status FileScannerV2::TEST_to_file_format(TFileFormatType::type format_type, |
225 | | format::FileFormat* file_format) { |
226 | | return _to_file_format(format_type, file_format); |
227 | | } |
228 | | |
229 | | bool FileScannerV2::TEST_is_partition_slot(const TFileScanSlotInfo& slot_info, |
230 | | const std::string& column_name) { |
231 | | return is_partition_slot(slot_info, column_name); |
232 | | } |
233 | | |
234 | | bool FileScannerV2::TEST_is_data_file_slot(const TFileScanSlotInfo& slot_info, |
235 | | const std::string& column_name) { |
236 | | return is_data_file_slot(slot_info, column_name); |
237 | | } |
238 | | |
239 | | Status FileScannerV2::TEST_rewrite_slot_refs_to_global_index( |
240 | | VExprSPtr* expr, |
241 | | const std::unordered_map<int32_t, format::GlobalIndex>& slot_id_to_global_index) { |
242 | | return rewrite_slot_refs_to_global_index(expr, slot_id_to_global_index); |
243 | | } |
244 | | |
245 | | FileScannerV2::RealtimeCounterDeltas FileScannerV2::TEST_collect_realtime_counter_deltas( |
246 | | const io::FileReaderStats& file_reader_stats, |
247 | | const io::FileCacheStatistics& file_cache_statistics, |
248 | | UncachedReaderBytesStorage uncached_reader_bytes_storage, int64_t* last_read_bytes, |
249 | | int64_t* last_read_rows, int64_t* last_bytes_read_from_local, |
250 | | int64_t* last_bytes_read_from_remote) { |
251 | | return _collect_realtime_counter_deltas(file_reader_stats, file_cache_statistics, |
252 | | uncached_reader_bytes_storage, last_read_bytes, |
253 | | last_read_rows, last_bytes_read_from_local, |
254 | | last_bytes_read_from_remote); |
255 | | } |
256 | | |
257 | | void FileScannerV2::TEST_report_file_cache_profile( |
258 | | RuntimeProfile* profile, const io::FileCacheStatistics& file_cache_statistics) { |
259 | | _report_file_cache_profile(profile, file_cache_statistics); |
260 | | } |
261 | | |
262 | | bool FileScannerV2::TEST_should_skip_not_found(const Status& status, bool ignore_not_found) { |
263 | | return _should_skip_not_found(status, ignore_not_found); |
264 | | } |
265 | | #endif |
266 | | |
267 | 60.6k | bool FileScannerV2::is_supported(const TFileScanRangeParams& params, const TFileRangeDesc& range) { |
268 | 60.6k | const auto format_type = get_range_format_type(params, range); |
269 | 60.6k | if (format_type == TFileFormatType::FORMAT_PARQUET || |
270 | 60.6k | format_type == TFileFormatType::FORMAT_ORC) { |
271 | 54.3k | return is_supported_table_format(range); |
272 | 54.3k | } else if (format_type == TFileFormatType::FORMAT_ARROW) { |
273 | 101 | return is_supported_arrow_table_format(range); |
274 | 6.29k | } else if (format_type == TFileFormatType::FORMAT_JNI) { |
275 | 5 | return is_supported_jni_table_format(range); |
276 | 6.28k | } else if (is_csv_format(format_type) || is_text_format(format_type) || |
277 | 6.28k | is_json_format(format_type) || is_native_format(format_type)) { |
278 | 6.28k | return is_supported_table_format(range); |
279 | 6.28k | } else { |
280 | 7 | LOG(WARNING) << "Unsupported file format type " << format_type << " for file scanner v2"; |
281 | 7 | return false; |
282 | 7 | } |
283 | 60.6k | } |
284 | | |
285 | | Status FileScannerV2::_validate_scan_range(const TFileScanRangeParams& params, |
286 | 60.6k | const TFileRangeDesc& range) { |
287 | 60.6k | if (!is_supported(params, range)) { |
288 | 1 | return Status::NotSupported( |
289 | 1 | "FileScannerV2 does not support table format {} with file format {}", |
290 | 1 | table_format_name(range), to_string(get_range_format_type(params, range))); |
291 | 1 | } |
292 | 60.6k | return Status::OK(); |
293 | 60.6k | } |
294 | | |
295 | | FileScannerV2::FileScannerV2(RuntimeState* state, FileScanLocalState* local_state, int64_t limit, |
296 | | std::shared_ptr<SplitSourceConnector> split_source, |
297 | | RuntimeProfile* profile, ShardedKVCache* kv_cache, |
298 | | const std::unordered_map<std::string, int>* colname_to_slot_id) |
299 | 73.8k | : Scanner(state, local_state, limit, profile), |
300 | 73.8k | _split_source(std::move(split_source)), |
301 | 73.8k | _kv_cache(kv_cache) { |
302 | 73.8k | (void)colname_to_slot_id; |
303 | 73.8k | if (state->get_query_ctx() != nullptr && |
304 | 73.9k | state->get_query_ctx()->file_scan_range_params_map.count(local_state->parent_id()) > 0) { |
305 | 73.8k | _params = &(state->get_query_ctx()->file_scan_range_params_map[local_state->parent_id()]); |
306 | 73.8k | } else { |
307 | 38 | _params = _split_source->get_params(); |
308 | 38 | } |
309 | 73.8k | } |
310 | | |
311 | 73.5k | Status FileScannerV2::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) { |
312 | 73.5k | RETURN_IF_ERROR(Scanner::init(state, conjuncts)); |
313 | 73.5k | _get_block_timer = |
314 | 73.5k | ADD_TIMER_WITH_LEVEL(_local_state->scanner_profile(), "FileScannerV2GetBlockTime", 1); |
315 | 73.5k | _not_found_file_counter = ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), |
316 | 73.5k | "NotFoundFileNum", TUnit::UNIT, 1); |
317 | 73.5k | _file_counter = |
318 | 73.5k | ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), "FileNumber", TUnit::UNIT, 1); |
319 | 73.5k | _file_read_bytes_counter = ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), |
320 | 73.5k | "FileReadBytes", TUnit::BYTES, 1); |
321 | 73.5k | _file_read_calls_counter = ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), |
322 | 73.5k | "FileReadCalls", TUnit::UNIT, 1); |
323 | 73.5k | _file_read_time_counter = |
324 | 73.5k | ADD_TIMER_WITH_LEVEL(_local_state->scanner_profile(), "FileReadTime", 1); |
325 | 73.5k | _adaptive_batch_predicted_rows_counter = ADD_COUNTER_WITH_LEVEL( |
326 | 73.5k | _local_state->scanner_profile(), "AdaptiveBatchPredictedRows", TUnit::UNIT, 1); |
327 | 73.5k | _adaptive_batch_actual_bytes_counter = ADD_COUNTER_WITH_LEVEL( |
328 | 73.5k | _local_state->scanner_profile(), "AdaptiveBatchActualBytes", TUnit::BYTES, 1); |
329 | 73.5k | _adaptive_batch_probe_count_counter = ADD_COUNTER_WITH_LEVEL( |
330 | 73.5k | _local_state->scanner_profile(), "AdaptiveBatchProbeCount", TUnit::UNIT, 1); |
331 | 73.5k | _file_cache_statistics = std::make_unique<io::FileCacheStatistics>(); |
332 | 73.5k | _file_reader_stats = std::make_unique<io::FileReaderStats>(); |
333 | 73.5k | RETURN_IF_ERROR(_init_io_ctx()); |
334 | 73.5k | _io_ctx->file_cache_stats = _file_cache_statistics.get(); |
335 | 73.5k | _io_ctx->file_reader_stats = _file_reader_stats.get(); |
336 | 73.5k | _io_ctx->is_disposable = _state->query_options().disable_file_cache; |
337 | 73.5k | return Status::OK(); |
338 | 73.5k | } |
339 | | |
340 | 74.1k | Status FileScannerV2::_open_impl(RuntimeState* state) { |
341 | 74.1k | RETURN_IF_CANCELLED(state); |
342 | 74.1k | RETURN_IF_ERROR(Scanner::_open_impl(state)); |
343 | 74.1k | RETURN_IF_ERROR(_get_next_scan_range(&_first_scan_range)); |
344 | 74.1k | if (_first_scan_range) { |
345 | 39.3k | RETURN_IF_ERROR(_create_table_reader_for_format(_current_range, &_table_reader)); |
346 | 39.3k | DORIS_CHECK(_table_reader != nullptr); |
347 | 39.3k | RETURN_IF_ERROR(_init_expr_ctxes()); |
348 | 39.3k | RETURN_IF_ERROR(_init_table_reader(_current_range)); |
349 | 39.3k | } |
350 | 74.1k | return Status::OK(); |
351 | 74.1k | } |
352 | | |
353 | 168k | Status FileScannerV2::_get_next_scan_range(bool* has_next) { |
354 | 168k | DORIS_CHECK(has_next != nullptr); |
355 | 168k | RETURN_IF_ERROR(_split_source->get_next(has_next, &_current_range)); |
356 | 168k | if (*has_next) { |
357 | 60.6k | RETURN_IF_ERROR(_validate_scan_range(*_params, _current_range)); |
358 | 60.6k | } |
359 | 168k | return Status::OK(); |
360 | 168k | } |
361 | | |
362 | 189k | Status FileScannerV2::_get_block_impl(RuntimeState* state, Block* block, bool* eof) { |
363 | 248k | while (true) { |
364 | 248k | RETURN_IF_CANCELLED(state); |
365 | 248k | if (!_has_prepared_split) { |
366 | 133k | RETURN_IF_ERROR(_prepare_next_split(eof)); |
367 | 133k | if (*eof) { |
368 | 73.5k | return Status::OK(); |
369 | 73.5k | } |
370 | 133k | } |
371 | | |
372 | 175k | { |
373 | 175k | SCOPED_TIMER(_get_block_timer); |
374 | 175k | if (_should_run_adaptive_batch_size()) { |
375 | 171k | _table_reader->set_batch_size(_predict_reader_batch_rows()); |
376 | 171k | } |
377 | 175k | const auto status = _table_reader->get_block(block, eof); |
378 | 175k | if (_should_skip_not_found(status, config::ignore_not_found_file_in_external_table)) { |
379 | 0 | RETURN_IF_ERROR(_table_reader->abort_split()); |
380 | 0 | COUNTER_UPDATE(_not_found_file_counter, 1); |
381 | 0 | _state->update_num_finished_scan_range(1); |
382 | 0 | _has_prepared_split = false; |
383 | 0 | block->clear_column_data(cast_set<int64_t>(_projected_columns.size())); |
384 | 0 | *eof = false; |
385 | 0 | continue; |
386 | 0 | } |
387 | 175k | RETURN_IF_ERROR(status); |
388 | 175k | } |
389 | 175k | if (*eof) { |
390 | 59.7k | _state->update_num_finished_scan_range(1); |
391 | 59.7k | _has_prepared_split = false; |
392 | 59.7k | *eof = false; |
393 | 59.7k | continue; |
394 | 59.7k | } |
395 | 115k | _update_adaptive_batch_size(*block); |
396 | 115k | return Status::OK(); |
397 | 175k | } |
398 | 189k | } |
399 | | |
400 | 133k | Status FileScannerV2::_prepare_next_split(bool* eos) { |
401 | 134k | while (true) { |
402 | 134k | bool has_next = _first_scan_range; |
403 | 134k | if (!_first_scan_range) { |
404 | 94.8k | RETURN_IF_ERROR(_get_next_scan_range(&has_next)); |
405 | 94.8k | } |
406 | 134k | _first_scan_range = false; |
407 | 134k | if (!has_next || _should_stop) { |
408 | 73.5k | *eos = true; |
409 | 73.5k | return Status::OK(); |
410 | 73.5k | } |
411 | 60.6k | DORIS_CHECK(_table_reader != nullptr); |
412 | 60.6k | _current_range_path = _current_range.path; |
413 | | |
414 | 60.6k | const auto format_type = get_range_format_type(*_params, _current_range); |
415 | 60.6k | _init_adaptive_batch_size_state(format_type); |
416 | 60.6k | if (_should_run_adaptive_batch_size()) { |
417 | | // JNI readers open eagerly in prepare_split(). Seed the probe size first so readers |
418 | | // such as Paimon also use it for their first physical read batch. |
419 | 58.3k | _table_reader->set_batch_size(_predict_reader_batch_rows()); |
420 | 58.3k | } |
421 | 60.6k | std::map<std::string, Field> partition_values; |
422 | 60.6k | RETURN_IF_ERROR(_generate_partition_values(_current_range, &partition_values)); |
423 | 60.6k | const auto status = |
424 | 60.6k | _prepare_table_reader_split(_current_range, std::move(partition_values)); |
425 | 60.6k | if (_should_skip_not_found(status, config::ignore_not_found_file_in_external_table)) { |
426 | 0 | RETURN_IF_ERROR(_table_reader->abort_split()); |
427 | 0 | COUNTER_UPDATE(_not_found_file_counter, 1); |
428 | 0 | _state->update_num_finished_scan_range(1); |
429 | 0 | continue; |
430 | 0 | } |
431 | 60.6k | RETURN_IF_ERROR(status); |
432 | 60.6k | if (_table_reader->current_split_pruned()) { |
433 | 278 | _state->update_num_finished_scan_range(1); |
434 | 278 | continue; |
435 | 278 | } |
436 | 60.3k | COUNTER_UPDATE(_file_counter, 1); |
437 | 60.3k | _has_prepared_split = true; |
438 | 60.3k | *eos = false; |
439 | 60.3k | return Status::OK(); |
440 | 60.6k | } |
441 | 133k | } |
442 | | |
443 | 39.3k | Status FileScannerV2::_init_table_reader(const TFileRangeDesc& range) { |
444 | 39.3k | const auto format_type = get_range_format_type(*_params, range); |
445 | 39.3k | format::FileFormat file_format; |
446 | 39.3k | RETURN_IF_ERROR(_to_file_format(format_type, &file_format)); |
447 | 39.3k | DORIS_CHECK(_table_reader != nullptr); |
448 | | |
449 | 39.3k | VExprContextSPtrs table_conjuncts; |
450 | 39.3k | RETURN_IF_ERROR(_build_table_conjuncts(&table_conjuncts)); |
451 | 39.3k | RETURN_IF_ERROR(_table_reader->init({ |
452 | 39.3k | .projected_columns = _projected_columns, |
453 | 39.3k | .conjuncts = std::move(table_conjuncts), |
454 | 39.3k | .format = file_format, |
455 | 39.3k | .scan_params = const_cast<TFileScanRangeParams*>(_params), |
456 | 39.3k | .io_ctx = _io_ctx, |
457 | 39.3k | .runtime_state = _state, |
458 | 39.3k | .scanner_profile = _local_state->scanner_profile(), |
459 | 39.3k | .file_slot_descs = &_file_slot_descs, |
460 | 39.3k | .push_down_agg_type = _local_state->get_push_down_agg_type(), |
461 | 39.3k | .condition_cache_digest = _local_state->get_condition_cache_digest(), |
462 | 39.3k | })); |
463 | 39.3k | return Status::OK(); |
464 | 39.3k | } |
465 | | |
466 | | Status FileScannerV2::_create_table_reader_for_format( |
467 | 39.3k | const TFileRangeDesc& range, std::unique_ptr<format::TableReader>* reader) const { |
468 | 39.3k | DORIS_CHECK(reader != nullptr); |
469 | 39.3k | const auto table_format = table_format_name(range); |
470 | 39.3k | if (table_format == "NotSet" || table_format == "tvf") { |
471 | 4.30k | *reader = std::make_unique<format::TableReader>(); |
472 | 35.0k | } else if (table_format == "hive") { |
473 | 19.9k | *reader = format::hive::HiveReader::create_unique(); |
474 | 19.9k | } else if (table_format == "iceberg") { |
475 | 15.0k | if (get_range_format_type(*_params, range) == TFileFormatType::FORMAT_JNI) { |
476 | 0 | *reader = std::make_unique<format::iceberg::IcebergSysTableJniReader>(); |
477 | 15.0k | } else { |
478 | 15.0k | *reader = std::make_unique<format::iceberg::IcebergTableReader>(); |
479 | 15.0k | } |
480 | 15.0k | } else if (table_format == "paimon") { |
481 | 0 | *reader = std::make_unique<format::paimon::PaimonHybridReader>(); |
482 | 88 | } else if (table_format == "hudi") { |
483 | 0 | *reader = std::make_unique<format::hudi::HudiHybridReader>(); |
484 | 88 | } else if (table_format == "jdbc") { |
485 | 0 | *reader = std::make_unique<format::jdbc::JdbcJniReader>(); |
486 | 88 | } else if (table_format == "max_compute") { |
487 | 0 | const auto* mc_desc = |
488 | 0 | static_cast<const MaxComputeTableDescriptor*>(_output_tuple_desc->table_desc()); |
489 | 0 | RETURN_IF_ERROR(mc_desc->init_status()); |
490 | 0 | *reader = std::make_unique<format::max_compute::MaxComputeJniReader>(mc_desc); |
491 | 88 | } else if (table_format == "trino_connector") { |
492 | 0 | *reader = std::make_unique<format::trino_connector::TrinoConnectorJniReader>(); |
493 | 98 | } else if (table_format == "remote_doris") { |
494 | 98 | *reader = std::make_unique<format::remote_doris::RemoteDorisReader>(); |
495 | 18.4E | } else { |
496 | 18.4E | return Status::NotSupported("FileScannerV2 does not support table format {}", table_format); |
497 | 18.4E | } |
498 | 39.3k | return Status::OK(); |
499 | 39.3k | } |
500 | | |
501 | | Status FileScannerV2::_prepare_table_reader_split(const TFileRangeDesc& range, |
502 | 60.6k | std::map<std::string, Field> partition_values) { |
503 | 60.6k | format::FileFormat current_split_format; |
504 | 60.6k | RETURN_IF_ERROR(_to_file_format(get_range_format_type(*_params, range), ¤t_split_format)); |
505 | 60.6k | VExprContextSPtrs conjuncts; |
506 | 60.6k | RETURN_IF_ERROR(_build_table_conjuncts(&conjuncts)); |
507 | 60.6k | VExprContextSPtrs partition_prune_conjuncts; |
508 | 60.6k | if (_state->query_options().enable_runtime_filter_partition_prune) { |
509 | 59.1k | RETURN_IF_ERROR(_build_table_conjuncts(&partition_prune_conjuncts)); |
510 | 59.1k | } |
511 | 60.6k | RETURN_IF_ERROR(_table_reader->prepare_split({ |
512 | 60.6k | .partition_values = std::move(partition_values), |
513 | 60.6k | .conjuncts = std::move(conjuncts), |
514 | 60.6k | .partition_prune_conjuncts = std::move(partition_prune_conjuncts), |
515 | | // A metadata COUNT split may span scheduler turns. Do not enter that irreversible |
516 | | // synthetic-row path while a runtime filter can still arrive between batches. |
517 | 60.6k | .all_runtime_filters_applied = _applied_rf_num == _total_rf_num, |
518 | 60.6k | .cache = _kv_cache, |
519 | 60.6k | .current_range = range, |
520 | 60.6k | .current_split_format = current_split_format, |
521 | 60.6k | .global_rowid_context = _create_global_rowid_context(range), |
522 | 60.6k | })); |
523 | 60.6k | return Status::OK(); |
524 | 60.6k | } |
525 | | |
526 | 236k | bool FileScannerV2::_should_skip_not_found(const Status& status, bool ignore_not_found) { |
527 | 236k | return ignore_not_found && status.is<ErrorCode::NOT_FOUND>(); |
528 | 236k | } |
529 | | |
530 | 7.55k | bool FileScannerV2::_should_enable_file_meta_cache() const { |
531 | 7.55k | return ExecEnv::GetInstance()->file_meta_cache()->enabled() && |
532 | 7.55k | _split_source->num_scan_ranges() < config::max_external_file_meta_cache_num / 3; |
533 | 7.55k | } |
534 | | |
535 | | std::optional<format::GlobalRowIdContext> FileScannerV2::_create_global_rowid_context( |
536 | 60.6k | const TFileRangeDesc& range) const { |
537 | 60.6k | if (!_need_global_rowid_column) { |
538 | 53.0k | return std::nullopt; |
539 | 53.0k | } |
540 | 7.55k | auto& id_file_map = _state->get_id_file_map(); |
541 | 7.55k | DORIS_CHECK(id_file_map != nullptr); |
542 | 7.55k | const auto file_id = id_file_map->get_file_mapping_id( |
543 | 7.55k | std::make_shared<FileMapping>(_local_state->cast<FileScanLocalState>().parent_id(), |
544 | 7.55k | range, _should_enable_file_meta_cache())); |
545 | 7.55k | return format::GlobalRowIdContext { |
546 | 7.55k | .version = IdManager::ID_VERSION, |
547 | 7.55k | .backend_id = BackendOptions::get_backend_id(), |
548 | 7.55k | .file_id = file_id, |
549 | 7.55k | }; |
550 | 60.6k | } |
551 | | |
552 | | Status FileScannerV2::_generate_partition_values( |
553 | 60.5k | const TFileRangeDesc& range, std::map<std::string, Field>* partition_values) const { |
554 | 60.5k | DORIS_CHECK(partition_values != nullptr); |
555 | 60.5k | partition_values->clear(); |
556 | 60.5k | if (!range.__isset.columns_from_path_keys || !range.__isset.columns_from_path) { |
557 | 45.0k | return Status::OK(); |
558 | 45.0k | } |
559 | 15.5k | DORIS_CHECK(range.columns_from_path_keys.size() == range.columns_from_path.size()); |
560 | 42.5k | for (size_t idx = 0; idx < range.columns_from_path_keys.size(); ++idx) { |
561 | 26.9k | const auto& key = range.columns_from_path_keys[idx]; |
562 | 26.9k | const auto it = _partition_slot_descs.find(key); |
563 | 26.9k | if (it == _partition_slot_descs.end()) { |
564 | 13.8k | continue; |
565 | 13.8k | } |
566 | 13.0k | const auto& value = range.columns_from_path[idx]; |
567 | 13.0k | const bool is_null = range.__isset.columns_from_path_is_null && |
568 | 13.0k | idx < range.columns_from_path_is_null.size() && |
569 | 13.0k | range.columns_from_path_is_null[idx]; |
570 | 13.0k | Field field; |
571 | 13.0k | DORIS_CHECK(it->second.slot_desc != nullptr); |
572 | 13.0k | RETURN_IF_ERROR(_parse_partition_value(it->second.slot_desc, value, is_null, &field)); |
573 | 13.0k | partition_values->emplace(it->second.canonical_name, std::move(field)); |
574 | 13.0k | } |
575 | 15.5k | return Status::OK(); |
576 | 15.5k | } |
577 | | |
578 | | Status FileScannerV2::_parse_partition_value(const SlotDescriptor* slot_desc, |
579 | | const std::string& value, bool is_null, |
580 | 13.0k | Field* field) const { |
581 | 13.0k | DORIS_CHECK(slot_desc != nullptr); |
582 | 13.0k | DORIS_CHECK(field != nullptr); |
583 | 13.0k | if (is_null) { |
584 | 866 | *field = Field::create_field<TYPE_NULL>(Null()); |
585 | 866 | return Status::OK(); |
586 | 866 | } |
587 | 12.2k | const auto data_type = remove_nullable(slot_desc->get_data_type_ptr()); |
588 | 12.2k | auto column = data_type->create_column(); |
589 | 12.2k | auto serde = data_type->get_serde(); |
590 | 12.2k | DataTypeSerDe::FormatOptions options; |
591 | 12.2k | options.converted_from_string = true; |
592 | 12.2k | StringRef ref(value.data(), value.size()); |
593 | 12.2k | RETURN_IF_ERROR(serde->from_string(ref, *column, options)); |
594 | 12.2k | DORIS_CHECK(column->size() == 1); |
595 | 12.2k | *field = (*column)[0]; |
596 | 12.2k | return Status::OK(); |
597 | 12.2k | } |
598 | | |
599 | 39.3k | Status FileScannerV2::_init_expr_ctxes() { |
600 | 39.3k | _slot_id_to_desc.clear(); |
601 | 39.3k | _slot_id_to_global_index.clear(); |
602 | 39.3k | _partition_slot_descs.clear(); |
603 | 39.3k | _file_slot_descs.clear(); |
604 | 235k | for (const auto* slot_desc : _output_tuple_desc->slots()) { |
605 | 235k | _slot_id_to_desc.emplace(slot_desc->id(), slot_desc); |
606 | 235k | } |
607 | 39.3k | DORIS_CHECK(_table_reader != nullptr); |
608 | 39.3k | RETURN_IF_ERROR(_build_projected_columns(*_table_reader)); |
609 | 39.3k | return Status::OK(); |
610 | 39.3k | } |
611 | | |
612 | 39.3k | Status FileScannerV2::_build_projected_columns(const format::TableReader& table_reader) { |
613 | 39.3k | _projected_columns.clear(); |
614 | 39.3k | _projected_columns.reserve(_params->required_slots.size()); |
615 | 39.3k | _need_global_rowid_column = false; |
616 | 39.3k | format::ProjectedColumnBuildContext build_context { |
617 | 39.3k | .scan_params = _params, |
618 | 39.3k | .range = &_current_range, |
619 | 39.3k | .runtime_state = _state, |
620 | 39.3k | }; |
621 | | |
622 | 275k | for (size_t slot_idx = 0; slot_idx < _params->required_slots.size(); ++slot_idx) { |
623 | 236k | const auto& slot_info = _params->required_slots[slot_idx]; |
624 | 236k | const auto it = _slot_id_to_desc.find(slot_info.slot_id); |
625 | 236k | if (it == _slot_id_to_desc.end()) { |
626 | 0 | return Status::InternalError("Unknown source slot descriptor, slot_id={}", |
627 | 0 | slot_info.slot_id); |
628 | 0 | } |
629 | 236k | auto column = _build_table_column(it->second); |
630 | 236k | if (column.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) { |
631 | 4.27k | _need_global_rowid_column = true; |
632 | 4.27k | } |
633 | 236k | RETURN_IF_ERROR(_build_default_expr(slot_info, &column.default_expr)); |
634 | 236k | build_context.schema_column.reset(); |
635 | 236k | RETURN_IF_ERROR(table_reader.annotate_projected_column(slot_info, &build_context, &column)); |
636 | | // Build nested children from access paths generated by the slot's access-path |
637 | | // expressions. A projected column can therefore contain only a subset of the schema |
638 | | // column's nested children. |
639 | 236k | RETURN_IF_ERROR(AccessPathParser::build_nested_children( |
640 | 236k | &column, it->second, |
641 | 236k | build_context.schema_column.has_value() ? &*build_context.schema_column : nullptr)); |
642 | 236k | if (is_partition_slot(slot_info, column.name)) { |
643 | 12.2k | column.is_partition_key = true; |
644 | 12.2k | _partition_slot_descs.emplace( |
645 | 12.2k | column.name, |
646 | 12.2k | PartitionSlotInfo {.slot_desc = it->second, .canonical_name = column.name}); |
647 | 12.2k | for (const auto& alias : column.name_mapping) { |
648 | 0 | _partition_slot_descs.emplace( |
649 | 0 | alias, |
650 | 0 | PartitionSlotInfo {.slot_desc = it->second, .canonical_name = column.name}); |
651 | 0 | } |
652 | 224k | } else if (is_data_file_slot(slot_info, column.name)) { |
653 | 219k | _file_slot_descs.push_back(const_cast<SlotDescriptor*>(it->second)); |
654 | 219k | } |
655 | 236k | const auto global_index = format::GlobalIndex(slot_idx); |
656 | 236k | _slot_id_to_global_index.emplace(slot_info.slot_id, global_index); |
657 | 236k | _projected_columns.push_back(std::move(column)); |
658 | 236k | } |
659 | 39.3k | RETURN_IF_ERROR(table_reader.validate_projected_columns(build_context)); |
660 | 39.3k | return Status::OK(); |
661 | 39.3k | } |
662 | | |
663 | | Status FileScannerV2::_build_default_expr(const TFileScanSlotInfo& slot_info, |
664 | 236k | VExprContextSPtr* ctx) const { |
665 | 236k | DORIS_CHECK(ctx != nullptr); |
666 | 236k | if (slot_info.__isset.default_value_expr && !slot_info.default_value_expr.nodes.empty()) { |
667 | 231k | return VExpr::create_expr_tree(slot_info.default_value_expr, *ctx); |
668 | 231k | } |
669 | | |
670 | 5.04k | if (_params->__isset.default_value_of_src_slot) { |
671 | 5.04k | const auto it = _params->default_value_of_src_slot.find(slot_info.slot_id); |
672 | 5.04k | if (it != _params->default_value_of_src_slot.end() && !it->second.nodes.empty()) { |
673 | 0 | return VExpr::create_expr_tree(it->second, *ctx); |
674 | 0 | } |
675 | 5.04k | } |
676 | 5.03k | return Status::OK(); |
677 | 5.03k | } |
678 | | |
679 | 236k | format::ColumnDefinition FileScannerV2::_build_table_column(const SlotDescriptor* slot_desc) { |
680 | 236k | DORIS_CHECK(slot_desc != nullptr); |
681 | 236k | format::ColumnDefinition column; |
682 | | // TODO(gabriel): why always BY_NAME here? |
683 | 236k | column.identifier = Field::create_field<TYPE_STRING>(slot_desc->col_name()); |
684 | 236k | column.name = slot_desc->col_name(); |
685 | 236k | column.type = slot_desc->get_data_type_ptr(); |
686 | 236k | return column; |
687 | 236k | } |
688 | | |
689 | 159k | Status FileScannerV2::_build_table_conjuncts(VExprContextSPtrs* conjuncts) const { |
690 | 159k | DORIS_CHECK(conjuncts != nullptr); |
691 | 159k | conjuncts->clear(); |
692 | 159k | conjuncts->reserve(_conjuncts.size()); |
693 | 159k | for (const auto& conjunct : _conjuncts) { |
694 | 106k | VExprSPtr root; |
695 | 106k | RETURN_IF_ERROR(format::clone_table_expr_tree(conjunct->root(), &root)); |
696 | 106k | RETURN_IF_ERROR(rewrite_slot_refs_to_global_index(&root, _slot_id_to_global_index)); |
697 | 106k | conjuncts->push_back(VExprContext::create_shared(std::move(root))); |
698 | 106k | } |
699 | 159k | return Status::OK(); |
700 | 159k | } |
701 | | |
702 | 0 | TFileFormatType::type FileScannerV2::_get_current_format_type() const { |
703 | 0 | return get_range_format_type(*_params, _current_range); |
704 | 0 | } |
705 | | |
706 | | Status FileScannerV2::_to_file_format(TFileFormatType::type format_type, |
707 | 99.9k | format::FileFormat* file_format) { |
708 | 99.9k | DORIS_CHECK(file_format != nullptr); |
709 | 99.9k | switch (format_type) { |
710 | 54.0k | case TFileFormatType::FORMAT_PARQUET: |
711 | 54.0k | *file_format = format::FileFormat::PARQUET; |
712 | 54.0k | return Status::OK(); |
713 | 35.6k | case TFileFormatType::FORMAT_ORC: |
714 | 35.6k | *file_format = format::FileFormat::ORC; |
715 | 35.6k | return Status::OK(); |
716 | 1 | case TFileFormatType::FORMAT_JNI: |
717 | 1 | *file_format = format::FileFormat::JNI; |
718 | 1 | return Status::OK(); |
719 | 1.80k | case TFileFormatType::FORMAT_CSV_PLAIN: |
720 | 1.80k | case TFileFormatType::FORMAT_CSV_GZ: |
721 | 1.81k | case TFileFormatType::FORMAT_CSV_BZ2: |
722 | 1.81k | case TFileFormatType::FORMAT_CSV_LZ4FRAME: |
723 | 1.81k | case TFileFormatType::FORMAT_CSV_LZ4BLOCK: |
724 | 1.81k | case TFileFormatType::FORMAT_CSV_LZOP: |
725 | 1.81k | case TFileFormatType::FORMAT_CSV_DEFLATE: |
726 | 1.81k | case TFileFormatType::FORMAT_CSV_SNAPPYBLOCK: |
727 | 1.81k | case TFileFormatType::FORMAT_PROTO: |
728 | 1.81k | *file_format = format::FileFormat::CSV; |
729 | 1.81k | return Status::OK(); |
730 | 7.06k | case TFileFormatType::FORMAT_TEXT: |
731 | 7.06k | *file_format = format::FileFormat::TEXT; |
732 | 7.06k | return Status::OK(); |
733 | 1.19k | case TFileFormatType::FORMAT_JSON: |
734 | 1.19k | *file_format = format::FileFormat::JSON; |
735 | 1.19k | return Status::OK(); |
736 | 5 | case TFileFormatType::FORMAT_NATIVE: |
737 | 5 | *file_format = format::FileFormat::NATIVE; |
738 | 5 | return Status::OK(); |
739 | 197 | case TFileFormatType::FORMAT_ARROW: |
740 | 197 | *file_format = format::FileFormat::ARROW; |
741 | 197 | return Status::OK(); |
742 | 0 | default: |
743 | 0 | return Status::NotSupported("FileScannerV2 does not support file format {}", |
744 | 0 | to_string(format_type)); |
745 | 99.9k | } |
746 | 99.9k | } |
747 | | |
748 | 73.7k | Status FileScannerV2::_init_io_ctx() { |
749 | 73.7k | _io_ctx = std::make_shared<io::IOContext>(); |
750 | 73.7k | _io_ctx->query_id = &_state->query_id(); |
751 | 73.7k | return Status::OK(); |
752 | 73.7k | } |
753 | | |
754 | 60.5k | void FileScannerV2::_reset_adaptive_batch_size_state() { |
755 | 60.5k | _block_size_predictor.reset(); |
756 | 60.5k | COUNTER_SET(_adaptive_batch_predicted_rows_counter, int64_t(0)); |
757 | 60.5k | COUNTER_SET(_adaptive_batch_actual_bytes_counter, int64_t(0)); |
758 | 60.5k | } |
759 | | |
760 | 60.5k | void FileScannerV2::_init_adaptive_batch_size_state(TFileFormatType::type format_type) { |
761 | 60.5k | _reset_adaptive_batch_size_state(); |
762 | 60.5k | if (!_should_enable_adaptive_batch_size(format_type)) { |
763 | 100 | return; |
764 | 100 | } |
765 | | |
766 | | // V2 native file readers do not have reliable row-width hints before the first batch. Start |
767 | | // every split with a small probe, then learn bytes-per-row from the materialized table block |
768 | | // and keep later batches close to RuntimeState::preferred_block_size_bytes(). |
769 | 60.4k | _block_size_predictor = std::make_unique<AdaptiveBlockSizePredictor>( |
770 | 60.4k | _state->preferred_block_size_bytes(), 0.0, ADAPTIVE_BATCH_INITIAL_PROBE_ROWS, |
771 | 60.4k | _state->batch_size()); |
772 | 60.4k | } |
773 | | |
774 | 60.6k | bool FileScannerV2::_should_enable_adaptive_batch_size(TFileFormatType::type format_type) const { |
775 | 60.6k | if (!config::enable_adaptive_batch_size) { |
776 | 0 | return false; |
777 | 0 | } |
778 | 60.6k | switch (format_type) { |
779 | 31.7k | case TFileFormatType::FORMAT_PARQUET: |
780 | 54.2k | case TFileFormatType::FORMAT_ORC: |
781 | 55.2k | case TFileFormatType::FORMAT_CSV_PLAIN: |
782 | 55.2k | case TFileFormatType::FORMAT_CSV_GZ: |
783 | 55.2k | case TFileFormatType::FORMAT_CSV_BZ2: |
784 | 55.2k | case TFileFormatType::FORMAT_CSV_LZ4FRAME: |
785 | 55.2k | case TFileFormatType::FORMAT_CSV_LZ4BLOCK: |
786 | 55.2k | case TFileFormatType::FORMAT_CSV_LZOP: |
787 | 55.2k | case TFileFormatType::FORMAT_CSV_DEFLATE: |
788 | 55.2k | case TFileFormatType::FORMAT_CSV_SNAPPYBLOCK: |
789 | 55.2k | case TFileFormatType::FORMAT_PROTO: |
790 | 59.9k | case TFileFormatType::FORMAT_TEXT: |
791 | 60.5k | case TFileFormatType::FORMAT_JSON: |
792 | 60.5k | case TFileFormatType::FORMAT_JNI: |
793 | 60.5k | return true; |
794 | 100 | default: |
795 | 100 | return false; |
796 | 60.6k | } |
797 | 60.6k | } |
798 | | |
799 | 351k | bool FileScannerV2::_should_run_adaptive_batch_size() const { |
800 | | // COUNT pushdown emits synthetic rows from file metadata and does not materialize file columns, |
801 | | // so there is no useful row-width sample to learn from. |
802 | 351k | return _block_size_predictor != nullptr && |
803 | 351k | _local_state->get_push_down_agg_type() != TPushAggOp::type::COUNT; |
804 | 351k | } |
805 | | |
806 | 229k | size_t FileScannerV2::_predict_reader_batch_rows() { |
807 | 229k | DORIS_CHECK(_block_size_predictor != nullptr); |
808 | | // Before history exists this returns the probe row count; after update(), it returns roughly |
809 | | // preferred_block_size_bytes / EWMA(bytes_per_row), capped by RuntimeState::batch_size(). |
810 | 229k | const size_t predicted_rows = _block_size_predictor->predict_next_rows(); |
811 | 229k | COUNTER_SET(_adaptive_batch_predicted_rows_counter, static_cast<int64_t>(predicted_rows)); |
812 | 229k | return predicted_rows; |
813 | 229k | } |
814 | | |
815 | 115k | void FileScannerV2::_update_adaptive_batch_size(const Block& block) { |
816 | 115k | if (!_should_run_adaptive_batch_size()) { |
817 | 2.15k | return; |
818 | 2.15k | } |
819 | 113k | COUNTER_SET(_adaptive_batch_actual_bytes_counter, static_cast<int64_t>(block.bytes())); |
820 | 113k | if (block.rows() == 0) { |
821 | 0 | return; |
822 | 0 | } |
823 | | // The sample is taken after TableReader has finalized file-local columns to table columns. |
824 | | // This matches the memory shape seen by upstream operators and catches very wide nested |
825 | | // columns, such as map/string payloads, after the first probe batch. |
826 | 113k | if (!_block_size_predictor->has_history()) { |
827 | 45.9k | COUNTER_UPDATE(_adaptive_batch_probe_count_counter, 1); |
828 | 45.9k | } |
829 | 113k | _block_size_predictor->update(block); |
830 | 113k | } |
831 | | |
832 | 74.2k | Status FileScannerV2::close(RuntimeState* state) { |
833 | 74.2k | if (!_try_close()) { |
834 | 1 | return Status::OK(); |
835 | 1 | } |
836 | 74.2k | if (_table_reader != nullptr) { |
837 | 39.3k | const auto close_status = _table_reader->close(); |
838 | 39.3k | if (!close_status.ok()) { |
839 | | // Reserve the close attempt with _try_close(), but commit the scanner-level closed |
840 | | // state only after the retained table reader has completed its retryable cleanup. |
841 | 1 | _is_closed.store(false); |
842 | 1 | return close_status; |
843 | 1 | } |
844 | 39.3k | _report_condition_cache_profile(); |
845 | 39.3k | _table_reader.reset(); |
846 | 39.3k | } |
847 | 74.2k | return Scanner::close(state); |
848 | 74.2k | } |
849 | | |
850 | 74.2k | void FileScannerV2::try_stop() { |
851 | 74.2k | Scanner::try_stop(); |
852 | 74.2k | if (_io_ctx) { |
853 | 74.2k | _io_ctx->should_stop = true; |
854 | 74.2k | } |
855 | 74.2k | } |
856 | | |
857 | 127k | void FileScannerV2::update_realtime_counters() { |
858 | 127k | if (_file_reader_stats == nullptr) { |
859 | 0 | return; |
860 | 0 | } |
861 | 127k | DORIS_CHECK(_file_cache_statistics != nullptr); |
862 | 127k | const int64_t bytes_read = cast_set<int64_t>(_file_reader_stats->read_bytes); |
863 | 127k | auto* local_state = static_cast<FileScanLocalState*>(_local_state); |
864 | 127k | const auto file_type = |
865 | 127k | _current_range.__isset.file_type |
866 | 127k | ? _current_range.file_type |
867 | 127k | : (_params != nullptr && _params->__isset.file_type ? _params->file_type |
868 | 35.8k | : TFileType::FILE_LOCAL); |
869 | 127k | const auto deltas = _collect_realtime_counter_deltas( |
870 | 127k | *_file_reader_stats, *_file_cache_statistics, _uncached_reader_bytes_storage(file_type), |
871 | 127k | &_last_read_bytes, &_last_read_rows, &_last_bytes_read_from_local, |
872 | 127k | &_last_bytes_read_from_remote); |
873 | | |
874 | 127k | COUNTER_UPDATE(local_state->_scan_bytes, deltas.scan_bytes); |
875 | 127k | COUNTER_UPDATE(local_state->_scan_rows, deltas.scan_rows); |
876 | | |
877 | 127k | _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_rows(deltas.scan_rows); |
878 | 127k | _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes(deltas.scan_bytes); |
879 | 127k | _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes_from_local_storage( |
880 | 127k | deltas.scan_bytes_from_local_storage); |
881 | 127k | _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes_from_remote_storage( |
882 | 127k | deltas.scan_bytes_from_remote_storage); |
883 | | |
884 | 127k | COUNTER_SET(_file_read_bytes_counter, bytes_read); |
885 | 127k | COUNTER_SET(_file_read_calls_counter, cast_set<int64_t>(_file_reader_stats->read_calls)); |
886 | 127k | COUNTER_SET(_file_read_time_counter, cast_set<int64_t>(_file_reader_stats->read_time_ns)); |
887 | | |
888 | 127k | DorisMetrics::instance()->query_scan_bytes->increment(deltas.scan_bytes); |
889 | 127k | DorisMetrics::instance()->query_scan_rows->increment(deltas.scan_rows); |
890 | 127k | DorisMetrics::instance()->query_scan_bytes_from_local->increment( |
891 | 127k | deltas.scan_bytes_from_local_storage); |
892 | 127k | DorisMetrics::instance()->query_scan_bytes_from_remote->increment( |
893 | 127k | deltas.scan_bytes_from_remote_storage); |
894 | 127k | } |
895 | | |
896 | | FileScannerV2::RealtimeCounterDeltas FileScannerV2::_collect_realtime_counter_deltas( |
897 | | const io::FileReaderStats& file_reader_stats, |
898 | | const io::FileCacheStatistics& file_cache_statistics, |
899 | | UncachedReaderBytesStorage uncached_reader_bytes_storage, int64_t* last_read_bytes, |
900 | | int64_t* last_read_rows, int64_t* last_bytes_read_from_local, |
901 | 127k | int64_t* last_bytes_read_from_remote) { |
902 | 127k | DORIS_CHECK(last_read_bytes != nullptr); |
903 | 127k | DORIS_CHECK(last_read_rows != nullptr); |
904 | 127k | DORIS_CHECK(last_bytes_read_from_local != nullptr); |
905 | 127k | DORIS_CHECK(last_bytes_read_from_remote != nullptr); |
906 | | |
907 | 127k | const int64_t read_bytes = cast_set<int64_t>(file_reader_stats.read_bytes); |
908 | 127k | const int64_t read_rows = cast_set<int64_t>(file_reader_stats.read_rows); |
909 | 127k | const int64_t bytes_read_from_local = file_cache_statistics.bytes_read_from_local; |
910 | 127k | const int64_t bytes_read_from_remote = file_cache_statistics.bytes_read_from_remote; |
911 | 127k | DORIS_CHECK(read_bytes >= *last_read_bytes); |
912 | 127k | DORIS_CHECK(read_rows >= *last_read_rows); |
913 | 127k | DORIS_CHECK(bytes_read_from_local >= *last_bytes_read_from_local); |
914 | 127k | DORIS_CHECK(bytes_read_from_remote >= *last_bytes_read_from_remote); |
915 | | |
916 | 127k | RealtimeCounterDeltas deltas; |
917 | 127k | deltas.scan_rows = read_rows - *last_read_rows; |
918 | 127k | deltas.scan_bytes = read_bytes - *last_read_bytes; |
919 | | // Peer cache is a known cache source, but it is not remote object storage. |
920 | 127k | const bool has_cache_source_stats = file_cache_statistics.num_local_io_total != 0 || |
921 | 127k | file_cache_statistics.num_remote_io_total != 0 || |
922 | 127k | file_cache_statistics.num_peer_io_total != 0 || |
923 | 127k | bytes_read_from_local != 0 || bytes_read_from_remote != 0 || |
924 | 127k | file_cache_statistics.bytes_read_from_peer != 0; |
925 | 127k | if (!has_cache_source_stats) { |
926 | 113k | switch (uncached_reader_bytes_storage) { |
927 | 857 | case UncachedReaderBytesStorage::LOCAL: |
928 | 857 | deltas.scan_bytes_from_local_storage = deltas.scan_bytes; |
929 | 857 | break; |
930 | 112k | case UncachedReaderBytesStorage::REMOTE: |
931 | 112k | deltas.scan_bytes_from_remote_storage = deltas.scan_bytes; |
932 | 112k | break; |
933 | 262 | case UncachedReaderBytesStorage::NONE: |
934 | 262 | break; |
935 | 113k | } |
936 | 113k | } else { |
937 | 13.4k | deltas.scan_bytes_from_local_storage = bytes_read_from_local - *last_bytes_read_from_local; |
938 | 13.4k | deltas.scan_bytes_from_remote_storage = |
939 | 13.4k | bytes_read_from_remote - *last_bytes_read_from_remote; |
940 | 13.4k | } |
941 | | |
942 | 127k | *last_read_bytes = read_bytes; |
943 | 127k | *last_read_rows = read_rows; |
944 | 127k | *last_bytes_read_from_local = bytes_read_from_local; |
945 | 127k | *last_bytes_read_from_remote = bytes_read_from_remote; |
946 | 127k | return deltas; |
947 | 127k | } |
948 | | |
949 | | FileScannerV2::UncachedReaderBytesStorage FileScannerV2::_uncached_reader_bytes_storage( |
950 | 127k | TFileType::type file_type) { |
951 | 127k | switch (file_type) { |
952 | 856 | case TFileType::FILE_LOCAL: |
953 | 856 | return UncachedReaderBytesStorage::LOCAL; |
954 | 262 | case TFileType::FILE_STREAM: |
955 | 262 | return UncachedReaderBytesStorage::NONE; |
956 | 0 | case TFileType::FILE_BROKER: |
957 | 49.2k | case TFileType::FILE_S3: |
958 | 125k | case TFileType::FILE_HDFS: |
959 | 125k | case TFileType::FILE_NET: |
960 | 125k | case TFileType::FILE_HTTP: |
961 | 125k | return UncachedReaderBytesStorage::REMOTE; |
962 | 127k | } |
963 | 0 | DORIS_CHECK(false) << "unknown file type: " << file_type; |
964 | 0 | return UncachedReaderBytesStorage::NONE; |
965 | 127k | } |
966 | | |
967 | 74.0k | void FileScannerV2::_collect_profile_before_close() { |
968 | 74.0k | _report_file_reader_predicate_filtered_rows(); |
969 | 74.0k | Scanner::_collect_profile_before_close(); |
970 | 74.0k | if (config::enable_file_cache && _state->query_options().enable_file_cache && |
971 | 74.0k | _profile != nullptr) { |
972 | 216 | _report_file_cache_profile(_profile, *_file_cache_statistics); |
973 | 216 | _state->get_query_ctx()->resource_ctx()->io_context()->update_bytes_write_into_cache( |
974 | 216 | _file_cache_statistics->bytes_write_into_cache); |
975 | 216 | } |
976 | 74.0k | if (_file_reader_stats != nullptr) { |
977 | 74.0k | COUNTER_SET(_file_read_bytes_counter, cast_set<int64_t>(_file_reader_stats->read_bytes)); |
978 | 74.0k | COUNTER_SET(_file_read_calls_counter, cast_set<int64_t>(_file_reader_stats->read_calls)); |
979 | 74.0k | COUNTER_SET(_file_read_time_counter, cast_set<int64_t>(_file_reader_stats->read_time_ns)); |
980 | 74.0k | } |
981 | | // Query profiles can be collected before Scanner::close() runs. Publish condition-cache |
982 | | // counters here as well, using deltas so this method and close() cannot double count. |
983 | 74.0k | _report_condition_cache_profile(); |
984 | 74.0k | } |
985 | | |
986 | | void FileScannerV2::_report_file_cache_profile( |
987 | 217 | RuntimeProfile* profile, const io::FileCacheStatistics& file_cache_statistics) { |
988 | 217 | io::FileCacheProfileReporter cache_profile(profile); |
989 | 217 | cache_profile.update(&file_cache_statistics); |
990 | 217 | } |
991 | | |
992 | 74.1k | bool FileScannerV2::_should_update_load_counters() const { |
993 | 74.1k | if (_is_load) { |
994 | 0 | return true; |
995 | 0 | } |
996 | | // TVF based loads (e.g. http_stream, group commit relay) plan the load source as a |
997 | | // tvf query scan without src tuple desc, so _is_load is false. But rows filtered by |
998 | | // the load's WHERE clause still need to be reported as unselected rows. FILE_STREAM |
999 | | // is only reachable from such load entries, never from normal queries, so use it to |
1000 | | // identify these scanners. |
1001 | 74.1k | return (_params != nullptr && _params->__isset.file_type && |
1002 | 74.1k | _params->file_type == TFileType::FILE_STREAM) || |
1003 | 74.1k | (_current_range.__isset.file_type && _current_range.file_type == TFileType::FILE_STREAM); |
1004 | 74.1k | } |
1005 | | |
1006 | 74.0k | void FileScannerV2::_report_file_reader_predicate_filtered_rows() { |
1007 | 18.4E | const int64_t filtered_rows = _io_ctx != nullptr ? _io_ctx->predicate_filtered_rows : 0; |
1008 | 74.0k | const int64_t filtered_delta = filtered_rows - _reported_predicate_filtered_rows; |
1009 | 74.0k | if (filtered_delta > 0) { |
1010 | | // File readers can evaluate localized conjuncts before a block reaches Scanner. Count |
1011 | | // those rows as scanner-level unselected rows so load statistics stay identical no matter |
1012 | | // whether a predicate is pushed down or evaluated by Scanner::_filter_output_block(). |
1013 | 2.72k | _counter.num_rows_unselected += filtered_delta; |
1014 | 2.72k | _reported_predicate_filtered_rows = filtered_rows; |
1015 | 2.72k | } |
1016 | 74.0k | } |
1017 | | |
1018 | 113k | void FileScannerV2::_report_condition_cache_profile() { |
1019 | 113k | auto* local_state = static_cast<FileScanLocalState*>(_local_state); |
1020 | 113k | const int64_t hit_count = |
1021 | 113k | _table_reader != nullptr ? _table_reader->condition_cache_hit_count() : 0; |
1022 | 113k | const int64_t hit_delta = hit_count - _reported_condition_cache_hit_count; |
1023 | 113k | if (hit_delta > 0) { |
1024 | 2.41k | COUNTER_UPDATE(local_state->_condition_cache_hit_counter, hit_delta); |
1025 | 2.41k | _reported_condition_cache_hit_count = hit_count; |
1026 | 2.41k | } |
1027 | 113k | const int64_t filtered_rows = _io_ctx != nullptr ? _io_ctx->condition_cache_filtered_rows : 0; |
1028 | 113k | const int64_t filtered_delta = filtered_rows - _reported_condition_cache_filtered_rows; |
1029 | 113k | if (filtered_delta > 0) { |
1030 | 444 | COUNTER_UPDATE(local_state->_condition_cache_filtered_rows_counter, filtered_delta); |
1031 | 444 | _reported_condition_cache_filtered_rows = filtered_rows; |
1032 | 444 | } |
1033 | 113k | } |
1034 | | |
1035 | | } // namespace doris |