Coverage Report

Created: 2026-10-26 08:51

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(_extract_partition_values(*ctx->range, ctx->tuple_descriptor,
34
11
                                              _fill_partition_values,
35
11
                                              &_fill_partition_value_is_null));
36
99
    for (const auto& desc : *ctx->column_descs) {
37
99
        if (desc.category == ColumnCategory::REGULAR ||
38
99
            desc.category == ColumnCategory::GENERATED) {
39
99
            ctx->column_names.push_back(desc.name);
40
99
        } else if (desc.category == ColumnCategory::SYNTHESIZED &&
41
0
                   desc.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) {
42
0
            auto topn_row_id_column_iter = _create_topn_row_id_column_iterator();
43
0
            this->register_synthesized_column_handler(
44
0
                    desc.name,
45
0
                    [iter = std::move(topn_row_id_column_iter), this, &desc](
46
0
                            Block* block, size_t rows) -> Status {
47
0
                        return fill_topn_row_id(iter, desc.name, block, rows);
48
0
                    });
49
0
            continue;
50
0
        }
51
99
    }
52
53
    // Get file type (available because _create_file_reader() runs before this hook)
54
11
    const orc::Type* orc_type_ptr = nullptr;
55
11
    RETURN_IF_ERROR(get_file_type(&orc_type_ptr));
56
11
    bool is_hive_col_name = OrcReader::is_hive1_col_name(orc_type_ptr);
57
58
    // Build table_info_node based on config
59
11
    if (get_state()->query_options().hive_orc_use_column_names && !is_hive_col_name) {
60
11
        RETURN_IF_ERROR(BuildTableInfoUtil::by_orc_name(ctx->tuple_descriptor, orc_type_ptr,
61
11
                                                        ctx->table_info_node, _is_file_slot));
62
11
    } else {
63
0
        ctx->table_info_node = std::make_shared<StructNode>();
64
0
        std::map<std::string, const SlotDescriptor*> slot_map;
65
0
        for (const auto& slot : ctx->tuple_descriptor->slots()) {
66
0
            slot_map.emplace(slot->col_name_lower_case(), slot);
67
0
        }
68
69
0
        for (size_t idx = 0; idx < get_scan_params().column_idxs.size(); idx++) {
70
0
            auto table_column_name = ctx->column_names[idx];
71
0
            auto file_index = get_scan_params().column_idxs[idx];
72
73
0
            if (file_index >= orc_type_ptr->getSubtypeCount()) {
74
0
                ctx->table_info_node->add_not_exist_children(table_column_name);
75
0
            } else {
76
0
                auto field_node = std::make_shared<Node>();
77
0
                RETURN_IF_ERROR(BuildTableInfoUtil::by_orc_name(
78
0
                        slot_map[table_column_name]->type(), orc_type_ptr->getSubtype(file_index),
79
0
                        field_node));
80
0
                ctx->table_info_node->add_children(
81
0
                        table_column_name, orc_type_ptr->getFieldName(file_index), field_node);
82
0
            }
83
0
            slot_map.erase(table_column_name);
84
0
        }
85
0
        for (const auto& [partition_col_name, _] : slot_map) {
86
0
            ctx->table_info_node->add_not_exist_children(partition_col_name);
87
0
        }
88
0
    }
89
90
    // Compute column_ids
91
11
    auto column_id_result = ColumnIdResult();
92
11
    if (get_state()->query_options().hive_orc_use_column_names && !is_hive_col_name) {
93
11
        column_id_result = _create_column_ids(orc_type_ptr, ctx->tuple_descriptor);
94
11
    } else {
95
0
        column_id_result =
96
0
                _create_column_ids_by_top_level_col_index(orc_type_ptr, ctx->tuple_descriptor);
97
0
    }
98
11
    ctx->column_ids = std::move(column_id_result.column_ids);
99
11
    ctx->filter_column_ids = std::move(column_id_result.filter_column_ids);
