@@ -6,6 +6,7 @@ mod decimal;
66mod extension;
77mod fixed_size_list;
88mod list;
9+ mod map;
910pub mod primitive;
1011mod struct_;
1112mod varbin;
@@ -20,6 +21,7 @@ use self::decimal::check_decimal_constant;
2021use self :: extension:: check_extension_constant;
2122use self :: fixed_size_list:: check_fixed_size_list_constant;
2223use self :: list:: check_listview_constant;
24+ use self :: map:: check_map_constant;
2325use self :: primitive:: check_primitive_constant;
2426use self :: struct_:: check_struct_constant;
2527use self :: varbin:: check_varbinview_constant;
@@ -402,9 +404,7 @@ impl AggregateFnVTable for IsConstant {
402404 Canonical :: Struct ( s) => check_struct_constant ( s, ctx) ?,
403405 Canonical :: Extension ( e) => check_extension_constant ( e, ctx) ?,
404406 Canonical :: List ( l) => check_listview_constant ( l, ctx) ?,
405- Canonical :: Map ( _) => {
406- vortex_bail ! ( "Map arrays don't support IsConstant" )
407- }
407+ Canonical :: Map ( m) => check_map_constant ( m, ctx) ?,
408408 Canonical :: FixedSizeList ( f) => check_fixed_size_list_constant ( f, ctx) ?,
409409 Canonical :: Null ( _) => true ,
410410 Canonical :: Union ( _) => {
@@ -456,14 +456,54 @@ mod tests {
456456 use crate :: arrays:: ListArray ;
457457 use crate :: arrays:: PrimitiveArray ;
458458 use crate :: arrays:: StructArray ;
459+ use crate :: builders:: MapBuilder ;
459460 use crate :: dtype:: DType ;
460461 use crate :: dtype:: DecimalDType ;
461462 use crate :: dtype:: FieldNames ;
463+ use crate :: dtype:: MapDType ;
462464 use crate :: dtype:: Nullability ;
463465 use crate :: dtype:: PType ;
464466 use crate :: expr:: stats:: Stat ;
467+ use crate :: scalar:: Scalar ;
465468 use crate :: validity:: Validity ;
466469
470+ type MapEntryFixture < ' a > = ( i32 , Option < & ' a str > ) ;
471+ type MapRowFixture < ' a > = Option < Vec < MapEntryFixture < ' a > > > ;
472+
473+ fn map_array_from_rows ( rows : & [ MapRowFixture < ' _ > ] ) -> VortexResult < crate :: ArrayRef > {
474+ let map_dtype = MapDType :: try_new (
475+ DType :: Primitive ( PType :: I32 , Nullability :: NonNullable ) ,
476+ DType :: Utf8 ( Nullability :: Nullable ) ,
477+ false ,
478+ ) ?;
479+ let dtype = DType :: Map ( map_dtype. clone ( ) , Nullability :: Nullable ) ;
480+ let mut builder =
481+ MapBuilder :: < u64 , u64 > :: with_capacity ( map_dtype, Nullability :: Nullable , rows. len ( ) ) ;
482+
483+ for row in rows {
484+ let scalar = match row {
485+ Some ( entries) => {
486+ let entries = entries
487+ . iter ( )
488+ . map ( |( key, value) | {
489+ let key = Scalar :: primitive ( * key, Nullability :: NonNullable ) ;
490+ let value = value. map_or_else (
491+ || Scalar :: null ( DType :: Utf8 ( Nullability :: Nullable ) ) ,
492+ |value| Scalar :: utf8 ( value, Nullability :: Nullable ) ,
493+ ) ;
494+ ( key, value)
495+ } )
496+ . collect :: < Vec < _ > > ( ) ;
497+ Scalar :: try_map ( dtype. clone ( ) , entries) ?
498+ }
499+ None => Scalar :: null ( dtype. clone ( ) ) ,
500+ } ;
501+ builder. append_value ( scalar. as_map ( ) ) ?;
502+ }
503+
504+ Ok ( builder. finish_into_map ( ) . into_array ( ) )
505+ }
506+
467507 // Tests migrated from compute/is_constant.rs
468508 #[ test]
469509 fn is_constant_min_max_no_nan ( ) -> VortexResult < ( ) > {
@@ -687,4 +727,26 @@ mod tests {
687727 assert_eq ! ( is_constant( & list_array. into_array( ) , & mut ctx) ?, expected) ;
688728 Ok ( ( ) )
689729 }
730+
731+ #[ test]
732+ fn test_map_is_constant ( ) -> VortexResult < ( ) > {
733+ let mut ctx = array_session ( ) . create_execution_ctx ( ) ;
734+
735+ let identical = map_array_from_rows ( & [
736+ Some ( vec ! [ ( 1 , Some ( "one" ) ) , ( 2 , None ) ] ) ,
737+ Some ( vec ! [ ( 1 , Some ( "one" ) ) , ( 2 , None ) ] ) ,
738+ ] ) ?;
739+ assert ! ( is_constant( & identical, & mut ctx) ?) ;
740+
741+ let different = map_array_from_rows ( & [
742+ Some ( vec ! [ ( 1 , Some ( "one" ) ) , ( 2 , None ) ] ) ,
743+ Some ( vec ! [ ( 1 , Some ( "one" ) ) , ( 3 , None ) ] ) ,
744+ ] ) ?;
745+ assert ! ( !is_constant( & different, & mut ctx) ?) ;
746+
747+ let all_null = map_array_from_rows ( & [ None , None ] ) ?;
748+ assert ! ( is_constant( & all_null, & mut ctx) ?) ;
749+
750+ Ok ( ( ) )
751+ }
690752}
0 commit comments