Skip to content

Commit 1e1469f

Browse files
committed
feat: implement append_array for FixedSizeBinaryBuilder
- Implemented append_array for the builder - extends from buffers in array - ensure value_length is compatible - To #8750 Signed-off-by: 蔡略 <cailue@apache.org>
1 parent c6cc7f8 commit 1e1469f

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

arrow-array/src/builder/fixed_size_binary_builder.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
use crate::array::Array;
1819
use crate::builder::ArrayBuilder;
1920
use crate::{ArrayRef, FixedSizeBinaryArray};
2021
use arrow_buffer::Buffer;
@@ -101,6 +102,23 @@ impl FixedSizeBinaryBuilder {
101102
self.null_buffer_builder.append_n_nulls(n);
102103
}
103104

105+
/// Appends all elements in array into the builder.
106+
pub fn append_array(&mut self, array: &FixedSizeBinaryArray) -> Result<(), ArrowError> {
107+
if self.value_length != array.value_length() {
108+
return Err(ArrowError::InvalidArgumentError(
109+
"Cannot append FixedSizeBinaryArray with different value length".to_string(),
110+
));
111+
}
112+
let buffer = array.value_data();
113+
self.values_builder.extend_from_slice(buffer);
114+
if let Some(validity) = array.nulls() {
115+
self.null_buffer_builder.append_buffer(validity);
116+
} else {
117+
self.null_buffer_builder.append_n_non_nulls(array.len());
118+
}
119+
Ok(())
120+
}
121+
104122
/// Returns the current values buffer as a slice
105123
pub fn values_slice(&self) -> &[u8] {
106124
self.values_builder.as_slice()
@@ -270,4 +288,43 @@ mod tests {
270288
fn test_fixed_size_binary_builder_invalid_value_length() {
271289
let _ = FixedSizeBinaryBuilder::with_capacity(15, -1);
272290
}
291+
292+
#[test]
293+
fn test_fixed_size_binary_builder_append_array() {
294+
let mut other_builder = FixedSizeBinaryBuilder::with_capacity(3, 5);
295+
other_builder.append_value(b"hello").unwrap();
296+
other_builder.append_null();
297+
other_builder.append_value(b"arrow").unwrap();
298+
let other_array = other_builder.finish();
299+
300+
let mut builder = FixedSizeBinaryBuilder::with_capacity(3, 5);
301+
builder.append_array(&other_array).unwrap();
302+
// Append again to test if breaks when appending multiple times
303+
builder.append_array(&other_array).unwrap();
304+
let array = builder.finish();
305+
306+
assert_eq!(array.value_length(), other_array.value_length());
307+
assert_eq!(&DataType::FixedSizeBinary(5), array.data_type());
308+
assert_eq!(6, array.len());
309+
assert_eq!(2, array.null_count());
310+
for i in 0..6 {
311+
assert_eq!(i * 5, array.value_offset(i as usize));
312+
}
313+
314+
assert_eq!(b"hello", array.value(0));
315+
assert_eq!(b"hello", array.value(3));
316+
assert_eq!(b"arrow", array.value(5));
317+
assert!(array.is_null(1));
318+
assert!(array.is_null(4));
319+
}
320+
321+
#[test]
322+
#[should_panic(expected = "Cannot append FixedSizeBinaryArray with different value length")]
323+
fn test_fixed_size_binary_builder_append_array_invalid_value_length() {
324+
let mut other_builder = FixedSizeBinaryBuilder::with_capacity(3, 4);
325+
other_builder.append_value(b"test").unwrap();
326+
let other_array = other_builder.finish();
327+
let mut builder = FixedSizeBinaryBuilder::with_capacity(3, 5);
328+
builder.append_array(&other_array).unwrap();
329+
}
273330
}

0 commit comments

Comments
 (0)