Skip to content

Commit b6d469d

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 b6d469d

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

arrow-array/src/builder/fixed_size_binary_builder.rs

Lines changed: 54 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,40 @@ 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+
let array = builder.finish();
303+
304+
assert_eq!(array.value_length(), other_array.value_length());
305+
assert_eq!(&DataType::FixedSizeBinary(5), array.data_type());
306+
assert_eq!(3, array.len());
307+
assert_eq!(1, array.null_count());
308+
assert_eq!(0, array.value_offset(0));
309+
assert_eq!(5, array.value_offset(1));
310+
assert_eq!(10, array.value_offset(2));
311+
312+
assert_eq!(b"hello", array.value(0));
313+
assert!(array.is_null(1));
314+
assert_eq!(b"arrow", array.value(2));
315+
assert_eq!(5, array.value_length());
316+
}
317+
318+
#[test]
319+
#[should_panic(expected = "Cannot append FixedSizeBinaryArray with different value length")]
320+
fn test_fixed_size_binary_builder_append_array_invalid_value_length() {
321+
let mut other_builder = FixedSizeBinaryBuilder::with_capacity(3, 4);
322+
other_builder.append_value(b"test").unwrap();
323+
let other_array = other_builder.finish();
324+
let mut builder = FixedSizeBinaryBuilder::with_capacity(3, 5);
325+
builder.append_array(&other_array).unwrap();
326+
}
273327
}

0 commit comments

Comments
 (0)