be/src/format_v2/table_reader.h
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 | | #pragma once |
19 | | |
20 | | #include <bvar/status.h> |
21 | | |
22 | | #include <algorithm> |
23 | | #include <exception> |
24 | | #include <map> |
25 | | #include <memory> |
26 | | #include <optional> |
27 | | #include <span> |
28 | | #include <string> |
29 | | #include <string_view> |
30 | | #include <unordered_map> |
31 | | #include <utility> |
32 | | #include <vector> |
33 | | |
34 | | #include "common/cast_set.h" |
35 | | #include "common/exception.h" |
36 | | #include "common/logging.h" |
37 | | #include "common/status.h" |
38 | | #include "core/assert_cast.h" |
39 | | #include "core/block/block.h" |
40 | | #include "core/column/column_array.h" |
41 | | #include "core/column/column_const.h" |
42 | | #include "core/column/column_map.h" |
43 | | #include "core/column/column_nullable.h" |
44 | | #include "core/column/column_struct.h" |
45 | | #include "core/column/column_vector.h" |
46 | | #include "core/column/variant_v2/column_variant_v2.h" |
47 | | #include "core/data_type/data_type.h" |
48 | | #include "core/data_type/data_type_array.h" |
49 | | #include "core/data_type/data_type_map.h" |
50 | | #include "core/data_type/data_type_nullable.h" |
51 | | #include "core/data_type/data_type_number.h" |
52 | | #include "core/data_type/data_type_string.h" |
53 | | #include "core/data_type/data_type_struct.h" |
54 | | #include "core/field.h" |
55 | | #include "exec/common/stringop_substring.h" |
56 | | #include "exprs/function/function_variant_element_v2.h" |
57 | | #include "exprs/vexpr.h" |
58 | | #include "exprs/vexpr_context.h" |
59 | | #include "exprs/vexpr_fwd.h" |
60 | | #include "exprs/vslot_ref.h" |
61 | | #include "format/table/deletion_vector.h" |
62 | | #include "format_v2/column_data.h" |
63 | | #include "format_v2/column_mapper.h" |
64 | | #include "format_v2/expr/cast.h" |
65 | | #include "format_v2/expr/delete_predicate.h" |
66 | | #include "format_v2/file_reader.h" |
67 | | #include "format_v2/parquet/reader/column_reader.h" |
68 | | #include "format_v2/schema_projection.h" |
69 | | #include "gen_cpp/PlanNodes_types.h" |
70 | | #include "io/io_common.h" |
71 | | #include "runtime/descriptors.h" |
72 | | #include "storage/segment/condition_cache.h" |
73 | | |
74 | | namespace doris { |
75 | | class Block; |
76 | | struct DeleteFileDesc; |
77 | | class RuntimeState; |
78 | | } // namespace doris |
79 | | |
80 | | namespace doris::format { |
81 | | |
82 | | using DeleteRows = std::vector<int64_t>; |
83 | | |
84 | | // Row-level predicates on table/global schema. They are rewritten to file-local expressions when |
85 | | // possible, and remain the source of row-level filtering after localization. |
86 | | struct TableFilter { |
87 | | VExprContextSPtr conjunct; |
88 | | std::vector<GlobalIndex> global_indices; |
89 | | }; |
90 | | |
91 | | struct ScanTask { |
92 | 127k | virtual ~ScanTask() = default; |
93 | | |
94 | | std::unique_ptr<io::FileDescription> data_file; |
95 | | }; |
96 | | |
97 | | struct ProjectedColumnBuildContext { |
98 | | const TFileScanRangeParams* scan_params = nullptr; |
99 | | const TFileRangeDesc* range = nullptr; |
100 | | RuntimeState* runtime_state = nullptr; |
101 | | const SlotDescriptor* slot_desc = nullptr; |
102 | | std::optional<ColumnDefinition> schema_column = std::nullopt; |
103 | | size_t next_file_column_idx = 0; |
104 | | }; |
105 | | |
106 | | struct ReadProfile { |
107 | | RuntimeProfile::Counter* total_timer = nullptr; |
108 | | RuntimeProfile::Counter* init_timer = nullptr; |
109 | | RuntimeProfile::Counter* num_delete_files = nullptr; |
110 | | RuntimeProfile::Counter* num_delete_rows = nullptr; |
111 | | RuntimeProfile::Counter* parse_delete_file_time = nullptr; |
112 | | RuntimeProfile::Counter* decoded_dv_cache_hit_count = nullptr; |
113 | | RuntimeProfile::Counter* decoded_dv_cache_miss_count = nullptr; |
114 | | RuntimeProfile::Counter* dv_file_cache_hit_count = nullptr; |
115 | | RuntimeProfile::Counter* dv_file_cache_miss_count = nullptr; |
116 | | RuntimeProfile::Counter* dv_file_cache_peer_read_count = nullptr; |
117 | | RuntimeProfile::Counter* exec_timer = nullptr; |
118 | | RuntimeProfile::Counter* prepare_split_timer = nullptr; |
119 | | RuntimeProfile::Counter* finalize_timer = nullptr; |
120 | | RuntimeProfile::Counter* create_reader_timer = nullptr; |
121 | | RuntimeProfile::Counter* pushdown_agg_timer = nullptr; |
122 | | RuntimeProfile::Counter* open_reader_timer = nullptr; |
123 | | RuntimeProfile::Counter* runtime_filter_partition_prune_timer = nullptr; |
124 | | RuntimeProfile::Counter* runtime_filter_partition_pruned_range_counter = nullptr; |
125 | | RuntimeProfile::Counter* close_timer = nullptr; |
126 | | RuntimeProfile::Counter* file_reader_total_timer = nullptr; |
127 | | RuntimeProfile::Counter* file_reader_init_timer = nullptr; |
128 | | RuntimeProfile::Counter* file_reader_schema_timer = nullptr; |
129 | | RuntimeProfile::Counter* file_reader_mapper_timer = nullptr; |
130 | | RuntimeProfile::Counter* file_reader_open_timer = nullptr; |
131 | | RuntimeProfile::Counter* file_reader_get_block_timer = nullptr; |
132 | | RuntimeProfile::Counter* file_reader_aggregate_timer = nullptr; |
133 | | RuntimeProfile::Counter* file_reader_close_timer = nullptr; |
134 | | }; |
135 | | |
136 | | struct TableReadOptions { |
137 | | // Columns need to be read from file and output by table reader. They are all in table/global |
138 | | // schema semantics. |
139 | | const std::vector<ColumnDefinition> projected_columns; |
140 | | // All complex conjuncts from scan operator |
141 | | const VExprContextSPtrs conjuncts; |
142 | | // File format of the underlying data files, needed for reader initialization and reader-level |
143 | | // filter pushdown. |
144 | | const FileFormat format; |
145 | | TFileScanRangeParams* scan_params; |
146 | | std::shared_ptr<io::IOContext> io_ctx; |
147 | | RuntimeState* runtime_state; |
148 | | RuntimeProfile* scanner_profile; |
149 | | // File formats without complete self-describing metadata, such as CSV, Text, and JSON, need |
150 | | // the FE-planned physical file slots to build their file-local schema and deserialize values. |
151 | | const std::vector<SlotDescriptor*>* file_slot_descs = nullptr; |
152 | | // Push-down aggregate type. |
153 | | const TPushAggOp::type push_down_agg_type = TPushAggOp::type::NONE; |
154 | | // Table/global indices of explicit COUNT arguments. nullopt means an old FE did not send the |
155 | | // semantic argument field, while an explicit empty vector means COUNT(*)/COUNT(1). Keeping |
156 | | // those states separate prevents a rolling-upgrade plan from being reinterpreted by a new BE. |
157 | | const std::optional<std::vector<GlobalIndex>> push_down_count_columns = std::nullopt; |
158 | | // Initial digest of predicates available during scanner open. Scanner-driven splits override it |
159 | | // with SplitReadOptions::condition_cache_digest after collecting late-arrival runtime filters. |
160 | | // A zero digest disables condition cache. |
161 | | uint64_t condition_cache_digest = 0; |
162 | | }; |
163 | | |
164 | | struct SplitReadOptions { |
165 | | // Split-level information for reader initialization, which may include file path, partition values, delete file info, etc. The content is table format specific and opaque to table reader base class; it's the responsibility of the concrete table reader implementation to parse necessary information for reader initialization and filter pushdown. |
166 | | std::map<std::string, Field> partition_values; |
167 | | // Latest scanner conjuncts rewritten to table/global column indices. Runtime filters may |
168 | | // arrive after TableReader::init(), so scanner-driven splits replace the initial snapshot. |
169 | | // nullopt preserves the initial snapshot for standalone TableReader callers. |
170 | | std::optional<VExprContextSPtrs> conjuncts = std::nullopt; |
171 | | // Independent clones used for partition pruning because evaluation prepares and opens them |
172 | | // against a synthetic partition block before the file reader opens its row-level conjuncts. |
173 | | VExprContextSPtrs partition_prune_conjuncts; |
174 | | // Table-level COUNT may emit one metadata-derived batch and resume on a later scheduler turn. |
175 | | // It is safe only after every runtime filter assigned to the scanner has arrived; otherwise a |
176 | | // filter could arrive after synthetic rows have already been returned and those rows cannot be |
177 | | // retracted. Standalone TableReader callers have no scanner runtime-filter lifecycle. |
178 | | bool all_runtime_filters_applied = true; |
179 | | // Digest for the exact scanner conjunct snapshot attached to this split. FileScannerV2 rebuilds |
180 | | // it after collecting late-arrival RFs, so different RF payloads cannot share a cache entry. A |
181 | | // zero value explicitly disables condition cache for this split. |
182 | | std::optional<uint64_t> condition_cache_digest; |
183 | | ShardedKVCache* cache = nullptr; |
184 | | TFileRangeDesc current_range; |
185 | | FileFormat current_split_format = FileFormat::PARQUET; |
186 | | std::optional<GlobalRowIdContext> global_rowid_context; |
187 | | }; |
188 | | |
189 | | // Base class for table-level readers. |
190 | | // This layer owns common table-level orchestration, such as split iteration, dynamic partition |
191 | | // pruning, delete handling and conversion from file-local blocks to table-schema blocks. Concrete |
192 | | // table-format readers only need to provide format-specific hooks for opening readers and parsing |
193 | | // split metadata. |
194 | | class TableReader { |
195 | | public: |
196 | 60.2k | virtual ~TableReader() = default; |
197 | | |
198 | | // Initialize common runtime options for the table reader. Subclasses may call this from their |
199 | | // own init(options); table-format schema and split metadata are provided later per split. |
200 | | virtual Status init(TableReadOptions&& options); |
201 | | |
202 | | // FileScannerV2 adjusts this before each get_block() using an adaptive bytes-per-row estimate. |
203 | | // Store it here as well as forwarding to the current reader so newly opened split readers start |
204 | | // with the latest predicted batch size. |
205 | 493k | virtual void set_batch_size(size_t batch_size) { |
206 | 493k | _batch_size = std::max<size_t>(1, batch_size); |
207 | 493k | if (_data_reader.reader != nullptr) { |
208 | 108k | _data_reader.reader->set_batch_size(_batch_size); |
209 | 108k | } |
210 | 493k | } |
211 | | |
212 | | #ifdef BE_TEST |
213 | | size_t TEST_batch_size() const { return _batch_size; } |
214 | | void TEST_set_condition_cache_hit_count(int64_t hits) { _condition_cache_hit_count = hits; } |
215 | | bool TEST_current_data_file_is_immutable() const { |
216 | | DORIS_CHECK(_current_task != nullptr); |
217 | | DORIS_CHECK(_current_task->data_file != nullptr); |
218 | | DORIS_CHECK(_current_file_description.has_value()); |
219 | | DORIS_CHECK(_current_task->data_file->is_immutable == |
220 | | _current_file_description->is_immutable); |
221 | | return _current_task->data_file->is_immutable; |
222 | | } |
223 | | #endif |
224 | | |
225 | | // Prepare for reading a new split/task. |
226 | | // 1. Pass a new split/task to reader, which will be used in subsequent open_reader() to initialize the underlying file reader. |
227 | | // 2. Parse delete predicates from split/task information, which will be used for later dynamic filtering and delete handling. |
228 | | virtual Status prepare_split(const SplitReadOptions& options); |
229 | | |
230 | 223k | virtual bool current_split_pruned() const { return _current_split_pruned; } |
231 | 365k | virtual bool current_split_uses_metadata_count() const { |
232 | 365k | return _current_split_uses_metadata_count; |
233 | 365k | } |
234 | | |
235 | | // Largest logical input retained while materializing the last returned batch. FileScannerV2 |
236 | | // combines this with the final output width when learning its adaptive batch size. |
237 | 116k | size_t last_batch_materialization_input_bytes() const { |
238 | 116k | return _last_batch_materialization_input_bytes; |
239 | 116k | } |
240 | | |
241 | | // Discard the active split after the caller decides an error is ignorable, for example a |
242 | | // stale external-table file listing that returns NOT_FOUND. The next prepare_split() must start |
243 | | // with no concrete reader or split-local state left from the failed split. |
244 | 1 | virtual Status abort_split() { |
245 | | // Ignored open failures still spend time closing partially initialized readers. Include |
246 | | // that recovery path in the common lifecycle profile so NOT_FOUND cannot become invisible. |
247 | 1 | SCOPED_TIMER(_profile.total_timer); |
248 | 1 | SCOPED_TIMER(_profile.close_timer); |
249 | 1 | if (_data_reader.reader != nullptr) { |
250 | 1 | RETURN_IF_ERROR(close_current_reader()); |
251 | 1 | } else { |
252 | 0 | _current_task.reset(); |
253 | 0 | _current_file_description.reset(); |
254 | 0 | } |
255 | 1 | _delete_rows = nullptr; |
256 | 1 | _remaining_table_level_count = -1; |
257 | 1 | _remaining_file_level_count = -1; |
258 | 1 | _current_split_uses_metadata_count = false; |
259 | 1 | _current_split_pruned = false; |
260 | 1 | return Status::OK(); |
261 | 1 | } |
262 | | |
263 | | // Public entry point for reading a table-schema block. The base class opens the current reader, |
264 | | // advances across EOF, and closes exhausted readers. Subclasses provide protected hooks for |
265 | | // table-format-specific behavior. |
266 | 233k | virtual Status get_block(Block* block, bool* eos) { |
267 | 233k | SCOPED_TIMER(_profile.total_timer); |
268 | 233k | SCOPED_TIMER(_profile.exec_timer); |
269 | 233k | DORIS_CHECK(block->columns() == _projected_columns.size()); |
270 | 233k | block->clear_column_data(_projected_columns.size()); |
271 | 233k | _last_batch_materialization_input_bytes = 0; |
272 | | |
273 | 364k | while (true) { |
274 | 363k | if (*eos) { |
275 | 0 | return Status::OK(); |
276 | 0 | } |
277 | 363k | if (_io_ctx != nullptr && _io_ctx->should_stop) { |
278 | 14 | *eos = true; |
279 | 14 | return Status::OK(); |
280 | 14 | } |
281 | 363k | if (!_data_reader.reader) { |
282 | 243k | if (_is_table_level_count_active()) { |
283 | 314 | RETURN_IF_ERROR(_read_table_level_count(block, eos)); |
284 | 314 | return Status::OK(); |
285 | 314 | } |
286 | 243k | if (_is_file_level_count_active()) { |
287 | 2.95k | RETURN_IF_ERROR(_read_file_level_count(block, eos)); |
288 | 2.95k | return Status::OK(); |
289 | 2.95k | } |
290 | 240k | RETURN_IF_ERROR(create_next_reader(eos)); |
291 | 240k | if (!_data_reader.reader) { |
292 | 119k | DCHECK(*eos); |
293 | 119k | return Status::OK(); |
294 | 119k | } |
295 | 240k | } |
296 | | |
297 | | // Materialize a reduced row set for upper aggregate operators when aggregate |
298 | | // pushdown can be applied. This is not the final aggregate result: COUNT emits |
299 | | // `count` default rows for the upper COUNT(*), and MIN/MAX emits two rows containing |
300 | | // file-level min/max values for the upper MIN/MAX. |
301 | 240k | if (!_aggregate_pushdown_tried) { |
302 | 120k | SCOPED_TIMER(_profile.pushdown_agg_timer); |
303 | 120k | bool pushed_down = false; |
304 | 120k | const auto status = _try_materialize_aggregate_pushdown_rows(block, &pushed_down); |
305 | 120k | if (!status.ok()) { |
306 | 1 | if (_io_ctx != nullptr && _io_ctx->should_stop && |
307 | 1 | status.is<ErrorCode::END_OF_FILE>()) { |
308 | 1 | *eos = true; |
309 | 1 | return Status::OK(); |
310 | 1 | } |
311 | 0 | return status; |
312 | 1 | } |
313 | 120k | if (pushed_down) { |
314 | 1.50k | return Status::OK(); |
315 | 1.50k | } |
316 | 120k | } |
317 | | |
318 | 239k | bool current_eof = false; |
319 | 239k | _data_reader.block_template.clear_column_data( |
320 | 239k | cast_set<int64_t>(_data_reader.file_block_layout.size())); |
321 | 239k | size_t current_rows = 0; |
322 | 239k | { |
323 | 239k | SCOPED_TIMER(_profile.file_reader_total_timer); |
324 | 239k | SCOPED_TIMER(_profile.file_reader_get_block_timer); |
325 | 239k | RETURN_IF_ERROR(_data_reader.reader->get_block(&_data_reader.block_template, |
326 | 239k | ¤t_rows, ¤t_eof)); |
327 | 239k | } |
328 | 240k | const bool stopped_during_read = _io_ctx != nullptr && _io_ctx->should_stop; |
329 | 239k | if (current_rows == 0) { |
330 | 130k | if (current_eof) { |
331 | 119k | _current_reader_reached_eof = !stopped_during_read; |
332 | 119k | RETURN_IF_ERROR(close_current_reader()); |
333 | 119k | } |
334 | 130k | continue; |
335 | 130k | } |
336 | 239k | DCHECK_EQ(_data_reader.block_template.columns(), _data_reader.file_block_layout.size()) |
337 | 0 | << _data_reader.block_template.dump_structure(); |
338 | 108k | #ifndef NDEBUG |
339 | 108k | RETURN_IF_ERROR(_check_file_block_columns("after file reader get_block", current_rows)); |
340 | 108k | #endif |
341 | 108k | DORIS_CHECK(block->columns() == _data_reader.column_mapper->mappings().size()); |
342 | 108k | RETURN_IF_ERROR(finalize_chunk(block, current_rows)); |
343 | 108k | #ifndef NDEBUG |
344 | 108k | RETURN_IF_ERROR( |
345 | 108k | _check_table_block_columns("after finalize_chunk", block, current_rows)); |
346 | 108k | #endif |
347 | 108k | if (current_eof) { |
348 | 19 | _current_reader_reached_eof = !stopped_during_read; |
349 | 19 | RETURN_IF_ERROR(close_current_reader()); |
350 | 19 | } |
351 | 108k | return Status::OK(); |
352 | 108k | } |
353 | 233k | } |
354 | | |
355 | | // Close the table reader and the currently active file reader. Subclasses that hold additional |
356 | | // table-format resources should override this and call TableReader::close() first. |
357 | 53.8k | virtual Status close() { |
358 | 53.8k | SCOPED_TIMER(_profile.total_timer); |
359 | 53.8k | SCOPED_TIMER(_profile.close_timer); |
360 | 53.8k | if (_data_reader.reader) { |
361 | 648 | RETURN_IF_ERROR(close_current_reader()); |
362 | 648 | } |
363 | 53.8k | _current_task.reset(); |
364 | 53.8k | _current_file_description.reset(); |
365 | 53.8k | _remaining_table_level_count = -1; |
366 | 53.8k | _remaining_file_level_count = -1; |
367 | 53.8k | _current_split_uses_metadata_count = false; |
368 | 53.8k | return Status::OK(); |
369 | 53.8k | } |
370 | | |
371 | 106k | virtual int64_t condition_cache_hit_count() const { return _condition_cache_hit_count; } |
372 | | |
373 | | virtual std::string debug_string() const; |
374 | | |
375 | | virtual Status annotate_projected_column(const TFileScanSlotInfo& slot_info, |
376 | | ProjectedColumnBuildContext* context, |
377 | | ColumnDefinition* column) const; |
378 | | |
379 | 34.3k | virtual Status validate_projected_columns(const ProjectedColumnBuildContext& context) const { |
380 | 34.3k | (void)context; |
381 | 34.3k | return Status::OK(); |
382 | 34.3k | } |
383 | | |
384 | | protected: |
385 | | // TableReader keeps the active file description both in the scan task and separately for |
386 | | // creating the physical reader. Table-format readers must update both copies when their |
387 | | // snapshot protocol guarantees that a file path is never overwritten with different bytes. |
388 | | // This guarantee lets readers safely build cache keys without mtime; it must not be used for |
389 | | // ordinary Hive/TVF files whose paths may be overwritten in place. |
390 | 89.4k | void mark_current_data_file_immutable() { |
391 | 89.4k | DORIS_CHECK(_current_task != nullptr); |
392 | 89.4k | DORIS_CHECK(_current_task->data_file != nullptr); |
393 | 89.4k | DORIS_CHECK(_current_file_description.has_value()); |
394 | 89.4k | _current_task->data_file->is_immutable = true; |
395 | 89.4k | _current_file_description->is_immutable = true; |
396 | 89.4k | } |
397 | | |
398 | | std::optional<ColumnDefinition> _find_current_table_column_by_field_id(int32_t field_id, |
399 | | DataTypePtr type) const; |
400 | | |
401 | | // Parse deletion vector information from table format specific file description. |
402 | | virtual Status _parse_deletion_vector_file(const TTableFormatFileDesc& t_desc, |
403 | 37.6k | DeleteFileDesc* desc, bool* has_delete_file) { |
404 | 37.6k | *has_delete_file = false; |
405 | 37.6k | return Status::OK(); |
406 | 37.6k | } |
407 | | |
408 | | // Advance to the next reader. This closes the current reader first and then opens the next |
409 | | // concrete reader. Subclasses should not duplicate this loop. |
410 | | Status create_next_reader(bool* eos); |
411 | | virtual Status create_file_reader(std::unique_ptr<FileReader>* reader); |
412 | 6.46k | virtual TableColumnMappingMode mapping_mode() const { return TableColumnMappingMode::BY_NAME; } |
413 | 89.9k | virtual void configure_mapper_options(TableColumnMapperOptions*) const {} |
414 | 38.1k | virtual Status annotate_file_schema(std::vector<ColumnDefinition>* file_schema) { |
415 | 38.1k | DORIS_CHECK(file_schema != nullptr); |
416 | 38.1k | return Status::OK(); |
417 | 38.1k | } |
418 | | |
419 | | // Open the concrete reader for the current split/task and build the file-local scan request. |
420 | 121k | virtual Status open_reader() { |
421 | 121k | SCOPED_TIMER(_profile.open_reader_timer); |
422 | | // 1. Get file schema and create column mapping. |
423 | 121k | std::vector<ColumnDefinition> file_schema; |
424 | 121k | { |
425 | 121k | SCOPED_TIMER(_profile.file_reader_total_timer); |
426 | 121k | SCOPED_TIMER(_profile.file_reader_schema_timer); |
427 | 121k | RETURN_IF_ERROR(_data_reader.reader->get_schema(&file_schema)); |
428 | 121k | } |
429 | | // For Paimon/Hudi, FE can provide field ids through `history_schema_info`. Annotate the |
430 | | // file schema before column mapping when the table format maps columns by field id. |
431 | 121k | RETURN_IF_ERROR(annotate_file_schema(&file_schema)); |
432 | 121k | _data_reader.file_schema = file_schema; |
433 | 121k | _mapper_options.mode = mapping_mode(); |
434 | 121k | configure_mapper_options(&_mapper_options); |
435 | | |
436 | 121k | { |
437 | 121k | SCOPED_TIMER(_profile.file_reader_total_timer); |
438 | 121k | SCOPED_TIMER(_profile.file_reader_mapper_timer); |
439 | 121k | _data_reader.column_mapper = _data_reader.reader->create_column_mapper(_mapper_options); |
440 | 121k | } |
441 | 121k | DORIS_CHECK(_data_reader.column_mapper != nullptr); |
442 | 121k | RETURN_IF_ERROR(_data_reader.column_mapper->create_mapping(_projected_columns, |
443 | 121k | _partition_values, file_schema)); |
444 | 121k | DORIS_CHECK(_data_reader.column_mapper->mappings().size() == _projected_columns.size()); |
445 | | |
446 | | // 2. Build table filters based on conjuncts and column predicates. |
447 | 121k | RETURN_IF_ERROR(_build_table_filters_from_conjuncts()); |
448 | | |
449 | | // 3. Create file scan request based on column mapping and table filters, then open file |
450 | | // reader with the request. File scan request carries row-level expression filters and |
451 | | // file-level pruning hints. Only expression filters decide returned rows. |
452 | 121k | auto file_request = std::make_shared<FileScanRequest>(); |
453 | 121k | RETURN_IF_ERROR(_data_reader.column_mapper->create_scan_request( |
454 | 121k | _table_filters, _projected_columns, file_request.get(), _runtime_state)); |
455 | 121k | bool constant_filter_pruned_split = false; |
456 | 121k | RETURN_IF_ERROR(_evaluate_constant_filters(&constant_filter_pruned_split)); |
457 | 121k | if (constant_filter_pruned_split) { |
458 | 645 | RETURN_IF_ERROR(close_current_reader()); |
459 | 645 | return Status::OK(); |
460 | 645 | } |
461 | | // COUNT(*) has no semantic column argument, but Nereids retains a minimum-width scan slot |
462 | | // so the scan node still has an output tuple. Record only the current non-predicate file |
463 | | // columns before table-format hooks add row-position or equality-delete dependencies. This |
464 | | // marker is independent of aggregate eligibility: with position deletes, for example, |
465 | | // metadata COUNT must fall back to reading rows, but an arbitrary unsupported TIME_MILLIS |
466 | | // placeholder still must not be validated or decoded merely to carry the surviving count. |
467 | 121k | if (_push_down_agg_type == TPushAggOp::type::COUNT && |
468 | 121k | _push_down_count_columns.has_value() && _push_down_count_columns->empty()) { |
469 | 1.96k | file_request->count_star_placeholder_columns.reserve( |
470 | 1.96k | file_request->non_predicate_columns.size()); |
471 | 1.96k | for (const auto& column : file_request->non_predicate_columns) { |
472 | 1.94k | file_request->count_star_placeholder_columns.push_back(column.column_id()); |
473 | 1.94k | } |
474 | 1.96k | } |
475 | 121k | RETURN_IF_ERROR(customize_file_scan_request(file_request.get())); |
476 | 121k | RETURN_IF_ERROR(_open_local_filter_exprs(*file_request)); |
477 | 121k | _data_reader.file_block_layout.clear(); |
478 | 121k | _data_reader.block_template.clear(); |
479 | 121k | _data_reader.file_block_layout.resize(file_request->local_positions.size()); |
480 | | |
481 | | // 4. Build file block layout from file schema and column mapping. The layout describes |
482 | | // the block returned by file reader before table-column materialization. |
483 | 527k | for (const auto& [file_column_id, block_position] : file_request->local_positions) { |
484 | 527k | DORIS_CHECK(block_position.value() < _data_reader.file_block_layout.size()); |
485 | 527k | const auto* field = _find_column_definition(_data_reader.file_schema, file_column_id); |
486 | 527k | DORIS_CHECK(field != nullptr); |
487 | | |
488 | 527k | ColumnDefinition projected_field; |
489 | 527k | { |
490 | 527k | auto it = std::find_if( |
491 | 527k | file_request->non_predicate_columns.begin(), |
492 | 527k | file_request->non_predicate_columns.end(), |
493 | 8.86M | [&](const LocalColumnIndex& p) { return p.column_id() == file_column_id; }); |
494 | 527k | if (it != file_request->non_predicate_columns.end()) { |
495 | 438k | RETURN_IF_ERROR(project_column_definition(*field, *it, &projected_field)); |
496 | 438k | } |
497 | 527k | } |
498 | 527k | { |
499 | 527k | auto it = std::find_if( |
500 | 527k | file_request->predicate_columns.begin(), |
501 | 527k | file_request->predicate_columns.end(), |
502 | 527k | [&](const LocalColumnIndex& p) { return p.column_id() == file_column_id; }); |
503 | 527k | if (it != file_request->predicate_columns.end()) { |
504 | 89.9k | RETURN_IF_ERROR(project_column_definition(*field, *it, &projected_field)); |
505 | 89.9k | } |
506 | 527k | } |
507 | 527k | _data_reader.file_block_layout[block_position.value()] = { |
508 | 527k | .file_column_id = file_column_id, |
509 | 527k | .name = projected_field.name, |
510 | 527k | .type = projected_field.type, |
511 | 527k | }; |
512 | 527k | DORIS_CHECK(_data_reader.file_block_layout[block_position.value()].type != nullptr); |
513 | 527k | } |
514 | | |
515 | | // 5. Prepare block template from file block layout. The block template stores the block |
516 | | // returned by file reader before table-column materialization. |
517 | 121k | _data_reader.block_template.reserve(_data_reader.file_block_layout.size()); |
518 | 527k | for (const auto& column : _data_reader.file_block_layout) { |
519 | 527k | _data_reader.block_template.insert( |
520 | 527k | {column.type->create_column(), column.type, column.name}); |
521 | 527k | } |
522 | 121k | if (VLOG_DEBUG_IS_ON) { |
523 | 0 | VLOG_DEBUG << "TableReader debug: " << debug_string(); |
524 | 0 | } |
525 | 121k | RETURN_IF_ERROR(_open_mapping_exprs()); |
526 | 121k | { |
527 | 121k | SCOPED_TIMER(_profile.file_reader_total_timer); |
528 | 121k | SCOPED_TIMER(_profile.file_reader_open_timer); |
529 | 121k | RETURN_IF_ERROR(_data_reader.reader->open(file_request)); |
530 | 121k | } |
531 | 121k | RETURN_IF_ERROR(_init_reader_condition_cache(*file_request)); |
532 | 121k | return Status::OK(); |
533 | 121k | } |
534 | | |
535 | | Status _build_table_filters_from_conjuncts(); |
536 | | Status _evaluate_partition_prune_conjuncts(const VExprContextSPtrs& conjuncts, |
537 | | bool* can_filter_all); |
538 | | static bool _is_safe_to_pre_execute(const VExprContextSPtr& conjunct); |
539 | | Status _build_partition_prune_block(Block* block) const; |
540 | | Status _open_local_filter_exprs(const FileScanRequest& file_request); |
541 | | Status _init_reader_condition_cache(const FileScanRequest& file_request); |
542 | | void _finalize_reader_condition_cache(); |
543 | | bool _should_enable_condition_cache(const FileScanRequest& file_request) const; |
544 | | |
545 | 121k | Status _evaluate_constant_filters(bool* can_filter_all) { |
546 | 121k | DORIS_CHECK(can_filter_all != nullptr); |
547 | 121k | DORIS_CHECK_LE(_constant_pruning_safe_filter_count, _table_filters.size()); |
548 | 121k | *can_filter_all = false; |
549 | | // The bound was derived from the original `_conjuncts` order, which includes slotless |
550 | | // expressions omitted from `_table_filters`. Iterating only this prefix therefore cannot |
551 | | // skip an unsafe row-level predicate and pre-execute a later constant predicate. |
552 | 189k | for (size_t i = 0; i < _constant_pruning_safe_filter_count; ++i) { |
553 | 68.1k | const auto& table_filter = _table_filters[i]; |
554 | 68.1k | if (table_filter.conjunct == nullptr) { |
555 | 0 | continue; |
556 | 0 | } |
557 | 68.1k | DORIS_CHECK(_is_safe_to_pre_execute(table_filter.conjunct)); |
558 | | // RuntimeFilterExpr does not implement execute_column_impl(); it is evaluated by the |
559 | | // row-level filter path through execute_filter(). Constant split pruning uses |
560 | | // VExprContext::execute() on a one-row synthetic block, so runtime filters must not be |
561 | | // pre-executed here even when their referenced slot maps to a constant value. |
562 | 68.1k | if (table_filter.conjunct->root()->is_rf_wrapper() || |
563 | 68.1k | !_table_filter_has_only_constant_entries(table_filter)) { |
564 | 64.3k | continue; |
565 | 64.3k | } |
566 | 3.78k | Block eval_block; |
567 | 3.78k | RETURN_IF_ERROR(_build_constant_filter_block(table_filter, &eval_block)); |
568 | 3.78k | RowDescriptor row_desc; |
569 | 3.78k | RETURN_IF_ERROR(table_filter.conjunct->prepare(_runtime_state, row_desc)); |
570 | 3.78k | RETURN_IF_ERROR(table_filter.conjunct->open(_runtime_state)); |
571 | 3.78k | int result_column_id = -1; |
572 | 3.78k | RETURN_IF_ERROR(table_filter.conjunct->execute(&eval_block, &result_column_id)); |
573 | 3.78k | DORIS_CHECK(result_column_id >= 0); |
574 | 3.78k | if (_filter_result_filters_all(eval_block.get_by_position(result_column_id).column)) { |
575 | 645 | *can_filter_all = true; |
576 | 645 | return Status::OK(); |
577 | 645 | } |
578 | 3.78k | } |
579 | 120k | return Status::OK(); |
580 | 121k | } |
581 | | |
582 | 61.1k | bool _table_filter_has_only_constant_entries(const TableFilter& table_filter) const { |
583 | 61.1k | const auto& filter_entries = _data_reader.column_mapper->filter_entries(); |
584 | 61.2k | for (const auto global_index : table_filter.global_indices) { |
585 | 61.2k | const auto entry_it = filter_entries.find(global_index); |
586 | 61.4k | if (entry_it == filter_entries.end() || !entry_it->second.is_constant()) { |
587 | 57.5k | return false; |
588 | 57.5k | } |
589 | 61.2k | } |
590 | 3.59k | return !table_filter.global_indices.empty(); |
591 | 61.1k | } |
592 | | |
593 | 3.81k | Status _build_constant_filter_block(const TableFilter& table_filter, Block* eval_block) { |
594 | 3.81k | DORIS_CHECK(eval_block != nullptr); |
595 | 3.81k | eval_block->clear(); |
596 | 3.81k | const auto& mappings = _data_reader.column_mapper->mappings(); |
597 | 3.81k | const auto& filter_entries = _data_reader.column_mapper->filter_entries(); |
598 | 3.81k | DORIS_CHECK(mappings.size() == _projected_columns.size()); |
599 | 15.1k | for (size_t column_idx = 0; column_idx < mappings.size(); ++column_idx) { |
600 | 11.3k | const auto global_index = GlobalIndex(column_idx); |
601 | 11.3k | const auto& mapping = mappings[column_idx]; |
602 | 11.3k | const auto entry_it = filter_entries.find(global_index); |
603 | 11.3k | const bool referenced_by_filter = |
604 | 11.3k | std::find(table_filter.global_indices.begin(), |
605 | 11.3k | table_filter.global_indices.end(), |
606 | 11.3k | global_index) != table_filter.global_indices.end(); |
607 | 11.3k | if (referenced_by_filter && entry_it != filter_entries.end() && |
608 | 11.3k | entry_it->second.is_constant()) { |
609 | 3.86k | ColumnPtr constant_column; |
610 | 3.86k | RETURN_IF_ERROR(_materialize_constant_filter_column( |
611 | 3.86k | entry_it->second.constant_index(), &constant_column)); |
612 | 3.86k | eval_block->insert({std::move(constant_column), mapping.table_type, |
613 | 3.86k | mapping.table_column_name}); |
614 | 7.45k | } else { |
615 | 7.45k | eval_block->insert({mapping.table_type->create_column_const_with_default_value(1), |
616 | 7.45k | mapping.table_type, mapping.table_column_name}); |
617 | 7.45k | } |
618 | 11.3k | } |
619 | 3.81k | return Status::OK(); |
620 | 3.81k | } |
621 | | |
622 | 3.87k | Status _materialize_constant_filter_column(ConstantIndex constant_index, ColumnPtr* column) { |
623 | 3.87k | DORIS_CHECK(column != nullptr); |
624 | 3.87k | const auto& constant_entry = _data_reader.column_mapper->constant_map().get(constant_index); |
625 | 3.87k | DORIS_CHECK(constant_entry.expr != nullptr); |
626 | 3.87k | DORIS_CHECK(constant_entry.type != nullptr); |
627 | 3.87k | RowDescriptor row_desc; |
628 | 3.87k | RETURN_IF_ERROR(constant_entry.expr->prepare(_runtime_state, row_desc)); |
629 | 3.87k | RETURN_IF_ERROR(constant_entry.expr->open(_runtime_state)); |
630 | 3.87k | Block eval_block; |
631 | 3.87k | eval_block.insert({constant_entry.type->create_column_const_with_default_value(1), |
632 | 3.87k | constant_entry.type, "__table_reader_constant_filter"}); |
633 | 3.87k | int result_column_id = -1; |
634 | 3.87k | RETURN_IF_ERROR(constant_entry.expr->execute(&eval_block, &result_column_id)); |
635 | 3.87k | DORIS_CHECK(result_column_id >= 0); |
636 | 3.87k | *column = eval_block.get_by_position(result_column_id).column; |
637 | 3.87k | DORIS_CHECK((*column)->size() == 1); |
638 | 3.87k | return Status::OK(); |
639 | 3.87k | } |
640 | | |
641 | 3.81k | static bool _filter_result_filters_all(const ColumnPtr& filter_column) { |
642 | 3.81k | DORIS_CHECK(filter_column.get() != nullptr); |
643 | 3.81k | DORIS_CHECK(filter_column->size() == 1); |
644 | 3.81k | return !filter_column->get_bool(0); |
645 | 3.81k | } |
646 | | |
647 | 120k | virtual Status customize_file_scan_request(FileScanRequest* file_request) { |
648 | 120k | return _append_delete_predicate(file_request); |
649 | 120k | } |
650 | | |
651 | 423k | bool _is_table_level_count_active() const { return _remaining_table_level_count >= 0; } |
652 | | |
653 | 242k | bool _is_file_level_count_active() const { return _remaining_file_level_count >= 0; } |
654 | | |
655 | 3.19k | Status _materialize_count_rows(size_t rows, Block* block) const { |
656 | 3.19k | DORIS_CHECK(block != nullptr); |
657 | 3.19k | DORIS_CHECK(block->columns() > 0 || rows == 0); |
658 | 6.38k | for (size_t column_idx = 0; column_idx < block->columns(); ++column_idx) { |
659 | 3.19k | auto column = block->get_by_position(column_idx).type->create_column(); |
660 | 3.19k | if (auto* nullable = check_and_get_column<ColumnNullable>(*column)) { |
661 | | // Metadata COUNT emits synthetic input rows for the unchanged upper aggregate. |
662 | | // They must be non-NULL for COUNT(nullable_col), and constructing them explicitly |
663 | | // also keeps every nullable null map boolean-valid in debug/ASAN block checks. |
664 | 3.19k | nullable->get_nested_column().insert_many_defaults(rows); |
665 | 3.19k | nullable->get_null_map_data().resize_fill(rows, 0); |
666 | 3.19k | } else { |
667 | 2 | column->insert_many_defaults(rows); |
668 | 2 | } |
669 | 3.19k | block->replace_by_position(column_idx, std::move(column)); |
670 | 3.19k | } |
671 | 3.19k | return Status::OK(); |
672 | 3.19k | } |
673 | | |
674 | 3.19k | Status _materialize_next_count_batch(int64_t* remaining_rows, Block* block) const { |
675 | 3.19k | DORIS_CHECK(remaining_rows != nullptr); |
676 | 3.19k | DORIS_CHECK(*remaining_rows > 0); |
677 | 3.19k | const int64_t batch_size = _runtime_state == nullptr |
678 | 3.19k | ? *remaining_rows |
679 | 3.19k | : static_cast<int64_t>(_runtime_state->batch_size()); |
680 | 3.19k | const auto rows = std::min(*remaining_rows, batch_size); |
681 | 3.19k | RETURN_IF_ERROR(_materialize_count_rows(cast_set<size_t>(rows), block)); |
682 | 3.19k | *remaining_rows -= rows; |
683 | 3.19k | return Status::OK(); |
684 | 3.19k | } |
685 | | |
686 | 3.40k | Status _read_count_batch(int64_t* remaining_rows, Block* block, bool* eos) { |
687 | 3.40k | DORIS_CHECK(block != nullptr); |
688 | 3.40k | DORIS_CHECK(eos != nullptr); |
689 | 3.40k | DORIS_CHECK(_push_down_agg_type == TPushAggOp::type::COUNT); |
690 | 3.40k | DORIS_CHECK(remaining_rows != nullptr); |
691 | 3.40k | DORIS_CHECK(*remaining_rows >= 0); |
692 | 3.40k | if (*remaining_rows == 0) { |
693 | 1.71k | *remaining_rows = -1; |
694 | 1.71k | _current_task.reset(); |
695 | 1.71k | *eos = true; |
696 | 1.71k | return Status::OK(); |
697 | 1.71k | } |
698 | 1.69k | RETURN_IF_ERROR(_materialize_next_count_batch(remaining_rows, block)); |
699 | 1.69k | *eos = false; |
700 | 1.69k | return Status::OK(); |
701 | 1.69k | } |
702 | | |
703 | 454 | Status _read_table_level_count(Block* block, bool* eos) { |
704 | 454 | return _read_count_batch(&_remaining_table_level_count, block, eos); |
705 | 454 | } |
706 | | |
707 | 2.95k | Status _read_file_level_count(Block* block, bool* eos) { |
708 | 2.95k | return _read_count_batch(&_remaining_file_level_count, block, eos); |
709 | 2.95k | } |
710 | | |
711 | | void _append_file_scan_column(FileScanRequest* request, LocalColumnId column_id, |
712 | 34.7k | std::vector<LocalColumnIndex>* scan_columns) { |
713 | 34.7k | DORIS_CHECK(request != nullptr); |
714 | 34.7k | DORIS_CHECK(scan_columns != nullptr); |
715 | 34.7k | FileScanRequestBuilder builder(request); |
716 | 34.7k | Status status; |
717 | 34.7k | if (scan_columns == &request->predicate_columns) { |
718 | 32.6k | status = builder.add_predicate_column(column_id); |
719 | 32.6k | } else { |
720 | 2.18k | DORIS_CHECK(scan_columns == &request->non_predicate_columns); |
721 | 2.18k | status = builder.add_non_predicate_column(column_id); |
722 | 2.18k | } |
723 | 34.7k | DORIS_CHECK(status.ok()) << status.to_string(); |
724 | 34.7k | if (column_id == LocalColumnId(ROW_POSITION_COLUMN_ID) && |
725 | 34.7k | _find_column_definition(_data_reader.file_schema, column_id) == nullptr) { |
726 | 29.3k | _data_reader.file_schema.push_back(row_position_column_definition()); |
727 | 29.3k | } |
728 | 34.7k | } |
729 | | |
730 | | // Append DeletePredicate to file scan request if there are deletes. The predicate will be evaluated in file reader level and filter out deleted rows before returning data to table reader. |
731 | 120k | Status _append_delete_predicate(FileScanRequest* request) { |
732 | 120k | DORIS_CHECK(request != nullptr); |
733 | 120k | if ((_delete_rows == nullptr || _delete_rows->empty()) && |
734 | 120k | (_deletion_vector == nullptr || _deletion_vector->isEmpty())) { |
735 | 92.8k | return Status::OK(); |
736 | 92.8k | } |
737 | 27.4k | const auto row_position_column_id = LocalColumnId(ROW_POSITION_COLUMN_ID); |
738 | 27.4k | _append_file_scan_column(request, row_position_column_id, &request->predicate_columns); |
739 | | |
740 | 27.4k | const auto block_position = request->local_positions.at(row_position_column_id); |
741 | 27.5k | auto append_predicate = [&](auto& deleted_rows) { |
742 | 27.5k | auto delete_predicate = std::make_shared<DeletePredicate>(deleted_rows); |
743 | 27.5k | delete_predicate->add_child(VSlotRef::create_shared( |
744 | 27.5k | cast_set<int>(block_position.value()), cast_set<int>(block_position.value()), |
745 | 27.5k | -1, std::make_shared<DataTypeInt64>(), ROW_POSITION_COLUMN_NAME)); |
746 | 27.5k | request->delete_conjuncts.push_back( |
747 | 27.5k | VExprContext::create_shared(std::move(delete_predicate))); |
748 | 27.5k | }; _ZZN5doris6format11TableReader24_append_delete_predicateEPNS0_15FileScanRequestEENKUlRT_E_clISt6vectorIlSaIlEEEEDaS5_ Line | Count | Source | 741 | 1.81k | auto append_predicate = [&](auto& deleted_rows) { | 742 | 1.81k | auto delete_predicate = std::make_shared<DeletePredicate>(deleted_rows); | 743 | 1.81k | delete_predicate->add_child(VSlotRef::create_shared( | 744 | 1.81k | cast_set<int>(block_position.value()), cast_set<int>(block_position.value()), | 745 | 1.81k | -1, std::make_shared<DataTypeInt64>(), ROW_POSITION_COLUMN_NAME)); | 746 | 1.81k | request->delete_conjuncts.push_back( | 747 | 1.81k | VExprContext::create_shared(std::move(delete_predicate))); | 748 | 1.81k | }; |
_ZZN5doris6format11TableReader24_append_delete_predicateEPNS0_15FileScanRequestEENKUlRT_E_clIN7roaring12Roaring64MapEEEDaS5_ Line | Count | Source | 741 | 25.7k | auto append_predicate = [&](auto& deleted_rows) { | 742 | 25.7k | auto delete_predicate = std::make_shared<DeletePredicate>(deleted_rows); | 743 | 25.7k | delete_predicate->add_child(VSlotRef::create_shared( | 744 | 25.7k | cast_set<int>(block_position.value()), cast_set<int>(block_position.value()), | 745 | 25.7k | -1, std::make_shared<DataTypeInt64>(), ROW_POSITION_COLUMN_NAME)); | 746 | 25.7k | request->delete_conjuncts.push_back( | 747 | 25.7k | VExprContext::create_shared(std::move(delete_predicate))); | 748 | 25.7k | }; |
|
749 | 27.4k | if (_delete_rows != nullptr && !_delete_rows->empty()) { |
750 | 1.81k | append_predicate(*_delete_rows); |
751 | 1.81k | } |
752 | 27.4k | if (_deletion_vector != nullptr && !_deletion_vector->isEmpty()) { |
753 | 25.8k | append_predicate(*_deletion_vector); |
754 | 25.8k | } |
755 | 27.4k | return Status::OK(); |
756 | 120k | } |
757 | | |
758 | | // Close the current concrete reader. This hook is called by both create_next_reader() and |
759 | | // close(), so it should remain idempotent. |
760 | 121k | virtual Status close_current_reader() { |
761 | 121k | _finalize_reader_condition_cache(); |
762 | 121k | { |
763 | 121k | SCOPED_TIMER(_profile.file_reader_total_timer); |
764 | 121k | SCOPED_TIMER(_profile.file_reader_close_timer); |
765 | 121k | RETURN_IF_ERROR(_data_reader.reader->close()); |
766 | 121k | } |
767 | 121k | _data_reader.reader.reset(); |
768 | 121k | if (_data_reader.column_mapper != nullptr) { |
769 | 121k | _data_reader.column_mapper->clear(); |
770 | 121k | _data_reader.column_mapper.reset(); |
771 | 121k | } |
772 | 121k | _table_filters.clear(); |
773 | 121k | _constant_pruning_safe_filter_count = 0; |
774 | 121k | _data_reader.file_schema.clear(); |
775 | 121k | _data_reader.file_block_layout.clear(); |
776 | 121k | _data_reader.block_template.clear(); |
777 | 121k | _current_task.reset(); |
778 | 121k | _current_file_description.reset(); |
779 | 121k | _current_reader_reached_eof = false; |
780 | 121k | return Status::OK(); |
781 | 121k | } |
782 | | |
783 | 7.11k | void _record_scan_rows(size_t rows) { |
784 | 7.11k | if (_io_ctx != nullptr && _io_ctx->file_reader_stats != nullptr) { |
785 | 7.11k | _io_ctx->file_reader_stats->read_rows += rows; |
786 | 7.11k | } |
787 | 7.11k | } |
788 | | |
789 | | // Finalize file-local block to table/global schema block. |
790 | 109k | Status finalize_chunk(Block* block, const size_t rows) { |
791 | 109k | SCOPED_TIMER(_profile.finalize_timer); |
792 | 109k | const auto& mappings = _data_reader.column_mapper->mappings(); |
793 | 109k | const size_t file_column_count = _data_reader.block_template.columns(); |
794 | 109k | const bool has_variant_path = std::ranges::any_of( |
795 | 109k | mappings, |
796 | 588k | [](const ColumnMapping& mapping) { return !mapping.column_paths.empty(); }); |
797 | | // Record before any projection transfers a carrier out of the file-local block. Current |
798 | | // Variant-aware Parquet readers may return a full root even when only a tiny Path Slot is |
799 | | // requested; future physical pruning automatically lowers this sample. |
800 | 109k | _last_batch_materialization_input_bytes = |
801 | 109k | has_variant_path ? _data_reader.block_template.bytes() : 0; |
802 | 109k | auto materialize_mapping = [&](size_t mapping_index, |
803 | 587k | bool take_projection_result) -> Status { |
804 | 587k | ColumnPtr column; |
805 | 587k | RETURN_IF_ERROR(_materialize_mapping_column(mappings[mapping_index], |
806 | 587k | &_data_reader.block_template, rows, &column, |
807 | 587k | take_projection_result)); |
808 | | // Materialization order is internal only. Always write the original global index |
809 | | // position so the table/global output schema remains unchanged. |
810 | 587k | block->replace_by_position(mapping_index, IColumn::mutate(std::move(column))); |
811 | 587k | return Status::OK(); |
812 | 587k | }; |
813 | | |
814 | 109k | if (!has_variant_path) { |
815 | | // Keep the common path identical to the original linear finalization. Building a |
816 | | // carrier ownership plan is only necessary when a Path Slot shares its root source. |
817 | 697k | for (size_t mapping_index = 0; mapping_index < mappings.size(); ++mapping_index) { |
818 | 588k | RETURN_IF_ERROR( |
819 | 588k | materialize_mapping(mapping_index, mapping_index + 1 == mappings.size())); |
820 | 588k | } |
821 | 18.4E | } else { |
822 | | // Record the last consumer in execution order once per chunk. This avoids scanning all |
823 | | // mappings for every output column when a Path Slot is present. |
824 | 18.4E | std::unordered_map<int32_t, size_t> last_file_column_consumers; |
825 | 18.4E | last_file_column_consumers.reserve(mappings.size()); |
826 | 18.4E | auto record_phase = [&](bool record_paths) { |
827 | 22 | for (size_t mapping_index = 0; mapping_index < mappings.size(); ++mapping_index) { |
828 | 14 | const auto& mapping = mappings[mapping_index]; |
829 | 14 | if ((!mapping.column_paths.empty()) != record_paths || |
830 | 14 | !mapping.file_local_id.has_value() || mapping.projection == nullptr) { |
831 | 7 | continue; |
832 | 7 | } |
833 | 7 | last_file_column_consumers[*mapping.file_local_id] = mapping_index; |
834 | 7 | } |
835 | 8 | }; |
836 | | // Every Path Slot reads its shared carrier before any root/normal mapping can transfer |
837 | | // that carrier out of the file block. |
838 | 18.4E | record_phase(true); |
839 | 18.4E | record_phase(false); |
840 | | |
841 | 18.4E | auto materialize_phase = [&](bool materialize_paths) -> Status { |
842 | 22 | for (size_t mapping_index = 0; mapping_index < mappings.size(); ++mapping_index) { |
843 | 14 | const auto& mapping = mappings[mapping_index]; |
844 | 14 | if ((!mapping.column_paths.empty()) != materialize_paths) { |
845 | 7 | continue; |
846 | 7 | } |
847 | 7 | bool take_projection_result = false; |
848 | 7 | if (mapping.file_local_id.has_value() && mapping.projection != nullptr) { |
849 | 7 | const auto last_consumer_it = |
850 | 7 | last_file_column_consumers.find(*mapping.file_local_id); |
851 | 7 | DORIS_CHECK(last_consumer_it != last_file_column_consumers.end()); |
852 | 7 | take_projection_result = last_consumer_it->second == mapping_index; |
853 | 7 | } |
854 | 7 | RETURN_IF_ERROR(materialize_mapping(mapping_index, take_projection_result)); |
855 | 7 | } |
856 | 8 | return Status::OK(); |
857 | 8 | }; |
858 | 18.4E | RETURN_IF_ERROR(materialize_phase(true)); |
859 | 18.4E | RETURN_IF_ERROR(materialize_phase(false)); |
860 | 18.4E | } |
861 | | // Expressions can append temporary result columns, while carrier transfer leaves same-row |
862 | | // constant placeholders so later expressions retain Block::rows(). Remove the temporary |
863 | | // results and restore only transferred file columns before the next physical batch. Other |
864 | | // file columns keep their allocated buffers for clear_column_data() to reuse. |
865 | 109k | _data_reader.block_template.erase_tail(file_column_count); |
866 | 681k | for (size_t idx = 0; idx < file_column_count; ++idx) { |
867 | 572k | auto& file_column = _data_reader.block_template.get_by_position(idx); |
868 | 572k | if (is_column_const(*file_column.column)) { |
869 | 86.0k | DORIS_CHECK(file_column.type != nullptr); |
870 | 86.0k | file_column.column = file_column.type->create_column(); |
871 | 86.0k | } |
872 | 572k | } |
873 | 109k | RETURN_IF_ERROR(materialize_virtual_columns(block)); |
874 | | // Enforce CHAR/VARCHAR length declared by the table schema after all file-to-table |
875 | | // materialization has finished. |
876 | 109k | RETURN_IF_ERROR(_truncate_char_or_varchar_columns(block)); |
877 | 109k | return Status::OK(); |
878 | 109k | } |
879 | | |
880 | | // Materialize virtual columns in the table block, such as Iceberg _row_id and |
881 | | // _last_updated_sequence_number. This runs after normal column materialization so finalize |
882 | | // expressions can reference those virtual columns. |
883 | 79.4k | virtual Status materialize_virtual_columns(Block* table_block) { return Status::OK(); } |
884 | | |
885 | | #ifndef NDEBUG |
886 | 109k | Status _check_file_block_columns(std::string_view stage, size_t rows) { |
887 | 109k | DORIS_CHECK(_data_reader.block_template.columns() == _data_reader.file_block_layout.size()); |
888 | 681k | for (size_t idx = 0; idx < _data_reader.block_template.columns(); ++idx) { |
889 | 571k | const auto& file_block_column = _data_reader.file_block_layout[idx]; |
890 | 571k | const auto& column_with_type = _data_reader.block_template.get_by_position(idx); |
891 | 571k | const auto* column = column_with_type.column.get(); |
892 | 571k | try { |
893 | 571k | if (column == nullptr) { |
894 | 0 | auto st = Status::InternalError( |
895 | 0 | "Invalid file block column {} at {}: file_column_id={}, name='{}', " |
896 | 0 | "type={}, column=null, expected_rows={}, reader={}", |
897 | 0 | idx, stage, file_block_column.file_column_id.value(), |
898 | 0 | file_block_column.name, |
899 | 0 | file_block_column.type == nullptr ? "null" |
900 | 0 | : file_block_column.type->get_name(), |
901 | 0 | rows, debug_string()); |
902 | 0 | LOG(WARNING) << st; |
903 | 0 | return st; |
904 | 0 | } |
905 | 571k | column->sanity_check(); |
906 | 571k | auto st = column_with_type.check_type_and_column_match(); |
907 | 571k | if (!st.ok()) { |
908 | 0 | auto contextual_status = Status::InternalError( |
909 | 0 | "Invalid file block column {} at {}: file_column_id={}, name='{}', " |
910 | 0 | "type={}, column={}, column_size={}, expected_rows={}, error={}, " |
911 | 0 | "reader={}", |
912 | 0 | idx, stage, file_block_column.file_column_id.value(), |
913 | 0 | file_block_column.name, |
914 | 0 | file_block_column.type == nullptr ? "null" |
915 | 0 | : file_block_column.type->get_name(), |
916 | 0 | column->get_name(), column->size(), rows, st.to_string(), |
917 | 0 | debug_string()); |
918 | 0 | LOG(WARNING) << contextual_status; |
919 | 0 | return contextual_status; |
920 | 0 | } |
921 | 571k | } catch (const Exception& e) { |
922 | 0 | auto st = Status::InternalError( |
923 | 0 | "Invalid file block column {} at {}: file_column_id={}, name='{}', " |
924 | 0 | "type={}, column={}, column_size={}, expected_rows={}, error={}, " |
925 | 0 | "reader={}", |
926 | 0 | idx, stage, file_block_column.file_column_id.value(), |
927 | 0 | file_block_column.name, |
928 | 0 | file_block_column.type == nullptr ? "null" |
929 | 0 | : file_block_column.type->get_name(), |
930 | 0 | column == nullptr ? "null" : column->get_name(), |
931 | 0 | column == nullptr ? 0 : column->size(), rows, e.to_string(), |
932 | 0 | debug_string()); |
933 | 0 | LOG(WARNING) << st; |
934 | 0 | return st; |
935 | 0 | } catch (const std::exception& e) { |
936 | 0 | auto st = Status::InternalError( |
937 | 0 | "Invalid file block column {} at {}: file_column_id={}, name='{}', " |
938 | 0 | "type={}, column={}, column_size={}, expected_rows={}, error={}, " |
939 | 0 | "reader={}", |
940 | 0 | idx, stage, file_block_column.file_column_id.value(), |
941 | 0 | file_block_column.name, |
942 | 0 | file_block_column.type == nullptr ? "null" |
943 | 0 | : file_block_column.type->get_name(), |
944 | 0 | column == nullptr ? "null" : column->get_name(), |
945 | 0 | column == nullptr ? 0 : column->size(), rows, e.what(), debug_string()); |
946 | 0 | LOG(WARNING) << st; |
947 | 0 | return st; |
948 | 0 | } |
949 | 571k | } |
950 | 109k | return Status::OK(); |
951 | 109k | } |
952 | | |
953 | 109k | Status _check_table_block_columns(std::string_view stage, const Block* block, size_t rows) { |
954 | 109k | DORIS_CHECK(block != nullptr); |
955 | 109k | DORIS_CHECK(block->columns() == _data_reader.column_mapper->mappings().size()); |
956 | 697k | for (size_t idx = 0; idx < block->columns(); ++idx) { |
957 | 587k | const auto& mapping = _data_reader.column_mapper->mappings()[idx]; |
958 | 587k | const auto& column_with_type = block->get_by_position(idx); |
959 | 587k | const auto* column = column_with_type.column.get(); |
960 | 587k | try { |
961 | 587k | if (column == nullptr) { |
962 | 0 | auto st = Status::InternalError( |
963 | 0 | "Invalid table block column {} at {}: table_column='{}', " |
964 | 0 | "global_index={}, type={}, column=null, expected_rows={}, mapping={}", |
965 | 0 | idx, stage, mapping.table_column_name, mapping.global_index.value(), |
966 | 0 | mapping.table_type == nullptr ? "null" : mapping.table_type->get_name(), |
967 | 0 | rows, mapping.debug_string()); |
968 | 0 | LOG(WARNING) << st; |
969 | 0 | return st; |
970 | 0 | } |
971 | 587k | column->sanity_check(); |
972 | 587k | auto st = column_with_type.check_type_and_column_match(); |
973 | 587k | if (!st.ok()) { |
974 | 0 | auto contextual_status = Status::InternalError( |
975 | 0 | "Invalid table block column {} at {}: table_column='{}', " |
976 | 0 | "global_index={}, type={}, column={}, column_size={}, " |
977 | 0 | "expected_rows={}, error={}, mapping={}", |
978 | 0 | idx, stage, mapping.table_column_name, mapping.global_index.value(), |
979 | 0 | mapping.table_type == nullptr ? "null" : mapping.table_type->get_name(), |
980 | 0 | column->get_name(), column->size(), rows, st.to_string(), |
981 | 0 | mapping.debug_string()); |
982 | 0 | LOG(WARNING) << contextual_status; |
983 | 0 | return contextual_status; |
984 | 0 | } |
985 | 587k | } catch (const Exception& e) { |
986 | 0 | auto st = Status::InternalError( |
987 | 0 | "Invalid table block column {} at {}: table_column='{}', global_index={}, " |
988 | 0 | "type={}, column={}, column_size={}, expected_rows={}, error={}, " |
989 | 0 | "mapping={}", |
990 | 0 | idx, stage, mapping.table_column_name, mapping.global_index.value(), |
991 | 0 | mapping.table_type == nullptr ? "null" : mapping.table_type->get_name(), |
992 | 0 | column == nullptr ? "null" : column->get_name(), |
993 | 0 | column == nullptr ? 0 : column->size(), rows, e.to_string(), |
994 | 0 | mapping.debug_string()); |
995 | 0 | LOG(WARNING) << st; |
996 | 0 | return st; |
997 | 0 | } catch (const std::exception& e) { |
998 | 0 | auto st = Status::InternalError( |
999 | 0 | "Invalid table block column {} at {}: table_column='{}', global_index={}, " |
1000 | 0 | "type={}, column={}, column_size={}, expected_rows={}, error={}, " |
1001 | 0 | "mapping={}", |
1002 | 0 | idx, stage, mapping.table_column_name, mapping.global_index.value(), |
1003 | 0 | mapping.table_type == nullptr ? "null" : mapping.table_type->get_name(), |
1004 | 0 | column == nullptr ? "null" : column->get_name(), |
1005 | 0 | column == nullptr ? 0 : column->size(), rows, e.what(), |
1006 | 0 | mapping.debug_string()); |
1007 | 0 | LOG(WARNING) << st; |
1008 | 0 | return st; |
1009 | 0 | } |
1010 | 587k | } |
1011 | 109k | return Status::OK(); |
1012 | 109k | } |
1013 | | #endif |
1014 | | |
1015 | 109k | Status _truncate_char_or_varchar_columns(Block* block) { |
1016 | 109k | DORIS_CHECK(block != nullptr); |
1017 | 109k | if (_runtime_state == nullptr || |
1018 | 109k | !_runtime_state->query_options().truncate_char_or_varchar_columns) { |
1019 | 109k | return Status::OK(); |
1020 | 109k | } |
1021 | 18.4E | DORIS_CHECK(block->columns() == _data_reader.column_mapper->mappings().size()); |
1022 | 18.4E | for (size_t idx = 0; idx < _data_reader.column_mapper->mappings().size(); ++idx) { |
1023 | 36 | const auto& mapping = _data_reader.column_mapper->mappings()[idx]; |
1024 | 36 | if (!_should_truncate_char_or_varchar_column(mapping)) { |
1025 | 12 | continue; |
1026 | 12 | } |
1027 | 24 | const auto target_len = |
1028 | 24 | assert_cast<const DataTypeString*>(remove_nullable(mapping.table_type).get()) |
1029 | 24 | ->len(); |
1030 | 24 | _truncate_char_or_varchar_column(block, idx, target_len); |
1031 | 24 | } |
1032 | 18.4E | return Status::OK(); |
1033 | 109k | } |
1034 | | |
1035 | | // Return true when the table schema has a bounded CHAR/VARCHAR length that is stricter than |
1036 | | // the file-side type. Examples: |
1037 | | // - table VARCHAR(10), file VARCHAR(20): truncate to 10; |
1038 | | // - table VARCHAR(10), file STRING: truncate to 10 because STRING has no declared bound; |
1039 | | // - table STRING, any file type: no truncation because the target has no bound. |
1040 | 41 | static bool _should_truncate_char_or_varchar_column(const ColumnMapping& mapping) { |
1041 | 41 | if (mapping.table_type == nullptr) { |
1042 | 0 | return false; |
1043 | 0 | } |
1044 | 41 | const auto table_type = remove_nullable(mapping.table_type); |
1045 | 41 | const auto primitive_type = table_type->get_primitive_type(); |
1046 | 41 | if (primitive_type != TYPE_VARCHAR && primitive_type != TYPE_CHAR) { |
1047 | 13 | return false; |
1048 | 13 | } |
1049 | 28 | const auto target_len = assert_cast<const DataTypeString*>(table_type.get())->len(); |
1050 | 28 | if (target_len <= 0) { |
1051 | 0 | return false; |
1052 | 0 | } |
1053 | 28 | if (mapping.file_type == nullptr) { |
1054 | 0 | return true; |
1055 | 0 | } |
1056 | 28 | const auto file_type = remove_nullable(mapping.file_type); |
1057 | 28 | DORIS_CHECK(file_type != nullptr); |
1058 | 28 | int file_len = -1; |
1059 | 28 | if (file_type->get_primitive_type() == TYPE_VARCHAR || |
1060 | 28 | file_type->get_primitive_type() == TYPE_CHAR || |
1061 | 28 | file_type->get_primitive_type() == TYPE_STRING) { |
1062 | 27 | file_len = assert_cast<const DataTypeString*>(file_type.get())->len(); |
1063 | 27 | } |
1064 | | |
1065 | 28 | return file_len < 0 || target_len < file_len; |
1066 | 28 | } |
1067 | | |
1068 | | // Truncate a materialized CHAR/VARCHAR column in place by reusing the vectorized substring |
1069 | | // implementation: substring(column, 1, len). Nullable columns are unwrapped before substring |
1070 | | // execution and wrapped back with the original null map afterward, because substring operates |
1071 | | // on the nested string payload only. |
1072 | 25 | static void _truncate_char_or_varchar_column(Block* block, size_t idx, int len) { |
1073 | 25 | DORIS_CHECK(block != nullptr); |
1074 | 25 | auto int_type = std::make_shared<DataTypeInt32>(); |
1075 | 25 | const auto num_columns_without_result = cast_set<uint32_t>(block->columns()); |
1076 | 25 | auto& target = block->get_by_position(idx); |
1077 | 25 | const bool is_nullable = target.type->is_nullable(); |
1078 | 25 | ColumnPtr input_column = target.column; |
1079 | 25 | ColumnPtr null_map_column; |
1080 | 25 | if (is_nullable) { |
1081 | 25 | const auto* nullable_column = assert_cast<const ColumnNullable*>(target.column.get()); |
1082 | 25 | input_column = nullable_column->get_nested_column_ptr(); |
1083 | 25 | null_map_column = nullable_column->get_null_map_column_ptr(); |
1084 | 25 | } |
1085 | 25 | block->replace_by_position(idx, std::move(input_column)); |
1086 | 25 | block->insert({int_type->create_column_const(block->rows(), to_field<TYPE_INT>(1)), |
1087 | 25 | int_type, "const 1"}); |
1088 | 25 | block->insert({int_type->create_column_const(block->rows(), to_field<TYPE_INT>(len)), |
1089 | 25 | int_type, "const len"}); |
1090 | 25 | block->insert({nullptr, std::make_shared<DataTypeString>(), "result"}); |
1091 | | |
1092 | 25 | ColumnNumbers temp_arguments(3); |
1093 | 25 | temp_arguments[0] = cast_set<uint32_t>(idx); |
1094 | 25 | temp_arguments[1] = num_columns_without_result; |
1095 | 25 | temp_arguments[2] = num_columns_without_result + 1; |
1096 | 25 | const uint32_t result_column_id = num_columns_without_result + 2; |
1097 | 25 | SubstringUtil::substring_execute(*block, temp_arguments, result_column_id, block->rows()); |
1098 | | |
1099 | 25 | ColumnPtr result_column = block->get_by_position(result_column_id).column; |
1100 | 25 | if (is_nullable) { |
1101 | 25 | result_column = ColumnNullable::create(std::move(result_column), null_map_column); |
1102 | 25 | } |
1103 | 25 | block->replace_by_position(idx, std::move(result_column)); |
1104 | 25 | block->erase_tail(num_columns_without_result); |
1105 | 25 | } |
1106 | | |
1107 | 120k | Status _try_materialize_aggregate_pushdown_rows(Block* block, bool* pushed_down) { |
1108 | 120k | DORIS_CHECK(block != nullptr); |
1109 | 120k | DORIS_CHECK(pushed_down != nullptr); |
1110 | 120k | *pushed_down = false; |
1111 | 120k | block->clear_column_data(_projected_columns.size()); |
1112 | 120k | _aggregate_pushdown_tried = true; |
1113 | 120k | if (!_supports_aggregate_pushdown(_push_down_agg_type)) { |
1114 | 119k | return Status::OK(); |
1115 | 119k | } |
1116 | | |
1117 | 1.25k | FileAggregateRequest file_request; |
1118 | 1.25k | RETURN_IF_ERROR(_build_file_aggregate_request(_push_down_agg_type, &file_request)); |
1119 | 1.25k | FileAggregateResult file_result; |
1120 | 1.25k | Status status; |
1121 | 1.25k | { |
1122 | 1.25k | SCOPED_TIMER(_profile.file_reader_total_timer); |
1123 | 1.25k | SCOPED_TIMER(_profile.file_reader_aggregate_timer); |
1124 | 1.25k | status = _data_reader.reader->get_aggregate_result(file_request, &file_result); |
1125 | 1.25k | } |
1126 | 1.25k | if (status.is<ErrorCode::NOT_IMPLEMENTED_ERROR>()) { |
1127 | 13 | return Status::OK(); |
1128 | 13 | } |
1129 | 1.23k | RETURN_IF_ERROR(status); |
1130 | 1.49k | if (_push_down_agg_type == TPushAggOp::type::COUNT) { |
1131 | 1.49k | DORIS_CHECK(file_result.count >= 0); |
1132 | | // The upper aggregate consumes synthetic input rows, but emitting the whole metadata |
1133 | | // count in one block bypasses the runtime batch contract and can allocate by file size. |
1134 | | // Keep the remaining cardinality as split state and expose at most one batch per call. |
1135 | 1.49k | _remaining_file_level_count = file_result.count; |
1136 | 1.49k | _current_split_uses_metadata_count = true; |
1137 | 1.49k | if (_remaining_file_level_count > 0) { |
1138 | 1.49k | RETURN_IF_ERROR(_materialize_next_count_batch(&_remaining_file_level_count, block)); |
1139 | 1.49k | } |
1140 | 18.4E | } else { |
1141 | 18.4E | RETURN_IF_ERROR( |
1142 | 18.4E | _materialize_aggregate_pushdown_rows(_push_down_agg_type, file_result, block)); |
1143 | 18.4E | } |
1144 | 1.23k | *pushed_down = true; |
1145 | 1.23k | RETURN_IF_ERROR(close_current_reader()); |
1146 | 1.23k | return Status::OK(); |
1147 | 1.23k | } |
1148 | | |
1149 | 121k | virtual bool _supports_aggregate_pushdown(TPushAggOp::type agg_type) const { |
1150 | | // Only COUNT and MIN/MAX can be push down. |
1151 | 121k | if (agg_type != TPushAggOp::type::COUNT && agg_type != TPushAggOp::type::MINMAX) { |
1152 | 117k | return false; |
1153 | 117k | } |
1154 | | // Aggregate pushdown returns reduced synthetic rows and may close the physical reader |
1155 | | // before the next scheduler turn. If a runtime filter is still pending, those rows could |
1156 | | // escape before the filter arrives and cannot later be reconstructed from real file rows. |
1157 | | // This is the same irreversibility constraint as table-level metadata COUNT, and applies |
1158 | | // to COUNT and MIN/MAX for Parquet/ORC as well as COUNT for text readers. |
1159 | 3.99k | if (!_all_runtime_filters_applied_for_split) { |
1160 | 2 | return false; |
1161 | 2 | } |
1162 | | // Scanner owns the original conjunct list and evaluates it after TableReader finalizes |
1163 | | // rows. Even a slotless conjunct that cannot become a TableFilter must see every source |
1164 | | // row before an aggregate reduces the stream to synthetic COUNT/MINMAX rows. |
1165 | 3.98k | if (!_conjuncts.empty()) { |
1166 | 5 | return false; |
1167 | 5 | } |
1168 | | // Only support aggregate pushdown when there is no delete or filter, so |
1169 | | // the reduced rows consumed by the upper aggregate remain semantically equivalent to a |
1170 | | // normal scan. |
1171 | 3.98k | if ((_delete_rows != nullptr && !_delete_rows->empty()) || |
1172 | 4.53k | (_deletion_vector != nullptr && !_deletion_vector->isEmpty())) { |
1173 | 1.09k | return false; |
1174 | 1.09k | } |
1175 | 2.88k | if (!_table_filters.empty()) { |
1176 | 0 | return false; |
1177 | 0 | } |
1178 | 3.45k | if (agg_type == TPushAggOp::type::COUNT) { |
1179 | | // Old FEs do not serialize push_down_count_slot_ids. During the supported BE-first |
1180 | | // rolling upgrade, nullopt therefore means "COUNT semantics are unknown", not |
1181 | | // COUNT(*). Fall back to reading rows until the FE explicitly sends either an empty |
1182 | | // list for COUNT(*) or one slot for COUNT(col). |
1183 | 3.45k | if (!_push_down_count_columns.has_value()) { |
1184 | 3 | return false; |
1185 | 3 | } |
1186 | | // COUNT(*) needs no column metadata. COUNT(col) currently supports one direct file |
1187 | | // column; multiple COUNT arguments fall back to the normal scan so every upper |
1188 | | // aggregate receives the original rows. |
1189 | 3.45k | if (_push_down_count_columns->empty()) { |
1190 | 2.63k | return true; |
1191 | 2.63k | } |
1192 | 819 | if (_push_down_count_columns->size() != 1) { |
1193 | 37 | return false; |
1194 | 37 | } |
1195 | 782 | const auto& mapping = _push_down_count_mapping(); |
1196 | | // Metadata COUNT skips TableReader's normal materialization path. Only a trivial |
1197 | | // mapping is safe: for example, a nullable Parquet INT mapped to a NOT NULL table |
1198 | | // BIGINT normally needs both an INT->BIGINT cast and nullability validation. Counting |
1199 | | // footer values directly would bypass both operations and could hide invalid data. |
1200 | 782 | return mapping.file_local_id.has_value() && mapping.file_type != nullptr && |
1201 | 782 | mapping.table_type != nullptr && mapping.is_trivial && |
1202 | 782 | mapping.virtual_column_type == TableVirtualColumnType::INVALID && |
1203 | 782 | mapping.default_expr == nullptr; |
1204 | 819 | } |
1205 | | // For MIN/MAX, only support direct file-to-table column mappings. The two emitted rows |
1206 | | // must be enough for the upper MIN/MAX aggregate without evaluating default expressions or |
1207 | | // virtual columns. |
1208 | 18.4E | for (const auto& mapping : _data_reader.column_mapper->mappings()) { |
1209 | 149 | if (!mapping.file_local_id.has_value() || |
1210 | 149 | mapping.virtual_column_type != TableVirtualColumnType::INVALID || |
1211 | 149 | mapping.default_expr != nullptr || mapping.file_type == nullptr || |
1212 | 149 | mapping.table_type == nullptr) { |
1213 | 9 | return false; |
1214 | 9 | } |
1215 | 140 | if (!_can_push_down_minmax_for_mapping(mapping)) { |
1216 | 46 | return false; |
1217 | 46 | } |
1218 | 140 | } |
1219 | 18.4E | return true; |
1220 | 18.4E | } |
1221 | | |
1222 | 578k | static ColumnPtr _detach_column(ColumnPtr column) { |
1223 | 578k | DORIS_CHECK(column.get() != nullptr); |
1224 | 578k | return IColumn::mutate(std::move(column)); |
1225 | 578k | } |
1226 | | |
1227 | 95.1k | static ColumnPtr _take_and_detach_block_column(Block* block, int position) { |
1228 | 95.1k | DORIS_CHECK(block != nullptr); |
1229 | 95.1k | DORIS_CHECK(position >= 0 && position < static_cast<int>(block->columns())); |
1230 | 95.1k | auto& source = block->get_by_position(position); |
1231 | 95.1k | ColumnPtr column = source.column; |
1232 | | // The final mapping no longer needs the file block. Release its COW owner before mutate(), |
1233 | | // otherwise nested MAP/STRING columns are deep-copied and a multi-GB payload can OOM. |
1234 | | // Keep a constant placeholder with the original row count: Block::rows() is derived from |
1235 | | // its first column, and later mapping/default expressions must still see the full batch |
1236 | | // after an earlier Path Slot transfers the carrier at position zero. |
1237 | 95.1k | block->replace_by_position( |
1238 | 95.1k | position, source.type->create_column_const_with_default_value(column->size())); |
1239 | 95.1k | return _detach_column(std::move(column)); |
1240 | 95.1k | } |
1241 | | |
1242 | 122k | static Status _align_column_nullability(ColumnPtr* column, const DataTypePtr& table_type) { |
1243 | 122k | DORIS_CHECK(column != nullptr); |
1244 | 122k | DORIS_CHECK(column->get() != nullptr); |
1245 | 122k | DORIS_CHECK(table_type != nullptr); |
1246 | | // Must return non-const column |
1247 | 122k | *column = (*column)->convert_to_full_column_if_const(); |
1248 | 122k | if (table_type->is_nullable()) { |
1249 | 61.1k | const auto& nested_type = |
1250 | 61.1k | assert_cast<const DataTypeNullable&>(*table_type).get_nested_type(); |
1251 | 61.1k | if (!(*column)->is_nullable()) { |
1252 | 2 | RETURN_IF_ERROR(_align_column_nullability(column, nested_type)); |
1253 | 2 | *column = make_nullable(*column); |
1254 | 2 | return Status::OK(); |
1255 | 2 | } |
1256 | 61.1k | const auto& nullable_column = assert_cast<const ColumnNullable&>(**column); |
1257 | 61.1k | ColumnPtr nested_column = nullable_column.get_nested_column_ptr(); |
1258 | 61.1k | RETURN_IF_ERROR(_align_column_nullability(&nested_column, nested_type)); |
1259 | 61.1k | *column = ColumnNullable::create(nested_column, |
1260 | 61.1k | nullable_column.get_null_map_column_ptr()); |
1261 | 61.1k | return Status::OK(); |
1262 | 61.1k | } |
1263 | 61.2k | if ((*column)->is_nullable()) { |
1264 | 0 | const auto& nullable_column = assert_cast<const ColumnNullable&>(**column); |
1265 | 0 | if (nullable_column.has_null()) { |
1266 | 0 | return Status::InternalError( |
1267 | 0 | "Default expression produced NULL for non-nullable table column"); |
1268 | 0 | } |
1269 | 0 | ColumnPtr nested_column = nullable_column.get_nested_column_ptr(); |
1270 | 0 | RETURN_IF_ERROR(_align_column_nullability(&nested_column, table_type)); |
1271 | 0 | *column = nested_column; |
1272 | 0 | return Status::OK(); |
1273 | 0 | } |
1274 | 61.2k | if (const auto* array_type = typeid_cast<const DataTypeArray*>(table_type.get())) { |
1275 | 187 | const auto& array_column = assert_cast<const ColumnArray&>(**column); |
1276 | 187 | ColumnPtr nested_column = array_column.get_data_ptr(); |
1277 | 187 | RETURN_IF_ERROR( |
1278 | 187 | _align_column_nullability(&nested_column, array_type->get_nested_type())); |
1279 | 187 | *column = ColumnArray::create(nested_column, array_column.get_offsets_ptr()); |
1280 | 187 | return Status::OK(); |
1281 | 187 | } |
1282 | 61.0k | if (const auto* map_type = typeid_cast<const DataTypeMap*>(table_type.get())) { |
1283 | 138 | const auto& map_column = assert_cast<const ColumnMap&>(**column); |
1284 | 138 | ColumnPtr key_column = map_column.get_keys_ptr(); |
1285 | 138 | ColumnPtr value_column = map_column.get_values_ptr(); |
1286 | 138 | RETURN_IF_ERROR(_align_column_nullability(&key_column, map_type->get_key_type())); |
1287 | 138 | RETURN_IF_ERROR(_align_column_nullability(&value_column, map_type->get_value_type())); |
1288 | 138 | *column = ColumnMap::create(key_column, value_column, map_column.get_offsets_ptr()); |
1289 | 138 | return Status::OK(); |
1290 | 138 | } |
1291 | 60.9k | if (const auto* struct_type = typeid_cast<const DataTypeStruct*>(table_type.get())) { |
1292 | 7.89k | const auto& struct_column = assert_cast<const ColumnStruct&>(**column); |
1293 | 7.89k | Columns columns = struct_column.get_columns_copy(); |
1294 | 7.89k | DORIS_CHECK(columns.size() == struct_type->get_elements().size()); |
1295 | 26.5k | for (size_t i = 0; i < columns.size(); ++i) { |
1296 | 18.6k | RETURN_IF_ERROR( |
1297 | 18.6k | _align_column_nullability(&columns[i], struct_type->get_element(i))); |
1298 | 18.6k | } |
1299 | 7.89k | *column = ColumnStruct::create(columns); |
1300 | 7.89k | return Status::OK(); |
1301 | 7.89k | } |
1302 | 53.0k | return Status::OK(); |
1303 | 60.9k | } |
1304 | | |
1305 | | static Status _execute_default_expr_without_root_type_check( |
1306 | | const VExprContextSPtr& default_expr, const Block* block, |
1307 | 21.3k | ColumnWithTypeAndName* result_data) { |
1308 | 21.3k | DORIS_CHECK(default_expr != nullptr); |
1309 | 21.3k | DORIS_CHECK(block != nullptr); |
1310 | 21.3k | DORIS_CHECK(result_data != nullptr); |
1311 | 21.3k | ColumnPtr result_column; |
1312 | 21.3k | Status st; |
1313 | 21.3k | RETURN_IF_CATCH_EXCEPTION({ |
1314 | 21.3k | st = default_expr->root()->execute_column_impl(default_expr.get(), block, nullptr, |
1315 | 21.3k | block->rows(), result_column); |
1316 | 21.3k | }); |
1317 | 21.4k | RETURN_IF_ERROR(st); |
1318 | 21.4k | DORIS_CHECK(result_column.get() != nullptr); |
1319 | 21.4k | if (result_column->size() != block->rows()) { |
1320 | 0 | return Status::InternalError( |
1321 | 0 | "Default expr {} return column size {} not equal to expected size {}", |
1322 | 0 | default_expr->expr_name(), result_column->size(), block->rows()); |
1323 | 0 | } |
1324 | 21.4k | result_data->column = result_column; |
1325 | 21.4k | result_data->type = default_expr->execute_type(block); |
1326 | 21.4k | result_data->name = default_expr->expr_name(); |
1327 | 21.4k | return Status::OK(); |
1328 | 21.4k | } |
1329 | | |
1330 | | Status _cast_column_to_type(ColumnPtr* column, const DataTypePtr& file_type, |
1331 | | const DataTypePtr& table_type, |
1332 | 9.55k | const std::string& column_name) const { |
1333 | 9.55k | DORIS_CHECK(column != nullptr); |
1334 | 9.55k | DORIS_CHECK(column->get() != nullptr); |
1335 | 9.55k | DORIS_CHECK(file_type != nullptr); |
1336 | 9.55k | DORIS_CHECK(table_type != nullptr); |
1337 | 9.55k | if (file_type->equals(*table_type)) { |
1338 | 0 | return Status::OK(); |
1339 | 0 | } |
1340 | | |
1341 | 9.55k | DataTypePtr input_type = file_type; |
1342 | | // Cast wrappers unwrap nullable inputs according to the declared input type, so keep the |
1343 | | // root nullability of the declared type aligned with the actual column shape. |
1344 | 9.55k | if ((*column)->is_nullable() && !input_type->is_nullable()) { |
1345 | 0 | input_type = make_nullable(input_type); |
1346 | 9.55k | } else if (!(*column)->is_nullable() && input_type->is_nullable()) { |
1347 | 1 | input_type = remove_nullable(input_type); |
1348 | 1 | } |
1349 | 9.55k | Block cast_block; |
1350 | 9.55k | cast_block.insert({*column, input_type, column_name}); |
1351 | 9.55k | auto slot_ref = VSlotRef::create_shared(0, 0, -1, input_type, column_name); |
1352 | 9.55k | auto cast_expr = Cast::create_shared(table_type); |
1353 | 9.55k | cast_expr->add_child(std::move(slot_ref)); |
1354 | 9.55k | auto cast_ctx = VExprContext::create_shared(std::move(cast_expr)); |
1355 | 9.55k | RowDescriptor row_desc; |
1356 | 9.55k | RETURN_IF_ERROR(cast_ctx->prepare(_runtime_state, row_desc)); |
1357 | 9.55k | RETURN_IF_ERROR(cast_ctx->open(_runtime_state)); |
1358 | 9.55k | ColumnPtr cast_column; |
1359 | 9.55k | RETURN_IF_ERROR(cast_ctx->execute(&cast_block, cast_column)); |
1360 | 9.55k | *column = std::move(cast_column); |
1361 | 9.55k | return Status::OK(); |
1362 | 9.55k | } |
1363 | | |
1364 | | Status _materialize_present_child_mapping_column(const ColumnMapping& mapping, |
1365 | | const ColumnPtr& file_column, |
1366 | 20.7k | const size_t rows, ColumnPtr* column) { |
1367 | 20.7k | DORIS_CHECK(column != nullptr); |
1368 | 20.7k | DORIS_CHECK(mapping.file_type != nullptr); |
1369 | 20.7k | DORIS_CHECK(mapping.table_type != nullptr); |
1370 | 20.7k | *column = file_column; |
1371 | 20.7k | if (!mapping.is_trivial) { |
1372 | 12.1k | if (!mapping.child_mappings.empty()) { |
1373 | 2.55k | RETURN_IF_ERROR( |
1374 | 2.55k | _materialize_complex_mapping_column(mapping, *column, rows, column)); |
1375 | 9.54k | } else { |
1376 | 9.54k | RETURN_IF_ERROR(_cast_column_to_type(column, mapping.file_type, mapping.table_type, |
1377 | 9.54k | mapping.file_column_name)); |
1378 | 9.54k | } |
1379 | 12.1k | } |
1380 | 20.7k | RETURN_IF_ERROR(_align_column_nullability(column, mapping.table_type)); |
1381 | 20.7k | return Status::OK(); |
1382 | 20.7k | } |
1383 | | |
1384 | | Status _materialize_mapping_column(const ColumnMapping& mapping, Block* current_block, |
1385 | | const size_t rows, ColumnPtr* column, |
1386 | 587k | bool take_projection_result = false) { |
1387 | 587k | if (!mapping.column_paths.empty()) { |
1388 | 8 | return _materialize_variant_path_mapping_column(mapping, current_block, rows, column, |
1389 | 8 | take_projection_result); |
1390 | 8 | } |
1391 | 587k | if (!mapping.is_trivial && mapping.file_local_id.has_value() && |
1392 | 587k | !mapping.child_mappings.empty()) { |
1393 | 11.2k | DCHECK(mapping.projection != nullptr); |
1394 | 11.2k | int res_id; |
1395 | 11.2k | auto st = mapping.projection->execute(current_block, &res_id); |
1396 | 11.2k | if (!st.ok()) { |
1397 | 0 | return Status::InternalError( |
1398 | 0 | "Failed to execute complex mapping projection for table column '{}' " |
1399 | 0 | "(global_index={}, file_local_id={}, rows={}): {}, mapping={}", |
1400 | 0 | mapping.table_column_name, mapping.global_index.value(), |
1401 | 0 | *mapping.file_local_id, rows, st.to_string(), mapping.debug_string()); |
1402 | 0 | } |
1403 | 11.2k | ColumnPtr result_column = take_projection_result |
1404 | 11.2k | ? _take_and_detach_block_column(current_block, res_id) |
1405 | 11.2k | : current_block->get_by_position(res_id).column; |
1406 | 11.2k | RETURN_IF_ERROR( |
1407 | 11.2k | _materialize_complex_mapping_column(mapping, result_column, rows, column)); |
1408 | 11.2k | return Status::OK(); |
1409 | 11.2k | } |
1410 | 576k | if (mapping.projection != nullptr) { |
1411 | 552k | int res_id; |
1412 | 552k | auto st = mapping.projection->execute(current_block, &res_id); |
1413 | 552k | if (!st.ok()) { |
1414 | 1 | std::string file_local_id = "null"; |
1415 | 1 | if (mapping.file_local_id.has_value()) { |
1416 | 1 | file_local_id = std::to_string(*mapping.file_local_id); |
1417 | 1 | } |
1418 | 1 | return Status::InternalError( |
1419 | 1 | "Failed to execute mapping projection for table column '{}' " |
1420 | 1 | "(global_index={}, file_local_id={}, rows={}): {}, mapping={}", |
1421 | 1 | mapping.table_column_name, mapping.global_index.value(), file_local_id, |
1422 | 1 | rows, st.to_string(), mapping.debug_string()); |
1423 | 1 | } |
1424 | 552k | if (take_projection_result) { |
1425 | 93.4k | *column = _take_and_detach_block_column(current_block, res_id); |
1426 | 459k | } else { |
1427 | 459k | ColumnPtr result_column = current_block->get_by_position(res_id).column; |
1428 | 459k | *column = _detach_column(std::move(result_column)); |
1429 | 459k | } |
1430 | 552k | return Status::OK(); |
1431 | 552k | } |
1432 | 24.2k | if (mapping.default_expr != nullptr) { |
1433 | 21.3k | return _materialize_default_mapping_column(mapping, current_block, rows, column); |
1434 | 21.3k | } |
1435 | 2.85k | ColumnPtr result_column = mapping.table_type->create_column_const_with_default_value(rows); |
1436 | 2.85k | *column = _detach_column(std::move(result_column)); |
1437 | 2.85k | return Status::OK(); |
1438 | 24.2k | } |
1439 | | |
1440 | | Status _materialize_default_mapping_column(const ColumnMapping& mapping, Block* current_block, |
1441 | 21.3k | size_t rows, ColumnPtr* column) { |
1442 | 21.3k | DORIS_CHECK(mapping.default_expr != nullptr); |
1443 | 21.3k | DORIS_CHECK(mapping.table_type != nullptr); |
1444 | 21.3k | DORIS_CHECK(current_block != nullptr); |
1445 | 21.3k | DORIS_CHECK(column != nullptr); |
1446 | 21.3k | if (current_block->rows() == rows) { |
1447 | 18.2k | ColumnWithTypeAndName result; |
1448 | 18.2k | RETURN_IF_ERROR(_execute_default_expr_without_root_type_check(mapping.default_expr, |
1449 | 18.2k | current_block, &result)); |
1450 | 18.2k | ColumnPtr result_column = result.column; |
1451 | 18.2k | RETURN_IF_ERROR(_align_column_nullability(&result_column, mapping.table_type)); |
1452 | 18.2k | *column = _detach_column(std::move(result_column)); |
1453 | 18.2k | return Status::OK(); |
1454 | 18.2k | } |
1455 | | |
1456 | 3.08k | DORIS_CHECK(mapping.constant_index.has_value()); |
1457 | 3.08k | Block eval_block; |
1458 | 3.08k | eval_block.insert({mapping.table_type->create_column_const_with_default_value(rows), |
1459 | 3.08k | mapping.table_type, "__table_reader_const_rows"}); |
1460 | 3.08k | ColumnWithTypeAndName result; |
1461 | 3.08k | RETURN_IF_ERROR(_execute_default_expr_without_root_type_check(mapping.default_expr, |
1462 | 3.08k | &eval_block, &result)); |
1463 | 3.08k | ColumnPtr result_column = result.column; |
1464 | 3.08k | RETURN_IF_ERROR(_align_column_nullability(&result_column, mapping.table_type)); |
1465 | 3.08k | *column = _detach_column(std::move(result_column)); |
1466 | 3.08k | return Status::OK(); |
1467 | 3.08k | } |
1468 | | |
1469 | | Status _materialize_variant_path_mapping_column(const ColumnMapping& mapping, |
1470 | | Block* current_block, size_t rows, |
1471 | | ColumnPtr* column, |
1472 | 8 | bool take_projection_result) { |
1473 | 8 | DORIS_CHECK(!mapping.column_paths.empty()); |
1474 | 8 | DORIS_CHECK(mapping.resolved_variant_path != nullptr); |
1475 | 8 | DORIS_CHECK(mapping.table_type != nullptr); |
1476 | 8 | DORIS_CHECK(current_block != nullptr); |
1477 | 8 | DORIS_CHECK(column != nullptr); |
1478 | | |
1479 | 8 | ColumnPtr root_column; |
1480 | 8 | if (mapping.projection != nullptr) { |
1481 | 6 | int result_id; |
1482 | 6 | auto status = mapping.projection->execute(current_block, &result_id); |
1483 | 6 | if (!status.ok()) { |
1484 | 0 | return Status::InternalError( |
1485 | 0 | "Failed to read Variant root carrier for Path Slot '{}' " |
1486 | 0 | "(global_index={}, rows={}): {}, mapping={}", |
1487 | 0 | mapping.table_column_name, mapping.global_index.value(), rows, |
1488 | 0 | status.to_string(), mapping.debug_string()); |
1489 | 0 | } |
1490 | 6 | root_column = take_projection_result |
1491 | 6 | ? _take_and_detach_block_column(current_block, result_id) |
1492 | 6 | : current_block->get_by_position(result_id).column; |
1493 | 6 | } else if (mapping.default_expr != nullptr) { |
1494 | | // Schema-evolution defaults describe the missing root column. Apply the same path |
1495 | | // extraction after evaluating that root value instead of exposing the whole default. |
1496 | 1 | RETURN_IF_ERROR(_materialize_default_mapping_column(mapping, current_block, rows, |
1497 | 1 | &root_column)); |
1498 | 1 | } else { |
1499 | | // A missing root without a default makes every requested subpath SQL NULL. |
1500 | 1 | *column = _detach_column( |
1501 | 1 | mapping.table_type->create_column_const_with_default_value(rows)); |
1502 | 1 | return Status::OK(); |
1503 | 1 | } |
1504 | | |
1505 | 7 | const ColumnPtr materialized = root_column->convert_to_full_column_if_const(); |
1506 | 7 | _last_batch_materialization_input_bytes = |
1507 | 7 | std::max(_last_batch_materialization_input_bytes, materialized->byte_size()); |
1508 | 7 | const IColumn* physical = materialized.get(); |
1509 | 7 | std::span<const uint8_t> outer_nulls; |
1510 | 7 | if (const auto* nullable = check_and_get_column<ColumnNullable>(physical)) { |
1511 | 7 | outer_nulls = nullable->get_null_map_data(); |
1512 | 7 | physical = &nullable->get_nested_column(); |
1513 | 7 | } |
1514 | 7 | const auto* variant = check_and_get_column<ColumnVariantV2>(physical); |
1515 | 7 | if (variant == nullptr) { |
1516 | 0 | return Status::NotSupported( |
1517 | 0 | "Variant Path Slot '{}' requires a ColumnVariantV2 root carrier, got {}. " |
1518 | 0 | "Legacy ColumnVariant path extraction is not equivalent because it also needs " |
1519 | 0 | "sparse/document fallback semantics", |
1520 | 0 | mapping.table_column_name, materialized->get_name()); |
1521 | 0 | } |
1522 | 7 | if (variant->size() != rows) { |
1523 | 0 | return Status::InternalError( |
1524 | 0 | "Variant root carrier for Path Slot '{}' has {} rows, expected {}", |
1525 | 0 | mapping.table_column_name, variant->size(), rows); |
1526 | 0 | } |
1527 | | |
1528 | 7 | ColumnPtr result; |
1529 | 7 | RETURN_IF_ERROR(extract_variant_element_v2(*variant, *mapping.resolved_variant_path, |
1530 | 7 | outer_nulls, &result)); |
1531 | 7 | RETURN_IF_ERROR(_align_column_nullability(&result, mapping.table_type)); |
1532 | 7 | *column = std::move(result); |
1533 | 7 | return Status::OK(); |
1534 | 7 | } |
1535 | | |
1536 | | Status _materialize_complex_mapping_column(const ColumnMapping& mapping, |
1537 | | const ColumnPtr& file_column, const size_t rows, |
1538 | 13.8k | ColumnPtr* column) { |
1539 | 13.8k | DORIS_CHECK(mapping.table_type != nullptr); |
1540 | 13.8k | DORIS_CHECK(file_column.get() != nullptr); |
1541 | 13.8k | const auto table_type = remove_nullable(mapping.table_type); |
1542 | 13.8k | switch (table_type->get_primitive_type()) { |
1543 | 4.65k | case TYPE_STRUCT: |
1544 | 4.65k | RETURN_IF_ERROR(_materialize_struct_mapping_column(mapping, file_column, rows, column)); |
1545 | 4.65k | break; |
1546 | 4.80k | case TYPE_ARRAY: |
1547 | 4.80k | RETURN_IF_ERROR(_materialize_array_mapping_column(mapping, file_column, rows, column)); |
1548 | 4.80k | break; |
1549 | 4.80k | case TYPE_MAP: |
1550 | 4.36k | RETURN_IF_ERROR(_materialize_map_mapping_column(mapping, file_column, rows, column)); |
1551 | 4.36k | break; |
1552 | 4.36k | default: |
1553 | 0 | *column = _detach_column(file_column); |
1554 | 0 | break; |
1555 | 13.8k | } |
1556 | 13.8k | return Status::OK(); |
1557 | 13.8k | } |
1558 | | |
1559 | | static std::vector<const ColumnMapping*> _present_child_mappings_in_file_order( |
1560 | 4.65k | const std::vector<ColumnMapping>& child_mappings) { |
1561 | 4.65k | std::vector<const ColumnMapping*> result; |
1562 | 4.65k | result.reserve(child_mappings.size()); |
1563 | 11.9k | for (const auto& child_mapping : child_mappings) { |
1564 | 11.9k | if (child_mapping.file_local_id.has_value()) { |
1565 | 7.24k | result.push_back(&child_mapping); |
1566 | 7.24k | } |
1567 | 11.9k | } |
1568 | 5.69k | std::ranges::sort(result, [](const ColumnMapping* lhs, const ColumnMapping* rhs) { |
1569 | 5.69k | DORIS_CHECK(lhs->file_local_id.has_value()); |
1570 | 5.69k | DORIS_CHECK(rhs->file_local_id.has_value()); |
1571 | 5.69k | return *lhs->file_local_id < *rhs->file_local_id; |
1572 | 5.69k | }); |
1573 | 4.65k | return result; |
1574 | 4.65k | } |
1575 | | |
1576 | | static size_t _file_child_ordinal_for_mapping( |
1577 | | const ColumnMapping& mapping, const ColumnMapping& child_mapping, |
1578 | 7.23k | const std::vector<const ColumnMapping*>& file_ordered_children) { |
1579 | 7.23k | DORIS_CHECK(child_mapping.file_local_id.has_value()); |
1580 | 7.23k | if (!mapping.projected_file_children.empty()) { |
1581 | 7.23k | const auto child_it = std::ranges::find_if( |
1582 | 12.1k | mapping.projected_file_children, [&](const ColumnDefinition& file_child) { |
1583 | 12.1k | return file_child.file_local_id() == *child_mapping.file_local_id; |
1584 | 12.1k | }); |
1585 | 7.23k | DORIS_CHECK(child_it != mapping.projected_file_children.end()); |
1586 | 7.23k | return static_cast<size_t>( |
1587 | 7.23k | std::distance(mapping.projected_file_children.begin(), child_it)); |
1588 | 7.23k | } |
1589 | 2 | const auto child_it = std::ranges::find(file_ordered_children, &child_mapping); |
1590 | 2 | DORIS_CHECK(child_it != file_ordered_children.end()); |
1591 | 2 | return static_cast<size_t>(std::distance(file_ordered_children.begin(), child_it)); |
1592 | 7.23k | } |
1593 | | |
1594 | | static std::vector<const ColumnMapping*> _child_mappings_in_table_type_order( |
1595 | 4.65k | const ColumnMapping& mapping, const DataTypeStruct& table_type) { |
1596 | 4.65k | std::vector<const ColumnMapping*> result; |
1597 | 4.65k | result.reserve(mapping.child_mappings.size()); |
1598 | 16.6k | for (size_t child_idx = 0; child_idx < table_type.get_elements().size(); ++child_idx) { |
1599 | 11.9k | const auto& child_name = table_type.get_element_name(child_idx); |
1600 | 11.9k | const auto child_it = std::ranges::find_if( |
1601 | 24.1k | mapping.child_mappings, [&](const ColumnMapping& child_mapping) { |
1602 | 24.1k | return child_mapping.table_column_name == child_name; |
1603 | 24.1k | }); |
1604 | 11.9k | DORIS_CHECK(child_it != mapping.child_mappings.end()) |
1605 | 0 | << mapping.debug_string() << ", table_child_name=" << child_name; |
1606 | 11.9k | result.push_back(&*child_it); |
1607 | 11.9k | } |
1608 | 4.65k | return result; |
1609 | 4.65k | } |
1610 | | |
1611 | | static const IColumn* _nested_column_if_nullable(const ColumnPtr& column, |
1612 | 13.8k | const NullMap** null_map) { |
1613 | 13.8k | DORIS_CHECK(column.get() != nullptr); |
1614 | 13.8k | if (const auto* nullable_column = check_and_get_column<ColumnNullable>(*column)) { |
1615 | 13.8k | if (null_map != nullptr) { |
1616 | 13.8k | *null_map = &nullable_column->get_null_map_data(); |
1617 | 13.8k | } |
1618 | 13.8k | return &nullable_column->get_nested_column(); |
1619 | 13.8k | } |
1620 | 4 | return column.get(); |
1621 | 13.8k | } |
1622 | | |
1623 | | Status _materialize_struct_mapping_column(const ColumnMapping& mapping, |
1624 | | const ColumnPtr& file_column, const size_t rows, |
1625 | 4.65k | ColumnPtr* column) { |
1626 | 4.65k | DORIS_CHECK(mapping.table_type != nullptr); |
1627 | 4.65k | const auto* table_type = |
1628 | 4.65k | assert_cast<const DataTypeStruct*>(remove_nullable(mapping.table_type).get()); |
1629 | 4.65k | const auto full_file_column = file_column->convert_to_full_column_if_const(); |
1630 | 4.65k | const NullMap* parent_null_map = nullptr; |
1631 | 4.65k | const auto* nested_file_column = |
1632 | 4.65k | _nested_column_if_nullable(full_file_column, &parent_null_map); |
1633 | 4.65k | const auto* file_struct = assert_cast<const ColumnStruct*>(nested_file_column); |
1634 | 4.65k | DORIS_CHECK(table_type->get_elements().size() == mapping.child_mappings.size()); |
1635 | | |
1636 | 4.65k | Columns child_columns; |
1637 | 4.65k | child_columns.reserve(mapping.child_mappings.size()); |
1638 | 4.65k | const auto file_ordered_children = |
1639 | 4.65k | _present_child_mappings_in_file_order(mapping.child_mappings); |
1640 | 4.65k | const auto table_ordered_children = |
1641 | 4.65k | _child_mappings_in_table_type_order(mapping, *table_type); |
1642 | 11.9k | for (const auto* child_mapping : table_ordered_children) { |
1643 | 11.9k | DORIS_CHECK(child_mapping != nullptr); |
1644 | 11.9k | if (!child_mapping->file_local_id.has_value()) { |
1645 | 4.75k | child_columns.push_back( |
1646 | 4.75k | (child_mapping->initial_default_column |
1647 | 4.75k | ? child_mapping->initial_default_column->clone_resized(rows) |
1648 | 4.75k | : child_mapping->table_type |
1649 | 4.75k | ->create_column_const_with_default_value(rows)) |
1650 | 4.75k | ->convert_to_full_column_if_const()); |
1651 | 4.75k | continue; |
1652 | 4.75k | } |
1653 | 7.23k | const auto file_child_idx = |
1654 | 7.23k | _file_child_ordinal_for_mapping(mapping, *child_mapping, file_ordered_children); |
1655 | 7.23k | DORIS_CHECK(file_child_idx < file_struct->get_columns().size()); |
1656 | 7.23k | ColumnPtr child_column = file_struct->get_column_ptr(file_child_idx); |
1657 | 7.23k | RETURN_IF_ERROR(_materialize_present_child_mapping_column(*child_mapping, child_column, |
1658 | 7.23k | rows, &child_column)); |
1659 | 7.23k | child_columns.push_back(std::move(child_column)); |
1660 | 7.23k | } |
1661 | 4.65k | MutableColumns mutable_child_columns; |
1662 | 4.65k | mutable_child_columns.reserve(child_columns.size()); |
1663 | 11.9k | for (auto& child_column : child_columns) { |
1664 | 11.9k | mutable_child_columns.push_back(IColumn::mutate(std::move(child_column))); |
1665 | 11.9k | } |
1666 | 4.65k | auto result = ColumnStruct::create(std::move(mutable_child_columns)); |
1667 | 4.65k | if (mapping.table_type->is_nullable()) { |
1668 | 4.65k | auto null_map = ColumnUInt8::create(); |
1669 | 4.65k | auto& null_map_data = null_map->get_data(); |
1670 | 4.65k | null_map_data.resize(rows); |
1671 | 4.65k | if (parent_null_map != nullptr) { |
1672 | 4.65k | DORIS_CHECK(parent_null_map->size() == rows); |
1673 | 4.65k | null_map_data.assign(parent_null_map->begin(), parent_null_map->end()); |
1674 | 4.65k | } else { |
1675 | 0 | std::fill(null_map_data.begin(), null_map_data.end(), 0); |
1676 | 0 | } |
1677 | 4.65k | *column = ColumnNullable::create(std::move(result), std::move(null_map)); |
1678 | 4.65k | } else { |
1679 | 2 | *column = std::move(result); |
1680 | 2 | } |
1681 | 4.65k | return Status::OK(); |
1682 | 4.65k | } |
1683 | | |
1684 | | Status _materialize_array_mapping_column(const ColumnMapping& mapping, |
1685 | | const ColumnPtr& file_column, const size_t rows, |
1686 | 4.80k | ColumnPtr* column) { |
1687 | 4.80k | DORIS_CHECK(mapping.child_mappings.size() == 1); |
1688 | 4.80k | const auto full_file_column = file_column->convert_to_full_column_if_const(); |
1689 | 4.80k | const NullMap* parent_null_map = nullptr; |
1690 | 4.80k | const auto* nested_file_column = |
1691 | 4.80k | _nested_column_if_nullable(full_file_column, &parent_null_map); |
1692 | 4.80k | const auto* file_array = assert_cast<const ColumnArray*>(nested_file_column); |
1693 | 4.80k | ColumnPtr nested_column = file_array->get_data_ptr(); |
1694 | 4.80k | const auto& element_mapping = mapping.child_mappings[0]; |
1695 | 4.80k | RETURN_IF_ERROR(_materialize_present_child_mapping_column( |
1696 | 4.80k | element_mapping, nested_column, nested_column->size(), &nested_column)); |
1697 | 4.80k | auto offsets_column = file_array->get_offsets_ptr()->convert_to_full_column_if_const(); |
1698 | 4.80k | auto result = ColumnArray::create(IColumn::mutate(std::move(nested_column)), |
1699 | 4.80k | IColumn::mutate(std::move(offsets_column))); |
1700 | 4.80k | if (mapping.table_type->is_nullable()) { |
1701 | 4.80k | auto null_map = ColumnUInt8::create(); |
1702 | 4.80k | auto& null_map_data = null_map->get_data(); |
1703 | 4.80k | null_map_data.resize(rows); |
1704 | 4.80k | if (parent_null_map != nullptr) { |
1705 | 4.80k | DORIS_CHECK(parent_null_map->size() == rows); |
1706 | 4.80k | null_map_data.assign(parent_null_map->begin(), parent_null_map->end()); |
1707 | 4.80k | } else { |
1708 | 0 | std::fill(null_map_data.begin(), null_map_data.end(), 0); |
1709 | 0 | } |
1710 | 4.80k | *column = ColumnNullable::create(std::move(result), std::move(null_map)); |
1711 | 4.80k | } else { |
1712 | 0 | *column = std::move(result); |
1713 | 0 | } |
1714 | 4.80k | return Status::OK(); |
1715 | 4.80k | } |
1716 | | |
1717 | | Status _materialize_map_mapping_column(const ColumnMapping& mapping, |
1718 | | const ColumnPtr& file_column, const size_t rows, |
1719 | 4.36k | ColumnPtr* column) { |
1720 | 4.36k | const auto full_file_column = file_column->convert_to_full_column_if_const(); |
1721 | 4.36k | const NullMap* parent_null_map = nullptr; |
1722 | 4.36k | const auto* nested_file_column = |
1723 | 4.36k | _nested_column_if_nullable(full_file_column, &parent_null_map); |
1724 | 4.36k | const auto* file_map = assert_cast<const ColumnMap*>(nested_file_column); |
1725 | 4.36k | ColumnPtr key_column = file_map->get_keys_ptr(); |
1726 | 4.36k | ColumnPtr value_column = file_map->get_values_ptr(); |
1727 | | |
1728 | 4.36k | const ColumnMapping* key_mapping = nullptr; |
1729 | 4.36k | const ColumnMapping* value_mapping = nullptr; |
1730 | 8.72k | for (const auto& child_mapping : mapping.child_mappings) { |
1731 | 8.72k | if (!child_mapping.file_local_id.has_value()) { |
1732 | 0 | continue; |
1733 | 0 | } |
1734 | 8.72k | if (*child_mapping.file_local_id == 0) { |
1735 | 4.36k | key_mapping = &child_mapping; |
1736 | 4.36k | } else if (*child_mapping.file_local_id == 1) { |
1737 | 4.36k | value_mapping = &child_mapping; |
1738 | 4.36k | } |
1739 | 8.72k | } |
1740 | | |
1741 | 4.36k | if (key_mapping != nullptr) { |
1742 | 4.36k | RETURN_IF_ERROR(_materialize_present_child_mapping_column( |
1743 | 4.36k | *key_mapping, key_column, key_column->size(), &key_column)); |
1744 | 4.36k | } |
1745 | 4.36k | if (value_mapping != nullptr) { |
1746 | 4.36k | RETURN_IF_ERROR(_materialize_present_child_mapping_column( |
1747 | 4.36k | *value_mapping, value_column, value_column->size(), &value_column)); |
1748 | 4.36k | } |
1749 | 4.36k | auto offsets_column = file_map->get_offsets_ptr()->convert_to_full_column_if_const(); |
1750 | 4.36k | auto result = ColumnMap::create(IColumn::mutate(std::move(key_column)), |
1751 | 4.36k | IColumn::mutate(std::move(value_column)), |
1752 | 4.36k | IColumn::mutate(std::move(offsets_column))); |
1753 | 4.36k | if (mapping.table_type->is_nullable()) { |
1754 | 4.36k | auto null_map = ColumnUInt8::create(); |
1755 | 4.36k | auto& null_map_data = null_map->get_data(); |
1756 | 4.36k | null_map_data.resize(rows); |
1757 | 4.36k | if (parent_null_map != nullptr) { |
1758 | 4.36k | DORIS_CHECK(parent_null_map->size() == rows); |
1759 | 4.36k | null_map_data.assign(parent_null_map->begin(), parent_null_map->end()); |
1760 | 4.36k | } else { |
1761 | 0 | std::fill(null_map_data.begin(), null_map_data.end(), 0); |
1762 | 0 | } |
1763 | 4.36k | *column = ColumnNullable::create(std::move(result), std::move(null_map)); |
1764 | 4.36k | } else { |
1765 | 2 | *column = std::move(result); |
1766 | 2 | } |
1767 | 4.36k | return Status::OK(); |
1768 | 4.36k | } |
1769 | | |
1770 | 120k | Status _open_mapping_exprs() { |
1771 | 120k | RowDescriptor row_desc; |
1772 | 523k | for (const auto& mapping : _data_reader.column_mapper->mappings()) { |
1773 | 523k | if (mapping.projection != nullptr) { |
1774 | 497k | RETURN_IF_ERROR(mapping.projection->prepare(_runtime_state, row_desc)); |
1775 | 497k | RETURN_IF_ERROR(mapping.projection->open(_runtime_state)); |
1776 | 497k | } |
1777 | 523k | if (mapping.default_expr != nullptr) { |
1778 | 22.1k | RETURN_IF_ERROR(mapping.default_expr->prepare(_runtime_state, row_desc)); |
1779 | 22.1k | RETURN_IF_ERROR(mapping.default_expr->open(_runtime_state)); |
1780 | 22.1k | } |
1781 | 523k | } |
1782 | 120k | return Status::OK(); |
1783 | 120k | } |
1784 | | |
1785 | | Status _build_file_aggregate_request(TPushAggOp::type agg_type, |
1786 | 1.52k | FileAggregateRequest* request) const { |
1787 | 1.52k | DORIS_CHECK(request != nullptr); |
1788 | 1.52k | DORIS_CHECK(_supports_aggregate_pushdown(agg_type)); |
1789 | 1.52k | request->agg_type = agg_type; |
1790 | 1.52k | request->columns.clear(); |
1791 | 1.52k | if (agg_type == TPushAggOp::type::COUNT) { |
1792 | 1.50k | DORIS_CHECK(_push_down_count_columns.has_value()); |
1793 | | // An empty explicit list is the semantic signal for COUNT(*). Do not inspect the |
1794 | | // mapping count: `SELECT COUNT(*) FROM t` may still project one nullable column because |
1795 | | // the planner keeps a placeholder slot. In a 10,000-row file where that arbitrary slot |
1796 | | // has 9,015 non-null values, passing the slot would ask Parquet/ORC metadata for |
1797 | | // COUNT(slot)=9,015 instead of the required row count 10,000. |
1798 | 1.50k | if (!_push_down_count_columns->empty()) { |
1799 | 252 | const auto& mapping = _push_down_count_mapping(); |
1800 | 252 | DORIS_CHECK(mapping.file_local_id.has_value()); |
1801 | 252 | FileAggregateRequest::Column column; |
1802 | 252 | column.projection = |
1803 | 252 | LocalColumnIndex::top_level(LocalColumnId(*mapping.file_local_id)); |
1804 | 252 | request->columns.push_back(std::move(column)); |
1805 | 252 | } |
1806 | 1.50k | return Status::OK(); |
1807 | 1.50k | } |
1808 | 22 | request->columns.reserve(_data_reader.column_mapper->mappings().size()); |
1809 | 47 | for (const auto& mapping : _data_reader.column_mapper->mappings()) { |
1810 | 47 | DORIS_CHECK(mapping.file_local_id.has_value()); |
1811 | 47 | FileAggregateRequest::Column column; |
1812 | 47 | column.projection = LocalColumnIndex::top_level(LocalColumnId(*mapping.file_local_id)); |
1813 | 47 | if (!mapping.child_mappings.empty()) { |
1814 | 1 | RETURN_IF_ERROR(build_aggregate_projection(mapping, &column.projection)); |
1815 | 1 | } |
1816 | 47 | request->columns.push_back(std::move(column)); |
1817 | 47 | } |
1818 | 22 | return Status::OK(); |
1819 | 22 | } |
1820 | | |
1821 | 1.03k | const ColumnMapping& _push_down_count_mapping() const { |
1822 | 1.03k | DORIS_CHECK(_push_down_count_columns.has_value()); |
1823 | 1.03k | DORIS_CHECK(_push_down_count_columns->size() == 1); |
1824 | 1.03k | const auto mapping_it = |
1825 | 1.03k | std::ranges::find(_data_reader.column_mapper->mappings(), |
1826 | 1.03k | _push_down_count_columns->front(), &ColumnMapping::global_index); |
1827 | | // FileScannerV2 translates FE SlotIds through the same projected-column list used to build |
1828 | | // the mapper, so a missing mapping is an FE/BE contract violation rather than a fallback. |
1829 | 1.03k | DORIS_CHECK(mapping_it != _data_reader.column_mapper->mappings().end()); |
1830 | 1.03k | return *mapping_it; |
1831 | 1.03k | } |
1832 | | |
1833 | | Status _materialize_aggregate_pushdown_rows(TPushAggOp::type agg_type, |
1834 | | const FileAggregateResult& file_result, |
1835 | 21 | Block* block) { |
1836 | 21 | DORIS_CHECK(agg_type == TPushAggOp::type::MINMAX); |
1837 | | // MIN/MAX pushdown emits two rows, min first and max second, for each projected column. |
1838 | | // The upper MIN/MAX aggregate consumes those two rows to produce the final aggregate value. |
1839 | 21 | DORIS_CHECK(file_result.columns.size() == _data_reader.column_mapper->mappings().size()); |
1840 | 21 | DORIS_CHECK(block->columns() == _data_reader.column_mapper->mappings().size()); |
1841 | 21 | Block file_block; |
1842 | 21 | file_block.reserve(_data_reader.file_block_layout.size()); |
1843 | 26 | for (const auto& column : _data_reader.file_block_layout) { |
1844 | 26 | file_block.insert({column.type->create_column(), column.type, column.name}); |
1845 | 26 | } |
1846 | 47 | for (size_t column_idx = 0; column_idx < file_result.columns.size(); ++column_idx) { |
1847 | 26 | const auto& result_column = file_result.columns[column_idx]; |
1848 | 26 | if (!result_column.has_min || !result_column.has_max) { |
1849 | 0 | return Status::NotSupported("Missing min/max aggregate result for column {}", |
1850 | 0 | _projected_columns[column_idx].name); |
1851 | 0 | } |
1852 | 26 | bool found_file_column = false; |
1853 | 33 | for (size_t block_position = 0; block_position < _data_reader.file_block_layout.size(); |
1854 | 33 | ++block_position) { |
1855 | 33 | if (_data_reader.file_block_layout[block_position].file_column_id == |
1856 | 33 | file_result.columns[column_idx].projection.column_id()) { |
1857 | 26 | found_file_column = true; |
1858 | 26 | auto column = file_block.get_by_position(block_position) |
1859 | 26 | .type->create_column() |
1860 | 26 | ->assert_mutable(); |
1861 | 26 | RETURN_IF_ERROR(_insert_aggregate_projection_value( |
1862 | 26 | file_result.columns[column_idx].projection, result_column.min_value, |
1863 | 26 | column.get())); |
1864 | 26 | RETURN_IF_ERROR(_insert_aggregate_projection_value( |
1865 | 26 | file_result.columns[column_idx].projection, result_column.max_value, |
1866 | 26 | column.get())); |
1867 | 26 | file_block.replace_by_position(block_position, std::move(column)); |
1868 | 26 | break; |
1869 | 26 | } |
1870 | 33 | } |
1871 | 26 | DORIS_CHECK(found_file_column); |
1872 | 26 | } |
1873 | 47 | for (size_t column_idx = 0; column_idx < _data_reader.column_mapper->mappings().size(); |
1874 | 26 | ++column_idx) { |
1875 | 26 | ColumnPtr table_column; |
1876 | 26 | RETURN_IF_ERROR(_materialize_mapping_column( |
1877 | 26 | _data_reader.column_mapper->mappings()[column_idx], &file_block, 2, |
1878 | 26 | &table_column, |
1879 | 26 | column_idx + 1 == _data_reader.column_mapper->mappings().size())); |
1880 | 26 | block->replace_by_position(column_idx, std::move(table_column)); |
1881 | 26 | } |
1882 | 21 | return Status::OK(); |
1883 | 21 | } |
1884 | | |
1885 | | struct FileBlockColumn { |
1886 | | LocalColumnId file_column_id = LocalColumnId::invalid(); |
1887 | | std::string name; |
1888 | | DataTypePtr type; |
1889 | | }; |
1890 | | |
1891 | | struct DataReader { |
1892 | | std::unique_ptr<FileReader> reader; |
1893 | | std::unique_ptr<TableColumnMapper> column_mapper; |
1894 | | // Schema of the data file, also including virtual column (row position). |
1895 | | std::vector<ColumnDefinition> file_schema; |
1896 | | // Layout of the block returned by file reader, determined by column mapping and file |
1897 | | // schema. It is used for file reader to materialize columns into correct type and position. |
1898 | | std::vector<FileBlockColumn> file_block_layout; |
1899 | | Block block_template; |
1900 | | }; |
1901 | | DataReader _data_reader; |
1902 | | std::vector<ColumnDefinition> _projected_columns; |
1903 | | std::unique_ptr<ScanTask> _current_task; |
1904 | | std::optional<io::FileDescription> _current_file_description; |
1905 | | // Range-level compression has higher priority than scan-param compression. TVF/load can keep |
1906 | | // the logical format as CSV/TEXT while carrying the concrete compression such as GZ or LZO on |
1907 | | // each TFileRangeDesc, matching the old FileScanner reader contract. |
1908 | | TFileCompressType::type _current_range_compress_type = TFileCompressType::UNKNOWN; |
1909 | | std::optional<TUniqueId> _current_range_load_id; |
1910 | | TFileRangeDesc _current_file_range_desc; |
1911 | | std::shared_ptr<io::FileSystemProperties> _system_properties; |
1912 | | // partition key -> value |
1913 | | std::map<std::string, Field> _partition_values; |
1914 | | // Predicates built from scan conjuncts before file-level localization. |
1915 | | std::vector<TableFilter> _table_filters; |
1916 | | // Number of localized filters before the first unsafe conjunct in the original row-level |
1917 | | // order. This differs from scanning `_table_filters` for safety because slotless predicates are |
1918 | | // intentionally absent from that vector but must still act as ordering barriers. |
1919 | | size_t _constant_pruning_safe_filter_count = 0; |
1920 | | VExprContextSPtrs _conjuncts; |
1921 | | ReadProfile _profile; |
1922 | | // Parsed from row-position based delete files, including position delete and deletion vector. |
1923 | | DeleteRows* _delete_rows = nullptr; |
1924 | | DeletionVector* _deletion_vector = nullptr; |
1925 | | TFileScanRangeParams* _scan_params; |
1926 | | std::shared_ptr<io::IOContext> _io_ctx; |
1927 | | RuntimeState* _runtime_state; |
1928 | | RuntimeProfile* _scanner_profile; |
1929 | | const std::vector<SlotDescriptor*>* _file_slot_descs = nullptr; |
1930 | | FileFormat _format; |
1931 | | TPushAggOp::type _push_down_agg_type = TPushAggOp::type::NONE; |
1932 | | std::optional<std::vector<GlobalIndex>> _push_down_count_columns; |
1933 | | size_t _batch_size = 0; |
1934 | | size_t _last_batch_materialization_input_bytes = 0; |
1935 | | uint64_t _initial_condition_cache_digest = 0; |
1936 | | uint64_t _condition_cache_digest = 0; |
1937 | | // True only when prepare_split() received a digest for the exact conjunct snapshot used by |
1938 | | // this split. Standalone callers that only supplied TableReadOptions::condition_cache_digest |
1939 | | // keep the conservative runtime-filter guard. |
1940 | | bool _condition_cache_digest_covers_current_split = false; |
1941 | | segment_v2::ConditionCache::ExternalCacheKey _condition_cache_key; |
1942 | | std::shared_ptr<std::vector<bool>> _condition_cache; |
1943 | | std::shared_ptr<ConditionCacheContext> _condition_cache_ctx; |
1944 | | int64_t _condition_cache_hit_count = 0; |
1945 | | bool _current_reader_reached_eof = false; |
1946 | | int64_t _remaining_table_level_count = -1; |
1947 | | int64_t _remaining_file_level_count = -1; |
1948 | | // True only after the active split selects a table-level row-count shortcut or successfully |
1949 | | // materializes COUNT rows from file metadata. FileScannerV2 uses this result, rather than the |
1950 | | // raw aggregate opcode, to keep adaptive batching enabled for normal row-scan fallbacks. |
1951 | | bool _current_split_uses_metadata_count = false; |
1952 | | // Snapshot supplied by FileScannerV2 for the active split. It gates every shortcut that emits |
1953 | | // irreversible aggregate rows, not only the table-level row-count shortcut in prepare_split(). |
1954 | | bool _all_runtime_filters_applied_for_split = true; |
1955 | | std::optional<GlobalRowIdContext> _global_rowid_context; |
1956 | | bool _aggregate_pushdown_tried = false; |
1957 | | bool _current_split_pruned = false; |
1958 | | TableColumnMapperOptions _mapper_options; |
1959 | | |
1960 | | private: |
1961 | | static const ColumnDefinition* _find_column_definition( |
1962 | 557k | const std::vector<ColumnDefinition>& schema, LocalColumnId column_id) { |
1963 | 9.99M | for (const auto& field : schema) { |
1964 | 9.99M | if (field.file_local_id() == column_id.value()) { |
1965 | 528k | return &field; |
1966 | 528k | } |
1967 | 9.99M | } |
1968 | 28.6k | return nullptr; |
1969 | 557k | } |
1970 | | |
1971 | 142 | static bool _can_push_down_minmax_for_mapping(const ColumnMapping& mapping) { |
1972 | 142 | if (mapping.child_mappings.empty()) { |
1973 | | // Direct mappings use a slot-ref projection to materialize the file column. The |
1974 | | // projection does not transform ordering; casts and other conversions are already |
1975 | | // represented by a non-trivial mapping and must fall back to row scanning. |
1976 | 139 | return mapping.is_trivial; |
1977 | 139 | } |
1978 | 3 | const auto primitive_type = remove_nullable(mapping.file_type)->get_primitive_type(); |
1979 | 3 | if (primitive_type != TYPE_STRUCT) { |
1980 | 1 | return false; |
1981 | 1 | } |
1982 | 2 | size_t mapped_children = 0; |
1983 | 2 | const ColumnMapping* mapped_child = nullptr; |
1984 | 2 | for (const auto& child_mapping : mapping.child_mappings) { |
1985 | 2 | if (!child_mapping.file_local_id.has_value()) { |
1986 | 0 | continue; |
1987 | 0 | } |
1988 | 2 | ++mapped_children; |
1989 | 2 | mapped_child = &child_mapping; |
1990 | 2 | } |
1991 | 2 | return mapped_children == 1 && mapped_child != nullptr && |
1992 | 2 | _can_push_down_minmax_for_mapping(*mapped_child); |
1993 | 3 | } |
1994 | | |
1995 | | static Status build_aggregate_projection(const ColumnMapping& mapping, |
1996 | 2 | LocalColumnIndex* projection) { |
1997 | 2 | DORIS_CHECK(projection != nullptr); |
1998 | 2 | DORIS_CHECK(mapping.file_local_id.has_value()); |
1999 | 2 | *projection = LocalColumnIndex::local(*mapping.file_local_id); |
2000 | 2 | projection->children.clear(); |
2001 | 2 | projection->project_all_children = true; |
2002 | 2 | if (mapping.child_mappings.empty()) { |
2003 | 1 | return Status::OK(); |
2004 | 1 | } |
2005 | 1 | projection->project_all_children = false; |
2006 | 1 | for (const auto& child_mapping : mapping.child_mappings) { |
2007 | 1 | if (!child_mapping.file_local_id.has_value()) { |
2008 | 0 | continue; |
2009 | 0 | } |
2010 | 1 | LocalColumnIndex child_projection; |
2011 | 1 | RETURN_IF_ERROR(build_aggregate_projection(child_mapping, &child_projection)); |
2012 | 1 | projection->children.push_back(std::move(child_projection)); |
2013 | 1 | } |
2014 | 1 | DORIS_CHECK(projection->children.size() == 1); |
2015 | 1 | return Status::OK(); |
2016 | 1 | } |
2017 | | |
2018 | | static Status _insert_aggregate_projection_value(const LocalColumnIndex& projection, |
2019 | 108 | const Field& value, IColumn* column) { |
2020 | 108 | DORIS_CHECK(column != nullptr); |
2021 | 108 | if (auto* nullable_column = check_and_get_column<ColumnNullable>(*column)) { |
2022 | 54 | RETURN_IF_ERROR(_insert_aggregate_projection_value( |
2023 | 54 | projection, value, &nullable_column->get_nested_column())); |
2024 | 54 | nullable_column->get_null_map_data().push_back(0); |
2025 | 54 | return Status::OK(); |
2026 | 54 | } |
2027 | 54 | if (projection.project_all_children || projection.children.empty()) { |
2028 | 52 | column->insert(value); |
2029 | 52 | return Status::OK(); |
2030 | 52 | } |
2031 | 2 | auto* struct_column = assert_cast<ColumnStruct*>(column); |
2032 | 2 | DORIS_CHECK(projection.children.size() == 1); |
2033 | 2 | const auto& child_projection = projection.children[0]; |
2034 | 2 | DORIS_CHECK(struct_column->get_columns().size() == 1); |
2035 | 2 | RETURN_IF_ERROR(_insert_aggregate_projection_value(child_projection, value, |
2036 | 2 | &struct_column->get_column(0))); |
2037 | 2 | return Status::OK(); |
2038 | 2 | } |
2039 | | |
2040 | | // Parse a DV into its compressed bitmap. Position delete files continue to use _delete_rows. |
2041 | | Status _parse_delete_predicates(const SplitReadOptions& options); |
2042 | | }; |
2043 | | |
2044 | | } // namespace doris::format |