Coverage Report

Created: 2026-07-11 20:10

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
829
Status HiveOrcReader::on_before_init_reader(ReaderInitContext* ctx) {
31
829
    _column_descs = ctx->column_descs;
32
829
    _fill_col_name_to_block_idx = ctx->col_name_to_block_idx;
33
829
    RETURN_IF_ERROR(_extract_partition_values(*ctx->range, ctx->tuple_descriptor,
34
829
                                              _fill_partition_values,
35
829
                                              &_fill_partition_value_is_null));
36
9.23k
    for (const auto& desc : *ctx->column_descs) {
37
9.23k
        if (desc.category == ColumnCategory::REGULAR ||
38
9.23k
            desc.category == ColumnCategory::GENERATED) {
39
8.20k
            ctx->column_names.push_back(desc.name);
40
8.20k
        } else if (desc.category == ColumnCategory::SYNTHESIZED &&
41
1.03k
                   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
9.23k
    }
52
53
    // Get file type (available because _create_file_reader() runs before this hook)
54
829
    const orc::Type* orc_type_ptr = nullptr;
55
829
    RETURN_IF_ERROR(get_file_type(&orc_type_ptr));
56
829
    bool is_hive_col_name = OrcReader::is_hive1_col_name(orc_type_ptr);
57
58
    // Build table_info_node based on config
59
829
    if (get_state()->query_options().hive_orc_use_column_names && !is_hive_col_name) {
60
24
        RETURN_IF_ERROR(BuildTableInfoUtil::by_orc_name(ctx->tuple_descriptor, orc_type_ptr,
61
24
                                                        ctx->table_info_node, _is_file_slot));
62
805
    } else {
63
805
        ctx->table_info_node = std::make_shared<StructNode>();
64
805
        std::map<std::string, const SlotDescriptor*> slot_map;
65
9.10k
        for (const auto& slot : ctx->tuple_descriptor->slots()) {
66
9.10k
            slot_map.emplace(slot->col_name_lower_case(), slot);
67
9.10k
        }
68
69
8.88k
        for (size_t idx = 0; idx < get_scan_params().column_idxs.size(); idx++) {
70
8.07k
            auto table_column_name = ctx->column_names[idx];
71
8.07k
            auto file_index = get_scan_params().column_idxs[idx];
72
73
8.07k
            if (file_index >= orc_type_ptr->getSubtypeCount()) {
74
20
                ctx->table_info_node->add_not_exist_children(table_column_name);
75
8.05k
            } else {
76
8.05k
                auto field_node = std::make_shared<Node>();
77
8.05k
                RETURN_IF_ERROR(BuildTableInfoUtil::by_orc_name(
78
8.05k
                        slot_map[table_column_name]->type(), orc_type_ptr->getSubtype(file_index),
79
8.05k
                        field_node));
80
8.05k
                ctx->table_info_node->add_children(
81
8.05k
                        table_column_name, orc_type_ptr->getFieldName(file_index), field_node);
82
8.05k
            }
83
8.07k
            slot_map.erase(table_column_name);
84
8.07k
        }
85
1.03k
        for (const auto& [partition_col_name, _] : slot_map) {
86
1.03k
            ctx->table_info_node->add_not_exist_children(partition_col_name);
87
1.03k
        }
88
805
    }
89
90
    // Compute column_ids
91
829
    auto column_id_result = ColumnIdResult();
92
829
    if (get_state()->query_options().hive_orc_use_column_names && !is_hive_col_name) {
93
24
        column_id_result = _create_column_ids(orc_type_ptr, ctx->tuple_descriptor);
94
805
    } else {
95
805
        column_id_result =
96
805
                _create_column_ids_by_top_level_col_index(orc_type_ptr, ctx->tuple_descriptor);
97
805
    }
98
829
    ctx->column_ids = std::move(column_id_result.column_ids);
99
829
    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
829
    return Status::OK();
103
829
}
104
105
ColumnIdResult HiveOrcReader::_create_column_ids(const orc::Type* orc_type,
106
30
                                                 const TupleDescriptor* tuple_descriptor) {
107
    // map top-level table column name (lower-cased) -> orc::Type*
108
30
    std::unordered_map<std::string, const orc::Type*> table_col_name_to_orc_type_map;
109
224
    for (uint64_t i = 0; i < orc_type->getSubtypeCount(); ++i) {
110
194
        auto orc_sub_type = orc_type->getSubtype(i);
111
194
        if (!orc_sub_type) continue;
112
113
194
        std::string table_col_name = to_lower(orc_type->getFieldName(i));
114
194
        table_col_name_to_orc_type_map[table_col_name] = orc_sub_type;
115
194
    }
116
117
30
    std::set<uint64_t> column_ids;
118
30
    std::set<uint64_t> filter_column_ids;
119
120
    // helper to process access paths for a given top-level orc field
121
30
    auto process_access_paths = [](const orc::Type* orc_field,
122
30
                                   const std::vector<TColumnAccessPath>& access_paths,
123
30
                                   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
142
    for (const auto* slot : tuple_descriptor->slots()) {
132
142
        auto it = table_col_name_to_orc_type_map.find(slot->col_name_lower_case());
133
142
        if (it == table_col_name_to_orc_type_map.end()) {
134
            // Column not found in file
135
21
            continue;
136
21
        }
137
121
        const orc::Type* orc_field = it->second;
138
139
        // primitive (non-nested) types
140
121
        if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY &&
141
121
             slot->col_type() != TYPE_MAP)) {
142
114
            column_ids.insert(orc_field->getColumnId());
143
114
            if (slot->is_predicate()) {
144
0
                filter_column_ids.insert(orc_field->getColumnId());
145
0
            }
146
114
            continue;
147
114
        }
148
149
        // complex types
150
7
        const auto& all_access_paths = slot->all_access_paths();
151
7
        process_access_paths(orc_field, all_access_paths, column_ids);
152
153
7
        const auto& predicate_access_paths = slot->predicate_access_paths();
154
7
        if (!predicate_access_paths.empty()) {
155
6
            process_access_paths(orc_field, predicate_access_paths, filter_column_ids);
156
6
        }
157
7
    }
158
159
30
    return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids));
