Coverage Report

Created: 2026-04-23 15:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format/table/hive_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/hive_reader.h"
19
20
#include <vector>
21
22
#include "common/status.h"
23
#include "format/table/hive/hive_orc_nested_column_utils.h"
24
#include "format/table/hive/hive_parquet_nested_column_utils.h"
25
#include "format/table/nested_column_access_helper.h"
26
#include "runtime/runtime_state.h"
27
28
namespace doris {
29
30
11
Status HiveOrcReader::on_before_init_reader(ReaderInitContext* ctx) {
31
11
    _column_descs = ctx->column_descs;
32
11
    _fill_col_name_to_block_idx = ctx->col_name_to_block_idx;
33
11
    RETURN_IF_ERROR(
34
11
            _extract_partition_values(*ctx->range, ctx->tuple_descriptor, _fill_partition_values));
35
99
    for (auto& desc : *ctx->column_descs) {
36
99
        if (desc.category == ColumnCategory::REGULAR ||
37
99
            desc.category == ColumnCategory::GENERATED) {
38
99
            ctx->column_names.push_back(desc.name);
39
99
        } else if (desc.category == ColumnCategory::SYNTHESIZED &&
40
0
                   desc.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) {
41
0
            auto topn_row_id_column_iter = _create_topn_row_id_column_iterator();
42
0
            this->register_synthesized_column_handler(
43
0
                    desc.name,
44
0
                    [iter = std::move(topn_row_id_column_iter), this, &desc](
45
0
                            Block* block, size_t rows) -> Status {
46
0
                        return fill_topn_row_id(iter, desc.name, block, rows);
47
0
                    });
48
0
            continue;
49
0
        }
50
99
    }
51
52
    // Get file type (available because _create_file_reader() runs before this hook)
53
11
    const orc::Type* orc_type_ptr = nullptr;
54
11
    RETURN_IF_ERROR(get_file_type(&orc_type_ptr));
55
11
    bool is_hive_col_name = OrcReader::is_hive1_col_name(orc_type_ptr);
56
57
    // Build table_info_node based on config
58
11
    if (get_state()->query_options().hive_orc_use_column_names && !is_hive_col_name) {
59
11
        RETURN_IF_ERROR(BuildTableInfoUtil::by_orc_name(ctx->tuple_descriptor, orc_type_ptr,
60
11
                                                        ctx->table_info_node, _is_file_slot));
61
11
    } else {
62
0
        ctx->table_info_node = std::make_shared<StructNode>();
63
0
        std::map<std::string, const SlotDescriptor*> slot_map;
64
0
        for (const auto& slot : ctx->tuple_descriptor->slots()) {
65
0
            slot_map.emplace(slot->col_name_lower_case(), slot);
66
0
        }
67
68
0
        for (size_t idx = 0; idx < get_scan_params().column_idxs.size(); idx++) {
69
0
            auto table_column_name = ctx->column_names[idx];
70
0
            auto file_index = get_scan_params().column_idxs[idx];
71
72
0
            if (file_index >= orc_type_ptr->getSubtypeCount()) {
73
0
                ctx->table_info_node->add_not_exist_children(table_column_name);
74
0
            } else {
75
0
                auto field_node = std::make_shared<Node>();
76
0
                RETURN_IF_ERROR(BuildTableInfoUtil::by_orc_name(
77
0
                        slot_map[table_column_name]->type(), orc_type_ptr->getSubtype(file_index),
78
0
                        field_node));
79
0
                ctx->table_info_node->add_children(
80
0
                        table_column_name, orc_type_ptr->getFieldName(file_index), field_node);
81
0
            }
82
0
            slot_map.erase(table_column_name);
83
0
        }
84
0
        for (const auto& [partition_col_name, _] : slot_map) {
85
0
            ctx->table_info_node->add_not_exist_children(partition_col_name);
86
0
        }
87
0
    }
88
89
    // Compute column_ids
90
11
    auto column_id_result = ColumnIdResult();
91
11
    if (get_state()->query_options().hive_orc_use_column_names && !is_hive_col_name) {
92
11
        column_id_result = _create_column_ids(orc_type_ptr, ctx->tuple_descriptor);
93
11
    } else {
94
0
        column_id_result =
95
0
                _create_column_ids_by_top_level_col_index(orc_type_ptr, ctx->tuple_descriptor);
96
0
    }
97
11
    ctx->column_ids = std::move(column_id_result.column_ids);
98
11
    ctx->filter_column_ids = std::move(column_id_result.filter_column_ids);
99
100
    // _is_acid is false by default, no need to set explicitly
101
11
    return Status::OK();
102
11
}
103
104
ColumnIdResult HiveOrcReader::_create_column_ids(const orc::Type* orc_type,
105
17
                                                 const TupleDescriptor* tuple_descriptor) {
106
    // map top-level table column name (lower-cased) -> orc::Type*
107
17
    std::unordered_map<std::string, const orc::Type*> table_col_name_to_orc_type_map;
108
164
    for (uint64_t i = 0; i < orc_type->getSubtypeCount(); ++i) {
109
147
        auto orc_sub_type = orc_type->getSubtype(i);
110
147
        if (!orc_sub_type) continue;
111
112
147
        std::string table_col_name = to_lower(orc_type->getFieldName(i));
113
147
        table_col_name_to_orc_type_map[table_col_name] = orc_sub_type;
114
147
    }
115
116
17
    std::set<uint64_t> column_ids;
117
17
    std::set<uint64_t> filter_column_ids;
118
119
    // helper to process access paths for a given top-level orc field
120
17
    auto process_access_paths = [](const orc::Type* orc_field,
121
17
                                   const std::vector<TColumnAccessPath>& access_paths,
122
17
                                   std::set<uint64_t>& out_ids) {
123
13
        process_nested_access_paths(
124
13
                orc_field, access_paths, out_ids,
125
13
                [](const orc::Type* type) { return type->getColumnId(); },
126
13
                [](const orc::Type* type) { return type->getMaximumColumnId(); },
127
13
                HiveOrcNestedColumnUtils::extract_nested_column_ids);
128
13
    };
129
130
112
    for (const auto* slot : tuple_descriptor->slots()) {
131
112
        auto it = table_col_name_to_orc_type_map.find(slot->col_name_lower_case());
132
112
        if (it == table_col_name_to_orc_type_map.end()) {
133
            // Column not found in file
134
0
            continue;
135
0
        }
136
112
        const orc::Type* orc_field = it->second;
137
138
        // primitive (non-nested) types
139
112
        if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY &&
140
112
             slot->col_type() != TYPE_MAP)) {
141
105
            column_ids.insert(orc_field->getColumnId());
142
105
            if (slot->is_predicate()) {
143
0
                filter_column_ids.insert(orc_field->getColumnId());
144
0
            }
145
105
            continue;
146
105
        }
147
148
        // complex types
149
7
        const auto& all_access_paths = slot->all_access_paths();
150
7
        process_access_paths(orc_field, all_access_paths, column_ids);
151
152
7
        const auto& predicate_access_paths = slot->predicate_access_paths();
153
7
        if (!predicate_access_paths.empty()) {
154
6
            process_access_paths(orc_field, predicate_access_paths, filter_column_ids);
155
6
        }
156
7
    }
