Apache GraphAr C++ Library
The C++ Library for Apache GraphAr
expression.h
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 
22 #include <memory>
23 #include <string>
24 
25 #include "arrow/compute/api.h"
26 
27 #include "graphar/graph_info.h"
28 
29 namespace graphar {
30 
31 using ArrowExpression = arrow::compute::Expression;
32 
37 class Expression {
38  public:
39  Expression() = default;
40  Expression(const Expression& other) = default;
41  virtual ~Expression() = default;
42 
52  virtual Result<ArrowExpression> Evaluate() = 0;
53 };
54 
60  public:
61  explicit ExpressionProperty(const Property& property) : property_(property) {}
62  explicit ExpressionProperty(const std::string& name)
63  : property_(Property(name)) {}
64  ExpressionProperty(const ExpressionProperty& other) = default;
65  ~ExpressionProperty() = default;
66 
67  Result<ArrowExpression> Evaluate() override;
68 
69  private:
70  Property property_;
71 };
72 
77 template <typename T,
78  bool IsScalar =
79  std::is_same_v<T, bool> || std::is_same_v<T, int32_t> ||
80  std::is_same_v<T, int64_t> || std::is_same_v<T, float> ||
81  std::is_same_v<T, double> || std::is_same_v<T, std::string> ||
82  std::is_same_v<T, const char*> ||
83  std::is_same_v<T, const char* const>,
84  typename = std::enable_if_t<IsScalar>>
85 class ExpressionLiteral : public Expression {
86  public:
87  explicit ExpressionLiteral(T value) : value_(value) {}
88  ExpressionLiteral(const ExpressionLiteral& other) = default;
89  ~ExpressionLiteral() = default;
90 
91  Result<ArrowExpression> Evaluate() { return arrow::compute::literal(value_); }
92 
93  private:
94  T value_;
95 };
96 
102  public:
103  ExpressionUnaryOp() = default;
104  explicit ExpressionUnaryOp(std::shared_ptr<Expression> expr) : expr_(expr) {}
105  ExpressionUnaryOp(const ExpressionUnaryOp& other) = default;
106  virtual ~ExpressionUnaryOp() {}
107 
108  protected:
109  std::shared_ptr<Expression> expr_;
110 };
111 
117  public:
118  ExpressionNot() = default;
119  explicit ExpressionNot(std::shared_ptr<Expression> expr)
120  : ExpressionUnaryOp(expr) {}
121  ExpressionNot(const ExpressionNot& other) = default;
122  ~ExpressionNot() = default;
123 
124  Result<ArrowExpression> Evaluate() override;
125 };
126 
132  public:
133  ExpressionBinaryOp() = default;
134  ExpressionBinaryOp(std::shared_ptr<Expression> lhs,
135  std::shared_ptr<Expression> rhs)
136  : lhs_(lhs), rhs_(rhs) {}
137  ExpressionBinaryOp(const ExpressionBinaryOp& other) = default;
138  ~ExpressionBinaryOp() = default;
139 
140  protected:
141  inline Status CheckNullArgs(std::shared_ptr<Expression> lhs,
142  std::shared_ptr<Expression> rhs) noexcept {
143  if (lhs == nullptr || rhs == nullptr) {
144  return Status::Invalid("Invalid expression: lhs or rhs is null");
145  }
146  return Status::OK();
147  }
148 
149  protected:
150  std::shared_ptr<Expression> lhs_;
151  std::shared_ptr<Expression> rhs_;
152 };
153 
160  public:
161  ExpressionEqual() = default;
162  ExpressionEqual(std::shared_ptr<Expression> lhs,
163  std::shared_ptr<Expression> rhs)
164  : ExpressionBinaryOp(lhs, rhs) {}
165  ExpressionEqual(const ExpressionEqual& other) = default;
166  ~ExpressionEqual() = default;
167 
168  Result<ArrowExpression> Evaluate() override;
169 };
170 
177  public:
178  ExpressionNotEqual() = default;
179  ExpressionNotEqual(std::shared_ptr<Expression> lhs,
180  std::shared_ptr<Expression> rhs)
181  : ExpressionBinaryOp(lhs, rhs) {}
182  ExpressionNotEqual(const ExpressionNotEqual& other) = default;
183  ~ExpressionNotEqual() = default;
184 
185  Result<ArrowExpression> Evaluate() override;
186 };
187 
194  public:
195  ExpressionGreaterThan() = default;
196  ExpressionGreaterThan(std::shared_ptr<Expression> lhs,
197  std::shared_ptr<Expression> rhs)
198  : ExpressionBinaryOp(lhs, rhs) {}
199  ExpressionGreaterThan(const ExpressionGreaterThan& other) = default;
200  ~ExpressionGreaterThan() = default;
201 
202  Result<ArrowExpression> Evaluate() override;
203 };
204 
211  public:
212  ExpressionGreaterEqual() = default;
213  ExpressionGreaterEqual(std::shared_ptr<Expression> lhs,
214  std::shared_ptr<Expression> rhs)
215  : ExpressionBinaryOp(lhs, rhs) {}
216  ExpressionGreaterEqual(const ExpressionGreaterEqual& other) = default;
217  ~ExpressionGreaterEqual() = default;
218 
219  Result<ArrowExpression> Evaluate() override;
220 };
221 
228  public:
229  ExpressionLessThan() = default;
230  ExpressionLessThan(std::shared_ptr<Expression> lhs,
231  std::shared_ptr<Expression> rhs)
232  : ExpressionBinaryOp(lhs, rhs) {}
233  ExpressionLessThan(const ExpressionLessThan& other) = default;
234  ~ExpressionLessThan() = default;
235 
236  Result<ArrowExpression> Evaluate() override;
237 };
238 
245  public:
246  ExpressionLessEqual() = default;
247  ExpressionLessEqual(std::shared_ptr<Expression> lhs,
248  std::shared_ptr<Expression> rhs)
249  : ExpressionBinaryOp(lhs, rhs) {}
250  ExpressionLessEqual(const ExpressionLessEqual& other) = default;
251  ~ExpressionLessEqual() = default;
252 
253  Result<ArrowExpression> Evaluate() override;
254 };
255 
262  public:
263  ExpressionAnd() = default;
264  ExpressionAnd(std::shared_ptr<Expression> lhs,
265  std::shared_ptr<Expression> rhs)
266  : ExpressionBinaryOp(lhs, rhs) {}
267  ExpressionAnd(const ExpressionAnd& other) = default;
268  ~ExpressionAnd() = default;
269 
270  Result<ArrowExpression> Evaluate() override;
271 };
272 
279  public:
280  ExpressionOr() = default;
281  ExpressionOr(std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs)
282  : ExpressionBinaryOp(lhs, rhs) {}
283  ExpressionOr(const ExpressionOr& other) = default;
284  ~ExpressionOr() = default;
285 
286  Result<ArrowExpression> Evaluate() override;
287 };
288 
292 [[nodiscard]] static inline std::shared_ptr<Expression> _Property(
293  const Property& property) {
294  return std::make_shared<ExpressionProperty>(property);
295 }
296 
297 [[nodiscard]] static inline std::shared_ptr<Expression> _Property(
298  const std::string& name) {
299  return std::make_shared<ExpressionProperty>(name);
300 }
301 
302 template <typename T,
303  bool IsScalar =
304  std::is_same_v<T, bool> || std::is_same_v<T, int32_t> ||
305  std::is_same_v<T, int64_t> || std::is_same_v<T, float> ||
306  std::is_same_v<T, double> || std::is_same_v<T, std::string> ||
307  std::is_same_v<T, const char*> ||
308  std::is_same_v<T, const char* const>,
309  typename = std::enable_if_t<IsScalar>>
310 [[nodiscard]] static inline std::shared_ptr<Expression> _Literal(T value) {
311  return std::make_shared<ExpressionLiteral<T>>(value);
312 }
313 
314 [[nodiscard]] static inline std::shared_ptr<Expression> _Not(
315  std::shared_ptr<Expression> expr) {
316  return std::make_shared<ExpressionNot>(expr);
317 }
318 
319 [[nodiscard]] static inline std::shared_ptr<Expression> _Equal(
320  std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
321  return std::make_shared<ExpressionEqual>(lhs, rhs);
322 }
323 
324 [[nodiscard]] static inline std::shared_ptr<Expression> _NotEqual(
325  std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
326  return std::make_shared<ExpressionNotEqual>(lhs, rhs);
327 }
328 
329 [[nodiscard]] static inline std::shared_ptr<Expression> _GreaterThan(
330  std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
331  return std::make_shared<ExpressionGreaterThan>(lhs, rhs);
332 }
333 
334 [[nodiscard]] static inline std::shared_ptr<Expression> _GreaterEqual(
335  std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
336  return std::make_shared<ExpressionGreaterEqual>(lhs, rhs);
337 }
338 
339 [[nodiscard]] static inline std::shared_ptr<Expression> _LessThan(
340  std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
341  return std::make_shared<ExpressionLessThan>(lhs, rhs);
342 }
343 
344 [[nodiscard]] static inline std::shared_ptr<Expression> _LessEqual(
345  std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
346  return std::make_shared<ExpressionLessEqual>(lhs, rhs);
347 }
348 
349 [[nodiscard]] static inline std::shared_ptr<Expression> _And(
350  std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
351  return std::make_shared<ExpressionAnd>(lhs, rhs);
352 }
353 
354 [[nodiscard]] static inline std::shared_ptr<Expression> _Or(
355  std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs) {
356  return std::make_shared<ExpressionOr>(lhs, rhs);
357 }
358 } // namespace graphar
Result< ArrowExpression > Evaluate() override
Evaluate Expression as arrow::compute::Expression e.g. new ExpressionEqual(new ExpressionProperty("a"...
Definition: expression.cc:75
Result< ArrowExpression > Evaluate() override
Evaluate Expression as arrow::compute::Expression e.g. new ExpressionEqual(new ExpressionProperty("a"...
Definition: expression.cc:33
Result< ArrowExpression > Evaluate() override
Evaluate Expression as arrow::compute::Expression e.g. new ExpressionEqual(new ExpressionProperty("a"...
Definition: expression.cc:54
Result< ArrowExpression > Evaluate() override
Evaluate Expression as arrow::compute::Expression e.g. new ExpressionEqual(new ExpressionProperty("a"...
Definition: expression.cc:47
virtual Result< ArrowExpression > Evaluate()=0
Evaluate Expression as arrow::compute::Expression e.g. new ExpressionEqual(new ExpressionProperty("a"...
Result< ArrowExpression > Evaluate() override
Evaluate Expression as arrow::compute::Expression e.g. new ExpressionEqual(new ExpressionProperty("a"...
Definition: expression.cc:68
Result< ArrowExpression > Evaluate() override
Evaluate Expression as arrow::compute::Expression e.g. new ExpressionEqual(new ExpressionProperty("a"...
Definition: expression.cc:61
Result< ArrowExpression > Evaluate()
Evaluate Expression as arrow::compute::Expression e.g. new ExpressionEqual(new ExpressionProperty("a"...
Definition: expression.h:91
Result< ArrowExpression > Evaluate() override
Evaluate Expression as arrow::compute::Expression e.g. new ExpressionEqual(new ExpressionProperty("a"...
Definition: expression.cc:40
Result< ArrowExpression > Evaluate() override
Evaluate Expression as arrow::compute::Expression e.g. new ExpressionEqual(new ExpressionProperty("a"...
Definition: expression.cc:28
Result< ArrowExpression > Evaluate() override
Evaluate Expression as arrow::compute::Expression e.g. new ExpressionEqual(new ExpressionProperty("a"...
Definition: expression.cc:82
Result< ArrowExpression > Evaluate() override
Evaluate Expression as arrow::compute::Expression e.g. new ExpressionEqual(new ExpressionProperty("a"...
Definition: expression.cc:25
Status outcome object (success or error)
Definition: status.h:123
static Status Invalid(Args &&... args)
Definition: status.h:188
static Status OK()
Definition: status.h:157