Skip to content

Commit a1d0943

Browse files
committed
feat(aggregate): register is_sorted and min_max for temporal extension types
Register ExtensionAggregate<IsSorted, E> and ExtensionAggregate<MinMax, E> in the default aggregate session for Date, Time and Timestamp, whose values order like their integer storage, through a new AggregateFnSession::register_storage_ordered helper. Remove IsSorted's extension branch: IsSorted rejects extension dtypes in return_dtype, so the branch was unreachable; extension inputs now go through the registered adapters. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hp7ZGEG8mrip6qkAtK4iWD Signed-off-by: Claude <[email protected]>
1 parent 3ce4231 commit a1d0943

4 files changed

Lines changed: 97 additions & 26 deletions

File tree

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ impl<A: AggregateFnVTable, E: ExtVTable> AggregateFnVTable for ExtensionAggregat
252252

253253
#[cfg(test)]
254254
mod tests {
255+
use rstest::rstest;
255256
use vortex_error::VortexResult;
256257

257258
use super::ExtensionAggregate;
@@ -261,13 +262,17 @@ mod tests {
261262
use crate::aggregate_fn::AggregateFnVTable;
262263
use crate::aggregate_fn::DynAccumulator;
263264
use crate::aggregate_fn::NumericalAggregateOpts;
265+
use crate::aggregate_fn::fns::is_sorted::IsSorted;
266+
use crate::aggregate_fn::fns::is_sorted::IsSortedOptions;
267+
use crate::aggregate_fn::fns::min_max::MinMax;
264268
use crate::aggregate_fn::fns::sum::Sum;
265269
use crate::aggregate_fn::session::AggregateFnSessionExt;
266270
use crate::arrays::ExtensionArray;
267271
use crate::arrays::PrimitiveArray;
268272
use crate::dtype::DType;
269273
use crate::dtype::Nullability;
270274
use crate::extension::datetime::Date;
275+
use crate::extension::datetime::Time;
271276
use crate::extension::datetime::TimeUnit;
272277
use crate::extension::datetime::Timestamp;
273278

@@ -350,4 +355,76 @@ mod tests {
350355
.is_none()
351356
);
352357
}
358+
359+
#[test]
360+
fn default_session_registers_temporal_order_aggregates() {
361+
let session = crate::array_session();
362+
let aggregate_fns = session.aggregate_fns();
363+
for id in [
364+
ExtensionAggregate::new(IsSorted, Date).id(),
365+
ExtensionAggregate::new(IsSorted, Time).id(),
366+
ExtensionAggregate::new(IsSorted, Timestamp).id(),
367+
ExtensionAggregate::new(MinMax, Date).id(),
368+
ExtensionAggregate::new(MinMax, Time).id(),
369+
ExtensionAggregate::new(MinMax, Timestamp).id(),
370+
] {
371+
assert!(aggregate_fns.find_plugin(&id).is_some(), "{id} is not registered");
372+
}
373+
}
374+
375+
#[rstest]
376+
#[case::sorted([Some(1_000), None, Some(2_000), Some(2_000)], false, true)]
377+
#[case::unsorted([Some(2_000), Some(1_000), None, Some(3_000)], false, false)]
378+
#[case::strict_with_duplicates([Some(1_000), Some(2_000), Some(2_000), None], true, false)]
379+
fn is_sorted_over_timestamps(
380+
#[case] values: [Option<i64>; 4],
381+
#[case] strict: bool,
382+
#[case] expected: bool,
383+
) -> VortexResult<()> {
384+
let session = crate::array_session();
385+
let mut ctx = session.create_execution_ctx();
386+
let ext_dtype = Timestamp::new(TimeUnit::Milliseconds, Nullability::Nullable).erased();
387+
let array = ExtensionArray::new(
388+
ext_dtype.clone(),
389+
PrimitiveArray::from_option_iter(values).into_array(),
390+
)
391+
.into_array();
392+
393+
let mut acc = Accumulator::try_new(
394+
ExtensionAggregate::new(IsSorted, Timestamp),
395+
IsSortedOptions { strict },
396+
DType::Extension(ext_dtype),
397+
)?;
398+
acc.accumulate(&array, &mut ctx)?;
399+
400+
assert_eq!(bool::try_from(&acc.finish()?)?, expected);
401+
Ok(())
402+
}
403+
404+
#[test]
405+
fn min_max_over_dates_matches_storage() -> VortexResult<()> {
406+
let session = crate::array_session();
407+
let mut ctx = session.create_execution_ctx();
408+
let ext_dtype = Date::new(TimeUnit::Days, Nullability::Nullable).erased();
409+
let storage = PrimitiveArray::from_option_iter([Some(20i32), None, Some(-3), Some(7)])
410+
.into_array();
411+
let array = ExtensionArray::new(ext_dtype.clone(), storage.clone()).into_array();
412+
413+
let mut acc = Accumulator::try_new(
414+
ExtensionAggregate::new(MinMax, Date),
415+
NumericalAggregateOpts::default(),
416+
DType::Extension(ext_dtype),
417+
)?;
418+
acc.accumulate(&array, &mut ctx)?;
419+
420+
let mut expected = Accumulator::try_new(
421+
MinMax,
422+
NumericalAggregateOpts::default(),
423+
storage.dtype().clone(),
424+
)?;
425+
expected.accumulate(&storage, &mut ctx)?;
426+
427+
assert_eq!(acc.finish()?, expected.finish()?);
428+
Ok(())
429+
}
353430
}

