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
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<const NamedReference> reference() const override = 0;
69
72 bool case_sensitive) const override = 0;
73
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>> {
97
98 public:
105 Expression::Operation op, std::shared_ptr<UnboundTerm<B>> term);
106
114 Expression::Operation op, std::shared_ptr<UnboundTerm<B>> term, Literal value);
115
123 Expression::Operation op, std::shared_ptr<UnboundTerm<B>> term,
124 std::vector<Literal> values);
125
126 ~UnboundPredicateImpl() override;
127
128 std::shared_ptr<const NamedReference> reference() const override {
129 return BASE::term()->reference();
130 }
131
132 std::string ToString() const override;
133
134 bool Equals(const Expression& other) const override;
135
137 bool case_sensitive) const override;
138
140
141 const Term& unbound_term() const override { return *BASE::term(); }
142
143 std::span<const Literal> literals() const override { return values_; }
144
145 private:
148 Literal value);
150 std::vector<Literal> values);
151
152 Result<std::shared_ptr<Expression>> BindUnaryOperation(
153 std::shared_ptr<B> bound_term) const;
154 Result<std::shared_ptr<Expression>> BindLiteralOperation(
155 std::shared_ptr<B> bound_term) const;
156 Result<std::shared_ptr<Expression>> BindInOperation(
157 std::shared_ptr<B> bound_term) const;
158
159 private:
160 std::vector<Literal> values_;
161};
162
164class ICEBERG_EXPORT BoundPredicate : public Predicate<BoundTerm>, public Bound {
165 public:
166 ~BoundPredicate() override;
167
168 using Predicate<BoundTerm>::op;
169
170 using Predicate<BoundTerm>::term;
171
172 std::shared_ptr<BoundReference> reference() override { return term_->reference(); }
173
174 Result<Literal> Evaluate(const StructLike& data) const override;
175
176 bool is_bound_predicate() const override { return true; }
177
182 virtual Result<bool> Test(const Literal& value) const = 0;
183
184 enum class Kind : int8_t {
185 // A unary predicate (tests for null, not-null, etc.).
186 kUnary = 0,
187 // A literal predicate (compares against a literal).
188 kLiteral,
189 // A set predicate (tests membership in a set).
190 kSet,
191 };
192
194 virtual Kind kind() const = 0;
195
196 protected:
197 BoundPredicate(Expression::Operation op, std::shared_ptr<BoundTerm> term);
198};
199
201class ICEBERG_EXPORT BoundUnaryPredicate : public BoundPredicate {
202 public:
209 Expression::Operation op, std::shared_ptr<BoundTerm> term);
210
211 ~BoundUnaryPredicate() override;
212
213 Result<bool> Test(const Literal& value) const override;
214
215 Kind kind() const override { return Kind::kUnary; }
216
217 std::string ToString() const override;
218
220
221 bool Equals(const Expression& other) const override;
222
223 private:
224 BoundUnaryPredicate(Expression::Operation op, std::shared_ptr<BoundTerm> term);
225};
226
228class ICEBERG_EXPORT BoundLiteralPredicate : public BoundPredicate {
229 public:
237 Expression::Operation op, std::shared_ptr<BoundTerm> term, Literal literal);
238
239 ~BoundLiteralPredicate() override;
240
242 const Literal& literal() const { return literal_; }
243
244 Result<bool> Test(const Literal& value) const override;
245
246 Kind kind() const override { return Kind::kLiteral; }
247
248 std::string ToString() const override;
249
251
252 bool Equals(const Expression& other) const override;
253
254 private:
255 BoundLiteralPredicate(Expression::Operation op, std::shared_ptr<BoundTerm> term,
256 Literal literal);
257
258 Literal literal_;
259};
260
262class ICEBERG_EXPORT BoundSetPredicate : public BoundPredicate {
263 public:
264 using LiteralSet = std::unordered_set<Literal, LiteralHash>;
265
273 Expression::Operation op, std::shared_ptr<BoundTerm> term,
274 std::span<const Literal> literals);
275
283 std::shared_ptr<BoundTerm> term,
284 LiteralSet value_set);
285
286 ~BoundSetPredicate() override;
287
289 const LiteralSet& literal_set() const { return value_set_; }
290
291 Result<bool> Test(const Literal& value) const override;
292
293 Kind kind() const override { return Kind::kSet; }
294
295 std::string ToString() const override;
296
298
299 bool Equals(const Expression& other) const override;
300
301 private:
302 BoundSetPredicate(Expression::Operation op, std::shared_ptr<BoundTerm> term,
303 std::span<const Literal> literals);
304
305 BoundSetPredicate(Expression::Operation op, std::shared_ptr<BoundTerm> term,
306 LiteralSet value_set);
307
308 LiteralSet value_set_;
309};
310
311} // namespace iceberg
Bound literal predicate (comparison against a single value).
Definition predicate.h:228
static Result< std::unique_ptr< BoundLiteralPredicate > > Make(Expression::Operation op, std::shared_ptr< BoundTerm > term, Literal literal)
Create a bound literal predicate.
Kind kind() const override
Returns the kind of this bound predicate.
Definition predicate.h:246
const Literal & literal() const
Returns the literal being compared against.
Definition predicate.h:242
Result< bool > Test(const Literal &value) const override
Test a value against this predicate.
Result< std::shared_ptr< Expression > > Negate() const override
Returns the negation of this expression, equivalent to not(this).
bool Equals(const Expression &other) const override
Returns whether this expression will accept the same values as another.
std::string ToString() const override
Get a user-readable string representation.
Bound predicates contain bound terms and can be evaluated.
Definition predicate.h:164
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:172
Result< Literal > Evaluate(const StructLike &data) const override
Evaluate this expression against a row-based data.
Bound set predicate (membership testing against a set of values).
Definition predicate.h:262
std::string ToString() const override
Get a user-readable string representation.
bool Equals(const Expression &other) const override
Returns whether this expression will accept the same values as another.
Kind kind() const override
Returns the kind of this bound predicate.
Definition predicate.h:293
static Result< std::unique_ptr< BoundSetPredicate > > Make(Expression::Operation op, std::shared_ptr< BoundTerm > term, std::span< const Literal > literals)
Create a bound set predicate.
const LiteralSet & literal_set() const
Returns the set of literals to test against.
Definition predicate.h:289
Result< std::shared_ptr< Expression > > Negate() const override
Returns the negation of this expression, equivalent to not(this).
static Result< std::unique_ptr< BoundSetPredicate > > Make(Expression::Operation op, std::shared_ptr< BoundTerm > term, LiteralSet value_set)
Create a bound set predicate using a set of literals.
Result< bool > Test(const Literal &value) const override
Test a value against this predicate.
Base class for bound terms.
Definition term.h:64
Bound unary predicate (null, not-null, etc.).
Definition predicate.h:201
std::string ToString() const override
Get a user-readable string representation.
Result< bool > Test(const Literal &value) const override
Test a value against this predicate.
bool Equals(const Expression &other) const override
Returns whether this expression will accept the same values as another.
Kind kind() const override
Returns the kind of this bound predicate.
Definition predicate.h:215
static Result< std::unique_ptr< BoundUnaryPredicate > > Make(Expression::Operation op, std::shared_ptr< BoundTerm > term)
Create a bound unary predicate.
Result< std::shared_ptr< Expression > > Negate() const override
Returns the negation of this expression, equivalent to not(this).
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:42
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
Predicate(Expression::Operation op, std::shared_ptr< T > term)
Create a predicate with an operation and term.
A schema for a Table.
Definition schema.h:51
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
static Result< std::unique_ptr< UnboundPredicateImpl< B > > > Make(Expression::Operation op, std::shared_ptr< UnboundTerm< B > > term)
Create an unbound predicate (unary operation).
Result< std::shared_ptr< Expression > > Bind(const Schema &schema, bool case_sensitive) const override
Bind this UnboundPredicate.
const Term & unbound_term() const override
Returns the term of this predicate as a base Term reference.
Definition predicate.h:141
std::string ToString() const override
Get a user-readable string representation.
static Result< std::unique_ptr< UnboundPredicateImpl< B > > > Make(Expression::Operation op, std::shared_ptr< UnboundTerm< B > > term, std::vector< Literal > values)
Create an unbound predicate with multiple values.
std::shared_ptr< const NamedReference > reference() const override
Returns the reference of this UnboundPredicate.
Definition predicate.h:128
Result< std::shared_ptr< Expression > > Negate() const override
Negate this UnboundPredicate.
std::span< const Literal > literals() const override
Returns the literals of this predicate.
Definition predicate.h:143
static Result< std::unique_ptr< UnboundPredicateImpl< B > > > Make(Expression::Operation op, std::shared_ptr< UnboundTerm< B > > term, Literal value)
Create an unbound predicate with a single value.
bool Equals(const Expression &other) const override
Returns whether this expression will accept the same values as another.
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.
std::shared_ptr< const NamedReference > reference() const override=0
Returns the reference of 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.
Base class for unbound terms.
Definition term.h:56
Interface for unbound expressions that need schema binding.
Definition expression.h:339
Define symbol visibility macros for core Iceberg APIs.
Define typed literal values used by expressions.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
std::expected< T, E > Result
Result alias.
Definition result.h:88