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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
//! Scalar downcasting methods to typed views.
use vortex_buffer::BufferString;
use vortex_buffer::ByteBuffer;
use vortex_error::VortexExpect;
use vortex_error::vortex_panic;
use crate::scalar::BinaryScalar;
use crate::scalar::BoolScalar;
use crate::scalar::DecimalScalar;
use crate::scalar::DecimalValue;
use crate::scalar::ExtScalar;
use crate::scalar::ListScalar;
use crate::scalar::PValue;
use crate::scalar::PrimitiveScalar;
use crate::scalar::Scalar;
use crate::scalar::ScalarValue;
use crate::scalar::StructScalar;
use crate::scalar::Utf8Scalar;
use crate::scalar::VariantScalar;
impl Scalar {
/// Returns a view of the scalar as a boolean scalar.
///
/// # Panics
///
/// Panics if the scalar does not have a [`Bool`](crate::dtype::DType::Bool) type.
pub fn as_bool(&self) -> BoolScalar<'_> {
self.as_bool_opt()
.vortex_expect("Failed to convert scalar to bool")
}
/// Returns a view of the scalar as a boolean scalar if it has a boolean type.
pub fn as_bool_opt(&self) -> Option<BoolScalar<'_>> {
BoolScalar::try_new(self.dtype(), self.value()).ok()
}
/// Returns a view of the scalar as a primitive scalar.
///
/// # Panics
///
/// Panics if the scalar does not have a [`Primitive`](crate::dtype::DType::Primitive) type.
pub fn as_primitive(&self) -> PrimitiveScalar<'_> {
self.as_primitive_opt()
.vortex_expect("Failed to convert scalar to primitive")
}
/// Returns a view of the scalar as a primitive scalar if it has a primitive type.
pub fn as_primitive_opt(&self) -> Option<PrimitiveScalar<'_>> {
PrimitiveScalar::try_new(self.dtype(), self.value()).ok()
}
/// Returns a view of the scalar as a decimal scalar.
///
/// # Panics
///
/// Panics if the scalar does not have a [`Decimal`](crate::dtype::DType::Decimal) type.
pub fn as_decimal(&self) -> DecimalScalar<'_> {
self.as_decimal_opt()
.vortex_expect("Failed to convert scalar to decimal")
}
/// Returns a view of the scalar as a decimal scalar if it has a decimal type.
pub fn as_decimal_opt(&self) -> Option<DecimalScalar<'_>> {
DecimalScalar::try_new(self.dtype(), self.value()).ok()
}
/// Returns a view of the scalar as a UTF-8 string scalar.
///
/// # Panics
///
/// Panics if the scalar does not have a [`Utf8`](crate::dtype::DType::Utf8) type.
pub fn as_utf8(&self) -> Utf8Scalar<'_> {
self.as_utf8_opt()
.vortex_expect("Failed to convert scalar to utf8")
}
/// Returns a view of the scalar as a UTF-8 string scalar if it has a UTF-8 type.
pub fn as_utf8_opt(&self) -> Option<Utf8Scalar<'_>> {
Utf8Scalar::try_new(self.dtype(), self.value()).ok()
}
/// Returns a view of the scalar as a binary scalar.
///
/// # Panics
///
/// Panics if the scalar does not have a [`Binary`](crate::dtype::DType::Binary) type.
pub fn as_binary(&self) -> BinaryScalar<'_> {
self.as_binary_opt()
.vortex_expect("Failed to convert scalar to binary")
}
/// Returns a view of the scalar as a binary scalar if it has a binary type.
pub fn as_binary_opt(&self) -> Option<BinaryScalar<'_>> {
BinaryScalar::try_new(self.dtype(), self.value()).ok()
}
/// Returns a view of the scalar as a struct scalar.
///
/// # Panics
///
/// Panics if the scalar does not have a [`Struct`](crate::dtype::DType::Struct) type.
pub fn as_struct(&self) -> StructScalar<'_> {
self.as_struct_opt()
.vortex_expect("Failed to convert scalar to struct")
}
/// Returns a view of the scalar as a struct scalar if it has a struct type.
pub fn as_struct_opt(&self) -> Option<StructScalar<'_>> {
StructScalar::try_new(self.dtype(), self.value()).ok()
}
/// Returns a view of the scalar as a list scalar.
///
/// Note that we use [`ListScalar`] to represent **both** [`List`](crate::dtype::DType::List) and
/// [`FixedSizeList`](crate::dtype::DType::FixedSizeList).
///
/// # Panics
///
/// Panics if the scalar does not have a [`List`](crate::dtype::DType::List) or [`FixedSizeList`](crate::dtype::DType::FixedSizeList) type.
pub fn as_list(&self) -> ListScalar<'_> {
self.as_list_opt()
.vortex_expect("Failed to convert scalar to list")
}
/// Returns a view of the scalar as a list scalar if it has a list type.
///
/// Note that we use [`ListScalar`] to represent **both** [`List`](crate::dtype::DType::List) and
/// [`FixedSizeList`](crate::dtype::DType::FixedSizeList).
pub fn as_list_opt(&self) -> Option<ListScalar<'_>> {
ListScalar::try_new(self.dtype(), self.value()).ok()
}
/// Returns a view of the scalar as an extension scalar.
///
/// # Panics
///
/// Panics if the scalar does not have a [`Extension`](crate::dtype::DType::Extension) type.
pub fn as_extension(&self) -> ExtScalar<'_> {
self.as_extension_opt()
.vortex_expect("Failed to convert scalar to extension")
}
/// Returns a view of the scalar as an extension scalar if it has an extension type.
pub fn as_extension_opt(&self) -> Option<ExtScalar<'_>> {
if !self.dtype().is_extension() {
return None;
}
// SAFETY: Because we are a valid Scalar, we have already validated that the value is valid
// for this extension type.
Some(ExtScalar::new_unchecked(self.dtype(), self.value()))
}
/// Returns a view of the scalar as a variant scalar.
///
/// # Panics
///
/// Panics if the scalar does not have a [`Variant`](crate::dtype::DType::Variant) type.
pub fn as_variant(&self) -> VariantScalar<'_> {
self.as_variant_opt()
.vortex_expect("Failed to convert scalar to variant")
}
/// Returns a view of the scalar as a variant scalar if it has a variant type.
pub fn as_variant_opt(&self) -> Option<VariantScalar<'_>> {
VariantScalar::try_new(self.dtype(), self.value()).ok()
}
}
impl ScalarValue {
/// Returns the boolean value, panicking if the value is not a [`Bool`][ScalarValue::Bool].
pub fn as_bool(&self) -> bool {
match self {
ScalarValue::Bool(b) => *b,
_ => vortex_panic!("ScalarValue is not a Bool"),
}
}
/// Returns the primitive value, panicking if the value is not a
/// [`Primitive`][ScalarValue::Primitive].
pub fn as_primitive(&self) -> &PValue {
match self {
ScalarValue::Primitive(p) => p,
_ => vortex_panic!("ScalarValue is not a Primitive"),
}
}
/// Returns the decimal value, panicking if the value is not a
/// [`Decimal`][ScalarValue::Decimal].
pub fn as_decimal(&self) -> &DecimalValue {
match self {
ScalarValue::Decimal(d) => d,
_ => vortex_panic!("ScalarValue is not a Decimal"),
}
}
/// Returns the UTF-8 string value, panicking if the value is not a [`Utf8`][ScalarValue::Utf8].
pub fn as_utf8(&self) -> &BufferString {
match self {
ScalarValue::Utf8(s) => s,
_ => vortex_panic!("ScalarValue is not a Utf8"),
}
}
/// Returns the binary value, panicking if the value is not a [`Binary`][ScalarValue::Binary].
pub fn as_binary(&self) -> &ByteBuffer {
match self {
ScalarValue::Binary(b) => b,
_ => vortex_panic!("ScalarValue is not a Binary"),
}
}
/// Returns the list elements, panicking if the value is not a [`List`][ScalarValue::List].
pub fn as_list(&self) -> &[Option<ScalarValue>] {
match self {
ScalarValue::List(elements) => elements,
_ => vortex_panic!("ScalarValue is not a List"),
}
}
/// Returns the boolean value, panicking if the value is not a [`Bool`][ScalarValue::Bool].
pub fn into_bool(self) -> bool {
match self {
ScalarValue::Bool(b) => b,
_ => vortex_panic!("ScalarValue is not a Bool"),
}
}
/// Returns the primitive value, panicking if the value is not a
/// [`Primitive`][ScalarValue::Primitive].
pub fn into_primitive(self) -> PValue {
match self {
ScalarValue::Primitive(p) => p,
_ => vortex_panic!("ScalarValue is not a Primitive"),
}
}
/// Returns the decimal value, panicking if the value is not a
/// [`Decimal`][ScalarValue::Decimal].
pub fn into_decimal(self) -> DecimalValue {
match self {
ScalarValue::Decimal(d) => d,
_ => vortex_panic!("ScalarValue is not a Decimal"),
}
}
/// Returns the UTF-8 string value, panicking if the value is not a [`Utf8`][ScalarValue::Utf8].
pub fn into_utf8(self) -> BufferString {
match self {
ScalarValue::Utf8(s) => s,
_ => vortex_panic!("ScalarValue is not a Utf8"),
}
}
/// Returns the binary value, panicking if the value is not a [`Binary`][ScalarValue::Binary].
pub fn into_binary(self) -> ByteBuffer {
match self {
ScalarValue::Binary(b) => b,
_ => vortex_panic!("ScalarValue is not a Binary"),
}
}
/// Returns the list elements, panicking if the value is not a [`List`][ScalarValue::List].
pub fn into_list(self) -> Vec<Option<ScalarValue>> {
match self {
ScalarValue::List(elements) => elements,
_ => vortex_panic!("ScalarValue is not a List"),
}
}
/// Returns the row-specific scalar wrapped by a variant, panicking if the value is not a
/// variant.
pub fn as_variant(&self) -> &Scalar {
match self {
ScalarValue::Variant(value) => value,
_ => vortex_panic!("ScalarValue is not a Variant"),
}
}
/// Returns the row-specific scalar wrapped by a variant, panicking if the value is not a
/// variant.
pub fn into_variant(self) -> Scalar {
match self {
ScalarValue::Variant(value) => *value,
_ => vortex_panic!("ScalarValue is not a Variant"),
}
}
}