iceberg-cpp
Loading...
Searching...
No Matches
term.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 <concepts>
26#include <memory>
27#include <string>
28#include <string_view>
29
31#include "iceberg/expression/literal.h"
32#include "iceberg/type_fwd.h"
34
35namespace iceberg {
36
38class ICEBERG_EXPORT Term : public util::Formattable {
39 public:
40 enum class Kind : uint8_t { kReference, kTransform, kExtract };
41
43 virtual Kind kind() const = 0;
44
46 virtual bool is_unbound() const = 0;
47};
48
49template <typename T>
50concept TermType = std::derived_from<T, Term>;
51
55template <typename B>
56class ICEBERG_EXPORT UnboundTerm : public Unbound<B>, public Term {
57 public:
58 using BoundType = B;
59
60 bool is_unbound() const override { return true; }
61};
62
64class ICEBERG_EXPORT BoundTerm : public Bound, public Term {
65 public:
66 ~BoundTerm() override;
67
69 virtual std::shared_ptr<Type> type() const = 0;
70
72 virtual bool MayProduceNull() const = 0;
73
80 virtual bool Equals(const BoundTerm& other) const = 0;
81
82 friend bool operator==(const BoundTerm& lhs, const BoundTerm& rhs) {
83 return lhs.Equals(rhs);
84 }
85
86 bool is_unbound() const override { return false; }
87};
88
90class ICEBERG_EXPORT Reference {
91 public:
92 virtual ~Reference();
93
95 virtual std::string_view name() const = 0;
96};
97
99class ICEBERG_EXPORT NamedReference
100 : public Reference,
101 public UnboundTerm<BoundReference>,
102 public std::enable_shared_from_this<NamedReference> {
103 public:
107 static Result<std::unique_ptr<NamedReference>> Make(std::string field_name);
108
109 ~NamedReference() override;
110
111 std::string_view name() const override { return field_name_; }
112
113 Result<std::shared_ptr<BoundReference>> Bind(const Schema& schema,
114 bool case_sensitive) const override;
115
116 std::shared_ptr<const NamedReference> reference() const override {
117 return shared_from_this();
118 }
119
120 std::string ToString() const override;
121
122 Kind kind() const override { return Kind::kReference; }
123
124 private:
125 explicit NamedReference(std::string field_name);
126
127 std::string field_name_;
128};
129
131class ICEBERG_EXPORT BoundReference
132 : public Reference,
133 public BoundTerm,
134 public std::enable_shared_from_this<BoundReference> {
135 public:
139 static Result<std::unique_ptr<BoundReference>> Make(
140 SchemaField field, std::unique_ptr<StructLikeAccessor> accessor);
141
142 ~BoundReference() override;
143
144 const SchemaField& field() const { return field_; }
145
146 std::string_view name() const override { return field_.name(); }
147
148 std::string ToString() const override;
149
150 Result<Literal> Evaluate(const StructLike& data) const override;
151
152 std::shared_ptr<BoundReference> reference() override { return shared_from_this(); }
153
154 std::shared_ptr<Type> type() const override { return field_.type(); }
155
156 int32_t field_id() const { return field_.field_id(); }
157
158 bool MayProduceNull() const override { return field_.optional(); }
159
160 bool Equals(const BoundTerm& other) const override;
161
162 Kind kind() const override { return Kind::kReference; }
163
164 const StructLikeAccessor& accessor() const { return *accessor_; }
165
166 private:
167 BoundReference(SchemaField field, std::unique_ptr<StructLikeAccessor> accessor);
168
169 SchemaField field_;
170 std::unique_ptr<StructLikeAccessor> accessor_;
171};
172
174class ICEBERG_EXPORT UnboundTransform : public UnboundTerm<class BoundTransform> {
175 public:
180 static Result<std::unique_ptr<UnboundTransform>> Make(
181 std::shared_ptr<NamedReference> ref, std::shared_ptr<Transform> transform);
182
183 ~UnboundTransform() override;
184
185 std::string ToString() const override;
186
187 Result<std::shared_ptr<BoundTransform>> Bind(const Schema& schema,
188 bool case_sensitive) const override;
189
190 std::shared_ptr<const NamedReference> reference() const override { return ref_; }
191
192 const std::shared_ptr<Transform>& transform() const { return transform_; }
193
194 Kind kind() const override { return Kind::kTransform; }
195
196 private:
197 UnboundTransform(std::shared_ptr<NamedReference> ref,
198 std::shared_ptr<Transform> transform);
199
200 std::shared_ptr<NamedReference> ref_;
201 std::shared_ptr<Transform> transform_;
202};
203
205class ICEBERG_EXPORT BoundTransform : public BoundTerm {
206 public:
212 static Result<std::unique_ptr<BoundTransform>> Make(
213 std::shared_ptr<BoundReference> ref, std::shared_ptr<Transform> transform,
214 std::shared_ptr<TransformFunction> transform_func);
215
216 ~BoundTransform() override;
217
218 std::string ToString() const override;
219
220 Result<Literal> Evaluate(const StructLike& data) const override;
221
222 std::shared_ptr<BoundReference> reference() override { return ref_; }
223
224 std::shared_ptr<Type> type() const override;
225
226 bool MayProduceNull() const override;
227
228 bool Equals(const BoundTerm& other) const override;
229
230 const std::shared_ptr<Transform>& transform() const { return transform_; }
231
232 Kind kind() const override { return Kind::kTransform; }
233
234 private:
235 BoundTransform(std::shared_ptr<BoundReference> ref,
236 std::shared_ptr<Transform> transform,
237 std::shared_ptr<TransformFunction> transform_func);
238
239 std::shared_ptr<BoundReference> ref_;
240 std::shared_ptr<Transform> transform_;
241 std::shared_ptr<TransformFunction> transform_func_;
242};
243
244} // namespace iceberg
A reference to a bound field.
Definition term.h:134
std::string_view name() const override
Returns the name of the referenced field.
Definition term.h:146
bool MayProduceNull() const override
Returns whether this term may produce null values.
Definition term.h:158
std::shared_ptr< BoundReference > reference() override
Returns the underlying bound reference for this term.
Definition term.h:152
Kind kind() const override
Returns the kind of this term.
Definition term.h:162
std::shared_ptr< Type > type() const override
Returns the type produced by this term.
Definition term.h:154
Base class for bound terms.
Definition term.h:64
virtual std::shared_ptr< Type > type() const =0
Returns the type produced by this term.
bool is_unbound() const override
Returns whether this term is unbound.
Definition term.h:86
virtual bool Equals(const BoundTerm &other) const =0
Returns whether this term is equivalent to another.
virtual bool MayProduceNull() const =0
Returns whether this term may produce null values.
A bound transform expression.
Definition term.h:205
Kind kind() const override
Returns the kind of this term.
Definition term.h:232
std::shared_ptr< BoundReference > reference() override
Returns the underlying bound reference for this term.
Definition term.h:222
Interface for bound expressions that can be evaluated.
Definition expression.h:360
A reference to an unbound named field.
Definition term.h:102
std::string_view name() const override
Returns the name of the referenced field.
Definition term.h:111
std::shared_ptr< const NamedReference > reference() const override
Returns the underlying named reference for this unbound term.
Definition term.h:116
Kind kind() const override
Returns the kind of this term.
Definition term.h:122
A reference represents a named field in an expression.
Definition term.h:90
virtual std::string_view name() const =0
Returns the name of the referenced field.
A type combined with a name.
Definition schema_field.h:39
A schema for a Table.
Definition schema.h:51
An accessor for a struct-like object.
Definition struct_like.h:105
An immutable struct-like wrapper.
Definition struct_like.h:62
A term is an expression node that produces a typed value when evaluated.
Definition term.h:38
virtual bool is_unbound() const =0
Returns whether this term is unbound.
virtual Kind kind() const =0
Returns the kind of this term.
Base class for unbound terms.
Definition term.h:56
bool is_unbound() const override
Returns whether this term is unbound.
Definition term.h:60
An unbound transform expression.
Definition term.h:174
std::shared_ptr< const NamedReference > reference() const override
Returns the underlying named reference for this unbound term.
Definition term.h:190
Kind kind() const override
Returns the kind of this term.
Definition term.h:194
Interface for unbound expressions that need schema binding.
Definition expression.h:339
Interface for objects that can be formatted via std::format.
Definition formattable.h:36
Definition term.h:50