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

Removed from_data from all arrays #1328

Merged
merged 1 commit into from Dec 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion arrow-pyarrow-integration-testing/src/c_stream.rs
Expand Up @@ -32,7 +32,7 @@ pub fn to_rust_iterator(ob: PyObject, py: Python) -> PyResult<Vec<PyObject>> {
pub fn from_rust_iterator(py: Python) -> PyResult<PyObject> {
// initialize an array
let array = Int32Array::from(&[Some(2), None, Some(1), None]);
let array = StructArray::from_data(
let array = StructArray::new(
DataType::Struct(vec![Field::new("a", array.data_type().clone(), true)]),
vec![array.boxed()],
None,
Expand Down
2 changes: 1 addition & 1 deletion benches/filter_kernels.rs
Expand Up @@ -37,7 +37,7 @@ fn add_benchmark(c: &mut Criterion) {

let filter_array = create_boolean_array(size, 0.0, 0.9);
let filter_array =
BooleanArray::from_data(DataType::Boolean, filter_array.values().clone(), None);
BooleanArray::new(DataType::Boolean, filter_array.values().clone(), None);

let arr_a = create_primitive_array::<f32>(size, 0.0);
c.bench_function(&format!("filter 2^{} f32", log2_size), |b| {
Expand Down
4 changes: 2 additions & 2 deletions benches/iter_list.rs
Expand Up @@ -14,7 +14,7 @@ fn add_benchmark(c: &mut Criterion) {
let size = 2usize.pow(log2_size);

let values = Buffer::from_iter(0..size as i32);
let values = PrimitiveArray::<i32>::from_data(DataType::Int32, values, None);
let values = PrimitiveArray::<i32>::new(DataType::Int32, values, None);

let offsets = (0..=size as i32).step_by(2).collect::<Vec<_>>();

Expand All @@ -23,7 +23,7 @@ fn add_benchmark(c: &mut Criterion) {
.collect::<Bitmap>();

let data_type = ListArray::<i32>::default_datatype(DataType::Int32);
let array = ListArray::<i32>::from_data(
let array = ListArray::<i32>::new(
data_type,
offsets.try_into().unwrap(),
Box::new(values),
Expand Down
2 changes: 1 addition & 1 deletion guide/src/high_level.md
Expand Up @@ -253,7 +253,7 @@ where
let values = array.values().iter().map(|v| op(*v)).collect::<Vec<_>>();

// create the new array, cloning its validity
PrimitiveArray::<O>::from_data(data_type.clone(), values.into(), array.validity().cloned())
PrimitiveArray::<O>::new(data_type.clone(), values.into(), array.validity().cloned())
}
```

Expand Down
38 changes: 14 additions & 24 deletions src/array/binary/mod.rs
Expand Up @@ -6,7 +6,7 @@ use crate::{
buffer::Buffer,
datatypes::DataType,
error::Error,
offset::{Offset, OffsetsBuffer},
offset::{Offset, Offsets, OffsetsBuffer},
trusted_len::TrustedLen,
};

Expand Down Expand Up @@ -273,12 +273,15 @@ impl<O: Offset> BinaryArray<O> {
mutable_values.into(),
Some(mutable_bitmap.into()),
)),
(Some(values), Some(offsets)) => Right(MutableBinaryArray::from_data(
self.data_type,
offsets,
values,
Some(mutable_bitmap),
)),
(Some(values), Some(offsets)) => Right(
MutableBinaryArray::try_new(
self.data_type,
offsets,
values,
Some(mutable_bitmap),
)
.unwrap(),
),
},
}
} else {
Expand All @@ -304,12 +307,9 @@ impl<O: Offset> BinaryArray<O> {
values.into(),
None,
)),
(Some(values), Some(offsets)) => Right(MutableBinaryArray::from_data(
self.data_type,
offsets,
values,
None,
)),
(Some(values), Some(offsets)) => Right(
MutableBinaryArray::try_new(self.data_type, offsets, values, None).unwrap(),
),
}
}
}
Expand All @@ -324,7 +324,7 @@ impl<O: Offset> BinaryArray<O> {
pub fn new_null(data_type: DataType, length: usize) -> Self {
Self::new(
data_type,
vec![O::default(); 1 + length].try_into().unwrap(),
Offsets::new_zeroed(length).into(),
Buffer::new(),
Some(Bitmap::new_zeroed(length)),
)
Expand Down Expand Up @@ -413,16 +413,6 @@ impl<O: Offset> BinaryArray<O> {
// soundness: I: TrustedLen
unsafe { Self::try_from_trusted_len_iter_unchecked(iter) }
}

/// Alias for `new`
pub fn from_data(
data_type: DataType,
offsets: OffsetsBuffer<O>,
values: Buffer<u8>,
validity: Option<Bitmap>,
) -> Self {
Self::new(data_type, offsets, values, validity)
}
}

impl<O: Offset> Array for BinaryArray<O> {
Expand Down
43 changes: 8 additions & 35 deletions src/array/binary/mutable.rs
Expand Up @@ -160,16 +160,6 @@ impl<O: Offset> MutableBinaryArray<O> {
validity.shrink_to_fit()
}
}

/// Equivalent to `Self::try_new(...).unwrap()`
pub fn from_data(
data_type: DataType,
offsets: Offsets<O>,
values: Vec<u8>,
validity: Option<MutableBitmap>,
) -> Self {
Self::try_new(data_type, offsets, values, validity).unwrap()
}
}

impl<O: Offset> MutableBinaryArray<O> {
Expand Down Expand Up @@ -204,25 +194,13 @@ impl<O: Offset> MutableArray for MutableBinaryArray<O> {
}

fn as_box(&mut self) -> Box<dyn Array> {
let (data_type, offsets, values) = std::mem::take(&mut self.values).into_inner();
BinaryArray::new(
data_type,
offsets.into(),
values.into(),
std::mem::take(&mut self.validity).map(|x| x.into()),
)
.boxed()
let array: BinaryArray<O> = std::mem::take(self).into();
array.boxed()
}

fn as_arc(&mut self) -> Arc<dyn Array> {
let (data_type, offsets, values) = std::mem::take(&mut self.values).into_inner();
BinaryArray::new(
data_type,
offsets.into(),
values.into(),
std::mem::take(&mut self.validity).map(|x| x.into()),
)
.arced()
let array: BinaryArray<O> = std::mem::take(self).into();
array.arced()
}

fn data_type(&self) -> &DataType {
Expand Down Expand Up @@ -270,7 +248,7 @@ impl<O: Offset> MutableBinaryArray<O> {
{
let (validity, offsets, values) = trusted_len_unzip(iterator);

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

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

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

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

/// Creates a [`MutableBinaryArray`] from an falible iterator of trusted length.
Expand Down Expand Up @@ -427,7 +400,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::from_data(Self::default_data_type(), offsets, values, None)
Self::try_new(Self::default_data_type(), offsets, values, None).unwrap()
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/array/binary/mutable_values.rs
Expand Up @@ -37,7 +37,8 @@ impl<O: Offset> From<MutableBinaryValuesArray<O>> for BinaryArray<O> {

impl<O: Offset> From<MutableBinaryValuesArray<O>> for MutableBinaryArray<O> {
fn from(other: MutableBinaryValuesArray<O>) -> Self {
MutableBinaryArray::<O>::from_data(other.data_type, other.offsets, other.values, None)
MutableBinaryArray::<O>::try_new(other.data_type, other.offsets, other.values, None)
.expect("MutableBinaryValuesArray is consistent with MutableBinaryArray")
}
}

Expand Down
36 changes: 12 additions & 24 deletions src/array/boolean/mod.rs
Expand Up @@ -85,6 +85,11 @@ impl BooleanArray {
})
}

/// Alias to `Self::try_new().unwrap()`
pub fn new(data_type: DataType, values: Bitmap, validity: Option<Bitmap>) -> Self {
Self::try_new(data_type, values, validity).unwrap()
}

/// Returns an iterator over the optional values of this [`BooleanArray`].
#[inline]
pub fn iter(&self) -> ZipValidity<bool, BitmapIter, BitmapIter> {
Expand Down Expand Up @@ -246,21 +251,18 @@ impl BooleanArray {
immutable,
Some(mutable_bitmap.into()),
)),
Right(mutable) => Right(MutableBooleanArray::from_data(
self.data_type,
mutable,
Some(mutable_bitmap),
)),
Right(mutable) => Right(
MutableBooleanArray::try_new(self.data_type, mutable, Some(mutable_bitmap))
.unwrap(),
),
},
}
} else {
match self.values.into_mut() {
Left(immutable) => Left(BooleanArray::new(self.data_type, immutable, None)),
Right(mutable) => Right(MutableBooleanArray::from_data(
self.data_type,
mutable,
None,
)),
Right(mutable) => {
Right(MutableBooleanArray::try_new(self.data_type, mutable, None).unwrap())
}
}
}
}
Expand Down Expand Up @@ -369,20 +371,6 @@ impl BooleanArray {
} = self;
(data_type, values, validity)
}

/// The canonical method to create a [`BooleanArray`]
/// # Panics
/// This function errors iff:
/// * The validity is not `None` and its length is different from `values`'s length
/// * The `data_type`'s [`PhysicalType`] is not equal to [`PhysicalType::Boolean`].
pub fn new(data_type: DataType, values: Bitmap, validity: Option<Bitmap>) -> Self {
Self::try_new(data_type, values, validity).unwrap()
}

/// Alias for `new`
pub fn from_data(data_type: DataType, values: Bitmap, validity: Option<Bitmap>) -> Self {
Self::new(data_type, values, validity)
}
}

impl Array for BooleanArray {
Expand Down