‎vortex-array/src/aggregate_fn/fns/is_sorted/extension.rs‎

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎vortex-array/src/aggregate_fn/fns/is_sorted/mod.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
mod bool;
55
mod decimal;
6-
mod extension;
76
mod primitive;
87
mod varbin;
98

@@ -18,7 +17,6 @@ use vortex_session::registry::CachedId;
1817

1918
use self::bool::check_bool_sorted;
2019
use self::decimal::check_decimal_sorted;
21-
use self::extension::check_extension_sorted;
2220
use self::primitive::check_primitive_sorted;
2321
use self::varbin::check_varbinview_sorted;
2422
use crate::ArrayRef;
@@ -514,9 +512,9 @@ impl AggregateFnVTable for IsSorted {
514512
check_varbinview_sorted(v, args.options.strict, ctx)?
515513
}
516514
Canonical::Decimal(d) => check_decimal_sorted(d, args.options.strict, ctx)?,
517-
Canonical::Extension(e) => check_extension_sorted(e, args.options.strict, ctx)?,
518515
Canonical::Null(_) => !args.options.strict,
519-
// Struct, List, FixedSizeList should have been filtered out by return_dtype
516+
// Struct, List, FixedSizeList and Extension should have been filtered out by
517+
// return_dtype
520518
_ => unreachable!(),
521519
};
522520

‎vortex-array/src/aggregate_fn/session.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::aggregate_fn::AggregateFnId;
1313
use crate::aggregate_fn::AggregateFnPluginRef;
1414
use crate::aggregate_fn::AggregateFnRef;
1515
use crate::aggregate_fn::AggregateFnVTable;
16+
use crate::aggregate_fn::ExtensionAggregate;
1617
use crate::aggregate_fn::fns::all_nan::AllNan;
1718
use crate::aggregate_fn::fns::all_non_distinct::AllNonDistinct;
1819
use crate::aggregate_fn::fns::all_non_nan::AllNonNan;
@@ -48,6 +49,10 @@ use crate::arrays::dict::compute::is_constant::DictIsConstantKernel;
4849
use crate::arrays::dict::compute::is_sorted::DictIsSortedKernel;
4950
use crate::arrays::dict::compute::min_max::DictMinMaxKernel;
5051
use crate::dtype::DType;
52+
use crate::dtype::extension::ExtVTable;
53+
use crate::extension::datetime::Date;
54+
use crate::extension::datetime::Time;
55+
use crate::extension::datetime::Timestamp;
5156

5257
/// Session state for aggregate functions and encoding-specific aggregate kernels.
5358
///
@@ -116,6 +121,12 @@ impl Default for AggregateFnSession {
116121
this.register(SumV2);
117122
this.register(UncompressedSizeInBytes);
118123

124+
// Temporal extension types order like their integer storage, so order-based aggregates
125+
// run on the storage.
126+
this.register_storage_ordered(Date);
127+
this.register_storage_ordered(Time);
128+
this.register_storage_ordered(Timestamp);
129+
119130
// Register the built-in aggregate kernels.
120131
this.register_aggregate_kernel(Chunked.id(), None::<AggregateFnId>, &ChunkedArrayAggregate);
121132
this.register_aggregate_kernel(Dict.id(), Some(MinMax.id()), &DictMinMaxKernel);
@@ -153,6 +164,13 @@ impl AggregateFnSession {
153164
self.registry.insert(id, pluginref);
154165
}
155166

167+
/// Register the order-based aggregates for an extension type `ext` whose values order like
168+
/// its storage, computing them over the storage through [`ExtensionAggregate`].
169+
pub fn register_storage_ordered<E: ExtVTable>(&self, ext: E) {
170+
self.register(ExtensionAggregate::new(IsSorted, ext.clone()));
171+
self.register(ExtensionAggregate::new(MinMax, ext));
172+
}
173+
156174
/// The default per-chunk zone statistics for a column of `input_dtype`, collected from every
157175
/// registered aggregate's `zone_stat_default`.
158176
///

0 commit comments

Comments
 (0)