diff --git a/arrow-buffer/src/buffer/scalar.rs b/arrow-buffer/src/buffer/scalar.rs index ca1a1d230b12..f1c2ae785720 100644 --- a/arrow-buffer/src/buffer/scalar.rs +++ b/arrow-buffer/src/buffer/scalar.rs @@ -18,7 +18,7 @@ use crate::alloc::Deallocation; use crate::buffer::Buffer; use crate::native::ArrowNativeType; -use crate::{MutableBuffer, OffsetBuffer}; +use crate::{BufferBuilder, MutableBuffer, OffsetBuffer}; use std::fmt::Formatter; use std::marker::PhantomData; use std::ops::Deref; @@ -160,6 +160,13 @@ impl From> for ScalarBuffer { } } +impl From> for ScalarBuffer { + fn from(mut value: BufferBuilder) -> Self { + let len = value.len(); + Self::new(value.finish(), 0, len) + } +} + impl FromIterator for ScalarBuffer { fn from_iter>(iter: I) -> Self { iter.into_iter().collect::>().into() @@ -269,4 +276,12 @@ mod tests { let buffer = Buffer::from_iter([0_i32, 1, 2]); ScalarBuffer::::new(buffer, 0, usize::MAX / 4 + 1); } + + #[test] + fn convert_from_buffer_builder() { + let input = vec![1, 2, 3, 4]; + let buffer_builder = BufferBuilder::from(input.clone()); + let scalar_buffer = ScalarBuffer::from(buffer_builder); + assert_eq!(scalar_buffer.as_ref(), input); + } }