@@ -252,6 +252,28 @@ where
252252 }
253253 }
254254
255+ /// Append a value multiple times to the array.
256+ /// This is the same as [`Self::append`] but allows to append the same value multiple times without doing multiple lookups.
257+ ///
258+ /// Returns an error if the new index would overflow the key type.
259+ pub fn append_n (
260+ & mut self ,
261+ value : impl AsRef < [ u8 ] > ,
262+ count : usize ,
263+ ) -> Result < K :: Native , ArrowError > {
264+ if self . byte_width != value. as_ref ( ) . len ( ) as i32 {
265+ Err ( ArrowError :: InvalidArgumentError ( format ! (
266+ "Invalid input length passed to FixedSizeBinaryBuilder. Expected {} got {}" ,
267+ self . byte_width,
268+ value. as_ref( ) . len( )
269+ ) ) )
270+ } else {
271+ let key = self . get_or_insert_key ( value) ?;
272+ self . keys_builder . append_value_n ( key, count) ;
273+ Ok ( key)
274+ }
275+ }
276+
255277 /// Appends a null slot into the builder
256278 #[ inline]
257279 pub fn append_null ( & mut self ) {
@@ -401,6 +423,39 @@ mod tests {
401423 assert_eq ! ( ava. value( 1 ) , values[ 1 ] . as_bytes( ) ) ;
402424 }
403425
426+ #[ test]
427+ fn test_fixed_size_dictionary_builder_append_n ( ) {
428+ let values = [ "abc" , "def" ] ;
429+ let mut b = FixedSizeBinaryDictionaryBuilder :: < Int8Type > :: new ( 3 ) ;
430+ assert_eq ! ( b. append_n( values[ 0 ] , 2 ) . unwrap( ) , 0 ) ;
431+ assert_eq ! ( b. append_n( values[ 1 ] , 3 ) . unwrap( ) , 1 ) ;
432+ assert_eq ! ( b. append_n( values[ 0 ] , 2 ) . unwrap( ) , 0 ) ;
433+ let array = b. finish ( ) ;
434+
435+ assert_eq ! (
436+ array. keys( ) ,
437+ & Int8Array :: from( vec![
438+ Some ( 0 ) ,
439+ Some ( 0 ) ,
440+ Some ( 1 ) ,
441+ Some ( 1 ) ,
442+ Some ( 1 ) ,
443+ Some ( 0 ) ,
444+ Some ( 0 ) ,
445+ ] ) ,
446+ ) ;
447+
448+ // Values are polymorphic and so require a downcast.
449+ let ava = array
450+ . values ( )
451+ . as_any ( )
452+ . downcast_ref :: < FixedSizeBinaryArray > ( )
453+ . unwrap ( ) ;
454+
455+ assert_eq ! ( ava. value( 0 ) , values[ 0 ] . as_bytes( ) ) ;
456+ assert_eq ! ( ava. value( 1 ) , values[ 1 ] . as_bytes( ) ) ;
457+ }
458+
404459 #[ test]
405460 fn test_fixed_size_dictionary_builder_wrong_size ( ) {
406461 let mut b = FixedSizeBinaryDictionaryBuilder :: < Int8Type > :: new ( 3 ) ;
@@ -414,6 +469,11 @@ mod tests {
414469 err,
415470 "Invalid argument error: Invalid input length passed to FixedSizeBinaryBuilder. Expected 3 got 0"
416471 ) ;
472+ let err = b. append_n ( "a" , 3 ) . unwrap_err ( ) . to_string ( ) ;
473+ assert_eq ! (
474+ err,
475+ "Invalid argument error: Invalid input length passed to FixedSizeBinaryBuilder. Expected 3 got 1"
476+ ) ;
417477 }
418478
419479 #[ test]
0 commit comments