157
158
17
    return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids));
159
17
}
160
161
ColumnIdResult HiveOrcReader::_create_column_ids_by_top_level_col_index(
162
6
        const orc::Type* orc_type, const TupleDescriptor* tuple_descriptor) {
163
    // map top-level table column position -> orc::Type*
164
6
    std::unordered_map<uint64_t, const orc::Type*> table_col_pos_to_orc_type_map;
165
54
    for (uint64_t i = 0; i < orc_type->getSubtypeCount(); ++i) {
166
48
        auto orc_sub_type = orc_type->getSubtype(i);
167
48
        if (!orc_sub_type) continue;
168
169
48
        table_col_pos_to_orc_type_map[i] = orc_sub_type;
170
48
    }
171
172
6
    std::set<uint64_t> column_ids;
173
6
    std::set<uint64_t> filter_column_ids;
174
175
    // helper to process access paths for a given top-level orc field
176
6
    auto process_access_paths = [](const orc::Type* orc_field,
177
6
                                   const std::vector<TColumnAccessPath>& access_paths,
178
13
                                   std::set<uint64_t>& out_ids) {
179
13
        process_nested_access_paths(
180
13
                orc_field, access_paths, out_ids,
181
13
                [](const orc::Type* type) { return type->getColumnId(); },
182
13
                [](const orc::Type* type) { return type->getMaximumColumnId(); },
183
13
                HiveOrcNestedColumnUtils::extract_nested_column_ids);
184
13
    };
185
186
13
    for (const auto* slot : tuple_descriptor->slots()) {
187
13
        auto it = table_col_pos_to_orc_type_map.find(slot->col_pos());
188
13
        if (it == table_col_pos_to_orc_type_map.end()) {
189
            // Column not found in file
190
0
            continue;
191
0
        }
192
13
        const orc::Type* orc_field = it->second;
193
194
        // primitive (non-nested) types
195
13
        if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY &&
196
13
             slot->col_type() != TYPE_MAP)) {
197
6
            column_ids.insert(orc_field->getColumnId());
198
6
            if (slot->is_predicate()) {
199
0
                filter_column_ids.insert(orc_field->getColumnId());
200
0
            }
201
6
            continue;
202
6
        }
