Coverage Report

Created: 2026-06-05 15:29

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