-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathexpression.h
More file actions
360 lines (311 loc) · 11.5 KB
/
expression.h
File metadata and controls
360 lines (311 loc) · 11.5 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* 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
#include <memory>
#include <string>
#include "arrow/compute/api.h"
#include "graphar/graph_info.h"
namespace graphar {
using ArrowExpression = arrow::compute::Expression;
/**
* This class wraps an arrow::compute::Expression and provides a way to
* construct it
*/
class Expression {
public:
Expression() = default;
Expression(const Expression& other) = default;
virtual ~Expression() = default;
/**
* @brief Evaluate Expression as arrow::compute::Expression e.g. new
* ExpressionEqual(new ExpressionProperty("a"), new
* ExpressionLiteral(1)) will be parsed as
* arrow::compute::equal(arrow::compute::field_ref("a"),
* arrow::compute::literal(1))
*
* @return The arrow::compute::Expression instance
*/
virtual Result<ArrowExpression> Evaluate() = 0;
};
/**
* This class wraps the Property and provides a way to construct property
* expression
*/
class ExpressionProperty : public Expression {
public:
explicit ExpressionProperty(const Property& property) : property_(property) {}
explicit ExpressionProperty(const std::string& name)
: property_(Property(name)) {}
ExpressionProperty(const ExpressionProperty& other) = default;
~ExpressionProperty() = default;
Result<ArrowExpression> Evaluate() override;
private:
Property property_;
};
/**
* This class wraps the literal. Only bool, int32, int64, float, double and
* string are allowed.
*/
template <typename T,
bool IsScalar =
std::is_same_v<T, bool> || std::is_same_v<T, int32_t> ||
std::is_same_v<T, int64_t> || std::is_same_v<T, float> ||
std::is_same_v<T, double> || std::is_same_v<T, std::string> ||
std::is_same_v<T, const char*> ||
std::is_same_v<T, const char* const>,
typename = std::enable_if_t<IsScalar>>
class ExpressionLiteral : public Expression {
public:
explicit ExpressionLiteral(T value) : value_(value) {}
ExpressionLiteral(const ExpressionLiteral& other) = default;
~ExpressionLiteral() = default;
Result<ArrowExpression> Evaluate() override {
return arrow::compute::literal(value_);
}
private:
T value_;
};
/**
* This class constructs a unary operator expression that accepts only one
* expression
*/
class ExpressionUnaryOp : public Expression {
public:
ExpressionUnaryOp() = default;
explicit ExpressionUnaryOp(std::shared_ptr<Expression> expr) : expr_(expr) {}
ExpressionUnaryOp(const ExpressionUnaryOp& other) = default;
virtual ~ExpressionUnaryOp() {}
protected:
std::shared_ptr<Expression> expr_;
};
/**
* This class constructs a NOT operator expression. e.g. new ExpressionNot(new
* ExpressionLiteral(true)) => NOT TRUE
*/
class ExpressionNot : public ExpressionUnaryOp {
public:
ExpressionNot() = default;
explicit ExpressionNot(std::shared_ptr<Expression> expr)
: ExpressionUnaryOp(expr) {}
ExpressionNot(const ExpressionNot& other) = default;
~ExpressionNot() = default;
Result<ArrowExpression> Evaluate() override;
};
/**
* This class constructs a binary operator expression that accepts two
* expressions e.g. a = 1, a > 1, a AND b, a OR b
*/
class ExpressionBinaryOp : public Expression {
public:
ExpressionBinaryOp() = default;
ExpressionBinaryOp(std::shared_ptr<Expression> lhs,
std::shared_ptr<Expression> rhs)
: lhs_(lhs), rhs_(rhs) {}
ExpressionBinaryOp(const ExpressionBinaryOp& other) = default;
~ExpressionBinaryOp() = default;
protected:
Status CheckNullArgs(std::shared_ptr<Expression> lhs,
std::shared_ptr<Expression> rhs) noexcept {
if (lhs == nullptr || rhs == nullptr) {
return Status::Invalid("Invalid expression: lhs or rhs is null");
}
return Status::OK();
}
protected:
std::shared_ptr<Expression> lhs_;
std::shared_ptr<Expression> rhs_;
};
/**
* This class constructs a EQUAL operator expression.
* e.g. new ExpressionEqual(new ExpressionProperty("a"), new
* ExpressionLiteral(1)) => a = 1
*/
class ExpressionEqual : public ExpressionBinaryOp {
public:
ExpressionEqual() = default;
ExpressionEqual(std::shared_ptr<Expression> lhs,
std::shared_ptr<Expression> rhs)
: ExpressionBinaryOp(lhs, rhs) {}
ExpressionEqual(const ExpressionEqual& other) = default;
~ExpressionEqual() = default;
Result<ArrowExpression> Evaluate() override;
};
/**
* This class constructs a NOT EQUAL operator expression.
* e.g. new ExpressionNotEqual(new ExpressionProperty("a"), new
* ExpressionLiteral(1)) => a != 1
*/
class ExpressionNotEqual : public ExpressionBinaryOp {
public:
ExpressionNotEqual() = default;
ExpressionNotEqual(std::shared_ptr<Expression> lhs,
std::shared_ptr<Expression> rhs)
: ExpressionBinaryOp(lhs, rhs) {}
ExpressionNotEqual(const ExpressionNotEqual& other) = default;
~ExpressionNotEqual() = default;
Result<ArrowExpression> Evaluate() override;
};
/**
* This class constructs a GREATER THAN operator expression.
* e.g. new ExpressionGreaterThan(new ExpressionProperty("a"), new
* ExpressionLiteral(1)) => a > 1
*/
class ExpressionGreaterThan : public ExpressionBinaryOp {
public:
ExpressionGreaterThan() = default;
ExpressionGreaterThan(std::shared_ptr<Expression> lhs,
std::shared_ptr<Expression> rhs)
: ExpressionBinaryOp(lhs, rhs) {}
ExpressionGreaterThan(const ExpressionGreaterThan& other) = default;
~ExpressionGreaterThan() = default;
Result<ArrowExpression> Evaluate() override;
};
/**
* This class constructs a GREATER EQUAL operator expression.
* e.g. new ExpressionGreaterEqual(new ExpressionProperty("a"), new
* ExpressionLiteral(1)) => a >= 1
*/
class ExpressionGreaterEqual : public ExpressionBinaryOp {
public:
ExpressionGreaterEqual() = default;
ExpressionGreaterEqual(std::shared_ptr<Expression> lhs,
std::shared_ptr<Expression> rhs)
: ExpressionBinaryOp(lhs, rhs) {}
ExpressionGreaterEqual(const ExpressionGreaterEqual& other) = default;
~ExpressionGreaterEqual() = default;
Result<ArrowExpression> Evaluate() override;
};
/**
* This class constructs a LESS THAN operator expression.
* e.g. new ExpressionLessThan(new ExpressionProperty("a"), new
* ExpressionLiteral(1)) => a < 1
*/
class ExpressionLessThan : public ExpressionBinaryOp {
public:
ExpressionLessThan() = default;
ExpressionLessThan(std::shared_ptr<Expression> lhs,
std::shared_ptr<Expression> rhs)
: ExpressionBinaryOp(lhs, rhs) {}
ExpressionLessThan(const ExpressionLessThan& other) = default;
~ExpressionLessThan() = default;
Result<ArrowExpression> Evaluate() override;
};
/**
* This class constructs a LESS EQUAL operator expression.
* e.g. new ExpressionLessEqual(new ExpressionProperty("a"), new
* ExpressionLiteral(1)) => a <= 1
*/
class ExpressionLessEqual : public ExpressionBinaryOp {
public:
ExpressionLessEqual() = default;
ExpressionLessEqual(std::shared_ptr<Expression> lhs,
std::shared_ptr<Expression> rhs)
: ExpressionBinaryOp(lhs, rhs) {}
ExpressionLessEqual(const ExpressionLessEqual& other) = default;
~ExpressionLessEqual() = default;
Result<ArrowExpression> Evaluate() override;
};
/**
* This class constructs a AND operator expression.
* e.g. new ExpressionAnd(new ExpressionLiteral(true), new
* ExpressionLiteral(1)) => TRUE AND 1
*/
class ExpressionAnd : public ExpressionBinaryOp {
public:
ExpressionAnd() = default;
ExpressionAnd(std::shared_ptr<Expression> lhs,
std::shared_ptr<Expression> rhs)
: ExpressionBinaryOp(lhs, rhs) {}
ExpressionAnd(const ExpressionAnd& other) = default;
~ExpressionAnd() = default;
Result<ArrowExpression> Evaluate() override;
};
/**
* This class constructs a OR operator expression.
* e.g. new ExpressionOr(new ExpressionLiteral(0), new
* ExpressionLiteral(true)) => 0 OR TRUE
*/
class ExpressionOr : public ExpressionBinaryOp {
public:
ExpressionOr() = default;
ExpressionOr(std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs)
: ExpressionBinaryOp(lhs, rhs) {}
ExpressionOr(const ExpressionOr& other) = default;
~ExpressionOr() = default;
Result<ArrowExpression> Evaluate() override;
};
/**
* Helper functions to construct a Expression.
*/
[[nodiscard]] static inline std::shared_ptr<Expression> _Property(
const Property& property) {
return std::make_shared<ExpressionProperty>(property);
}
[[nodiscard]] static inline std::shared_ptr<Expression> _Property(
const std::string& name) {
return std::make_shared<ExpressionProperty>(name);
}
template <typename T,
bool IsScalar =
std::is_same_v<T, bool> || std::is_same_v<T, int32_t> ||
std::is_same_v<T, int64_t> || std::is_same_v<T, float> ||
std::is_same_v<T, double> || std::is_same_v<T, std::string> ||
std::is_same_v<T, const char*> ||
std::is_same_v<T, const char* const>,
typename = std::enable_if_t<IsScalar>>
[[nodiscard]] static inline std::shared_ptr<Expression> _Literal(T value) {
return std::make_shared<ExpressionLiteral<T>>(value);
}
[[nodiscard]] static inline std::shared_ptr<Expression> _Not(
std::shared_ptr<Expression> expr) {
return std::make_shared<ExpressionNot>(expr);
}
[[nodiscard]] static inline std::shared_ptr<Expression> _Equal(
std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
return std::make_shared<ExpressionEqual>(lhs, rhs);
}
[[nodiscard]] static inline std::shared_ptr<Expression> _NotEqual(
std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
return std::make_shared<ExpressionNotEqual>(lhs, rhs);
}
[[nodiscard]] static inline std::shared_ptr<Expression> _GreaterThan(
std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
return std::make_shared<ExpressionGreaterThan>(lhs, rhs);
}
[[nodiscard]] static inline std::shared_ptr<Expression> _GreaterEqual(
std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
return std::make_shared<ExpressionGreaterEqual>(lhs, rhs);
}
[[nodiscard]] static inline std::shared_ptr<Expression> _LessThan(
std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
return std::make_shared<ExpressionLessThan>(lhs, rhs);
}
[[nodiscard]] static inline std::shared_ptr<Expression> _LessEqual(
std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
return std::make_shared<ExpressionLessEqual>(lhs, rhs);
}
[[nodiscard]] static inline std::shared_ptr<Expression> _And(
std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
return std::make_shared<ExpressionAnd>(lhs, rhs);
}
[[nodiscard]] static inline std::shared_ptr<Expression> _Or(
std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
return std::make_shared<ExpressionOr>(lhs, rhs);
}
} // namespace graphar