-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy patharray.cpp
More file actions
344 lines (290 loc) · 10.8 KB
/
Copy patharray.cpp
File metadata and controls
344 lines (290 loc) · 10.8 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
#include "vortex/array.hpp"
#include "vortex/common.hpp"
#include "vortex/dtype.hpp"
#include "vortex/error.hpp"
#include <vortex.h>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
namespace vortex {
using detail::Access;
using detail::throw_on_error;
Validity::Validity(const Validity &other)
: type_(other.type_), array_(other.array_ != nullptr ? vx_array_clone(other.array_) : nullptr) {
}
Validity::Validity(ValidityType type) : type_(type), array_(nullptr) {
if (type == ValidityType::FromArray) {
throw VortexException("Validity(ValidityType) called with ValidityType::FromArray",
ErrorCode::InvalidArgument);
}
}
Validity Validity::from_array(const Array &bools) {
if (!bools.has_dtype(DataTypeVariant::Bool)) {
throw VortexException("Validity array isn't a Bool array", ErrorCode::InvalidArgument);
}
return {ValidityType::FromArray, vx_array_clone(Access::c_ptr(bools))};
}
Validity::Validity(Validity &&other) noexcept : type_(other.type_), array_(other.array_) {
other.array_ = nullptr;
other.type_ = ValidityType::NonNullable;
}
Validity &Validity::operator=(const Validity &other) {
if (this != &other) {
*this = Validity(other);
}
return *this;
}
Validity &Validity::operator=(Validity &&other) noexcept {
if (this != &other) {
vx_array_free(array_);
type_ = other.type_;
array_ = other.array_;
other.array_ = nullptr;
other.type_ = ValidityType::NonNullable;
}
return *this;
}
Validity::~Validity() {
vx_array_free(array_);
}
Array Validity::array() const {
if (type_ != ValidityType::FromArray || array_ == nullptr) {
throw VortexException("validity has no backing array", ErrorCode::InvalidArgument);
}
return Access::adopt<Array>(vx_array_clone(array_));
}
namespace detail {
bool ValidityBits::is_null(size_t index) const {
if (all_invalid_) {
return true;
}
if (bits_ == nullptr) {
return false;
}
const size_t bit = bit_offset_ + index;
return (bits_[bit / 8] >> (bit % 8) & 1) == 0;
}
ValidityBits::ValidityBits(const Session &session, const vx_array *canonical) {
vx_validity raw {};
vx_error *error = nullptr;
vx_array_get_validity(canonical, &raw, &error);
throw_on_error(error);
switch (static_cast<ValidityType>(raw.type)) {
case ValidityType::NonNullable:
case ValidityType::AllValid:
return;
case ValidityType::AllInvalid:
all_invalid_ = true;
return;
case ValidityType::FromArray:
break;
}
owner_ = vx_array_canonicalize(Access::c_ptr(session), raw.array, &error);
vx_array_free(raw.array);
throw_on_error(error);
bits_ = static_cast<const uint8_t *>(vx_array_data_ptr_bool(owner_, &bit_offset_, &error));
if (error != nullptr) {
vx_array_free(owner_);
}
throw_on_error(error);
}
ValidityBits::ValidityBits(ValidityBits &&other) noexcept
: owner_(other.owner_), bits_(other.bits_), bit_offset_(other.bit_offset_),
all_invalid_(other.all_invalid_) {
other.owner_ = nullptr;
other.bits_ = nullptr;
}
ValidityBits &ValidityBits::operator=(ValidityBits &&other) noexcept {
if (this != &other) {
vx_array_free(owner_);
owner_ = other.owner_;
bits_ = other.bits_;
bit_offset_ = other.bit_offset_;
all_invalid_ = other.all_invalid_;
other.owner_ = nullptr;
other.bits_ = nullptr;
}
return *this;
}
ValidityBits::~ValidityBits() {
vx_array_free(owner_);
}
} // namespace detail
static const vx_struct_fields *struct_fields_or_throw(const vx_dtype *dtype) {
const vx_struct_fields *fields = vx_dtype_struct_dtype(dtype);
if (fields == nullptr) {
throw VortexException("dtype is not a struct", ErrorCode::MismatchedTypes);
}
return fields;
}
void Array::Deleter::operator()(const vx_array *ptr) const noexcept {
vx_array_free(ptr);
}
Array::Array(const Array &other) : handle_(vx_array_clone(other.handle_.get())) {
}
Array &Array::operator=(const Array &other) {
if (this != &other) {
handle_.reset(vx_array_clone(other.handle_.get()));
}
return *this;
}
Array Array::null(size_t len) {
return Access::adopt<Array>(vx_array_new_null(len));
}
Array Array::primitive_raw(vx_ptype ptype, const void *data, size_t len, const Validity &validity) {
std::optional<Array> keep_alive;
vx_validity raw {};
raw.type = static_cast<vx_validity_type>(validity.type());
if (validity.type() == ValidityType::FromArray) {
keep_alive = validity.array();
raw.array = Access::c_ptr(*keep_alive);
}
vx_error *error = nullptr;
const vx_array *out = vx_array_new_primitive(ptype, data, len, &raw, &error);
throw_on_error(error);
return Access::adopt<Array>(out);
}
Array Array::from_arrow(ArrowArray *array, ArrowSchema *schema, bool nullable) {
vx_error *error = nullptr;
const vx_array *out = vx_array_from_arrow(array, schema, nullable, &error);
throw_on_error(error);
return Access::adopt<Array>(out);
}
size_t Array::size() const {
return vx_array_len(handle_.get());
}
bool Array::nullable() const {
return vx_array_is_nullable(handle_.get());
}
bool Array::has_dtype(DataTypeVariant v) const {
return vx_array_has_dtype(handle_.get(), static_cast<vx_dtype_variant>(v));
}
bool Array::is_primitive(PType p) const {
return vx_array_is_primitive(handle_.get(), static_cast<vx_ptype>(p));
}
DataType Array::dtype() const {
return Access::adopt<DataType>(vx_array_dtype(handle_.get()));
}
Validity Array::validity() const {
vx_validity raw {};
vx_error *error = nullptr;
vx_array_get_validity(handle_.get(), &raw, &error);
throw_on_error(error);
return Access::adopt<Validity>(static_cast<ValidityType>(raw.type), raw.array);
}
size_t Array::null_count() const {
vx_error *error = nullptr;
const size_t count = vx_array_invalid_count(handle_.get(), &error);
throw_on_error(error);
return count;
}
Array Array::field(size_t index) const {
vx_error *error = nullptr;
const vx_array *out = vx_array_get_field(handle_.get(), index, &error);
throw_on_error(error);
return Access::adopt<Array>(out);
}
Array Array::field(std::string_view name) const {
const DataType dt = dtype();
const std::unique_ptr<const vx_struct_fields, decltype(&vx_struct_fields_free)> fields(
struct_fields_or_throw(detail::Access::c_ptr(dt)),
&vx_struct_fields_free);
const uint64_t fields_size = vx_struct_fields_nfields(fields.get());
for (uint64_t i = 0; i < fields_size; ++i) {
const vx_view field = vx_struct_fields_field_name(fields.get(), i);
if (std::string_view {field.ptr, field.len} == name) {
return this->field(i);
}
}
throw VortexException("no field named \"" + std::string(name) + "\"", ErrorCode::InvalidArgument);
}
Array Array::slice(size_t begin, size_t end) const {
vx_error *error = nullptr;
const vx_array *out = vx_array_slice(handle_.get(), begin, end, &error);
throw_on_error(error);
return Access::adopt<Array>(out);
}
Array Array::apply(const Expression &expr) const {
vx_error *error = nullptr;
const vx_array *out = vx_array_apply(handle_.get(), Access::c_ptr(expr), &error);
throw_on_error(error);
return Access::adopt<Array>(out);
}
Array Array::canonicalize(const Session &session) const {
vx_error *error = nullptr;
const vx_array *out = vx_array_canonicalize(Access::c_ptr(session), handle_.get(), &error);
throw_on_error(error);
return Access::adopt<Array>(out);
}
Array make_struct(std::span<const ColumnField> fields, const Validity &validity) {
vx_validity raw {};
raw.type = static_cast<vx_validity_type>(validity.type());
std::optional<Array> keep_alive;
if (validity.type() == ValidityType::FromArray) {
keep_alive = validity.array();
raw.array = Access::c_ptr(*keep_alive);
}
std::unique_ptr<vx_struct_column_builder, decltype(&vx_struct_column_builder_free)> handle(
vx_struct_column_builder_new(&raw, fields.size()),
&vx_struct_column_builder_free);
vx_error *error = nullptr;
for (const auto &[name, array] : fields) {
vx_struct_column_builder_add_field(handle.get(), detail::to_view(name), Access::c_ptr(array), &error);
throw_on_error(error);
}
const vx_array *out = vx_struct_column_builder_finalize(handle.release(), &error);
throw_on_error(error);
return Access::adopt<Array>(out);
}
Array make_struct(std::initializer_list<ColumnField> fields, const Validity &validity) {
return make_struct({fields.begin(), fields.end()}, validity);
}
PrimitiveView<bool> Array::bools(const Session &session) const {
Array canonical = canonicalize(session);
const vx_array *raw = Access::c_ptr(canonical);
if (!vx_array_has_dtype(raw, DTYPE_BOOL)) {
throw VortexException("bools(): array is not a Bool array", ErrorCode::MismatchedTypes);
}
detail::ValidityBits validity(session, raw);
const size_t len = vx_array_len(raw);
return PrimitiveView<bool>(std::move(canonical), std::move(validity), len);
}
StringView Array::strings(const Session &session) const {
Array canonical = canonicalize(session);
const vx_array *raw = Access::c_ptr(canonical);
if (!vx_array_has_dtype(raw, DTYPE_UTF8)) {
throw VortexException("strings(): array is not a Utf8 array", ErrorCode::MismatchedTypes);
}
detail::ValidityBits validity(session, raw);
const size_t len = vx_array_len(raw);
return StringView(std::move(canonical), std::move(validity), len);
}
BytesView Array::bytes(const Session &session) const {
Array canonical = canonicalize(session);
const vx_array *raw = Access::c_ptr(canonical);
if (!vx_array_has_dtype(raw, DTYPE_BINARY)) {
throw VortexException("bytes(): array is not a Binary array", ErrorCode::MismatchedTypes);
}
detail::ValidityBits validity(session, raw);
const size_t len = vx_array_len(raw);
return BytesView(std::move(canonical), std::move(validity), len);
}
bool PrimitiveView<bool>::value(size_t i) const {
return vx_array_get_bool(Access::c_ptr(canonical_), i);
}
std::string_view StringView::operator[](size_t i) const {
vx_error *error = nullptr;
const vx_view out = vx_array_utf8_at(Access::c_ptr(canonical_), i, &error);
throw_on_error(error);
return {out.ptr, out.len};
}
BinaryView BytesView::operator[](size_t i) const {
vx_error *error = nullptr;
const vx_view out = vx_array_binary_at(Access::c_ptr(canonical_), i, &error);
throw_on_error(error);
return {reinterpret_cast<const std::byte *>(out.ptr), out.len};
}
} // namespace vortex