160
30
}
161
162
ColumnIdResult HiveOrcReader::_create_column_ids_by_top_level_col_index(
163
810
        const orc::Type* orc_type, const TupleDescriptor* tuple_descriptor) {
164
    // map top-level table column position -> orc::Type*
165
810
    std::unordered_map<uint64_t, const orc::Type*> table_col_pos_to_orc_type_map;
166
14.7k
    for (uint64_t i = 0; i < orc_type->getSubtypeCount(); ++i) {
167
13.9k
        auto orc_sub_type = orc_type->getSubtype(i);
168
13.9k
        if (!orc_sub_type) continue;
169
170
13.9k
        table_col_pos_to_orc_type_map[i] = orc_sub_type;
171
13.9k
    }
172
173
810
    std::set<uint64_t> column_ids;
174
810
    std::set<uint64_t> filter_column_ids;
175
176
    // helper to process access paths for a given top-level orc field
177
810
    auto process_access_paths = [](const orc::Type* orc_field,
178
810
                                   const std::vector<TColumnAccessPath>& access_paths,
179
810
                                   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
9.09k
    for (const auto* slot : tuple_descriptor->slots()) {
188
9.09k
        auto it = table_col_pos_to_orc_type_map.find(slot->col_pos());
189
9.09k
        if (it == table_col_pos_to_orc_type_map.end()) {
190
            // Column not found in file
191
9.07k
            continue;
192
9.07k
        }
193
16
        const orc::Type* orc_field = it->second;
194
195
        // primitive (non-nested) types
196
16
        if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY &&
197
16
             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
10
        const auto& all_access_paths = slot->all_access_paths();
206
        // complex types
207
10
        process_access_paths(orc_field, all_access_paths, column_ids);
208
209
10
        const auto& predicate_access_paths = slot->predicate_access_paths();
210
10
        if (!predicate_access_paths.empty()) {
211
6
            process_access_paths(orc_field, predicate_access_paths, filter_column_ids);
212
6
        }
213
10
    }
214
215
810
    return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids));
