Coverage Report

Created: 2026-06-18 08:17

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
18.0k
Status IcebergParquetReader::on_before_init_reader(ReaderInitContext* ctx) {
131
18.0k
    _column_descs = ctx->column_descs;
132
18.0k
    _fill_col_name_to_block_idx = ctx->col_name_to_block_idx;
133
18.0k
    _file_format = Fileformat::PARQUET;
134
135
    // Get file metadata schema first (available because _open_file() already ran)
136
18.0k
    const FieldDescriptor* field_desc = nullptr;
137
18.0k
    RETURN_IF_ERROR(this->get_file_metadata_schema(&field_desc));
138
18.0k
    DCHECK(field_desc != nullptr);
139
140
    // Build table_info_node by field_id or name matching.
141
    // This must happen BEFORE column classification so we can use children_column_exists
142
    // to check if a column exists in the file (by field ID, not name).
143
18.0k
    if (!get_scan_params().__isset.history_schema_info ||
144
18.0k
        get_scan_params().history_schema_info.empty()) [[unlikely]] {
145
1
        RETURN_IF_ERROR(BuildTableInfoUtil::by_parquet_name(ctx->tuple_descriptor, *field_desc,
146
1
                                                            ctx->table_info_node));
147
18.0k
    } else {
148
18.0k
        RETURN_IF_ERROR(BuildTableInfoUtil::by_parquet_field_id_with_name_mapping(
149
18.0k
                get_scan_params().history_schema_info.front().root_field, *field_desc,
150
18.0k
                ctx->table_info_node));
151
18.0k
    }
152
153
18.0k
    std::unordered_set<std::string> partition_col_names;
154
18.0k
    if (ctx->range->__isset.columns_from_path_keys) {
155
5.37k
        partition_col_names.insert(ctx->range->columns_from_path_keys.begin(),
156
5.37k
                                   ctx->range->columns_from_path_keys.end());
157
5.37k
    }
158
159
    // Single pass: classify columns, detect $row_id, handle partition fallback.
160
18.0k
    bool has_partition_from_path = false;
161
54.5k
    for (const auto& desc : *ctx->column_descs) {
162
54.5k
        if (desc.category == ColumnCategory::SYNTHESIZED) {
163
340
            if (desc.name == BeConsts::ICEBERG_ROWID_COL) {
164
94
                this->register_synthesized_column_handler(
165
94
                        BeConsts::ICEBERG_ROWID_COL, [this](Block* block, size_t rows) -> Status {
166
94
                            return _fill_iceberg_row_id(block, rows);
167
94
                        });
168
94
                continue;
169
246
            } else if (desc.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) {
170
246
                auto topn_row_id_column_iter = _create_topn_row_id_column_iterator();
171
246
                this->register_synthesized_column_handler(
172
246
                        desc.name,
173
246
                        [iter = std::move(topn_row_id_column_iter), this, &desc](
174
280
                                Block* block, size_t rows) -> Status {
175
280
                            return fill_topn_row_id(iter, desc.name, block, rows);
176
280
                        });
177
246
                continue;
178
246
            }
179
54.2k
        } else if (desc.category == ColumnCategory::PARTITION_KEY) {
180
3.16k
            bool has_partition_value = partition_col_names.contains(desc.name);
181
3.16k
            bool exists_in_file = ctx->table_info_node->children_column_exists(desc.name);
182
3.16k
            if (!has_partition_value || exists_in_file) {
183
                // Keep PARTITION_KEY category stable for scan planning, but still read
184
                // from file when the column exists there.
185
3.16k
                ctx->column_names.push_back(desc.name);
186
3.16k
                continue;
187
3.16k
            }
188
18.4E
            has_partition_from_path = true;
189
18.4E
        } else if (desc.category == ColumnCategory::REGULAR) {
190
50.6k
            ctx->column_names.push_back(desc.name);
191
50.6k
        } else if (desc.category == ColumnCategory::GENERATED) {
192
446
            _init_row_lineage_columns();
193
446
            if (desc.name == ROW_LINEAGE_ROW_ID) {
194
250
                ctx->column_names.push_back(desc.name);
195
250
                this->register_generated_column_handler(
196
252
                        ROW_LINEAGE_ROW_ID, [this](Block* block, size_t rows) -> Status {
197
252
                            return _fill_row_lineage_row_id(block, rows);
198
252
                        });
199
250
                continue;
200
250
            } else if (desc.name == ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER) {
201
194
                ctx->column_names.push_back(desc.name);
202
194
                this->register_generated_column_handler(
203
194
                        ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER,
204
194
                        [this](Block* block, size_t rows) -> Status {
205
194
                            return _fill_row_lineage_last_updated_sequence_number(block, rows);
206
194
                        });
207
194
                continue;
208
194
            }
209
446
        }
210
54.5k
    }
211
212
    // Set up partition value extraction if any partition columns need filling from path
213
18.0k
    if (has_partition_from_path) {
214
0
        RETURN_IF_ERROR(_extract_partition_values(*ctx->range, ctx->tuple_descriptor,
215
0
                                                  _fill_partition_values,
216
0
                                                  &_fill_partition_value_is_null));
217
0
    }
218
219
18.0k
    _all_required_col_names = ctx->column_names;
220
221
    // Create column IDs from field descriptor
222
18.0k
    auto column_id_result = _create_column_ids(field_desc, ctx->tuple_descriptor);
223
18.0k
    ctx->column_ids = std::move(column_id_result.column_ids);
224
18.0k
    ctx->filter_column_ids = std::move(column_id_result.filter_column_ids);
225
226
    // Build field_id -> block_column_name mapping for equality delete filtering.
227
    // This was previously done in init_reader() column matching (pre-CRTP refactoring).
228
54.5k
    for (const auto* slot : ctx->tuple_descriptor->slots()) {
229
54.5k
        _id_to_block_column_name.emplace(slot->col_unique_id(), slot->col_name());
230
54.5k
    }
231
232
    // Process delete files (must happen before _do_init_reader so expand col IDs are included)
233
18.0k
    RETURN_IF_ERROR(_init_row_filters());
234
235
    // Add expand column IDs for equality delete and remap expand column names
236
    // to match master's behavior:
237
    // - Use field_id to find the actual file column name in Parquet schema
238
    // - Prefix with __equality_delete_column__ to avoid name conflicts
239
    // - Correctly map table_col_name → file_col_name in table_info_node
240
18.0k
    const static std::string EQ_DELETE_PRE = "__equality_delete_column__";
241
18.0k
    std::unordered_map<int, std::string> field_id_to_file_col_name;
242
141k
    for (int i = 0; i < field_desc->size(); ++i) {
243
123k
        auto field_schema = field_desc->get_column(i);
244
123k
        if (field_schema) {
245
123k
            field_id_to_file_col_name[field_schema->field_id] = field_schema->name;
246
123k
        }
247
123k
    }
248
249
    // Rebuild _expand_col_names with proper file-column-based names
250
18.0k
    std::vector<std::string> new_expand_col_names;
251
18.3k
    for (size_t i = 0; i < _expand_col_names.size(); ++i) {
252
318
        const auto& old_name = _expand_col_names[i];
253
        // Find the field_id for this expand column
254
318
        int field_id = -1;
255
386
        for (auto& [fid, name] : _id_to_block_column_name) {
256
386
            if (name == old_name) {
257
318
                field_id = fid;
258
318
                break;
259
318
            }
260
386
        }
261
262
318
        std::string file_col_name = old_name;
263
318
        auto it = field_id_to_file_col_name.find(field_id);
264
318
        if (it != field_id_to_file_col_name.end()) {
265
318
            file_col_name = it->second;
266
318
        }
267
268
318
        std::string table_col_name = EQ_DELETE_PRE + file_col_name;
269
270
        // Update _id_to_block_column_name
271
318
        if (field_id >= 0) {
272
318
            _id_to_block_column_name[field_id] = table_col_name;
273
318
        }
274
275
        // Update _expand_columns name
276
318
        if (i < _expand_columns.size()) {
277
318
            _expand_columns[i].name = table_col_name;
278
318
        }
279
280
318
        new_expand_col_names.push_back(table_col_name);
281
282
        // Add column IDs
283
318
        if (it != field_id_to_file_col_name.end()) {
284
584
            for (int j = 0; j < field_desc->size(); ++j) {
285
584
                auto field_schema = field_desc->get_column(j);
286
584
                if (field_schema && field_schema->field_id == field_id) {
287
318
                    ctx->column_ids.insert(field_schema->get_column_id());
288
318
                    break;
289
318
                }
290
584
            }
291
318
        }
292
293
        // Register in table_info_node: table_col_name → file_col_name
294
318
        ctx->column_names.push_back(table_col_name);
295
318
        ctx->table_info_node->add_children(table_col_name, file_col_name,
296
318
                                           TableSchemaChangeHelper::ConstNode::get_instance());
297
318
    }
298
18.0k
    _expand_col_names = std::move(new_expand_col_names);
299
300
    // Enable group filtering for Iceberg
301
18.0k
    _filter_groups = true;
302
303
18.0k
    return Status::OK();
304
18.0k
}
305
306
// ============================================================================
307
// IcebergParquetReader: _create_column_ids
308
// ============================================================================
309
ColumnIdResult IcebergParquetReader::_create_column_ids(const FieldDescriptor* field_desc,
310
18.0k
                                                        const TupleDescriptor* tuple_descriptor) {
311
18.0k
    auto* mutable_field_desc = const_cast<FieldDescriptor*>(field_desc);
312
18.0k
    mutable_field_desc->assign_ids();
313
314
18.0k
    std::unordered_map<int, const FieldSchema*> iceberg_id_to_field_schema_map;
315
141k
    for (int i = 0; i < field_desc->size(); ++i) {
316
123k
        auto field_schema = field_desc->get_column(i);
317
123k
        if (!field_schema) continue;
318
123k
        int iceberg_id = field_schema->field_id;
319
123k
        iceberg_id_to_field_schema_map[iceberg_id] = field_schema;
320
123k
    }
321
322
18.0k
    std::set<uint64_t> column_ids;
323
18.0k
    std::set<uint64_t> filter_column_ids;
324
325
18.0k
    auto process_access_paths = [](const FieldSchema* parquet_field,
326
18.0k
                                   const std::vector<TColumnAccessPath>& access_paths,
327
18.0k
                                   std::set<uint64_t>& out_ids) {
328
14.9k
        process_nested_access_paths(
329
14.9k
                parquet_field, access_paths, out_ids,
330
14.9k
                [](const FieldSchema* field) { return field->get_column_id(); },
331
14.9k
                [](const FieldSchema* field) { return field->get_max_column_id(); },
332
14.9k
                IcebergParquetNestedColumnUtils::extract_nested_column_ids);
333
14.9k
    };
334
335
54.5k
    for (const auto* slot : tuple_descriptor->slots()) {
336
54.5k
        auto it = iceberg_id_to_field_schema_map.find(slot->col_unique_id());
337
54.5k
        if (it == iceberg_id_to_field_schema_map.end()) {
338
3.54k
            continue;
339
3.54k
        }
340
51.0k
        auto field_schema = it->second;
341
342
51.0k
        if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY &&
343
51.0k
             slot->col_type() != TYPE_MAP)) {
344
36.7k
            column_ids.insert(field_schema->column_id);
345
36.7k
            if (slot->is_predicate()) {
346
5.78k
                filter_column_ids.insert(field_schema->column_id);
347
5.78k
            }
348
36.7k
            continue;
349
36.7k
        }
