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