Coverage Report

Created: 2026-10-13 04:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format/parquet/schema_desc.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/parquet/schema_desc.h"
19
20
#include <ctype.h>
21
22
#include <algorithm>
23
#include <ostream>
24
#include <utility>
25
26
#include "common/cast_set.h"
27
#include "common/logging.h"
28
#include "core/data_type/data_type_array.h"
29
#include "core/data_type/data_type_factory.hpp"
30
#include "core/data_type/data_type_map.h"
31
#include "core/data_type/data_type_struct.h"
32
#include "core/data_type/define_primitive_type.h"
33
#include "format/generic_reader.h"
34
#include "format/table/table_schema_change_helper.h"
35
#include "util/slice.h"
36
#include "util/string_util.h"
37
38
namespace doris {
39
40
5.77k
static bool is_group_node(const tparquet::SchemaElement& schema) {
41
5.77k
    return schema.num_children > 0;
42
5.77k
}
43
44
852
static bool is_list_node(const tparquet::SchemaElement& schema) {
45
852
    return schema.__isset.converted_type && schema.converted_type == tparquet::ConvertedType::LIST;
46
852
}
47
48
1.09k
static bool is_map_node(const tparquet::SchemaElement& schema) {
49
1.09k
    return schema.__isset.converted_type &&
50
1.09k
           (schema.converted_type == tparquet::ConvertedType::MAP ||
51
680
            schema.converted_type == tparquet::ConvertedType::MAP_KEY_VALUE);
52
1.09k
}
53
54
4.95k
static bool is_repeated_node(const tparquet::SchemaElement& schema) {
55
4.95k
    return schema.__isset.repetition_type &&
56
4.95k
           schema.repetition_type == tparquet::FieldRepetitionType::REPEATED;
57
4.95k
}
58
59
240
static bool is_required_node(const tparquet::SchemaElement& schema) {
60
240
    return schema.__isset.repetition_type &&
61
240
           schema.repetition_type == tparquet::FieldRepetitionType::REQUIRED;
62
240
}
63
64
5.15k
static bool is_optional_node(const tparquet::SchemaElement& schema) {
65
5.15k
    return schema.__isset.repetition_type &&
66
5.15k
           schema.repetition_type == tparquet::FieldRepetitionType::OPTIONAL;
67
5.15k
}
68
69
440
static int num_children_node(const tparquet::SchemaElement& schema) {
70
440
    return schema.__isset.num_children ? schema.num_children : 0;
71
440
}
72
73
/**
74
 * `repeated_parent_def_level` is the definition level of the first ancestor node whose repetition_type equals REPEATED.
75
 * Empty array/map values are not stored in doris columns, so have to use `repeated_parent_def_level` to skip the
76
 * empty or null values in ancestor node.
77
 *
78
 * For instance, considering an array of strings with 3 rows like the following:
79
 * null, [], [a, b, c]
80
 * We can store four elements in data column: null, a, b, c
81
 * and the offsets column is: 1, 1, 4
82
 * and the null map is: 1, 0, 0
83
 * For the i-th row in array column: range from `offsets[i - 1]` until `offsets[i]` represents the elements in this row,
84
 * so we can't store empty array/map values in doris data column.
85
 * As a comparison, spark does not require `repeated_parent_def_level`,
86
 * because the spark column stores empty array/map values , and use anther length column to indicate empty values.
87
 * Please reference: https://github.com/apache/spark/blob/master/sql/core/src/main/java/org/apache/spark/sql/execution/datasources/parquet/ParquetColumnVector.java
88
 *
89
 * Furthermore, we can also avoid store null array/map values in doris data column.
90
 * The same three rows as above, We can only store three elements in data column: a, b, c
91
 * and the offsets column is: 0, 0, 3
92
 * and the null map is: 1, 0, 0
93
 *
94
 * Inherit the repetition and definition level from parent node, if the parent node is repeated,
95
 * we should set repeated_parent_def_level = definition_level, otherwise as repeated_parent_def_level.
96
 * @param parent parent node
97
 * @param repeated_parent_def_level the first ancestor node whose repetition_type equals REPEATED
98
 */
99
1.09k
static void set_child_node_level(FieldSchema* parent, int16_t repeated_parent_def_level) {
100
2.40k
    for (auto& child : parent->children) {
101
2.40k
        child.repetition_level = parent->repetition_level;
102
2.40k
        child.definition_level = parent->definition_level;
103
2.40k
        child.repeated_parent_def_level = repeated_parent_def_level;
104
2.40k
    }
105
1.09k
}
106
107
440
static bool is_struct_list_node(const tparquet::SchemaElement& schema) {
108
440
    const std::string& name = schema.name;
109
440
    static const Slice array_slice("array", 5);
110
440
    static const Slice tuple_slice("_tuple", 6);
111
440
    Slice slice(name);
112
440
    return slice == array_slice || slice.ends_with(tuple_slice);
113
440
}
114
115
0
std::string FieldSchema::debug_string() const {
116
0
    std::stringstream ss;
117
0
    ss << "FieldSchema(name=" << name << ", R=" << repetition_level << ", D=" << definition_level;
118
0
    if (children.size() > 0) {
119
0
        ss << ", type=" << data_type->get_name() << ", children=[";
120
0
        for (int i = 0; i < children.size(); ++i) {
121
0
            if (i != 0) {
122
0
                ss << ", ";
123
0
            }
124
0
            ss << children[i].debug_string();
125
0
        }
126
0
        ss << "]";
127
0
    } else {
128
0
        ss << ", physical_type=" << physical_type;
129
0
        ss << " , doris_type=" << data_type->get_name();
130
0
    }
131
0
    ss << ")";
132
0
    return ss.str();
133
0
}
134
135
382
Status FieldDescriptor::parse_from_thrift(const std::vector<tparquet::SchemaElement>& t_schemas) {
136
382
    if (t_schemas.size() == 0 || !is_group_node(t_schemas[0])) {
137
0
        return Status::InvalidArgument("Wrong parquet root schema element");
138
0
    }
139
382
    const auto& root_schema = t_schemas[0];
140
382
    _fields.resize(root_schema.num_children);
141
382
    _next_schema_pos = 1;
142
143
3.13k
    for (int i = 0; i < root_schema.num_children; ++i) {
144
2.75k
        RETURN_IF_ERROR(parse_node_field(t_schemas, _next_schema_pos, &_fields[i]));
145
2.75k
        if (_name_to_field.find(_fields[i].name) != _name_to_field.end()) {
146
0
            return Status::InvalidArgument("Duplicated field name: {}", _fields[i].name);
147
0
        }
148
2.75k
        _name_to_field.emplace(_fields[i].name, &_fields[i]);
149
2.75k
    }
150
151
382
    if (_next_schema_pos != t_schemas.size()) {
152
0
        return Status::InvalidArgument("Remaining {} unparsed schema elements",
153
0
                                       t_schemas.size() - _next_schema_pos);
154
0
    }
155
156
382
    return Status::OK();
157
382
}
158
159
Status FieldDescriptor::parse_node_field(const std::vector<tparquet::SchemaElement>& t_schemas,
160
5.15k
                                         size_t curr_pos, FieldSchema* node_field) {
161
5.15k
    if (curr_pos >= t_schemas.size()) {
162
0
        return Status::InvalidArgument("Out-of-bounds index of schema elements");
163
0
    }
164
5.15k
    auto& t_schema = t_schemas[curr_pos];
165
5.15k
    if (is_group_node(t_schema)) {
166
        // nested structure or nullable list
167
1.09k
        return parse_group_field(t_schemas, curr_pos, node_field);
168
1.09k
    }
169
4.06k
    if (is_repeated_node(t_schema)) {
170
        // repeated <primitive-type> <name> (LIST)
171
        // produce required list<element>
172
0
        node_field->repetition_level++;
173
0
        node_field->definition_level++;
174
0
        node_field->children.resize(1);
175
0
        set_child_node_level(node_field, node_field->definition_level);
176
0
        auto child = &node_field->children[0];
177
0
        parse_physical_field(t_schema, false, child);
178
179
0
        node_field->name = t_schema.name;
180
0
        node_field->lower_case_name = to_lower(t_schema.name);
181
0
        node_field->data_type = std::make_shared<DataTypeArray>(make_nullable(child->data_type));
182
0
        _next_schema_pos = curr_pos + 1;
183
0
        node_field->field_id = t_schema.__isset.field_id ? t_schema.field_id : -1;
184
4.06k
    } else {
185
4.06k
        bool is_optional = is_optional_node(t_schema);
186
4.06k
        if (is_optional) {
187
3.58k
            node_field->definition_level++;
188
3.58k
        }
189
4.06k
        parse_physical_field(t_schema, is_optional, node_field);
190
4.06k
        _next_schema_pos = curr_pos + 1;
191
4.06k
    }
192
4.06k
    return Status::OK();
193
5.15k
}
194
195
void FieldDescriptor::parse_physical_field(const tparquet::SchemaElement& physical_schema,
196
4.06k
                                           bool is_nullable, FieldSchema* physical_field) {
197
4.06k
    physical_field->name = physical_schema.name;
198
4.06k
    physical_field->lower_case_name = to_lower(physical_field->name);
199
4.06k
    physical_field->parquet_schema = physical_schema;
200
4.06k
    physical_field->physical_type = physical_schema.type;
201
4.06k
    physical_field->column_id = UNASSIGNED_COLUMN_ID; // Initialize column_id
202
4.06k
    _physical_fields.push_back(physical_field);
203
4.06k
    physical_field->physical_column_index = cast_set<int>(_physical_fields.size() - 1);
204
4.06k
    auto type = get_doris_type(physical_schema, is_nullable);
205
4.06k
    physical_field->data_type = type.first;
206
4.06k
    physical_field->is_type_compatibility = type.second;
207
4.06k
    physical_field->field_id = physical_schema.__isset.field_id ? physical_schema.field_id : -1;
208
4.06k
}
209
210
std::pair<DataTypePtr, bool> FieldDescriptor::get_doris_type(
211
4.06k
        const tparquet::SchemaElement& physical_schema, bool nullable) {
212
4.06k
    std::pair<DataTypePtr, bool> ans = {std::make_shared<DataTypeNothing>(), false};
213
4.06k
    try {
214
4.06k
        if (physical_schema.__isset.logicalType) {
215
2.04k
            ans = convert_to_doris_type(physical_schema.logicalType, nullable);
216
2.04k
        } else if (physical_schema.__isset.converted_type) {
217
371
            ans = convert_to_doris_type(physical_schema, nullable);
218
371
        }
219
4.06k
    } catch (...) {
220
        // now the Not supported exception are ignored
221
        // so those byte_array maybe be treated as varbinary(now) : string(before)
222
0
    }
223
4.06k
    if (ans.first->get_primitive_type() == PrimitiveType::INVALID_TYPE) {
224
1.64k
        switch (physical_schema.type) {
225
196
        case tparquet::Type::BOOLEAN:
226
196
            ans.first = DataTypeFactory::instance().create_data_type(TYPE_BOOLEAN, nullable);
227
196
            break;
228
590
        case tparquet::Type::INT32:
229
590
            ans.first = DataTypeFactory::instance().create_data_type(TYPE_INT, nullable);
230
590
            break;
231
274
        case tparquet::Type::INT64:
232
274
            ans.first = DataTypeFactory::instance().create_data_type(TYPE_BIGINT, nullable);
233
274
            break;
234
52
        case tparquet::Type::INT96:
235
            // INT96 has no timezone annotation. A table's logical schema may override this
236
            // for known instant columns, but a catalog option cannot supply missing semantics.
237
52
            ans.first =
238
52
                    DataTypeFactory::instance().create_data_type(TYPE_DATETIMEV2, nullable, 0, 6);
239
52
            break;
240
182
        case tparquet::Type::FLOAT:
241
182
            ans.first = DataTypeFactory::instance().create_data_type(TYPE_FLOAT, nullable);
242
182
            break;
243
301
        case tparquet::Type::DOUBLE:
244
301
            ans.first = DataTypeFactory::instance().create_data_type(TYPE_DOUBLE, nullable);
245
301
            break;
246
50
        case tparquet::Type::BYTE_ARRAY:
247
50
            if (_enable_mapping_varbinary) {
248
                // if physical_schema not set logicalType and converted_type,
249
                // we treat BYTE_ARRAY as VARBINARY by default, so that we can read all data directly.
250
25
                ans.first = DataTypeFactory::instance().create_data_type(TYPE_VARBINARY, nullable);
251
25
            } else {
252
25
                ans.first = DataTypeFactory::instance().create_data_type(TYPE_STRING, nullable);
253
25
            }
254
50
            break;
255
0
        case tparquet::Type::FIXED_LEN_BYTE_ARRAY:
256
0
            ans.first = DataTypeFactory::instance().create_data_type(TYPE_STRING, nullable);
257
0
            break;
258
0
        default:
259
0
            throw Exception(Status::InternalError("Not supported parquet logicalType{}",
260
0
                                                  physical_schema.type));
261
0
            break;
262
1.64k
        }
263
1.64k
    }
264
4.06k
    return ans;
265
4.06k
}
266
267
std::pair<DataTypePtr, bool> FieldDescriptor::convert_to_doris_type(
268
2.04k
        tparquet::LogicalType logicalType, bool nullable) {
269
2.04k
    std::pair<DataTypePtr, bool> ans = {std::make_shared<DataTypeNothing>(), false};
270
2.04k
    bool& is_type_compatibility = ans.second;
271
2.04k
    if (logicalType.__isset.STRING) {
272
1.16k
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_STRING, nullable);
273
1.16k
    } else if (logicalType.__isset.DECIMAL) {
274
266
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_DECIMAL128I, nullable,
275
266
                                                                 logicalType.DECIMAL.precision,
276
266
                                                                 logicalType.DECIMAL.scale);
277
622
    } else if (logicalType.__isset.DATE) {
278
108
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_DATEV2, nullable);
279
514
    } else if (logicalType.__isset.INTEGER) {
280
187
        if (logicalType.INTEGER.isSigned) {
281
187
            if (logicalType.INTEGER.bitWidth <= 8) {
282
62
                ans.first = DataTypeFactory::instance().create_data_type(TYPE_TINYINT, nullable);
283
125
            } else if (logicalType.INTEGER.bitWidth <= 16) {
284
125
                ans.first = DataTypeFactory::instance().create_data_type(TYPE_SMALLINT, nullable);
285
125
            } else if (logicalType.INTEGER.bitWidth <= 32) {
286
0
                ans.first = DataTypeFactory::instance().create_data_type(TYPE_INT, nullable);
287
0
            } else {
288
0
                ans.first = DataTypeFactory::instance().create_data_type(TYPE_BIGINT, nullable);
289
0
            }
290
187
        } else {
291
0
            is_type_compatibility = true;
292
0
            if (logicalType.INTEGER.bitWidth <= 8) {
293
0
                ans.first = DataTypeFactory::instance().create_data_type(TYPE_SMALLINT, nullable);
294
0
            } else if (logicalType.INTEGER.bitWidth <= 16) {
295
0
                ans.first = DataTypeFactory::instance().create_data_type(TYPE_INT, nullable);
296
0
            } else if (logicalType.INTEGER.bitWidth <= 32) {
297
0
                ans.first = DataTypeFactory::instance().create_data_type(TYPE_BIGINT, nullable);
298
0
            } else {
299
0
                ans.first = DataTypeFactory::instance().create_data_type(TYPE_LARGEINT, nullable);
300
0
            }
301
0
        }
302
327
    } else if (logicalType.__isset.TIME) {
303
0
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_TIMEV2, nullable);
304
327
    } else if (logicalType.__isset.TIMESTAMP) {
305
114
        if (_enable_mapping_timestamp_tz) {
306
12
            if (logicalType.TIMESTAMP.isAdjustedToUTC) {
307
                // treat TIMESTAMP with isAdjustedToUTC as TIMESTAMPTZ
308
0
                ans.first = DataTypeFactory::instance().create_data_type(
309
0
                        TYPE_TIMESTAMPTZ, nullable, 0,
310
0
                        logicalType.TIMESTAMP.unit.__isset.MILLIS ? 3 : 6);
311
0
                return ans;
312
0
            }
313
12
        }
314
114
        ans.first = DataTypeFactory::instance().create_data_type(
315
114
                TYPE_DATETIMEV2, nullable, 0, logicalType.TIMESTAMP.unit.__isset.MILLIS ? 3 : 6);
316
213
    } else if (logicalType.__isset.JSON) {
317
0
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_STRING, nullable);
318
213
    } else if (logicalType.__isset.UUID) {
319
        // UUID is a logical type; its binary storage does not make it an untyped byte array.
320
213
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_UUID, nullable);
321
213
    } else if (logicalType.__isset.FLOAT16) {
322
0
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_FLOAT, nullable);
323
0
    } else {
324
0
        throw Exception(Status::InternalError("Not supported parquet logicalType"));
325
0
    }