100
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
14
Status HiveParquetReader::on_before_init_reader(ReaderInitContext* ctx) {
218
14
    _column_descs = ctx->column_descs;
219
14
    _fill_col_name_to_block_idx = ctx->col_name_to_block_idx;
220
14
    RETURN_IF_ERROR(_extract_partition_values(*ctx->range, ctx->tuple_descriptor,
221
14
                                              _fill_partition_values,
222
14
                                              &_fill_partition_value_is_null));
223
51
    for (const auto& desc : *ctx->column_descs) {
224
51
        if (desc.category == ColumnCategory::REGULAR ||
225
51
            desc.category == ColumnCategory::GENERATED) {
226
51
            ctx->column_names.push_back(desc.name);
227
51
        } else if (desc.category == ColumnCategory::SYNTHESIZED &&
228
0
                   desc.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) {
229
0
            auto topn_row_id_column_iter = _create_topn_row_id_column_iterator();
230
0
            this->register_synthesized_column_handler(
231
0
                    desc.name,
232
0
                    [iter = std::move(topn_row_id_column_iter), this, &desc](
233
0
                            Block* block, size_t rows) -> Status {
234
0
                        return fill_topn_row_id(iter, desc.name, block, rows);
235
0
                    });
236
0
            continue;
237
0
        }
238
51
    }
239
240
    // Get file metadata schema (available because _open_file() runs before this hook)
241
14
    const FieldDescriptor* field_desc = nullptr;
242
14
    RETURN_IF_ERROR(get_file_metadata_schema(&field_desc));
243
14
    DCHECK(field_desc != nullptr);
244
245
    // Build table_info_node based on config
246
14
    if (get_state()->query_options().hive_parquet_use_column_names) {
247
14
        RETURN_IF_ERROR(BuildTableInfoUtil::by_parquet_name(ctx->tuple_descriptor, *field_desc,
248
14
                                                            ctx->table_info_node, _is_file_slot));
249
14
    } else {
250
0
        ctx->table_info_node = std::make_shared<StructNode>();
251
0
        std::map<std::string, const SlotDescriptor*> slot_map;
252
0
        for (const auto& slot : ctx->tuple_descriptor->slots()) {
253
0
            slot_map.emplace(slot->col_name_lower_case(), slot);
254
0
        }
255
256
0
        auto parquet_fields_schema = field_desc->get_fields_schema();
257
0
        for (size_t idx = 0; idx < get_scan_params().column_idxs.size(); idx++) {
258
0
            auto table_column_name = ctx->column_names[idx];
259
0
            auto file_index = get_scan_params().column_idxs[idx];
260
261
0
            if (file_index >= parquet_fields_schema.size()) {
262
0
                ctx->table_info_node->add_not_exist_children(table_column_name);
263
0
            } else {
264
0
                auto field_node = std::make_shared<Node>();
265
0
                RETURN_IF_ERROR(BuildTableInfoUtil::by_parquet_name(
266
0
                        slot_map[table_column_name]->type(), parquet_fields_schema[file_index],
267
0
                        field_node));
268
0
                ctx->table_info_node->add_children(
269
0
                        table_column_name, parquet_fields_schema[file_index].name, field_node);
270
0
            }
271
0
            slot_map.erase(table_column_name);
272
0
        }
273
0
        for (const auto& [partition_col_name, _] : slot_map) {
274
0
            ctx->table_info_node->add_not_exist_children(partition_col_name);
275
0
        }
276
0
    }
277
278
    // Compute column_ids for lazy materialization
279
14
    auto column_id_result = ColumnIdResult();
280
14
    if (get_state()->query_options().hive_parquet_use_column_names) {
281
14
        column_id_result = _create_column_ids(field_desc, ctx->tuple_descriptor);
282
14
    } else {
283
0
        column_id_result =
284
0
                _create_column_ids_by_top_level_col_index(field_desc, ctx->tuple_descriptor);
285
0
    }
286
14
    ctx->column_ids = std::move(column_id_result.column_ids);
287
14
    ctx->filter_column_ids = std::move(column_id_result.filter_column_ids);
288
289
14
    _filter_groups = true;
290
14
    return Status::OK();
291
14
}
292
293
ColumnIdResult HiveParquetReader::_create_column_ids(const FieldDescriptor* field_desc,
294
20
                                                     const TupleDescriptor* tuple_descriptor) {
295
    // First, assign column IDs to the field descriptor
296
20
    auto* mutable_field_desc = const_cast<FieldDescriptor*>(field_desc);
297
20
    mutable_field_desc->assign_ids();
298
299
    // map top-level table column name (lower-cased) -> FieldSchema*
300
20
    std::unordered_map<std::string, const FieldSchema*> table_col_name_to_field_schema_map;
301
278
    for (int i = 0; i < field_desc->size(); ++i) {
302
258
        auto field_schema = field_desc->get_column(i);
303
258
        if (!field_schema) continue;
304
305
258
        table_col_name_to_field_schema_map[field_schema->lower_case_name] = field_schema;
306
258
    }
307
308
20
    std::set<uint64_t> column_ids;
309
20
    std::set<uint64_t> filter_column_ids;
310
311
    // helper to process access paths for a given top-level parquet field
312
20
    auto process_access_paths = [](const FieldSchema* parquet_field,
313
20
                                   const std::vector<TColumnAccessPath>& access_paths,
314
20
                                   std::set<uint64_t>& out_ids) {
315
13
        process_nested_access_paths(
316
13
                parquet_field, access_paths, out_ids,
317
13
                [](const FieldSchema* field) { return field->get_column_id(); },
318
13
                [](const FieldSchema* field) { return field->get_max_column_id(); },
319
13
                HiveParquetNestedColumnUtils::extract_nested_column_ids);
320
13
    };
321
322
64
    for (const auto* slot : tuple_descriptor->slots()) {
323
64
        auto it = table_col_name_to_field_schema_map.find(slot->col_name_lower_case());
324
64
        if (it == table_col_name_to_field_schema_map.end()) {
325
            // Column not found in file
326
0
            continue;
327
0
        }
328
64
        auto field_schema = it->second;
329
330
        // primitive (non-nested) types
331
64
        if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY &&
332
64
             slot->col_type() != TYPE_MAP)) {
333
57
            column_ids.insert(field_schema->column_id);
334
335
57
            if (slot->is_predicate()) {
336
0
                filter_column_ids.insert(field_schema->column_id);
337
0
            }
338
57
            continue;
339
57
        }
