iceberg-cpp
Loading...
Searching...
No Matches
type_util.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
22#include <functional>
23#include <memory>
24#include <string>
25#include <string_view>
26#include <unordered_map>
27#include <unordered_set>
28#include <vector>
29
30#include "iceberg/iceberg_export.h"
31#include "iceberg/result.h"
33#include "iceberg/type_fwd.h"
34#include "iceberg/util/string_util.h"
35
38
39namespace iceberg {
40
43 public:
44 explicit IdToFieldVisitor(
45 std::unordered_map<int32_t, std::reference_wrapper<const SchemaField>>&
46 id_to_field);
47 Status Visit(const PrimitiveType& type);
48 Status Visit(const VariantType& type);
49 Status Visit(const NestedType& type);
50
51 private:
52 std::unordered_map<int32_t, std::reference_wrapper<const SchemaField>>& id_to_field_;
53};
54
58 public:
59 explicit NameToIdVisitor(
60 std::unordered_map<std::string, int32_t, StringHash, std::equal_to<>>& name_to_id,
61 std::unordered_map<int32_t, std::string>* id_to_name, bool case_sensitive = true,
62 std::function<std::string(std::string_view)> quoting_func = {});
63 Status Visit(const ListType& type, const std::string& path,
64 const std::string& short_path);
65 Status Visit(const MapType& type, const std::string& path,
66 const std::string& short_path);
67 Status Visit(const StructType& type, const std::string& path,
68 const std::string& short_path);
69 Status Visit(const PrimitiveType& type, const std::string& path,
70 const std::string& short_path);
71 Status Visit(const VariantType& type, const std::string& path,
72 const std::string& short_path);
73 void Finish();
74
75 private:
76 std::string BuildPath(std::string_view prefix, std::string_view field_name,
77 bool case_sensitive);
78
79 private:
80 bool case_sensitive_;
81 std::unordered_map<std::string, int32_t, StringHash, std::equal_to<>>& name_to_id_;
82 std::unordered_map<int32_t, std::string>* id_to_name_;
83 std::unordered_map<std::string, int32_t, StringHash, std::equal_to<>> short_name_to_id_;
84 std::function<std::string(std::string_view)> quoting_func_;
85};
86
89 public:
90 Status Visit(const PrimitiveType& type);
91 Status Visit(const VariantType& type);
92 Status Visit(const StructType& type);
93 Status Visit(const ListType& type);
94 Status Visit(const MapType& type);
95 std::unordered_map<int32_t, std::vector<size_t>> Finish();
96
97 private:
98 constexpr static int32_t kUnassignedFieldId = -1;
99 int32_t current_field_id_ = kUnassignedFieldId;
100 std::vector<size_t> current_path_;
101 std::unordered_map<int32_t, std::vector<size_t>> position_path_;
102};
103
113 public:
114 PruneColumnVisitor(const std::unordered_set<int32_t>& selected_ids,
115 bool select_full_types);
116
117 Result<std::shared_ptr<Type>> Visit(const std::shared_ptr<Type>& type) const;
118 Result<std::shared_ptr<Type>> Visit(const SchemaField& field) const;
119 static SchemaField MakeField(const SchemaField& field, std::shared_ptr<Type> type);
120 Result<std::shared_ptr<Type>> Visit(const std::shared_ptr<StructType>& type) const;
121 Result<std::shared_ptr<Type>> Visit(const std::shared_ptr<ListType>& type) const;
122 Result<std::shared_ptr<Type>> Visit(const std::shared_ptr<MapType>& type) const;
123
124 private:
125 const std::unordered_set<int32_t>& selected_ids_;
126 const bool select_full_types_;
127};
128
131 public:
132 explicit GetProjectedIdsVisitor(bool include_struct_ids = false);
133
134 Status Visit(const Type& type);
135 Status VisitNested(const NestedType& type);
136 Status VisitPrimitive(const PrimitiveType& type);
137 std::unordered_set<int32_t> Finish() const;
138
139 static Result<std::unordered_set<int32_t>> GetProjectedIds(
140 const Type& type, bool include_struct_ids = false);
141
142 private:
143 const bool include_struct_ids_;
144 std::unordered_set<int32_t> ids_;
145};
146
155ICEBERG_EXPORT std::unordered_map<int32_t, int32_t> IndexParents(
156 const StructType& root_struct);
157
160 public:
161 explicit AssignFreshIdVisitor(std::function<int32_t()> next_id);
162
163 std::shared_ptr<Type> Visit(const std::shared_ptr<Type>& type) const;
164 std::shared_ptr<StructType> Visit(const StructType& type) const;
165 std::shared_ptr<ListType> Visit(const ListType& type) const;
166 std::shared_ptr<MapType> Visit(const MapType& type) const;
167
168 private:
169 std::function<int32_t()> next_id_;
170};
171
178ICEBERG_EXPORT Result<std::shared_ptr<Schema>> AssignFreshIds(
179 int32_t schema_id, const Schema& schema, std::function<int32_t()> next_id);
180
192ICEBERG_EXPORT bool IsPromotionAllowed(const std::shared_ptr<Type>& from_type,
193 const std::shared_ptr<Type>& to_type);
194
195} // namespace iceberg
Assigns fresh IDs to all fields in the schema.
Definition type_util.h:159
Visitor for getting projected field IDs.
Definition type_util.h:130
Visitor for building a map from field ID to SchemaField reference.
Definition type_util.h:42
A data type representing a list of values.
Definition type.h:156
A data type representing a dictionary of values.
Definition type.h:191
Visitor for building maps from field name to field ID and field ID to field name.
Definition type_util.h:57
A data type that has child fields.
Definition type.h:85
Visitor for building a map from field ID to position path.
Definition type_util.h:88
A data type that does not have child fields.
Definition type.h:78
Visitor for pruning columns based on selected field IDs.
Definition type_util.h:112
A type combined with a name.
Definition schema_field.h:39
A schema for a Table.
Definition schema.h:51
A data type representing a struct with nested fields.
Definition type.h:119
Interface for a data type for a field.
Definition type.h:44
A semi-structured type whose structure may vary across rows.
Definition type.h:233
Result< R > Visit(const std::shared_ptr< Expression > &expr, V &visitor)
Traverse an expression tree with a visitor.
Definition expression_visitor.h:283
Transparent hash function that supports std::string_view as lookup key.
Definition string_util.h:154