-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathexpression.cpp
More file actions
159 lines (131 loc) · 4.81 KB
/
Copy pathexpression.cpp
File metadata and controls
159 lines (131 loc) · 4.81 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
#include "vortex/common.hpp"
#include "vortex/error.hpp"
#include "vortex/expression.hpp"
#include <vortex.h>
#include <span>
#include <string>
#include <string_view>
#include <vector>
namespace vortex {
using detail::Access;
using detail::throw_on_error;
using detail::to_view;
void Expression::Deleter::operator()(const vx_expression *ptr) const noexcept {
vx_expression_free(ptr);
}
Expression::Expression(const Expression &other) : handle_(vx_expression_clone(other.handle_.get())) {
}
Expression &Expression::operator=(const Expression &other) {
if (this != &other) {
handle_.reset(vx_expression_clone(other.handle_.get()));
}
return *this;
}
Expression Expression::operator[](std::string_view field) const {
vx_expression *out = vx_expression_get_item(to_view(field), handle_.get());
if (out == nullptr) {
throw VortexException("get_item: field name is not valid UTF-8", ErrorCode::InvalidArgument);
}
return Access::adopt<Expression>(out);
}
Expression Expression::is_null() const {
return expr::is_null(*this);
}
template <class T>
static Expression select_impl(std::span<T> names, const vx_expression *expr) {
std::vector<vx_view> raw;
raw.reserve(names.size());
for (const auto &name : names) {
raw.push_back(to_view(name));
}
return Access::adopt<Expression>(vx_expression_select(raw.data(), raw.size(), expr));
}
Expression Expression::select(std::span<const std::string> names) const {
return select_impl(names, handle_.get());
}
Expression Expression::select(std::span<const std::string_view> names) const {
return select_impl(names, handle_.get());
}
Expression Expression::select(std::initializer_list<std::string_view> names) const {
std::span span {names.begin(), names.end()};
return select_impl(span, handle_.get());
}
namespace expr {
Expression root() {
return Access::adopt<Expression>(vx_expression_root());
}
Expression col(std::string_view name) {
return root()[name];
}
Expression lit(const Scalar &value) {
vx_error *error = nullptr;
vx_expression *out = vx_expression_literal(Access::c_ptr(value), &error);
throw_on_error(error);
return Access::adopt<Expression>(out);
}
Expression binary_op(BinaryOperator op, const Expression &l, const Expression &r) {
return Access::adopt<Expression>(
vx_expression_binary(static_cast<vx_binary_operator>(op), Access::c_ptr(l), Access::c_ptr(r)));
}
Expression eq(const Expression &l, const Expression &r) {
return binary_op(BinaryOperator::Eq, l, r);
}
Expression neq(const Expression &l, const Expression &r) {
return binary_op(BinaryOperator::NotEq, l, r);
}
Expression lt(const Expression &l, const Expression &r) {
return binary_op(BinaryOperator::Lt, l, r);
}
Expression lte(const Expression &l, const Expression &r) {
return binary_op(BinaryOperator::Lte, l, r);
}
Expression gt(const Expression &l, const Expression &r) {
return binary_op(BinaryOperator::Gt, l, r);
}
Expression gte(const Expression &l, const Expression &r) {
return binary_op(BinaryOperator::Gte, l, r);
}
Expression add(const Expression &l, const Expression &r) {
return binary_op(BinaryOperator::Add, l, r);
}
Expression sub(const Expression &l, const Expression &r) {
return binary_op(BinaryOperator::Sub, l, r);
}
Expression mul(const Expression &l, const Expression &r) {
return binary_op(BinaryOperator::Mul, l, r);
}
Expression div(const Expression &l, const Expression &r) {
return binary_op(BinaryOperator::Div, l, r);
}
static Expression combine(vx_expression *(*combiner)(const vx_expression *const *, size_t),
std::span<const Expression> children) {
std::vector<const vx_expression *> raw;
raw.reserve(children.size());
for (const auto &child : children) {
raw.push_back(Access::c_ptr(child));
}
vx_expression *out = combiner(raw.data(), raw.size());
if (out == nullptr) {
throw VortexException("empty expression list", ErrorCode::InvalidArgument);
}
return Access::adopt<Expression>(out);
}
Expression and_all(std::span<const Expression> children) {
return combine(vx_expression_and, children);
}
Expression or_all(std::span<const Expression> children) {
return combine(vx_expression_or, children);
}
Expression logical_not(const Expression &child) {
return Access::adopt<Expression>(vx_expression_not(Access::c_ptr(child)));
}
Expression is_null(const Expression &child) {
return Access::adopt<Expression>(vx_expression_is_null(Access::c_ptr(child)));
}
Expression list_contains(const Expression &list, const Expression &value) {
return Access::adopt<Expression>(vx_expression_list_contains(Access::c_ptr(list), Access::c_ptr(value)));
}
} // namespace expr
} // namespace vortex