Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Do not check offsets or utf8 validity in ffi (#505) #510

Merged
merged 1 commit into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/array/binary/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<O: Offset, A: ffi::ArrowArrayRef> FromFfi<A> for BinaryArray<O> {
validity = validity.map(|x| x.slice(offset, length))
}

Ok(Self::from_data(
Ok(Self::from_data_unchecked(
Self::default_data_type(),
offsets,
values,
Expand Down
37 changes: 35 additions & 2 deletions src/array/binary/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{bitmap::Bitmap, buffer::Buffer, datatypes::DataType};

use super::{
display_fmt, display_helper, specification::check_offsets, specification::Offset, Array,
GenericBinaryArray,
display_fmt, display_helper, specification::check_offsets,
specification::check_offsets_minimal, specification::Offset, Array, GenericBinaryArray,
};

mod ffi;
Expand Down Expand Up @@ -84,6 +84,39 @@ impl<O: Offset> BinaryArray<O> {
}
}

/// The same as [`BinaryArray::from_data`] but does not check for offsets.
/// # Safety
/// * `offsets` MUST be monotonically increasing
/// # Panics
/// This function panics iff:
/// * The `data_type`'s physical type is not consistent with the offset `O`.
/// * The last element of `offsets` is different from `values.len()`.
/// * The validity is not `None` and its length is different from `offsets.len() - 1`.
pub fn from_data_unchecked(
data_type: DataType,
offsets: Buffer<O>,
values: Buffer<u8>,
validity: Option<Bitmap>,
) -> Self {
check_offsets_minimal(&offsets, values.len());

if let Some(validity) = &validity {
assert_eq!(offsets.len() - 1, validity.len());
}

if data_type.to_physical_type() != Self::default_data_type().to_physical_type() {
panic!("BinaryArray can only be initialized with DataType::Binary or DataType::LargeBinary")
}

Self {
data_type,
offsets,
values,
validity,
offset: 0,
}
}

/// Creates a new [`BinaryArray`] by slicing this [`BinaryArray`].
/// # Implementation
/// This function is `O(1)`: all data will be shared between both arrays.
Expand Down
2 changes: 1 addition & 1 deletion src/array/utf8/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ impl<O: Offset, A: ffi::ArrowArrayRef> FromFfi<A> for Utf8Array<O> {
validity = validity.map(|x| x.slice(offset, length))
}
let data_type = Self::default_data_type();
Ok(Self::from_data(data_type, offsets, values, validity))
Ok(Self::from_data_unchecked(data_type, offsets, values, validity))
}
}