340
341
        // complex types
342
7
        const auto& all_access_paths = slot->all_access_paths();
343
7
        process_access_paths(field_schema, all_access_paths, column_ids);
344
345
7
        const auto& predicate_access_paths = slot->predicate_access_paths();
346
7
        if (!predicate_access_paths.empty()) {
347
6
            process_access_paths(field_schema, predicate_access_paths, filter_column_ids);
348
6
        }
349
7
    }
350
351
20
    return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids));
352
20
}
353
354
ColumnIdResult HiveParquetReader::_create_column_ids_by_top_level_col_index(
355
6
        const FieldDescriptor* field_desc, const TupleDescriptor* tuple_descriptor) {
356
    // First, assign column IDs to the field descriptor
357
6
    auto* mutable_field_desc = const_cast<FieldDescriptor*>(field_desc);
358
6
    mutable_field_desc->assign_ids();
359
360
    // map top-level table column position -> FieldSchema*
361
6
    std::unordered_map<uint64_t, const FieldSchema*> table_col_pos_to_field_schema_map;
362
54
    for (int i = 0; i < field_desc->size(); ++i) {
363
48
        auto field_schema = field_desc->get_column(i);
364
48
        if (!field_schema) continue;
365
366
48
        table_col_pos_to_field_schema_map[i] = field_schema;
367
48
    }
368
369
6
    std::set<uint64_t> column_ids;
370
6
    std::set<uint64_t> filter_column_ids;
371
372
    // helper to process access paths for a given top-level parquet field
373
6
    auto process_access_paths = [](const FieldSchema* parquet_field,
374
6
                                   const std::vector<TColumnAccessPath>& access_paths,
375
13
                                   std::set<uint64_t>& out_ids) {
376
13
        process_nested_access_paths(
377
13
                parquet_field, access_paths, out_ids,
378
13
                [](const FieldSchema* field) { return field->get_column_id(); },
379
13
                [](const FieldSchema* field) { return field->get_max_column_id(); },
380
13
                HiveParquetNestedColumnUtils::extract_nested_column_ids);
381
13
    };
382
383
13
    for (const auto* slot : tuple_descriptor->slots()) {
384
13
        auto it = table_col_pos_to_field_schema_map.find(slot->col_pos());
385
13
        if (it == table_col_pos_to_field_schema_map.end()) {
386
            // Column not found in file
387
0
            continue;
388
0
        }
389
13
        auto field_schema = it->second;
390
391
        // primitive (non-nested) types
392
13
        if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY &&
393
13
             slot->col_type() != TYPE_MAP)) {
394
6
            column_ids.insert(field_schema->column_id);
395
396
6
            if (slot->is_predicate()) {
397
0
                filter_column_ids.insert(field_schema->column_id);
398
0
            }
399
6
            continue;
400
6
        }
401
402
        // complex types
403
7
        const auto& all_access_paths = slot->all_access_paths();
404
7
        process_access_paths(field_schema, all_access_paths, column_ids);
405
406
7
        const auto& predicate_access_paths = slot->predicate_access_paths();
407
7
        if (!predicate_access_paths.empty()) {
408
6
            process_access_paths(field_schema, predicate_access_paths, filter_column_ids);
409
6
        }
410
7
    }
411
412
6
    return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids));
413
6
}
414
415
} // namespace doris