be/src/format/table/iceberg_reader.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "format/table/iceberg_reader.h" |
19 | | |
20 | | #include <gen_cpp/Descriptors_types.h> |
21 | | #include <gen_cpp/Metrics_types.h> |
22 | | #include <gen_cpp/PlanNodes_types.h> |
23 | | #include <gen_cpp/parquet_types.h> |
24 | | #include <glog/logging.h> |
25 | | #include <parallel_hashmap/phmap.h> |
26 | | #include <rapidjson/document.h> |
27 | | |
28 | | #include <algorithm> |
29 | | #include <cstring> |
30 | | #include <functional> |
31 | | #include <memory> |
32 | | |
33 | | #include "common/compiler_util.h" // IWYU pragma: keep |
34 | | #include "common/consts.h" |
35 | | #include "common/status.h" |
36 | | #include "core/assert_cast.h" |
37 | | #include "core/block/block.h" |
38 | | #include "core/block/column_with_type_and_name.h" |
39 | | #include "core/column/column.h" |
40 | | #include "core/column/column_nullable.h" |
41 | | #include "core/column/column_string.h" |
42 | | #include "core/column/column_vector.h" |
43 | | #include "core/data_type/data_type_factory.hpp" |
44 | | #include "core/data_type/data_type_nullable.h" |
45 | | #include "core/data_type/define_primitive_type.h" |
46 | | #include "core/data_type/primitive_type.h" |
47 | | #include "core/string_ref.h" |
48 | | #include "exprs/aggregate/aggregate_function.h" |
49 | | #include "format/format_common.h" |
50 | | #include "format/generic_reader.h" |
51 | | #include "format/orc/vorc_reader.h" |
52 | | #include "format/parquet/schema_desc.h" |
53 | | #include "format/parquet/vparquet_column_chunk_reader.h" |
54 | | #include "format/table/deletion_vector_reader.h" |
55 | | #include "format/table/iceberg/iceberg_orc_nested_column_utils.h" |
56 | | #include "format/table/iceberg/iceberg_parquet_nested_column_utils.h" |
57 | | #include "format/table/nested_column_access_helper.h" |
58 | | #include "format/table/table_schema_change_helper.h" |
59 | | #include "runtime/runtime_state.h" |
60 | | #include "util/coding.h" |
61 | | |
62 | | namespace cctz { |
63 | | class time_zone; |
64 | | } // namespace cctz |
65 | | namespace doris { |
66 | | class RowDescriptor; |
67 | | class SlotDescriptor; |
68 | | class TupleDescriptor; |
69 | | |
70 | | namespace io { |
71 | | struct IOContext; |
72 | | } // namespace io |
73 | | class VExprContext; |
74 | | } // namespace doris |
75 | | |
76 | | namespace doris { |
77 | | const std::string IcebergOrcReader::ICEBERG_ORC_ATTRIBUTE = "iceberg.id"; |
78 | | |
79 | | bool IcebergTableReader::_is_fully_dictionary_encoded( |
80 | 8 | const tparquet::ColumnMetaData& column_metadata) { |
81 | 14 | const auto is_dictionary_encoding = [](tparquet::Encoding::type encoding) { |
82 | 14 | return encoding == tparquet::Encoding::PLAIN_DICTIONARY || |
83 | 14 | encoding == tparquet::Encoding::RLE_DICTIONARY; |
84 | 14 | }; |
85 | 12 | const auto is_data_page = [](tparquet::PageType::type page_type) { |
86 | 12 | return page_type == tparquet::PageType::DATA_PAGE || |
87 | 12 | page_type == tparquet::PageType::DATA_PAGE_V2; |
88 | 12 | }; |
89 | 8 | const auto is_level_encoding = [](tparquet::Encoding::type encoding) { |
90 | 2 | return encoding == tparquet::Encoding::RLE || encoding == tparquet::Encoding::BIT_PACKED; |
91 | 2 | }; |
92 | | |
93 | | // A column chunk may have a dictionary page but still contain plain-encoded data pages. |
94 | | // Only treat it as dictionary-coded when all data pages are dictionary encoded. |
95 | 8 | if (column_metadata.__isset.encoding_stats) { |
96 | 7 | bool has_data_page_stats = false; |
97 | 12 | for (const tparquet::PageEncodingStats& enc_stat : column_metadata.encoding_stats) { |
98 | 12 | if (is_data_page(enc_stat.page_type) && enc_stat.count > 0) { |
99 | 8 | has_data_page_stats = true; |
100 | 8 | if (!is_dictionary_encoding(enc_stat.encoding)) { |
101 | 2 | return false; |
102 | 2 | } |
103 | 8 | } |
104 | 12 | } |
105 | 5 | if (has_data_page_stats) { |
106 | 4 | return true; |
107 | 4 | } |
108 | 5 | } |
109 | | |
110 | 2 | bool has_dict_encoding = false; |
111 | 2 | bool has_nondict_encoding = false; |
112 | 3 | for (const tparquet::Encoding::type& encoding : column_metadata.encodings) { |
113 | 3 | if (is_dictionary_encoding(encoding)) { |
114 | 1 | has_dict_encoding = true; |
115 | 1 | } |
116 | | |
117 | 3 | if (!is_dictionary_encoding(encoding) && !is_level_encoding(encoding)) { |
118 | 2 | has_nondict_encoding = true; |
119 | 2 | break; |
120 | 2 | } |
121 | 3 | } |
122 | 2 | if (!has_dict_encoding || has_nondict_encoding) { |
123 | 2 | return false; |
124 | 2 | } |
125 | | |
126 | 0 | return true; |
127 | 2 | } |
128 | | |
129 | | // ============================================================================ |
130 | | // IcebergParquetReader: on_before_init_reader (Parquet-specific schema matching) |
131 | | // ============================================================================ |
132 | 1 | Status IcebergParquetReader::on_before_init_reader(ReaderInitContext* ctx) { |
133 | 1 | _column_descs = ctx->column_descs; |
134 | 1 | _fill_col_name_to_block_idx = ctx->col_name_to_block_idx; |
135 | 1 | _file_format = Fileformat::PARQUET; |
136 | | |
137 | | // Get file metadata schema first (available because _open_file() already ran) |
138 | 1 | const FieldDescriptor* field_desc = nullptr; |
139 | 1 | RETURN_IF_ERROR(this->get_file_metadata_schema(&field_desc)); |
140 | 1 | DCHECK(field_desc != nullptr); |
141 | | |
142 | | // Build table_info_node by field_id or name matching. |
143 | | // This must happen BEFORE column classification so we can use children_column_exists |
144 | | // to check if a column exists in the file (by field ID, not name). |
145 | 1 | if (!get_scan_params().__isset.history_schema_info || |
146 | 1 | get_scan_params().history_schema_info.empty()) [[unlikely]] { |
147 | 1 | RETURN_IF_ERROR(BuildTableInfoUtil::by_parquet_name(ctx->tuple_descriptor, *field_desc, |
148 | 1 | ctx->table_info_node)); |
149 | 1 | } else { |
150 | 0 | RETURN_IF_ERROR(BuildTableInfoUtil::by_parquet_field_id_with_name_mapping( |
151 | 0 | get_scan_params().history_schema_info.front().root_field, *field_desc, |
152 | 0 | ctx->table_info_node)); |
153 | 0 | } |
154 | | |
155 | 1 | std::unordered_set<std::string> partition_col_names; |
156 | 1 | if (ctx->range->__isset.columns_from_path_keys) { |
157 | 0 | partition_col_names.insert(ctx->range->columns_from_path_keys.begin(), |
158 | 0 | ctx->range->columns_from_path_keys.end()); |
159 | 0 | } |
160 | | |
161 | | // Single pass: classify columns, detect $row_id, handle partition fallback. |
162 | 1 | bool has_partition_from_path = false; |
163 | 2 | for (const auto& desc : *ctx->column_descs) { |
164 | 2 | if (desc.category == ColumnCategory::SYNTHESIZED) { |
165 | 0 | if (desc.name == BeConsts::ICEBERG_ROWID_COL) { |
166 | 0 | this->register_synthesized_column_handler( |
167 | 0 | BeConsts::ICEBERG_ROWID_COL, [this](Block* block, size_t rows) -> Status { |
168 | 0 | return _fill_iceberg_row_id(block, rows); |
169 | 0 | }); |
170 | 0 | continue; |
171 | 0 | } else if (desc.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) { |
172 | 0 | auto topn_row_id_column_iter = _create_topn_row_id_column_iterator(); |
173 | 0 | this->register_synthesized_column_handler( |
174 | 0 | desc.name, |
175 | 0 | [iter = std::move(topn_row_id_column_iter), this, &desc]( |
176 | 0 | Block* block, size_t rows) -> Status { |
177 | 0 | return fill_topn_row_id(iter, desc.name, block, rows); |
178 | 0 | }); |
179 | 0 | continue; |
180 | 0 | } |
181 | 2 | } else if (desc.category == ColumnCategory::PARTITION_KEY) { |
182 | 0 | bool has_partition_value = partition_col_names.contains(desc.name); |
183 | 0 | bool exists_in_file = ctx->table_info_node->children_column_exists(desc.name); |
184 | 0 | if (!has_partition_value || exists_in_file) { |
185 | | // Keep PARTITION_KEY category stable for scan planning, but still read |
186 | | // from file when the column exists there. |
187 | 0 | ctx->column_names.push_back(desc.name); |
188 | 0 | continue; |
189 | 0 | } |
190 | 0 | has_partition_from_path = true; |
191 | 2 | } else if (desc.category == ColumnCategory::REGULAR) { |
192 | 2 | ctx->column_names.push_back(desc.name); |
193 | 2 | } else if (desc.category == ColumnCategory::GENERATED) { |
194 | 0 | _init_row_lineage_columns(); |
195 | 0 | if (desc.name == ROW_LINEAGE_ROW_ID) { |
196 | 0 | ctx->column_names.push_back(desc.name); |
197 | 0 | this->register_generated_column_handler( |
198 | 0 | ROW_LINEAGE_ROW_ID, [this](Block* block, size_t rows) -> Status { |
199 | 0 | return _fill_row_lineage_row_id(block, rows); |
200 | 0 | }); |
201 | 0 | continue; |
202 | 0 | } else if (desc.name == ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER) { |
203 | 0 | ctx->column_names.push_back(desc.name); |
204 | 0 | this->register_generated_column_handler( |
205 | 0 | ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER, |
206 | 0 | [this](Block* block, size_t rows) -> Status { |
207 | 0 | return _fill_row_lineage_last_updated_sequence_number(block, rows); |
208 | 0 | }); |
209 | 0 | continue; |
210 | 0 | } |
211 | 0 | } |
212 | 2 | } |
213 | | |
214 | | // Set up partition value extraction if any partition columns need filling from path |
215 | 1 | if (has_partition_from_path) { |
216 | 0 | RETURN_IF_ERROR(_extract_partition_values(*ctx->range, ctx->tuple_descriptor, |
217 | 0 | _fill_partition_values, |
218 | 0 | &_fill_partition_value_is_null)); |
219 | 0 | } |
220 | | |
221 | 1 | _all_required_col_names = ctx->column_names; |
222 | | |
223 | | // Create column IDs from field descriptor |
224 | 1 | auto column_id_result = _create_column_ids(field_desc, ctx->tuple_descriptor); |
225 | 1 | ctx->column_ids = std::move(column_id_result.column_ids); |
226 | 1 | ctx->filter_column_ids = std::move(column_id_result.filter_column_ids); |
227 | | |
228 | | // Build field_id -> block_column_name mapping for equality delete filtering. |
229 | | // This was previously done in init_reader() column matching (pre-CRTP refactoring). |
230 | 2 | for (const auto* slot : ctx->tuple_descriptor->slots()) { |
231 | 2 | _id_to_block_column_name.emplace(slot->col_unique_id(), slot->col_name()); |
232 | 2 | } |
233 | | |
234 | | // Process delete files (must happen before _do_init_reader so expand col IDs are included) |
235 | 1 | RETURN_IF_ERROR(_init_row_filters()); |
236 | | |
237 | | // Add expand column IDs for equality delete and remap expand column names |
238 | | // to match master's behavior: |
239 | | // - Use field_id to find the actual file column name in Parquet schema |
240 | | // - Prefix with __equality_delete_column__ to avoid name conflicts |
241 | | // - Correctly map table_col_name → file_col_name in table_info_node |
242 | 1 | const static std::string EQ_DELETE_PRE = "__equality_delete_column__"; |
243 | 1 | std::unordered_map<int, std::string> field_id_to_file_col_name; |
244 | 4 | for (int i = 0; i < field_desc->size(); ++i) { |
245 | 3 | auto field_schema = field_desc->get_column(i); |
246 | 3 | if (field_schema) { |
247 | 3 | field_id_to_file_col_name[field_schema->field_id] = field_schema->name; |
248 | 3 | } |
249 | 3 | } |
250 | | |
251 | | // Rebuild _expand_col_names with proper file-column-based names |
252 | 1 | std::vector<std::string> new_expand_col_names; |
253 | 1 | for (size_t i = 0; i < _expand_col_names.size(); ++i) { |
254 | 0 | const auto& old_name = _expand_col_names[i]; |
255 | | // Find the field_id for this expand column |
256 | 0 | int field_id = -1; |
257 | 0 | for (auto& [fid, name] : _id_to_block_column_name) { |
258 | 0 | if (name == old_name) { |
259 | 0 | field_id = fid; |
260 | 0 | break; |
261 | 0 | } |
262 | 0 | } |
263 | |
|
264 | 0 | std::string file_col_name = old_name; |
265 | 0 | auto it = field_id_to_file_col_name.find(field_id); |
266 | 0 | if (it != field_id_to_file_col_name.end()) { |
267 | 0 | file_col_name = it->second; |
268 | 0 | } |
269 | |
|
270 | 0 | std::string table_col_name = EQ_DELETE_PRE + file_col_name; |
271 | | |
272 | | // Update _id_to_block_column_name |
273 | 0 | if (field_id >= 0) { |
274 | 0 | _id_to_block_column_name[field_id] = table_col_name; |
275 | 0 | } |
276 | | |
277 | | // Update _expand_columns name |
278 | 0 | if (i < _expand_columns.size()) { |
279 | 0 | _expand_columns[i].name = table_col_name; |
280 | 0 | } |
281 | |
|
282 | 0 | new_expand_col_names.push_back(table_col_name); |
283 | | |
284 | | // Add column IDs |
285 | 0 | if (it != field_id_to_file_col_name.end()) { |
286 | 0 | for (int j = 0; j < field_desc->size(); ++j) { |
287 | 0 | auto field_schema = field_desc->get_column(j); |
288 | 0 | if (field_schema && field_schema->field_id == field_id) { |
289 | 0 | ctx->column_ids.insert(field_schema->get_column_id()); |
290 | 0 | break; |
291 | 0 | } |
292 | 0 | } |
293 | 0 | } |
294 | | |
295 | | // Register in table_info_node: table_col_name → file_col_name |
296 | 0 | ctx->column_names.push_back(table_col_name); |
297 | 0 | ctx->table_info_node->add_children(table_col_name, file_col_name, |
298 | 0 | TableSchemaChangeHelper::ConstNode::get_instance()); |
299 | 0 | } |
300 | 1 | _expand_col_names = std::move(new_expand_col_names); |
301 | | |
302 | | // Enable group filtering for Iceberg |
303 | 1 | _filter_groups = true; |
304 | | |
305 | 1 | return Status::OK(); |
306 | 1 | } |
307 | | |
308 | | // ============================================================================ |
309 | | // IcebergParquetReader: _create_column_ids |
310 | | // ============================================================================ |
311 | | ColumnIdResult IcebergParquetReader::_create_column_ids(const FieldDescriptor* field_desc, |
312 | 7 | const TupleDescriptor* tuple_descriptor) { |
313 | 7 | auto* mutable_field_desc = const_cast<FieldDescriptor*>(field_desc); |
314 | 7 | mutable_field_desc->assign_ids(); |
315 | | |
316 | 7 | std::unordered_map<int, const FieldSchema*> iceberg_id_to_field_schema_map; |
317 | 58 | for (int i = 0; i < field_desc->size(); ++i) { |
318 | 51 | auto field_schema = field_desc->get_column(i); |
319 | 51 | if (!field_schema) continue; |
320 | 51 | int iceberg_id = field_schema->field_id; |
321 | 51 | iceberg_id_to_field_schema_map[iceberg_id] = field_schema; |
322 | 51 | } |
323 | | |
324 | 7 | std::set<uint64_t> column_ids; |
325 | 7 | std::set<uint64_t> filter_column_ids; |
326 | | |
327 | 7 | auto process_access_paths = [](const FieldSchema* parquet_field, |
328 | 7 | const std::vector<TColumnAccessPath>& access_paths, |
329 | 14 | std::set<uint64_t>& out_ids) { |
330 | 14 | process_nested_access_paths( |
331 | 14 | parquet_field, access_paths, out_ids, |
332 | 14 | [](const FieldSchema* field) { return field->get_column_id(); }, |
333 | 14 | [](const FieldSchema* field) { return field->get_max_column_id(); }, |
334 | 14 | IcebergParquetNestedColumnUtils::extract_nested_column_ids); |
335 | 14 | }; |
336 | | |
337 | 15 | for (const auto* slot : tuple_descriptor->slots()) { |
338 | 15 | auto it = iceberg_id_to_field_schema_map.find(slot->col_unique_id()); |
339 | 15 | if (it == iceberg_id_to_field_schema_map.end()) { |
340 | 0 | continue; |
341 | 0 | } |
342 | 15 | auto field_schema = it->second; |
343 | | |
344 | 15 | if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY && |
345 | 15 | slot->col_type() != TYPE_MAP)) { |
346 | 7 | column_ids.insert(field_schema->column_id); |
347 | 7 | if (slot->is_predicate()) { |
348 | 0 | filter_column_ids.insert(field_schema->column_id); |
349 | 0 | } |
350 | 7 | continue; |
351 | 7 | } |
352 | | |
353 | 8 | const auto& all_access_paths = slot->all_access_paths(); |
354 | 8 | process_access_paths(field_schema, all_access_paths, column_ids); |
355 | | |
356 | 8 | const auto& predicate_access_paths = slot->predicate_access_paths(); |
357 | 8 | if (!predicate_access_paths.empty()) { |
358 | 6 | process_access_paths(field_schema, predicate_access_paths, filter_column_ids); |
359 | 6 | } |
360 | 8 | } |
361 | 7 | return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids)); |
362 | 7 | } |
363 | | |
364 | | // ============================================================================ |
365 | | // IcebergParquetReader: _read_position_delete_file |
366 | | // ============================================================================ |
367 | | Status IcebergParquetReader::_read_position_delete_file(const TFileRangeDesc* delete_range, |
368 | 1 | DeleteFile* position_delete) { |
369 | 1 | ParquetReader parquet_delete_reader(get_profile(), get_scan_params(), *delete_range, |
370 | 1 | READ_DELETE_FILE_BATCH_SIZE, &get_state()->timezone_obj(), |
371 | 1 | get_io_ctx(), get_state(), _meta_cache); |
372 | | // The delete file range has size=-1 (read whole file). We must disable |
373 | | // row group filtering before init; otherwise _do_init_reader returns EndOfFile |
374 | | // when _filter_groups && _range_size < 0. |
375 | 1 | ParquetInitContext delete_ctx; |
376 | 1 | delete_ctx.filter_groups = false; |
377 | 1 | delete_ctx.column_names = delete_file_col_names; |
378 | 1 | delete_ctx.col_name_to_block_idx = |
379 | 1 | const_cast<std::unordered_map<std::string, uint32_t>*>(&DELETE_COL_NAME_TO_BLOCK_IDX); |
380 | 1 | RETURN_IF_ERROR(parquet_delete_reader.init_reader(&delete_ctx)); |
381 | | |
382 | 0 | const tparquet::FileMetaData* meta_data = parquet_delete_reader.get_meta_data(); |
383 | 0 | bool dictionary_coded = true; |
384 | 0 | for (const auto& row_group : meta_data->row_groups) { |
385 | 0 | const auto& column_chunk = row_group.columns[ICEBERG_FILE_PATH_INDEX]; |
386 | 0 | if (!(column_chunk.__isset.meta_data && has_dict_page(column_chunk.meta_data))) { |
387 | 0 | dictionary_coded = false; |
388 | 0 | break; |
389 | 0 | } |
390 | 0 | } |
391 | 0 | DataTypePtr data_type_file_path = make_nullable(std::make_shared<DataTypeString>()); |
392 | 0 | DataTypePtr data_type_pos = make_nullable(std::make_shared<DataTypeInt64>()); |
393 | 0 | bool eof = false; |
394 | 0 | while (!eof) { |
395 | 0 | Block block = { |
396 | 0 | dictionary_coded |
397 | 0 | ? ColumnWithTypeAndName {ColumnNullable::create(ColumnDictI32::create(), |
398 | 0 | ColumnUInt8::create()), |
399 | 0 | data_type_file_path, ICEBERG_FILE_PATH} |
400 | 0 | : ColumnWithTypeAndName {data_type_file_path, ICEBERG_FILE_PATH}, |
401 | |
|
402 | 0 | {data_type_pos, ICEBERG_ROW_POS}}; |
403 | 0 | size_t read_rows = 0; |
404 | 0 | RETURN_IF_ERROR(parquet_delete_reader.get_next_block(&block, &read_rows, &eof)); |
405 | | |
406 | 0 | if (read_rows <= 0) { |
407 | 0 | break; |
408 | 0 | } |
409 | 0 | RETURN_IF_ERROR(_gen_position_delete_file_range(block, position_delete, read_rows, |
410 | 0 | dictionary_coded)); |
411 | 0 | } |
412 | 0 | return Status::OK(); |
413 | 0 | }; |
414 | | |
415 | | // ============================================================================ |
416 | | // IcebergOrcReader: on_before_init_reader (ORC-specific schema matching) |
417 | | // ============================================================================ |
418 | 1 | Status IcebergOrcReader::on_before_init_reader(ReaderInitContext* ctx) { |
419 | 1 | _column_descs = ctx->column_descs; |
420 | 1 | _fill_col_name_to_block_idx = ctx->col_name_to_block_idx; |
421 | 1 | _file_format = Fileformat::ORC; |
422 | | |
423 | | // Get ORC file type first (available because _create_file_reader() already ran) |
424 | 1 | const orc::Type* orc_type_ptr = nullptr; |
425 | 1 | RETURN_IF_ERROR(this->get_file_type(&orc_type_ptr)); |
426 | | |
427 | | // Build table_info_node by field_id or name matching. |
428 | | // This must happen BEFORE column classification so we can use children_column_exists |
429 | | // to check if a column exists in the file (by field ID, not name). |
430 | 1 | if (!get_scan_params().__isset.history_schema_info || |
431 | 1 | get_scan_params().history_schema_info.empty()) [[unlikely]] { |
432 | 1 | RETURN_IF_ERROR(BuildTableInfoUtil::by_orc_name(ctx->tuple_descriptor, orc_type_ptr, |
433 | 1 | ctx->table_info_node)); |
434 | 1 | } else { |
435 | 0 | RETURN_IF_ERROR(BuildTableInfoUtil::by_orc_field_id_with_name_mapping( |
436 | 0 | get_scan_params().history_schema_info.front().root_field, orc_type_ptr, |
437 | 0 | ICEBERG_ORC_ATTRIBUTE, ctx->table_info_node)); |
438 | 0 | } |
439 | | |
440 | 1 | std::unordered_set<std::string> partition_col_names; |
441 | 1 | if (ctx->range->__isset.columns_from_path_keys) { |
442 | 0 | partition_col_names.insert(ctx->range->columns_from_path_keys.begin(), |
443 | 0 | ctx->range->columns_from_path_keys.end()); |
444 | 0 | } |
445 | | |
446 | | // Single pass: classify columns, detect $row_id, handle partition fallback. |
447 | 1 | bool has_partition_from_path = false; |
448 | 2 | for (const auto& desc : *ctx->column_descs) { |
449 | 2 | if (desc.category == ColumnCategory::SYNTHESIZED) { |
450 | 0 | if (desc.name == BeConsts::ICEBERG_ROWID_COL) { |
451 | 0 | this->register_synthesized_column_handler( |
452 | 0 | BeConsts::ICEBERG_ROWID_COL, [this](Block* block, size_t rows) -> Status { |
453 | 0 | return _fill_iceberg_row_id(block, rows); |
454 | 0 | }); |
455 | 0 | continue; |
456 | 0 | } else if (desc.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) { |
457 | 0 | auto topn_row_id_column_iter = _create_topn_row_id_column_iterator(); |
458 | 0 | this->register_synthesized_column_handler( |
459 | 0 | desc.name, |
460 | 0 | [iter = std::move(topn_row_id_column_iter), this, &desc]( |
461 | 0 | Block* block, size_t rows) -> Status { |
462 | 0 | return fill_topn_row_id(iter, desc.name, block, rows); |
463 | 0 | }); |
464 | 0 | continue; |
465 | 0 | } |
466 | 2 | } else if (desc.category == ColumnCategory::PARTITION_KEY) { |
467 | 0 | bool has_partition_value = partition_col_names.contains(desc.name); |
468 | 0 | bool exists_in_file = ctx->table_info_node->children_column_exists(desc.name); |
469 | 0 | if (!has_partition_value || exists_in_file) { |
470 | 0 | ctx->column_names.push_back(desc.name); |
471 | 0 | continue; |
472 | 0 | } |
473 | 0 | has_partition_from_path = true; |
474 | 2 | } else if (desc.category == ColumnCategory::REGULAR) { |
475 | 2 | ctx->column_names.push_back(desc.name); |
476 | 2 | } else if (desc.category == ColumnCategory::GENERATED) { |
477 | 0 | _init_row_lineage_columns(); |
478 | 0 | if (desc.name == ROW_LINEAGE_ROW_ID) { |
479 | 0 | ctx->column_names.push_back(desc.name); |
480 | 0 | this->register_generated_column_handler( |
481 | 0 | ROW_LINEAGE_ROW_ID, [this](Block* block, size_t rows) -> Status { |
482 | 0 | return _fill_row_lineage_row_id(block, rows); |
483 | 0 | }); |
484 | 0 | continue; |
485 | 0 | } else if (desc.name == ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER) { |
486 | 0 | ctx->column_names.push_back(desc.name); |
487 | 0 | this->register_generated_column_handler( |
488 | 0 | ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER, |
489 | 0 | [this](Block* block, size_t rows) -> Status { |
490 | 0 | return _fill_row_lineage_last_updated_sequence_number(block, rows); |
491 | 0 | }); |
492 | 0 | continue; |
493 | 0 | } |
494 | 0 | } |
495 | 2 | } |
496 | | |
497 | 1 | if (has_partition_from_path) { |
498 | 0 | RETURN_IF_ERROR(_extract_partition_values(*ctx->range, ctx->tuple_descriptor, |
499 | 0 | _fill_partition_values, |
500 | 0 | &_fill_partition_value_is_null)); |
501 | 0 | } |
502 | | |
503 | 1 | _all_required_col_names = ctx->column_names; |
504 | | |
505 | | // Create column IDs from ORC type |
506 | 1 | auto column_id_result = _create_column_ids(orc_type_ptr, ctx->tuple_descriptor); |
507 | 1 | ctx->column_ids = std::move(column_id_result.column_ids); |
508 | 1 | ctx->filter_column_ids = std::move(column_id_result.filter_column_ids); |
509 | | |
510 | | // Build field_id -> block_column_name mapping for equality delete filtering. |
511 | 2 | for (const auto* slot : ctx->tuple_descriptor->slots()) { |
512 | 2 | _id_to_block_column_name.emplace(slot->col_unique_id(), slot->col_name()); |
513 | 2 | } |
514 | | |
515 | | // Process delete files (must happen before _do_init_reader so expand col IDs are included) |
516 | 1 | RETURN_IF_ERROR(_init_row_filters()); |
517 | | |
518 | | // Add expand column IDs for equality delete and remap expand column names |
519 | | // (matching master's behavior with __equality_delete_column__ prefix) |
520 | 1 | const static std::string EQ_DELETE_PRE = "__equality_delete_column__"; |
521 | 1 | std::unordered_map<int, std::string> field_id_to_file_col_name; |
522 | 4 | for (uint64_t i = 0; i < orc_type_ptr->getSubtypeCount(); ++i) { |
523 | 3 | std::string col_name = orc_type_ptr->getFieldName(i); |
524 | 3 | const orc::Type* sub_type = orc_type_ptr->getSubtype(i); |
525 | 3 | if (sub_type->hasAttributeKey(ICEBERG_ORC_ATTRIBUTE)) { |
526 | 3 | int fid = std::stoi(sub_type->getAttributeValue(ICEBERG_ORC_ATTRIBUTE)); |
527 | 3 | field_id_to_file_col_name[fid] = col_name; |
528 | 3 | } |
529 | 3 | } |
530 | | |
531 | 1 | std::vector<std::string> new_expand_col_names; |
532 | 1 | for (size_t i = 0; i < _expand_col_names.size(); ++i) { |
533 | 0 | const auto& old_name = _expand_col_names[i]; |
534 | 0 | int field_id = -1; |
535 | 0 | for (auto& [fid, name] : _id_to_block_column_name) { |
536 | 0 | if (name == old_name) { |
537 | 0 | field_id = fid; |
538 | 0 | break; |
539 | 0 | } |
540 | 0 | } |
541 | |
|
542 | 0 | std::string file_col_name = old_name; |
543 | 0 | auto it = field_id_to_file_col_name.find(field_id); |
544 | 0 | if (it != field_id_to_file_col_name.end()) { |
545 | 0 | file_col_name = it->second; |
546 | 0 | } |
547 | |
|
548 | 0 | std::string table_col_name = EQ_DELETE_PRE + file_col_name; |
549 | |
|
550 | 0 | if (field_id >= 0) { |
551 | 0 | _id_to_block_column_name[field_id] = table_col_name; |
552 | 0 | } |
553 | 0 | if (i < _expand_columns.size()) { |
554 | 0 | _expand_columns[i].name = table_col_name; |
555 | 0 | } |
556 | 0 | new_expand_col_names.push_back(table_col_name); |
557 | | |
558 | | // Add column IDs |
559 | 0 | if (it != field_id_to_file_col_name.end()) { |
560 | 0 | for (uint64_t j = 0; j < orc_type_ptr->getSubtypeCount(); ++j) { |
561 | 0 | const orc::Type* sub_type = orc_type_ptr->getSubtype(j); |
562 | 0 | if (orc_type_ptr->getFieldName(j) == file_col_name) { |
563 | 0 | ctx->column_ids.insert(sub_type->getColumnId()); |
564 | 0 | break; |
565 | 0 | } |
566 | 0 | } |
567 | 0 | } |
568 | |
|
569 | 0 | ctx->column_names.push_back(table_col_name); |
570 | 0 | ctx->table_info_node->add_children(table_col_name, file_col_name, |
571 | 0 | TableSchemaChangeHelper::ConstNode::get_instance()); |
572 | 0 | } |
573 | 1 | _expand_col_names = std::move(new_expand_col_names); |
574 | | |
575 | 1 | return Status::OK(); |
576 | 1 | } |
577 | | |
578 | | // ============================================================================ |
579 | | // IcebergOrcReader: _create_column_ids |
580 | | // ============================================================================ |
581 | | ColumnIdResult IcebergOrcReader::_create_column_ids(const orc::Type* orc_type, |
582 | 7 | const TupleDescriptor* tuple_descriptor) { |
583 | 7 | std::unordered_map<int, const orc::Type*> iceberg_id_to_orc_type_map; |
584 | 58 | for (uint64_t i = 0; i < orc_type->getSubtypeCount(); ++i) { |
585 | 51 | auto orc_sub_type = orc_type->getSubtype(i); |
586 | 51 | if (!orc_sub_type) continue; |
587 | 51 | if (!orc_sub_type->hasAttributeKey(ICEBERG_ORC_ATTRIBUTE)) { |
588 | 0 | continue; |
589 | 0 | } |
590 | 51 | int iceberg_id = std::stoi(orc_sub_type->getAttributeValue(ICEBERG_ORC_ATTRIBUTE)); |
591 | 51 | iceberg_id_to_orc_type_map[iceberg_id] = orc_sub_type; |
592 | 51 | } |
593 | | |
594 | 7 | std::set<uint64_t> column_ids; |
595 | 7 | std::set<uint64_t> filter_column_ids; |
596 | | |
597 | 7 | auto process_access_paths = [](const orc::Type* orc_field, |
598 | 7 | const std::vector<TColumnAccessPath>& access_paths, |
599 | 14 | std::set<uint64_t>& out_ids) { |
600 | 14 | process_nested_access_paths( |
601 | 14 | orc_field, access_paths, out_ids, |
602 | 14 | [](const orc::Type* type) { return type->getColumnId(); }, |
603 | 14 | [](const orc::Type* type) { return type->getMaximumColumnId(); }, |
604 | 14 | IcebergOrcNestedColumnUtils::extract_nested_column_ids); |
605 | 14 | }; |
606 | | |
607 | 15 | for (const auto* slot : tuple_descriptor->slots()) { |
608 | 15 | auto it = iceberg_id_to_orc_type_map.find(slot->col_unique_id()); |
609 | 15 | if (it == iceberg_id_to_orc_type_map.end()) { |
610 | 0 | continue; |
611 | 0 | } |
612 | 15 | const orc::Type* orc_field = it->second; |
613 | | |
614 | 15 | if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY && |
615 | 15 | slot->col_type() != TYPE_MAP)) { |
616 | 7 | column_ids.insert(orc_field->getColumnId()); |
617 | 7 | if (slot->is_predicate()) { |
618 | 0 | filter_column_ids.insert(orc_field->getColumnId()); |
619 | 0 | } |
620 | 7 | continue; |
621 | 7 | } |
622 | | |
623 | 8 | const auto& all_access_paths = slot->all_access_paths(); |
624 | 8 | process_access_paths(orc_field, all_access_paths, column_ids); |
625 | | |
626 | 8 | const auto& predicate_access_paths = slot->predicate_access_paths(); |
627 | 8 | if (!predicate_access_paths.empty()) { |
628 | 6 | process_access_paths(orc_field, predicate_access_paths, filter_column_ids); |
629 | 6 | } |
630 | 8 | } |
631 | | |
632 | 7 | return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids)); |
633 | 7 | } |
634 | | |
635 | | // ============================================================================ |
636 | | // IcebergOrcReader: _read_position_delete_file |
637 | | // ============================================================================ |
638 | | Status IcebergOrcReader::_read_position_delete_file(const TFileRangeDesc* delete_range, |
639 | 0 | DeleteFile* position_delete) { |
640 | 0 | OrcReader orc_delete_reader(get_profile(), get_state(), get_scan_params(), *delete_range, |
641 | 0 | READ_DELETE_FILE_BATCH_SIZE, get_state()->timezone(), get_io_ctx(), |
642 | 0 | _meta_cache); |
643 | 0 | OrcInitContext delete_ctx; |
644 | 0 | delete_ctx.column_names = delete_file_col_names; |
645 | 0 | delete_ctx.col_name_to_block_idx = |
646 | 0 | const_cast<std::unordered_map<std::string, uint32_t>*>(&DELETE_COL_NAME_TO_BLOCK_IDX); |
647 | 0 | RETURN_IF_ERROR(orc_delete_reader.init_reader(&delete_ctx)); |
648 | | |
649 | 0 | bool eof = false; |
650 | 0 | DataTypePtr data_type_file_path {new DataTypeString}; |
651 | 0 | DataTypePtr data_type_pos {new DataTypeInt64}; |
652 | 0 | while (!eof) { |
653 | 0 | Block block = {{data_type_file_path, ICEBERG_FILE_PATH}, {data_type_pos, ICEBERG_ROW_POS}}; |
654 | |
|
655 | 0 | size_t read_rows = 0; |
656 | 0 | RETURN_IF_ERROR(orc_delete_reader.get_next_block(&block, &read_rows, &eof)); |
657 | | |
658 | 0 | RETURN_IF_ERROR(_gen_position_delete_file_range(block, position_delete, read_rows, false)); |
659 | 0 | } |
660 | 0 | return Status::OK(); |
661 | 0 | } |
662 | | |
663 | | } // namespace doris |