@@ -10,7 +10,7 @@ use builders::ListBuilder;
1010use vortex_buffer:: Buffer ;
1111use vortex_dtype:: { DType , NativePType , Nullability , PType } ;
1212use vortex_error:: { VortexExpect , VortexUnwrap } ;
13- use vortex_scalar:: arbitrary:: { random_decimal , random_scalar} ;
13+ use vortex_scalar:: arbitrary:: random_scalar;
1414use vortex_scalar:: { Scalar , match_each_decimal_value_type} ;
1515
1616use super :: {
@@ -47,90 +47,14 @@ fn split_number_into_parts(n: usize, parts: usize) -> Vec<usize> {
4747 . collect ( )
4848}
4949
50+ /// Creates a random array with a random number of chunks.
5051fn random_array ( u : & mut Unstructured , dtype : & DType , len : Option < usize > ) -> Result < ArrayRef > {
5152 let num_chunks = u. int_in_range ( 1 ..=3 ) ?;
5253 let chunk_lens = len. map ( |l| split_number_into_parts ( l, num_chunks) ) ;
5354 let mut chunks = ( 0 ..num_chunks)
5455 . map ( |i| {
5556 let chunk_len = chunk_lens. as_ref ( ) . map ( |c| c[ i] ) ;
56- match dtype {
57- DType :: Null => Ok ( NullArray :: new (
58- chunk_len
59- . map ( Ok )
60- . unwrap_or_else ( || u. int_in_range ( 0 ..=100 ) ) ?,
61- )
62- . into_array ( ) ) ,
63- DType :: Bool ( n) => random_bool ( u, * n, chunk_len) ,
64- DType :: Primitive ( ptype, n) => match ptype {
65- PType :: U8 => random_primitive :: < u8 > ( u, * n, chunk_len) ,
66- PType :: U16 => random_primitive :: < u16 > ( u, * n, chunk_len) ,
67- PType :: U32 => random_primitive :: < u32 > ( u, * n, chunk_len) ,
68- PType :: U64 => random_primitive :: < u64 > ( u, * n, chunk_len) ,
69- PType :: I8 => random_primitive :: < i8 > ( u, * n, chunk_len) ,
70- PType :: I16 => random_primitive :: < i16 > ( u, * n, chunk_len) ,
71- PType :: I32 => random_primitive :: < i32 > ( u, * n, chunk_len) ,
72- PType :: I64 => random_primitive :: < i64 > ( u, * n, chunk_len) ,
73- PType :: F16 => Ok ( random_primitive :: < u16 > ( u, * n, chunk_len) ?
74- . to_primitive ( )
75- . vortex_unwrap ( )
76- . reinterpret_cast ( PType :: F16 )
77- . into_array ( ) ) ,
78- PType :: F32 => random_primitive :: < f32 > ( u, * n, chunk_len) ,
79- PType :: F64 => random_primitive :: < f64 > ( u, * n, chunk_len) ,
80- } ,
81- DType :: Decimal ( decimal, n) => {
82- let elem_len = chunk_len. unwrap_or ( u. int_in_range ( 0 ..=20 ) ?) ;
83- match_each_decimal_value_type ! ( smallest_storage_type( decimal) , |DVT | {
84- let mut builder =
85- DecimalBuilder :: new:: <DVT >( decimal. precision( ) , decimal. scale( ) , * n) ;
86- for _i in 0 ..elem_len {
87- builder
88- . append_scalar_value( random_decimal( u, decimal) ?)
89- . vortex_unwrap( ) ;
90- }
91- Ok ( builder. finish( ) )
92- } )
93- }
94- DType :: Utf8 ( n) => random_string ( u, * n, chunk_len) ,
95- DType :: Binary ( n) => random_bytes ( u, * n, chunk_len) ,
96- DType :: Struct ( sdt, n) => {
97- let first_array = sdt
98- . fields ( )
99- . next ( )
100- . map ( |d| random_array ( u, & d, chunk_len) )
101- . transpose ( ) ?;
102- let resolved_len = first_array
103- . as_ref ( )
104- . map ( |a| a. len ( ) )
105- . or ( chunk_len)
106- . map ( Ok )
107- . unwrap_or_else ( || u. int_in_range ( 0 ..=100 ) ) ?;
108- let children = first_array
109- . into_iter ( )
110- . map ( Ok )
111- . chain (
112- sdt. fields ( )
113- . skip ( 1 )
114- . map ( |d| random_array ( u, & d, Some ( resolved_len) ) ) ,
115- )
116- . collect :: < Result < Vec < _ > > > ( ) ?;
117- Ok ( StructArray :: try_new (
118- sdt. names ( ) . clone ( ) ,
119- children,
120- resolved_len,
121- random_validity ( u, * n, resolved_len) ?,
122- )
123- . vortex_unwrap ( )
124- . into_array ( ) )
125- }
126- DType :: List ( ldt, n) => random_list ( u, ldt, * n, chunk_len) ,
127- DType :: FixedSizeList ( ..) => {
128- unimplemented ! ( "TODO(connor)[FixedSizeList]: Create canonical fixed-size list" )
129- }
130- DType :: Extension ( ..) => {
131- todo ! ( "Extension arrays are not implemented" )
132- }
133- }
57+ random_array_chunk ( u, dtype, chunk_len)
13458 } )
13559 . collect :: < Result < Vec < _ > > > ( ) ?;
13660
@@ -144,6 +68,93 @@ fn random_array(u: &mut Unstructured, dtype: &DType, len: Option<usize>) -> Resu
14468 }
14569}
14670
71+ /// Creates a random array chunk.
72+ fn random_array_chunk (
73+ u : & mut Unstructured < ' _ > ,
74+ dtype : & DType ,
75+ chunk_len : Option < usize > ,
76+ ) -> std:: result:: Result < Arc < dyn Array + ' static > , arbitrary:: Error > {
77+ match dtype {
78+ DType :: Null => Ok ( NullArray :: new (
79+ chunk_len
80+ . map ( Ok )
81+ . unwrap_or_else ( || u. int_in_range ( 0 ..=100 ) ) ?,
82+ )
83+ . into_array ( ) ) ,
84+ DType :: Bool ( n) => random_bool ( u, * n, chunk_len) ,
85+ DType :: Primitive ( ptype, n) => match ptype {
86+ PType :: U8 => random_primitive :: < u8 > ( u, * n, chunk_len) ,
87+ PType :: U16 => random_primitive :: < u16 > ( u, * n, chunk_len) ,
88+ PType :: U32 => random_primitive :: < u32 > ( u, * n, chunk_len) ,
89+ PType :: U64 => random_primitive :: < u64 > ( u, * n, chunk_len) ,
90+ PType :: I8 => random_primitive :: < i8 > ( u, * n, chunk_len) ,
91+ PType :: I16 => random_primitive :: < i16 > ( u, * n, chunk_len) ,
92+ PType :: I32 => random_primitive :: < i32 > ( u, * n, chunk_len) ,
93+ PType :: I64 => random_primitive :: < i64 > ( u, * n, chunk_len) ,
94+ PType :: F16 => Ok ( random_primitive :: < u16 > ( u, * n, chunk_len) ?
95+ . to_primitive ( )
96+ . vortex_unwrap ( )
97+ . reinterpret_cast ( PType :: F16 )
98+ . into_array ( ) ) ,
99+ PType :: F32 => random_primitive :: < f32 > ( u, * n, chunk_len) ,
100+ PType :: F64 => random_primitive :: < f64 > ( u, * n, chunk_len) ,
101+ } ,
102+ DType :: Decimal ( decimal, n) => {
103+ let elem_len = chunk_len. unwrap_or ( u. int_in_range ( 0 ..=20 ) ?) ;
104+ match_each_decimal_value_type ! ( smallest_storage_type( decimal) , |DVT | {
105+ let mut builder =
106+ DecimalBuilder :: new:: <DVT >( decimal. precision( ) , decimal. scale( ) , * n) ;
107+ for _i in 0 ..elem_len {
108+ let random_decimal = random_scalar( u, & DType :: Decimal ( * decimal, * n) ) ?;
109+ builder. append_scalar( & random_decimal) . vortex_expect(
110+ "was somehow unable to append a decimal to a decimal builder" ,
111+ ) ;
112+ }
113+ Ok ( builder. finish( ) )
114+ } )
115+ }
116+ DType :: Utf8 ( n) => random_string ( u, * n, chunk_len) ,
117+ DType :: Binary ( n) => random_bytes ( u, * n, chunk_len) ,
118+ DType :: Struct ( sdt, n) => {
119+ let first_array = sdt
120+ . fields ( )
121+ . next ( )
122+ . map ( |d| random_array ( u, & d, chunk_len) )
123+ . transpose ( ) ?;
124+ let resolved_len = first_array
125+ . as_ref ( )
126+ . map ( |a| a. len ( ) )
127+ . or ( chunk_len)
128+ . map ( Ok )
129+ . unwrap_or_else ( || u. int_in_range ( 0 ..=100 ) ) ?;
130+ let children = first_array
131+ . into_iter ( )
132+ . map ( Ok )
133+ . chain (
134+ sdt. fields ( )
135+ . skip ( 1 )
136+ . map ( |d| random_array ( u, & d, Some ( resolved_len) ) ) ,
137+ )
138+ . collect :: < Result < Vec < _ > > > ( ) ?;
139+ Ok ( StructArray :: try_new (
140+ sdt. names ( ) . clone ( ) ,
141+ children,
142+ resolved_len,
143+ random_validity ( u, * n, resolved_len) ?,
144+ )
145+ . vortex_unwrap ( )
146+ . into_array ( ) )
147+ }
148+ DType :: List ( ldt, n) => random_list ( u, ldt, * n, chunk_len) ,
149+ DType :: FixedSizeList ( ..) => {
150+ unimplemented ! ( "TODO(connor)[FixedSizeList]: Create canonical fixed-size list" )
151+ }
152+ DType :: Extension ( ..) => {
153+ todo ! ( "Extension arrays are not implemented" )
154+ }
155+ }
156+ }
157+
147158fn random_list (
148159 u : & mut Unstructured ,
149160 ldt : & Arc < DType > ,
0 commit comments