350
351
14.2k
        const auto& all_access_paths = slot->all_access_paths();
352
14.2k
        process_access_paths(field_schema, all_access_paths, column_ids);
353
354
14.2k
        const auto& predicate_access_paths = slot->predicate_access_paths();
355
14.2k
        if (!predicate_access_paths.empty()) {
356
762
            process_access_paths(field_schema, predicate_access_paths, filter_column_ids);
357
762
        }
358
14.2k
    }
359
18.0k
    return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids));
360
18.0k
}
361
362
// ============================================================================
363
// IcebergParquetReader: _read_position_delete_file
364
// ============================================================================
365
Status IcebergParquetReader::_read_position_delete_file(const TFileRangeDesc* delete_range,
366
790
                                                        DeleteFile* position_delete) {
367
790
    ParquetReader parquet_delete_reader(get_profile(), get_scan_params(), *delete_range,
368
790
                                        READ_DELETE_FILE_BATCH_SIZE, &get_state()->timezone_obj(),
369
790
                                        get_io_ctx(), get_state(), _meta_cache);
370
    // The delete file range has size=-1 (read whole file). We must disable
371
    // row group filtering before init; otherwise _do_init_reader returns EndOfFile
372
    // when _filter_groups && _range_size < 0.
373
790
    ParquetInitContext delete_ctx;
374
790
    delete_ctx.filter_groups = false;
375
790
    delete_ctx.column_names = delete_file_col_names;
376
790
    delete_ctx.col_name_to_block_idx =
377
790
            const_cast<std::unordered_map<std::string, uint32_t>*>(&DELETE_COL_NAME_TO_BLOCK_IDX);
378
790
    RETURN_IF_ERROR(parquet_delete_reader.init_reader(&delete_ctx));
379
380
790
    const tparquet::FileMetaData* meta_data = parquet_delete_reader.get_meta_data();
381
790
    bool dictionary_coded = true;
382
790
    for (const auto& row_group : meta_data->row_groups) {
383
790
        const auto& column_chunk = row_group.columns[ICEBERG_FILE_PATH_INDEX];
384
790
        if (!(column_chunk.__isset.meta_data && has_dict_page(column_chunk.meta_data))) {
385
370
            dictionary_coded = false;
386
370
            break;
387
370
        }
388
790
    }
389
790
    DataTypePtr data_type_file_path {new DataTypeString};
390
790
    DataTypePtr data_type_pos {new DataTypeInt64};
391
790
    bool eof = false;
392
1.58k
    while (!eof) {
393
790
        Block block = {dictionary_coded
394
790
                               ? ColumnWithTypeAndName {ColumnDictI32::create(),
395
420
                                                        data_type_file_path, ICEBERG_FILE_PATH}
396
790
                               : ColumnWithTypeAndName {data_type_file_path, ICEBERG_FILE_PATH},
397
398
790
                       {data_type_pos, ICEBERG_ROW_POS}};
399
790
        size_t read_rows = 0;
400
790
        RETURN_IF_ERROR(parquet_delete_reader.get_next_block(&block, &read_rows, &eof));
401
402
790
        if (read_rows <= 0) {
403
0
            break;
404
0
        }
405
790
        _gen_position_delete_file_range(block, position_delete, read_rows, dictionary_coded);
406
790
    }
407
790
    return Status::OK();
408
790
};
409
410
// ============================================================================
411
// IcebergOrcReader: on_before_init_reader (ORC-specific schema matching)
412
// ============================================================================
413
6.55k
Status IcebergOrcReader::on_before_init_reader(ReaderInitContext* ctx) {
414
6.55k
    _column_descs = ctx->column_descs;
415
6.55k
    _fill_col_name_to_block_idx = ctx->col_name_to_block_idx;
416
6.55k
    _file_format = Fileformat::ORC;
417
418
    // Get ORC file type first (available because _create_file_reader() already ran)
419
6.55k
    const orc::Type* orc_type_ptr = nullptr;
420
6.55k
    RETURN_IF_ERROR(this->get_file_type(&orc_type_ptr));
421
422
    // Build table_info_node by field_id or name matching.
423
    // This must happen BEFORE column classification so we can use children_column_exists
424
    // to check if a column exists in the file (by field ID, not name).
425
6.55k
    if (!get_scan_params().__isset.history_schema_info ||
426
6.55k
        get_scan_params().history_schema_info.empty()) [[unlikely]] {
427
1
        RETURN_IF_ERROR(BuildTableInfoUtil::by_orc_name(ctx->tuple_descriptor, orc_type_ptr,
428
1
                                                        ctx->table_info_node));
429
6.55k
    } else {
430
6.55k
        RETURN_IF_ERROR(BuildTableInfoUtil::by_orc_field_id_with_name_mapping(
431
6.55k
                get_scan_params().history_schema_info.front().root_field, orc_type_ptr,
432
6.55k
                ICEBERG_ORC_ATTRIBUTE, ctx->table_info_node));
433
6.55k
    }
434
435
6.55k
    std::unordered_set<std::string> partition_col_names;
436
6.55k
    if (ctx->range->__isset.columns_from_path_keys) {
437
706
        partition_col_names.insert(ctx->range->columns_from_path_keys.begin(),
438
706
                                   ctx->range->columns_from_path_keys.end());
439
706
    }
440
441
    // Single pass: classify columns, detect $row_id, handle partition fallback.
442
6.55k
    bool has_partition_from_path = false;
443
32.0k
    for (const auto& desc : *ctx->column_descs) {
444
32.0k
        if (desc.category == ColumnCategory::SYNTHESIZED) {
445
326
            if (desc.name == BeConsts::ICEBERG_ROWID_COL) {
446
94
                this->register_synthesized_column_handler(
447
94
                        BeConsts::ICEBERG_ROWID_COL, [this](Block* block, size_t rows) -> Status {
448
90
                            return _fill_iceberg_row_id(block, rows);
449
90
                        });
450
94
                continue;
451
232
            } else if (desc.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) {
452
232
                auto topn_row_id_column_iter = _create_topn_row_id_column_iterator();
453
232
                this->register_synthesized_column_handler(
454
232
                        desc.name,
455
232
                        [iter = std::move(topn_row_id_column_iter), this, &desc](
456
232
                                Block* block, size_t rows) -> Status {
457
228
                            return fill_topn_row_id(iter, desc.name, block, rows);
458
228
                        });
459
232
                continue;
460
232
            }
461
31.7k
        } else if (desc.category == ColumnCategory::PARTITION_KEY) {
462
896
            bool has_partition_value = partition_col_names.contains(desc.name);
463
896
            bool exists_in_file = ctx->table_info_node->children_column_exists(desc.name);
464
896
            if (!has_partition_value || exists_in_file) {
465
896
                ctx->column_names.push_back(desc.name);
466
896
                continue;
467
896
            }
468
0
            has_partition_from_path = true;
469
30.8k
        } else if (desc.category == ColumnCategory::REGULAR) {
470
30.4k
            ctx->column_names.push_back(desc.name);
471
30.4k
        } else if (desc.category == ColumnCategory::GENERATED) {
472
416
            _init_row_lineage_columns();
473
416
            if (desc.name == ROW_LINEAGE_ROW_ID) {
474
222
                ctx->column_names.push_back(desc.name);
475
222
                this->register_generated_column_handler(
476
222
                        ROW_LINEAGE_ROW_ID, [this](Block* block, size_t rows) -> Status {
477
222
                            return _fill_row_lineage_row_id(block, rows);
478
222
                        });
479
222
                continue;
480
222
            } else if (desc.name == ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER) {
481
194
                ctx->column_names.push_back(desc.name);
482
194
                this->register_generated_column_handler(
483
194
                        ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER,
484
194
                        [this](Block* block, size_t rows) -> Status {
485
194
                            return _fill_row_lineage_last_updated_sequence_number(block, rows);
486
194
                        });
487
194
                continue;
488
194
            }
489
416
        }
490
32.0k
    }
491
492
6.55k
    if (has_partition_from_path) {
493
0
        RETURN_IF_ERROR(_extract_partition_values(*ctx->range, ctx->tuple_descriptor,
494
0
                                                  _fill_partition_values,
495
0
                                                  &_fill_partition_value_is_null));
496
0
    }
497
498
6.55k
    _all_required_col_names = ctx->column_names;
499
500
    // Create column IDs from ORC type
501
6.55k
    auto column_id_result = _create_column_ids(orc_type_ptr, ctx->tuple_descriptor);
502
6.55k
    ctx->column_ids = std::move(column_id_result.column_ids);
503
6.55k
    ctx->filter_column_ids = std::move(column_id_result.filter_column_ids);
504
505
    // Build field_id -> block_column_name mapping for equality delete filtering.
506
32.0k
    for (const auto* slot : ctx->tuple_descriptor->slots()) {
507
32.0k
        _id_to_block_column_name.emplace(slot->col_unique_id(), slot->col_name());
508
32.0k
    }
509
510
    // Process delete files (must happen before _do_init_reader so expand col IDs are included)
511
6.55k
    RETURN_IF_ERROR(_init_row_filters());
512
513
    // Add expand column IDs for equality delete and remap expand column names
514
    // (matching master's behavior with __equality_delete_column__ prefix)
515
6.55k
    const static std::string EQ_DELETE_PRE = "__equality_delete_column__";
516
6.55k
    std::unordered_map<int, std::string> field_id_to_file_col_name;
517
51.1k
    for (uint64_t i = 0; i < orc_type_ptr->getSubtypeCount(); ++i) {
518
44.5k
        std::string col_name = orc_type_ptr->getFieldName(i);
519
44.5k
        const orc::Type* sub_type = orc_type_ptr->getSubtype(i);
520
44.5k
        if (sub_type->hasAttributeKey(ICEBERG_ORC_ATTRIBUTE)) {
521
44.5k
            int fid = std::stoi(sub_type->getAttributeValue(ICEBERG_ORC_ATTRIBUTE));
522
44.5k
            field_id_to_file_col_name[fid] = col_name;
523
44.5k
        }
524
44.5k
    }
525
526
6.55k
    std::vector<std::string> new_expand_col_names;
527
6.87k
    for (size_t i = 0; i < _expand_col_names.size(); ++i) {
528
318
        const auto& old_name = _expand_col_names[i];
529
318
        int field_id = -1;
530
386
        for (auto& [fid, name] : _id_to_block_column_name) {
531
386
            if (name == old_name) {
532
318
                field_id = fid;
533
318
                break;
534
318
            }
535
386
        }
536
537
318
        std::string file_col_name = old_name;
538
318
        auto it = field_id_to_file_col_name.find(field_id);
539
318
        if (it != field_id_to_file_col_name.end()) {
540
318
            file_col_name = it->second;
541
318
        }
542
543
318
        std::string table_col_name = EQ_DELETE_PRE + file_col_name;
544
545
318
        if (field_id >= 0) {
546
318
            _id_to_block_column_name[field_id] = table_col_name;
547
318
        }
548
318
        if (i < _expand_columns.size()) {
549
318
            _expand_columns[i].name = table_col_name;
550
318
        }
551
318
        new_expand_col_names.push_back(table_col_name);
552
553
        // Add column IDs
554
318
        if (it != field_id_to_file_col_name.end()) {
555
584
            for (uint64_t j = 0; j < orc_type_ptr->getSubtypeCount(); ++j) {
556
584
                const orc::Type* sub_type = orc_type_ptr->getSubtype(j);
557
584
                if (orc_type_ptr->getFieldName(j) == file_col_name) {
558
318
                    ctx->column_ids.insert(sub_type->getColumnId());
559
318
                    break;
560
318
                }
561
584
            }
562
318
        }
563
564
318
        ctx->column_names.push_back(table_col_name);
565
318
        ctx->table_info_node->add_children(table_col_name, file_col_name,
566
318
                                           TableSchemaChangeHelper::ConstNode::get_instance());
567
318
    }
568
6.55k
    _expand_col_names = std::move(new_expand_col_names);
569
570
6.55k
    return Status::OK();
571
6.55k
}
572
573
// ============================================================================
574
// IcebergOrcReader: _create_column_ids
575
// ============================================================================
576
ColumnIdResult IcebergOrcReader::_create_column_ids(const orc::Type* orc_type,
577
6.55k
                                                    const TupleDescriptor* tuple_descriptor) {
578
6.55k
    std::unordered_map<int, const orc::Type*> iceberg_id_to_orc_type_map;
579
51.1k
    for (uint64_t i = 0; i < orc_type->getSubtypeCount(); ++i) {
580
44.6k
        auto orc_sub_type = orc_type->getSubtype(i);
581
44.6k
        if (!orc_sub_type) continue;
582
44.6k
        if (!orc_sub_type->hasAttributeKey(ICEBERG_ORC_ATTRIBUTE)) {
583
0
            continue;
584
0
        }
585
44.6k
        int iceberg_id = std::stoi(orc_sub_type->getAttributeValue(ICEBERG_ORC_ATTRIBUTE));
586
44.6k
        iceberg_id_to_orc_type_map[iceberg_id] = orc_sub_type;
587
44.6k
    }
588
589
6.55k
    std::set<uint64_t> column_ids;
590
6.55k
    std::set<uint64_t> filter_column_ids;
591
592
6.55k
    auto process_access_paths = [](const orc::Type* orc_field,
593
6.55k
                                   const std::vector<TColumnAccessPath>& access_paths,
594
13.9k
                                   std::set<uint64_t>& out_ids) {
595
13.9k
        process_nested_access_paths(
596
13.9k
                orc_field, access_paths, out_ids,
597
14.0k
                [](const orc::Type* type) { return type->getColumnId(); },
598
13.9k
                [](const orc::Type* type) { return type->getMaximumColumnId(); },
599
13.9k
                IcebergOrcNestedColumnUtils::extract_nested_column_ids);
600
13.9k
    };
601
602
32.1k
    for (const auto* slot : tuple_descriptor->slots()) {
603
32.1k
        auto it = iceberg_id_to_orc_type_map.find(slot->col_unique_id());
604
32.1k
        if (it == iceberg_id_to_orc_type_map.end()) {
605
2.53k
            continue;
606
2.53k
        }
607
29.5k
        const orc::Type* orc_field = it->second;
608
609
29.5k
        if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY &&
610
29.5k
             slot->col_type() != TYPE_MAP)) {
611
16.2k
            column_ids.insert(orc_field->getColumnId());
612
16.2k
            if (slot->is_predicate()) {
613
2.30k
                filter_column_ids.insert(orc_field->getColumnId());
614
2.30k
            }
615
16.2k
            continue;
616
16.2k
        }