203
204
7
        const auto& all_access_paths = slot->all_access_paths();
205
        // complex types
206
7
        process_access_paths(orc_field, all_access_paths, column_ids);
207
208
7
        const auto& predicate_access_paths = slot->predicate_access_paths();
209
7
        if (!predicate_access_paths.empty()) {
210
6
            process_access_paths(orc_field, predicate_access_paths, filter_column_ids);
211
6
        }
212
7
    }
213
214
6
    return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids));
215
6
}
216
217
0
Status HiveParquetReader::on_before_init_reader(ReaderInitContext* ctx) {
218
0
    _column_descs = ctx->column_descs;
219
0
    _fill_col_name_to_block_idx = ctx->col_name_to_block_idx;
220
0
    RETURN_IF_ERROR(
221
0
            _extract_partition_values(*ctx->range, ctx->tuple_descriptor, _fill_partition_values));
222
0
    for (auto& desc : *ctx->column_descs) {
223
0
        if (desc.category == ColumnCategory::REGULAR ||
224
0
            desc.category == ColumnCategory::GENERATED) {
225
0
            ctx->column_names.push_back(desc.name);
226
0
        } else if (desc.category == ColumnCategory::SYNTHESIZED &&
227
0
                   desc.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) {
228
0
            auto topn_row_id_column_iter = _create_topn_row_id_column_iterator();
229
0
            this->register_synthesized_column_handler(
230
0
                    desc.name,
231
0
                    [iter = std::move(topn_row_id_column_iter), this, &desc](
232
0
                            Block* block, size_t rows) -> Status {
233
0
                        return fill_topn_row_id(iter, desc.name, block, rows);
234
0
                    });
235
0
            continue;
236
0
        }
237
0
    }
238
239
    // Get file metadata schema (available because _open_file() runs before this hook)
240
0
    const FieldDescriptor* field_desc = nullptr;
241
0
    RETURN_IF_ERROR(get_file_metadata_schema(&field_desc));
242
0
    DCHECK(field_desc != nullptr);
243
244
    // Build table_info_node based on config
245
0
    if (get_state()->query_options().hive_parquet_use_column_names) {
246
0
        RETURN_IF_ERROR(BuildTableInfoUtil::by_parquet_name(ctx->tuple_descriptor, *field_desc,
247
0
                                                            ctx->table_info_node, _is_file_slot));
248
0
    } else {
249
0
        ctx->table_info_node = std::make_shared<StructNode>();
250
0
        std::map<std::string, const SlotDescriptor*> slot_map;
251
0
        for (const auto& slot : ctx->tuple_descriptor->slots()) {
252
0
            slot_map.emplace(slot->col_name_lower_case(), slot);
253
0
        }
254
255
0
        auto parquet_fields_schema = field_desc->get_fields_schema();
256
0
        for (size_t idx = 0; idx < get_scan_params().column_idxs.size(); idx++) {
257
0
            auto table_column_name = ctx->column_names[idx];
258
0
            auto file_index = get_scan_params().column_idxs[idx];
259
260
0
            if (file_index >= parquet_fields_schema.size()) {
261
0
                ctx->table_info_node->add_not_exist_children(table_column_name);
262
0
            } else {
263
0
                auto field_node = std::make_shared<Node>();
264
0
                RETURN_IF_ERROR(BuildTableInfoUtil::by_parquet_name(
265
0
                        slot_map[table_column_name]->type(), parquet_fields_schema[file_index],
266
0
                        field_node));
267
0
                ctx->table_info_node->add_children(
268
0
                        table_column_name, parquet_fields_schema[file_index].name, field_node);
269
0
            }
270
0
            slot_map.erase(table_column_name);
271
0
        }
272
0
        for (const auto& [partition_col_name, _] : slot_map) {
273
0
            ctx->table_info_node->add_not_exist_children(partition_col_name);
274
0
        }
275
0
    }
276
277
    // Compute column_ids for lazy materialization
278
0
    auto column_id_result = ColumnIdResult();
279
0
    if (get_state()->query_options().hive_parquet_use_column_names) {
280
0
        column_id_result = _create_column_ids(field_desc, ctx->tuple_descriptor);
281
0
    } else {
282
0
        column_id_result =
283
0
                _create_column_ids_by_top_level_col_index(field_desc, ctx->tuple_descriptor);
284
0
    }
285
0
    ctx->column_ids = std::move(column_id_result.column_ids);
286
0
    ctx->filter_column_ids = std::move(column_id_result.filter_column_ids);
287
288
0
    _filter_groups = true;
289
0
    return Status::OK();
290
0
}
291
292
ColumnIdResult HiveParquetReader::_create_column_ids(const FieldDescriptor* field_desc,
293
20
                                                     const TupleDescriptor* tuple_descriptor) {
294
    // First, assign column IDs to the field descriptor
295
20
    auto* mutable_field_desc = const_cast<FieldDescriptor*>(field_desc);
296
20
    mutable_field_desc->assign_ids();
297
298
    // map top-level table column name (lower-cased) -> FieldSchema*
299
20
    std::unordered_map<std::string, const FieldSchema*> table_col_name_to_field_schema_map;
300
278
    for (int i = 0; i < field_desc->size(); ++i) {
301
258
        auto field_schema = field_desc->get_column(i);
302
258
        if (!field_schema) continue;
303
304
258
        table_col_name_to_field_schema_map[field_schema->lower_case_name] = field_schema;
305
258
    }
306
307
20
    std::set<uint64_t> column_ids;
308
20
    std::set<uint64_t> filter_column_ids;
309
310
    // helper to process access paths for a given top-level parquet field
311
20
    auto process_access_paths = [](const FieldSchema* parquet_field,
312
20
                                   const std::vector<TColumnAccessPath>& access_paths,
313
20
                                   std::set<uint64_t>& out_ids) {
314
13
        process_nested_access_paths(
315
13
                parquet_field, access_paths, out_ids,
316
13
                [](const FieldSchema* field) { return field->get_column_id(); },
317
13
                [](const FieldSchema* field) { return field->get_max_column_id(); },
318
13
                HiveParquetNestedColumnUtils::extract_nested_column_ids);
319
13
    };
320
321
64
    for (const auto* slot : tuple_descriptor->slots()) {
322
64
        auto it = table_col_name_to_field_schema_map.find(slot->col_name_lower_case());
323
64
        if (it == table_col_name_to_field_schema_map.end()) {
324
            // Column not found in file
325
0
            continue;
326
0
        }
327
64
        auto field_schema = it->second;
328
329
        // primitive (non-nested) types
330
64
        if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY &&
331
64
             slot->col_type() != TYPE_MAP)) {
332
57
            column_ids.insert(field_schema->column_id);
333
334
57
            if (slot->is_predicate()) {
335
0
                filter_column_ids.insert(field_schema->column_id);
336
0
            }
337
57
            continue;
338
57
        }
