|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +//! Regression tests for extension dtype nullability changes and debug output. |
| 5 | +
|
| 6 | +use std::sync::atomic::AtomicUsize; |
| 7 | +use std::sync::atomic::Ordering; |
| 8 | + |
| 9 | +use rstest::rstest; |
| 10 | +use vortex_error::VortexResult; |
| 11 | +use vortex_error::vortex_err; |
| 12 | +use vortex_session::registry::CachedId; |
| 13 | + |
| 14 | +use super::ExtDType; |
| 15 | +use super::ExtId; |
| 16 | +use super::ExtVTable; |
| 17 | +use crate::dtype::DType; |
| 18 | +use crate::dtype::Nullability; |
| 19 | +use crate::dtype::PType; |
| 20 | +use crate::extension::datetime::TimeUnit; |
| 21 | +use crate::extension::datetime::Timestamp; |
| 22 | +use crate::scalar::ScalarValue; |
| 23 | + |
| 24 | +/// The number of times [`CountingValidator::validate_dtype`] ran. |
| 25 | +static VALIDATIONS: AtomicUsize = AtomicUsize::new(0); |
| 26 | + |
| 27 | +/// An extension type that counts how often its dtype is validated. |
| 28 | +#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)] |
| 29 | +struct CountingValidator; |
| 30 | + |
| 31 | +impl ExtVTable for CountingValidator { |
| 32 | + type Metadata = u32; |
| 33 | + type NativeValue<'a> = &'a ScalarValue; |
| 34 | + |
| 35 | + fn id(&self) -> ExtId { |
| 36 | + static ID: CachedId = CachedId::new("test.counting_validator"); |
| 37 | + *ID |
| 38 | + } |
| 39 | + |
| 40 | + fn serialize_metadata(&self, metadata: &Self::Metadata) -> VortexResult<Vec<u8>> { |
| 41 | + Ok(metadata.to_le_bytes().to_vec()) |
| 42 | + } |
| 43 | + |
| 44 | + fn deserialize_metadata(&self, metadata: &[u8]) -> VortexResult<Self::Metadata> { |
| 45 | + let bytes = metadata |
| 46 | + .try_into() |
| 47 | + .map_err(|_| vortex_err!("Expected four metadata bytes, got {}", metadata.len()))?; |
| 48 | + |
| 49 | + Ok(u32::from_le_bytes(bytes)) |
| 50 | + } |
| 51 | + |
| 52 | + fn validate_dtype(_ext_dtype: &ExtDType<Self>) -> VortexResult<()> { |
| 53 | + VALIDATIONS.fetch_add(1, Ordering::Relaxed); |
| 54 | + Ok(()) |
| 55 | + } |
| 56 | + |
| 57 | + fn unpack_native<'a>( |
| 58 | + _ext_dtype: &'a ExtDType<Self>, |
| 59 | + storage_value: &'a ScalarValue, |
| 60 | + ) -> VortexResult<Self::NativeValue<'a>> { |
| 61 | + Ok(storage_value) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// One test owns `VALIDATIONS`, so test threads cannot race on the counter. |
| 66 | +#[test] |
| 67 | +fn test_nullability_changes_do_not_validate_again() -> VortexResult<()> { |
| 68 | + for initial in [Nullability::NonNullable, Nullability::Nullable] { |
| 69 | + VALIDATIONS.store(0, Ordering::Relaxed); |
| 70 | + let dtype = |
| 71 | + ExtDType::<CountingValidator>::try_new(42, DType::Primitive(PType::I64, initial))?; |
| 72 | + assert_eq!(VALIDATIONS.load(Ordering::Relaxed), 1); |
| 73 | + |
| 74 | + for target in [Nullability::Nullable, Nullability::NonNullable] { |
| 75 | + let changed = dtype.with_nullability(target); |
| 76 | + assert_eq!( |
| 77 | + changed.storage_dtype(), |
| 78 | + &DType::Primitive(PType::I64, target) |
| 79 | + ); |
| 80 | + assert_eq!(changed.metadata::<CountingValidator>(), &42); |
| 81 | + assert_eq!(changed.id(), dtype.id()); |
| 82 | + } |
| 83 | + assert_eq!(VALIDATIONS.load(Ordering::Relaxed), 1); |
| 84 | + } |
| 85 | + |
| 86 | + Ok(()) |
| 87 | +} |
| 88 | + |
| 89 | +#[rstest] |
| 90 | +#[case::compact(false)] |
| 91 | +#[case::pretty(true)] |
| 92 | +fn test_debug_preserves_metadata(#[case] pretty: bool) { |
| 93 | + let dtype = Timestamp::new_with_tz( |
| 94 | + TimeUnit::Milliseconds, |
| 95 | + Some("UTC".into()), |
| 96 | + Nullability::Nullable, |
| 97 | + ) |
| 98 | + .erased(); |
| 99 | + let output = if pretty { |
| 100 | + format!("{dtype:#?}") |
| 101 | + } else { |
| 102 | + format!("{dtype:?}") |
| 103 | + }; |
| 104 | + |
| 105 | + assert!(output.starts_with("ExtDType {"), "{output}"); |
| 106 | + assert!(output.contains("metadata: TimestampOptions {"), "{output}"); |
| 107 | + assert!(output.contains("unit: Milliseconds"), "{output}"); |
| 108 | + assert_eq!( |
| 109 | + output.contains("metadata: TimestampOptions {\n"), |
| 110 | + pretty, |
| 111 | + "{output}" |
| 112 | + ); |
| 113 | + assert_eq!(output.matches("UTC").count(), 1, "{output}"); |
| 114 | + assert!(!output.contains("Ok(())"), "{output}"); |
| 115 | +} |
0 commit comments