|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
| 18 | +use crate::array::Array; |
18 | 19 | use crate::builder::ArrayBuilder; |
19 | 20 | use crate::{ArrayRef, FixedSizeBinaryArray}; |
20 | 21 | use arrow_buffer::Buffer; |
@@ -101,6 +102,23 @@ impl FixedSizeBinaryBuilder { |
101 | 102 | self.null_buffer_builder.append_n_nulls(n); |
102 | 103 | } |
103 | 104 |
|
| 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 | + |
104 | 122 | /// Returns the current values buffer as a slice |
105 | 123 | pub fn values_slice(&self) -> &[u8] { |
106 | 124 | self.values_builder.as_slice() |
@@ -270,4 +288,43 @@ mod tests { |
270 | 288 | fn test_fixed_size_binary_builder_invalid_value_length() { |
271 | 289 | let _ = FixedSizeBinaryBuilder::with_capacity(15, -1); |
272 | 290 | } |
| 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 | + } |
273 | 330 | } |
0 commit comments