Skip to content

Commit a1057db

Browse files
authored
FFI: get scalars from arrays (#8896)
- Add vx_array_get_scalar method for retrieving vx_scalar. - Add scalar's primitive, utf8, bool, and binary getters generated from a macro. - Replace decimal scalar creation functions with a macro. - Remove vx_array's primitive getters in favor of obtaining a vx_scarar. #7483 Signed-off-by: Mikhail Kot <[email protected]>
1 parent 6ae2a8b commit a1057db

15 files changed

Lines changed: 891 additions & 450 deletions

File tree

‎lang/cpp/include/vortex/array.hpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "vortex/dtype.hpp"
77
#include "vortex/error.hpp"
88
#include "vortex/expression.hpp"
9+
#include "vortex/scalar.hpp"
910
#include "vortex/session.hpp"
1011

1112
#include <vortex.h>
@@ -211,6 +212,8 @@ class Array {
211212
// Bulk view over Binary values.
212213
BytesView bytes(const Session &session) const;
213214

215+
Scalar scalar_at(const Session &session, size_t index) const;
216+
214217
private:
215218
friend struct detail::Access;
216219
friend class StringView;

‎lang/cpp/include/vortex/scalar.hpp‎

Lines changed: 143 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,159 @@ class Scalar {
2626
bool is_null() const;
2727
DataType dtype() const;
2828

29+
/**
30+
* Read scalar's value.
31+
*
32+
* Supported types are bool, primitives, std::string_view, and BinaryView.
33+
*
34+
* std::string_view and BinaryView returned borrow from scalar and stay
35+
* valid while scalar is valid.
36+
*
37+
* Throws if scalar type does not match T or value is Null.
38+
*/
39+
template <element_type T>
40+
T get() const;
41+
42+
/**
43+
* Read a decimal scalar's unscaled value.
44+
* int8/16/32/64_t are supported.
45+
* Throws if scalar is not a decimal or is Null.
46+
* Trying to get decimal's value that overflows T is UB.
47+
*/
48+
template <primitive_type T>
49+
T get_decimal() const;
50+
2951
private:
3052
friend struct detail::Access;
31-
explicit Scalar(vx_scalar *owned) : handle_(owned) {
53+
explicit Scalar(const vx_scalar *owned) : handle_(owned) {
3254
}
33-
vx_scalar *release() && {
55+
const vx_scalar *release() && {
3456
return handle_.release();
3557
}
3658

3759
struct Deleter {
38-
void operator()(vx_scalar *ptr) const noexcept;
60+
void operator()(const vx_scalar *ptr) const noexcept;
3961
};
40-
std::unique_ptr<vx_scalar, Deleter> handle_;
62+
std::unique_ptr<const vx_scalar, Deleter> handle_;
4163
};
4264

65+
template <element_type T>
66+
T Scalar::get() const {
67+
using enum DataTypeVariant;
68+
using enum ErrorCode;
69+
using enum PType;
70+
71+
const DataType data_type = dtype();
72+
const DataTypeVariant variant = data_type.variant();
73+
if (is_null()) {
74+
throw VortexException("Scalar is null", InvalidArgument);
75+
}
76+
77+
const vx_scalar *const h = handle_.get();
78+
if constexpr (std::is_same_v<T, bool>) {
79+
if (variant != Bool) {
80+
throw VortexException("scalar get type doesn't match", InvalidArgument);
81+
}
82+
return vx_scalar_get_bool(h);
83+
} else if constexpr (std::is_same_v<T, std::string_view>) {
84+
if (variant != Utf8) {
85+
throw VortexException("scalar get type doesn't match", InvalidArgument);
86+
}
87+
vx_view v = vx_scalar_get_utf8(h);
88+
return std::string_view(v.ptr, v.len);
89+
} else if constexpr (std::is_same_v<T, BinaryView>) {
90+
if (variant != Binary) {
91+
throw VortexException("scalar get type doesn't match", InvalidArgument);
92+
}
93+
vx_view v = vx_scalar_get_binary(h);
94+
return BinaryView(reinterpret_cast<const std::byte *>(v.ptr), v.len);
95+
} else if constexpr (std::is_same_v<T, uint8_t>) {
96+
if (variant != Primitive || data_type.primitive_type() != U8) {
97+
throw VortexException("scalar get type doesn't match", ErrorCode::InvalidArgument);
98+
}
99+
return vx_scalar_get_u8(h);
100+
} else if constexpr (std::is_same_v<T, uint16_t>) {
101+
if (variant != Primitive || data_type.primitive_type() != U16) {
102+
throw VortexException("scalar get type doesn't match", ErrorCode::InvalidArgument);
103+
}
104+
return vx_scalar_get_u16(h);
105+
} else if constexpr (std::is_same_v<T, uint32_t>) {
106+
if (variant != Primitive || data_type.primitive_type() != U32) {
107+
throw VortexException("scalar get type doesn't match", ErrorCode::InvalidArgument);
108+
}
109+
return vx_scalar_get_u32(h);
110+
} else if constexpr (std::is_same_v<T, uint64_t>) {
111+
if (variant != Primitive || data_type.primitive_type() != U64) {
112+
throw VortexException("scalar get type doesn't match", ErrorCode::InvalidArgument);
113+
}
114+
return vx_scalar_get_u64(h);
115+
} else if constexpr (std::is_same_v<T, int8_t>) {
116+
if (variant != Primitive || data_type.primitive_type() != I8) {
117+
throw VortexException("scalar get type doesn't match", ErrorCode::InvalidArgument);
118+
}
119+
return vx_scalar_get_i8(h);
120+
} else if constexpr (std::is_same_v<T, int16_t>) {
121+
if (variant != Primitive || data_type.primitive_type() != I16) {
122+
throw VortexException("scalar get type doesn't match", ErrorCode::InvalidArgument);
123+
}
124+
return vx_scalar_get_i16(h);
125+
} else if constexpr (std::is_same_v<T, int32_t>) {
126+
if (variant != Primitive || data_type.primitive_type() != I32) {
127+
throw VortexException("scalar get type doesn't match", ErrorCode::InvalidArgument);
128+
}
129+
return vx_scalar_get_i32(h);
130+
} else if constexpr (std::is_same_v<T, int64_t>) {
131+
if (variant != Primitive || data_type.primitive_type() != I64) {
132+
throw VortexException("scalar get type doesn't match", ErrorCode::InvalidArgument);
133+
}
134+
return vx_scalar_get_i64(h);
135+
} else if constexpr (std::is_same_v<T, float>) {
136+
if (variant != Primitive || data_type.primitive_type() != F32) {
137+
throw VortexException("scalar get type doesn't match", ErrorCode::InvalidArgument);
138+
}
139+
return vx_scalar_get_f32(h);
140+
} else if constexpr (std::is_same_v<T, double>) {
141+
if (variant != Primitive || data_type.primitive_type() != F64) {
142+
throw VortexException("scalar get type doesn't match", ErrorCode::InvalidArgument);
143+
}
144+
return vx_scalar_get_f64(h);
145+
} else if constexpr (std::is_same_v<T, float16_t>) {
146+
if (variant != Primitive || data_type.primitive_type() != F16) {
147+
throw VortexException("scalar get type doesn't match", ErrorCode::InvalidArgument);
148+
}
149+
const uint16_t bits = vx_scalar_get_f16_bits(h);
150+
#if __STDCPP_FLOAT16_T__ != 1
151+
return {.bits = bits};
152+
#else
153+
return std::bit_cast<float16_t>(bits);
154+
#endif
155+
} else {
156+
static_assert(false, "scalar get is not supported for this type");
157+
}
158+
}
159+
160+
template <primitive_type T>
161+
T Scalar::get_decimal() const {
162+
if (is_null()) {
163+
throw VortexException("Scalar is null", ErrorCode::InvalidArgument);
164+
}
165+
if (dtype().variant() != DataTypeVariant::Decimal) {
166+
throw VortexException("DataType is not Decimal", ErrorCode::InvalidArgument);
167+
}
168+
const vx_scalar *const h = handle_.get();
169+
if constexpr (std::is_same_v<T, int8_t>) {
170+
return vx_scalar_get_decimal_i8(h);
171+
} else if constexpr (std::is_same_v<T, int16_t>) {
172+
return vx_scalar_get_decimal_i16(h);
173+
} else if constexpr (std::is_same_v<T, int32_t>) {
174+
return vx_scalar_get_decimal_i32(h);
175+
} else if constexpr (std::is_same_v<T, int64_t>) {
176+
return vx_scalar_get_decimal_i64(h);
177+
} else {
178+
static_assert(false, "unsupported decimal scalar get type");
179+
}
180+
}
181+
43182
namespace detail {
44183
vx_scalar *make_bool(bool value, bool nullable);
45184
vx_scalar *make_primitive(vx_ptype ptype, const void *value, bool nullable);

‎lang/cpp/src/array.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "vortex/common.hpp"
66
#include "vortex/dtype.hpp"
77
#include "vortex/error.hpp"
8+
#include "vortex/scalar.hpp"
9+
#include "vortex/session.hpp"
810

911
#include <vortex.h>
1012

@@ -324,7 +326,19 @@ BytesView Array::bytes(const Session &session) const {
324326
return BytesView(std::move(canonical), std::move(validity), len);
325327
}
326328

329+
Scalar Array::scalar_at(const Session &session, size_t index) const {
330+
vx_error *error = nullptr;
331+
const vx_scalar *scalar = vx_array_get_scalar(Access::c_ptr(session), handle_.get(), index, &error);
332+
throw_on_error(error);
333+
return Access::adopt<Scalar>(scalar);
334+
}
335+
327336
bool PrimitiveView<bool>::value(size_t i) const {
337+
if (i >= size_) {
338+
throw VortexException("index " + std::to_string(i) + " out of bounds for view of size " +
339+
std::to_string(size_),
340+
ErrorCode::OutOfBounds);
341+
}
328342
return vx_array_get_bool(Access::c_ptr(canonical_), i);
329343
}
330344

‎lang/cpp/src/scalar.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace vortex {
1717
using detail::Access;
1818
using detail::throw_on_error;
1919

20-
void Scalar::Deleter::operator()(vx_scalar *ptr) const noexcept {
20+
void Scalar::Deleter::operator()(const vx_scalar *ptr) const noexcept {
2121
vx_scalar_free(ptr);
2222
}
2323

‎lang/cpp/tests/expression.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ TEST_CASE("Operator overloading", "[expr]") {
8585
REQUIRE(bits.value(1));
8686
REQUIRE(bits.value(2));
8787
REQUIRE_FALSE(bits.value(3));
88+
REQUIRE_THROWS_AS(bits.value(data.size()), VortexException);
8889
}
8990

9091
TEST_CASE("Apply error", "[expr]") {

‎lang/cpp/tests/float16_t.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4-
#include "vortex/dtype.hpp"
54
#include <catch2/catch_test_macros.hpp>
65
#include <vortex/scalar.hpp>
76

@@ -25,11 +24,13 @@ TEST_CASE("F16 scalar", "[scalar]") {
2524
Scalar scalar = scalar::of(float16t);
2625
REQUIRE(scalar.dtype().variant() == DataTypeVariant::Primitive);
2726
REQUIRE(scalar.dtype().primitive_type() == vortex::PType::F16);
27+
REQUIRE(scalar.get<float16_t>() == float16t);
2828

2929
_Float16 float16t_alias = 1.0f16;
3030
scalar = scalar::of(float16t_alias);
3131
REQUIRE(scalar.dtype().variant() == DataTypeVariant::Primitive);
3232
REQUIRE(scalar.dtype().primitive_type() == vortex::PType::F16);
33+
REQUIRE(scalar.get<float16_t>() == float16t_alias);
3334
}
3435
#endif
3536
} // namespace

‎lang/cpp/tests/float16_t_compat.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ TEST_CASE("F16 scalar (compatibility)", "[scalar]") {
3434
Scalar scalar = scalar::of(float16t);
3535
REQUIRE(scalar.dtype().variant() == DataTypeVariant::Primitive);
3636
REQUIRE(scalar.dtype().primitive_type() == vortex::PType::F16);
37+
REQUIRE(scalar.get<float16_t>() == float16t);
3738
}
3839
} // namespace

‎lang/cpp/tests/scalar.cpp‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ using namespace std::string_view_literals;
1010

1111
namespace {
1212
using enum vortex::PType;
13+
using scalar::decimal;
14+
using scalar::of;
1315

1416
TEST_CASE("Boolean scalar", "[scalar]") {
1517
Scalar s = scalar::of(true);
@@ -29,7 +31,7 @@ TEST_CASE("Integer scalars", "[scalar]") {
2931
}
3032

3133
TEST_CASE("Float scalars", "[scalar]") {
32-
REQUIRE(scalar::of(1.5F).dtype().primitive_type() == F32);
34+
REQUIRE(scalar::of(1.5f).dtype().primitive_type() == F32);
3335
REQUIRE(scalar::of(1.5).dtype().primitive_type() == F64);
3436
REQUIRE(scalar::of(float16_t {0x3C00}).dtype().primitive_type() == F16);
3537
}
@@ -44,15 +46,19 @@ TEST_CASE("Null scalar", "[scalar]") {
4446
Scalar s = scalar::null(dtype::int32(true));
4547
REQUIRE(s.is_null());
4648
REQUIRE(s.dtype().variant() == DataTypeVariant::Primitive);
49+
REQUIRE_THROWS_AS(s.get<int32_t>(), VortexException);
4750
}
4851

4952
TEST_CASE("UTF-8 scalar", "[scalar]") {
5053
Scalar s = scalar::of("hello"sv);
5154
REQUIRE_FALSE(s.is_null());
5255
REQUIRE(s.dtype().variant() == DataTypeVariant::Utf8);
56+
REQUIRE_THROWS_AS(s.get<float>(), VortexException);
5357

5458
REQUIRE_FALSE(scalar::of(""sv).is_null());
5559
s = scalar::of("Широкая строка"sv);
60+
REQUIRE_THROWS_AS(s.get<bool>(), VortexException);
61+
REQUIRE_THROWS_AS(s.get_decimal<int8_t>(), VortexException);
5662
REQUIRE(s.dtype().variant() == DataTypeVariant::Utf8);
5763

5864
REQUIRE_THROWS_AS(scalar::of("\xFF\xFE"sv), VortexException);
@@ -87,6 +93,40 @@ TEST_CASE("Decimal scalars", "[scalar]") {
8793
REQUIRE(d64.dtype().decimal_scale() == 3);
8894
}
8995

96+
TEST_CASE("Invalid scalar getter", "[scalar]") {
97+
Scalar scalar = of<int32_t>(0);
98+
REQUIRE_THROWS_AS(scalar.get<double>(), VortexException);
99+
}
100+
101+
TEST_CASE("Primitive getters", "[scalar]") {
102+
REQUIRE(of<uint64_t>(1ULL << 40).get<uint64_t>() == (1ULL << 40));
103+
REQUIRE(of<int32_t>(-7).get<int32_t>() == -7);
104+
REQUIRE(of(2.5).get<double>() == 2.5);
105+
REQUIRE(of(true).get<bool>());
106+
}
107+
108+
TEST_CASE("String getters", "[scalar]") {
109+
Scalar s = of("hello"sv);
110+
REQUIRE(s.get<std::string_view>() == "hello"sv);
111+
112+
const std::byte bytes[] = {std::byte {1}, std::byte {2}, std::byte {0}, std::byte {4}};
113+
Scalar b = of(std::span<const std::byte> {bytes});
114+
BinaryView view = b.get<BinaryView>();
115+
REQUIRE(view.size() == 4);
116+
REQUIRE(view[0] == std::byte {1});
117+
REQUIRE(view[3] == std::byte {4});
118+
}
119+
120+
TEST_CASE("Decimal getters", "[scalar]") {
121+
Scalar s = scalar::null(dtype::decimal(5, 2));
122+
REQUIRE_THROWS_AS(s.get_decimal<int8_t>(), VortexException);
123+
124+
REQUIRE(decimal<int8_t>(56, 5, 2).get_decimal<int8_t>() == 56);
125+
REQUIRE(decimal<int16_t>(1234, 5, 2).get_decimal<int16_t>() == 1234);
126+
REQUIRE(decimal<int32_t>(5678, 6, 2).get_decimal<int32_t>() == 5678);
127+
REQUIRE(decimal<int64_t>(99999, 12, 3).get_decimal<int64_t>() == 99999);
128+
}
129+
90130
TEST_CASE("Copy scalar", "[scalar]") {
91131
Scalar a = scalar::of<int64_t>(42);
92132
Scalar b = a;

0 commit comments

Comments
 (0)