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