iceberg-cpp
Loading...
Searching...
No Matches
name_mapping.h
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
22#include <functional>
23#include <map>
24#include <memory>
25#include <optional>
26#include <span>
27#include <string>
28#include <unordered_map>
29#include <unordered_set>
30#include <vector>
31
32#include "iceberg/iceberg_export.h"
33#include "iceberg/result.h"
34#include "iceberg/schema.h"
35
36namespace iceberg {
37
41struct ICEBERG_EXPORT MappedField {
43 std::unordered_set<std::string> names;
45 std::optional<int32_t> field_id;
48 std::shared_ptr<class MappedFields> nested_mapping;
49
50 friend bool operator==(const MappedField& lhs, const MappedField& rhs);
51};
52
53using MappedFieldConstRef = std::reference_wrapper<const MappedField>;
54
56class ICEBERG_EXPORT MappedFields {
57 public:
61 static std::unique_ptr<MappedFields> Make(std::vector<MappedField> fields);
62
66 std::optional<MappedFieldConstRef> Field(int32_t id) const;
67
71 std::optional<int32_t> Id(std::string_view name) const;
72
74 size_t Size() const;
75
77 std::span<const MappedField> fields() const;
78
79 ICEBERG_EXPORT friend bool operator==(const MappedFields& lhs, const MappedFields& rhs);
80
81 private:
82 explicit MappedFields(std::vector<MappedField> fields);
83
84 const std::unordered_map<std::string_view, int32_t>& LazyNameToId() const;
85 const std::unordered_map<int32_t, MappedFieldConstRef>& LazyIdToField() const;
86
87 private:
88 std::vector<MappedField> fields_;
89
90 // Lazy-initialized mappings
91 mutable std::unordered_map<std::string_view, int32_t> name_to_id_;
92 mutable std::unordered_map<int32_t, MappedFieldConstRef> id_to_field_;
93};
94
96class ICEBERG_EXPORT NameMapping {
97 public:
99 static std::unique_ptr<NameMapping> Make(std::unique_ptr<MappedFields> fields);
100
102 static std::unique_ptr<NameMapping> Make(std::vector<MappedField> fields);
103
105 static std::unique_ptr<NameMapping> MakeEmpty();
106
108 std::optional<MappedFieldConstRef> Find(int32_t id) const;
109
111 std::optional<MappedFieldConstRef> Find(std::span<const std::string> names) const;
112
114 std::optional<MappedFieldConstRef> Find(const std::string& name) const;
115
117 const MappedFields& AsMappedFields() const;
118
119 ICEBERG_EXPORT friend bool operator==(const NameMapping& lhs, const NameMapping& rhs);
120
121 private:
122 explicit NameMapping(std::unique_ptr<MappedFields> mapping);
123
124 const std::unordered_map<int32_t, MappedFieldConstRef>& LazyFieldsById() const;
125 const std::unordered_map<std::string, MappedFieldConstRef>& LazyFieldsByName() const;
126
127 private:
128 std::unique_ptr<MappedFields> mapping_;
129
130 // Lazy-initialized mappings
131 mutable std::unordered_map<int32_t, MappedFieldConstRef> fields_by_id_;
132 mutable std::unordered_map<std::string, MappedFieldConstRef> fields_by_name_;
133};
134
135ICEBERG_EXPORT std::string ToString(const MappedField& field);
136ICEBERG_EXPORT std::string ToString(const MappedFields& fields);
137ICEBERG_EXPORT std::string ToString(const NameMapping& mapping);
138
145ICEBERG_EXPORT Result<std::unique_ptr<NameMapping>> CreateMapping(const Schema& schema);
146
153ICEBERG_EXPORT Result<std::unique_ptr<NameMapping>> UpdateMapping(
154 const NameMapping& mapping,
155 const std::unordered_map<int32_t, std::shared_ptr<SchemaField>>& updates,
156 const std::multimap<int32_t, int32_t>& adds);
157
158} // namespace iceberg
A list of field mappings for child field of structs, maps, and lists.
Definition name_mapping.h:56
Represents a mapping from external schema names to Iceberg type IDs.
Definition name_mapping.h:96
A schema for a Table.
Definition schema.h:49
An immutable mapping between a field ID and a set of names.
Definition name_mapping.h:41
std::shared_ptr< class MappedFields > nested_mapping
An optional list of field mappings for child field of structs, maps, and lists.
Definition name_mapping.h:48
std::optional< int32_t > field_id
An optional Iceberg field ID used when a field's name is present in names.
Definition name_mapping.h:45
std::unordered_set< std::string > names
A required list of 0 or more names for a field.
Definition name_mapping.h:43