216
810
}
217
218
619
Status HiveParquetReader::on_before_init_reader(ReaderInitContext* ctx) {
219
619
    _column_descs = ctx->column_descs;
220
619
    _fill_col_name_to_block_idx = ctx->col_name_to_block_idx;
221
619
    RETURN_IF_ERROR(_extract_partition_values(*ctx->range, ctx->tuple_descriptor,
222
619
                                              _fill_partition_values,
223
619
                                              &_fill_partition_value_is_null));
224
2.32k
    for (const auto& desc : *ctx->column_descs) {
225
2.32k
        if (desc.category == ColumnCategory::REGULAR ||
226
2.32k
            desc.category == ColumnCategory::GENERATED) {
227
2.29k
            ctx->column_names.push_back(desc.name);
228
2.29k
        } 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.32k
    }
240
241
    // Get file metadata schema (available because _open_file() runs before this hook)
242
619
    const FieldDescriptor* field_desc = nullptr;
243
619
    RETURN_IF_ERROR(get_file_metadata_schema(&field_desc));
244
619
    DCHECK(field_desc != nullptr);
245
246
    // Build table_info_node based on config
247
619
    if (get_state()->query_options().hive_parquet_use_column_names) {
248
605
        RETURN_IF_ERROR(BuildTableInfoUtil::by_parquet_name(ctx->tuple_descriptor, *field_desc,
249
605
                                                            ctx->table_info_node, _is_file_slot));
250
605
    } 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
619
    auto column_id_result = ColumnIdResult();
281
619
    if (get_state()->query_options().hive_parquet_use_column_names) {
282
605
        column_id_result = _create_column_ids(field_desc, ctx->tuple_descriptor);
283
605
    } else {
284
14
        column_id_result =
285
14
                _create_column_ids_by_top_level_col_index(field_desc, ctx->tuple_descriptor);
286
14
    }
287
619
    ctx->column_ids = std::move(column_id_result.column_ids);
288
619
    ctx->filter_column_ids = std::move(column_id_result.filter_column_ids);
289
290
619
    _filter_groups = true;
291
619
    return Status::OK();
292
619
}
293
294
ColumnIdResult HiveParquetReader::_create_column_ids(const FieldDescriptor* field_desc,
295
611
                                                     const TupleDescriptor* tuple_descriptor) {
296
    // First, assign column IDs to the field descriptor
297
611
    auto* mutable_field_desc = const_cast<FieldDescriptor*>(field_desc);
298
611
    mutable_field_desc->assign_ids();
299
300
    // map top-level table column name (lower-cased) -> FieldSchema*
301
611
    std::unordered_map<std::string, const FieldSchema*> table_col_name_to_field_schema_map;
302
4.13k
    for (int i = 0; i < field_desc->size(); ++i) {
303
3.52k
        auto field_schema = field_desc->get_column(i);
304
3.52k
        if (!field_schema) continue;
305
306
3.52k
        table_col_name_to_field_schema_map[field_schema->lower_case_name] = field_schema;
307
3.52k
    }
308
309
611
    std::set<uint64_t> column_ids;
310
611
    std::set<uint64_t> filter_column_ids;
311
312
    // helper to process access paths for a given top-level parquet field
313
611
    auto process_access_paths = [](const FieldSchema* parquet_field,
314
611
                                   const std::vector<TColumnAccessPath>& access_paths,
315
611
                                   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.30k
    for (const auto* slot : tuple_descriptor->slots()) {
324
2.30k
        auto it = table_col_name_to_field_schema_map.find(slot->col_name_lower_case());
325
2.30k
        if (it == table_col_name_to_field_schema_map.end()) {
326
            // Column not found in file
327
48
            continue;
328
48
        }
329
2.25k
        auto field_schema = it->second;
330
331
        // primitive (non-nested) types
332
2.25k
        if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY &&
333
2.25k
             slot->col_type() != TYPE_MAP)) {
334
1.84k
            column_ids.insert(field_schema->column_id);
335
336
1.84k
            if (slot->is_predicate()) {
337
21
                filter_column_ids.insert(field_schema->column_id);
338
21
            }
339
1.84k
            continue;
340
1.84k
        }
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
611
    return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids));
353
611
}
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