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