Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(rust): Rename data_type -> dtype #18566

Merged
merged 4 commits into from
Sep 5, 2024
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 5 additions & 5 deletions crates/polars-arrow/src/array/binary/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::offset::{Offset, OffsetsBuffer};

impl<O: Offset> Arrow2Arrow for BinaryArray<O> {
fn to_data(&self) -> ArrayData {
let data_type = self.data_type.clone().into();
let builder = ArrayDataBuilder::new(data_type)
let dtype = self.dtype.clone().into();
let builder = ArrayDataBuilder::new(dtype)
.len(self.offsets().len_proxy())
.buffers(vec![
self.offsets.clone().into_inner().into(),
Expand All @@ -20,11 +20,11 @@ impl<O: Offset> Arrow2Arrow for BinaryArray<O> {
}

fn from_data(data: &ArrayData) -> Self {
let data_type = data.data_type().clone().into();
let dtype = data.data_type().clone().into();

if data.is_empty() {
// Handle empty offsets
return Self::new_empty(data_type);
return Self::new_empty(dtype);
}

let buffers = data.buffers();
Expand All @@ -34,7 +34,7 @@ impl<O: Offset> Arrow2Arrow for BinaryArray<O> {
offsets.slice(data.offset(), data.len() + 1);

Self {
data_type,
dtype,
offsets,
values: buffers[1].clone().into(),
validity: data.nulls().map(|n| Bitmap::from_null_buffer(n.clone())),
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-arrow/src/array/binary/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ unsafe impl<O: Offset> ToFfi for BinaryArray<O> {
});

Self {
data_type: self.data_type.clone(),
dtype: self.dtype.clone(),
validity,
offsets: self.offsets.clone(),
values: self.values.clone(),
Expand All @@ -50,7 +50,7 @@ unsafe impl<O: Offset> ToFfi for BinaryArray<O> {

impl<O: Offset, A: ffi::ArrowArrayRef> FromFfi<A> for BinaryArray<O> {
unsafe fn try_from_ffi(array: A) -> PolarsResult<Self> {
let data_type = array.data_type().clone();
let dtype = array.dtype().clone();

let validity = unsafe { array.validity() }?;
let offsets = unsafe { array.buffer::<O>(1) }?;
Expand All @@ -59,6 +59,6 @@ impl<O: Offset, A: ffi::ArrowArrayRef> FromFfi<A> for BinaryArray<O> {
// assumption that data from FFI is well constructed
let offsets = unsafe { OffsetsBuffer::new_unchecked(offsets) };

Self::try_new(data_type, offsets, values, validity)
Self::try_new(dtype, offsets, values, validity)
}
}
76 changes: 35 additions & 41 deletions crates/polars-arrow/src/array/binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ mod data;
/// * `len` is equal to `validity.len()`, when defined.
#[derive(Clone)]
pub struct BinaryArray<O: Offset> {
data_type: ArrowDataType,
dtype: ArrowDataType,
offsets: OffsetsBuffer<O>,
values: Buffer<u8>,
validity: Option<Bitmap>,
Expand All @@ -69,11 +69,11 @@ impl<O: Offset> BinaryArray<O> {
/// This function returns an error iff:
/// * The last offset is not equal to the values' length.
/// * the validity's length is not equal to `offsets.len()`.
/// * The `data_type`'s [`crate::datatypes::PhysicalType`] is not equal to either `Binary` or `LargeBinary`.
/// * The `dtype`'s [`crate::datatypes::PhysicalType`] is not equal to either `Binary` or `LargeBinary`.
/// # Implementation
/// This function is `O(1)`
pub fn try_new(
data_type: ArrowDataType,
dtype: ArrowDataType,
offsets: OffsetsBuffer<O>,
values: Buffer<u8>,
validity: Option<Bitmap>,
Expand All @@ -87,12 +87,12 @@ impl<O: Offset> BinaryArray<O> {
polars_bail!(ComputeError: "validity mask length must match the number of values")
}

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

Ok(Self {
data_type,
dtype,
offsets,
values,
validity,
Expand All @@ -105,13 +105,13 @@ impl<O: Offset> BinaryArray<O> {
///
/// The invariants must be valid (see try_new).
pub unsafe fn new_unchecked(
data_type: ArrowDataType,
dtype: ArrowDataType,
offsets: OffsetsBuffer<O>,
values: Buffer<u8>,
validity: Option<Bitmap>,
) -> Self {
Self {
data_type,
dtype,
offsets,
values,
validity,
Expand Down Expand Up @@ -188,8 +188,8 @@ impl<O: Offset> BinaryArray<O> {

/// Returns the [`ArrowDataType`] of this array.
#[inline]
pub fn data_type(&self) -> &ArrowDataType {
&self.data_type
pub fn dtype(&self) -> &ArrowDataType {
&self.dtype
}

/// Returns the values of this [`BinaryArray`].
Expand Down Expand Up @@ -246,12 +246,12 @@ impl<O: Offset> BinaryArray<O> {
#[must_use]
pub fn into_inner(self) -> (ArrowDataType, OffsetsBuffer<O>, Buffer<u8>, Option<Bitmap>) {
let Self {
data_type,
dtype,
offsets,
values,
validity,
} = self;
(data_type, offsets, values, validity)
(dtype, offsets, values, validity)
}

/// Try to convert this `BinaryArray` to a `MutableBinaryArray`
Expand All @@ -262,33 +262,33 @@ impl<O: Offset> BinaryArray<O> {
match bitmap.into_mut() {
// SAFETY: invariants are preserved
Left(bitmap) => Left(BinaryArray::new(
self.data_type,
self.dtype,
self.offsets,
self.values,
Some(bitmap),
)),
Right(mutable_bitmap) => match (self.values.into_mut(), self.offsets.into_mut()) {
(Left(values), Left(offsets)) => Left(BinaryArray::new(
self.data_type,
self.dtype,
offsets,
values,
Some(mutable_bitmap.into()),
)),
(Left(values), Right(offsets)) => Left(BinaryArray::new(
self.data_type,
self.dtype,
offsets.into(),
values,
Some(mutable_bitmap.into()),
)),
(Right(values), Left(offsets)) => Left(BinaryArray::new(
self.data_type,
self.dtype,
offsets,
values.into(),
Some(mutable_bitmap.into()),
)),
(Right(values), Right(offsets)) => Right(
MutableBinaryArray::try_new(
self.data_type,
self.dtype,
offsets,
values,
Some(mutable_bitmap),
Expand All @@ -300,38 +300,32 @@ impl<O: Offset> BinaryArray<O> {
} else {
match (self.values.into_mut(), self.offsets.into_mut()) {
(Left(values), Left(offsets)) => {
Left(BinaryArray::new(self.data_type, offsets, values, None))
Left(BinaryArray::new(self.dtype, offsets, values, None))
},
(Left(values), Right(offsets)) => {
Left(BinaryArray::new(self.dtype, offsets.into(), values, None))
},
(Right(values), Left(offsets)) => {
Left(BinaryArray::new(self.dtype, offsets, values.into(), None))
},
(Right(values), Right(offsets)) => {
Right(MutableBinaryArray::try_new(self.dtype, offsets, values, None).unwrap())
},
(Left(values), Right(offsets)) => Left(BinaryArray::new(
self.data_type,
offsets.into(),
values,
None,
)),
(Right(values), Left(offsets)) => Left(BinaryArray::new(
self.data_type,
offsets,
values.into(),
None,
)),
(Right(values), Right(offsets)) => Right(
MutableBinaryArray::try_new(self.data_type, offsets, values, None).unwrap(),
),
}
}
}

/// Creates an empty [`BinaryArray`], i.e. whose `.len` is zero.
pub fn new_empty(data_type: ArrowDataType) -> Self {
Self::new(data_type, OffsetsBuffer::new(), Buffer::new(), None)
pub fn new_empty(dtype: ArrowDataType) -> Self {
Self::new(dtype, OffsetsBuffer::new(), Buffer::new(), None)
}

/// Creates an null [`BinaryArray`], i.e. whose `.null_count() == .len()`.
#[inline]
pub fn new_null(data_type: ArrowDataType, length: usize) -> Self {
pub fn new_null(dtype: ArrowDataType, length: usize) -> Self {
unsafe {
Self::new_unchecked(
data_type,
dtype,
Offsets::new_zeroed(length).into(),
Buffer::new(),
Some(Bitmap::new_zeroed(length)),
Expand All @@ -340,7 +334,7 @@ impl<O: Offset> BinaryArray<O> {
}

/// Returns the default [`ArrowDataType`], `DataType::Binary` or `DataType::LargeBinary`
pub fn default_data_type() -> ArrowDataType {
pub fn default_dtype() -> ArrowDataType {
if O::IS_LARGE {
ArrowDataType::LargeBinary
} else {
Expand All @@ -350,12 +344,12 @@ impl<O: Offset> BinaryArray<O> {

/// Alias for unwrapping [`Self::try_new`]
pub fn new(
data_type: ArrowDataType,
dtype: ArrowDataType,
offsets: OffsetsBuffer<O>,
values: Buffer<u8>,
validity: Option<Bitmap>,
) -> Self {
Self::try_new(data_type, offsets, values, validity).unwrap()
Self::try_new(dtype, offsets, values, validity).unwrap()
}

/// Returns a [`BinaryArray`] from an iterator of trusted length.
Expand Down Expand Up @@ -463,13 +457,13 @@ impl<O: Offset> Splitable for BinaryArray<O> {

(
Self {
data_type: self.data_type.clone(),
dtype: self.dtype.clone(),
offsets: lhs_offsets,
values: self.values.clone(),
validity: lhs_validity,
},
Self {
data_type: self.data_type.clone(),
dtype: self.dtype.clone(),
offsets: rhs_offsets,
values: self.values.clone(),
validity: rhs_validity,
Expand Down
22 changes: 11 additions & 11 deletions crates/polars-arrow/src/array/binary/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ impl<O: Offset> MutableBinaryArray<O> {
/// This function returns an error iff:
/// * The last offset is not equal to the values' length.
/// * the validity's length is not equal to `offsets.len()`.
/// * The `data_type`'s [`crate::datatypes::PhysicalType`] is not equal to either `Binary` or `LargeBinary`.
/// * The `dtype`'s [`crate::datatypes::PhysicalType`] is not equal to either `Binary` or `LargeBinary`.
/// # Implementation
/// This function is `O(1)`
pub fn try_new(
data_type: ArrowDataType,
dtype: ArrowDataType,
offsets: Offsets<O>,
values: Vec<u8>,
validity: Option<MutableBitmap>,
) -> PolarsResult<Self> {
let values = MutableBinaryValuesArray::try_new(data_type, offsets, values)?;
let values = MutableBinaryValuesArray::try_new(dtype, offsets, values)?;

if validity
.as_ref()
Expand All @@ -79,8 +79,8 @@ impl<O: Offset> MutableBinaryArray<O> {
Self::from_trusted_len_iter(slice.as_ref().iter().map(|x| x.as_ref()))
}

fn default_data_type() -> ArrowDataType {
BinaryArray::<O>::default_data_type()
fn default_dtype() -> ArrowDataType {
BinaryArray::<O>::default_dtype()
}

/// Initializes a new [`MutableBinaryArray`] with a pre-allocated capacity of slots.
Expand Down Expand Up @@ -201,8 +201,8 @@ impl<O: Offset> MutableArray for MutableBinaryArray<O> {
array.arced()
}

fn data_type(&self) -> &ArrowDataType {
self.values.data_type()
fn dtype(&self) -> &ArrowDataType {
self.values.dtype()
}

fn as_any(&self) -> &dyn std::any::Any {
Expand Down Expand Up @@ -247,7 +247,7 @@ impl<O: Offset> MutableBinaryArray<O> {
{
let (validity, offsets, values) = trusted_len_unzip(iterator);

Self::try_new(Self::default_data_type(), offsets, values, validity).unwrap()
Self::try_new(Self::default_dtype(), offsets, values, validity).unwrap()
}

/// Creates a [`MutableBinaryArray`] from an iterator of trusted length.
Expand All @@ -271,7 +271,7 @@ impl<O: Offset> MutableBinaryArray<O> {
iterator: I,
) -> Self {
let (offsets, values) = trusted_len_values_iter(iterator);
Self::try_new(Self::default_data_type(), offsets, values, None).unwrap()
Self::try_new(Self::default_dtype(), offsets, values, None).unwrap()
}

/// Creates a new [`BinaryArray`] from a [`TrustedLen`] of `&[u8]`.
Expand Down Expand Up @@ -305,7 +305,7 @@ impl<O: Offset> MutableBinaryArray<O> {
validity = None;
}

Ok(Self::try_new(Self::default_data_type(), offsets, values, validity).unwrap())
Ok(Self::try_new(Self::default_dtype(), offsets, values, validity).unwrap())
}

/// Creates a [`MutableBinaryArray`] from an falible iterator of trusted length.
Expand Down Expand Up @@ -403,7 +403,7 @@ impl<O: Offset> MutableBinaryArray<O> {
/// Creates a new [`MutableBinaryArray`] from a [`Iterator`] of `&[u8]`.
pub fn from_iter_values<T: AsRef<[u8]>, I: Iterator<Item = T>>(iterator: I) -> Self {
let (offsets, values) = values_iter(iterator);
Self::try_new(Self::default_data_type(), offsets, values, None).unwrap()
Self::try_new(Self::default_dtype(), offsets, values, None).unwrap()
}

/// Extend with a fallible iterator
Expand Down
Loading