iceberg-cpp
Loading...
Searching...
No Matches
predicate.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 <unordered_set>
26
28#include "iceberg/expression/literal.h"
30#include "iceberg/iceberg_export.h"
31
32namespace iceberg {
33
37template <TermType T>
38class ICEBERG_EXPORT Predicate : public virtual Expression {
39 public:
40 ~Predicate() override;
41
42 Expression::Operation op() const override { return operation_; }
43
45 const std::shared_ptr<T>& term() const { return term_; }
46
47 protected:
52 Predicate(Expression::Operation op, std::shared_ptr<T> term);
53
54 Expression::Operation operation_;
55 std::shared_ptr<T> term_;
56};
57
62class ICEBERG_EXPORT UnboundPredicate : public virtual Expression,
63 public Unbound<Expression> {
64 public:
65 ~UnboundPredicate() override = default;
66
68 std::shared_ptr<NamedReference> reference() override = 0;
69
71 Result<std::shared_ptr<Expression>> Bind(const Schema& schema,
72 bool case_sensitive) const override = 0;
73
75 Result<std::shared_ptr<Expression>> Negate() const override = 0;
76
77 bool is_unbound_predicate() const override { return true; }
78
80 virtual const Term& unbound_term() const = 0;
81
83 virtual std::span<const Literal> literals() const = 0;
84
85 protected:
86 UnboundPredicate() = default;
87};
88
93template <typename B>
94class ICEBERG_EXPORT UnboundPredicateImpl : public UnboundPredicate,
95 public Predicate<UnboundTerm<B>> {
96 using BASE = Predicate<UnboundTerm<B>>;
97
98 public:
104 static Result<std::unique_ptr<UnboundPredicateImpl<B>>> Make(
105 Expression::Operation op, std::shared_ptr<UnboundTerm<B>> term);
106
113 static Result<std::unique_ptr<UnboundPredicateImpl<B>>> Make(
114 Expression::Operation op, std::shared_ptr<UnboundTerm<B>> term, Literal value);
115
122 static Result<std::unique_ptr<UnboundPredicateImpl<B>>> Make(
123 Expression::Operation op, std::shared_ptr<UnboundTerm<B>> term,
124 std::vector<Literal> values);
125
126 ~UnboundPredicateImpl() override;
127
128 std::shared_ptr<NamedReference> reference() override {
129 return BASE::term()->reference();
130 }
131
132 std::string ToString() const override;
133
134 Result<std::shared_ptr<Expression>> Bind(const Schema& schema,
135 bool case_sensitive) const override;
136
137 Result<std::shared_ptr<Expression>> Negate() const override;
138
139 const Term& unbound_term() const override { return *BASE::term(); }
140
141 std::span<const Literal> literals() const override { return values_; }
142
143 private:
144 UnboundPredicateImpl(Expression::Operation op, std::shared_ptr<UnboundTerm<B>> term);
145 UnboundPredicateImpl(Expression::Operation op, std::shared_ptr<UnboundTerm<B>> term,
146 Literal value);
147 UnboundPredicateImpl(Expression::Operation op, std::shared_ptr<UnboundTerm<B>> term,
148 std::vector<Literal> values);
149
150 Result<std::shared_ptr<Expression>> BindUnaryOperation(
151 std::shared_ptr<B> bound_term) const;
152 Result<std::shared_ptr<Expression>> BindLiteralOperation(
153 std::shared_ptr<B> bound_term) const;
154 Result<std::shared_ptr<Expression>> BindInOperation(
155 std::shared_ptr<B> bound_term) const;
156
157 private:
158 std::vector<Literal> values_;
159};
160
162class ICEBERG_EXPORT BoundPredicate : public Predicate<BoundTerm>, public Bound {
163 public:
164 ~BoundPredicate() override;
165
166 using Predicate<BoundTerm>::op;
167
168 using Predicate<BoundTerm>::term;
169
170 std::shared_ptr<BoundReference> reference() override { return term_->reference(); }
171
172 Result<Literal> Evaluate(const StructLike& data) const override;
173
174 bool is_bound_predicate() const override { return true; }
175
180 virtual Result<bool> Test(const Literal& value) const = 0;
181
182 enum class Kind : int8_t {
183 // A unary predicate (tests for null, not-null, etc.).
184 kUnary = 0,
185 // A literal predicate (compares against a literal).
186 kLiteral,
187 // A set predicate (tests membership in a set).
188 kSet,
189 };
190
192 virtual Kind kind() const = 0;
193
194 protected:
195 BoundPredicate(Expression::Operation op, std::shared_ptr<BoundTerm> term);
196};
197
199class ICEBERG_EXPORT BoundUnaryPredicate : public BoundPredicate {
200 public:
206 static Result<std::unique_ptr<BoundUnaryPredicate>> Make(
207 Expression::Operation op, std::shared_ptr<BoundTerm> term);
208
209 ~BoundUnaryPredicate() override;
210
211 Result<bool> Test(const Literal& value) const override;
212
213 Kind kind() const override { return Kind::kUnary; }
214
215 std::string ToString() const override;
216
217 Result<std::shared_ptr<Expression>> Negate() const override;
218
219 bool Equals(const Expression& other) const override;
220
221 private:
222 BoundUnaryPredicate(Expression::Operation op, std::shared_ptr<BoundTerm> term);
223};
224
226class ICEBERG_EXPORT BoundLiteralPredicate : public BoundPredicate {
227 public:
234 static Result<std::unique_ptr<BoundLiteralPredicate>> Make(
235 Expression::Operation op, std::shared_ptr<BoundTerm> term, Literal literal);
236
237 ~BoundLiteralPredicate() override;
238
240 const Literal& literal() const { return literal_; }
241
242 Result<bool> Test(const Literal& value) const override;
243
244 Kind kind() const override { return Kind::kLiteral; }
245
246 std::string ToString() const override;
247
248 Result<std::shared_ptr<Expression>> Negate() const override;
249
250 bool Equals(const Expression& other) const override;
251
252 private:
253 BoundLiteralPredicate(Expression::Operation op, std::shared_ptr<BoundTerm> term,
254 Literal literal);
255
256 Literal literal_;
257};
258
260class ICEBERG_EXPORT BoundSetPredicate : public BoundPredicate {
261 public:
262 using LiteralSet = std::unordered_set<Literal, LiteralHash>;
263
270 static Result<std::unique_ptr<BoundSetPredicate>> Make(
271 Expression::Operation op, std::shared_ptr<BoundTerm> term,
272 std::span<const Literal> literals);
273
280 static Result<std::unique_ptr<BoundSetPredicate>> Make(Expression::Operation op,
281 std::shared_ptr<BoundTerm> term,
282 LiteralSet value_set);
283
284 ~BoundSetPredicate() override;
285
287 const LiteralSet& literal_set() const { return value_set_; }
288
289 Result<bool> Test(const Literal& value) const override;
290
291 Kind kind() const override { return Kind::kSet; }
292
293 std::string ToString() const override;
294
295 Result<std::shared_ptr<Expression>> Negate() const override;
296
297 bool Equals(const Expression& other) const override;
298
299 private:
300 BoundSetPredicate(Expression::Operation op, std::shared_ptr<BoundTerm> term,
301 std::span<const Literal> literals);
302
303 BoundSetPredicate(Expression::Operation op, std::shared_ptr<BoundTerm> term,
304 LiteralSet value_set);
305
306 LiteralSet value_set_;
307};
308
309} // namespace iceberg
Bound literal predicate (comparison against a single value).
Definition predicate.h:226
Kind kind() const override
Returns the kind of this bound predicate.
Definition predicate.h:244
const Literal & literal() const
Returns the literal being compared against.
Definition predicate.h:240
Bound predicates contain bound terms and can be evaluated.
Definition predicate.h:162
virtual Kind kind() const =0
Returns the kind of this bound predicate.
virtual Result< bool > Test(const Literal &value) const =0
Test a value against this predicate.
std::shared_ptr< BoundReference > reference() override
Returns the underlying bound reference for this term.
Definition predicate.h:170
Bound set predicate (membership testing against a set of values).
Definition predicate.h:260
Kind kind() const override
Returns the kind of this bound predicate.
Definition predicate.h:291
const LiteralSet & literal_set() const
Returns the set of literals to test against.
Definition predicate.h:287
Base class for bound terms.
Definition term.h:64
Bound unary predicate (null, not-null, etc.).
Definition predicate.h:199
Kind kind() const override
Returns the kind of this bound predicate.
Definition predicate.h:213
Interface for bound expressions that can be evaluated.
Definition expression.h:360
Represents a boolean expression tree.
Definition expression.h:37
Operation
Operation types for expressions.
Definition expression.h:40
Literal is a literal value that is associated with a primitive type.
Definition literal.h:39
A predicate is a boolean expression that tests a term against some criteria.
Definition predicate.h:38
Expression::Operation op() const override
Returns the operation for an expression node.
Definition predicate.h:42
const std::shared_ptr< T > & term() const
Returns the term this predicate tests.
Definition predicate.h:45
A schema for a Table.
Definition schema.h:49
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
Unbound predicates contain unbound terms and must be bound to a concrete schema before they can be ev...
Definition predicate.h:95
const Term & unbound_term() const override
Returns the term of this predicate as a base Term reference.
Definition predicate.h:139
std::span< const Literal > literals() const override
Returns the literals of this predicate.
Definition predicate.h:141
std::shared_ptr< NamedReference > reference() override
Returns the reference of this UnboundPredicate.
Definition predicate.h:128
Non-template base class for all UnboundPredicate instances.
Definition predicate.h:63
Result< std::shared_ptr< Expression > > Bind(const Schema &schema, bool case_sensitive) const override=0
Bind this UnboundPredicate.
Result< std::shared_ptr< Expression > > Negate() const override=0
Negate this UnboundPredicate.
virtual std::span< const Literal > literals() const =0
Returns the literals of this predicate.
virtual const Term & unbound_term() const =0
Returns the term of this predicate as a base Term reference.
std::shared_ptr< NamedReference > reference() override=0
Returns the reference of this UnboundPredicate.
Interface for unbound expressions that need schema binding.
Definition expression.h:339