iceberg-cpp
Loading...
Searching...
No Matches
schema.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
25
26#include <cstdint>
27#include <functional>
28#include <optional>
29#include <string>
30#include <unordered_map>
31#include <unordered_set>
32#include <vector>
33
34#include "iceberg/iceberg_export.h"
35#include "iceberg/result.h"
37#include "iceberg/type.h"
38#include "iceberg/util/lazy.h"
39#include "iceberg/util/string_util.h"
40
41namespace iceberg {
42
43class SchemaCache;
44struct SchemaReassignIdContext;
45
51class ICEBERG_EXPORT Schema : public StructType {
52 public:
53 static constexpr int32_t kInitialSchemaId = 0;
54 static constexpr int32_t kInitialColumnId = 0;
55 static constexpr int32_t kInvalidColumnId = -1;
56
58 static constexpr std::string_view kAllColumns = "*";
59
63 using GetId = std::function<int32_t(int32_t)>;
64 using IdMap = std::unordered_map<int32_t, int32_t>;
65
66 explicit Schema(std::vector<SchemaField> fields, int32_t schema_id = kInitialSchemaId,
67 GetId get_id = {});
68
69 ~Schema() override;
70
78 static Result<std::unique_ptr<Schema>> Make(std::vector<SchemaField> fields,
79 int32_t schema_id,
80 std::vector<int32_t> identifier_field_ids,
81 GetId get_id = {});
82
91 static Result<std::unique_ptr<Schema>> Make(
92 std::vector<SchemaField> fields, int32_t schema_id,
93 const std::vector<std::string>& identifier_field_names, GetId get_id = {});
94
110 static Status ValidateIdentifierFields(
111 int32_t field_id, const Schema& schema,
112 const std::unordered_map<int32_t, int32_t>& id_to_parent);
113
117 static const std::shared_ptr<Schema>& EmptySchema();
118
123 int32_t schema_id() const;
124
125 std::string ToString() const override;
126
136 Result<std::optional<std::reference_wrapper<const SchemaField>>> FindFieldByName(
137 std::string_view name, bool case_sensitive = true) const;
138
143 Result<std::optional<std::reference_wrapper<const SchemaField>>> FindFieldById(
144 int32_t field_id) const;
145
151 Result<std::optional<std::string_view>> FindColumnNameById(int32_t field_id) const;
152
157 Result<std::unique_ptr<StructLikeAccessor>> GetAccessorById(int32_t field_id) const;
158
166 Result<std::unique_ptr<Schema>> Select(std::span<const std::string> names,
167 bool case_sensitive = true) const;
168
175 Result<std::unique_ptr<Schema>> Project(
176 const std::unordered_set<int32_t>& field_ids) const;
177
179 const std::vector<int32_t>& IdentifierFieldIds() const;
180
182 const IdMap& IdsToReassigned() const;
183
185 const IdMap& IdsToOriginal() const;
186
188 Result<std::vector<std::string>> IdentifierFieldNames() const;
189
192 Result<int32_t> HighestFieldId() const;
193
196 bool SameSchema(const Schema& other) const;
197
205 Status Validate(int32_t format_version) const;
206
207 friend bool operator==(const Schema& lhs, const Schema& rhs) { return lhs.Equals(rhs); }
208
209 private:
210 using StructType::Equals;
212 bool Equals(const Schema& other) const;
213
214 const int32_t schema_id_;
215 // Field IDs that uniquely identify rows in the table.
216 std::vector<int32_t> identifier_field_ids_;
217 std::unique_ptr<SchemaReassignIdContext> reassign_id_context_;
218 // Cache for schema mappings to facilitate fast lookups.
219 std::unique_ptr<SchemaCache> cache_;
220};
221
222// Cache for schema mappings to facilitate fast lookups.
223class ICEBERG_EXPORT SchemaCache {
224 public:
225 explicit SchemaCache(const Schema* schema) : schema_(schema) {}
226
227 using IdToFieldMap =
228 std::unordered_map<int32_t, std::reference_wrapper<const SchemaField>>;
229 using IdToFieldMapRef = std::reference_wrapper<const IdToFieldMap>;
230
231 struct NameIdMap {
237 std::unordered_map<std::string, int32_t, StringHash, std::equal_to<>> name_to_id;
238
243 std::unordered_map<int32_t, std::string> id_to_name;
244 };
245 using NameIdMapRef = std::reference_wrapper<const NameIdMap>;
246
247 using LowercaseNameToIdMap =
248 std::unordered_map<std::string, int32_t, StringHash, std::equal_to<>>;
249 using LowercaseNameToIdMapRef = std::reference_wrapper<const LowercaseNameToIdMap>;
250
251 using IdToPositionPathMap = std::unordered_map<int32_t, std::vector<size_t>>;
252 using IdToPositionPathMapRef = std::reference_wrapper<const IdToPositionPathMap>;
253
254 Result<IdToFieldMapRef> GetIdToFieldMap() const;
255 Result<NameIdMapRef> GetNameIdMap() const;
256 Result<LowercaseNameToIdMapRef> GetLowercaseNameToIdMap() const;
257 Result<IdToPositionPathMapRef> GetIdToPositionPathMap() const;
258 Result<int32_t> GetHighestFieldId() const;
259
260 private:
261 static Result<IdToFieldMap> InitIdToFieldMap(const Schema* schema);
262 static Result<NameIdMap> InitNameIdMap(const Schema* schema);
263 static Result<LowercaseNameToIdMap> InitLowerCaseNameToIdMap(const Schema* schema);
264 static Result<IdToPositionPathMap> InitIdToPositionPath(const Schema* schema);
265 static Result<int32_t> InitHighestFieldId(const Schema* schema);
266
267 const Schema* schema_;
268 // Mapping from field id to field.
269 Lazy<InitIdToFieldMap> id_to_field_;
270 // Mapping from field name to field id.
271 Lazy<InitNameIdMap> name_id_map_;
272 // Mapping from lowercased field name to field id.
273 Lazy<InitLowerCaseNameToIdMap> lowercase_name_to_id_;
274 // Mapping from field id to (nested) position path to access the field.
275 Lazy<InitIdToPositionPath> id_to_position_path_;
276 // Highest field ID in the schema.
277 Lazy<InitHighestFieldId> highest_field_id_;
278};
279
280} // namespace iceberg
Definition schema.h:223
A schema for a Table.
Definition schema.h:51
std::function< int32_t(int32_t)> GetId
Maps an original field ID to its reassigned ID.
Definition schema.h:63
A data type representing a struct with nested fields.
Definition type.h:119
Definition schema.h:231
std::unordered_map< int32_t, std::string > id_to_name
Mapping from field ID to canonical name.
Definition schema.h:243
std::unordered_map< std::string, int32_t, StringHash, std::equal_to<> > name_to_id
Mapping from canonical field name to ID.
Definition schema.h:237