@@ -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+
2951private:
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+
43182namespace detail {
44183vx_scalar *make_bool (bool value, bool nullable);
45184vx_scalar *make_primitive (vx_ptype ptype, const void *value, bool nullable);
0 commit comments