@@ -456,15 +456,20 @@ impl ValidityVTable<Sequence> for Sequence {
456456pub struct Sequence ;
457457
458458impl Sequence {
459- fn stats ( multiplier : PValue ) -> StatsSet {
460- // A sequence A[i] = base + i * multiplier is sorted iff multiplier >= 0,
461- // and strictly sorted iff multiplier > 0.
462- let ( is_sorted, is_strict_sorted) = match_each_pvalue ! (
463- multiplier,
464- uint: |v| { ( true , v > 0 ) } ,
465- int: |v| { ( v >= 0 , v > 0 ) } ,
466- float: |_v| { unreachable!( "float multiplier not supported" ) }
467- ) ;
459+ fn stats ( multiplier : PValue , length : usize ) -> StatsSet {
460+ // A sequence A[i] = base + i * multiplier with at least two elements is sorted iff
461+ // multiplier >= 0, and strictly sorted iff multiplier > 0. A single element has no
462+ // adjacent pair to compare, so it is strictly sorted whatever the multiplier.
463+ let ( is_sorted, is_strict_sorted) = if length <= 1 {
464+ ( true , true )
465+ } else {
466+ match_each_pvalue ! (
467+ multiplier,
468+ uint: |v| { ( true , v > 0 ) } ,
469+ int: |v| { ( v >= 0 , v > 0 ) } ,
470+ float: |_v| { unreachable!( "float multiplier not supported" ) }
471+ )
472+ } ;
468473
469474 // SAFETY: we don't have duplicate stats.
470475 unsafe {
@@ -495,7 +500,7 @@ impl Sequence {
495500 let dtype = DType :: Primitive ( ptype, nullability) ;
496501 let ( base, multiplier) = SequenceData :: normalize ( base, multiplier, ptype)
497502 . vortex_expect ( "SequenceArray parts must be representable in the output ptype" ) ;
498- let stats = Self :: stats ( multiplier) ;
503+ let stats = Self :: stats ( multiplier, length ) ;
499504 let data = unsafe { SequenceData :: new_unchecked ( base, multiplier) } ;
500505 unsafe {
501506 Array :: from_parts_unchecked ( ArrayParts :: new (
@@ -519,7 +524,7 @@ impl Sequence {
519524 ) -> VortexResult < SequenceArray > {
520525 let dtype = DType :: Primitive ( ptype, nullability) ;
521526 let data = SequenceData :: try_new ( base, multiplier, ptype, nullability, length) ?;
522- let stats = Self :: stats ( data. multiplier ( ) ) ;
527+ let stats = Self :: stats ( data. multiplier ( ) , length ) ;
523528 Ok ( unsafe {
524529 Array :: from_parts_unchecked ( ArrayParts :: new (
525530 Sequence ,
@@ -542,7 +547,7 @@ impl Sequence {
542547 let ptype = T :: PTYPE ;
543548 let dtype = DType :: Primitive ( ptype, nullability) ;
544549 let data = SequenceData :: try_new_typed ( base, multiplier, nullability, length) ?;
545- let stats = Self :: stats ( data. multiplier ( ) ) ;
550+ let stats = Self :: stats ( data. multiplier ( ) , length ) ;
546551 Ok ( unsafe {
547552 Array :: from_parts_unchecked ( ArrayParts :: new (
548553 Sequence ,
@@ -687,6 +692,67 @@ mod tests {
687692 Ok ( ( ) )
688693 }
689694
695+ #[ rstest]
696+ fn singleton_is_strict_sorted ( #[ values( -1i64 , 0 , 3 ) ] multiplier : i64 ) -> VortexResult < ( ) > {
697+ let typed = Sequence :: try_new_typed ( 10i64 , multiplier, Nullability :: NonNullable , 1 ) ?;
698+ let untyped = Sequence :: try_new (
699+ PValue :: from ( 10i64 ) ,
700+ PValue :: from ( multiplier) ,
701+ PType :: I64 ,
702+ Nullability :: NonNullable ,
703+ 1 ,
704+ ) ?;
705+ let sliced = Sequence :: try_new_typed ( 10i64 , multiplier, Nullability :: NonNullable , 4 ) ?
706+ . into_array ( )
707+ . slice ( 1 ..2 ) ?;
708+ assert ! ( sliced. is:: <Sequence >( ) ) ;
709+
710+ for array in [ typed. into_array ( ) , untyped. into_array ( ) , sliced] {
711+ for stat in [ Stat :: IsSorted , Stat :: IsStrictSorted ] {
712+ let value = array
713+ . statistics ( )
714+ . with_typed_stats_set ( |s| s. get_as :: < bool > ( stat) ) ;
715+ assert_eq ! ( value, StatPrecision :: Exact ( true ) ) ;
716+ }
717+ }
718+ Ok ( ( ) )
719+ }
720+
721+ #[ test]
722+ fn decode_keeps_singleton_sorted_over_stale_hints ( ) -> VortexResult < ( ) > {
723+ let array = Sequence :: try_new_typed ( 10i64 , -1 , Nullability :: NonNullable , 1 ) ?. into_array ( ) ;
724+ // Older writers stored the multiplier-only answer for one-row sequences.
725+ array
726+ . statistics ( )
727+ . set ( Stat :: IsSorted , StatPrecision :: Exact ( false . into ( ) ) ) ;
728+ array
729+ . statistics ( )
730+ . set ( Stat :: IsStrictSorted , StatPrecision :: Exact ( false . into ( ) ) ) ;
731+
732+ let dtype = array. dtype ( ) . clone ( ) ;
733+ let ctx = ArrayContext :: empty ( ) ;
734+ let serialized = array. serialize ( & ctx, & SESSION , & SerializeOptions :: default ( ) ) ?;
735+ let mut concat = ByteBufferMut :: empty ( ) ;
736+ for buf in serialized {
737+ concat. extend_from_slice ( buf. as_ref ( ) ) ;
738+ }
739+
740+ let decoded = SerializedArray :: try_from ( concat. freeze ( ) ) ?. decode (
741+ & dtype,
742+ 1 ,
743+ & ReadContext :: new ( ctx. to_ids ( ) ) ,
744+ & SESSION ,
745+ ) ?;
746+
747+ for stat in [ Stat :: IsSorted , Stat :: IsStrictSorted ] {
748+ let value = decoded
749+ . statistics ( )
750+ . with_typed_stats_set ( |s| s. get_as :: < bool > ( stat) ) ;
751+ assert_eq ! ( value, StatPrecision :: Exact ( true ) ) ;
752+ }
753+ Ok ( ( ) )
754+ }
755+
690756 // This is regression test for an issue caught by the fuzzer, where SequenceArrays with
691757 // multiplier > i64::MAX were unable to be constructed.
692758 #[ test]
0 commit comments