Skip to content

Commit 6348fca

Browse files
committed
more interesting test
Signed-off-by: Connor Tsui <[email protected]>
1 parent f8f4dce commit 6348fca

5 files changed

Lines changed: 221 additions & 20 deletions

File tree

‎vortex-array/src/extension/datetime/timestamp.rs‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,10 @@ impl fmt::Display for TimestampValue<'_> {
9999
let ts = jiff::Timestamp::UNIX_EPOCH + span;
100100

101101
match tz {
102-
None => {
103-
write!(f, "{}", ts)
104-
}
102+
None => write!(f, "{ts}"),
105103
Some(tz) => {
106-
write!(f, "{}", ts.in_tz(tz.as_ref()).map_err(|_| fmt::Error)?)
104+
let adjusted_ts = ts.in_tz(tz.as_ref()).vortex_expect("unknown timezone");
105+
write!(f, "{adjusted_ts}",)
107106
}
108107
}
109108
}
@@ -189,33 +188,38 @@ impl ExtVTable for Timestamp {
189188
storage_value: &'a ScalarValue,
190189
) -> VortexResult<Self::NativeValue<'a>> {
191190
let ts_value = storage_value.as_primitive().cast::<i64>()?;
191+
let tz = metadata.tz.as_ref();
192192

193-
// Validate the storage value is within the valid range for Timestamp.
194-
let span = match metadata.unit {
195-
TimeUnit::Nanoseconds => Span::new().nanoseconds(ts_value),
196-
TimeUnit::Microseconds => Span::new().microseconds(ts_value),
197-
TimeUnit::Milliseconds => Span::new().milliseconds(ts_value),
198-
TimeUnit::Seconds => Span::new().seconds(ts_value),
193+
let (span, value) = match metadata.unit {
194+
TimeUnit::Nanoseconds => (
195+
Span::new().nanoseconds(ts_value),
196+
TimestampValue::Nanoseconds(ts_value, tz),
197+
),
198+
TimeUnit::Microseconds => (
199+
Span::new().microseconds(ts_value),
200+
TimestampValue::Microseconds(ts_value, tz),
201+
),
202+
TimeUnit::Milliseconds => (
203+
Span::new().milliseconds(ts_value),
204+
TimestampValue::Milliseconds(ts_value, tz),
205+
),
206+
TimeUnit::Seconds => (
207+
Span::new().seconds(ts_value),
208+
TimestampValue::Seconds(ts_value, tz),
209+
),
199210
TimeUnit::Days => vortex_bail!("Timestamp does not support Days time unit"),
200211
};
201212

213+
// Validate the storage value is within the valid range for Timestamp.
202214
let ts = jiff::Timestamp::UNIX_EPOCH
203215
.checked_add(span)
204216
.map_err(|e| vortex_err!("Invalid timestamp scalar: {}", e))?;
205217

206-
if let Some(tz) = &metadata.tz {
218+
if let Some(tz) = tz {
207219
ts.in_tz(tz.as_ref())
208220
.map_err(|e| vortex_err!("Invalid timezone for timestamp scalar: {}", e))?;
209221
}
210222

211-
let tz = metadata.tz.as_ref();
212-
213-
match metadata.unit {
214-
TimeUnit::Nanoseconds => Ok(TimestampValue::Nanoseconds(ts_value, tz)),
215-
TimeUnit::Microseconds => Ok(TimestampValue::Microseconds(ts_value, tz)),
216-
TimeUnit::Milliseconds => Ok(TimestampValue::Milliseconds(ts_value, tz)),
217-
TimeUnit::Seconds => Ok(TimestampValue::Seconds(ts_value, tz)),
218-
TimeUnit::Days => unreachable!("Timestamp does not support Days time unit"),
219-
}
223+
Ok(value)
220224
}
221225
}

‎vortex-array/src/extension/mod.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use std::fmt;
77

88
pub mod datetime;
99

10+
#[cfg(test)]
11+
mod tests;
12+
1013
/// An empty metadata struct for extension dtypes that do not require any metadata.
1114
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1215
pub struct EmptyMetadata;
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
//! Test extension types for exercising the [`ExtVTable`] contract.
5+
6+
mod divisible_int;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ impl Eq for ExtScalarValueRef {}
111111

112112
impl PartialOrd for ExtScalarValueRef {
113113
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
114+
// TODO(connor): Should this check if the IDs are equal before ordering?
114115
self.0.storage_value().partial_cmp(other.0.storage_value())
115116
}
116117
}

0 commit comments

Comments
 (0)