617
618
13.3k
        const auto& all_access_paths = slot->all_access_paths();
619
13.3k
        process_access_paths(orc_field, all_access_paths, column_ids);
620
621
13.3k
        const auto& predicate_access_paths = slot->predicate_access_paths();
622
13.3k
        if (!predicate_access_paths.empty()) {
623
730
            process_access_paths(orc_field, predicate_access_paths, filter_column_ids);
624
730
        }
625
13.3k
    }
626
627
6.55k
    return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids));
628
6.55k
}
629
630
// ============================================================================
631
// IcebergOrcReader: _read_position_delete_file
632
// ============================================================================
633
Status IcebergOrcReader::_read_position_delete_file(const TFileRangeDesc* delete_range,
634
968
                                                    DeleteFile* position_delete) {
635
968
    OrcReader orc_delete_reader(get_profile(), get_state(), get_scan_params(), *delete_range,
636
968
                                READ_DELETE_FILE_BATCH_SIZE, get_state()->timezone(), get_io_ctx(),
637
968
                                _meta_cache);
638
968
    OrcInitContext delete_ctx;
639
968
    delete_ctx.column_names = delete_file_col_names;
640
968
    delete_ctx.col_name_to_block_idx =
641
968
            const_cast<std::unordered_map<std::string, uint32_t>*>(&DELETE_COL_NAME_TO_BLOCK_IDX);
642
968
    RETURN_IF_ERROR(orc_delete_reader.init_reader(&delete_ctx));
643
644
968
    bool eof = false;
645
968
    DataTypePtr data_type_file_path {new DataTypeString};
646
968
    DataTypePtr data_type_pos {new DataTypeInt64};
647
2.90k
    while (!eof) {
648
1.93k
        Block block = {{data_type_file_path, ICEBERG_FILE_PATH}, {data_type_pos, ICEBERG_ROW_POS}};
649
650
1.93k
        size_t read_rows = 0;
651
1.93k
        RETURN_IF_ERROR(orc_delete_reader.get_next_block(&block, &read_rows, &eof));
652
653
1.93k
        _gen_position_delete_file_range(block, position_delete, read_rows, false);
654
1.93k
    }
655
968
    return Status::OK();
656
968
}
657
658
} // namespace doris