Skip to content

Commit 1caf136

Browse files
committed
pause
Signed-off-by: Connor Tsui <[email protected]>
1 parent 6a4f127 commit 1caf136

3 files changed

Lines changed: 45 additions & 8 deletions

File tree

‎vortex-array/src/scalar/constructor.rs‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::scalar::DecimalValue;
2222
use crate::scalar::PValue;
2323
use crate::scalar::Scalar;
2424
use crate::scalar::ScalarValue;
25+
use crate::scalar::extension::ExtScalarValue;
2526

2627
// TODO(connor): Really, we want `try_` constructors that return errors instead of just panic.
2728
impl Scalar {
@@ -171,10 +172,21 @@ impl Scalar {
171172
}
172173

173174
/// Creates a new extension scalar wrapping the given storage value.
174-
pub fn extension<V: ExtVTable + Default>(options: V::Metadata, value: Scalar) -> Self {
175-
let ext_dtype = ExtDType::<V>::try_new(options, value.dtype().clone())
175+
pub fn extension<V: ExtVTable + Default>(
176+
metadata: V::Metadata,
177+
storage_scalar: Scalar,
178+
) -> Self {
179+
let ext_dtype = ExtDType::<V>::try_new(metadata, storage_scalar.dtype().clone())
176180
.vortex_expect("Failed to create extension dtype");
177-
Self::try_new(DType::Extension(ext_dtype.erased()), value.into_value())
181+
let storage_value = storage_scalar.into_value();
182+
183+
let ext_value = storage_value.map(|sv| {
184+
let owned = ExtScalarValue::<V>::try_new(&ext_dtype, sv)
185+
.vortex_expect("unable to construct an extension `Scalar`");
186+
ScalarValue::Extension(owned.erased())
187+
});
188+
189+
Self::try_new(DType::Extension(ext_dtype.erased()), ext_value)
178190
.vortex_expect("unable to construct an extension `Scalar`")
179191
}
180192

@@ -183,9 +195,10 @@ impl Scalar {
183195
/// # Panics
184196
///
185197
/// Panics if the storage dtype of `ext_dtype` does not match `value`'s dtype.
186-
pub fn extension_ref(ext_dtype: ExtDTypeRef, value: Scalar) -> Self {
187-
assert_eq!(ext_dtype.storage_dtype(), value.dtype());
188-
Self::try_new(DType::Extension(ext_dtype), value.into_value())
198+
pub fn extension_ref(ext_dtype: ExtDTypeRef, storage: Scalar) -> Self {
199+
assert_eq!(ext_dtype.storage_dtype(), storage.dtype());
200+
201+
Self::try_new(DType::Extension(ext_dtype), storage.into_value())
189202
.vortex_expect("unable to construct an extension `Scalar`")
190203
}
191204
}

‎vortex-array/src/scalar/downcast.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::scalar::Scalar;
2020
use crate::scalar::ScalarValue;
2121
use crate::scalar::StructScalar;
2222
use crate::scalar::Utf8Scalar;
23+
use crate::scalar::extension::ExtScalarValueRef;
2324

2425
impl Scalar {
2526
/// Returns a view of the scalar as a boolean scalar.
@@ -206,6 +207,15 @@ impl ScalarValue {
206207
}
207208
}
208209

210+
/// Returns the extension value, panicking if the value is not an
211+
/// [`Extension`](ScalarValue::Extension).
212+
pub fn as_extension(&self) -> &ExtScalarValueRef {
213+
match self {
214+
ScalarValue::Extension(e) => e,
215+
_ => vortex_panic!("ScalarValue is not an Extension"),
216+
}
217+
}
218+
209219
/// Returns the boolean value, panicking if the value is not a [`Bool`][ScalarValue::Bool].
210220
pub fn into_bool(self) -> bool {
211221
match self {
@@ -255,4 +265,13 @@ impl ScalarValue {
255265
_ => vortex_panic!("ScalarValue is not a List"),
256266
}
257267
}
268+
269+
/// Returns the extension value, panicking if the value is not an
270+
/// [`Extension`](ScalarValue::Extension).
271+
pub fn into_extension(self) -> ExtScalarValueRef {
272+
match self {
273+
ScalarValue::Extension(e) => e,
274+
_ => vortex_panic!("ScalarValue is not an Extension"),
275+
}
276+
}
258277
}

‎vortex-array/src/scalar/typed_view/extension.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,13 @@ impl<'a> ExtScalar<'a> {
7070

7171
/// Returns the storage scalar of the extension scalar.
7272
pub fn to_storage_scalar(&self) -> Scalar {
73-
Scalar::try_new(self.ext_dtype.storage_dtype().clone(), self.value.cloned())
74-
.vortex_expect("ExtScalar is invalid")
73+
Scalar::try_new(
74+
self.ext_dtype.storage_dtype().clone(),
75+
self.value
76+
.map(|value| value.as_extension().storage_value())
77+
.cloned(),
78+
)
79+
.vortex_expect("ExtScalar is invalid")
7580
}
7681

7782
/// Return the [`DType`] of the extension scalar.

0 commit comments

Comments
 (0)