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