This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/main by this push:
     new 1cc659d6e3 feat: implement append_array for FixedSizeBinaryBuilder 
(#8989)
1cc659d6e3 is described below

commit 1cc659d6e38fc08e624f10a15afbe16983924756
Author: ClSlaid <[email protected]>
AuthorDate: Thu Dec 18 04:24:19 2025 +0800

    feat: implement append_array for FixedSizeBinaryBuilder (#8989)
    
    - Implemented append_array for the builder
        - appends values and nulls one by one
        - tested with unit test
    - To #8750
    
    # Which issue does this PR close?
    
    We generally require a GitHub issue to be filed for all bug fixes and
    enhancements and this helps us generate change logs for our releases.
    You can link an issue to this PR using the GitHub syntax.
    
    - Closes #8750.
    
    # Rationale for this change
    
    Added `append_array` fn for the builder
    
    # What changes are included in this PR?
    
    There is no need to duplicate the description in the issue here but it
    is sometimes worth providing a summary of the individual changes in this
    PR.
    
    # Are these changes tested?
    
    We typically require tests for all PRs in order to:
    1. Prevent the code from being accidentally broken by subsequent changes
    2. Serve as another way to document the expected behavior of the code
    
    If tests are not included in your PR, please explain why (for example,
    are they covered by existing tests)?
    
    # Are there any user-facing changes?
    
    If there are user-facing changes then we may require documentation to be
    updated before approving the PR.
    
    If there are any breaking changes to public APIs, please call them out.
    
    ---------
    
    Signed-off-by: 蔡略 <[email protected]>
    Co-authored-by: Andrew Lamb <[email protected]>
---
 .../src/builder/fixed_size_binary_builder.rs       | 59 ++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/arrow-array/src/builder/fixed_size_binary_builder.rs 
b/arrow-array/src/builder/fixed_size_binary_builder.rs
index 8fd6b72c05..f6b4c33d94 100644
--- a/arrow-array/src/builder/fixed_size_binary_builder.rs
+++ b/arrow-array/src/builder/fixed_size_binary_builder.rs
@@ -15,6 +15,7 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use crate::array::Array;
 use crate::builder::ArrayBuilder;
 use crate::{ArrayRef, FixedSizeBinaryArray};
 use arrow_buffer::Buffer;
@@ -101,6 +102,23 @@ impl FixedSizeBinaryBuilder {
         self.null_buffer_builder.append_n_nulls(n);
     }
 
+    /// Appends all elements in array into the builder.
+    pub fn append_array(&mut self, array: &FixedSizeBinaryArray) -> Result<(), 
ArrowError> {
+        if self.value_length != array.value_length() {
+            return Err(ArrowError::InvalidArgumentError(
+                "Cannot append FixedSizeBinaryArray with different value 
length".to_string(),
+            ));
+        }
+        let buffer = array.value_data();
+        self.values_builder.extend_from_slice(buffer);
+        if let Some(validity) = array.nulls() {
+            self.null_buffer_builder.append_buffer(validity);
+        } else {
+            self.null_buffer_builder.append_n_non_nulls(array.len());
+        }
+        Ok(())
+    }
+
     /// Returns the current values buffer as a slice
     pub fn values_slice(&self) -> &[u8] {
         self.values_builder.as_slice()
@@ -270,4 +288,45 @@ mod tests {
     fn test_fixed_size_binary_builder_invalid_value_length() {
         let _ = FixedSizeBinaryBuilder::with_capacity(15, -1);
     }
+
+    #[test]
+    fn test_fixed_size_binary_builder_append_array() {
+        let mut other_builder = FixedSizeBinaryBuilder::with_capacity(3, 5);
+        other_builder.append_value(b"hello").unwrap();
+        other_builder.append_null();
+        other_builder.append_value(b"arrow").unwrap();
+        let other_array = other_builder.finish();
+
+        let mut builder = FixedSizeBinaryBuilder::with_capacity(6, 5);
+        builder.append_array(&other_array).unwrap();
+        // Append again to test if breaks when appending multiple times
+        builder.append_array(&other_array).unwrap();
+        let array = builder.finish();
+
+        assert_eq!(array.value_length(), other_array.value_length());
+        assert_eq!(&DataType::FixedSizeBinary(5), array.data_type());
+        assert_eq!(6, array.len());
+        assert_eq!(2, array.null_count());
+        for i in 0..6 {
+            assert_eq!(i * 5, array.value_offset(i as usize));
+        }
+
+        assert_eq!(b"hello", array.value(0));
+        assert!(array.is_null(1));
+        assert_eq!(b"arrow", array.value(2));
+
+        assert_eq!(b"hello", array.value(3));
+        assert!(array.is_null(4));
+        assert_eq!(b"arrow", array.value(5));
+    }
+
+    #[test]
+    #[should_panic(expected = "Cannot append FixedSizeBinaryArray with 
different value length")]
+    fn test_fixed_size_binary_builder_append_array_invalid_value_length() {
+        let mut other_builder = FixedSizeBinaryBuilder::with_capacity(3, 4);
+        other_builder.append_value(b"test").unwrap();
+        let other_array = other_builder.finish();
+        let mut builder = FixedSizeBinaryBuilder::with_capacity(3, 5);
+        builder.append_array(&other_array).unwrap();
+    }
 }

Reply via email to