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