326
2.04k
    return ans;
327
2.04k
}
328
329
std::pair<DataTypePtr, bool> FieldDescriptor::convert_to_doris_type(
330
371
        const tparquet::SchemaElement& physical_schema, bool nullable) {
331
371
    std::pair<DataTypePtr, bool> ans = {std::make_shared<DataTypeNothing>(), false};
332
371
    bool& is_type_compatibility = ans.second;
333
371
    switch (physical_schema.converted_type) {
334
155
    case tparquet::ConvertedType::type::UTF8:
335
155
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_STRING, nullable);
336
155
        break;
337
62
    case tparquet::ConvertedType::type::DECIMAL:
338
62
        ans.first = DataTypeFactory::instance().create_data_type(
339
62
                TYPE_DECIMAL128I, nullable, physical_schema.precision, physical_schema.scale);
340
62
        break;
341
56
    case tparquet::ConvertedType::type::DATE:
342
56
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_DATEV2, nullable);
343
56
        break;
344
0
    case tparquet::ConvertedType::type::TIME_MILLIS:
345
0
        [[fallthrough]];
346
0
    case tparquet::ConvertedType::type::TIME_MICROS:
347
0
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_TIMEV2, nullable);
348
0
        break;
349
0
    case tparquet::ConvertedType::type::TIMESTAMP_MILLIS:
