|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +//! A test extension type representing unsigned integers divisible by a given divisor. |
| 5 | + |
| 6 | +use std::fmt; |
| 7 | + |
| 8 | +use vortex_error::VortexExpect; |
| 9 | +use vortex_error::VortexResult; |
| 10 | +use vortex_error::vortex_bail; |
| 11 | +use vortex_error::vortex_ensure; |
| 12 | + |
| 13 | +use crate::dtype::DType; |
| 14 | +use crate::dtype::Nullability; |
| 15 | +use crate::dtype::PType; |
| 16 | +use crate::dtype::extension::ExtDType; |
| 17 | +use crate::dtype::extension::ExtId; |
| 18 | +use crate::dtype::extension::ExtVTable; |
| 19 | +use crate::scalar::ScalarValue; |
| 20 | + |
| 21 | +/// The divisor stored as extension metadata. |
| 22 | +#[derive(Clone, Debug, PartialEq, Eq, Hash)] |
| 23 | +pub struct Divisor(pub u64); |
| 24 | + |
| 25 | +impl fmt::Display for Divisor { |
| 26 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 27 | + write!(f, "divisible by {}", self.0) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/// Extension type for unsigned integers that must be divisible by the metadata divisor. |
| 32 | +#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] |
| 33 | +pub struct DivisibleInt; |
| 34 | + |
| 35 | +impl DivisibleInt { |
| 36 | + /// Creates a new divisible integer extension dtype. |
| 37 | + pub fn new(divisor: u64, nullability: Nullability) -> ExtDType<Self> { |
| 38 | + ExtDType::try_new(Divisor(divisor), DType::Primitive(PType::U64, nullability)) |
| 39 | + .vortex_expect("valid divisible int dtype") |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl ExtVTable for DivisibleInt { |
| 44 | + type Metadata = Divisor; |
| 45 | + type NativeValue<'a> = u64; |
| 46 | + |
| 47 | + fn id(&self) -> ExtId { |
| 48 | + ExtId::new_ref("test.divisible_int") |
| 49 | + } |
| 50 | + |
| 51 | + fn serialize_metadata(&self, metadata: &Self::Metadata) -> VortexResult<Vec<u8>> { |
| 52 | + Ok(metadata.0.to_le_bytes().to_vec()) |
| 53 | + } |
| 54 | + |
| 55 | + fn deserialize_metadata(&self, data: &[u8]) -> VortexResult<Self::Metadata> { |
| 56 | + vortex_ensure!(data.len() == 8, "divisible int metadata must be 8 bytes"); |
| 57 | + let bytes: [u8; 8] = data |
| 58 | + .try_into() |
| 59 | + .map_err(|_| vortex_error::vortex_err!("divisible int metadata must be 8 bytes"))?; |
| 60 | + let n = u64::from_le_bytes(bytes); |
| 61 | + vortex_ensure!(n > 0, "divisor must be greater than 0"); |
| 62 | + Ok(Divisor(n)) |
| 63 | + } |
| 64 | + |
| 65 | + fn validate_dtype( |
| 66 | + &self, |
| 67 | + _metadata: &Self::Metadata, |
| 68 | + storage_dtype: &DType, |
| 69 | + ) -> VortexResult<()> { |
| 70 | + vortex_ensure!( |
| 71 | + matches!(storage_dtype, DType::Primitive(PType::U64, _)), |
| 72 | + "divisible int storage dtype must be u64" |
| 73 | + ); |
| 74 | + Ok(()) |
| 75 | + } |
| 76 | + |
| 77 | + fn unpack_native( |
| 78 | + &self, |
| 79 | + metadata: &Self::Metadata, |
| 80 | + _storage_dtype: &DType, |
| 81 | + storage_value: &ScalarValue, |
| 82 | + ) -> VortexResult<Self::NativeValue<'_>> { |
| 83 | + let value = storage_value.as_primitive().cast::<u64>()?; |
| 84 | + if value % metadata.0 != 0 { |
| 85 | + vortex_bail!("{} is not divisible by {}", value, metadata.0); |
| 86 | + } |
| 87 | + Ok(value) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +#[cfg(test)] |
| 92 | +mod tests { |
| 93 | + use vortex_error::VortexResult; |
| 94 | + |
| 95 | + use super::DivisibleInt; |
| 96 | + use super::Divisor; |
| 97 | + use crate::dtype::DType; |
| 98 | + use crate::dtype::Nullability; |
| 99 | + use crate::dtype::PType; |
| 100 | + use crate::dtype::extension::ExtVTable; |
| 101 | + use crate::scalar::PValue; |
| 102 | + use crate::scalar::ScalarValue; |
| 103 | + use crate::scalar::extension::ExtScalarValue; |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn accepts_divisible_values() -> VortexResult<()> { |
| 107 | + let div7 = DivisibleInt::new(7, Nullability::NonNullable); |
| 108 | + |
| 109 | + for multiple in [0, 7, 14, 21, 7000] { |
| 110 | + let sv = ExtScalarValue::<DivisibleInt>::try_new( |
| 111 | + &div7, |
| 112 | + ScalarValue::Primitive(PValue::U64(multiple)), |
| 113 | + )?; |
| 114 | + assert_eq!( |
| 115 | + sv.storage_value(), |
| 116 | + &ScalarValue::Primitive(PValue::U64(multiple)) |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + Ok(()) |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn rejects_non_divisible_values() -> VortexResult<()> { |
| 125 | + let div7 = DivisibleInt::new(7, Nullability::NonNullable); |
| 126 | + |
| 127 | + for bad in [1, 2, 6, 8, 13, 15] { |
| 128 | + assert!( |
| 129 | + ExtScalarValue::<DivisibleInt>::try_new( |
| 130 | + &div7, |
| 131 | + ScalarValue::Primitive(PValue::U64(bad)), |
| 132 | + ) |
| 133 | + .is_err(), |
| 134 | + "{bad} should not be accepted as divisible by 7" |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + Ok(()) |
| 139 | + } |
| 140 | + |
| 141 | + #[test] |
| 142 | + fn metadata_roundtrip() -> VortexResult<()> { |
| 143 | + let vtable = DivisibleInt; |
| 144 | + let divisor = Divisor(42); |
| 145 | + |
| 146 | + let bytes = vtable.serialize_metadata(&divisor)?; |
| 147 | + let decoded = vtable.deserialize_metadata(&bytes)?; |
| 148 | + |
| 149 | + assert_eq!(decoded, divisor); |
| 150 | + Ok(()) |
| 151 | + } |
| 152 | + |
| 153 | + #[test] |
| 154 | + fn rejects_zero_divisor() { |
| 155 | + let vtable = DivisibleInt; |
| 156 | + let bytes = 0u64.to_le_bytes(); |
| 157 | + assert!(vtable.deserialize_metadata(&bytes).is_err()); |
| 158 | + } |
| 159 | + |
| 160 | + #[test] |
| 161 | + fn rejects_wrong_storage_dtype() { |
| 162 | + let vtable = DivisibleInt; |
| 163 | + let divisor = Divisor(10); |
| 164 | + |
| 165 | + assert!( |
| 166 | + vtable |
| 167 | + .validate_dtype( |
| 168 | + &divisor, |
| 169 | + &DType::Primitive(PType::I32, Nullability::NonNullable) |
| 170 | + ) |
| 171 | + .is_err() |
| 172 | + ); |
| 173 | + assert!( |
| 174 | + vtable |
| 175 | + .validate_dtype(&divisor, &DType::Utf8(Nullability::NonNullable)) |
| 176 | + .is_err() |
| 177 | + ); |
| 178 | + assert!( |
| 179 | + vtable |
| 180 | + .validate_dtype( |
| 181 | + &divisor, |
| 182 | + &DType::Primitive(PType::U64, Nullability::NonNullable) |
| 183 | + ) |
| 184 | + .is_ok() |
| 185 | + ); |
| 186 | + } |
| 187 | +} |
0 commit comments