Skip to content

Commit

Permalink
Allow NullBuffer construction directly from array (#6769)
Browse files Browse the repository at this point in the history
* Simplify call sites constructing Buffer from array

Converting array to slice is not necessary, since the Buffer can be
constructed directly from the array.

* Allow NullBuffer construction directly from array

For convenience and API compatibility with Buffer.
  • Loading branch information
findepi authored Nov 22, 2024
1 parent 853626e commit d1e76c2
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 17 deletions.
6 changes: 3 additions & 3 deletions arrow-array/src/array/binary_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ mod tests {
let values = b"helloparquet";
let child_data = ArrayData::builder(DataType::UInt8)
.len(12)
.add_buffer(Buffer::from(&values[..]))
.add_buffer(Buffer::from(values))
.build()
.unwrap();
let offsets = [0, 5, 5, 12].map(|n| O::from_usize(n).unwrap());
Expand Down Expand Up @@ -415,7 +415,7 @@ mod tests {
let child_data = ArrayData::builder(DataType::UInt8)
.len(15)
.offset(5)
.add_buffer(Buffer::from(&values[..]))
.add_buffer(Buffer::from(values))
.build()
.unwrap();

Expand Down Expand Up @@ -460,7 +460,7 @@ mod tests {
let values = b"HelloArrow";
let child_data = ArrayData::builder(DataType::UInt8)
.len(10)
.add_buffer(Buffer::from(&values[..]))
.add_buffer(Buffer::from(values))
.null_bit_buffer(Some(Buffer::from_slice_ref([0b1010101010])))
.build()
.unwrap();
Expand Down
6 changes: 3 additions & 3 deletions arrow-array/src/array/fixed_size_binary_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ mod tests {

let array_data = ArrayData::builder(DataType::FixedSizeBinary(5))
.len(3)
.add_buffer(Buffer::from(&values[..]))
.add_buffer(Buffer::from(&values))
.build()
.unwrap();
let fixed_size_binary_array = FixedSizeBinaryArray::from(array_data);
Expand Down Expand Up @@ -691,7 +691,7 @@ mod tests {
let array_data = ArrayData::builder(DataType::FixedSizeBinary(5))
.len(2)
.offset(1)
.add_buffer(Buffer::from(&values[..]))
.add_buffer(Buffer::from(&values))
.build()
.unwrap();
let fixed_size_binary_array = FixedSizeBinaryArray::from(array_data);
Expand Down Expand Up @@ -798,7 +798,7 @@ mod tests {

let array_data = ArrayData::builder(DataType::FixedSizeBinary(5))
.len(3)
.add_buffer(Buffer::from(&values[..]))
.add_buffer(Buffer::from(&values))
.build()
.unwrap();
let arr = FixedSizeBinaryArray::from(array_data);
Expand Down
2 changes: 1 addition & 1 deletion arrow-array/src/array/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2296,7 +2296,7 @@ mod tests {
];
let array_data = ArrayData::builder(DataType::Decimal128(38, 6))
.len(2)
.add_buffer(Buffer::from(&values[..]))
.add_buffer(Buffer::from(&values))
.build()
.unwrap();
let decimal_array = Decimal128Array::from(array_data);
Expand Down
6 changes: 3 additions & 3 deletions arrow-array/src/array/string_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ mod tests {
let child_data = ArrayData::builder(DataType::UInt8)
.len(15)
.offset(5)
.add_buffer(Buffer::from(&values[..]))
.add_buffer(Buffer::from(values))
.build()
.unwrap();

Expand Down Expand Up @@ -427,7 +427,7 @@ mod tests {
let values = b"HelloArrow";
let child_data = ArrayData::builder(DataType::UInt8)
.len(10)
.add_buffer(Buffer::from(&values[..]))
.add_buffer(Buffer::from(values))
.null_bit_buffer(Some(Buffer::from_slice_ref([0b1010101010])))
.build()
.unwrap();
Expand Down Expand Up @@ -469,7 +469,7 @@ mod tests {
let values = b"HelloArrow";
let child_data = ArrayData::builder(DataType::UInt16)
.len(5)
.add_buffer(Buffer::from(&values[..]))
.add_buffer(Buffer::from(values))
.build()
.unwrap();

Expand Down
6 changes: 6 additions & 0 deletions arrow-buffer/src/buffer/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ impl From<&[bool]> for NullBuffer {
}
}

impl<const N: usize> From<&[bool; N]> for NullBuffer {
fn from(value: &[bool; N]) -> Self {
value[..].into()
}
}

impl From<Vec<bool>> for NullBuffer {
fn from(value: Vec<bool>) -> Self {
BooleanBuffer::from(value).into()
Expand Down
2 changes: 1 addition & 1 deletion arrow-ipc/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,7 @@ mod tests {

#[test]
fn test_roundtrip_struct_empty_fields() {
let nulls = NullBuffer::from(&[true, true, false][..]);
let nulls = NullBuffer::from(&[true, true, false]);
let rb = RecordBatch::try_from_iter([(
"",
Arc::new(StructArray::new_empty_fields(nulls.len(), Some(nulls))) as _,
Expand Down
2 changes: 1 addition & 1 deletion arrow-string/src/substring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ mod tests {

let data = ArrayData::builder(DataType::FixedSizeBinary(5))
.len(2)
.add_buffer(Buffer::from(&values[..]))
.add_buffer(Buffer::from(&values))
.offset(1)
.null_bit_buffer(Some(Buffer::from(bits_v)))
.build()
Expand Down
2 changes: 1 addition & 1 deletion arrow/examples/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn main() {
let array_data = ArrayData::builder(DataType::Utf8)
.len(3)
.add_buffer(Buffer::from(offsets.to_byte_slice()))
.add_buffer(Buffer::from(&values[..]))
.add_buffer(Buffer::from(&values))
.null_bit_buffer(Some(Buffer::from([0b00000101])))
.build()
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions arrow/tests/array_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ fn make_fixed_size_list_array() -> FixedSizeListArray {
}

fn make_fixed_size_binary_array() -> FixedSizeBinaryArray {
let values: [u8; 15] = *b"hellotherearrow";
let values: &[u8; 15] = b"hellotherearrow";

let array_data = ArrayData::builder(DataType::FixedSizeBinary(5))
.len(3)
.add_buffer(Buffer::from(&values[..]))
.add_buffer(Buffer::from(values))
.build()
.unwrap();
FixedSizeBinaryArray::from(array_data)
Expand Down
4 changes: 2 additions & 2 deletions parquet/src/arrow/buffer/view_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ mod tests {
#[test]
fn test_view_buffer_append_view() {
let mut buffer = ViewBuffer::default();
let string_buffer = Buffer::from(&b"0123456789long string to test string view"[..]);
let string_buffer = Buffer::from(b"0123456789long string to test string view");
let block_id = buffer.append_block(string_buffer);

unsafe {
Expand All @@ -157,7 +157,7 @@ mod tests {
#[test]
fn test_view_buffer_pad_null() {
let mut buffer = ViewBuffer::default();
let string_buffer = Buffer::from(&b"0123456789long string to test string view"[..]);
let string_buffer = Buffer::from(b"0123456789long string to test string view");
let block_id = buffer.append_block(string_buffer);

unsafe {
Expand Down

0 comments on commit d1e76c2

Please sign in to comment.