350
0
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_DATETIMEV2, nullable, 0, 3);
351
0
        break;
352
24
    case tparquet::ConvertedType::type::TIMESTAMP_MICROS:
353
24
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_DATETIMEV2, nullable, 0, 6);
354
24
        break;
355
36
    case tparquet::ConvertedType::type::INT_8:
356
36
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_TINYINT, nullable);
357
36
        break;
358
0
    case tparquet::ConvertedType::type::UINT_8:
359
0
        is_type_compatibility = true;
360
0
        [[fallthrough]];
361
38
    case tparquet::ConvertedType::type::INT_16:
362
38
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_SMALLINT, nullable);
363
38
        break;
364
0
    case tparquet::ConvertedType::type::UINT_16:
365
0
        is_type_compatibility = true;
366
0
        [[fallthrough]];
367
0
    case tparquet::ConvertedType::type::INT_32:
368
0
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_INT, nullable);
369
0
        break;
370
0
    case tparquet::ConvertedType::type::UINT_32:
371
0
        is_type_compatibility = true;
372
0
        [[fallthrough]];
373
0
    case tparquet::ConvertedType::type::INT_64:
374
0
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_BIGINT, nullable);
375
0
        break;
376
0
    case tparquet::ConvertedType::type::UINT_64:
377
0
        is_type_compatibility = true;
