be/src/format/table/iceberg/schema.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/iceberg/schema.h" |
19 | | |
20 | | #include <functional> |
21 | | |
22 | | namespace doris::iceberg { |
23 | | |
24 | | const std::string Schema::ALL_COLUMNS = "*"; |
25 | | const int Schema::DEFAULT_SCHEMA_ID = 0; |
26 | | |
27 | | Schema::Schema(int schema_id, std::vector<NestedField> columns) |
28 | 36 | : _schema_id(schema_id), _root_struct(std::move(columns)) { |
29 | 36 | FieldPath path; |
30 | 86 | std::function<void(const NestedField&)> index_field = [&](const NestedField& field) { |
31 | 86 | path.push_back(&field); |
32 | 86 | _id_to_field[field.field_id()] = &field; |
33 | 86 | _id_to_field_path[field.field_id()] = path; |
34 | 86 | Type* type = field.field_type(); |
35 | 86 | if (type->is_struct_type()) { |
36 | 8 | for (const auto& child : type->as_struct_type()->fields()) { |
37 | 8 | index_field(child); |
38 | 8 | } |
39 | 80 | } else if (type->is_list_type()) { |
40 | 5 | index_field(type->as_list_type()->element_field()); |
41 | 75 | } else if (type->is_map_type()) { |
42 | 4 | index_field(type->as_map_type()->key_field()); |
43 | 4 | index_field(type->as_map_type()->value_field()); |
44 | 4 | } |
45 | 86 | path.pop_back(); |
46 | 86 | }; |
47 | 65 | for (const auto& field : _root_struct.fields()) { |
48 | 65 | index_field(field); |
49 | 65 | } |
50 | 36 | } |
51 | 30 | Schema::Schema(std::vector<NestedField> columns) : Schema(DEFAULT_SCHEMA_ID, std::move(columns)) {} |
52 | | |
53 | 3 | Type* Schema::find_type(int id) const { |
54 | 3 | auto it = _id_to_field.find(id); |
55 | 3 | if (it != _id_to_field.end()) { |
56 | 3 | return it->second->field_type(); |
57 | 3 | } |
58 | 0 | return nullptr; |
59 | 3 | } |
60 | | |
61 | 6 | const NestedField* Schema::find_field(int id) const { |
62 | 6 | auto it = _id_to_field.find(id); |
63 | 6 | if (it != _id_to_field.end()) { |
64 | 6 | return it->second; |
65 | 6 | } |
66 | 0 | return nullptr; |
67 | 6 | } |
68 | | |
69 | 3 | const Schema::FieldPath* Schema::find_field_path(int id) const { |
70 | 3 | auto it = _id_to_field_path.find(id); |
71 | 3 | return it == _id_to_field_path.end() ? nullptr : &it->second; |
72 | 3 | } |
73 | | |
74 | | } // namespace doris::iceberg |