Coverage Report

Created: 2026-07-12 14:58

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