-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathprimitive.rs
More file actions
218 lines (185 loc) · 7.09 KB
/
primitive.rs
File metadata and controls
218 lines (185 loc) · 7.09 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
//! Conversions for [`PrimitiveScalar`]s.
use vortex_error::VortexError;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_err;
use crate::dtype::DType;
use crate::dtype::NativePType;
use crate::dtype::Nullability;
use crate::dtype::PType;
use crate::dtype::half::f16;
use crate::scalar::PValue;
use crate::scalar::PrimitiveScalar;
use crate::scalar::Scalar;
use crate::scalar::ScalarValue;
// TODO(connor): Ideally we remove this.
impl From<PrimitiveScalar<'_>> for Scalar {
fn from(ps: PrimitiveScalar<'_>) -> Self {
// SAFETY: `PrimitiveScalar` is already a valid `Scalar`.
unsafe {
Scalar::new_unchecked(ps.dtype().clone(), ps.pvalue().map(ScalarValue::Primitive))
}
}
}
impl From<PValue> for ScalarValue {
fn from(value: PValue) -> Self {
ScalarValue::Primitive(value)
}
}
/// Implements scalar conversions for a primitive type.
macro_rules! primitive_scalar {
($T:ty) => {
////////////////////////////////////////////////////////////////////////////////////////////
// `Scalar` INTO $T conversions.
////////////////////////////////////////////////////////////////////////////////////////////
/// Fallible conversion from a [`ScalarValue`] into an `T`.
///
/// # Errors
///
/// Returns an error if unable to convert the scalar value into the target type,
impl TryFrom<&ScalarValue> for $T {
type Error = VortexError;
fn try_from(value: &ScalarValue) -> VortexResult<Self> {
value.as_primitive().cast::<$T>()
}
}
/// Fallible conversion from a [`Scalar`] into an `T`.
///
/// # Errors
///
/// Returns an error if unable to convert the scalar into the target type, or if the
/// `Scalar` itself is null.
impl TryFrom<&Scalar> for $T {
type Error = VortexError;
fn try_from(value: &Scalar) -> VortexResult<Self> {
match value.value() {
Some(ScalarValue::Primitive(pv)) => pv.cast::<$T>(),
Some(_) => Err(vortex_err!(
"Expected primitive scalar, found {}",
value.dtype()
)),
None => Err(vortex_err!("Can't extract present value from null scalar")),
}
}
}
/// Fallible conversion from a [`Scalar`] into an `Option<T>`.
///
/// # Errors
///
/// Returns an error if the [`Scalar`] is not primitive, or if it is unable to convert the
/// [`Scalar`] into the target type.
impl TryFrom<&Scalar> for Option<$T> {
type Error = VortexError;
fn try_from(value: &Scalar) -> VortexResult<Self> {
let primitive_scalar = PrimitiveScalar::try_new(value.dtype(), value.value())?;
primitive_scalar.try_typed_value::<$T>()
}
}
////////////////////////////////////////////////////////////////////////////////////////////
// `Scalar` FROM $T conversions.
////////////////////////////////////////////////////////////////////////////////////////////
/// `Into<ScalarValue>` for T.
impl From<$T> for ScalarValue {
fn from(value: $T) -> Self {
ScalarValue::Primitive(value.into())
}
}
/// Non-nullable `Into<Scalar>` implementation for T.
impl From<$T> for Scalar {
fn from(value: $T) -> Self {
Scalar::try_new(
DType::Primitive(<$T>::PTYPE, Nullability::NonNullable),
Some(ScalarValue::Primitive(value.into())),
)
.vortex_expect(
"somehow unable to construct a primitive `Scalar` from a native type",
)
}
}
/// Nullable `Into<Scalar>` implementation for T.
impl From<Option<$T>> for Scalar {
fn from(value: Option<$T>) -> Self {
Scalar::try_new(
DType::Primitive(<$T>::PTYPE, Nullability::Nullable),
value.map(|value| ScalarValue::Primitive(value.into())),
)
.vortex_expect(
"somehow unable to construct a primitive `Scalar` from a native type",
)
}
}
};
}
primitive_scalar!(u8);
primitive_scalar!(u16);
primitive_scalar!(u32);
primitive_scalar!(u64);
primitive_scalar!(i8);
primitive_scalar!(i16);
primitive_scalar!(i32);
primitive_scalar!(i64);
primitive_scalar!(f16);
primitive_scalar!(f32);
primitive_scalar!(f64);
////////////////////////////////////////////////////////////////////////////////////////////
// `Scalar` <---> `usize` conversions.
////////////////////////////////////////////////////////////////////////////////////////////
// NB: We cast `usize` to `u64` (which should always succeed) for better ergonomics.
/// Fallible conversion from a [`ScalarValue`] into an `T`.
///
/// # Errors
///
/// Returns an error if unable to convert the scalar value into the target type,
impl TryFrom<&ScalarValue> for usize {
type Error = VortexError;
fn try_from(value: &ScalarValue) -> VortexResult<Self> {
let val = value.as_primitive().cast::<u64>()?;
Ok(usize::try_from(val)?)
}
}
impl TryFrom<&Scalar> for usize {
type Error = VortexError;
fn try_from(value: &Scalar) -> Result<Self, Self::Error> {
let prim_scalar = value
.as_primitive_opt()
.ok_or_else(|| vortex_err!("Expected primitive scalar, found {}", value.dtype()))?;
let prim = prim_scalar
.as_::<u64>()
.ok_or_else(|| vortex_err!("cannot convert Null to usize"))?;
Ok(usize::try_from(prim)?)
}
}
impl TryFrom<&Scalar> for Option<usize> {
type Error = VortexError;
fn try_from(value: &Scalar) -> Result<Self, Self::Error> {
let prim_scalar = value
.as_primitive_opt()
.ok_or_else(|| vortex_err!("Expected primitive scalar, found {}", value.dtype()))?;
Ok(prim_scalar.as_::<u64>().map(usize::try_from).transpose()?)
}
}
impl From<usize> for ScalarValue {
fn from(value: usize) -> Self {
ScalarValue::Primitive((value as u64).into())
}
}
impl From<usize> for Scalar {
fn from(value: usize) -> Self {
Scalar::try_new(
DType::Primitive(PType::U64, Nullability::NonNullable),
Some(ScalarValue::Primitive((value as u64).into())),
)
.vortex_expect("somehow unable to construct a primitive `Scalar` from a native type")
}
}
impl From<Option<usize>> for Scalar {
fn from(value: Option<usize>) -> Self {
Scalar::try_new(
DType::Primitive(PType::U64, Nullability::Nullable),
value.map(|value| ScalarValue::Primitive((value as u64).into())),
)
.vortex_expect("somehow unable to construct a primitive `Scalar` from a native type")
}
}