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