@@ -26,6 +26,27 @@ 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/BinaryView's returned borrow from scalar and stay valid
35+ * while scalar is valud.
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, is Null, or value does not fit in T.
46+ */
47+ template <primitive_type T>
48+ T get_decimal () const ;
49+
2950private:
3051 friend struct detail ::Access;
3152 explicit Scalar (vx_scalar *owned) : handle_(owned) {
@@ -40,6 +61,58 @@ class Scalar {
4061 std::unique_ptr<vx_scalar, Deleter> handle_;
4162};
4263
64+ template <element_type T>
65+ T Scalar::get () const {
66+ vx_scalar *h = handle_.get ();
67+ if constexpr (std::is_same_v<T, bool >) {
68+ return vx_scalar_get_bool (h);
69+ } else if constexpr (std::is_same_v<T, std::string_view>) {
70+ vx_view v = vx_scalar_get_utf8 (h);
71+ return std::string_view (v.ptr , v.len );
72+ } else if constexpr (std::is_same_v<T, BinaryView>) {
73+ vx_view v = vx_scalar_get_binary (h);
74+ return BinaryView (reinterpret_cast <const std::byte *>(v.ptr ), v.len );
75+ } else if constexpr (std::is_same_v<T, uint8_t >) {
76+ return vx_scalar_get_u8 (h);
77+ } else if constexpr (std::is_same_v<T, uint16_t >) {
78+ return vx_scalar_get_u16 (h);
79+ } else if constexpr (std::is_same_v<T, uint32_t >) {
80+ return vx_scalar_get_u32 (h);
81+ } else if constexpr (std::is_same_v<T, uint64_t >) {
82+ return vx_scalar_get_u64 (h);
83+ } else if constexpr (std::is_same_v<T, int8_t >) {
84+ return vx_scalar_get_i8 (h);
85+ } else if constexpr (std::is_same_v<T, int16_t >) {
86+ return vx_scalar_get_i16 (h);
87+ } else if constexpr (std::is_same_v<T, int32_t >) {
88+ return vx_scalar_get_i32 (h);
89+ } else if constexpr (std::is_same_v<T, int64_t >) {
90+ return vx_scalar_get_i64 (h);
91+ } else if constexpr (std::is_same_v<T, float >) {
92+ return vx_scalar_get_f32 (h);
93+ } else if constexpr (std::is_same_v<T, double >) {
94+ return vx_scalar_get_f64 (h);
95+ } else {
96+ static_assert (false , " f16 scalar get is not supported" );
97+ }
98+ }
99+
100+ template <primitive_type T>
101+ T Scalar::get_decimal () const {
102+ vx_scalar *h = handle_.get ();
103+ if constexpr (std::is_same_v<T, int8_t >) {
104+ return vx_scalar_get_decimal_i8 (h);
105+ } else if constexpr (std::is_same_v<T, int16_t >) {
106+ return vx_scalar_get_decimal_i16 (h);
107+ } else if constexpr (std::is_same_v<T, int32_t >) {
108+ return vx_scalar_get_decimal_i32 (h);
109+ } else if constexpr (std::is_same_v<T, int64_t >) {
110+ return vx_scalar_get_decimal_i64 (h);
111+ } else {
112+ static_assert (false , " unsupported decimal scalar get type" );
113+ }
114+ }
115+
43116namespace detail {
44117vx_scalar *make_bool (bool value, bool nullable);
45118vx_scalar *make_primitive (vx_ptype ptype, const void *value, bool nullable);
0 commit comments