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
35#include "iceberg/result.h"
37#include "iceberg/type.h"
38#include "iceberg/util/lazy.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
92 std::vector<SchemaField> fields, int32_t schema_id,
93 const std::vector<std::string>& identifier_field_names, GetId get_id = {});
94
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
137 std::string_view name, bool case_sensitive = true) const;
138
144 int32_t field_id) const;
145
152
158
166 Result<std::unique_ptr<Schema>> Select(std::span<const std::string> names,
167 bool case_sensitive = true) const;
168
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
189
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 lazy.h:36
Definition schema.h:223
A schema for a Table.
Definition schema.h:51
static Result< std::unique_ptr< Schema > > Make(std::vector< SchemaField > fields, int32_t schema_id, const std::vector< std::string > &identifier_field_names, GetId get_id={})
Create a schema.
std::string ToString() const override
Get a user-readable string representation.
Result< std::unique_ptr< Schema > > Select(std::span< const std::string > names, bool case_sensitive=true) const
Creates a projected schema from selected field names.
static Status ValidateIdentifierFields(int32_t field_id, const Schema &schema, const std::unordered_map< int32_t, int32_t > &id_to_parent)
Validate that the identifier field with the given ID is valid for the schema.
Result< std::unique_ptr< Schema > > Project(const std::unordered_set< int32_t > &field_ids) const
Creates a projected schema from selected field IDs.
std::function< int32_t(int32_t)> GetId
Maps an original field ID to its reassigned ID.
Definition schema.h:63
Result< std::optional< std::string_view > > FindColumnNameById(int32_t field_id) const
Returns the canonical field name for the given id.
Result< std::optional< std::reference_wrapper< const SchemaField > > > FindFieldByName(std::string_view name, bool case_sensitive=true) const
Recursively find the SchemaField by field name.
const IdMap & IdsToReassigned() const
Return a map of original field IDs to reassigned field IDs.
Status Validate(int32_t format_version) const
Validate the schema for a given format version.
bool SameSchema(const Schema &other) const
Checks whether this schema is equivalent to another schema while ignoring the schema id.
int32_t schema_id() const
Get the schema ID.
Result< std::vector< std::string > > IdentifierFieldNames() const
Return the canonical field names of the identifier fields.
const IdMap & IdsToOriginal() const
Return a map of reassigned field IDs to original field IDs.
Result< std::optional< std::reference_wrapper< const SchemaField > > > FindFieldById(int32_t field_id) const
Recursively find the SchemaField by field id.
const std::vector< int32_t > & IdentifierFieldIds() const
Return the field IDs of the identifier fields.
static const std::shared_ptr< Schema > & EmptySchema()
Get an empty schema.
Result< std::unique_ptr< StructLikeAccessor > > GetAccessorById(int32_t field_id) const
Get the accessor to access the field by field id.
Result< int32_t > HighestFieldId() const
Get the highest field ID in the schema.
static Result< std::unique_ptr< Schema > > Make(std::vector< SchemaField > fields, int32_t schema_id, std::vector< int32_t > identifier_field_ids, GetId get_id={})
Create a schema.
A data type representing a struct with nested fields.
Definition type.h:119
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
std::expected< T, E > Result
Result alias.
Definition result.h:88
Define Result, Status, and error helpers.
Provide string utility helpers.
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