378
0
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_LARGEINT, nullable);
379
0
        break;
380
0
    case tparquet::ConvertedType::type::JSON:
381
0
        ans.first = DataTypeFactory::instance().create_data_type(TYPE_STRING, nullable);
382
0
        break;
383
0
    default:
384
0
        throw Exception(Status::InternalError("Not supported parquet ConvertedType: {}",
385
0
                                              physical_schema.converted_type));
386
371
    }
387
371
    return ans;
388
371
}
389
390
Status FieldDescriptor::parse_group_field(const std::vector<tparquet::SchemaElement>& t_schemas,
391
1.09k
                                          size_t curr_pos, FieldSchema* group_field) {
392
1.09k
    auto& group_schema = t_schemas[curr_pos];
393
1.09k
    if (is_map_node(group_schema)) {
394
        // the map definition:
395
        // optional group <name> (MAP) {
396
        //   repeated group map (MAP_KEY_VALUE) {
397
        //     required <type> key;
398
        //     optional <type> value;
399
        //   }
400
        // }
401
240
        return parse_map_field(t_schemas, curr_pos, group_field);
402
240
    }
403
852
    if (is_list_node(group_schema)) {
404
        // the list definition:
405
        // optional group <name> (LIST) {
406
        //   repeated group [bag | list] { // hive or spark
407
        //     optional <type> [array_element | element]; // hive or spark
408
        //   }
409
        // }
410
440
        return parse_list_field(t_schemas, curr_pos, group_field);
411
440
    }
412
413
412
    if (is_repeated_node(group_schema)) {
414
0
        group_field->repetition_level++;
415
0
        group_field->definition_level++;
416
0
        group_field->children.resize(1);
417
0
        set_child_node_level(group_field, group_field->definition_level);
418
0
        auto struct_field = &group_field->children[0];
419
        // the list of struct:
420
        // repeated group <name> (LIST) {
421
        //   optional/required <type> <name>;
422
        //   ...
423
        // }
424
        // produce a non-null list<struct>
425
0
        RETURN_IF_ERROR(parse_struct_field(t_schemas, curr_pos, struct_field));
426
427
0
        group_field->name = group_schema.name;
428
0
        group_field->lower_case_name = to_lower(group_field->name);
429
0
        group_field->column_id = UNASSIGNED_COLUMN_ID; // Initialize column_id
430
0
        group_field->data_type =
431
0
                std::make_shared<DataTypeArray>(make_nullable(struct_field->data_type));
432
0
        group_field->field_id = group_schema.__isset.field_id ? group_schema.field_id : -1;
433
412
    } else {
434
412
        RETURN_IF_ERROR(parse_struct_field(t_schemas, curr_pos, group_field));
435
412
    }
436
437
412
    return Status::OK();
438
412
}
439
440
Status FieldDescriptor::parse_list_field(const std::vector<tparquet::SchemaElement>& t_schemas,
441
440
                                         size_t curr_pos, FieldSchema* list_field) {
442
    // the list definition:
443
    // spark and hive have three level schemas but with different schema name
444
    // spark: <column-name> - "list" - "element"
445
    // hive: <column-name> - "bag" - "array_element"
446
    // parse three level schemas to two level primitive like: LIST<INT>,
447
    // or nested structure like: LIST<MAP<INT, INT>>
448
440
    auto& first_level = t_schemas[curr_pos];
449
440
    if (first_level.num_children != 1) {
450
0
        return Status::InvalidArgument("List element should have only one child");
451
0
    }
452
453
440
    if (curr_pos + 1 >= t_schemas.size()) {
454
0
        return Status::InvalidArgument("List element should have the second level schema");
455
0
    }
456
457
440
    if (first_level.repetition_type == tparquet::FieldRepetitionType::REPEATED) {
458
0
        return Status::InvalidArgument("List element can't be a repeated schema");
459
0
    }
460
461
    // the repeated schema element
462
440
    auto& second_level = t_schemas[curr_pos + 1];
463
440
    if (second_level.repetition_type != tparquet::FieldRepetitionType::REPEATED) {
464
0
        return Status::InvalidArgument("The second level of list element should be repeated");
465
0
    }
466
467
    // This indicates if this list is nullable.
468
440
    bool is_optional = is_optional_node(first_level);
469
440
    if (is_optional) {
470
388
        list_field->definition_level++;
471
388
    }
472
440
    list_field->repetition_level++;
473
440
    list_field->definition_level++;
474
440
    list_field->children.resize(1);
475
440
    FieldSchema* list_child = &list_field->children[0];
476
477
440
    size_t num_children = num_children_node(second_level);
478
440
    if (num_children > 0) {
479
440
        if (num_children == 1 && !is_struct_list_node(second_level)) {
480
            // optional field, and the third level element is the nested structure in list
481
            // produce nested structure like: LIST<INT>, LIST<MAP>, LIST<LIST<...>>
482
            // skip bag/list, it's a repeated element.
483
440
            set_child_node_level(list_field, list_field->definition_level);
484
440
            RETURN_IF_ERROR(parse_node_field(t_schemas, curr_pos + 2, list_child));
485
440
        } else {
486
            // required field, produce the list of struct
487
0
            set_child_node_level(list_field, list_field->definition_level);
488
0
            RETURN_IF_ERROR(parse_struct_field(t_schemas, curr_pos + 1, list_child));
489
0
        }
490
440
    } else if (num_children == 0) {
491
        // required two level list, for compatibility reason.
492
0
        set_child_node_level(list_field, list_field->definition_level);
493
0
        parse_physical_field(second_level, false, list_child);
494
0
        _next_schema_pos = curr_pos + 2;
495
0
    }
496
497
440
    list_field->name = first_level.name;
498
440
    list_field->lower_case_name = to_lower(first_level.name);
499
440
    list_field->column_id = UNASSIGNED_COLUMN_ID; // Initialize column_id
500
440
    list_field->data_type =
501
440
            std::make_shared<DataTypeArray>(make_nullable(list_field->children[0].data_type));
502
440
    if (is_optional) {
503
388
        list_field->data_type = make_nullable(list_field->data_type);
504
388
    }
505
440
    list_field->field_id = first_level.__isset.field_id ? first_level.field_id : -1;
506
507
440
    return Status::OK();
508
440
}
509
510
Status FieldDescriptor::parse_map_field(const std::vector<tparquet::SchemaElement>& t_schemas,
511
240
                                        size_t curr_pos, FieldSchema* map_field) {
512
    // the map definition in parquet:
513
    // optional group <name> (MAP) {
514
    //   repeated group map (MAP_KEY_VALUE) {
515
    //     required <type> key;
516
    //     optional <type> value;
517
    //   }
518
    // }
519
    // Map value can be optional, the map without values is a SET
520
240
    if (curr_pos + 2 >= t_schemas.size()) {
521
0
        return Status::InvalidArgument("Map element should have at least three levels");
522
0
    }
523
240
    auto& map_schema = t_schemas[curr_pos];
524
240
    if (map_schema.num_children != 1) {
525
0
        return Status::InvalidArgument(
526
0
                "Map element should have only one child(name='map', type='MAP_KEY_VALUE')");
527
0
    }
528
240
    if (is_repeated_node(map_schema)) {
529
0
        return Status::InvalidArgument("Map element can't be a repeated schema");
530
0
    }
531
240
    auto& map_key_value = t_schemas[curr_pos + 1];
532
240
    if (!is_group_node(map_key_value) || !is_repeated_node(map_key_value)) {
533
0
        return Status::InvalidArgument(
534
0
                "the second level in map must be a repeated group(key and value)");
535
0
    }
536
240
    auto& map_key = t_schemas[curr_pos + 2];
537
240
    if (!is_required_node(map_key)) {
538
0
        LOG(WARNING) << "Filed " << map_schema.name << " is map type, but with nullable key column";
539
0
    }
540
541
240
    if (map_key_value.num_children == 1) {
542
        // The map with three levels is a SET
543
0
        return parse_list_field(t_schemas, curr_pos, map_field);
544
0
    }
545
240
    if (map_key_value.num_children != 2) {
546
        // A standard map should have four levels
547
0
        return Status::InvalidArgument(
548
0
                "the second level in map(MAP_KEY_VALUE) should have two children");
549
0
    }
550
    // standard map
551
240
    bool is_optional = is_optional_node(map_schema);
552
240
    if (is_optional) {
553
214
        map_field->definition_level++;
554
214
    }
555
240
    map_field->repetition_level++;
556
240
    map_field->definition_level++;
557
558
    // Directly create key and value children instead of intermediate key_value node
559
240
    map_field->children.resize(2);
560
    // map is a repeated node, we should set the `repeated_parent_def_level` of its children as `definition_level`
561
240
    set_child_node_level(map_field, map_field->definition_level);
562
563
240
    auto key_field = &map_field->children[0];
564
240
    auto value_field = &map_field->children[1];
565
566
    // Parse key and value fields directly from the key_value group's children
567
240
    _next_schema_pos = curr_pos + 2; // Skip key_value group, go directly to key
568
240
    RETURN_IF_ERROR(parse_node_field(t_schemas, _next_schema_pos, key_field));
569
240
    RETURN_IF_ERROR(parse_node_field(t_schemas, _next_schema_pos, value_field));
570
571
240
    map_field->name = map_schema.name;
572
240
    map_field->lower_case_name = to_lower(map_field->name);
573
240
    map_field->column_id = UNASSIGNED_COLUMN_ID; // Initialize column_id
574
240
    map_field->data_type = std::make_shared<DataTypeMap>(make_nullable(key_field->data_type),
575
240
                                                         make_nullable(value_field->data_type));
576
240
    if (is_optional) {
577
214
        map_field->data_type = make_nullable(map_field->data_type);
578
214
    }
579
240
    map_field->field_id = map_schema.__isset.field_id ? map_schema.field_id : -1;
580
581
240
    return Status::OK();
582
240
}
583
584
Status FieldDescriptor::parse_struct_field(const std::vector<tparquet::SchemaElement>& t_schemas,
585
412
                                           size_t curr_pos, FieldSchema* struct_field) {
586
    // the nested column in parquet, parse group to struct.
587
412
    auto& struct_schema = t_schemas[curr_pos];
588
412
    bool is_optional = is_optional_node(struct_schema);
589
412
    if (is_optional) {
590
393
        struct_field->definition_level++;
591
393
    }
592
412
    auto num_children = struct_schema.num_children;
593
412
    struct_field->children.resize(num_children);
594
412
    set_child_node_level(struct_field, struct_field->repeated_parent_def_level);
595
412
    _next_schema_pos = curr_pos + 1;
596
1.89k
    for (int i = 0; i < num_children; ++i) {
597
1.48k
        RETURN_IF_ERROR(parse_node_field(t_schemas, _next_schema_pos, &struct_field->children[i]));
598
1.48k
    }
599
412
    struct_field->name = struct_schema.name;
600
412
    struct_field->lower_case_name = to_lower(struct_field->name);
601
412
    struct_field->column_id = UNASSIGNED_COLUMN_ID; // Initialize column_id
602
603
412
    struct_field->field_id = struct_schema.__isset.field_id ? struct_schema.field_id : -1;
604
412
    DataTypes res_data_types;
605
412
    std::vector<String> names;
606
1.89k
    for (int i = 0; i < num_children; ++i) {
607
1.48k
        res_data_types.push_back(make_nullable(struct_field->children[i].data_type));
608
1.48k
        names.push_back(struct_field->children[i].name);
609
1.48k
    }
610
412
    struct_field->data_type = std::make_shared<DataTypeStruct>(res_data_types, names);
611
412
    if (is_optional) {
612
393
        struct_field->data_type = make_nullable(struct_field->data_type);
613
393
    }
614
412
    return Status::OK();
615
412
}
616
617
5
int FieldDescriptor::get_column_index(const std::string& column) const {
618
15
    for (int32_t i = 0; i < _fields.size(); i++) {
619
15
        if (_fields[i].name == column) {
620
5
            return i;
621
5
        }
622
15
    }
623
0
    return -1;
624
5
}
625
626
3.45k
FieldSchema* FieldDescriptor::get_column(const std::string& name) const {
627
3.45k
    auto it = _name_to_field.find(name);
628
3.45k
    if (it != _name_to_field.end()) {
629
3.45k
        return it->second;
630
3.45k
    }
631
0
    throw Exception(Status::InternalError("Name {} not found in FieldDescriptor!", name));
632
0
    return nullptr;
633
3.45k
}
634
635
37
void FieldDescriptor::get_column_names(std::unordered_set<std::string>* names) const {
636
37
    names->clear();
637
650
    for (const FieldSchema& f : _fields) {
638
650
        names->emplace(f.name);
639
650
    }
640
37
}
641
642
0
std::string FieldDescriptor::debug_string() const {
643
0
    std::stringstream ss;
644
0
    ss << "fields=[";
645
0
    for (int i = 0; i < _fields.size(); ++i) {
646
0
        if (i != 0) {
647
0
            ss << ", ";
648
0
        }
649
0
        ss << _fields[i].debug_string();
650
0
    }
651
0
    ss << "]";
652
0
    return ss.str();
653
0
}
654
655
51
void FieldDescriptor::assign_ids() {
656
51
    uint64_t next_id = 1;
657
398
    for (auto& field : _fields) {
658
398
        field.assign_ids(next_id);
659
398
    }
660
51
}
661
662
0
const FieldSchema* FieldDescriptor::find_column_by_id(uint64_t column_id) const {
663
0
    for (const auto& field : _fields) {
664
0
        if (auto result = field.find_column_by_id(column_id)) {
665
0
            return result;
666
0
        }
667
0
    }
668
0
    return nullptr;
669
0
}
670
671
1.95k
void FieldSchema::assign_ids(uint64_t& next_id) {
672
1.95k
    column_id = next_id++;
673
674
1.95k
    for (auto& child : children) {
675
1.55k
        child.assign_ids(next_id);
676
1.55k
    }
677
678
1.95k
    max_column_id = next_id - 1;
679
1.95k
}
680
681
0
const FieldSchema* FieldSchema::find_column_by_id(uint64_t target_id) const {
682
0
    if (column_id == target_id) {
683
0
        return this;
684
0
    }
685
686
0
    for (const auto& child : children) {
687
0
        if (auto result = child.find_column_by_id(target_id)) {
688
0
            return result;
689
0
        }
690
0
    }
691
692
0
    return nullptr;
693
0
}
694
695
399
uint64_t FieldSchema::get_column_id() const {
696
399
    return column_id;
697
399
}
698
699
0
void FieldSchema::set_column_id(uint64_t id) {
700
0
    column_id = id;
701
0
}
702
703
121
uint64_t FieldSchema::get_max_column_id() const {
704
121
    return max_column_id;
705
121
}
706
707
} // namespace doris