From 5dae1206847585f1d89c00d9146ca9e88e50b605 Mon Sep 17 00:00:00 2001 From: Ben Chambers Date: Fri, 27 Aug 2021 20:00:29 -0700 Subject: [PATCH 1/3] stub out impl --- arrow/src/array/builder.rs | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/arrow/src/array/builder.rs b/arrow/src/array/builder.rs index 8065f16ade76..c0b58e4e6456 100644 --- a/arrow/src/array/builder.rs +++ b/arrow/src/array/builder.rs @@ -261,6 +261,20 @@ impl BufferBuilder { self.len += slice.len(); } + #[inline] + pub fn append_trusted_len_iter(&mut self, iter: impl IntoIterator) { + let iter = iter.into_iter(); + let len = iter + .size_hint() + .1 + .expect("append_trusted_len_iter expects upper bound"); + self.reserve(len); + for v in iter { + self.buffer.push(v) + } + self.len += len; + } + /// Resets this builder and returns an immutable [`Buffer`](crate::buffer::Buffer). /// /// # Example: @@ -695,6 +709,14 @@ impl PrimitiveBuilder { Ok(()) } + #[inline] + pub fn append_nulls(&mut self, n: usize) -> Result<()> { + self.materialize_bitmap_builder(); + self.bitmap_builder.as_mut().unwrap().append_n(n, false); + self.values_builder.advance(n); + Ok(()) + } + /// Appends an `Option` into the builder #[inline] pub fn append_option(&mut self, v: Option) -> Result<()> { @@ -737,6 +759,25 @@ impl PrimitiveBuilder { Ok(()) } + /// Appends values from a trusted length iterator. + #[inline] + pub fn append_trusted_len_iter( + &mut self, + iter: impl IntoIterator, + ) -> Result<()> { + let mut iter = iter.into_iter(); + let len = iter + .size_hint() + .1 + .expect("append_trusted_len_iter requires an upper bound"); + + if let Some(b) = self.bitmap_builder.as_mut() { + b.append_n(len, true); + } + self.values_builder.append_trusted_len_iter(iter); + Ok(()) + } + /// Builds the `PrimitiveArray` and reset this builder. pub fn finish(&mut self) -> PrimitiveArray { let len = self.len(); From c538859eba87c1195acf15d17d6191d628957cb4 Mon Sep 17 00:00:00 2001 From: Ben Chambers Date: Fri, 27 Aug 2021 20:18:57 -0700 Subject: [PATCH 2/3] mark unsafe --- arrow/src/array/builder.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/arrow/src/array/builder.rs b/arrow/src/array/builder.rs index c0b58e4e6456..db3a839394bc 100644 --- a/arrow/src/array/builder.rs +++ b/arrow/src/array/builder.rs @@ -261,8 +261,11 @@ impl BufferBuilder { self.len += slice.len(); } + /// # Safety + /// This requires the iterator be a trusted length. This could instead require + /// the iterator implement `TrustedLen` once that is stabilized. #[inline] - pub fn append_trusted_len_iter(&mut self, iter: impl IntoIterator) { + pub unsafe fn append_trusted_len_iter(&mut self, iter: impl IntoIterator) { let iter = iter.into_iter(); let len = iter .size_hint() @@ -760,12 +763,16 @@ impl PrimitiveBuilder { } /// Appends values from a trusted length iterator. + /// + /// # Safety + /// This requires the iterator be a trusted length. This could instead require + /// the iterator implement `TrustedLen` once that is stabilized. #[inline] - pub fn append_trusted_len_iter( + pub unsafe fn append_trusted_len_iter( &mut self, iter: impl IntoIterator, ) -> Result<()> { - let mut iter = iter.into_iter(); + let iter = iter.into_iter(); let len = iter .size_hint() .1 From f31e3a1b1ae35a2156d5ed9d7588216069ae13c6 Mon Sep 17 00:00:00 2001 From: Ben Chambers Date: Sun, 29 Aug 2021 14:59:44 -0700 Subject: [PATCH 3/3] add tests --- arrow/src/array/builder.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/arrow/src/array/builder.rs b/arrow/src/array/builder.rs index db3a839394bc..8139b798b945 100644 --- a/arrow/src/array/builder.rs +++ b/arrow/src/array/builder.rs @@ -2787,6 +2787,35 @@ mod tests { } } + #[test] + fn test_primitive_array_builder_i32_append_iter() { + let mut builder = Int32Array::builder(5); + unsafe { builder.append_trusted_len_iter(0..5) }.unwrap(); + let arr = builder.finish(); + assert_eq!(5, arr.len()); + assert_eq!(0, arr.offset()); + assert_eq!(0, arr.null_count()); + for i in 0..5 { + assert!(!arr.is_null(i)); + assert!(arr.is_valid(i)); + assert_eq!(i as i32, arr.value(i)); + } + } + + #[test] + fn test_primitive_array_builder_i32_append_nulls() { + let mut builder = Int32Array::builder(5); + builder.append_nulls(5).unwrap(); + let arr = builder.finish(); + assert_eq!(5, arr.len()); + assert_eq!(0, arr.offset()); + assert_eq!(5, arr.null_count()); + for i in 0..5 { + assert!(arr.is_null(i)); + assert!(!arr.is_valid(i)); + } + } + #[test] fn test_primitive_array_builder_date32() { let mut builder = Date32Array::builder(5);