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<NamedReference> reference() override { return shared_from_this(); }
117
118 std::string ToString() const override;
119
120 Kind kind() const override { return Kind::kReference; }
121
122 private:
123 explicit NamedReference(std::string field_name);
124
125 std::string field_name_;
126};
127
129class ICEBERG_EXPORT BoundReference
130 : public Reference,
131 public BoundTerm,
132 public std::enable_shared_from_this<BoundReference> {
133 public:
137 static Result<std::unique_ptr<BoundReference>> Make(
138 SchemaField field, std::unique_ptr<StructLikeAccessor> accessor);
139
140 ~BoundReference() override;
141
142 const SchemaField& field() const { return field_; }
143
144 std::string_view name() const override { return field_.name(); }
145
146 std::string ToString() const override;
147
148 Result<Literal> Evaluate(const StructLike& data) const override;
149
150 std::shared_ptr<BoundReference> reference() override { return shared_from_this(); }
151
152 std::shared_ptr<Type> type() const override { return field_.type(); }
153
154 int32_t field_id() const { return field_.field_id(); }
155
156 bool MayProduceNull() const override { return field_.optional(); }
157
158 bool Equals(const BoundTerm& other) const override;
159
160 Kind kind() const override { return Kind::kReference; }
161
162 const StructLikeAccessor& accessor() const { return *accessor_; }
163
164 private:
165 BoundReference(SchemaField field, std::unique_ptr<StructLikeAccessor> accessor);
166
167 SchemaField field_;
168 std::unique_ptr<StructLikeAccessor> accessor_;
169};
170
172class ICEBERG_EXPORT UnboundTransform : public UnboundTerm<class BoundTransform> {
173 public:
178 static Result<std::unique_ptr<UnboundTransform>> Make(
179 std::shared_ptr<NamedReference> ref, std::shared_ptr<Transform> transform);
180
181 ~UnboundTransform() override;
182
183 std::string ToString() const override;
184
185 Result<std::shared_ptr<BoundTransform>> Bind(const Schema& schema,
186 bool case_sensitive) const override;
187
188 std::shared_ptr<NamedReference> reference() override { return ref_; }
189
190 const std::shared_ptr<Transform>& transform() const { return transform_; }
191
192 Kind kind() const override { return Kind::kTransform; }
193
194 private:
195 UnboundTransform(std::shared_ptr<NamedReference> ref,
196 std::shared_ptr<Transform> transform);
197
198 std::shared_ptr<NamedReference> ref_;
199 std::shared_ptr<Transform> transform_;
200};
201
203class ICEBERG_EXPORT BoundTransform : public BoundTerm {
204 public:
210 static Result<std::unique_ptr<BoundTransform>> Make(
211 std::shared_ptr<BoundReference> ref, std::shared_ptr<Transform> transform,
212 std::shared_ptr<TransformFunction> transform_func);
213
214 ~BoundTransform() override;
215
216 std::string ToString() const override;
217
218 Result<Literal> Evaluate(const StructLike& data) const override;
219
220 std::shared_ptr<BoundReference> reference() override { return ref_; }
221
222 std::shared_ptr<Type> type() const override;
223
224 bool MayProduceNull() const override;
225
226 bool Equals(const BoundTerm& other) const override;
227
228 const std::shared_ptr<Transform>& transform() const { return transform_; }
229
230 Kind kind() const override { return Kind::kTransform; }
231
232 private:
233 BoundTransform(std::shared_ptr<BoundReference> ref,
234 std::shared_ptr<Transform> transform,
235 std::shared_ptr<TransformFunction> transform_func);
236
237 std::shared_ptr<BoundReference> ref_;
238 std::shared_ptr<Transform> transform_;
239 std::shared_ptr<TransformFunction> transform_func_;
240};
241
242} // namespace iceberg
A reference to a bound field.
Definition term.h:132
std::string_view name() const override
Returns the name of the referenced field.
Definition term.h:144
bool MayProduceNull() const override
Returns whether this term may produce null values.
Definition term.h:156
std::shared_ptr< BoundReference > reference() override
Returns the underlying bound reference for this term.
Definition term.h:150
Kind kind() const override
Returns the kind of this term.
Definition term.h:160
std::shared_ptr< Type > type() const override
Returns the type produced by this term.
Definition term.h:152
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:203
Kind kind() const override
Returns the kind of this term.
Definition term.h:230
std::shared_ptr< BoundReference > reference() override
Returns the underlying bound reference for this term.
Definition term.h:220
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< NamedReference > reference() 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:120
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:49
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:172
Kind kind() const override
Returns the kind of this term.
Definition term.h:192
std::shared_ptr< NamedReference > reference() override
Returns the underlying named reference for this unbound term.
Definition term.h:188
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