Skip to content

Commit dbaf5f8

Browse files
authored
Decode DateTimeParts values in single pass (#10140)
Use LaneZip kernel to decode datetimeparts from components in single pass
1 parent 96ac9f2 commit dbaf5f8

5 files changed

Lines changed: 271 additions & 44 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎encodings/datetime-parts/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ num-traits = { workspace = true }
2020
prost = { workspace = true }
2121
vortex-array = { workspace = true }
2222
vortex-buffer = { workspace = true }
23+
vortex-compute = { workspace = true }
2324
vortex-error = { workspace = true }
2425
vortex-mask = { workspace = true }
2526
vortex-session = { workspace = true }

‎encodings/datetime-parts/src/canonical.rs‎

Lines changed: 225 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4+
use std::mem::MaybeUninit;
5+
46
use num_traits::AsPrimitive;
7+
use vortex_array::ArrayRef;
58
use vortex_array::ExecutionCtx;
69
use vortex_array::IntoArray;
710
use vortex_array::arrays::Primitive;
811
use vortex_array::arrays::PrimitiveArray;
912
use vortex_array::arrays::TemporalArray;
1013
use vortex_array::arrays::primitive::PrimitiveArrayExt;
14+
use vortex_array::builtins::ArrayBuiltins;
1115
use vortex_array::dtype::DType;
16+
use vortex_array::dtype::Nullability;
17+
use vortex_array::dtype::PType;
1218
use vortex_array::extension::datetime::TimeUnit;
1319
use vortex_array::extension::datetime::Timestamp;
1420
use vortex_array::match_each_integer_ptype;
1521
use vortex_buffer::BufferMut;
22+
use vortex_compute::lane_kernels::IndexedSource;
23+
use vortex_compute::lane_kernels::IndexedSourceExt;
24+
use vortex_compute::lane_kernels::LaneZip;
25+
use vortex_compute::lane_kernels::Repeat;
1626
use vortex_error::VortexExpect as _;
1727
use vortex_error::VortexResult;
1828
use vortex_error::vortex_panic;
1929

2030
use crate::array::DateTimePartsParts;
31+
use crate::timestamp::SECONDS_PER_DAY;
2132

22-
/// Decode [`DateTimePartsParts`] into a [`TemporalArray`].
33+
/// Decode a [`DateTimePartsParts`] back into a [`TemporalArray`].
34+
///
35+
/// The three components are combined in a single pass over the rows. Constant seconds or
36+
/// subseconds, which `split_temporal` produces for second-precision data, are folded into the
37+
/// kernel instead of materialized.
2338
pub fn decode_to_temporal(
2439
parts: DateTimePartsParts,
2540
dtype: &DType,
@@ -41,54 +56,51 @@ pub fn decode_to_temporal(
4156
TimeUnit::Days => vortex_panic!(InvalidArgument: "cannot decode into TimeUnit::D"),
4257
};
4358

44-
// Days is guaranteed Primitive by require_child.
4559
let days = parts.days.as_::<Primitive>();
4660
let validity = days.validity()?;
61+
let len = days.len();
4762

48-
let mut values: BufferMut<i64> = match_each_integer_ptype!(days.ptype(), |D| {
49-
BufferMut::from_iter(days.as_slice::<D>().iter().map(|d| {
50-
let d: i64 = d.as_();
51-
d * 86_400 * divisor
52-
}))
53-
});
63+
let seconds = TimePart::try_new(&parts.seconds, ctx)?;
64+
let subseconds = TimePart::try_new(&parts.subseconds, ctx)?;
5465

55-
// Seconds/subseconds may be Constant — handle the fast path.
56-
if let Some(seconds) = parts.seconds.as_constant() {
57-
let seconds = seconds
58-
.as_primitive()
59-
.as_::<i64>()
60-
.vortex_expect("non-nullable");
61-
let seconds = seconds * divisor;
62-
for v in values.iter_mut() {
63-
*v += seconds;
64-
}
65-
} else {
66-
let seconds_buf = parts.seconds.execute::<PrimitiveArray>(ctx)?;
67-
match_each_integer_ptype!(seconds_buf.ptype(), |S| {
68-
for (v, second) in values.iter_mut().zip(seconds_buf.as_slice::<S>()) {
69-
let second: i64 = second.as_();
70-
*v += second * divisor;
71-
}
72-
});
73-
}
66+
let mut values = BufferMut::<i64>::with_capacity_in(len, ctx.allocator().clone());
67+
let out = &mut values.spare_capacity_mut()[..len];
7468

75-
if let Some(subseconds) = parts.subseconds.as_constant() {
76-
let subseconds = subseconds
77-
.as_primitive()
78-
.as_::<i64>()
79-
.vortex_expect("non-nullable");
80-
for v in values.iter_mut() {
81-
*v += subseconds;
69+
match_each_integer_ptype!(days.ptype(), |D| {
70+
let days = days.as_slice::<D>();
71+
match (&seconds, &subseconds) {
72+
(TimePart::Constant(seconds), TimePart::Constant(subseconds)) => combine_parts(
73+
days,
74+
Repeat::new(*seconds, len),
75+
Repeat::new(*subseconds, len),
76+
divisor,
77+
out,
78+
),
79+
(TimePart::Constant(seconds), TimePart::Values(subseconds)) => combine_parts(
80+
days,
81+
Repeat::new(*seconds, len),
82+
subseconds.as_slice::<i32>(),
83+
divisor,
84+
out,
85+
),
86+
(TimePart::Values(seconds), TimePart::Constant(subseconds)) => combine_parts(
87+
days,
88+
seconds.as_slice::<i32>(),
89+
Repeat::new(*subseconds, len),
90+
divisor,
91+
out,
92+
),
93+
(TimePart::Values(seconds), TimePart::Values(subseconds)) => combine_parts(
94+
days,
95+
seconds.as_slice::<i32>(),
96+
subseconds.as_slice::<i32>(),
97+
divisor,
98+
out,
99+
),
82100
}
83-
} else {
84-
let subseconds_buf = parts.subseconds.execute::<PrimitiveArray>(ctx)?;
85-
match_each_integer_ptype!(subseconds_buf.ptype(), |S| {
86-
for (v, subsecond) in values.iter_mut().zip(subseconds_buf.as_slice::<S>()) {
87-
let subsecond: i64 = subsecond.as_();
88-
*v += subsecond;
89-
}
90-
});
91-
}
101+
});
102+
// SAFETY: `combine_parts` writes every lane of `out`, which spans exactly `len` items.
103+
unsafe { values.set_len(len) };
92104

93105
Ok(TemporalArray::new_timestamp(
94106
PrimitiveArray::new(values.freeze(), validity).into_array(),
@@ -97,15 +109,70 @@ pub fn decode_to_temporal(
97109
))
98110
}
99111

112+
/// A non-nullable integer component of every timestamp.
113+
enum TimePart {
114+
/// One value shared by every row.
115+
Constant(i64),
116+
/// A materialized `i32` column.
117+
Values(PrimitiveArray),
118+
}
119+
120+
impl TimePart {
121+
fn try_new(part: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<Self> {
122+
if let Some(constant) = part.as_constant() {
123+
return Ok(Self::Constant(
124+
constant
125+
.as_primitive()
126+
.as_::<i64>()
127+
.vortex_expect("non-nullable"),
128+
));
129+
}
130+
131+
// `split_temporal` produces `i32` parts, so this cast is normally a no-op. Other widths
132+
// are cast up front rather than instantiating the fused kernel for every width
133+
// combination.
134+
let values = part
135+
.cast(DType::Primitive(PType::I32, Nullability::NonNullable))?
136+
.execute::<PrimitiveArray>(ctx)?;
137+
Ok(Self::Values(values))
138+
}
139+
}
140+
141+
/// Writes `days * 86_400 * divisor + seconds * divisor + subseconds` for every lane of `out`.
142+
fn combine_parts<D, S, U>(
143+
days: &[D],
144+
seconds: S,
145+
subseconds: U,
146+
divisor: i64,
147+
out: &mut [MaybeUninit<i64>],
148+
) where
149+
D: Copy + AsPrimitive<i64>,
150+
S: IndexedSource,
151+
S::Item: AsPrimitive<i64>,
152+
U: IndexedSource,
153+
U::Item: AsPrimitive<i64>,
154+
{
155+
let day_scale = SECONDS_PER_DAY * divisor;
156+
LaneZip::new(LaneZip::new(days, seconds), subseconds).map_into(
157+
out,
158+
|((day, second), subsecond)| {
159+
day.as_() * day_scale + second.as_() * divisor + subsecond.as_()
160+
},
161+
);
162+
}
163+
100164
#[cfg(test)]
101165
mod test {
102166
use std::sync::LazyLock;
103167

104168
use rstest::rstest;
169+
use vortex_array::Canonical;
105170
use vortex_array::IntoArray;
106171
use vortex_array::VortexSessionExecute;
172+
use vortex_array::arrays::ConstantArray;
107173
use vortex_array::arrays::PrimitiveArray;
108174
use vortex_array::arrays::TemporalArray;
175+
use vortex_array::arrays::extension::ExtensionArrayExt;
109176
use vortex_array::assert_arrays_eq;
110177
use vortex_array::extension::datetime::TimeUnit;
111178
use vortex_array::validity::Validity;
@@ -124,6 +191,19 @@ mod test {
124191
session
125192
});
126193

194+
/// Executes a datetime-parts array and returns its timestamp storage values.
195+
fn execute_storage(
196+
array: vortex_array::ArrayRef,
197+
ctx: &mut vortex_array::ExecutionCtx,
198+
) -> VortexResult<PrimitiveArray> {
199+
array
200+
.execute::<Canonical>(ctx)?
201+
.into_extension()
202+
.storage_array()
203+
.clone()
204+
.execute::<PrimitiveArray>(ctx)
205+
}
206+
127207
#[rstest]
128208
#[case(Validity::NonNullable)]
129209
#[case(Validity::AllValid)]
@@ -171,4 +251,106 @@ mod test {
171251
assert_arrays_eq!(primitive_values, milliseconds, &mut ctx);
172252
Ok(())
173253
}
254+
255+
#[rstest]
256+
#[case::seconds(TimeUnit::Seconds)]
257+
#[case::milliseconds(TimeUnit::Milliseconds)]
258+
#[case::nanoseconds(TimeUnit::Nanoseconds)]
259+
fn decode_round_trips_many_rows(#[case] unit: TimeUnit) -> VortexResult<()> {
260+
let divisor: i64 = match unit {
261+
TimeUnit::Seconds => 1,
262+
TimeUnit::Milliseconds => 1_000,
263+
TimeUnit::Microseconds => 1_000_000,
264+
TimeUnit::Nanoseconds => 1_000_000_000,
265+
TimeUnit::Days => unreachable!(),
266+
};
267+
// Cover several days, both signs, and lengths that leave a partial chunk.
268+
let timestamps = PrimitiveArray::from_iter(
269+
(-1_500i64..1_500).map(|i| i * 7_919 * divisor + (i.rem_euclid(97)) * (divisor / 97)),
270+
);
271+
let mut ctx = SESSION.create_execution_ctx();
272+
let date_times = DateTimeParts::try_from_temporal(
273+
TemporalArray::new_timestamp(timestamps.clone().into_array(), unit, None),
274+
&mut ctx,
275+
)?;
276+
277+
let decoded = execute_storage(date_times.into_array(), &mut ctx)?;
278+
assert_arrays_eq!(decoded, timestamps, &mut ctx);
279+
Ok(())
280+
}
281+
282+
#[rstest]
283+
#[case::constant_seconds(true, false)]
284+
#[case::constant_subseconds(false, true)]
285+
#[case::both_constant(true, true)]
286+
fn decode_folds_constant_parts(
287+
#[case] constant_seconds: bool,
288+
#[case] constant_subseconds: bool,
289+
) -> VortexResult<()> {
290+
let mut ctx = SESSION.create_execution_ctx();
291+
const LEN: i32 = 200;
292+
let len = LEN as usize;
293+
let days = PrimitiveArray::from_iter((0..LEN).map(|i| i - 100));
294+
let seconds = if constant_seconds {
295+
ConstantArray::new(3_600i32, len).into_array()
296+
} else {
297+
PrimitiveArray::from_iter((0..LEN).map(|i| i * 400)).into_array()
298+
};
299+
let subseconds = if constant_subseconds {
300+
ConstantArray::new(250i32, len).into_array()
301+
} else {
302+
PrimitiveArray::from_iter((0..LEN).map(|i| i * 3)).into_array()
303+
};
304+
305+
let expected = PrimitiveArray::from_iter((0..i64::from(LEN)).map(|i| {
306+
let seconds = if constant_seconds { 3_600 } else { i * 400 };
307+
let subseconds = if constant_subseconds { 250 } else { i * 3 };
308+
(i - 100) * 86_400_000 + seconds * 1_000 + subseconds
309+
}));
310+
let dtype = TemporalArray::new_timestamp(
311+
expected.clone().into_array(),
312+
TimeUnit::Milliseconds,
313+
None,
314+
)
315+
.dtype()
316+
.clone();
317+
318+
let decoded = execute_storage(
319+
DateTimeParts::try_new(dtype, days.into_array(), seconds, subseconds)?.into_array(),
320+
&mut ctx,
321+
)?;
322+
assert_arrays_eq!(decoded, expected, &mut ctx);
323+
Ok(())
324+
}
325+
326+
#[test]
327+
fn decode_casts_narrow_parts() -> VortexResult<()> {
328+
let mut ctx = SESSION.create_execution_ctx();
329+
let days = PrimitiveArray::from_iter([0i16, 1, -1]);
330+
let seconds = PrimitiveArray::from_iter([5u8, 6, 7]);
331+
let subseconds = PrimitiveArray::from_iter([1i64, 2, 3]);
332+
333+
let expected =
334+
PrimitiveArray::from_iter([5_001i64, 86_400_000 + 6_002, -86_400_000 + 7_003]);
335+
let dtype = TemporalArray::new_timestamp(
336+
expected.clone().into_array(),
337+
TimeUnit::Milliseconds,
338+
None,
339+
)
340+
.dtype()
341+
.clone();
342+
343+
let decoded = execute_storage(
344+
DateTimeParts::try_new(
345+
dtype,
346+
days.into_array(),
347+
seconds.into_array(),
348+
subseconds.into_array(),
349+
)?
350+
.into_array(),
351+
&mut ctx,
352+
)?;
353+
assert_arrays_eq!(decoded, expected, &mut ctx);
354+
Ok(())
355+
}
174356
}

‎vortex-compute/src/lane_kernels/mod.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//!
1212
//! The module is split into:
1313
//!
14-
//! - [`source`] — the [`IndexedSource`] trait, [`LaneZip`], and read-only adapters.
14+
//! - [`source`] — the [`IndexedSource`] trait, [`LaneZip`], [`Repeat`], and read-only adapters.
1515
//! - [`sink`] — the [`IndexedSink`] trait and [`ReinterpretSink`].
1616
//! - [`map_into`] — out-of-place kernels via [`IndexedSourceExt`] (writes into a
1717
//! caller-provided `&mut [MaybeUninit<R>]`).
@@ -39,6 +39,7 @@ pub use sink::IndexedSink;
3939
pub use sink::ReinterpretSink;
4040
pub use source::IndexedSource;
4141
pub use source::LaneZip;
42+
pub use source::Repeat;
4243

4344
/// Loop-tiling chunk length for the **no-mask** kernels ([`IndexedSourceExt::map_into`],
4445
/// [`IndexedSourceExt::try_map_into`], [`IndexedSinkExt::map_into_in_place`]).

0 commit comments

Comments
 (0)