|
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,19 @@ 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 | + for i in 0..array.len() { |
| 108 | + if array.is_null(i) { |
| 109 | + self.append_null(); |
| 110 | + } else { |
| 111 | + let value = array.value(i); |
| 112 | + self.append_value(value)?; |
| 113 | + } |
| 114 | + } |
| 115 | + Ok(()) |
| 116 | + } |
| 117 | + |
104 | 118 | /// Returns the current values buffer as a slice |
105 | 119 | pub fn values_slice(&self) -> &[u8] { |
106 | 120 | self.values_builder.as_slice() |
@@ -270,4 +284,23 @@ mod tests { |
270 | 284 | fn test_fixed_size_binary_builder_invalid_value_length() { |
271 | 285 | let _ = FixedSizeBinaryBuilder::with_capacity(15, -1); |
272 | 286 | } |
| 287 | + |
| 288 | + #[test] |
| 289 | + fn test_fixed_size_binary_builder_append_array() { |
| 290 | + let mut other_builder = FixedSizeBinaryBuilder::with_capacity(3, 5); |
| 291 | + other_builder.append_value(b"hello").unwrap(); |
| 292 | + other_builder.append_null(); |
| 293 | + other_builder.append_value(b"arrow").unwrap(); |
| 294 | + let other_array = other_builder.finish(); |
| 295 | + |
| 296 | + let mut builder = FixedSizeBinaryBuilder::with_capacity(3, 5); |
| 297 | + builder.append_array(&other_array).unwrap(); |
| 298 | + let array = builder.finish(); |
| 299 | + |
| 300 | + assert_eq!(&DataType::FixedSizeBinary(5), array.data_type()); |
| 301 | + assert_eq!(3, array.len()); |
| 302 | + assert_eq!(1, array.null_count()); |
| 303 | + assert_eq!(10, array.value_offset(2)); |
| 304 | + assert_eq!(5, array.value_length()); |
| 305 | + } |
273 | 306 | } |
0 commit comments