Coverage Report

Created: 2026-05-17 16:59

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