|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use vortex_error::VortexResult; |
| 5 | +use vortex_error::vortex_bail; |
| 6 | +use vortex_session::VortexSession; |
| 7 | +use vortex_session::registry::CachedId; |
| 8 | + |
| 9 | +use crate::ArrayRef; |
| 10 | +use crate::ExecutionCtx; |
| 11 | +use crate::IntoArray; |
| 12 | +use crate::arrays::ConstantArray; |
| 13 | +use crate::arrays::ExtensionArray; |
| 14 | +use crate::arrays::extension::ExtensionArrayExt; |
| 15 | +use crate::dtype::DType; |
| 16 | +use crate::expr::Expression; |
| 17 | +use crate::scalar_fn::Arity; |
| 18 | +use crate::scalar_fn::ChildName; |
| 19 | +use crate::scalar_fn::EmptyOptions; |
| 20 | +use crate::scalar_fn::ExecutionArgs; |
| 21 | +use crate::scalar_fn::ScalarFnId; |
| 22 | +use crate::scalar_fn::ScalarFnVTable; |
| 23 | + |
| 24 | +/// Extract the storage values from an extension array. |
| 25 | +#[derive(Clone)] |
| 26 | +pub struct ExtStorage; |
| 27 | + |
| 28 | +impl ScalarFnVTable for ExtStorage { |
| 29 | + type Options = EmptyOptions; |
| 30 | + |
| 31 | + fn id(&self) -> ScalarFnId { |
| 32 | + static ID: CachedId = CachedId::new("vortex.ext.storage"); |
| 33 | + *ID |
| 34 | + } |
| 35 | + |
| 36 | + fn serialize(&self, _options: &Self::Options) -> VortexResult<Option<Vec<u8>>> { |
| 37 | + Ok(Some(vec![])) |
| 38 | + } |
| 39 | + |
| 40 | + fn deserialize( |
| 41 | + &self, |
| 42 | + _metadata: &[u8], |
| 43 | + _session: &VortexSession, |
| 44 | + ) -> VortexResult<Self::Options> { |
| 45 | + Ok(EmptyOptions) |
| 46 | + } |
| 47 | + |
| 48 | + fn arity(&self, _options: &Self::Options) -> Arity { |
| 49 | + Arity::Exact(1) |
| 50 | + } |
| 51 | + |
| 52 | + fn child_name(&self, _options: &Self::Options, child_idx: usize) -> ChildName { |
| 53 | + match child_idx { |
| 54 | + 0 => ChildName::from("input"), |
| 55 | + _ => unreachable!("Invalid child index {child_idx} for ext_storage()"), |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + fn return_dtype(&self, _options: &Self::Options, arg_dtypes: &[DType]) -> VortexResult<DType> { |
| 60 | + let DType::Extension(ext_dtype) = &arg_dtypes[0] else { |
| 61 | + vortex_bail!("ext_storage() requires Extension, got {}", arg_dtypes[0]); |
| 62 | + }; |
| 63 | + |
| 64 | + Ok(ext_dtype.storage_dtype().clone()) |
| 65 | + } |
| 66 | + |
| 67 | + fn execute( |
| 68 | + &self, |
| 69 | + _options: &Self::Options, |
| 70 | + args: &dyn ExecutionArgs, |
| 71 | + ctx: &mut ExecutionCtx, |
| 72 | + ) -> VortexResult<ArrayRef> { |
| 73 | + let input = args.get(0)?; |
| 74 | + |
| 75 | + if !matches!(input.dtype(), DType::Extension(_)) { |
| 76 | + vortex_bail!("ext_storage() requires Extension, got {}", input.dtype()); |
| 77 | + } |
| 78 | + |
| 79 | + if let Some(scalar) = input.as_constant() { |
| 80 | + let storage_scalar = scalar.as_extension().to_storage_scalar(); |
| 81 | + return Ok(ConstantArray::new(storage_scalar, args.row_count()).into_array()); |
| 82 | + } |
| 83 | + |
| 84 | + let input = input.execute::<ExtensionArray>(ctx)?; |
| 85 | + Ok(input.storage_array().clone()) |
| 86 | + } |
| 87 | + |
| 88 | + fn validity( |
| 89 | + &self, |
| 90 | + _options: &Self::Options, |
| 91 | + expression: &Expression, |
| 92 | + ) -> VortexResult<Option<Expression>> { |
| 93 | + Ok(Some(expression.child(0).validity()?)) |
| 94 | + } |
| 95 | + |
| 96 | + fn is_null_sensitive(&self, _options: &Self::Options) -> bool { |
| 97 | + false |
| 98 | + } |
| 99 | + |
| 100 | + fn is_fallible(&self, _options: &Self::Options) -> bool { |
| 101 | + false |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +#[cfg(test)] |
| 106 | +mod tests { |
| 107 | + use vortex_buffer::buffer; |
| 108 | + use vortex_error::VortexResult; |
| 109 | + |
| 110 | + use crate::IntoArray; |
| 111 | + use crate::arrays::ConstantArray; |
| 112 | + use crate::arrays::ExtensionArray; |
| 113 | + use crate::arrays::PrimitiveArray; |
| 114 | + use crate::assert_arrays_eq; |
| 115 | + use crate::dtype::DType; |
| 116 | + use crate::dtype::Nullability; |
| 117 | + use crate::dtype::PType; |
| 118 | + use crate::dtype::extension::ExtDTypeRef; |
| 119 | + use crate::expr::ext_storage; |
| 120 | + use crate::expr::root; |
| 121 | + use crate::extension::datetime::TimeUnit; |
| 122 | + use crate::extension::datetime::Timestamp; |
| 123 | + use crate::scalar::Scalar; |
| 124 | + |
| 125 | + fn ext_dtype(nullability: Nullability) -> ExtDTypeRef { |
| 126 | + Timestamp::new(TimeUnit::Nanoseconds, nullability).erased() |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn extracts_extension_storage_array() -> VortexResult<()> { |
| 131 | + let storage = buffer![2i64, 4, 6].into_array(); |
| 132 | + let array = |
| 133 | + ExtensionArray::new(ext_dtype(Nullability::NonNullable), storage.clone()).into_array(); |
| 134 | + |
| 135 | + let result = array.apply(&ext_storage(root()))?; |
| 136 | + |
| 137 | + assert_eq!( |
| 138 | + result.dtype(), |
| 139 | + &DType::Primitive(PType::I64, Nullability::NonNullable) |
| 140 | + ); |
| 141 | + assert_arrays_eq!(result, storage); |
| 142 | + Ok(()) |
| 143 | + } |
| 144 | + |
| 145 | + #[test] |
| 146 | + fn extracts_nullable_extension_storage_array() -> VortexResult<()> { |
| 147 | + let storage = PrimitiveArray::from_option_iter([Some(2i64), None, Some(6)]).into_array(); |
| 148 | + let array = |
| 149 | + ExtensionArray::new(ext_dtype(Nullability::Nullable), storage.clone()).into_array(); |
| 150 | + |
| 151 | + let result = array.apply(&ext_storage(root()))?; |
| 152 | + |
| 153 | + assert_eq!( |
| 154 | + result.dtype(), |
| 155 | + &DType::Primitive(PType::I64, Nullability::Nullable) |
| 156 | + ); |
| 157 | + assert_arrays_eq!(result, storage); |
| 158 | + Ok(()) |
| 159 | + } |
| 160 | + |
| 161 | + #[test] |
| 162 | + fn extracts_constant_extension_storage_scalar() -> VortexResult<()> { |
| 163 | + let storage_scalar = Scalar::primitive(4i64, Nullability::NonNullable); |
| 164 | + let scalar = |
| 165 | + Scalar::extension_ref(ext_dtype(Nullability::NonNullable), storage_scalar.clone()); |
| 166 | + let array = ConstantArray::new(scalar, 3).into_array(); |
| 167 | + |
| 168 | + let result = array.apply(&ext_storage(root()))?; |
| 169 | + |
| 170 | + assert_eq!( |
| 171 | + result.dtype(), |
| 172 | + &DType::Primitive(PType::I64, Nullability::NonNullable) |
| 173 | + ); |
| 174 | + assert_arrays_eq!(result, ConstantArray::new(storage_scalar, 3)); |
| 175 | + Ok(()) |
| 176 | + } |
| 177 | + |
| 178 | + #[test] |
| 179 | + fn rejects_non_extension_input() { |
| 180 | + let dtype = DType::Primitive(PType::U64, Nullability::NonNullable); |
| 181 | + let err = ext_storage(root()).return_dtype(&dtype).unwrap_err(); |
| 182 | + assert!(err.to_string().contains("requires Extension")); |
| 183 | + } |
| 184 | + |
| 185 | + #[test] |
| 186 | + fn test_display() { |
| 187 | + assert_eq!(ext_storage(root()).to_string(), "vortex.ext.storage($)"); |
| 188 | + } |
| 189 | +} |
0 commit comments