Skip to content

Commit f465a8a

Browse files
committed
feat: implement append_array for FixedSizeBinaryBuilder
- Implemented append_array for the builder - appends values and nulls one by one - tested with unit test - To #8750 Signed-off-by: 蔡略 <cailue@apache.org>
1 parent c6cc7f8 commit f465a8a

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

arrow-array/src/builder/fixed_size_binary_builder.rs

Lines changed: 33 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,19 @@ 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+
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+
104118
/// Returns the current values buffer as a slice
105119
pub fn values_slice(&self) -> &[u8] {
106120
self.values_builder.as_slice()
@@ -270,4 +284,23 @@ mod tests {
270284
fn test_fixed_size_binary_builder_invalid_value_length() {
271285
let _ = FixedSizeBinaryBuilder::with_capacity(15, -1);
272286
}
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+
}
273306
}

0 commit comments

Comments
 (0)