339
340
        // complex types
341
7
        const auto& all_access_paths = slot->all_access_paths();
342
7
        process_access_paths(field_schema, all_access_paths, column_ids);
343
344
7
        const auto& predicate_access_paths = slot->predicate_access_paths();
345
7
        if (!predicate_access_paths.empty()) {
346
6
            process_access_paths(field_schema, predicate_access_paths, filter_column_ids);
347
6
        }
348
7
    }
349
350
20
    return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids));
351
20
}
352
353
ColumnIdResult HiveParquetReader::_create_column_ids_by_top_level_col_index(
354
6
        const FieldDescriptor* field_desc, const TupleDescriptor* tuple_descriptor) {
355
    // First, assign column IDs to the field descriptor
356
6
    auto* mutable_field_desc = const_cast<FieldDescriptor*>(field_desc);
357
6
    mutable_field_desc->assign_ids();
358
359
    // map top-level table column position -> FieldSchema*
360
6
    std::unordered_map<uint64_t, const FieldSchema*> table_col_pos_to_field_schema_map;
361
54
    for (int i = 0; i < field_desc->size(); ++i) {
362
48
        auto field_schema = field_desc->get_column(i);
363
48
        if (!field_schema) continue;
364
365
48
        table_col_pos_to_field_schema_map[i] = field_schema;
366
48
    }
367
368
6
    std::set<uint64_t> column_ids;
369
6
    std::set<uint64_t> filter_column_ids;
370
371
    // helper to process access paths for a given top-level parquet field
372
6
    auto process_access_paths = [](const FieldSchema* parquet_field,
373
6
                                   const std::vector<TColumnAccessPath>& access_paths,
374
13
                                   std::set<uint64_t>& out_ids) {
375
13
        process_nested_access_paths(
376
13
                parquet_field, access_paths, out_ids,
377
13
                [](const FieldSchema* field) { return field->get_column_id(); },
378
13
                [](const FieldSchema* field) { return field->get_max_column_id(); },
379
13
                HiveParquetNestedColumnUtils::extract_nested_column_ids);
380
13
    };
381
382
13
    for (const auto* slot : tuple_descriptor->slots()) {
383
13
        auto it = table_col_pos_to_field_schema_map.find(slot->col_pos());
384
13
        if (it == table_col_pos_to_field_schema_map.end()) {
385
            // Column not found in file
386
0
            continue;
387
0
        }
388
13
        auto field_schema = it->second;
389
390
        // primitive (non-nested) types
391
13
        if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY &&
392
13
             slot->col_type() != TYPE_MAP)) {
393
6
            column_ids.insert(field_schema->column_id);
394
395
6
            if (slot->is_predicate()) {
396
0
                filter_column_ids.insert(field_schema->column_id);
397
0
            }
398
6
            continue;
399
6
        }
400
401
        // complex types
402
7
        const auto& all_access_paths = slot->all_access_paths();
403
7
        process_access_paths(field_schema, all_access_paths, column_ids);
404
405
7
        const auto& predicate_access_paths = slot->predicate_access_paths();
406
7
        if (!predicate_access_paths.empty()) {
407
6
            process_access_paths(field_schema, predicate_access_paths, filter_column_ids);
408
6
        }
409
7
    }
410
411
6
    return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids));
412
6
}
413
414
} // namespace doris