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 | 120 | Status IcebergParquetReader::on_before_init_reader(ReaderInitContext* ctx) { |
133 | 120 | _column_descs = ctx->column_descs; |
134 | 120 | _fill_col_name_to_block_idx = ctx->col_name_to_block_idx; |
135 | 120 | _file_format = Fileformat::PARQUET; |
136 | | |
137 | | // Get file metadata schema first (available because _open_file() already ran) |
138 | 120 | const FieldDescriptor* field_desc = nullptr; |
139 | 120 | RETURN_IF_ERROR(this->get_file_metadata_schema(&field_desc)); |
140 | 120 | 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 | 120 | if (!get_scan_params().__isset.history_schema_info || |
146 | 120 | 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 | 119 | } else { |
150 | 119 | RETURN_IF_ERROR(BuildTableInfoUtil::by_parquet_field_id_with_name_mapping( |
151 | 119 | get_scan_params().history_schema_info.front().root_field, *field_desc, |
152 | 119 | ctx->table_info_node)); |
153 | 119 | } |
154 | | |
155 | 120 | std::unordered_set<std::string> partition_col_names; |
156 | 120 | if (ctx->range->__isset.columns_from_path_keys) { |
157 | 38 | partition_col_names.insert(ctx->range->columns_from_path_keys.begin(), |
158 | 38 | ctx->range->columns_from_path_keys.end()); |
159 | 38 | } |
160 | | |
161 | | // Single pass: classify columns, detect $row_id, handle partition fallback. |
162 | 120 | bool has_partition_from_path = false; |
163 | 767 | for (const auto& desc : *ctx->column_descs) { |
164 | 767 | 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 | 767 | } else if (desc.category == ColumnCategory::PARTITION_KEY) { |
182 | 38 | bool has_partition_value = partition_col_names.contains(desc.name); |
183 | 38 | bool exists_in_file = ctx->table_info_node->children_column_exists(desc.name); |
184 | 38 | 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 | 38 | ctx->column_names.push_back(desc.name); |
188 | 38 | continue; |
189 | 38 | } |
190 | 0 | has_partition_from_path = true; |
191 | 729 | } else if (desc.category == ColumnCategory::REGULAR) { |
192 | 729 | ctx->column_names.push_back(desc.name); |
193 | 729 | } 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 | 767 | } |
213 | | |
214 | | // Set up partition value extraction if any partition columns need filling from path |
215 | 120 | 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 | 120 | _all_required_col_names = ctx->column_names; |
222 | | |
223 | | // Create column IDs from field descriptor |
224 | 120 | auto column_id_result = |
225 | 120 | _create_column_ids(field_desc, ctx->tuple_descriptor, ctx->table_info_node); |
226 | 120 | ctx->column_ids = std::move(column_id_result.column_ids); |
227 | 120 | ctx->filter_column_ids = std::move(column_id_result.filter_column_ids); |
228 | | |
229 | | // Build field_id -> block_column_name mapping for equality delete filtering. |
230 | | // This was previously done in init_reader() column matching (pre-CRTP refactoring). |
231 | 771 | for (const auto* slot : ctx->tuple_descriptor->slots()) { |
232 | 771 | _id_to_block_column_name.emplace(slot->col_unique_id(), slot->col_name()); |
233 | 771 | } |
234 | | |
235 | | // Process delete files (must happen before _do_init_reader so expand col IDs are included) |
236 | 120 | RETURN_IF_ERROR(_init_row_filters()); |
237 | | |
238 | | // Add expand column IDs for equality delete and remap expand column names |
239 | | // to match master's behavior: |
240 | | // - Use field_id to find the actual file column name in Parquet schema |
241 | | // - Prefix with __equality_delete_column__ to avoid name conflicts |
242 | | // - Correctly map table_col_name → file_col_name in table_info_node |
243 | 112 | const static std::string EQ_DELETE_PRE = "__equality_delete_column__"; |
244 | 112 | std::unordered_map<int, const FieldSchema*> field_id_to_file_column; |
245 | 112 | bool all_file_columns_have_field_ids = true; |
246 | 1.00k | for (int i = 0; i < field_desc->size(); ++i) { |
247 | 888 | const auto* field_schema = field_desc->get_column(i); |
248 | 888 | if (field_schema) { |
249 | 886 | field_id_to_file_column[field_schema->field_id] = field_schema; |
250 | 886 | if (field_schema->field_id < 0) { |
251 | 2 | all_file_columns_have_field_ids = false; |
252 | 2 | } |
253 | 886 | } |
254 | 888 | } |
255 | 112 | const auto struct_node = |
256 | 112 | std::dynamic_pointer_cast<TableSchemaChangeHelper::StructNode>(ctx->table_info_node); |
257 | 112 | DORIS_CHECK(struct_node != nullptr); |
258 | | |
259 | | // Rebuild _expand_col_names with proper file-column-based names |
260 | 112 | std::vector<std::string> new_expand_col_names; |
261 | 113 | for (size_t i = 0; i < _expand_col_names.size(); ++i) { |
262 | 1 | const auto& old_name = _expand_col_names[i]; |
263 | | // Find the field_id for this expand column |
264 | 1 | int field_id = -1; |
265 | 1 | for (auto& [fid, name] : _id_to_block_column_name) { |
266 | 1 | if (name == old_name) { |
267 | 1 | field_id = fid; |
268 | 1 | break; |
269 | 1 | } |
270 | 1 | } |
271 | | |
272 | 1 | const FieldSchema* file_column = nullptr; |
273 | 1 | if (!all_file_columns_have_field_ids && struct_node->get_children().contains(old_name) && |
274 | 1 | struct_node->children_column_exists(old_name)) { |
275 | | // Iceberg files written without field ids must use schema.name-mapping.default. The |
276 | | // root schema mapper deliberately switches the whole file to BY_NAME when even one |
277 | | // top-level field id is absent. Hidden equality keys must make the same choice: a |
278 | | // different physical column may still carry this key's stale id after migration. |
279 | 1 | const auto& mapped_name = struct_node->children_file_column_name(old_name); |
280 | 2 | for (int j = 0; j < field_desc->size(); ++j) { |
281 | 2 | const auto* candidate = field_desc->get_column(j); |
282 | 2 | if (candidate != nullptr && candidate->name == mapped_name) { |
283 | 1 | file_column = candidate; |
284 | 1 | break; |
285 | 1 | } |
286 | 2 | } |
287 | 1 | DORIS_CHECK(file_column != nullptr); |
288 | 1 | } else if (all_file_columns_have_field_ids) { |
289 | 0 | auto id_it = field_id_to_file_column.find(field_id); |
290 | 0 | if (id_it != field_id_to_file_column.end()) { |
291 | 0 | file_column = id_it->second; |
292 | 0 | } |
293 | 0 | } |
294 | | |
295 | 1 | const std::string file_col_name = file_column == nullptr ? old_name : file_column->name; |
296 | 1 | std::string table_col_name = EQ_DELETE_PRE + file_col_name; |
297 | | |
298 | | // Update _id_to_block_column_name |
299 | 1 | if (field_id >= 0) { |
300 | 1 | _id_to_block_column_name[field_id] = table_col_name; |
301 | 1 | } |
302 | | |
303 | | // Update _expand_columns name |
304 | 1 | if (i < _expand_columns.size()) { |
305 | 1 | _expand_columns[i].name = table_col_name; |
306 | 1 | } |
307 | | |
308 | 1 | if (file_column == nullptr) { |
309 | 0 | DORIS_CHECK(i < _expand_columns.size()); |
310 | 0 | RETURN_IF_ERROR(_register_missing_equality_delete_column(field_id, table_col_name, |
311 | 0 | _expand_columns[i].type)); |
312 | | // The old data file predates this equality key. Keep it in the expand block so the |
313 | | // synthesized-column hook can materialize its logical initial default before reader |
314 | | // filtering, but do not advertise it to Parquet as a physical child. |
315 | 0 | new_expand_col_names.push_back(table_col_name); |
316 | 0 | continue; |
317 | 0 | } |
318 | | |
319 | 1 | new_expand_col_names.push_back(table_col_name); |
320 | | |
321 | | // Add column IDs |
322 | 1 | ctx->column_ids.insert(file_column->get_column_id()); |
323 | | |
324 | | // Register in table_info_node: table_col_name → file_col_name |
325 | 1 | ctx->column_names.push_back(table_col_name); |
326 | 1 | ctx->table_info_node->add_children(table_col_name, file_col_name, |
327 | 1 | TableSchemaChangeHelper::ConstNode::get_instance()); |
328 | 1 | } |
329 | 112 | _expand_col_names = std::move(new_expand_col_names); |
330 | | |
331 | | // Enable group filtering for Iceberg |
332 | 112 | _filter_groups = true; |
333 | | |
334 | 112 | return Status::OK(); |
335 | 112 | } |
336 | | |
337 | | // ============================================================================ |
338 | | // IcebergParquetReader: _create_column_ids |
339 | | // ============================================================================ |
340 | | ColumnIdResult IcebergParquetReader::_create_column_ids( |
341 | | const FieldDescriptor* field_desc, const TupleDescriptor* tuple_descriptor, |
342 | 124 | const std::shared_ptr<TableSchemaChangeHelper::Node>& table_info_node) { |
343 | 124 | auto* mutable_field_desc = const_cast<FieldDescriptor*>(field_desc); |
344 | 124 | mutable_field_desc->assign_ids(); |
345 | | |
346 | 124 | std::unordered_map<int, const FieldSchema*> iceberg_id_to_field_schema_map; |
347 | 1.07k | for (int i = 0; i < field_desc->size(); ++i) { |
348 | 950 | auto field_schema = field_desc->get_column(i); |
349 | 950 | if (!field_schema) continue; |
350 | 950 | int iceberg_id = field_schema->field_id; |
351 | 950 | iceberg_id_to_field_schema_map[iceberg_id] = field_schema; |
352 | 950 | } |
353 | | |
354 | 124 | std::set<uint64_t> column_ids; |
355 | 124 | std::set<uint64_t> filter_column_ids; |
356 | | |
357 | 124 | auto process_access_paths = [](const FieldSchema* parquet_field, |
358 | 124 | const std::vector<TColumnAccessPath>& access_paths, |
359 | 124 | std::set<uint64_t>& out_ids) { |
360 | 14 | process_nested_access_paths( |
361 | 14 | parquet_field, access_paths, out_ids, |
362 | 14 | [](const FieldSchema* field) { return field->get_column_id(); }, |
363 | 14 | [](const FieldSchema* field) { return field->get_max_column_id(); }, |
364 | 14 | IcebergParquetNestedColumnUtils::extract_nested_column_ids); |
365 | 14 | }; |
366 | | |
367 | 784 | for (const auto* slot : tuple_descriptor->slots()) { |
368 | 784 | const FieldSchema* field_schema = nullptr; |
369 | 784 | if (table_info_node != nullptr) { |
370 | 771 | if (table_info_node->children_column_exists(slot->col_name())) { |
371 | | // Use the physical child selected by the schema-mapping pass. This keeps partial-id |
372 | | // files in BY_NAME mode from binding a projected column through an unrelated stale |
373 | | // field id. |
374 | 749 | const auto& file_column_name = |
375 | 749 | table_info_node->children_file_column_name(slot->col_name()); |
376 | 6.03k | for (int i = 0; i < field_desc->size(); ++i) { |
377 | 6.03k | const auto* candidate = field_desc->get_column(i); |
378 | 6.03k | if (candidate != nullptr && candidate->name == file_column_name) { |
379 | 749 | field_schema = candidate; |
380 | 749 | break; |
381 | 749 | } |
382 | 6.03k | } |
383 | 749 | DORIS_CHECK(field_schema != nullptr); |
384 | 749 | } |
385 | 771 | } else { |
386 | 13 | auto it = iceberg_id_to_field_schema_map.find(slot->col_unique_id()); |
387 | 13 | if (it != iceberg_id_to_field_schema_map.end()) { |
388 | 13 | field_schema = it->second; |
389 | 13 | } |
390 | 13 | } |
391 | 784 | if (field_schema == nullptr) { |
392 | 18 | continue; |
393 | 18 | } |
394 | | |
395 | 766 | if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY && |
396 | 766 | slot->col_type() != TYPE_MAP)) { |
397 | 748 | column_ids.insert(field_schema->column_id); |
398 | 748 | if (slot->is_predicate()) { |
399 | 0 | filter_column_ids.insert(field_schema->column_id); |
400 | 0 | } |
401 | 748 | continue; |
402 | 748 | } |
403 | | |
404 | 18 | const auto& all_access_paths = slot->all_access_paths(); |
405 | 18 | process_access_paths(field_schema, all_access_paths, column_ids); |
406 | | |
407 | 18 | const auto& predicate_access_paths = slot->predicate_access_paths(); |
408 | 18 | if (!predicate_access_paths.empty()) { |
409 | 6 | process_access_paths(field_schema, predicate_access_paths, filter_column_ids); |
410 | 6 | } |
411 | 18 | } |
412 | 124 | return {std::move(column_ids), std::move(filter_column_ids)}; |
413 | 124 | } |
414 | | |
415 | | // ============================================================================ |
416 | | // IcebergParquetReader: _read_position_delete_file |
417 | | // ============================================================================ |
418 | | Status IcebergParquetReader::_read_position_delete_file(const TFileRangeDesc* delete_range, |
419 | 1 | DeleteFile* position_delete) { |
420 | 1 | ParquetReader parquet_delete_reader(get_profile(), get_scan_params(), *delete_range, |
421 | 1 | READ_DELETE_FILE_BATCH_SIZE, &get_state()->timezone_obj(), |
422 | 1 | get_io_ctx(), get_state(), _meta_cache); |
423 | | // The delete file range has size=-1 (read whole file). We must disable |
424 | | // row group filtering before init; otherwise _do_init_reader returns EndOfFile |
425 | | // when _filter_groups && _range_size < 0. |
426 | 1 | ParquetInitContext delete_ctx; |
427 | 1 | delete_ctx.filter_groups = false; |
428 | 1 | delete_ctx.column_names = delete_file_col_names; |
429 | 1 | delete_ctx.col_name_to_block_idx = |
430 | 1 | const_cast<std::unordered_map<std::string, uint32_t>*>(&DELETE_COL_NAME_TO_BLOCK_IDX); |
431 | 1 | RETURN_IF_ERROR(parquet_delete_reader.init_reader(&delete_ctx)); |
432 | | |
433 | 0 | const tparquet::FileMetaData* meta_data = parquet_delete_reader.get_meta_data(); |
434 | 0 | bool dictionary_coded = true; |
435 | 0 | for (const auto& row_group : meta_data->row_groups) { |
436 | 0 | const auto& column_chunk = row_group.columns[ICEBERG_FILE_PATH_INDEX]; |
437 | 0 | if (!(column_chunk.__isset.meta_data && has_dict_page(column_chunk.meta_data))) { |
438 | 0 | dictionary_coded = false; |
439 | 0 | break; |
440 | 0 | } |
441 | 0 | } |
442 | 0 | DataTypePtr data_type_file_path = make_nullable(std::make_shared<DataTypeString>()); |
443 | 0 | DataTypePtr data_type_pos = make_nullable(std::make_shared<DataTypeInt64>()); |
444 | 0 | bool eof = false; |
445 | 0 | while (!eof) { |
446 | 0 | Block block = { |
447 | 0 | dictionary_coded |
448 | 0 | ? ColumnWithTypeAndName {ColumnNullable::create(ColumnDictI32::create(), |
449 | 0 | ColumnUInt8::create()), |
450 | 0 | data_type_file_path, ICEBERG_FILE_PATH} |
451 | 0 | : ColumnWithTypeAndName {data_type_file_path, ICEBERG_FILE_PATH}, |
452 | |
|
453 | 0 | {data_type_pos, ICEBERG_ROW_POS}}; |
454 | 0 | size_t read_rows = 0; |
455 | 0 | RETURN_IF_ERROR(parquet_delete_reader.get_next_block(&block, &read_rows, &eof)); |
456 | | |
457 | 0 | if (read_rows <= 0) { |
458 | 0 | break; |
459 | 0 | } |
460 | 0 | RETURN_IF_ERROR(_gen_position_delete_file_range(block, position_delete, read_rows, |
461 | 0 | dictionary_coded)); |
462 | 0 | } |
463 | 0 | return Status::OK(); |
464 | 0 | }; |
465 | | |
466 | | // ============================================================================ |
467 | | // IcebergOrcReader: on_before_init_reader (ORC-specific schema matching) |
468 | | // ============================================================================ |
469 | 101 | Status IcebergOrcReader::on_before_init_reader(ReaderInitContext* ctx) { |
470 | 101 | _column_descs = ctx->column_descs; |
471 | 101 | _fill_col_name_to_block_idx = ctx->col_name_to_block_idx; |
472 | 101 | _file_format = Fileformat::ORC; |
473 | | |
474 | | // Get ORC file type first (available because _create_file_reader() already ran) |
475 | 101 | const orc::Type* orc_type_ptr = nullptr; |
476 | 101 | RETURN_IF_ERROR(this->get_file_type(&orc_type_ptr)); |
477 | | |
478 | | // Build table_info_node by field_id or name matching. |
479 | | // This must happen BEFORE column classification so we can use children_column_exists |
480 | | // to check if a column exists in the file (by field ID, not name). |
481 | 101 | if (!get_scan_params().__isset.history_schema_info || |
482 | 101 | get_scan_params().history_schema_info.empty()) [[unlikely]] { |
483 | 1 | RETURN_IF_ERROR(BuildTableInfoUtil::by_orc_name(ctx->tuple_descriptor, orc_type_ptr, |
484 | 1 | ctx->table_info_node)); |
485 | 100 | } else { |
486 | 100 | RETURN_IF_ERROR(BuildTableInfoUtil::by_orc_field_id_with_name_mapping( |
487 | 100 | get_scan_params().history_schema_info.front().root_field, orc_type_ptr, |
488 | 100 | ICEBERG_ORC_ATTRIBUTE, ctx->table_info_node)); |
489 | 100 | } |
490 | | |
491 | 101 | std::unordered_set<std::string> partition_col_names; |
492 | 101 | if (ctx->range->__isset.columns_from_path_keys) { |
493 | 0 | partition_col_names.insert(ctx->range->columns_from_path_keys.begin(), |
494 | 0 | ctx->range->columns_from_path_keys.end()); |
495 | 0 | } |
496 | | |
497 | | // Single pass: classify columns, detect $row_id, handle partition fallback. |
498 | 101 | bool has_partition_from_path = false; |
499 | 162 | for (const auto& desc : *ctx->column_descs) { |
500 | 162 | if (desc.category == ColumnCategory::SYNTHESIZED) { |
501 | 0 | if (desc.name == BeConsts::ICEBERG_ROWID_COL) { |
502 | 0 | this->register_synthesized_column_handler( |
503 | 0 | BeConsts::ICEBERG_ROWID_COL, [this](Block* block, size_t rows) -> Status { |
504 | 0 | return _fill_iceberg_row_id(block, rows); |
505 | 0 | }); |
506 | 0 | continue; |
507 | 0 | } else if (desc.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) { |
508 | 0 | auto topn_row_id_column_iter = _create_topn_row_id_column_iterator(); |
509 | 0 | this->register_synthesized_column_handler( |
510 | 0 | desc.name, |
511 | 0 | [iter = std::move(topn_row_id_column_iter), this, &desc]( |
512 | 0 | Block* block, size_t rows) -> Status { |
513 | 0 | return fill_topn_row_id(iter, desc.name, block, rows); |
514 | 0 | }); |
515 | 0 | continue; |
516 | 0 | } |
517 | 162 | } else if (desc.category == ColumnCategory::PARTITION_KEY) { |
518 | 0 | bool has_partition_value = partition_col_names.contains(desc.name); |
519 | 0 | bool exists_in_file = ctx->table_info_node->children_column_exists(desc.name); |
520 | 0 | if (!has_partition_value || exists_in_file) { |
521 | 0 | ctx->column_names.push_back(desc.name); |
522 | 0 | continue; |
523 | 0 | } |
524 | 0 | has_partition_from_path = true; |
525 | 164 | } else if (desc.category == ColumnCategory::REGULAR) { |
526 | 164 | ctx->column_names.push_back(desc.name); |
527 | 18.4E | } else if (desc.category == ColumnCategory::GENERATED) { |
528 | 0 | _init_row_lineage_columns(); |
529 | 0 | if (desc.name == ROW_LINEAGE_ROW_ID) { |
530 | 0 | ctx->column_names.push_back(desc.name); |
531 | 0 | this->register_generated_column_handler( |
532 | 0 | ROW_LINEAGE_ROW_ID, [this](Block* block, size_t rows) -> Status { |
533 | 0 | return _fill_row_lineage_row_id(block, rows); |
534 | 0 | }); |
535 | 0 | continue; |
536 | 0 | } else if (desc.name == ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER) { |
537 | 0 | ctx->column_names.push_back(desc.name); |
538 | 0 | this->register_generated_column_handler( |
539 | 0 | ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER, |
540 | 0 | [this](Block* block, size_t rows) -> Status { |
541 | 0 | return _fill_row_lineage_last_updated_sequence_number(block, rows); |
542 | 0 | }); |
543 | 0 | continue; |
544 | 0 | } |
545 | 0 | } |
546 | 162 | } |
547 | | |
548 | 101 | if (has_partition_from_path) { |
549 | 0 | RETURN_IF_ERROR(_extract_partition_values(*ctx->range, ctx->tuple_descriptor, |
550 | 0 | _fill_partition_values, |
551 | 0 | &_fill_partition_value_is_null)); |
552 | 0 | } |
553 | | |
554 | 101 | _all_required_col_names = ctx->column_names; |
555 | | |
556 | | // Create column IDs from ORC type |
557 | 101 | auto column_id_result = |
558 | 101 | _create_column_ids(orc_type_ptr, ctx->tuple_descriptor, ctx->table_info_node); |
559 | 101 | ctx->column_ids = std::move(column_id_result.column_ids); |
560 | 101 | ctx->filter_column_ids = std::move(column_id_result.filter_column_ids); |
561 | | |
562 | | // Build field_id -> block_column_name mapping for equality delete filtering. |
563 | 162 | for (const auto* slot : ctx->tuple_descriptor->slots()) { |
564 | 162 | _id_to_block_column_name.emplace(slot->col_unique_id(), slot->col_name()); |
565 | 162 | } |
566 | | |
567 | | // Process delete files (must happen before _do_init_reader so expand col IDs are included) |
568 | 101 | RETURN_IF_ERROR(_init_row_filters()); |
569 | | |
570 | | // Add expand column IDs for equality delete and remap expand column names |
571 | | // (matching master's behavior with __equality_delete_column__ prefix) |
572 | 101 | const static std::string EQ_DELETE_PRE = "__equality_delete_column__"; |
573 | 101 | std::unordered_map<int, const orc::Type*> field_id_to_file_column; |
574 | 101 | bool all_file_columns_have_field_ids = true; |
575 | 356 | for (uint64_t i = 0; i < orc_type_ptr->getSubtypeCount(); ++i) { |
576 | 255 | const orc::Type* sub_type = orc_type_ptr->getSubtype(i); |
577 | 255 | if (sub_type->hasAttributeKey(ICEBERG_ORC_ATTRIBUTE)) { |
578 | 255 | int fid = std::stoi(sub_type->getAttributeValue(ICEBERG_ORC_ATTRIBUTE)); |
579 | 255 | field_id_to_file_column[fid] = sub_type; |
580 | 255 | } else { |
581 | 0 | all_file_columns_have_field_ids = false; |
582 | 0 | } |
583 | 255 | } |
584 | 101 | const auto struct_node = |
585 | 101 | std::dynamic_pointer_cast<TableSchemaChangeHelper::StructNode>(ctx->table_info_node); |
586 | 101 | DORIS_CHECK(struct_node != nullptr); |
587 | | |
588 | 101 | std::vector<std::string> new_expand_col_names; |
589 | 103 | for (size_t i = 0; i < _expand_col_names.size(); ++i) { |
590 | 2 | const auto& old_name = _expand_col_names[i]; |
591 | 2 | int field_id = -1; |
592 | 2 | for (auto& [fid, name] : _id_to_block_column_name) { |
593 | 2 | if (name == old_name) { |
594 | 2 | field_id = fid; |
595 | 2 | break; |
596 | 2 | } |
597 | 2 | } |
598 | | |
599 | 2 | const orc::Type* file_column = nullptr; |
600 | 2 | if (!all_file_columns_have_field_ids && struct_node->get_children().contains(old_name) && |
601 | 2 | struct_node->children_column_exists(old_name)) { |
602 | | // Match the root ORC schema mapper's all-or-nothing BY_NAME decision. Accepting a |
603 | | // matching id in a partial-id file could bind this hidden key to an unrelated stale |
604 | | // column instead of the current name or historical alias selected by table_info_node. |
605 | 1 | const auto& mapped_name = struct_node->children_file_column_name(old_name); |
606 | 2 | for (uint64_t j = 0; j < orc_type_ptr->getSubtypeCount(); ++j) { |
607 | 2 | if (orc_type_ptr->getFieldName(j) == mapped_name) { |
608 | 1 | file_column = orc_type_ptr->getSubtype(j); |
609 | 1 | break; |
610 | 1 | } |
611 | 2 | } |
612 | 1 | DORIS_CHECK(file_column != nullptr); |
613 | 1 | } else if (all_file_columns_have_field_ids) { |
614 | 1 | auto id_it = field_id_to_file_column.find(field_id); |
615 | 1 | if (id_it != field_id_to_file_column.end()) { |
616 | 0 | file_column = id_it->second; |
617 | 0 | } |
618 | 1 | } |
619 | | |
620 | 2 | std::string file_col_name = old_name; |
621 | 2 | if (file_column != nullptr) { |
622 | 2 | for (uint64_t j = 0; j < orc_type_ptr->getSubtypeCount(); ++j) { |
623 | 2 | if (orc_type_ptr->getSubtype(j) == file_column) { |
624 | 1 | file_col_name = orc_type_ptr->getFieldName(j); |
625 | 1 | break; |
626 | 1 | } |
627 | 2 | } |
628 | 1 | } |
629 | 2 | std::string table_col_name = EQ_DELETE_PRE + file_col_name; |
630 | | |
631 | 2 | if (field_id >= 0) { |
632 | 2 | _id_to_block_column_name[field_id] = table_col_name; |
633 | 2 | } |
634 | 2 | if (i < _expand_columns.size()) { |
635 | 2 | _expand_columns[i].name = table_col_name; |
636 | 2 | } |
637 | 2 | if (file_column == nullptr) { |
638 | 1 | DORIS_CHECK(i < _expand_columns.size()); |
639 | 1 | RETURN_IF_ERROR(_register_missing_equality_delete_column(field_id, table_col_name, |
640 | 1 | _expand_columns[i].type)); |
641 | | // The old data file predates this equality key. Keep it in the expand block so the |
642 | | // synthesized-column hook can materialize its logical initial default before ORC's |
643 | | // block-size checks. Adding it to column_names/table_info_node would mark it as an |
644 | | // existing ORC child and make OrcReader read a column that is not present in the file. |
645 | 1 | new_expand_col_names.push_back(table_col_name); |
646 | 1 | continue; |
647 | 1 | } |
648 | 1 | new_expand_col_names.push_back(table_col_name); |
649 | | |
650 | | // Add column IDs |
651 | 1 | ctx->column_ids.insert(file_column->getColumnId()); |
652 | | |
653 | 1 | ctx->column_names.push_back(table_col_name); |
654 | 1 | ctx->table_info_node->add_children(table_col_name, file_col_name, |
655 | 1 | TableSchemaChangeHelper::ConstNode::get_instance()); |
656 | 1 | } |
657 | 101 | _expand_col_names = std::move(new_expand_col_names); |
658 | | |
659 | 101 | return Status::OK(); |
660 | 101 | } |
661 | | |
662 | | // ============================================================================ |
663 | | // IcebergOrcReader: _create_column_ids |
664 | | // ============================================================================ |
665 | | ColumnIdResult IcebergOrcReader::_create_column_ids( |
666 | | const orc::Type* orc_type, const TupleDescriptor* tuple_descriptor, |
667 | 105 | const std::shared_ptr<TableSchemaChangeHelper::Node>& table_info_node) { |
668 | 105 | std::unordered_map<int, const orc::Type*> iceberg_id_to_orc_type_map; |
669 | 410 | for (uint64_t i = 0; i < orc_type->getSubtypeCount(); ++i) { |
670 | 305 | const auto* orc_sub_type = orc_type->getSubtype(i); |
671 | 305 | if (!orc_sub_type) { |
672 | 0 | continue; |
673 | 0 | } |
674 | 305 | if (!orc_sub_type->hasAttributeKey(ICEBERG_ORC_ATTRIBUTE)) { |
675 | 2 | continue; |
676 | 2 | } |
677 | 303 | int iceberg_id = std::stoi(orc_sub_type->getAttributeValue(ICEBERG_ORC_ATTRIBUTE)); |
678 | 303 | iceberg_id_to_orc_type_map[iceberg_id] = orc_sub_type; |
679 | 303 | } |
680 | | |
681 | 105 | std::set<uint64_t> column_ids; |
682 | 105 | std::set<uint64_t> filter_column_ids; |
683 | | |
684 | 105 | auto process_access_paths = [](const orc::Type* orc_field, |
685 | 105 | const std::vector<TColumnAccessPath>& access_paths, |
686 | 105 | std::set<uint64_t>& out_ids) { |
687 | 14 | process_nested_access_paths( |
688 | 14 | orc_field, access_paths, out_ids, |
689 | 14 | [](const orc::Type* type) { return type->getColumnId(); }, |
690 | 14 | [](const orc::Type* type) { return type->getMaximumColumnId(); }, |
691 | 14 | IcebergOrcNestedColumnUtils::extract_nested_column_ids); |
692 | 14 | }; |
693 | | |
694 | 175 | for (const auto* slot : tuple_descriptor->slots()) { |
695 | 175 | const orc::Type* orc_field = nullptr; |
696 | 175 | if (table_info_node != nullptr) { |
697 | 164 | if (table_info_node->children_column_exists(slot->col_name())) { |
698 | | // Select the physical child resolved by the shared schema-mapping pass. Hidden |
699 | | // equality keys and projected columns must obey the same BY_NAME decision for |
700 | | // partial-id ORC files. |
701 | 144 | const auto& file_column_name = |
702 | 144 | table_info_node->children_file_column_name(slot->col_name()); |
703 | 343 | for (uint64_t i = 0; i < orc_type->getSubtypeCount(); ++i) { |
704 | 343 | if (orc_type->getFieldName(i) == file_column_name) { |
705 | 146 | orc_field = orc_type->getSubtype(i); |
706 | 146 | break; |
707 | 146 | } |
708 | 343 | } |
709 | 144 | DORIS_CHECK(orc_field != nullptr); |
710 | 144 | } |
711 | 164 | } else { |
712 | 11 | auto it = iceberg_id_to_orc_type_map.find(slot->col_unique_id()); |
713 | 13 | if (it != iceberg_id_to_orc_type_map.end()) { |
714 | 13 | orc_field = it->second; |
715 | 13 | } |
716 | 11 | } |
717 | 175 | if (orc_field == nullptr) { |
718 | 18 | continue; |
719 | 18 | } |
720 | | |
721 | 157 | if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY && |
722 | 157 | slot->col_type() != TYPE_MAP)) { |
723 | 147 | column_ids.insert(orc_field->getColumnId()); |
724 | 147 | if (slot->is_predicate()) { |
725 | 0 | filter_column_ids.insert(orc_field->getColumnId()); |
726 | 0 | } |
727 | 147 | continue; |
728 | 147 | } |
729 | | |
730 | 10 | const auto& all_access_paths = slot->all_access_paths(); |
731 | 10 | process_access_paths(orc_field, all_access_paths, column_ids); |
732 | | |
733 | 10 | const auto& predicate_access_paths = slot->predicate_access_paths(); |
734 | 10 | if (!predicate_access_paths.empty()) { |
735 | 6 | process_access_paths(orc_field, predicate_access_paths, filter_column_ids); |
736 | 6 | } |
737 | 10 | } |
738 | | |
739 | 105 | return {std::move(column_ids), std::move(filter_column_ids)}; |
740 | 105 | } |
741 | | |
742 | | // ============================================================================ |
743 | | // IcebergOrcReader: _read_position_delete_file |
744 | | // ============================================================================ |
745 | | Status IcebergOrcReader::_read_position_delete_file(const TFileRangeDesc* delete_range, |
746 | 0 | DeleteFile* position_delete) { |
747 | 0 | OrcReader orc_delete_reader(get_profile(), get_state(), get_scan_params(), *delete_range, |
748 | 0 | READ_DELETE_FILE_BATCH_SIZE, get_state()->timezone(), get_io_ctx(), |
749 | 0 | _meta_cache); |
750 | 0 | OrcInitContext delete_ctx; |
751 | 0 | delete_ctx.column_names = delete_file_col_names; |
752 | 0 | delete_ctx.col_name_to_block_idx = |
753 | 0 | const_cast<std::unordered_map<std::string, uint32_t>*>(&DELETE_COL_NAME_TO_BLOCK_IDX); |
754 | 0 | RETURN_IF_ERROR(orc_delete_reader.init_reader(&delete_ctx)); |
755 | | |
756 | 0 | bool eof = false; |
757 | 0 | DataTypePtr data_type_file_path {new DataTypeString}; |
758 | 0 | DataTypePtr data_type_pos {new DataTypeInt64}; |
759 | 0 | while (!eof) { |
760 | 0 | Block block = {{data_type_file_path, ICEBERG_FILE_PATH}, {data_type_pos, ICEBERG_ROW_POS}}; |
761 | |
|
762 | 0 | size_t read_rows = 0; |
763 | 0 | RETURN_IF_ERROR(orc_delete_reader.get_next_block(&block, &read_rows, &eof)); |
764 | | |
765 | 0 | RETURN_IF_ERROR(_gen_position_delete_file_range(block, position_delete, read_rows, false)); |
766 | 0 | } |
767 | 0 | return Status::OK(); |
768 | 0 | } |
769 | | |
770 | | } // namespace doris |