Coverage Report

Created: 2026-06-02 13:16

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