@@ -22,6 +22,7 @@ use crate::scalar::DecimalValue;
2222use crate :: scalar:: PValue ;
2323use crate :: scalar:: Scalar ;
2424use 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.
2728impl 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}
0 commit comments