Coverage Report

Created: 2026-07-09 16:19

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