be/src/exec/scan/file_scanner.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.h" |
19 | | |
20 | | #include <fmt/format.h> |
21 | | #include <gen_cpp/Exprs_types.h> |
22 | | #include <gen_cpp/Metrics_types.h> |
23 | | #include <gen_cpp/Opcodes_types.h> |
24 | | #include <gen_cpp/PaloInternalService_types.h> |
25 | | #include <gen_cpp/PlanNodes_types.h> |
26 | | #include <glog/logging.h> |
27 | | |
28 | | #include <algorithm> |
29 | | #include <boost/iterator/iterator_facade.hpp> |
30 | | #include <map> |
31 | | #include <ranges> |
32 | | #include <tuple> |
33 | | #include <unordered_map> |
34 | | #include <utility> |
35 | | |
36 | | #include "common/cast_set.h" |
37 | | #include "common/compiler_util.h" // IWYU pragma: keep |
38 | | #include "common/config.h" |
39 | | #include "common/consts.h" |
40 | | #include "common/logging.h" |
41 | | #include "common/status.h" |
42 | | #include "core/block/column_with_type_and_name.h" |
43 | | #include "core/block/columns_with_type_and_name.h" |
44 | | #include "core/column/column.h" |
45 | | #include "core/column/column_nullable.h" |
46 | | #include "core/column/column_vector.h" |
47 | | #include "core/data_type/data_type.h" |
48 | | #include "core/data_type/data_type_nullable.h" |
49 | | #include "core/data_type/data_type_string.h" |
50 | | #include "core/string_ref.h" |
51 | | #include "exec/common/stringop_substring.h" |
52 | | #include "exec/rowid_fetcher.h" |
53 | | #include "exec/scan/scan_node.h" |
54 | | #include "exprs/aggregate/aggregate_function.h" |
55 | | #include "exprs/function/function.h" |
56 | | #include "exprs/function/simple_function_factory.h" |
57 | | #include "exprs/vexpr.h" |
58 | | #include "exprs/vexpr_context.h" |
59 | | #include "exprs/vexpr_fwd.h" |
60 | | #include "exprs/vslot_ref.h" |
61 | | #include "format/arrow/arrow_stream_reader.h" |
62 | | #include "format/count_reader.h" |
63 | | #include "format/csv/csv_reader.h" |
64 | | #include "format/json/new_json_reader.h" |
65 | | #include "format/native/native_reader.h" |
66 | | #include "format/orc/vorc_reader.h" |
67 | | #include "format/parquet/vparquet_reader.h" |
68 | | #include "format/table/es/es_http_reader.h" |
69 | | #include "format/table/hive_reader.h" |
70 | | #include "format/table/hudi_jni_reader.h" |
71 | | #include "format/table/hudi_reader.h" |
72 | | #include "format/table/iceberg_reader.h" |
73 | | #include "format/table/iceberg_sys_table_jni_reader.h" |
74 | | #include "format/table/jdbc_jni_reader.h" |
75 | | #include "format/table/max_compute_jni_reader.h" |
76 | | #include "format/table/paimon_cpp_reader.h" |
77 | | #include "format/table/paimon_jni_reader.h" |
78 | | #include "format/table/paimon_predicate_converter.h" |
79 | | #include "format/table/paimon_reader.h" |
80 | | #include "format/table/partition_column_filler.h" |
81 | | #include "format/table/remote_doris_reader.h" |
82 | | #include "format/table/transactional_hive_reader.h" |
83 | | #include "format/table/trino_connector_jni_reader.h" |
84 | | #include "format/text/text_reader.h" |
85 | | #ifdef BUILD_RUST_READERS |
86 | | #include "format/lance/lance_rust_reader.h" |
87 | | #endif |
88 | | #include "io/cache/block_file_cache_profile.h" |
89 | | #include "load/group_commit/wal/wal_reader.h" |
90 | | #include "runtime/descriptors.h" |
91 | | #include "runtime/runtime_profile.h" |
92 | | #include "runtime/runtime_state.h" |
93 | | |
94 | | namespace cctz { |
95 | | class time_zone; |
96 | | } // namespace cctz |
97 | | namespace doris { |
98 | | class ShardedKVCache; |
99 | | } // namespace doris |
100 | | |
101 | | namespace doris { |
102 | | using namespace ErrorCode; |
103 | | |
104 | | const std::string FileScanner::FileReadBytesProfile = "FileReadBytes"; |
105 | | const std::string FileScanner::FileReadTimeProfile = "FileReadTime"; |
106 | | |
107 | | FileScanner::FileScanner(RuntimeState* state, FileScanLocalState* local_state, int64_t limit, |
108 | | std::shared_ptr<SplitSourceConnector> split_source, |
109 | | RuntimeProfile* profile, ShardedKVCache* kv_cache, |
110 | | const std::unordered_map<std::string, int>* colname_to_slot_id) |
111 | 2 | : Scanner(state, local_state, limit, profile), |
112 | 2 | _split_source(split_source), |
113 | 2 | _cur_reader(nullptr), |
114 | 2 | _cur_reader_eof(false), |
115 | 2 | _kv_cache(kv_cache), |
116 | 2 | _strict_mode(false), |
117 | 2 | _col_name_to_slot_id(colname_to_slot_id) { |
118 | 2 | if (state->get_query_ctx() != nullptr && |
119 | 2 | state->get_query_ctx()->file_scan_range_params_map.count(local_state->parent_id()) > 0) { |
120 | 0 | _params = &(state->get_query_ctx()->file_scan_range_params_map[local_state->parent_id()]); |
121 | 2 | } else { |
122 | | // old fe thrift protocol |
123 | 2 | _params = _split_source->get_params(); |
124 | 2 | } |
125 | 2 | if (_params->__isset.strict_mode) { |
126 | 0 | _strict_mode = _params->strict_mode; |
127 | 0 | } |
128 | | |
129 | | // For load scanner, there are input and output tuple. |
130 | | // For query scanner, there is only output tuple |
131 | 2 | _input_tuple_desc = state->desc_tbl().get_tuple_descriptor(_params->src_tuple_id); |
132 | 2 | _real_tuple_desc = _input_tuple_desc == nullptr ? _output_tuple_desc : _input_tuple_desc; |
133 | 2 | _is_load = (_input_tuple_desc != nullptr); |
134 | 2 | _configure_file_scan_handlers(); |
135 | 2 | } |
136 | | |
137 | 18 | void FileScanner::_configure_file_scan_handlers() { |
138 | 18 | if (_is_load) { |
139 | 2 | _init_src_block_handler = &FileScanner::_init_src_block_for_load; |
140 | 2 | _process_src_block_after_read_handler = |
141 | 2 | &FileScanner::_process_src_block_after_read_for_load; |
142 | 2 | _should_push_down_predicates_handler = &FileScanner::_should_push_down_predicates_for_load; |
143 | 2 | _should_enable_condition_cache_handler = |
144 | 2 | &FileScanner::_should_enable_condition_cache_for_load; |
145 | 16 | } else { |
146 | 16 | _init_src_block_handler = &FileScanner::_init_src_block_for_query; |
147 | 16 | _process_src_block_after_read_handler = |
148 | 16 | &FileScanner::_process_src_block_after_read_for_query; |
149 | 16 | _should_push_down_predicates_handler = &FileScanner::_should_push_down_predicates_for_query; |
150 | 16 | _should_enable_condition_cache_handler = |
151 | 16 | &FileScanner::_should_enable_condition_cache_for_query; |
152 | 16 | } |
153 | 18 | } |
154 | | |
155 | 2 | Status FileScanner::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) { |
156 | 2 | RETURN_IF_ERROR(Scanner::init(state, conjuncts)); |
157 | 2 | _get_block_timer = |
158 | 2 | ADD_TIMER_WITH_LEVEL(_local_state->scanner_profile(), "FileScannerGetBlockTime", 1); |
159 | 2 | _cast_to_input_block_timer = ADD_TIMER_WITH_LEVEL(_local_state->scanner_profile(), |
160 | 2 | "FileScannerCastInputBlockTime", 1); |
161 | 2 | _fill_missing_columns_timer = ADD_TIMER_WITH_LEVEL(_local_state->scanner_profile(), |
162 | 2 | "FileScannerFillMissingColumnTime", 1); |
163 | 2 | _pre_filter_timer = |
164 | 2 | ADD_TIMER_WITH_LEVEL(_local_state->scanner_profile(), "FileScannerPreFilterTimer", 1); |
165 | 2 | _convert_to_output_block_timer = ADD_TIMER_WITH_LEVEL(_local_state->scanner_profile(), |
166 | 2 | "FileScannerConvertOuputBlockTime", 1); |
167 | 2 | _runtime_filter_partition_prune_timer = ADD_TIMER_WITH_LEVEL( |
168 | 2 | _local_state->scanner_profile(), "FileScannerRuntimeFilterPartitionPruningTime", 1); |
169 | 2 | _empty_file_counter = |
170 | 2 | ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), "EmptyFileNum", TUnit::UNIT, 1); |
171 | 2 | _not_found_file_counter = ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), |
172 | 2 | "NotFoundFileNum", TUnit::UNIT, 1); |
173 | 2 | _fully_skipped_file_counter = ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), |
174 | 2 | "FullySkippedFileNum", TUnit::UNIT, 1); |
175 | 2 | _file_counter = |
176 | 2 | ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), "FileNumber", TUnit::UNIT, 1); |
177 | | |
178 | 2 | _file_read_bytes_counter = ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), |
179 | 2 | FileReadBytesProfile, TUnit::BYTES, 1); |
180 | 2 | _file_read_calls_counter = ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), |
181 | 2 | "FileReadCalls", TUnit::UNIT, 1); |
182 | 2 | _file_read_time_counter = |
183 | 2 | ADD_TIMER_WITH_LEVEL(_local_state->scanner_profile(), FileReadTimeProfile, 1); |
184 | | |
185 | 2 | _runtime_filter_partition_pruned_range_counter = |
186 | 2 | ADD_COUNTER_WITH_LEVEL(_local_state->scanner_profile(), |
187 | 2 | "RuntimeFilterPartitionPrunedRangeNum", TUnit::UNIT, 1); |
188 | | // Keep the current file's adaptive state while also preserving the peak value across all |
189 | | // files handled by this scanner instance. |
190 | 2 | _adaptive_batch_predicted_rows_counter = |
191 | 2 | _local_state->scanner_profile()->AddHighWaterMarkCounter( |
192 | 2 | "AdaptiveBatchPredictedRows", TUnit::UNIT, RuntimeProfile::ROOT_COUNTER, 1); |
193 | 2 | _adaptive_batch_actual_bytes_before_truncate_counter = |
194 | 2 | _local_state->scanner_profile()->AddHighWaterMarkCounter( |
195 | 2 | "AdaptiveBatchActualBytesBeforeTruncate", TUnit::BYTES, |
196 | 2 | RuntimeProfile::ROOT_COUNTER, 1); |
197 | 2 | _adaptive_batch_actual_bytes_after_truncate_counter = |
198 | 2 | _local_state->scanner_profile()->AddHighWaterMarkCounter( |
199 | 2 | "AdaptiveBatchActualBytesAfterTruncate", TUnit::BYTES, |
200 | 2 | RuntimeProfile::ROOT_COUNTER, 1); |
201 | 2 | _adaptive_batch_probe_count_counter = ADD_COUNTER_WITH_LEVEL( |
202 | 2 | _local_state->scanner_profile(), "AdaptiveBatchProbeCount", TUnit::UNIT, 1); |
203 | | |
204 | 2 | _file_cache_statistics.reset(new io::FileCacheStatistics()); |
205 | 2 | _file_reader_stats.reset(new io::FileReaderStats()); |
206 | | |
207 | 2 | RETURN_IF_ERROR(_init_io_ctx()); |
208 | 2 | _io_ctx->file_cache_stats = _file_cache_statistics.get(); |
209 | 2 | _io_ctx->file_reader_stats = _file_reader_stats.get(); |
210 | 2 | _io_ctx->is_disposable = _state->query_options().disable_file_cache; |
211 | | |
212 | 2 | if (_is_load) { |
213 | 0 | _src_row_desc.reset(new RowDescriptor(_state->desc_tbl(), |
214 | 0 | std::vector<TupleId>({_input_tuple_desc->id()}))); |
215 | | // prepare pre filters |
216 | 0 | if (_params->__isset.pre_filter_exprs_list) { |
217 | 0 | RETURN_IF_ERROR(doris::VExpr::create_expr_trees(_params->pre_filter_exprs_list, |
218 | 0 | _pre_conjunct_ctxs)); |
219 | 0 | } else if (_params->__isset.pre_filter_exprs) { |
220 | 0 | VExprContextSPtr context; |
221 | 0 | RETURN_IF_ERROR(doris::VExpr::create_expr_tree(_params->pre_filter_exprs, context)); |
222 | 0 | _pre_conjunct_ctxs.emplace_back(context); |
223 | 0 | } |
224 | | |
225 | 0 | for (auto& conjunct : _pre_conjunct_ctxs) { |
226 | 0 | RETURN_IF_ERROR(conjunct->prepare(_state, *_src_row_desc)); |
227 | 0 | RETURN_IF_ERROR(conjunct->open(_state)); |
228 | 0 | } |
229 | | |
230 | 0 | _dest_row_desc.reset(new RowDescriptor(_state->desc_tbl(), |
231 | 0 | std::vector<TupleId>({_output_tuple_desc->id()}))); |
232 | 0 | } |
233 | | |
234 | 2 | _default_val_row_desc.reset( |
235 | 2 | new RowDescriptor(_state->desc_tbl(), std::vector<TupleId>({_real_tuple_desc->id()}))); |
236 | | |
237 | 2 | return Status::OK(); |
238 | 2 | } |
239 | | |
240 | 0 | bool FileScanner::_should_enable_adaptive_batch_size(TFileFormatType::type format_type) const { |
241 | | // Only enable for readers that support set_batch_size(). |
242 | | // Table-format wrappers are covered because they delegate to native readers. |
243 | 0 | if (!config::enable_adaptive_batch_size) { |
244 | 0 | return false; |
245 | 0 | } |
246 | 0 | switch (format_type) { |
247 | 0 | case TFileFormatType::FORMAT_PARQUET: |
248 | 0 | case TFileFormatType::FORMAT_ORC: |
249 | 0 | case TFileFormatType::FORMAT_CSV_PLAIN: |
250 | 0 | case TFileFormatType::FORMAT_CSV_GZ: |
251 | 0 | case TFileFormatType::FORMAT_CSV_BZ2: |
252 | 0 | case TFileFormatType::FORMAT_CSV_LZ4FRAME: |
253 | 0 | case TFileFormatType::FORMAT_CSV_LZ4BLOCK: |
254 | 0 | case TFileFormatType::FORMAT_CSV_LZOP: |
255 | 0 | case TFileFormatType::FORMAT_CSV_DEFLATE: |
256 | 0 | case TFileFormatType::FORMAT_CSV_SNAPPYBLOCK: |
257 | 0 | case TFileFormatType::FORMAT_PROTO: |
258 | 0 | case TFileFormatType::FORMAT_TEXT: |
259 | 0 | case TFileFormatType::FORMAT_JSON: |
260 | 0 | case TFileFormatType::FORMAT_JNI: |
261 | 0 | return true; |
262 | 0 | default: |
263 | 0 | return false; |
264 | 0 | } |
265 | 0 | } |
266 | | |
267 | 86 | bool FileScanner::_should_run_adaptive_batch_size() const { |
268 | | // Skip adaptive batch sizing for pushed-down COUNT(*): the reader is wrapped by CountReader |
269 | | // and only emits a single aggregated row count instead of materializing real columns, so |
270 | | // there is no per-row byte cost to learn from and no benefit in tuning the batch size. |
271 | 86 | return _block_size_predictor != nullptr && _get_push_down_agg_type() != TPushAggOp::type::COUNT; |
272 | 86 | } |
273 | | |
274 | 1 | void FileScanner::_reset_adaptive_batch_size_state() { |
275 | 1 | _block_size_predictor.reset(); |
276 | 1 | COUNTER_SET(_adaptive_batch_predicted_rows_counter, int64_t(0)); |
277 | 1 | COUNTER_SET(_adaptive_batch_actual_bytes_before_truncate_counter, int64_t(0)); |
278 | 1 | COUNTER_SET(_adaptive_batch_actual_bytes_after_truncate_counter, int64_t(0)); |
279 | 1 | } |
280 | | |
281 | 0 | void FileScanner::_init_adaptive_batch_size_state(TFileFormatType::type format_type) { |
282 | 0 | _reset_adaptive_batch_size_state(); |
283 | 0 | if (!_should_enable_adaptive_batch_size(format_type)) { |
284 | 0 | return; |
285 | 0 | } |
286 | | |
287 | | // External file readers do not provide reliable memory-size metadata hints. Use a small probe |
288 | | // batch so the predictor can learn from real FileScanner output quickly. |
289 | 0 | _block_size_predictor = std::make_unique<AdaptiveBlockSizePredictor>( |
290 | 0 | _state->preferred_block_size_bytes(), 0.0, ADAPTIVE_BATCH_INITIAL_PROBE_ROWS, |
291 | 0 | _state->batch_size()); |
292 | 0 | } |
293 | | |
294 | 0 | size_t FileScanner::_predict_reader_batch_rows() { |
295 | 0 | DCHECK(_block_size_predictor != nullptr); |
296 | 0 | size_t predicted_rows = _block_size_predictor->predict_next_rows(); |
297 | 0 | COUNTER_SET(_adaptive_batch_predicted_rows_counter, static_cast<int64_t>(predicted_rows)); |
298 | 0 | return predicted_rows; |
299 | 0 | } |
300 | | |
301 | 25 | void FileScanner::_update_adaptive_batch_size_before_truncate(const Block& block) { |
302 | 25 | if (!_should_run_adaptive_batch_size()) { |
303 | 25 | return; |
304 | 25 | } |
305 | | |
306 | | // Learn from the logical bytes before CHAR/VARCHAR truncation. The truncated block can be |
307 | | // much smaller than the data the reader and FileScanner have already materialized. |
308 | 0 | COUNTER_SET(_adaptive_batch_actual_bytes_before_truncate_counter, |
309 | 0 | static_cast<int64_t>(block.bytes())); |
310 | 0 | if (block.rows() == 0) { |
311 | 0 | return; |
312 | 0 | } |
313 | | |
314 | | // Count a probe only when we actually obtain the first non-empty sample that seeds history. |
315 | 0 | if (!_block_size_predictor->has_history()) { |
316 | 0 | COUNTER_UPDATE(_adaptive_batch_probe_count_counter, 1); |
317 | 0 | } |
318 | 0 | _block_size_predictor->update(block); |
319 | 0 | } |
320 | | |
321 | 25 | void FileScanner::_update_adaptive_batch_size_after_truncate(const Block& block) { |
322 | 25 | if (!_should_run_adaptive_batch_size()) { |
323 | 25 | return; |
324 | 25 | } |
325 | | |
326 | | // Keep the post-truncate size only for observability. It should not affect the next batch |
327 | | // because truncation happens after the upstream memory cost has already been paid. |
328 | 0 | COUNTER_SET(_adaptive_batch_actual_bytes_after_truncate_counter, |
329 | 0 | static_cast<int64_t>(block.bytes())); |
330 | 0 | } |
331 | | |
332 | | // check if the expr is a partition pruning expr |
333 | 0 | bool FileScanner::_check_partition_prune_expr(const VExprSPtr& expr) { |
334 | 0 | if (expr->is_slot_ref()) { |
335 | 0 | auto* slot_ref = static_cast<VSlotRef*>(expr.get()); |
336 | 0 | return _partition_slot_index_map.find(slot_ref->slot_id()) != |
337 | 0 | _partition_slot_index_map.end(); |
338 | 0 | } |
339 | 0 | if (expr->is_literal()) { |
340 | 0 | return true; |
341 | 0 | } |
342 | 0 | return std::ranges::all_of(expr->children(), [this](const auto& child) { |
343 | 0 | return _check_partition_prune_expr(child); |
344 | 0 | }); |
345 | 0 | } |
346 | | |
347 | 0 | bool FileScanner::_contains_runtime_filter(const VExprContextSPtrs& conjuncts) const { |
348 | 0 | return std::ranges::any_of( |
349 | 0 | conjuncts, [](const auto& conjunct) { return conjunct->root()->is_rf_wrapper(); }); |
350 | 0 | } |
351 | | |
352 | 0 | void FileScanner::_init_runtime_filter_partition_prune_ctxs() { |
353 | 0 | _runtime_filter_partition_prune_ctxs.clear(); |
354 | 0 | for (auto& conjunct : _conjuncts) { |
355 | 0 | auto impl = conjunct->root()->get_impl(); |
356 | | // If impl is not null, which means this a conjuncts from runtime filter. |
357 | 0 | auto expr = impl ? impl : conjunct->root(); |
358 | 0 | if (_check_partition_prune_expr(expr)) { |
359 | 0 | _runtime_filter_partition_prune_ctxs.emplace_back(conjunct); |
360 | 0 | } |
361 | 0 | } |
362 | 0 | } |
363 | | |
364 | 0 | void FileScanner::_init_runtime_filter_partition_prune_block() { |
365 | | // init block with empty column |
366 | 0 | for (auto const* slot_desc : _real_tuple_desc->slots()) { |
367 | 0 | _runtime_filter_partition_prune_block.insert( |
368 | 0 | ColumnWithTypeAndName(slot_desc->get_empty_mutable_column(), |
369 | 0 | slot_desc->get_data_type_ptr(), slot_desc->col_name())); |
370 | 0 | } |
371 | 0 | } |
372 | | |
373 | 0 | Status FileScanner::_process_runtime_filters_partition_prune(bool& can_filter_all) { |
374 | 0 | SCOPED_TIMER(_runtime_filter_partition_prune_timer); |
375 | 0 | if (_runtime_filter_partition_prune_ctxs.empty() || _partition_col_descs.empty()) { |
376 | 0 | return Status::OK(); |
377 | 0 | } |
378 | 0 | size_t partition_value_column_size = 1; |
379 | | |
380 | | // 1. Get partition key values to string columns. |
381 | 0 | std::unordered_map<SlotId, MutableColumnPtr> partition_slot_id_to_column; |
382 | 0 | for (auto const& partition_col_desc : _partition_col_descs) { |
383 | 0 | const auto& [partition_value, partition_slot_desc] = partition_col_desc.second; |
384 | 0 | auto data_type = partition_slot_desc->get_data_type_ptr(); |
385 | 0 | auto partition_value_column = data_type->create_column(); |
386 | 0 | auto null_it = _partition_value_is_null.find(partition_slot_desc->col_name()); |
387 | 0 | DORIS_CHECK(null_it != _partition_value_is_null.end()); |
388 | 0 | RETURN_IF_ERROR(fill_partition_column_from_path_value( |
389 | 0 | *partition_value_column, *partition_slot_desc, partition_value, |
390 | 0 | partition_value_column_size, null_it->second)); |
391 | | |
392 | 0 | partition_slot_id_to_column[partition_slot_desc->id()] = std::move(partition_value_column); |
393 | 0 | } |
394 | | |
395 | | // 2. Fill _runtime_filter_partition_prune_block from the partition column, then execute conjuncts and filter block. |
396 | | // 2.1 Fill _runtime_filter_partition_prune_block from the partition column to match the conjuncts executing. |
397 | 0 | size_t index = 0; |
398 | 0 | bool first_column_filled = false; |
399 | 0 | for (auto const* slot_desc : _real_tuple_desc->slots()) { |
400 | 0 | if (partition_slot_id_to_column.find(slot_desc->id()) != |
401 | 0 | partition_slot_id_to_column.end()) { |
402 | 0 | auto partition_value_column = std::move(partition_slot_id_to_column[slot_desc->id()]); |
403 | 0 | _runtime_filter_partition_prune_block.replace_by_position( |
404 | 0 | index, std::move(partition_value_column)); |
405 | 0 | if (index == 0) { |
406 | 0 | first_column_filled = true; |
407 | 0 | } |
408 | 0 | } |
409 | 0 | index++; |
410 | 0 | } |
411 | | |
412 | | // 2.2 Execute conjuncts. |
413 | 0 | if (!first_column_filled) { |
414 | | // VExprContext.execute has an optimization, the filtering is executed when block->rows() > 0 |
415 | | // The following process may be tricky and time-consuming, but we have no other way. |
416 | 0 | auto column = IColumn::mutate( |
417 | 0 | std::move(_runtime_filter_partition_prune_block.get_by_position(0).column)); |
418 | 0 | column->resize(partition_value_column_size); |
419 | 0 | _runtime_filter_partition_prune_block.replace_by_position(0, std::move(column)); |
420 | 0 | } |
421 | 0 | IColumn::Filter result_filter(_runtime_filter_partition_prune_block.rows(), 1); |
422 | 0 | RETURN_IF_ERROR(VExprContext::execute_conjuncts(_runtime_filter_partition_prune_ctxs, nullptr, |
423 | 0 | &_runtime_filter_partition_prune_block, |
424 | 0 | &result_filter, &can_filter_all)); |
425 | 0 | return Status::OK(); |
426 | 0 | } |
427 | | |
428 | 1 | Status FileScanner::_process_conjuncts() { |
429 | 1 | _slot_id_to_filter_conjuncts.clear(); |
430 | 1 | _not_single_slot_filter_conjuncts.clear(); |
431 | 1 | for (auto& conjunct : _push_down_conjuncts) { |
432 | 1 | auto impl = conjunct->root()->get_impl(); |
433 | | // If impl is not null, which means this a conjuncts from runtime filter. |
434 | 1 | auto cur_expr = impl ? impl : conjunct->root(); |
435 | | |
436 | 1 | std::vector<int> slot_ids; |
437 | 1 | _get_slot_ids(cur_expr.get(), &slot_ids); |
438 | 1 | if (slot_ids.empty()) { |
439 | 1 | _not_single_slot_filter_conjuncts.emplace_back(conjunct); |
440 | 1 | continue; |
441 | 1 | } |
442 | 0 | bool single_slot = true; |
443 | 0 | for (int i = 1; i < slot_ids.size(); i++) { |
444 | 0 | if (slot_ids[i] != slot_ids[0]) { |
445 | 0 | single_slot = false; |
446 | 0 | break; |
447 | 0 | } |
448 | 0 | } |
449 | 0 | if (single_slot) { |
450 | 0 | SlotId slot_id = slot_ids[0]; |
451 | 0 | _slot_id_to_filter_conjuncts[slot_id].emplace_back(conjunct); |
452 | 0 | } else { |
453 | 0 | _not_single_slot_filter_conjuncts.emplace_back(conjunct); |
454 | 0 | } |
455 | 0 | } |
456 | 1 | return Status::OK(); |
457 | 1 | } |
458 | | |
459 | 1 | Status FileScanner::_process_late_arrival_conjuncts() { |
460 | 1 | if (_push_down_conjuncts.size() < _conjuncts.size()) { |
461 | 1 | _push_down_conjuncts = _conjuncts; |
462 | | // Do not clear _conjuncts here! |
463 | | // We must keep it for fallback filtering, especially when mixing |
464 | | // Native readers (which use _push_down_conjuncts) and JNI readers (which rely on _conjuncts). |
465 | | // _conjuncts.clear(); |
466 | 1 | RETURN_IF_ERROR(_process_conjuncts()); |
467 | 1 | } |
468 | 1 | if (_applied_rf_num == _total_rf_num) { |
469 | 1 | _local_state->scanner_profile()->add_info_string("ApplyAllRuntimeFilters", "True"); |
470 | 1 | } |
471 | 1 | return Status::OK(); |
472 | 1 | } |
473 | | |
474 | 1 | void FileScanner::_get_slot_ids(VExpr* expr, std::vector<int>* slot_ids) { |
475 | 1 | for (auto& child_expr : expr->children()) { |
476 | 0 | if (child_expr->is_slot_ref()) { |
477 | 0 | VSlotRef* slot_ref = reinterpret_cast<VSlotRef*>(child_expr.get()); |
478 | 0 | SlotDescriptor* slot_desc = _state->desc_tbl().get_slot_descriptor(slot_ref->slot_id()); |
479 | 0 | slot_desc->set_is_predicate(true); |
480 | 0 | slot_ids->emplace_back(slot_ref->slot_id()); |
481 | 0 | } else { |
482 | 0 | _get_slot_ids(child_expr.get(), slot_ids); |
483 | 0 | } |
484 | 0 | } |
485 | 1 | } |
486 | | |
487 | 0 | Status FileScanner::_open_impl(RuntimeState* state) { |
488 | 0 | RETURN_IF_CANCELLED(state); |
489 | 0 | RETURN_IF_ERROR(Scanner::_open_impl(state)); |
490 | 0 | if (_local_state) { |
491 | 0 | _condition_cache_digest = _local_state->get_condition_cache_digest(); |
492 | 0 | } |
493 | 0 | RETURN_IF_ERROR(_split_source->get_next(&_first_scan_range, &_current_range)); |
494 | 0 | if (_first_scan_range) { |
495 | 0 | RETURN_IF_ERROR(_init_expr_ctxes()); |
496 | 0 | if (_state->query_options().enable_runtime_filter_partition_prune && |
497 | 0 | !_partition_slot_index_map.empty()) { |
498 | 0 | _init_runtime_filter_partition_prune_ctxs(); |
499 | 0 | _init_runtime_filter_partition_prune_block(); |
500 | 0 | } |
501 | 0 | } else { |
502 | | // there's no scan range in split source. stop scanner directly. |
503 | 0 | _scanner_eof = true; |
504 | 0 | } |
505 | | |
506 | 0 | return Status::OK(); |
507 | 0 | } |
508 | | |
509 | 37 | Status FileScanner::_get_block_impl(RuntimeState* state, Block* block, bool* eof) { |
510 | 37 | Status st = _get_block_wrapped(state, block, eof); |
511 | | |
512 | 37 | if (!st.ok()) { |
513 | | // add cur path in error msg for easy debugging |
514 | 1 | return std::move(st.append(". cur path: " + get_current_scan_range_name())); |
515 | 1 | } |
516 | 36 | return st; |
517 | 37 | } |
518 | | |
519 | | // For query: |
520 | | // [exist cols] [non-exist cols] [col from path] input output |
521 | | // A B C D E |
522 | | // _init_src_block x x x x x - x |
523 | | // get_next_block x x x - - - x |
524 | | // _cast_to_input_block - - - - - - - |
525 | | // _fill_columns_from_path - - - - x - x |
526 | | // _fill_missing_columns - - - x - - x |
527 | | // _convert_to_output_block - - - - - - - |
528 | | // |
529 | | // For load: |
530 | | // [exist cols] [non-exist cols] [col from path] input output |
531 | | // A B C D E |
532 | | // _init_src_block x x x x x x - |
533 | | // get_next_block x x x - - x - |
534 | | // _cast_to_input_block x x x - - x - |
535 | | // _fill_columns_from_path - - - - x x - |
536 | | // _fill_missing_columns - - - x - x - |
537 | | // _convert_to_output_block - - - - - - x |
538 | 37 | Status FileScanner::_get_block_wrapped(RuntimeState* state, Block* block, bool* eof) { |
539 | 37 | do { |
540 | 37 | RETURN_IF_CANCELLED(state); |
541 | 37 | if (_cur_reader == nullptr || _cur_reader_eof) { |
542 | 1 | _finalize_reader_condition_cache(); |
543 | | // The file may not exist because the file list is got from meta cache, |
544 | | // And the file may already be removed from storage. |
545 | | // Just ignore not found files. |
546 | 1 | Status st = _get_next_reader(); |
547 | 1 | if (st.is<ErrorCode::NOT_FOUND>() && config::ignore_not_found_file_in_external_table) { |
548 | 0 | _cur_reader_eof = true; |
549 | 0 | COUNTER_UPDATE(_not_found_file_counter, 1); |
550 | 0 | continue; |
551 | 1 | } else if (st.is<ErrorCode::END_OF_FILE>()) { |
552 | 0 | _cur_reader_eof = true; |
553 | 0 | COUNTER_UPDATE(_fully_skipped_file_counter, 1); |
554 | 0 | continue; |
555 | 1 | } else if (!st) { |
556 | 1 | return st; |
557 | 1 | } |
558 | 0 | _init_reader_condition_cache(); |
559 | 0 | } |
560 | | |
561 | 36 | if (_scanner_eof) { |
562 | 0 | *eof = true; |
563 | 0 | return Status::OK(); |
564 | 0 | } |
565 | | |
566 | | // Init src block for load job based on the data file schema (e.g. parquet) |
567 | | // For query job, simply set _src_block_ptr to block. |
568 | 36 | size_t read_rows = 0; |
569 | 36 | RETURN_IF_ERROR(_init_src_block(block)); |
570 | | |
571 | 36 | if (_should_run_adaptive_batch_size()) { |
572 | 0 | _cur_reader->set_batch_size(_predict_reader_batch_rows()); |
573 | 0 | } |
574 | 36 | { |
575 | 36 | SCOPED_TIMER(_get_block_timer); |
576 | | |
577 | | // Read next block. |
578 | | // Some of column in block may not be filled (column not exist in file) |
579 | 36 | RETURN_IF_ERROR( |
580 | 36 | _cur_reader->get_next_block(_src_block_ptr, &read_rows, &_cur_reader_eof)); |
581 | 36 | } |
582 | | // use read_rows instead of _src_block_ptr->rows(), because the first column of _src_block_ptr |
583 | | // may not be filled after calling `get_next_block()`, so _src_block_ptr->rows() may return wrong result. |
584 | 36 | if (read_rows > 0) { |
585 | 25 | if ((!_cur_reader->count_read_rows()) && _io_ctx) { |
586 | 0 | _io_ctx->file_reader_stats->read_rows += read_rows; |
587 | 0 | } |
588 | | // If the push_down_agg_type is COUNT, no need to do the rest, |
589 | | // because we only save a number in block. |
590 | 25 | if (_get_push_down_agg_type() != TPushAggOp::type::COUNT) { |
591 | 25 | RETURN_IF_ERROR(_process_src_block_after_read(block)); |
592 | 25 | } |
593 | 25 | } |
594 | 36 | break; |
595 | 36 | } while (true); |
596 | | |
597 | | // Update filtered rows and unselected rows for load, reset counter. |
598 | | // { |
599 | | // state->update_num_rows_load_filtered(_counter.num_rows_filtered); |
600 | | // state->update_num_rows_load_unselected(_counter.num_rows_unselected); |
601 | | // _reset_counter(); |
602 | | // } |
603 | 36 | return Status::OK(); |
604 | 37 | } |
605 | | |
606 | | /** |
607 | | * Check whether there are complex types in parquet/orc reader in broker/stream load. |
608 | | * Broker/stream load will cast any type as string type, and complex types will be casted wrong. |
609 | | * This is a temporary method, and will be replaced by tvf. |
610 | | */ |
611 | 0 | Status FileScanner::_check_output_block_types() { |
612 | | // Only called from _init_src_block_for_load, so _is_load is always true. |
613 | 0 | TFileFormatType::type format_type = _params->format_type; |
614 | 0 | if (format_type == TFileFormatType::FORMAT_PARQUET || |
615 | 0 | format_type == TFileFormatType::FORMAT_ORC) { |
616 | 0 | for (auto slot : _output_tuple_desc->slots()) { |
617 | 0 | if (is_complex_type(slot->type()->get_primitive_type())) { |
618 | 0 | return Status::InternalError( |
619 | 0 | "Parquet/orc doesn't support complex types in broker/stream load, " |
620 | 0 | "please use tvf(table value function) to insert complex types."); |
621 | 0 | } |
622 | 0 | } |
623 | 0 | } |
624 | 0 | return Status::OK(); |
625 | 0 | } |
626 | | |
627 | 36 | Status FileScanner::_init_src_block(Block* block) { |
628 | 36 | DCHECK(_init_src_block_handler != nullptr); |
629 | 36 | return (this->*_init_src_block_handler)(block); |
630 | 36 | } |
631 | | |
632 | 36 | Status FileScanner::_init_src_block_for_query(Block* block) { |
633 | 36 | _src_block_ptr = block; |
634 | | |
635 | | // Build name to index map only once on first call. |
636 | 36 | if (_src_block_name_to_idx.empty()) { |
637 | 16 | _src_block_name_to_idx = block->get_name_to_pos_map(); |
638 | 16 | } |
639 | 36 | return Status::OK(); |
640 | 36 | } |
641 | | |
642 | 0 | Status FileScanner::_init_src_block_for_load(Block* block) { |
643 | 0 | static_cast<void>(block); |
644 | 0 | RETURN_IF_ERROR(_check_output_block_types()); |
645 | | |
646 | | // if (_src_block_init) { |
647 | | // _src_block.clear_column_data(); |
648 | | // _src_block_ptr = &_src_block; |
649 | | // return Status::OK(); |
650 | | // } |
651 | | |
652 | 0 | _src_block.clear(); |
653 | 0 | uint32_t idx = 0; |
654 | | // slots in _input_tuple_desc contains all slots describe in load statement, eg: |
655 | | // -H "columns: k1, k2, tmp1, k3 = tmp1 + 1" |
656 | | // _input_tuple_desc will contains: k1, k2, tmp1 |
657 | | // and some of them are from file, such as k1 and k2, and some of them may not exist in file, such as tmp1 |
658 | | // _input_tuple_desc also contains columns from path |
659 | 0 | for (auto& slot : _input_tuple_desc->slots()) { |
660 | 0 | DataTypePtr data_type; |
661 | 0 | auto it = _slot_lower_name_to_col_type.find(slot->col_name()); |
662 | 0 | if (slot->is_skip_bitmap_col()) { |
663 | 0 | _skip_bitmap_col_idx = idx; |
664 | 0 | } |
665 | 0 | if (_params->__isset.sequence_map_col) { |
666 | 0 | if (_params->sequence_map_col == slot->col_name()) { |
667 | 0 | _sequence_map_col_uid = slot->col_unique_id(); |
668 | 0 | } |
669 | 0 | } |
670 | 0 | data_type = |
671 | 0 | it == _slot_lower_name_to_col_type.end() ? slot->type() : make_nullable(it->second); |
672 | 0 | MutableColumnPtr data_column = data_type->create_column(); |
673 | 0 | _src_block.insert( |
674 | 0 | ColumnWithTypeAndName(std::move(data_column), data_type, slot->col_name())); |
675 | 0 | _src_block_name_to_idx.emplace(slot->col_name(), idx++); |
676 | 0 | } |
677 | 0 | if (_params->__isset.sequence_map_col) { |
678 | 0 | for (const auto& slot : _output_tuple_desc->slots()) { |
679 | | // When the target table has seqeunce map column, _input_tuple_desc will not contains __DORIS_SEQUENCE_COL__, |
680 | | // so we should get its column unique id from _output_tuple_desc |
681 | 0 | if (slot->is_sequence_col()) { |
682 | 0 | _sequence_col_uid = slot->col_unique_id(); |
683 | 0 | } |
684 | 0 | } |
685 | 0 | } |
686 | 0 | _src_block_ptr = &_src_block; |
687 | 0 | _src_block_init = true; |
688 | 0 | return Status::OK(); |
689 | 0 | } |
690 | | |
691 | 0 | Status FileScanner::_cast_to_input_block(Block* block) { |
692 | | // Only called from _process_src_block_after_read_for_load, so _is_load is always true. |
693 | 0 | SCOPED_TIMER(_cast_to_input_block_timer); |
694 | | // cast primitive type(PT0) to primitive type(PT1) |
695 | 0 | uint32_t idx = 0; |
696 | 0 | for (auto& slot_desc : _input_tuple_desc->slots()) { |
697 | 0 | if (_slot_lower_name_to_col_type.find(slot_desc->col_name()) == |
698 | 0 | _slot_lower_name_to_col_type.end()) { |
699 | | // skip columns which does not exist in file |
700 | 0 | continue; |
701 | 0 | } |
702 | 0 | auto& arg = _src_block_ptr->get_by_position(_src_block_name_to_idx[slot_desc->col_name()]); |
703 | 0 | auto return_type = slot_desc->get_data_type_ptr(); |
704 | | // remove nullable here, let the get_function decide whether nullable |
705 | 0 | auto data_type = get_data_type_with_default_argument(remove_nullable(return_type)); |
706 | 0 | ColumnsWithTypeAndName arguments { |
707 | 0 | arg, {data_type->create_column(), data_type, slot_desc->col_name()}}; |
708 | 0 | auto func_cast = |
709 | 0 | SimpleFunctionFactory::instance().get_function("CAST", arguments, return_type, {}); |
710 | 0 | if (!func_cast) { |
711 | 0 | return Status::InternalError("Function CAST[arg={}, col name={}, return={}] not found!", |
712 | 0 | arg.type->get_name(), slot_desc->col_name(), |
713 | 0 | return_type->get_name()); |
714 | 0 | } |
715 | 0 | idx = _src_block_name_to_idx[slot_desc->col_name()]; |
716 | 0 | DCHECK(_state != nullptr); |
717 | 0 | auto ctx = FunctionContext::create_context(_state, {}, {}); |
718 | 0 | RETURN_IF_ERROR( |
719 | 0 | func_cast->execute(ctx.get(), *_src_block_ptr, {idx}, idx, arg.column->size())); |
720 | 0 | _src_block_ptr->get_by_position(idx).type = std::move(return_type); |
721 | 0 | } |
722 | 0 | return Status::OK(); |
723 | 0 | } |
724 | | |
725 | 0 | Status FileScanner::_pre_filter_src_block() { |
726 | | // Only called from _process_src_block_after_read_for_load, so _is_load is always true. |
727 | 0 | if (!_pre_conjunct_ctxs.empty()) { |
728 | 0 | SCOPED_TIMER(_pre_filter_timer); |
729 | 0 | auto origin_column_num = _src_block_ptr->columns(); |
730 | 0 | auto old_rows = _src_block_ptr->rows(); |
731 | 0 | RETURN_IF_ERROR( |
732 | 0 | VExprContext::filter_block(_pre_conjunct_ctxs, _src_block_ptr, origin_column_num)); |
733 | 0 | _counter.num_rows_unselected += old_rows - _src_block_ptr->rows(); |
734 | 0 | } |
735 | 0 | return Status::OK(); |
736 | 0 | } |
737 | | |
738 | 0 | Status FileScanner::_convert_to_output_block(Block* block) { |
739 | | // Only called from _process_src_block_after_read_for_load, so _is_load is always true. |
740 | 0 | SCOPED_TIMER(_convert_to_output_block_timer); |
741 | | // The block is passed from scanner context's free blocks, |
742 | | // which is initialized by output columns |
743 | | // so no need to clear it |
744 | | // block->clear(); |
745 | |
|
746 | 0 | int ctx_idx = 0; |
747 | 0 | size_t rows = _src_block_ptr->rows(); |
748 | 0 | auto filter_column = ColumnUInt8::create(rows, 1); |
749 | 0 | auto& filter_map = filter_column->get_data(); |
750 | | |
751 | | // After convert, the column_ptr should be copied into output block. |
752 | | // Can not use block->insert() because it may cause use_count() non-zero bug |
753 | 0 | auto scoped_mutable_output_block = |
754 | 0 | VectorizedUtils::build_scoped_mutable_mem_reuse_block(block, *_dest_row_desc); |
755 | 0 | auto& mutable_output_block = scoped_mutable_output_block.mutable_block(); |
756 | 0 | auto& mutable_output_columns = mutable_output_block.mutable_columns(); |
757 | |
|
758 | 0 | std::vector<BitmapValue>* skip_bitmaps {nullptr}; |
759 | 0 | MutableColumnPtr skip_bitmap_column; |
760 | 0 | if (_should_process_skip_bitmap_col()) { |
761 | 0 | skip_bitmap_column = IColumn::mutate( |
762 | 0 | std::move(_src_block_ptr->get_by_position(_skip_bitmap_col_idx).column)); |
763 | 0 | auto* skip_bitmap_nullable_col_ptr = assert_cast<ColumnNullable*>(skip_bitmap_column.get()); |
764 | 0 | skip_bitmaps = &(assert_cast<ColumnBitmap*>( |
765 | 0 | skip_bitmap_nullable_col_ptr->get_nested_column_ptr().get()) |
766 | 0 | ->get_data()); |
767 | | // NOTE: |
768 | | // - If the table has sequence type column, __DORIS_SEQUENCE_COL__ will be put in _input_tuple_desc, so whether |
769 | | // __DORIS_SEQUENCE_COL__ will be marked in skip bitmap depends on whether it's specified in that row |
770 | | // - If the table has sequence map column, __DORIS_SEQUENCE_COL__ will not be put in _input_tuple_desc, |
771 | | // so __DORIS_SEQUENCE_COL__ will be ommited if it't specified in a row and will not be marked in skip bitmap. |
772 | | // So we should mark __DORIS_SEQUENCE_COL__ in skip bitmap here if the corresponding sequence map column us marked |
773 | 0 | if (_sequence_map_col_uid != -1) { |
774 | 0 | for (int j = 0; j < rows; ++j) { |
775 | 0 | if ((*skip_bitmaps)[j].contains(_sequence_map_col_uid)) { |
776 | 0 | (*skip_bitmaps)[j].add(_sequence_col_uid); |
777 | 0 | } |
778 | 0 | } |
779 | 0 | } |
780 | 0 | _src_block_ptr->replace_by_position(_skip_bitmap_col_idx, std::move(skip_bitmap_column)); |
781 | 0 | } |
782 | | |
783 | | // for (auto slot_desc : _output_tuple_desc->slots()) { |
784 | 0 | for (int j = 0; j < mutable_output_columns.size(); ++j) { |
785 | 0 | auto* slot_desc = _output_tuple_desc->slots()[j]; |
786 | 0 | int dest_index = ctx_idx; |
787 | 0 | ColumnPtr column_ptr; |
788 | |
|
789 | 0 | auto& ctx = _dest_vexpr_ctx[dest_index]; |
790 | | // PT1 => dest primitive type |
791 | 0 | RETURN_IF_ERROR(ctx->execute(_src_block_ptr, column_ptr)); |
792 | | // column_ptr maybe a ColumnConst, convert it to a normal column |
793 | 0 | column_ptr = column_ptr->convert_to_full_column_if_const(); |
794 | 0 | DCHECK(column_ptr); |
795 | | |
796 | | // because of src_slot_desc is always be nullable, so the column_ptr after do dest_expr |
797 | | // is likely to be nullable |
798 | 0 | if (LIKELY(column_ptr->is_nullable())) { |
799 | 0 | const auto* nullable_column = reinterpret_cast<const ColumnNullable*>(column_ptr.get()); |
800 | 0 | for (int i = 0; i < rows; ++i) { |
801 | 0 | if (filter_map[i] && nullable_column->is_null_at(i)) { |
802 | | // skip checks for non-mentioned columns in flexible partial update |
803 | 0 | if (skip_bitmaps == nullptr || |
804 | 0 | !skip_bitmaps->at(i).contains(slot_desc->col_unique_id())) { |
805 | | // clang-format off |
806 | 0 | if (_strict_mode && (_src_slot_descs_order_by_dest[dest_index]) && |
807 | 0 | !_src_block_ptr->get_by_position(_dest_slot_to_src_slot_index[dest_index]).column->is_null_at(i)) { |
808 | 0 | filter_map[i] = false; |
809 | 0 | RETURN_IF_ERROR(_state->append_error_msg_to_file( |
810 | 0 | [&]() -> std::string { |
811 | 0 | return _src_block_ptr->dump_one_line(i, _num_of_columns_from_file); |
812 | 0 | }, |
813 | 0 | [&]() -> std::string { |
814 | 0 | auto raw_value = |
815 | 0 | _src_block_ptr->get_by_position(_dest_slot_to_src_slot_index[dest_index]).column->get_data_at(i); |
816 | 0 | std::string raw_string = raw_value.to_string(); |
817 | 0 | fmt::memory_buffer error_msg; |
818 | 0 | fmt::format_to(error_msg,"column({}) value is incorrect while strict mode is {}, src value is {}", |
819 | 0 | slot_desc->col_name(), _strict_mode, raw_string); |
820 | 0 | return fmt::to_string(error_msg); |
821 | 0 | })); |
822 | 0 | } else if (!slot_desc->is_nullable()) { |
823 | 0 | filter_map[i] = false; |
824 | 0 | RETURN_IF_ERROR(_state->append_error_msg_to_file( |
825 | 0 | [&]() -> std::string { |
826 | 0 | return _src_block_ptr->dump_one_line(i, _num_of_columns_from_file); |
827 | 0 | }, |
828 | 0 | [&]() -> std::string { |
829 | 0 | fmt::memory_buffer error_msg; |
830 | 0 | fmt::format_to(error_msg, "column({}) values is null while columns is not nullable", slot_desc->col_name()); |
831 | 0 | return fmt::to_string(error_msg); |
832 | 0 | })); |
833 | 0 | } |
834 | | // clang-format on |
835 | 0 | } |
836 | 0 | } |
837 | 0 | } |
838 | 0 | if (!slot_desc->is_nullable()) { |
839 | 0 | column_ptr = remove_nullable(column_ptr); |
840 | 0 | } |
841 | 0 | } else if (slot_desc->is_nullable()) { |
842 | 0 | column_ptr = make_nullable(column_ptr); |
843 | 0 | } |
844 | 0 | mutable_output_columns[j]->insert_range_from(*column_ptr, 0, rows); |
845 | 0 | ctx_idx++; |
846 | 0 | } |
847 | 0 | scoped_mutable_output_block.restore(); |
848 | | |
849 | | // after do the dest block insert operation, clear _src_block to remove the reference of origin column |
850 | 0 | _src_block_ptr->clear(); |
851 | |
|
852 | 0 | size_t dest_size = block->columns(); |
853 | | // do filter |
854 | 0 | block->insert(ColumnWithTypeAndName(std::move(filter_column), std::make_shared<DataTypeUInt8>(), |
855 | 0 | "filter column")); |
856 | 0 | RETURN_IF_ERROR(Block::filter_block(block, dest_size, dest_size)); |
857 | | |
858 | 0 | _counter.num_rows_filtered += rows - block->rows(); |
859 | 0 | return Status::OK(); |
860 | 0 | } |
861 | | |
862 | 25 | Status FileScanner::_process_src_block_after_read(Block* block) { |
863 | 25 | DCHECK(_process_src_block_after_read_handler != nullptr); |
864 | 25 | return (this->*_process_src_block_after_read_handler)(block); |
865 | 25 | } |
866 | | |
867 | 25 | Status FileScanner::_process_src_block_after_read_for_query(Block* block) { |
868 | 25 | _update_adaptive_batch_size_before_truncate(*block); |
869 | | |
870 | | // Truncate CHAR/VARCHAR columns when target size is smaller than file schema. |
871 | | // This is needed for external table queries with truncate_char_or_varchar_columns=true. |
872 | 25 | RETURN_IF_ERROR(_truncate_char_or_varchar_columns(block)); |
873 | | |
874 | 25 | _update_adaptive_batch_size_after_truncate(*block); |
875 | 25 | return Status::OK(); |
876 | 25 | } |
877 | | |
878 | 0 | Status FileScanner::_process_src_block_after_read_for_load(Block* block) { |
879 | | // Convert the src block columns type in-place. |
880 | 0 | RETURN_IF_ERROR(_cast_to_input_block(block)); |
881 | | // All readers fill partition and missing columns inside get_next_block: |
882 | | // ORC/Parquet: in _do_get_next_block via on_fill_partition_columns/on_fill_missing_columns |
883 | | // CSV/JSON/others: in on_after_read_block via fill_remaining_columns |
884 | | // Assert all columns have consistent row counts. |
885 | 0 | if (_src_block_ptr->columns() > 0) { |
886 | 0 | size_t rows = _src_block_ptr->get_by_position(0).column->size(); |
887 | 0 | for (size_t i = 1; i < _src_block_ptr->columns(); ++i) { |
888 | 0 | DCHECK_EQ(_src_block_ptr->get_by_position(i).column->size(), rows) |
889 | 0 | << "Column " << _src_block_ptr->get_by_position(i).name << " has " |
890 | 0 | << _src_block_ptr->get_by_position(i).column->size() << " rows, expected " |
891 | 0 | << rows; |
892 | 0 | } |
893 | 0 | } |
894 | | // Apply _pre_conjunct_ctxs to filter src block. |
895 | 0 | RETURN_IF_ERROR(_pre_filter_src_block()); |
896 | | |
897 | | // Convert src block to output block (dest block), then apply filters. |
898 | 0 | RETURN_IF_ERROR(_convert_to_output_block(block)); |
899 | | |
900 | 0 | _update_adaptive_batch_size_before_truncate(*block); |
901 | | |
902 | | // Truncate CHAR/VARCHAR columns when target size is smaller than file schema. |
903 | 0 | RETURN_IF_ERROR(_truncate_char_or_varchar_columns(block)); |
904 | | |
905 | 0 | _update_adaptive_batch_size_after_truncate(*block); |
906 | 0 | return Status::OK(); |
907 | 0 | } |
908 | | |
909 | 25 | Status FileScanner::_truncate_char_or_varchar_columns(Block* block) { |
910 | | // Truncate char columns or varchar columns if size is smaller than file columns |
911 | | // or not found in the file column schema. |
912 | 25 | if (!_state->query_options().truncate_char_or_varchar_columns) { |
913 | 25 | return Status::OK(); |
914 | 25 | } |
915 | 0 | int idx = 0; |
916 | 0 | for (auto* slot_desc : _real_tuple_desc->slots()) { |
917 | 0 | const auto& type = slot_desc->type(); |
918 | 0 | if (type->get_primitive_type() != TYPE_VARCHAR && type->get_primitive_type() != TYPE_CHAR) { |
919 | 0 | ++idx; |
920 | 0 | continue; |
921 | 0 | } |
922 | 0 | auto iter = _source_file_col_name_types.find(slot_desc->col_name()); |
923 | 0 | if (iter != _source_file_col_name_types.end()) { |
924 | 0 | const auto file_type_desc = _source_file_col_name_types[slot_desc->col_name()]; |
925 | 0 | int l = -1; |
926 | 0 | if (auto* ftype = check_and_get_data_type<DataTypeString>( |
927 | 0 | remove_nullable(file_type_desc).get())) { |
928 | 0 | l = ftype->len(); |
929 | 0 | } |
930 | 0 | if ((assert_cast<const DataTypeString*>(remove_nullable(type).get())->len() > 0) && |
931 | 0 | (assert_cast<const DataTypeString*>(remove_nullable(type).get())->len() < l || |
932 | 0 | l < 0)) { |
933 | 0 | _truncate_char_or_varchar_column( |
934 | 0 | block, idx, |
935 | 0 | assert_cast<const DataTypeString*>(remove_nullable(type).get())->len()); |
936 | 0 | } |
937 | 0 | } else { |
938 | 0 | _truncate_char_or_varchar_column( |
939 | 0 | block, idx, |
940 | 0 | assert_cast<const DataTypeString*>(remove_nullable(type).get())->len()); |
941 | 0 | } |
942 | 0 | ++idx; |
943 | 0 | } |
944 | 0 | return Status::OK(); |
945 | 25 | } |
946 | | |
947 | | // VARCHAR substring(VARCHAR str, INT pos[, INT len]) |
948 | 0 | void FileScanner::_truncate_char_or_varchar_column(Block* block, int idx, int len) { |
949 | 0 | auto int_type = std::make_shared<DataTypeInt32>(); |
950 | 0 | uint32_t num_columns_without_result = block->columns(); |
951 | 0 | const ColumnNullable* col_nullable = |
952 | 0 | assert_cast<const ColumnNullable*>(block->get_by_position(idx).column.get()); |
953 | 0 | const ColumnPtr& string_column_ptr = col_nullable->get_nested_column_ptr(); |
954 | 0 | ColumnPtr null_map_column_ptr = col_nullable->get_null_map_column_ptr(); |
955 | 0 | block->replace_by_position(idx, std::move(string_column_ptr)); |
956 | 0 | block->insert({int_type->create_column_const(block->rows(), to_field<TYPE_INT>(1)), int_type, |
957 | 0 | "const 1"}); // pos is 1 |
958 | 0 | block->insert({int_type->create_column_const(block->rows(), to_field<TYPE_INT>(len)), int_type, |
959 | 0 | fmt::format("const {}", len)}); // len |
960 | 0 | block->insert({nullptr, std::make_shared<DataTypeString>(), "result"}); // result column |
961 | 0 | ColumnNumbers temp_arguments(3); |
962 | 0 | temp_arguments[0] = idx; // str column |
963 | 0 | temp_arguments[1] = num_columns_without_result; // pos |
964 | 0 | temp_arguments[2] = num_columns_without_result + 1; // len |
965 | 0 | uint32_t result_column_id = num_columns_without_result + 2; |
966 | |
|
967 | 0 | SubstringUtil::substring_execute(*block, temp_arguments, result_column_id, block->rows()); |
968 | 0 | auto res = ColumnNullable::create(block->get_by_position(result_column_id).column, |
969 | 0 | null_map_column_ptr); |
970 | 0 | block->replace_by_position(idx, std::move(res)); |
971 | 0 | Block::erase_useless_column(block, num_columns_without_result); |
972 | 0 | } |
973 | | |
974 | 0 | std::shared_ptr<segment_v2::RowIdColumnIteratorV2> FileScanner::_create_row_id_column_iterator() { |
975 | 0 | auto& id_file_map = _state->get_id_file_map(); |
976 | 0 | auto file_id = id_file_map->get_file_mapping_id( |
977 | 0 | std::make_shared<FileMapping>(((FileScanLocalState*)_local_state)->parent_id(), |
978 | 0 | _current_range, _should_enable_file_meta_cache())); |
979 | 0 | return std::make_shared<RowIdColumnIteratorV2>(IdManager::ID_VERSION, |
980 | 0 | BackendOptions::get_backend_id(), file_id); |
981 | 0 | } |
982 | | |
983 | 26 | void FileScanner::_fill_base_init_context(ReaderInitContext* ctx) { |
984 | 26 | ctx->column_descs = &_column_descs; |
985 | 26 | ctx->col_name_to_block_idx = &_src_block_name_to_idx; |
986 | 26 | ctx->state = _state; |
987 | 26 | ctx->tuple_descriptor = _real_tuple_desc; |
988 | 26 | ctx->row_descriptor = _default_val_row_desc.get(); |
989 | 26 | ctx->params = _params; |
990 | 26 | ctx->range = &_current_range; |
991 | 26 | ctx->table_info_node = TableSchemaChangeHelper::ConstNode::get_instance(); |
992 | 26 | ctx->push_down_agg_type = _get_push_down_agg_type(); |
993 | 26 | } |
994 | | |
995 | 1 | Status FileScanner::_get_next_reader() { |
996 | 1 | while (true) { |
997 | 1 | if (_cur_reader) { |
998 | 0 | _cur_reader->collect_profile_before_close(); |
999 | 0 | RETURN_IF_ERROR(_cur_reader->close()); |
1000 | 0 | _state->update_num_finished_scan_range(1); |
1001 | 0 | } |
1002 | 1 | _cur_reader.reset(nullptr); |
1003 | 1 | _reset_adaptive_batch_size_state(); |
1004 | 1 | _src_block_init = false; |
1005 | 1 | bool has_next = _first_scan_range; |
1006 | 1 | if (!_first_scan_range) { |
1007 | 1 | RETURN_IF_ERROR(_split_source->get_next(&has_next, &_current_range)); |
1008 | 1 | } |
1009 | 1 | _first_scan_range = false; |
1010 | 1 | if (!has_next || _should_stop) { |
1011 | 0 | _scanner_eof = true; |
1012 | 0 | return Status::OK(); |
1013 | 0 | } |
1014 | | |
1015 | 1 | const TFileRangeDesc& range = _current_range; |
1016 | 1 | _current_range_path = range.path; |
1017 | | |
1018 | 1 | if (!_partition_slot_index_map.empty()) { |
1019 | | // we need get partition columns first for runtime filter partition pruning |
1020 | 0 | RETURN_IF_ERROR(_generate_partition_columns()); |
1021 | | |
1022 | 0 | if (_state->query_options().enable_runtime_filter_partition_prune) { |
1023 | | // if enable_runtime_filter_partition_prune is true, we need to check whether this range can be filtered out |
1024 | | // by runtime filter partition prune |
1025 | 0 | if (_push_down_conjuncts.size() < _conjuncts.size()) { |
1026 | | // there are new runtime filters, need to re-init runtime filter partition pruning ctxs |
1027 | 0 | _init_runtime_filter_partition_prune_ctxs(); |
1028 | 0 | } |
1029 | |
|
1030 | 0 | bool can_filter_all = false; |
1031 | 0 | RETURN_IF_ERROR(_process_runtime_filters_partition_prune(can_filter_all)); |
1032 | 0 | if (can_filter_all) { |
1033 | | // this range can be filtered out by runtime filter partition pruning |
1034 | | // so we need to skip this range |
1035 | 0 | COUNTER_UPDATE(_runtime_filter_partition_pruned_range_counter, 1); |
1036 | 0 | continue; |
1037 | 0 | } |
1038 | 0 | } |
1039 | 0 | } |
1040 | | |
1041 | | // create reader for specific format |
1042 | 1 | Status init_status = Status::OK(); |
1043 | 1 | TFileFormatType::type format_type = _get_current_format_type(); |
1044 | | // for compatibility, this logic is deprecated in 3.1 |
1045 | 1 | if (format_type == TFileFormatType::FORMAT_JNI && range.__isset.table_format_params) { |
1046 | 0 | if (range.table_format_params.table_format_type == "paimon" && |
1047 | 0 | !range.table_format_params.paimon_params.__isset.paimon_split) { |
1048 | | // use native reader |
1049 | 0 | auto format = range.table_format_params.paimon_params.file_format; |
1050 | 0 | if (format == "orc") { |
1051 | 0 | format_type = TFileFormatType::FORMAT_ORC; |
1052 | 0 | } else if (format == "parquet") { |
1053 | 0 | format_type = TFileFormatType::FORMAT_PARQUET; |
1054 | 0 | } else { |
1055 | 0 | return Status::InternalError("Not supported paimon file format: {}", format); |
1056 | 0 | } |
1057 | 0 | } |
1058 | 0 | } |
1059 | | |
1060 | 1 | bool push_down_predicates = _should_push_down_predicates(format_type); |
1061 | 1 | bool need_to_get_parsed_schema = false; |
1062 | 1 | switch (format_type) { |
1063 | 1 | case TFileFormatType::FORMAT_JNI: { |
1064 | 1 | ReaderInitContext jni_ctx; |
1065 | 1 | _fill_base_init_context(&jni_ctx); |
1066 | | |
1067 | 1 | if (range.__isset.table_format_params && |
1068 | 1 | range.table_format_params.table_format_type == "max_compute") { |
1069 | 0 | const auto* mc_desc = static_cast<const MaxComputeTableDescriptor*>( |
1070 | 0 | _real_tuple_desc->table_desc()); |
1071 | 0 | if (!mc_desc->init_status()) { |
1072 | 0 | return mc_desc->init_status(); |
1073 | 0 | } |
1074 | 0 | std::unique_ptr<MaxComputeJniReader> mc_reader = MaxComputeJniReader::create_unique( |
1075 | 0 | mc_desc, range.table_format_params.max_compute_params, _file_slot_descs, |
1076 | 0 | range, _state, _profile); |
1077 | 0 | init_status = static_cast<GenericReader*>(mc_reader.get())->init_reader(&jni_ctx); |
1078 | 0 | _cur_reader = std::move(mc_reader); |
1079 | 1 | } else if (range.__isset.table_format_params && |
1080 | 1 | range.table_format_params.table_format_type == "paimon") { |
1081 | 0 | if (_state->query_options().__isset.enable_paimon_cpp_reader && |
1082 | 0 | _state->query_options().enable_paimon_cpp_reader) { |
1083 | 0 | auto cpp_reader = PaimonCppReader::create_unique(_file_slot_descs, _state, |
1084 | 0 | _profile, range, _params); |
1085 | 0 | if (!_is_load && !_push_down_conjuncts.empty()) { |
1086 | 0 | PaimonPredicateConverter predicate_converter(_file_slot_descs, _state); |
1087 | 0 | auto predicate = predicate_converter.build(_push_down_conjuncts); |
1088 | 0 | if (predicate) { |
1089 | 0 | cpp_reader->set_predicate(std::move(predicate)); |
1090 | 0 | } |
1091 | 0 | } |
1092 | 0 | init_status = |
1093 | 0 | static_cast<GenericReader*>(cpp_reader.get())->init_reader(&jni_ctx); |
1094 | 0 | _cur_reader = std::move(cpp_reader); |
1095 | 0 | } else { |
1096 | 0 | auto paimon_reader = PaimonJniReader::create_unique(_file_slot_descs, _state, |
1097 | 0 | _profile, range, _params); |
1098 | 0 | init_status = |
1099 | 0 | static_cast<GenericReader*>(paimon_reader.get())->init_reader(&jni_ctx); |
1100 | 0 | _cur_reader = std::move(paimon_reader); |
1101 | 0 | } |
1102 | 1 | } else if (range.__isset.table_format_params && |
1103 | 1 | range.table_format_params.table_format_type == "hudi") { |
1104 | 0 | auto hudi_reader = HudiJniReader::create_unique( |
1105 | 0 | *_params, range.table_format_params.hudi_params, _file_slot_descs, _state, |
1106 | 0 | _profile); |
1107 | 0 | init_status = static_cast<GenericReader*>(hudi_reader.get())->init_reader(&jni_ctx); |
1108 | 0 | _cur_reader = std::move(hudi_reader); |
1109 | 1 | } else if (range.__isset.table_format_params && |
1110 | 1 | range.table_format_params.table_format_type == "trino_connector") { |
1111 | 0 | auto trino_reader = TrinoConnectorJniReader::create_unique(_file_slot_descs, _state, |
1112 | 0 | _profile, range); |
1113 | 0 | init_status = |
1114 | 0 | static_cast<GenericReader*>(trino_reader.get())->init_reader(&jni_ctx); |
1115 | 0 | _cur_reader = std::move(trino_reader); |
1116 | 1 | } else if (range.__isset.table_format_params && |
1117 | 1 | range.table_format_params.table_format_type == "jdbc") { |
1118 | | // Extract jdbc params from table_format_params |
1119 | 0 | std::map<std::string, std::string> jdbc_params( |
1120 | 0 | range.table_format_params.jdbc_params.begin(), |
1121 | 0 | range.table_format_params.jdbc_params.end()); |
1122 | 0 | auto jdbc_reader = JdbcJniReader::create_unique(_file_slot_descs, _state, _profile, |
1123 | 0 | jdbc_params); |
1124 | 0 | init_status = static_cast<GenericReader*>(jdbc_reader.get())->init_reader(&jni_ctx); |
1125 | 0 | _cur_reader = std::move(jdbc_reader); |
1126 | 1 | } else if (range.__isset.table_format_params && |
1127 | 1 | range.table_format_params.table_format_type == "iceberg") { |
1128 | 0 | auto iceberg_sys_reader = IcebergSysTableJniReader::create_unique( |
1129 | 0 | _file_slot_descs, _state, _profile, range, _params); |
1130 | 0 | init_status = static_cast<GenericReader*>(iceberg_sys_reader.get()) |
1131 | 0 | ->init_reader(&jni_ctx); |
1132 | 0 | _cur_reader = std::move(iceberg_sys_reader); |
1133 | 0 | } |
1134 | | // Set col_name_to_block_idx for JNI readers to avoid repeated map creation |
1135 | 1 | if (_cur_reader) { |
1136 | 0 | if (auto* jni_reader = dynamic_cast<JniReader*>(_cur_reader.get())) { |
1137 | 0 | jni_reader->set_col_name_to_block_idx(&_src_block_name_to_idx); |
1138 | 0 | } |
1139 | 0 | } |
1140 | 1 | break; |
1141 | 1 | } |
1142 | 0 | case TFileFormatType::FORMAT_PARQUET: { |
1143 | 0 | auto file_meta_cache_ptr = _should_enable_file_meta_cache() |
1144 | 0 | ? ExecEnv::GetInstance()->file_meta_cache() |
1145 | 0 | : nullptr; |
1146 | 0 | if (push_down_predicates) { |
1147 | 0 | RETURN_IF_ERROR(_process_late_arrival_conjuncts()); |
1148 | 0 | } |
1149 | 0 | RETURN_IF_ERROR(_init_parquet_reader(file_meta_cache_ptr)); |
1150 | | |
1151 | 0 | need_to_get_parsed_schema = true; |
1152 | 0 | break; |
1153 | 0 | } |
1154 | 0 | case TFileFormatType::FORMAT_ORC: { |
1155 | 0 | auto file_meta_cache_ptr = _should_enable_file_meta_cache() |
1156 | 0 | ? ExecEnv::GetInstance()->file_meta_cache() |
1157 | 0 | : nullptr; |
1158 | 0 | if (push_down_predicates) { |
1159 | 0 | RETURN_IF_ERROR(_process_late_arrival_conjuncts()); |
1160 | 0 | } |
1161 | 0 | RETURN_IF_ERROR(_init_orc_reader(file_meta_cache_ptr)); |
1162 | | |
1163 | 0 | need_to_get_parsed_schema = true; |
1164 | 0 | break; |
1165 | 0 | } |
1166 | 0 | case TFileFormatType::FORMAT_CSV_PLAIN: |
1167 | 0 | case TFileFormatType::FORMAT_CSV_GZ: |
1168 | 0 | case TFileFormatType::FORMAT_CSV_BZ2: |
1169 | 0 | case TFileFormatType::FORMAT_CSV_LZ4FRAME: |
1170 | 0 | case TFileFormatType::FORMAT_CSV_LZ4BLOCK: |
1171 | 0 | case TFileFormatType::FORMAT_CSV_LZOP: |
1172 | 0 | case TFileFormatType::FORMAT_CSV_DEFLATE: |
1173 | 0 | case TFileFormatType::FORMAT_CSV_SNAPPYBLOCK: |
1174 | 0 | case TFileFormatType::FORMAT_PROTO: { |
1175 | 0 | auto reader = CsvReader::create_unique(_state, _profile, &_counter, *_params, range, |
1176 | 0 | _file_slot_descs, _state->batch_size(), nullptr, |
1177 | 0 | _io_ctx); |
1178 | 0 | CsvInitContext csv_ctx; |
1179 | 0 | _fill_base_init_context(&csv_ctx); |
1180 | 0 | csv_ctx.is_load = _is_load; |
1181 | 0 | init_status = static_cast<GenericReader*>(reader.get())->init_reader(&csv_ctx); |
1182 | 0 | _cur_reader = std::move(reader); |
1183 | 0 | break; |
1184 | 0 | } |
1185 | 0 | case TFileFormatType::FORMAT_TEXT: { |
1186 | 0 | auto reader = TextReader::create_unique(_state, _profile, &_counter, *_params, range, |
1187 | 0 | _file_slot_descs, _state->batch_size(), nullptr, |
1188 | 0 | _io_ctx); |
1189 | 0 | CsvInitContext text_ctx; |
1190 | 0 | _fill_base_init_context(&text_ctx); |
1191 | 0 | text_ctx.is_load = _is_load; |
1192 | 0 | init_status = static_cast<GenericReader*>(reader.get())->init_reader(&text_ctx); |
1193 | 0 | _cur_reader = std::move(reader); |
1194 | 0 | break; |
1195 | 0 | } |
1196 | 0 | case TFileFormatType::FORMAT_JSON: { |
1197 | 0 | _cur_reader = NewJsonReader::create_unique(_state, _profile, &_counter, *_params, range, |
1198 | 0 | _file_slot_descs, &_scanner_eof, |
1199 | 0 | _state->batch_size(), nullptr, _io_ctx); |
1200 | 0 | JsonInitContext json_ctx; |
1201 | 0 | _fill_base_init_context(&json_ctx); |
1202 | 0 | json_ctx.col_default_value_ctx = &_col_default_value_ctx; |
1203 | 0 | json_ctx.is_load = _is_load; |
1204 | 0 | init_status = _cur_reader->init_reader(&json_ctx); |
1205 | 0 | break; |
1206 | 0 | } |
1207 | | |
1208 | 0 | case TFileFormatType::FORMAT_WAL: { |
1209 | 0 | _cur_reader = WalReader::create_unique(_state); |
1210 | 0 | WalInitContext wal_ctx; |
1211 | 0 | _fill_base_init_context(&wal_ctx); |
1212 | 0 | wal_ctx.output_tuple_descriptor = _output_tuple_desc; |
1213 | 0 | init_status = _cur_reader->init_reader(&wal_ctx); |
1214 | 0 | break; |
1215 | 0 | } |
1216 | 0 | case TFileFormatType::FORMAT_NATIVE: { |
1217 | 0 | auto reader = NativeReader::create_unique(_profile, *_params, range, _io_ctx, _state); |
1218 | 0 | ReaderInitContext native_ctx; |
1219 | 0 | _fill_base_init_context(&native_ctx); |
1220 | 0 | init_status = static_cast<GenericReader*>(reader.get())->init_reader(&native_ctx); |
1221 | 0 | _cur_reader = std::move(reader); |
1222 | 0 | need_to_get_parsed_schema = false; |
1223 | 0 | break; |
1224 | 0 | } |
1225 | 0 | case TFileFormatType::FORMAT_ARROW: { |
1226 | 0 | ReaderInitContext arrow_ctx; |
1227 | 0 | _fill_base_init_context(&arrow_ctx); |
1228 | |
|
1229 | 0 | if (range.__isset.table_format_params && |
1230 | 0 | range.table_format_params.table_format_type == "remote_doris") { |
1231 | 0 | auto doris_reader = |
1232 | 0 | RemoteDorisReader::create_unique(_file_slot_descs, _state, _profile, range); |
1233 | 0 | init_status = |
1234 | 0 | static_cast<GenericReader*>(doris_reader.get())->init_reader(&arrow_ctx); |
1235 | 0 | if (doris_reader) { |
1236 | 0 | doris_reader->set_col_name_to_block_idx(&_src_block_name_to_idx); |
1237 | 0 | } |
1238 | 0 | _cur_reader = std::move(doris_reader); |
1239 | 0 | } else { |
1240 | 0 | auto arrow_reader = |
1241 | 0 | ArrowStreamReader::create_unique(_state, _profile, &_counter, *_params, |
1242 | 0 | range, _file_slot_descs, _io_ctx.get()); |
1243 | 0 | init_status = |
1244 | 0 | static_cast<GenericReader*>(arrow_reader.get())->init_reader(&arrow_ctx); |
1245 | 0 | _cur_reader = std::move(arrow_reader); |
1246 | 0 | } |
1247 | 0 | break; |
1248 | 0 | } |
1249 | | #ifdef BUILD_RUST_READERS |
1250 | | case TFileFormatType::FORMAT_LANCE: { |
1251 | | auto lance_reader = LanceRustReader::create_unique(_file_slot_descs, _state, _profile, |
1252 | | range, _params); |
1253 | | init_status = lance_reader->init_reader(); |
1254 | | _cur_reader = std::move(lance_reader); |
1255 | | need_to_get_parsed_schema = true; |
1256 | | break; |
1257 | | } |
1258 | | #endif |
1259 | 0 | case TFileFormatType::FORMAT_ES_HTTP: { |
1260 | 0 | _cur_reader = EsHttpReader::create_unique(_file_slot_descs, _state, _profile, range, |
1261 | 0 | *_params, _real_tuple_desc); |
1262 | 0 | init_status = static_cast<EsHttpReader*>(_cur_reader.get())->init_reader(); |
1263 | 0 | break; |
1264 | 0 | } |
1265 | 0 | default: |
1266 | 0 | return Status::NotSupported("Not supported create reader for file format: {}.", |
1267 | 0 | to_string(_params->format_type)); |
1268 | 1 | } |
1269 | | |
1270 | 1 | if (_cur_reader == nullptr) { |
1271 | 1 | return Status::NotSupported( |
1272 | 1 | "Not supported create reader for table format: {} / file format: {}.", |
1273 | 1 | range.__isset.table_format_params ? range.table_format_params.table_format_type |
1274 | 1 | : "NotSet", |
1275 | 1 | to_string(_params->format_type)); |
1276 | 1 | } |
1277 | 0 | COUNTER_UPDATE(_file_counter, 1); |
1278 | | // The FileScanner for external table may try to open not exist files, |
1279 | | // Because FE file cache for external table may out of date. |
1280 | | // So, NOT_FOUND for FileScanner is not a fail case. |
1281 | | // Will remove this after file reader refactor. |
1282 | 0 | if (init_status.is<END_OF_FILE>()) { |
1283 | 0 | COUNTER_UPDATE(_empty_file_counter, 1); |
1284 | 0 | continue; |
1285 | 0 | } else if (init_status.is<ErrorCode::NOT_FOUND>()) { |
1286 | 0 | if (config::ignore_not_found_file_in_external_table) { |
1287 | 0 | COUNTER_UPDATE(_not_found_file_counter, 1); |
1288 | 0 | continue; |
1289 | 0 | } |
1290 | 0 | return Status::InternalError("failed to find reader, err: {}", init_status.to_string()); |
1291 | 0 | } else if (!init_status.ok()) { |
1292 | 0 | return Status::InternalError("failed to init reader, err: {}", init_status.to_string()); |
1293 | 0 | } |
1294 | | |
1295 | | // For table-level COUNT pushdown, offsets are undefined so we must skip |
1296 | | // _set_fill_or_truncate_columns (it uses [start_offset, end_offset] to |
1297 | | // filter row groups, which would produce incorrect empty results). |
1298 | 0 | bool is_table_level_count = _get_push_down_agg_type() == TPushAggOp::type::COUNT && |
1299 | 0 | range.__isset.table_format_params && |
1300 | 0 | range.table_format_params.table_level_row_count >= 0; |
1301 | 0 | if (!is_table_level_count) { |
1302 | 0 | Status status = _set_fill_or_truncate_columns(need_to_get_parsed_schema); |
1303 | 0 | if (status.is<END_OF_FILE>()) { // all parquet row groups are filtered |
1304 | 0 | continue; |
1305 | 0 | } else if (!status.ok()) { |
1306 | 0 | return Status::InternalError("failed to set_fill_or_truncate_columns, err: {}", |
1307 | 0 | status.to_string()); |
1308 | 0 | } |
1309 | 0 | } |
1310 | | |
1311 | | // Unified COUNT(*) pushdown: replace the real reader with CountReader |
1312 | | // decorator if the reader accepts COUNT and can provide a total row count. |
1313 | 0 | if (_cur_reader->get_push_down_agg_type() == TPushAggOp::type::COUNT) { |
1314 | 0 | int64_t total_rows = -1; |
1315 | 0 | if (is_table_level_count) { |
1316 | | // FE-provided count (may account for table-format deletions) |
1317 | 0 | total_rows = range.table_format_params.table_level_row_count; |
1318 | 0 | } else if (_cur_reader->supports_count_pushdown()) { |
1319 | | // File metadata count (ORC footer / Parquet row groups) |
1320 | 0 | total_rows = _cur_reader->get_total_rows(); |
1321 | 0 | } |
1322 | 0 | if (total_rows >= 0) { |
1323 | 0 | auto batch_size = _state->batch_size(); |
1324 | 0 | _cur_reader = std::make_unique<CountReader>(total_rows, batch_size, |
1325 | 0 | std::move(_cur_reader)); |
1326 | 0 | } |
1327 | 0 | } |
1328 | 0 | _cur_reader_eof = false; |
1329 | 0 | _init_adaptive_batch_size_state(format_type); |
1330 | 0 | break; |
1331 | 0 | } |
1332 | 0 | return Status::OK(); |
1333 | 1 | } |
1334 | | |
1335 | | Status FileScanner::_init_parquet_reader(FileMetaCache* file_meta_cache_ptr, |
1336 | 14 | std::unique_ptr<ParquetReader> parquet_reader) { |
1337 | 14 | const TFileRangeDesc& range = _current_range; |
1338 | 14 | Status init_status = Status::OK(); |
1339 | | |
1340 | 14 | phmap::flat_hash_map<int, std::vector<std::shared_ptr<ColumnPredicate>>> slot_id_to_predicates = |
1341 | 14 | _local_state |
1342 | 14 | ? _local_state->cast<FileScanLocalState>()._slot_id_to_predicates |
1343 | 14 | : phmap::flat_hash_map<int, std::vector<std::shared_ptr<ColumnPredicate>>> {}; |
1344 | | |
1345 | | // Build unified ParquetInitContext (shared by all Parquet reader variants) |
1346 | 14 | ParquetInitContext pctx; |
1347 | 14 | _fill_base_init_context(&pctx); |
1348 | 14 | pctx.conjuncts = &_push_down_conjuncts; |
1349 | 14 | pctx.slot_id_to_predicates = &slot_id_to_predicates; |
1350 | 14 | pctx.colname_to_slot_id = _col_name_to_slot_id; |
1351 | 14 | pctx.not_single_slot_filter_conjuncts = &_not_single_slot_filter_conjuncts; |
1352 | 14 | pctx.slot_id_to_filter_conjuncts = &_slot_id_to_filter_conjuncts; |
1353 | | |
1354 | 14 | if (range.__isset.table_format_params && |
1355 | 14 | range.table_format_params.table_format_type == "iceberg") { |
1356 | | // IcebergParquetReader IS-A ParquetReader (CRTP mixin), no wrapping needed |
1357 | 0 | std::unique_ptr<IcebergParquetReader> iceberg_reader = IcebergParquetReader::create_unique( |
1358 | 0 | _kv_cache, _profile, *_params, range, _state->batch_size(), &_state->timezone_obj(), |
1359 | 0 | _io_ctx, _state, file_meta_cache_ptr); |
1360 | 0 | iceberg_reader->set_create_row_id_column_iterator_func( |
1361 | 0 | [this]() -> std::shared_ptr<segment_v2::RowIdColumnIteratorV2> { |
1362 | 0 | return _create_row_id_column_iterator(); |
1363 | 0 | }); |
1364 | 0 | init_status = static_cast<GenericReader*>(iceberg_reader.get())->init_reader(&pctx); |
1365 | 0 | _cur_reader = std::move(iceberg_reader); |
1366 | 14 | } else if (range.__isset.table_format_params && |
1367 | 14 | range.table_format_params.table_format_type == "paimon") { |
1368 | | // PaimonParquetReader IS-A ParquetReader, no wrapping needed |
1369 | 0 | auto paimon_reader = PaimonParquetReader::create_unique( |
1370 | 0 | _profile, *_params, range, _state->batch_size(), &_state->timezone_obj(), _kv_cache, |
1371 | 0 | _io_ctx, _state, file_meta_cache_ptr); |
1372 | 0 | init_status = static_cast<GenericReader*>(paimon_reader.get())->init_reader(&pctx); |
1373 | 0 | _cur_reader = std::move(paimon_reader); |
1374 | 14 | } else if (range.__isset.table_format_params && |
1375 | 14 | range.table_format_params.table_format_type == "hudi") { |
1376 | | // HudiParquetReader IS-A ParquetReader, no wrapping needed |
1377 | 0 | auto hudi_reader = HudiParquetReader::create_unique( |
1378 | 0 | _profile, *_params, range, _state->batch_size(), &_state->timezone_obj(), _io_ctx, |
1379 | 0 | _state, file_meta_cache_ptr); |
1380 | 0 | init_status = static_cast<GenericReader*>(hudi_reader.get())->init_reader(&pctx); |
1381 | 0 | _cur_reader = std::move(hudi_reader); |
1382 | 14 | } else if (range.table_format_params.table_format_type == "hive") { |
1383 | 14 | auto hive_reader = HiveParquetReader::create_unique( |
1384 | 14 | _profile, *_params, range, _state->batch_size(), &_state->timezone_obj(), _io_ctx, |
1385 | 14 | _state, &_is_file_slot, file_meta_cache_ptr, |
1386 | 14 | _state->query_options().enable_parquet_lazy_mat); |
1387 | 14 | hive_reader->set_create_row_id_column_iterator_func( |
1388 | 14 | [this]() -> std::shared_ptr<segment_v2::RowIdColumnIteratorV2> { |
1389 | 0 | return _create_row_id_column_iterator(); |
1390 | 0 | }); |
1391 | 14 | init_status = static_cast<GenericReader*>(hive_reader.get())->init_reader(&pctx); |
1392 | 14 | _cur_reader = std::move(hive_reader); |
1393 | 14 | } else if (range.table_format_params.table_format_type == "tvf") { |
1394 | 0 | if (!parquet_reader) { |
1395 | 0 | parquet_reader = ParquetReader::create_unique( |
1396 | 0 | _profile, *_params, range, _state->batch_size(), &_state->timezone_obj(), |
1397 | 0 | _io_ctx, _state, file_meta_cache_ptr, |
1398 | 0 | _state->query_options().enable_parquet_lazy_mat); |
1399 | 0 | } |
1400 | 0 | parquet_reader->set_create_row_id_column_iterator_func( |
1401 | 0 | [this]() -> std::shared_ptr<segment_v2::RowIdColumnIteratorV2> { |
1402 | 0 | return _create_row_id_column_iterator(); |
1403 | 0 | }); |
1404 | 0 | init_status = static_cast<GenericReader*>(parquet_reader.get())->init_reader(&pctx); |
1405 | 0 | _cur_reader = std::move(parquet_reader); |
1406 | 0 | } else if (_is_load) { |
1407 | 0 | if (!parquet_reader) { |
1408 | 0 | parquet_reader = ParquetReader::create_unique( |
1409 | 0 | _profile, *_params, range, _state->batch_size(), &_state->timezone_obj(), |
1410 | 0 | _io_ctx, _state, file_meta_cache_ptr, |
1411 | 0 | _state->query_options().enable_parquet_lazy_mat); |
1412 | 0 | } |
1413 | 0 | init_status = static_cast<GenericReader*>(parquet_reader.get())->init_reader(&pctx); |
1414 | 0 | _cur_reader = std::move(parquet_reader); |
1415 | 0 | } |
1416 | | |
1417 | 14 | return init_status; |
1418 | 14 | } |
1419 | | |
1420 | | Status FileScanner::_init_orc_reader(FileMetaCache* file_meta_cache_ptr, |
1421 | 11 | std::unique_ptr<OrcReader> orc_reader) { |
1422 | 11 | const TFileRangeDesc& range = _current_range; |
1423 | 11 | Status init_status = Status::OK(); |
1424 | | |
1425 | | // Build unified OrcInitContext (shared by all ORC reader variants) |
1426 | 11 | OrcInitContext octx; |
1427 | 11 | _fill_base_init_context(&octx); |
1428 | 11 | octx.conjuncts = &_push_down_conjuncts; |
1429 | 11 | octx.not_single_slot_filter_conjuncts = &_not_single_slot_filter_conjuncts; |
1430 | 11 | octx.slot_id_to_filter_conjuncts = &_slot_id_to_filter_conjuncts; |
1431 | | |
1432 | 11 | if (range.__isset.table_format_params && |
1433 | 11 | range.table_format_params.table_format_type == "transactional_hive") { |
1434 | | // TransactionalHiveReader IS-A OrcReader, no wrapping needed |
1435 | 0 | auto tran_orc_reader = TransactionalHiveReader::create_unique( |
1436 | 0 | _profile, _state, *_params, range, _state->batch_size(), _state->timezone(), |
1437 | 0 | _io_ctx, file_meta_cache_ptr); |
1438 | 0 | tran_orc_reader->set_create_row_id_column_iterator_func( |
1439 | 0 | [this]() -> std::shared_ptr<segment_v2::RowIdColumnIteratorV2> { |
1440 | 0 | return _create_row_id_column_iterator(); |
1441 | 0 | }); |
1442 | 0 | init_status = static_cast<GenericReader*>(tran_orc_reader.get())->init_reader(&octx); |
1443 | |
|
1444 | 0 | _cur_reader = std::move(tran_orc_reader); |
1445 | 11 | } else if (range.__isset.table_format_params && |
1446 | 11 | range.table_format_params.table_format_type == "iceberg") { |
1447 | | // IcebergOrcReader IS-A OrcReader (CRTP mixin), no wrapping needed |
1448 | 0 | std::unique_ptr<IcebergOrcReader> iceberg_reader = IcebergOrcReader::create_unique( |
1449 | 0 | _kv_cache, _profile, _state, *_params, range, _state->batch_size(), |
1450 | 0 | _state->timezone(), _io_ctx, file_meta_cache_ptr); |
1451 | 0 | iceberg_reader->set_create_row_id_column_iterator_func( |
1452 | 0 | [this]() -> std::shared_ptr<segment_v2::RowIdColumnIteratorV2> { |
1453 | 0 | return _create_row_id_column_iterator(); |
1454 | 0 | }); |
1455 | 0 | init_status = static_cast<GenericReader*>(iceberg_reader.get())->init_reader(&octx); |
1456 | |
|
1457 | 0 | _cur_reader = std::move(iceberg_reader); |
1458 | 11 | } else if (range.__isset.table_format_params && |
1459 | 11 | range.table_format_params.table_format_type == "paimon") { |
1460 | | // PaimonOrcReader IS-A OrcReader, no wrapping needed |
1461 | 0 | auto paimon_reader = PaimonOrcReader::create_unique( |
1462 | 0 | _profile, _state, *_params, range, _state->batch_size(), _state->timezone(), |
1463 | 0 | _kv_cache, _io_ctx, file_meta_cache_ptr); |
1464 | 0 | init_status = static_cast<GenericReader*>(paimon_reader.get())->init_reader(&octx); |
1465 | |
|
1466 | 0 | _cur_reader = std::move(paimon_reader); |
1467 | 11 | } else if (range.__isset.table_format_params && |
1468 | 11 | range.table_format_params.table_format_type == "hudi") { |
1469 | | // HudiOrcReader IS-A OrcReader, no wrapping needed |
1470 | 0 | auto hudi_reader = HudiOrcReader::create_unique(_profile, _state, *_params, range, |
1471 | 0 | _state->batch_size(), _state->timezone(), |
1472 | 0 | _io_ctx, file_meta_cache_ptr); |
1473 | 0 | init_status = static_cast<GenericReader*>(hudi_reader.get())->init_reader(&octx); |
1474 | |
|
1475 | 0 | _cur_reader = std::move(hudi_reader); |
1476 | 11 | } else if (range.__isset.table_format_params && |
1477 | 11 | range.table_format_params.table_format_type == "hive") { |
1478 | 11 | auto hive_reader = HiveOrcReader::create_unique( |
1479 | 11 | _profile, _state, *_params, range, _state->batch_size(), _state->timezone(), |
1480 | 11 | _io_ctx, &_is_file_slot, file_meta_cache_ptr, |
1481 | 11 | _state->query_options().enable_orc_lazy_mat); |
1482 | 11 | hive_reader->set_create_row_id_column_iterator_func( |
1483 | 11 | [this]() -> std::shared_ptr<segment_v2::RowIdColumnIteratorV2> { |
1484 | 0 | return _create_row_id_column_iterator(); |
1485 | 0 | }); |
1486 | 11 | init_status = static_cast<GenericReader*>(hive_reader.get())->init_reader(&octx); |
1487 | | |
1488 | 11 | _cur_reader = std::move(hive_reader); |
1489 | 11 | } else if (range.__isset.table_format_params && |
1490 | 0 | range.table_format_params.table_format_type == "tvf") { |
1491 | 0 | if (!orc_reader) { |
1492 | 0 | orc_reader = OrcReader::create_unique( |
1493 | 0 | _profile, _state, *_params, range, _state->batch_size(), _state->timezone(), |
1494 | 0 | _io_ctx, file_meta_cache_ptr, _state->query_options().enable_orc_lazy_mat); |
1495 | 0 | } |
1496 | 0 | orc_reader->set_create_row_id_column_iterator_func( |
1497 | 0 | [this]() -> std::shared_ptr<segment_v2::RowIdColumnIteratorV2> { |
1498 | 0 | return _create_row_id_column_iterator(); |
1499 | 0 | }); |
1500 | 0 | init_status = static_cast<GenericReader*>(orc_reader.get())->init_reader(&octx); |
1501 | 0 | _cur_reader = std::move(orc_reader); |
1502 | 0 | } else if (_is_load) { |
1503 | 0 | if (!orc_reader) { |
1504 | 0 | orc_reader = OrcReader::create_unique( |
1505 | 0 | _profile, _state, *_params, range, _state->batch_size(), _state->timezone(), |
1506 | 0 | _io_ctx, file_meta_cache_ptr, _state->query_options().enable_orc_lazy_mat); |
1507 | 0 | } |
1508 | 0 | init_status = static_cast<GenericReader*>(orc_reader.get())->init_reader(&octx); |
1509 | 0 | _cur_reader = std::move(orc_reader); |
1510 | 0 | } |
1511 | | |
1512 | 11 | return init_status; |
1513 | 11 | } |
1514 | | |
1515 | 25 | Status FileScanner::_set_fill_or_truncate_columns(bool need_to_get_parsed_schema) { |
1516 | 25 | _slot_lower_name_to_col_type.clear(); |
1517 | | |
1518 | 25 | std::unordered_map<std::string, DataTypePtr> name_to_col_type; |
1519 | 25 | RETURN_IF_ERROR(_cur_reader->get_columns(&name_to_col_type)); |
1520 | | |
1521 | 309 | for (const auto& [col_name, col_type] : name_to_col_type) { |
1522 | 309 | auto col_name_lower = to_lower(col_name); |
1523 | 309 | if (_partition_col_descs.contains(col_name_lower)) { |
1524 | | /* |
1525 | | * `_slot_lower_name_to_col_type` is used by `_init_src_block` and `_cast_to_input_block` during LOAD to |
1526 | | * generate columns of the corresponding type, which records the columns existing in the file. |
1527 | | * |
1528 | | * When a column in `COLUMNS FROM PATH` exists in a file column, the column type in the block will |
1529 | | * not match the slot type in `_output_tuple_desc`, causing an error when |
1530 | | * Serde `deserialize_one_cell_from_json` fills the partition values. |
1531 | | * |
1532 | | * So for partition column not need fill _slot_lower_name_to_col_type. |
1533 | | */ |
1534 | 0 | continue; |
1535 | 0 | } |
1536 | 309 | _slot_lower_name_to_col_type.emplace(col_name_lower, col_type); |
1537 | 309 | } |
1538 | | |
1539 | 25 | RETURN_IF_ERROR(_generate_truncate_columns(need_to_get_parsed_schema)); |
1540 | 25 | return Status::OK(); |
1541 | 25 | } |
1542 | | |
1543 | 25 | Status FileScanner::_generate_truncate_columns(bool need_to_get_parsed_schema) { |
1544 | 25 | _source_file_col_name_types.clear(); |
1545 | | // The col names and types of source file, such as parquet, orc files. |
1546 | 25 | if (_state->query_options().truncate_char_or_varchar_columns && need_to_get_parsed_schema) { |
1547 | 0 | std::vector<std::string> source_file_col_names; |
1548 | 0 | std::vector<DataTypePtr> source_file_col_types; |
1549 | 0 | Status status = |
1550 | 0 | _cur_reader->get_parsed_schema(&source_file_col_names, &source_file_col_types); |
1551 | 0 | if (!status.ok() && status.code() != TStatusCode::NOT_IMPLEMENTED_ERROR) { |
1552 | 0 | return status; |
1553 | 0 | } |
1554 | 0 | DCHECK_EQ(source_file_col_names.size(), source_file_col_types.size()); |
1555 | 0 | for (int i = 0; i < source_file_col_names.size(); ++i) { |
1556 | 0 | _source_file_col_name_types[to_lower(source_file_col_names[i])] = |
1557 | 0 | source_file_col_types[i]; |
1558 | 0 | } |
1559 | 0 | } |
1560 | 25 | return Status::OK(); |
1561 | 25 | } |
1562 | | |
1563 | 16 | Status FileScanner::prepare_for_read_lines(const TFileRangeDesc& range) { |
1564 | 16 | _current_range = range; |
1565 | | |
1566 | 16 | _file_cache_statistics.reset(new io::FileCacheStatistics()); |
1567 | 16 | _file_reader_stats.reset(new io::FileReaderStats()); |
1568 | | |
1569 | 16 | _file_read_bytes_counter = |
1570 | 16 | ADD_COUNTER_WITH_LEVEL(_profile, FileReadBytesProfile, TUnit::BYTES, 1); |
1571 | 16 | _file_read_time_counter = ADD_TIMER_WITH_LEVEL(_profile, FileReadTimeProfile, 1); |
1572 | | |
1573 | 16 | RETURN_IF_ERROR(_init_io_ctx()); |
1574 | 16 | _io_ctx->file_cache_stats = _file_cache_statistics.get(); |
1575 | 16 | _io_ctx->file_reader_stats = _file_reader_stats.get(); |
1576 | 16 | _default_val_row_desc.reset(new RowDescriptor((TupleDescriptor*)_real_tuple_desc)); |
1577 | 16 | RETURN_IF_ERROR(_init_expr_ctxes()); |
1578 | | |
1579 | | // Since only one column is read from the file, there is no need to filter, so set these variables to empty. |
1580 | 16 | _push_down_conjuncts.clear(); |
1581 | 16 | _not_single_slot_filter_conjuncts.clear(); |
1582 | 16 | _slot_id_to_filter_conjuncts.clear(); |
1583 | 16 | _kv_cache = nullptr; |
1584 | 16 | return Status::OK(); |
1585 | 16 | } |
1586 | | |
1587 | | Status FileScanner::read_lines_from_range(const TFileRangeDesc& range, |
1588 | | const std::list<int64_t>& row_ids, Block* result_block, |
1589 | | const ExternalFileMappingInfo& external_info, |
1590 | 25 | int64_t* init_reader_ms, int64_t* get_block_ms) { |
1591 | 25 | _current_range = range; |
1592 | 25 | RETURN_IF_ERROR(_generate_partition_columns()); |
1593 | | |
1594 | 25 | TFileFormatType::type format_type = _get_current_format_type(); |
1595 | 25 | Status init_status = Status::OK(); |
1596 | | |
1597 | 25 | auto file_meta_cache_ptr = external_info.enable_file_meta_cache |
1598 | 25 | ? ExecEnv::GetInstance()->file_meta_cache() |
1599 | 25 | : nullptr; |
1600 | | |
1601 | 25 | RETURN_IF_ERROR(scope_timer_run( |
1602 | 25 | [&]() -> Status { |
1603 | 25 | switch (format_type) { |
1604 | 25 | case TFileFormatType::FORMAT_PARQUET: { |
1605 | 25 | std::unique_ptr<ParquetReader> parquet_reader = ParquetReader::create_unique( |
1606 | 25 | _profile, *_params, range, 1, &_state->timezone_obj(), _io_ctx, _state, |
1607 | 25 | file_meta_cache_ptr, false); |
1608 | 25 | RETURN_IF_ERROR( |
1609 | 25 | _init_parquet_reader(file_meta_cache_ptr, std::move(parquet_reader))); |
1610 | | // _init_parquet_reader may create a new table-format specific reader |
1611 | | // (e.g., HiveParquetReader) that replaces the original parquet_reader. |
1612 | | // We need to re-apply read_by_rows to the actual _cur_reader. |
1613 | 25 | RETURN_IF_ERROR(_cur_reader->read_by_rows(row_ids)); |
1614 | 25 | break; |
1615 | 25 | } |
1616 | 25 | case TFileFormatType::FORMAT_ORC: { |
1617 | 25 | std::unique_ptr<OrcReader> orc_reader = OrcReader::create_unique( |
1618 | 25 | _profile, _state, *_params, range, 1, _state->timezone(), _io_ctx, |
1619 | 25 | file_meta_cache_ptr, false); |
1620 | 25 | RETURN_IF_ERROR(_init_orc_reader(file_meta_cache_ptr, std::move(orc_reader))); |
1621 | | // Same as above: re-apply read_by_rows to the actual _cur_reader. |
1622 | 25 | RETURN_IF_ERROR(_cur_reader->read_by_rows(row_ids)); |
1623 | 25 | break; |
1624 | 25 | } |
1625 | 25 | default: { |
1626 | 25 | return Status::NotSupported( |
1627 | 25 | "Not support create lines reader for file format: {}," |
1628 | 25 | "only support parquet and orc.", |
1629 | 25 | to_string(_params->format_type)); |
1630 | 25 | } |
1631 | 25 | } |
1632 | 25 | return Status::OK(); |
1633 | 25 | }, |
1634 | 25 | init_reader_ms)); |
1635 | | |
1636 | 25 | RETURN_IF_ERROR(_set_fill_or_truncate_columns(true)); |
1637 | 25 | _cur_reader_eof = false; |
1638 | | |
1639 | 25 | RETURN_IF_ERROR(scope_timer_run( |
1640 | 25 | [&]() -> Status { |
1641 | 25 | while (!_cur_reader_eof) { |
1642 | 25 | bool eof = false; |
1643 | 25 | RETURN_IF_ERROR(_get_block_impl(_state, result_block, &eof)); |
1644 | 25 | } |
1645 | 25 | return Status::OK(); |
1646 | 25 | }, |
1647 | 25 | get_block_ms)); |
1648 | | |
1649 | 25 | _cur_reader->collect_profile_before_close(); |
1650 | 25 | RETURN_IF_ERROR(_cur_reader->close()); |
1651 | | |
1652 | 25 | COUNTER_UPDATE(_file_read_bytes_counter, _file_reader_stats->read_bytes); |
1653 | 25 | COUNTER_UPDATE(_file_read_time_counter, _file_reader_stats->read_time_ns); |
1654 | 25 | return Status::OK(); |
1655 | 25 | } |
1656 | | |
1657 | 25 | Status FileScanner::_generate_partition_columns() { |
1658 | 25 | _partition_col_descs.clear(); |
1659 | 25 | _partition_value_is_null.clear(); |
1660 | 25 | const TFileRangeDesc& range = _current_range; |
1661 | 25 | if (!range.__isset.columns_from_path_keys) { |
1662 | 25 | return Status::OK(); |
1663 | 25 | } |
1664 | 0 | DORIS_CHECK(range.__isset.columns_from_path); |
1665 | 0 | DORIS_CHECK(range.columns_from_path.size() == range.columns_from_path_keys.size()); |
1666 | 0 | const bool has_null_flags = range.__isset.columns_from_path_is_null; |
1667 | 0 | if (has_null_flags) { |
1668 | 0 | DORIS_CHECK(range.columns_from_path_is_null.size() == range.columns_from_path_keys.size()); |
1669 | 0 | } |
1670 | |
|
1671 | 0 | std::unordered_map<std::string, int> partition_name_to_key_index; |
1672 | 0 | int index = 0; |
1673 | 0 | for (const auto& key : range.columns_from_path_keys) { |
1674 | 0 | partition_name_to_key_index.emplace(key, index++); |
1675 | 0 | } |
1676 | | |
1677 | | // Iterate _column_descs to find PARTITION_KEY columns instead of _partition_slot_descs. |
1678 | 0 | for (const auto& col_desc : _column_descs) { |
1679 | 0 | if (col_desc.category != ColumnCategory::PARTITION_KEY) { |
1680 | 0 | continue; |
1681 | 0 | } |
1682 | 0 | auto pit = partition_name_to_key_index.find(col_desc.name); |
1683 | 0 | if (pit != partition_name_to_key_index.end()) { |
1684 | 0 | auto values_index = cast_set<size_t>(pit->second); |
1685 | 0 | _partition_col_descs.emplace( |
1686 | 0 | col_desc.name, |
1687 | 0 | std::make_tuple(range.columns_from_path[values_index], col_desc.slot_desc)); |
1688 | 0 | _partition_value_is_null.emplace(col_desc.name, |
1689 | 0 | has_null_flags |
1690 | 0 | ? range.columns_from_path_is_null[values_index] |
1691 | 0 | : false); |
1692 | 0 | } |
1693 | 0 | } |
1694 | 0 | return Status::OK(); |
1695 | 25 | } |
1696 | | |
1697 | 16 | Status FileScanner::_init_expr_ctxes() { |
1698 | 16 | std::map<SlotId, int> full_src_index_map; |
1699 | 16 | std::map<SlotId, SlotDescriptor*> full_src_slot_map; |
1700 | 16 | std::map<std::string, int> partition_name_to_key_index_map; |
1701 | 16 | int index = 0; |
1702 | 117 | for (const auto& slot_desc : _real_tuple_desc->slots()) { |
1703 | 117 | full_src_slot_map.emplace(slot_desc->id(), slot_desc); |
1704 | 117 | full_src_index_map.emplace(slot_desc->id(), index++); |
1705 | 117 | } |
1706 | | |
1707 | | // For external table query, find the index of column in path. |
1708 | | // Because query doesn't always search for all columns in a table |
1709 | | // and the order of selected columns is random. |
1710 | | // All ranges in _ranges vector should have identical columns_from_path_keys |
1711 | | // because they are all file splits for the same external table. |
1712 | | // So here use the first element of _ranges to fill the partition_name_to_key_index_map |
1713 | 16 | if (_current_range.__isset.columns_from_path_keys) { |
1714 | 0 | std::vector<std::string> key_map = _current_range.columns_from_path_keys; |
1715 | 0 | if (!key_map.empty()) { |
1716 | 0 | for (size_t i = 0; i < key_map.size(); i++) { |
1717 | 0 | partition_name_to_key_index_map.emplace(key_map[i], i); |
1718 | 0 | } |
1719 | 0 | } |
1720 | 0 | } |
1721 | | |
1722 | 16 | _num_of_columns_from_file = _params->num_of_columns_from_file; |
1723 | 117 | for (const auto& slot_info : _params->required_slots) { |
1724 | 117 | auto slot_id = slot_info.slot_id; |
1725 | 117 | auto it = full_src_slot_map.find(slot_id); |
1726 | 117 | if (it == std::end(full_src_slot_map)) { |
1727 | 0 | return Status::InternalError( |
1728 | 0 | fmt::format("Unknown source slot descriptor, slot_id={}", slot_id)); |
1729 | 0 | } |
1730 | | |
1731 | 117 | ColumnDescriptor col_desc; |
1732 | 117 | col_desc.name = it->second->col_name(); |
1733 | 117 | col_desc.slot_desc = it->second; |
1734 | | |
1735 | | // Read category from Thrift if available (new FE), otherwise fall back |
1736 | | // to slot_info.is_file_slot + partition_name_to_key_index_map for broker/stream load |
1737 | | // where the FE does not set TColumnCategory. |
1738 | 117 | if (slot_info.__isset.category) { |
1739 | 0 | switch (slot_info.category) { |
1740 | 0 | case TColumnCategory::REGULAR: |
1741 | 0 | col_desc.category = ColumnCategory::REGULAR; |
1742 | 0 | break; |
1743 | 0 | case TColumnCategory::PARTITION_KEY: |
1744 | 0 | col_desc.category = ColumnCategory::PARTITION_KEY; |
1745 | 0 | break; |
1746 | 0 | case TColumnCategory::SYNTHESIZED: |
1747 | 0 | col_desc.category = ColumnCategory::SYNTHESIZED; |
1748 | 0 | break; |
1749 | 0 | case TColumnCategory::GENERATED: |
1750 | 0 | col_desc.category = ColumnCategory::GENERATED; |
1751 | 0 | break; |
1752 | 0 | } |
1753 | 117 | } else if (partition_name_to_key_index_map.contains(it->second->col_name()) && |
1754 | 117 | !slot_info.is_file_slot) { |
1755 | 0 | col_desc.category = ColumnCategory::PARTITION_KEY; |
1756 | 0 | } |
1757 | | |
1758 | | // Derive is_file_slot from category |
1759 | 117 | bool is_file_slot = (col_desc.category == ColumnCategory::REGULAR || |
1760 | 117 | col_desc.category == ColumnCategory::GENERATED); |
1761 | | |
1762 | 117 | if (partition_name_to_key_index_map.contains(it->second->col_name())) { |
1763 | 0 | if (_is_load) { |
1764 | 0 | auto iti = full_src_index_map.find(slot_id); |
1765 | 0 | _partition_slot_index_map.emplace(slot_id, iti->second - _num_of_columns_from_file); |
1766 | 0 | } else { |
1767 | 0 | auto kit = partition_name_to_key_index_map.find(it->second->col_name()); |
1768 | 0 | _partition_slot_index_map.emplace(slot_id, kit->second); |
1769 | 0 | } |
1770 | 0 | } |
1771 | | |
1772 | 117 | if (is_file_slot) { |
1773 | 117 | _is_file_slot.emplace(slot_id); |
1774 | 117 | _file_slot_descs.emplace_back(it->second); |
1775 | 117 | _file_col_names.push_back(it->second->col_name()); |
1776 | 117 | } |
1777 | | |
1778 | 117 | _column_descs.push_back(col_desc); |
1779 | 117 | } |
1780 | | |
1781 | | // set column name to default value expr map |
1782 | | // new inline TFileScanSlotInfo.default_value_expr (preferred) |
1783 | 117 | for (const auto& slot_info : _params->required_slots) { |
1784 | 117 | auto slot_id = slot_info.slot_id; |
1785 | 117 | auto it = full_src_slot_map.find(slot_id); |
1786 | 117 | if (it == std::end(full_src_slot_map)) { |
1787 | 0 | continue; |
1788 | 0 | } |
1789 | 117 | const std::string& col_name = it->second->col_name(); |
1790 | | |
1791 | 117 | VExprContextSPtr ctx; |
1792 | 117 | bool has_default = false; |
1793 | | |
1794 | | // Prefer inline default_value_expr from TFileScanSlotInfo (new FE) |
1795 | 117 | if (slot_info.__isset.default_value_expr && !slot_info.default_value_expr.nodes.empty()) { |
1796 | 0 | RETURN_IF_ERROR(VExpr::create_expr_tree(slot_info.default_value_expr, ctx)); |
1797 | 0 | RETURN_IF_ERROR(ctx->prepare(_state, *_default_val_row_desc)); |
1798 | 0 | RETURN_IF_ERROR(ctx->open(_state)); |
1799 | 0 | has_default = true; |
1800 | 117 | } else if (slot_info.__isset.default_value_expr) { |
1801 | | // Empty nodes means null default (same as legacy empty TExpr) |
1802 | 0 | has_default = true; |
1803 | 0 | } |
1804 | | |
1805 | | // Fall back to legacy default_value_of_src_slot map for mixed-version FE/BE |
1806 | | // and callers that have not preserved inline default_value_expr yet. |
1807 | 117 | if (!has_default) { |
1808 | 117 | auto legacy_it = _params->default_value_of_src_slot.find(slot_id); |
1809 | 117 | if (legacy_it != std::end(_params->default_value_of_src_slot)) { |
1810 | 0 | if (!legacy_it->second.nodes.empty()) { |
1811 | 0 | RETURN_IF_ERROR(VExpr::create_expr_tree(legacy_it->second, ctx)); |
1812 | 0 | RETURN_IF_ERROR(ctx->prepare(_state, *_default_val_row_desc)); |
1813 | 0 | RETURN_IF_ERROR(ctx->open(_state)); |
1814 | 0 | } |
1815 | 0 | has_default = true; |
1816 | 0 | } |
1817 | 117 | } |
1818 | | |
1819 | 117 | if (has_default) { |
1820 | | // if expr is empty, the default value will be null |
1821 | 0 | _col_default_value_ctx.emplace(col_name, ctx); |
1822 | 0 | } |
1823 | 117 | } |
1824 | | |
1825 | | // Populate default_expr in each ColumnDescriptor from _col_default_value_ctx. |
1826 | | // This makes default values available to readers via column_descs, eliminating the |
1827 | | // need for the separate _generate_missing_columns roundtrip. |
1828 | 117 | for (auto& col_desc : _column_descs) { |
1829 | 117 | auto it = _col_default_value_ctx.find(col_desc.name); |
1830 | 117 | if (it != _col_default_value_ctx.end()) { |
1831 | 0 | col_desc.default_expr = it->second; |
1832 | 0 | } |
1833 | 117 | } |
1834 | | |
1835 | 16 | if (_is_load) { |
1836 | | // follow desc expr map is only for load task. |
1837 | 0 | bool has_slot_id_map = _params->__isset.dest_sid_to_src_sid_without_trans; |
1838 | 0 | int idx = 0; |
1839 | 0 | for (auto* slot_desc : _output_tuple_desc->slots()) { |
1840 | 0 | auto it = _params->expr_of_dest_slot.find(slot_desc->id()); |
1841 | 0 | if (it == std::end(_params->expr_of_dest_slot)) { |
1842 | 0 | return Status::InternalError("No expr for dest slot, id={}, name={}", |
1843 | 0 | slot_desc->id(), slot_desc->col_name()); |
1844 | 0 | } |
1845 | | |
1846 | 0 | VExprContextSPtr ctx; |
1847 | 0 | if (!it->second.nodes.empty()) { |
1848 | 0 | RETURN_IF_ERROR(VExpr::create_expr_tree(it->second, ctx)); |
1849 | 0 | RETURN_IF_ERROR(ctx->prepare(_state, *_src_row_desc)); |
1850 | 0 | RETURN_IF_ERROR(ctx->open(_state)); |
1851 | 0 | } |
1852 | 0 | _dest_vexpr_ctx.emplace_back(ctx); |
1853 | 0 | _dest_slot_name_to_idx[slot_desc->col_name()] = idx++; |
1854 | |
|
1855 | 0 | if (has_slot_id_map) { |
1856 | 0 | auto it1 = _params->dest_sid_to_src_sid_without_trans.find(slot_desc->id()); |
1857 | 0 | if (it1 == std::end(_params->dest_sid_to_src_sid_without_trans)) { |
1858 | 0 | _src_slot_descs_order_by_dest.emplace_back(nullptr); |
1859 | 0 | } else { |
1860 | 0 | auto _src_slot_it = full_src_slot_map.find(it1->second); |
1861 | 0 | if (_src_slot_it == std::end(full_src_slot_map)) { |
1862 | 0 | return Status::InternalError("No src slot {} in src slot descs", |
1863 | 0 | it1->second); |
1864 | 0 | } |
1865 | 0 | _dest_slot_to_src_slot_index.emplace(_src_slot_descs_order_by_dest.size(), |
1866 | 0 | full_src_index_map[_src_slot_it->first]); |
1867 | 0 | _src_slot_descs_order_by_dest.emplace_back(_src_slot_it->second); |
1868 | 0 | } |
1869 | 0 | } |
1870 | 0 | } |
1871 | 0 | } |
1872 | 16 | return Status::OK(); |
1873 | 16 | } |
1874 | | |
1875 | 3 | bool FileScanner::_should_enable_condition_cache() { |
1876 | 3 | DCHECK(_should_enable_condition_cache_handler != nullptr); |
1877 | 3 | if (_condition_cache_digest == 0 || !(this->*_should_enable_condition_cache_handler)()) { |
1878 | 3 | return false; |
1879 | 3 | } |
1880 | | |
1881 | | // Condition cache starts as all-false and is turned true only by native readers when a |
1882 | | // row-level predicate leaves at least one row in the granule. COUNT pushdown may replace the |
1883 | | // native reader with CountReader, which only emits row counts and never runs that marking path. |
1884 | 0 | if (_get_push_down_agg_type() == TPushAggOp::type::COUNT) { |
1885 | 0 | return false; |
1886 | 0 | } |
1887 | | |
1888 | | // The cache is populated by native readers while evaluating pushed-down predicates. |
1889 | | // Scanner-only predicates cannot mark reader granules, so there is nothing useful to cache. |
1890 | 0 | if (_push_down_conjuncts.empty()) { |
1891 | 0 | return false; |
1892 | 0 | } |
1893 | | |
1894 | | // Runtime filters are query-local dynamic predicates. Some ready RF implementations can hash |
1895 | | // their payload into get_digest(), but FileScanner cannot rely on that for all RFs reaching the |
1896 | | // native reader. In particular, ScanLocalState computes _condition_cache_digest during open(), |
1897 | | // while FileScanner may append late-arrival RFs in _process_late_arrival_conjuncts() |
1898 | | // immediately before initializing Parquet/ORC readers. |
1899 | | // |
1900 | | // Reading a weaker cache entry would be safe by itself: if a cached bitmap only represented |
1901 | | // static predicate P, false granules for P are also false for P AND RF. The unsafe part is |
1902 | | // writing. On cache miss, native readers mark survivor granules using all pushed-down |
1903 | | // predicates, including late RFs. Without a read-only cache mode, this would insert a bitmap for |
1904 | | // P AND RF under a digest that only represents P. |
1905 | | // |
1906 | | // Example: |
1907 | | // Q1 static predicate: k = 1, late RF payload: partition_key IN ('2024-02-01') |
1908 | | // Q2 static predicate: k = 1, late RF payload: partition_key IN ('2024-03-01') |
1909 | | // If both scans share the same file/range/digest, reusing Q1's bitmap for Q2 can skip row |
1910 | | // ranges according to the wrong RF payload. Keep RF predicate pushdown enabled for reader-side |
1911 | | // filtering, but do not persist its result in condition cache. |
1912 | 0 | return !_contains_runtime_filter(_conjuncts) && !_contains_runtime_filter(_push_down_conjuncts); |
1913 | 0 | } |
1914 | | |
1915 | 0 | bool FileScanner::_should_enable_condition_cache_for_load() const { |
1916 | 0 | return false; |
1917 | 0 | } |
1918 | | |
1919 | 0 | bool FileScanner::_should_enable_condition_cache_for_query() const { |
1920 | 0 | return true; |
1921 | 0 | } |
1922 | | |
1923 | 1 | bool FileScanner::_should_push_down_predicates(TFileFormatType::type format_type) const { |
1924 | 1 | DCHECK(_should_push_down_predicates_handler != nullptr); |
1925 | 1 | return (this->*_should_push_down_predicates_handler)(format_type); |
1926 | 1 | } |
1927 | | |
1928 | 1 | bool FileScanner::_should_push_down_predicates_for_load(TFileFormatType::type format_type) const { |
1929 | 1 | static_cast<void>(format_type); |
1930 | 1 | return false; |
1931 | 1 | } |
1932 | | |
1933 | 0 | bool FileScanner::_should_push_down_predicates_for_query(TFileFormatType::type format_type) const { |
1934 | | // JNI readers handle predicate conversion in their own paths. |
1935 | 0 | return format_type != TFileFormatType::FORMAT_JNI; |
1936 | 0 | } |
1937 | | |
1938 | 0 | void FileScanner::_init_reader_condition_cache() { |
1939 | 0 | _condition_cache = nullptr; |
1940 | 0 | _condition_cache_ctx = nullptr; |
1941 | |
|
1942 | 0 | if (!_should_enable_condition_cache() || !_cur_reader) { |
1943 | 0 | return; |
1944 | 0 | } |
1945 | | |
1946 | | // Disable condition cache when delete operations exist (e.g. Iceberg position/equality |
1947 | | // deletes, Hive ACID deletes). Cached granule results may become stale if delete files |
1948 | | // change between queries while the data file's cache key remains the same. |
1949 | 0 | if (_cur_reader->has_delete_operations()) { |
1950 | 0 | return; |
1951 | 0 | } |
1952 | | |
1953 | 0 | auto* cache = segment_v2::ConditionCache::instance(); |
1954 | 0 | _condition_cache_key = segment_v2::ConditionCache::ExternalCacheKey( |
1955 | 0 | _current_range.path, |
1956 | 0 | _current_range.__isset.modification_time ? _current_range.modification_time : 0, |
1957 | 0 | _current_range.__isset.file_size ? _current_range.file_size : -1, |
1958 | 0 | _condition_cache_digest, |
1959 | 0 | _current_range.__isset.start_offset ? _current_range.start_offset : 0, |
1960 | 0 | _current_range.__isset.size ? _current_range.size : -1); |
1961 | |
|
1962 | 0 | segment_v2::ConditionCacheHandle handle; |
1963 | 0 | auto condition_cache_hit = cache->lookup(_condition_cache_key, &handle); |
1964 | 0 | if (condition_cache_hit) { |
1965 | 0 | _condition_cache = handle.get_filter_result(); |
1966 | 0 | _condition_cache_hit_count++; |
1967 | 0 | } else { |
1968 | | // Allocate cache pre-sized to total number of granules. |
1969 | | // We add +1 as a safety margin: when a file is split across multiple scanners |
1970 | | // and the first row of this scanner's range is not aligned to a granule boundary, |
1971 | | // the data may span one more granule than ceil(total_rows / GRANULE_SIZE). |
1972 | | // The extra element costs only 1 bit and never affects correctness (an extra |
1973 | | // false-granule beyond the actual data range won't overlap any real row range). |
1974 | 0 | int64_t total_rows = _cur_reader->get_total_rows(); |
1975 | 0 | if (total_rows > 0) { |
1976 | 0 | size_t num_granules = (total_rows + ConditionCacheContext::GRANULE_SIZE - 1) / |
1977 | 0 | ConditionCacheContext::GRANULE_SIZE; |
1978 | 0 | _condition_cache = std::make_shared<std::vector<bool>>(num_granules + 1, false); |
1979 | 0 | } |
1980 | 0 | } |
1981 | |
|
1982 | 0 | if (_condition_cache) { |
1983 | | // Create context to pass to readers (native readers use it; non-native readers ignore it) |
1984 | 0 | _condition_cache_ctx = std::make_shared<ConditionCacheContext>(); |
1985 | 0 | _condition_cache_ctx->is_hit = condition_cache_hit; |
1986 | 0 | _condition_cache_ctx->filter_result = _condition_cache; |
1987 | 0 | _cur_reader->set_condition_cache_context(_condition_cache_ctx); |
1988 | 0 | } |
1989 | 0 | } |
1990 | | |
1991 | 3 | void FileScanner::_finalize_reader_condition_cache() { |
1992 | 3 | if (!_should_enable_condition_cache() || !_condition_cache_ctx || |
1993 | 3 | _condition_cache_ctx->is_hit) { |
1994 | 3 | _condition_cache = nullptr; |
1995 | 3 | _condition_cache_ctx = nullptr; |
1996 | 3 | return; |
1997 | 3 | } |
1998 | | // Only store the cache if the reader was fully consumed. If the scan was |
1999 | | // truncated early (e.g. by LIMIT), the cache is incomplete — unread granules |
2000 | | // would remain false and cause surviving rows to be incorrectly skipped on HIT. |
2001 | 0 | if (!_cur_reader_eof) { |
2002 | 0 | _condition_cache = nullptr; |
2003 | 0 | _condition_cache_ctx = nullptr; |
2004 | 0 | return; |
2005 | 0 | } |
2006 | | |
2007 | 0 | auto* cache = segment_v2::ConditionCache::instance(); |
2008 | 0 | cache->insert(_condition_cache_key, std::move(_condition_cache)); |
2009 | 0 | _condition_cache = nullptr; |
2010 | 0 | _condition_cache_ctx = nullptr; |
2011 | 0 | } |
2012 | | |
2013 | 2 | Status FileScanner::close(RuntimeState* state) { |
2014 | 2 | if (!_try_close()) { |
2015 | 0 | return Status::OK(); |
2016 | 0 | } |
2017 | | |
2018 | 2 | _finalize_reader_condition_cache(); |
2019 | | |
2020 | 2 | if (_cur_reader) { |
2021 | 0 | RETURN_IF_ERROR(_cur_reader->close()); |
2022 | 0 | } |
2023 | | |
2024 | 2 | RETURN_IF_ERROR(Scanner::close(state)); |
2025 | 2 | return Status::OK(); |
2026 | 2 | } |
2027 | | |
2028 | 0 | void FileScanner::try_stop() { |
2029 | 0 | Scanner::try_stop(); |
2030 | 0 | if (_io_ctx) { |
2031 | 0 | _io_ctx->should_stop = true; |
2032 | 0 | } |
2033 | 0 | } |
2034 | | |
2035 | 0 | void FileScanner::update_realtime_counters() { |
2036 | 0 | FileScanLocalState* local_state = static_cast<FileScanLocalState*>(_local_state); |
2037 | |
|
2038 | 0 | COUNTER_UPDATE(local_state->_scan_bytes, _file_reader_stats->read_bytes); |
2039 | 0 | COUNTER_UPDATE(local_state->_scan_rows, _file_reader_stats->read_rows); |
2040 | |
|
2041 | 0 | _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_rows( |
2042 | 0 | _file_reader_stats->read_rows); |
2043 | 0 | _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes( |
2044 | 0 | _file_reader_stats->read_bytes); |
2045 | |
|
2046 | 0 | int64_t delta_bytes_read_from_local = |
2047 | 0 | _file_cache_statistics->bytes_read_from_local - _last_bytes_read_from_local; |
2048 | 0 | int64_t delta_bytes_read_from_remote = |
2049 | 0 | _file_cache_statistics->bytes_read_from_remote - _last_bytes_read_from_remote; |
2050 | 0 | if (_file_cache_statistics->bytes_read_from_local == 0 && |
2051 | 0 | _file_cache_statistics->bytes_read_from_remote == 0) { |
2052 | 0 | _state->get_query_ctx() |
2053 | 0 | ->resource_ctx() |
2054 | 0 | ->io_context() |
2055 | 0 | ->update_scan_bytes_from_remote_storage(_file_reader_stats->read_bytes); |
2056 | 0 | DorisMetrics::instance()->query_scan_bytes_from_local->increment( |
2057 | 0 | _file_reader_stats->read_bytes); |
2058 | 0 | } else { |
2059 | 0 | _state->get_query_ctx()->resource_ctx()->io_context()->update_scan_bytes_from_local_storage( |
2060 | 0 | delta_bytes_read_from_local); |
2061 | 0 | _state->get_query_ctx() |
2062 | 0 | ->resource_ctx() |
2063 | 0 | ->io_context() |
2064 | 0 | ->update_scan_bytes_from_remote_storage(delta_bytes_read_from_remote); |
2065 | 0 | DorisMetrics::instance()->query_scan_bytes_from_local->increment( |
2066 | 0 | delta_bytes_read_from_local); |
2067 | 0 | DorisMetrics::instance()->query_scan_bytes_from_remote->increment( |
2068 | 0 | delta_bytes_read_from_remote); |
2069 | 0 | } |
2070 | |
|
2071 | 0 | COUNTER_UPDATE(_file_read_bytes_counter, _file_reader_stats->read_bytes); |
2072 | |
|
2073 | 0 | DorisMetrics::instance()->query_scan_bytes->increment(_file_reader_stats->read_bytes); |
2074 | 0 | DorisMetrics::instance()->query_scan_rows->increment(_file_reader_stats->read_rows); |
2075 | |
|
2076 | 0 | _file_reader_stats->read_bytes = 0; |
2077 | 0 | _file_reader_stats->read_rows = 0; |
2078 | |
|
2079 | 0 | _last_bytes_read_from_local = _file_cache_statistics->bytes_read_from_local; |
2080 | 0 | _last_bytes_read_from_remote = _file_cache_statistics->bytes_read_from_remote; |
2081 | 0 | } |
2082 | | |
2083 | 0 | void FileScanner::_collect_profile_before_close() { |
2084 | 0 | Scanner::_collect_profile_before_close(); |
2085 | 0 | if (config::enable_file_cache && _state->query_options().enable_file_cache && |
2086 | 0 | _profile != nullptr) { |
2087 | 0 | io::FileCacheProfileReporter cache_profile(_profile); |
2088 | 0 | cache_profile.update(_file_cache_statistics.get()); |
2089 | 0 | _state->get_query_ctx()->resource_ctx()->io_context()->update_bytes_write_into_cache( |
2090 | 0 | _file_cache_statistics->bytes_write_into_cache); |
2091 | 0 | } |
2092 | |
|
2093 | 0 | if (_cur_reader != nullptr) { |
2094 | 0 | _cur_reader->collect_profile_before_close(); |
2095 | 0 | } |
2096 | |
|
2097 | 0 | FileScanLocalState* local_state = static_cast<FileScanLocalState*>(_local_state); |
2098 | 0 | COUNTER_UPDATE(local_state->_scan_bytes, _file_reader_stats->read_bytes); |
2099 | 0 | COUNTER_UPDATE(local_state->_scan_rows, _file_reader_stats->read_rows); |
2100 | |
|
2101 | 0 | COUNTER_UPDATE(_file_read_bytes_counter, _file_reader_stats->read_bytes); |
2102 | 0 | COUNTER_UPDATE(_file_read_calls_counter, _file_reader_stats->read_calls); |
2103 | 0 | COUNTER_UPDATE(_file_read_time_counter, _file_reader_stats->read_time_ns); |
2104 | 0 | COUNTER_UPDATE(local_state->_condition_cache_hit_counter, _condition_cache_hit_count); |
2105 | 0 | if (_io_ctx) { |
2106 | 0 | COUNTER_UPDATE(local_state->_condition_cache_filtered_rows_counter, |
2107 | 0 | _io_ctx->condition_cache_filtered_rows); |
2108 | 0 | } |
2109 | |
|
2110 | 0 | DorisMetrics::instance()->query_scan_bytes->increment(_file_reader_stats->read_bytes); |
2111 | 0 | DorisMetrics::instance()->query_scan_rows->increment(_file_reader_stats->read_rows); |
2112 | 0 | } |
2113 | | |
2114 | | } // namespace doris |