iceberg-cpp
Loading...
Searching...
No Matches
name_mapping.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
24
25#include <functional>
26#include <map>
27#include <memory>
28#include <optional>
29#include <span>
30#include <string>
31#include <unordered_map>
32#include <unordered_set>
33#include <vector>
34
36#include "iceberg/result.h"
37#include "iceberg/schema.h"
38
39namespace iceberg {
40
44struct ICEBERG_EXPORT MappedField {
46 std::unordered_set<std::string> names;
48 std::optional<int32_t> field_id;
51 std::shared_ptr<class MappedFields> nested_mapping;
52
53 friend bool operator==(const MappedField& lhs, const MappedField& rhs);
54};
55
56using MappedFieldConstRef = std::reference_wrapper<const MappedField>;
57
59class ICEBERG_EXPORT MappedFields {
60 public:
64 static std::unique_ptr<MappedFields> Make(std::vector<MappedField> fields);
65
69 std::optional<MappedFieldConstRef> Field(int32_t id) const;
70
74 std::optional<int32_t> Id(std::string_view name) const;
75
77 size_t Size() const;
78
80 std::span<const MappedField> fields() const;
81
82 ICEBERG_EXPORT friend bool operator==(const MappedFields& lhs, const MappedFields& rhs);
83
84 private:
85 explicit MappedFields(std::vector<MappedField> fields);
86
87 const std::unordered_map<std::string_view, int32_t>& LazyNameToId() const;
88 const std::unordered_map<int32_t, MappedFieldConstRef>& LazyIdToField() const;
89
90 private:
91 std::vector<MappedField> fields_;
92
93 // Lazy-initialized mappings
94 mutable std::unordered_map<std::string_view, int32_t> name_to_id_;
95 mutable std::unordered_map<int32_t, MappedFieldConstRef> id_to_field_;
96};
97
99class ICEBERG_EXPORT NameMapping {
100 public:
102 static std::unique_ptr<NameMapping> Make(std::unique_ptr<MappedFields> fields);
103
105 static std::unique_ptr<NameMapping> Make(std::vector<MappedField> fields);
106
108 static std::unique_ptr<NameMapping> MakeEmpty();
109
111 std::optional<MappedFieldConstRef> Find(int32_t id) const;
112
114 std::optional<MappedFieldConstRef> Find(std::span<const std::string> names) const;
115
117 std::optional<MappedFieldConstRef> Find(const std::string& name) const;
118
121
122 ICEBERG_EXPORT friend bool operator==(const NameMapping& lhs, const NameMapping& rhs);
123
124 private:
125 explicit NameMapping(std::unique_ptr<MappedFields> mapping);
126
127 const std::unordered_map<int32_t, MappedFieldConstRef>& LazyFieldsById() const;
128 const std::unordered_map<std::string, MappedFieldConstRef>& LazyFieldsByName() const;
129
130 private:
131 std::unique_ptr<MappedFields> mapping_;
132
133 // Lazy-initialized mappings
134 mutable std::unordered_map<int32_t, MappedFieldConstRef> fields_by_id_;
135 mutable std::unordered_map<std::string, MappedFieldConstRef> fields_by_name_;
136};
137
138ICEBERG_EXPORT std::string ToString(const MappedField& field);
139ICEBERG_EXPORT std::string ToString(const MappedFields& fields);
140ICEBERG_EXPORT std::string ToString(const NameMapping& mapping);
141
149
157 const NameMapping& mapping,
158 const std::unordered_map<int32_t, std::shared_ptr<SchemaField>>& updates,
159 const std::multimap<int32_t, int32_t>& adds);
160
161} // namespace iceberg
A list of field mappings for child field of structs, maps, and lists.
Definition name_mapping.h:59
std::optional< MappedFieldConstRef > Field(int32_t id) const
Get the field for a given field ID.
static std::unique_ptr< MappedFields > Make(std::vector< MappedField > fields)
Create a new MappedFields instance.
std::optional< int32_t > Id(std::string_view name) const
Get the field ID for a given field name.
std::span< const MappedField > fields() const
Get the list of field mappings.
size_t Size() const
Get the number of field mappings.
Represents a mapping from external schema names to Iceberg type IDs.
Definition name_mapping.h:99
static std::unique_ptr< NameMapping > Make(std::vector< MappedField > fields)
Create a new NameMapping instance.
const MappedFields & AsMappedFields() const
Get the underlying MappedFields instance.
std::optional< MappedFieldConstRef > Find(std::span< const std::string > names) const
Find a field by its unconcatenated names.
static std::unique_ptr< NameMapping > Make(std::unique_ptr< MappedFields > fields)
Create a new NameMapping instance.
static std::unique_ptr< NameMapping > MakeEmpty()
Create an empty NameMapping instance.
std::optional< MappedFieldConstRef > Find(int32_t id) const
Find a field by its ID.
std::optional< MappedFieldConstRef > Find(const std::string &name) const
Find a field by its (concatenated) name.
A schema for a Table.
Definition schema.h:51
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
ICEBERG_EXPORT Result< std::unique_ptr< NameMapping > > UpdateMapping(const NameMapping &mapping, const std::unordered_map< int32_t, std::shared_ptr< SchemaField > > &updates, const std::multimap< int32_t, int32_t > &adds)
Update a name-based mapping using changes to a schema.
ICEBERG_EXPORT Result< std::unique_ptr< NameMapping > > CreateMapping(const Schema &schema)
Create a name-based mapping for a schema.
ICEBERG_EXPORT std::string_view ToString(Expression::Operation op)
Returns a string representation of an expression operation.
std::expected< T, E > Result
Result alias.
Definition result.h:88
Define Result, Status, and error helpers.
An immutable mapping between a field ID and a set of names.
Definition name_mapping.h:44
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:51
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:48
std::unordered_set< std::string > names
A required list of 0 or more names for a field.
Definition name_mapping.h:46