-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathexpression_visitor.h
More file actions
334 lines (292 loc) · 13.7 KB
/
expression_visitor.h
File metadata and controls
334 lines (292 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#pragma once
/// \file iceberg/expression/expression_visitor.h
/// Visitor pattern implementation for traversing Iceberg expression trees.
#include <concepts>
#include <memory>
#include <typeinfo>
#include "iceberg/expression/aggregate.h"
#include "iceberg/expression/expression.h"
#include "iceberg/expression/literal.h"
#include "iceberg/expression/predicate.h"
#include "iceberg/expression/term.h"
#include "iceberg/iceberg_export.h"
#include "iceberg/result.h"
#include "iceberg/util/checked_cast.h"
#include "iceberg/util/macros.h"
namespace iceberg {
/// \brief Base visitor for traversing expression trees.
///
/// This visitor traverses expression trees in postorder traversal and calls appropriate
/// visitor methods for each node. Subclasses can override specific methods to implement
/// custom behavior.
///
/// \tparam R The return type produced by visitor methods
template <typename R>
class ICEBERG_EXPORT ExpressionVisitor {
using ParamType = std::conditional_t<std::is_fundamental_v<R>, R, const R&>;
public:
virtual ~ExpressionVisitor() = default;
/// \brief Visit a True expression (always evaluates to true).
virtual Result<R> AlwaysTrue() = 0;
/// \brief Visit a False expression (always evaluates to false).
virtual Result<R> AlwaysFalse() = 0;
/// \brief Visit a Not expression.
/// \param child_result The result from visiting the child expression
virtual Result<R> Not(ParamType child_result) = 0;
/// \brief Visit an And expression.
/// \param left_result The result from visiting the left child
/// \param right_result The result from visiting the right child
virtual Result<R> And(ParamType left_result, ParamType right_result) = 0;
/// \brief Visit an Or expression.
/// \param left_result The result from visiting the left child
/// \param right_result The result from visiting the right child
virtual Result<R> Or(ParamType left_result, ParamType right_result) = 0;
/// \brief Visit a bound predicate.
/// \param pred The bound predicate to visit
virtual Result<R> Predicate(const std::shared_ptr<BoundPredicate>& pred) = 0;
/// \brief Visit an unbound predicate.
/// \param pred The unbound predicate to visit
virtual Result<R> Predicate(const std::shared_ptr<UnboundPredicate>& pred) = 0;
/// \brief Visit a bound aggregate.
/// \param aggregate The bound aggregate to visit.
virtual Result<R> Aggregate(
[[maybe_unused]] const std::shared_ptr<BoundAggregate>& aggregate) {
ICEBERG_DCHECK(aggregate != nullptr, "Bound aggregate cannot be null");
return NotSupported("Visitor {} does not support bound aggregate",
typeid(*this).name());
}
/// \brief Visit an unbound aggregate.
/// \param aggregate The unbound aggregate to visit.
virtual Result<R> Aggregate(
[[maybe_unused]] const std::shared_ptr<UnboundAggregate>& aggregate) {
ICEBERG_DCHECK(aggregate != nullptr, "Unbound aggregate cannot be null");
return NotSupported("Visitor {} does not support unbound aggregate",
typeid(*this).name());
}
};
/// \brief Visitor for bound expressions.
///
/// This visitor is for traversing bound expression trees.
///
/// \tparam R The return type produced by visitor methods
template <typename R>
class ICEBERG_EXPORT BoundVisitor : public ExpressionVisitor<R> {
public:
~BoundVisitor() override = default;
/// \brief Visit an IS_NULL bound expression.
/// \param expr The bound expression being tested
virtual Result<R> IsNull(const std::shared_ptr<Bound>& expr) = 0;
/// \brief Visit a NOT_NULL bound expression.
/// \param expr The bound expression being tested
virtual Result<R> NotNull(const std::shared_ptr<Bound>& expr) = 0;
/// \brief Visit an IS_NAN bound expression.
/// \param expr The bound expression being tested
virtual Result<R> IsNaN([[maybe_unused]] const std::shared_ptr<Bound>& expr) {
return NotSupported("IsNaN operation is not supported by this visitor");
}
/// \brief Visit a NOT_NAN bound expression.
/// \param expr The bound expression being tested
virtual Result<R> NotNaN([[maybe_unused]] const std::shared_ptr<Bound>& expr) {
return NotSupported("NotNaN operation is not supported by this visitor");
}
/// \brief Visit a less-than bound expression.
/// \param expr The bound expression being tested
/// \param lit The literal value to compare against
virtual Result<R> Lt(const std::shared_ptr<Bound>& expr, const Literal& lit) = 0;
/// \brief Visit a less-than-or-equal bound expression.
/// \param expr The bound expression being tested
/// \param lit The literal value to compare against
virtual Result<R> LtEq(const std::shared_ptr<Bound>& expr, const Literal& lit) = 0;
/// \brief Visit a greater-than bound expression.
/// \param expr The bound expression being tested
/// \param lit The literal value to compare against
virtual Result<R> Gt(const std::shared_ptr<Bound>& expr, const Literal& lit) = 0;
/// \brief Visit a greater-than-or-equal bound expression.
/// \param expr The bound expression being tested
/// \param lit The literal value to compare against
virtual Result<R> GtEq(const std::shared_ptr<Bound>& expr, const Literal& lit) = 0;
/// \brief Visit an equality bound expression.
/// \param expr The bound expression being tested
/// \param lit The literal value to compare against
virtual Result<R> Eq(const std::shared_ptr<Bound>& expr, const Literal& lit) = 0;
/// \brief Visit a not-equal bound expression.
/// \param expr The bound expression being tested
/// \param lit The literal value to compare against
virtual Result<R> NotEq(const std::shared_ptr<Bound>& expr, const Literal& lit) = 0;
/// \brief Visit a starts-with bound expression.
/// \param expr The bound expression being tested
/// \param lit The literal value to check for prefix match
virtual Result<R> StartsWith([[maybe_unused]] const std::shared_ptr<Bound>& expr,
[[maybe_unused]] const Literal& lit) {
return NotSupported("StartsWith operation is not supported by this visitor");
}
/// \brief Visit a not-starts-with bound expression.
/// \param expr The bound expression being tested
/// \param lit The literal value to check for prefix match
virtual Result<R> NotStartsWith([[maybe_unused]] const std::shared_ptr<Bound>& expr,
[[maybe_unused]] const Literal& lit) {
return NotSupported("NotStartsWith operation is not supported by this visitor");
}
/// \brief Visit an IN set bound expression.
/// \param expr The bound expression being tested
/// \param literal_set The set of literal values to test membership
virtual Result<R> In(
[[maybe_unused]] const std::shared_ptr<Bound>& expr,
[[maybe_unused]] const BoundSetPredicate::LiteralSet& literal_set) {
return NotSupported("In operation is not supported by this visitor");
}
/// \brief Visit a NOT_IN set bound expression.
/// \param expr The bound expression being tested
/// \param literal_set The set of literal values to test membership
virtual Result<R> NotIn(
[[maybe_unused]] const std::shared_ptr<Bound>& expr,
[[maybe_unused]] const BoundSetPredicate::LiteralSet& literal_set) {
return NotSupported("NotIn operation is not supported by this visitor");
}
/// \brief Visit a bound predicate.
///
/// This method dispatches to specific visitor methods based on the predicate
/// type and operation.
///
/// \param pred The bound predicate to visit
Result<R> Predicate(const std::shared_ptr<BoundPredicate>& pred) override {
ICEBERG_DCHECK(pred != nullptr, "BoundPredicate cannot be null");
switch (pred->kind()) {
case BoundPredicate::Kind::kUnary: {
switch (pred->op()) {
case Expression::Operation::kIsNull:
return IsNull(pred->term());
case Expression::Operation::kNotNull:
return NotNull(pred->term());
case Expression::Operation::kIsNan:
return IsNaN(pred->term());
case Expression::Operation::kNotNan:
return NotNaN(pred->term());
default:
return InvalidExpression("Invalid operation for BoundUnaryPredicate: {}",
ToString(pred->op()));
}
}
case BoundPredicate::Kind::kLiteral: {
const auto& literal_pred =
internal::checked_cast<const BoundLiteralPredicate&>(*pred);
switch (pred->op()) {
case Expression::Operation::kLt:
return Lt(pred->term(), literal_pred.literal());
case Expression::Operation::kLtEq:
return LtEq(pred->term(), literal_pred.literal());
case Expression::Operation::kGt:
return Gt(pred->term(), literal_pred.literal());
case Expression::Operation::kGtEq:
return GtEq(pred->term(), literal_pred.literal());
case Expression::Operation::kEq:
return Eq(pred->term(), literal_pred.literal());
case Expression::Operation::kNotEq:
return NotEq(pred->term(), literal_pred.literal());
case Expression::Operation::kStartsWith:
return StartsWith(pred->term(), literal_pred.literal());
case Expression::Operation::kNotStartsWith:
return NotStartsWith(pred->term(), literal_pred.literal());
default:
return InvalidExpression("Invalid operation for BoundLiteralPredicate: {}",
ToString(pred->op()));
}
}
case BoundPredicate::Kind::kSet: {
const auto& set_pred = internal::checked_cast<const BoundSetPredicate&>(*pred);
switch (pred->op()) {
case Expression::Operation::kIn:
return In(pred->term(), set_pred.literal_set());
case Expression::Operation::kNotIn:
return NotIn(pred->term(), set_pred.literal_set());
default:
return InvalidExpression("Invalid operation for BoundSetPredicate: {}",
ToString(pred->op()));
}
}
}
return InvalidExpression("Unsupported bound predicate: {}", pred->ToString());
}
/// \brief Visit an unbound predicate.
///
/// \param pred The unbound predicate
Result<R> Predicate(const std::shared_ptr<UnboundPredicate>& pred) override {
ICEBERG_DCHECK(pred != nullptr, "UnboundPredicate cannot be null");
return NotSupported("Not a bound predicate: {}", pred->ToString());
}
};
/// \brief Traverse an expression tree with a visitor.
///
/// This function traverses the given expression tree in postorder traversal and calls
/// appropriate visitor methods for each node. Results from child nodes are passed to
/// parent nodes.
///
/// \tparam R The return type produced by the visitor
/// \tparam V The visitor type (must derive from ExpressionVisitor<R>)
/// \param expr The expression to traverse
/// \param visitor The visitor to use for traversal
/// \return The result produced by the visitor for the root expression node
template <typename R, typename V>
requires std::derived_from<V, ExpressionVisitor<R>>
Result<R> Visit(const std::shared_ptr<Expression>& expr, V& visitor) {
ICEBERG_DCHECK(expr != nullptr, "Expression cannot be null");
if (expr->is_bound_predicate()) {
return visitor.Predicate(std::dynamic_pointer_cast<BoundPredicate>(expr));
}
if (expr->is_unbound_predicate()) {
return visitor.Predicate(std::dynamic_pointer_cast<UnboundPredicate>(expr));
}
if (expr->is_bound_aggregate()) {
return visitor.Aggregate(std::dynamic_pointer_cast<BoundAggregate>(expr));
}
if (expr->is_unbound_aggregate()) {
return visitor.Aggregate(std::dynamic_pointer_cast<UnboundAggregate>(expr));
}
switch (expr->op()) {
case Expression::Operation::kTrue:
return visitor.AlwaysTrue();
case Expression::Operation::kFalse:
return visitor.AlwaysFalse();
case Expression::Operation::kNot: {
const auto& not_expr = internal::checked_pointer_cast<Not>(expr);
ICEBERG_ASSIGN_OR_RAISE(auto child_result,
(Visit<R, V>(not_expr->child(), visitor)));
return visitor.Not(std::move(child_result));
}
case Expression::Operation::kAnd: {
const auto& and_expr = internal::checked_pointer_cast<And>(expr);
ICEBERG_ASSIGN_OR_RAISE(auto left_result, (Visit<R, V>(and_expr->left(), visitor)));
ICEBERG_ASSIGN_OR_RAISE(auto right_result,
(Visit<R, V>(and_expr->right(), visitor)));
return visitor.And(std::move(left_result), std::move(right_result));
}
case Expression::Operation::kOr: {
const auto& or_expr = internal::checked_pointer_cast<Or>(expr);
ICEBERG_ASSIGN_OR_RAISE(auto left_result, (Visit<R, V>(or_expr->left(), visitor)));
ICEBERG_ASSIGN_OR_RAISE(auto right_result,
(Visit<R, V>(or_expr->right(), visitor)));
return visitor.Or(std::move(left_result), std::move(right_result));
}
default:
return InvalidExpression("Unknown expression operation: {}", expr->ToString());
}
}
} // namespace iceberg