Skip to content

Commit 7c103e0

Browse files
authored
FromArrowArray is implemented for &dyn Array instead of ArrayRef (#3378)
We never rely on the value being an owned Arc --------- Signed-off-by: Robert Kruszewski <[email protected]>
1 parent e44a03b commit 7c103e0

File tree

10 files changed

+25
-23
lines changed

10 files changed

+25
-23
lines changed

vortex-array/src/arrays/varbin/canonical.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl CanonicalVTable<VarBinVTable> for VarBinVTable {
2121
_ => unreachable!("VarBinArray must have Utf8 or Binary dtype"),
2222
};
2323
Ok(Canonical::VarBinView(
24-
ArrayRef::from_arrow(array, nullable).to_varbinview()?,
24+
ArrayRef::from_arrow(array.as_ref(), nullable).to_varbinview()?,
2525
))
2626
}
2727
}

vortex-array/src/arrow/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl ArrayVTable<ArrowVTable> for ArrowVTable {
8484

8585
impl CanonicalVTable<ArrowVTable> for ArrowVTable {
8686
fn canonicalize(array: &ArrowArray) -> VortexResult<Canonical> {
87-
ArrayRef::from_arrow(array.inner.clone(), array.dtype.is_nullable()).to_canonical()
87+
ArrayRef::from_arrow(array.inner.as_ref(), array.dtype.is_nullable()).to_canonical()
8888
}
8989
}
9090

vortex-array/src/arrow/convert.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use arrow_array::array::{
2-
Array as ArrowArray, ArrayRef as ArrowArrayRef, ArrowPrimitiveType,
3-
BooleanArray as ArrowBooleanArray, GenericByteArray, NullArray as ArrowNullArray,
4-
OffsetSizeTrait, PrimitiveArray as ArrowPrimitiveArray, StructArray as ArrowStructArray,
2+
Array as ArrowArray, ArrowPrimitiveType, BooleanArray as ArrowBooleanArray, GenericByteArray,
3+
NullArray as ArrowNullArray, OffsetSizeTrait, PrimitiveArray as ArrowPrimitiveArray,
4+
StructArray as ArrowStructArray,
55
};
66
use arrow_array::cast::{AsArray, as_null_array};
77
use arrow_array::types::{
@@ -233,7 +233,7 @@ impl FromArrowArray<&ArrowStructArray> for ArrayRef {
233233
.columns()
234234
.iter()
235235
.zip(value.fields())
236-
.map(|(c, field)| Self::from_arrow(c.clone(), field.is_nullable()))
236+
.map(|(c, field)| Self::from_arrow(c.as_ref(), field.is_nullable()))
237237
.collect(),
238238
value.len(),
239239
nulls(value.nulls(), nullable),
@@ -252,7 +252,7 @@ impl<O: OffsetSizeTrait + NativePType> FromArrowArray<&GenericListArray<O>> for
252252
dt => vortex_panic!("Invalid data type for ListArray: {dt}"),
253253
};
254254
ListArray::try_new(
255-
Self::from_arrow(value.values().clone(), elem_nullable),
255+
Self::from_arrow(value.values().as_ref(), elem_nullable),
256256
// offsets are always non-nullable
257257
value.offsets().clone().into_array(),
258258
nulls(value.nulls(), nullable),
@@ -286,8 +286,8 @@ fn nulls(nulls: Option<&NullBuffer>, nullable: bool) -> Validity {
286286
}
287287
}
288288

289-
impl FromArrowArray<ArrowArrayRef> for ArrayRef {
290-
fn from_arrow(array: ArrowArrayRef, nullable: bool) -> Self {
289+
impl FromArrowArray<&dyn ArrowArray> for ArrayRef {
290+
fn from_arrow(array: &dyn ArrowArray, nullable: bool) -> Self {
291291
match array.data_type() {
292292
DataType::Boolean => Self::from_arrow(array.as_boolean(), nullable),
293293
DataType::UInt8 => Self::from_arrow(array.as_primitive::<UInt8Type>(), nullable),
@@ -322,7 +322,7 @@ impl FromArrowArray<ArrowArrayRef> for ArrayRef {
322322
DataType::Struct(_) => Self::from_arrow(array.as_struct(), nullable),
323323
DataType::List(_) => Self::from_arrow(array.as_list::<i32>(), nullable),
324324
DataType::LargeList(_) => Self::from_arrow(array.as_list::<i64>(), nullable),
325-
DataType::Null => Self::from_arrow(as_null_array(&array), nullable),
325+
DataType::Null => Self::from_arrow(as_null_array(array), nullable),
326326
DataType::Timestamp(u, _) => match u {
327327
ArrowTimeUnit::Second => {
328328
Self::from_arrow(array.as_primitive::<TimestampSecondType>(), nullable)

vortex-array/src/arrow/record_batch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl TryIntoArray for RecordBatch {
2020
self.columns()
2121
.iter()
2222
.zip(self.schema().fields())
23-
.map(|(array, field)| ArrayRef::from_arrow(array.clone(), field.is_nullable()))
23+
.map(|(array, field)| ArrayRef::from_arrow(array.as_ref(), field.is_nullable()))
2424
.collect(),
2525
self.num_rows(),
2626
Validity::NonNullable, // Must match FromArrowType<SchemaRef> for DType

vortex-array/src/compute/boolean.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::any::Any;
2-
use std::sync::{Arc, LazyLock};
2+
use std::sync::LazyLock;
33

44
use arcref::ArcRef;
5-
use arrow_array::ArrayRef as ArrowArrayRef;
65
use arrow_array::cast::AsArray;
76
use arrow_schema::DataType;
87
use vortex_dtype::DType;
@@ -253,10 +252,7 @@ pub(crate) fn arrow_boolean(
253252
BooleanOperator::OrKleene => arrow_arith::boolean::or_kleene(&lhs, &rhs)?,
254253
};
255254

256-
Ok(ArrayRef::from_arrow(
257-
Arc::new(array) as ArrowArrayRef,
258-
nullable,
259-
))
255+
Ok(ArrayRef::from_arrow(&array, nullable))
260256
}
261257

262258
#[cfg(test)]

vortex-array/src/compute/filter.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ pub fn arrow_filter_fn(array: &dyn Array, mask: &Mask) -> VortexResult<ArrayRef>
239239
let mask_array = BooleanArray::new(values.boolean_buffer().clone(), None);
240240
let filtered = arrow_select::filter::filter(array_ref.as_ref(), &mask_array)?;
241241

242-
Ok(ArrayRef::from_arrow(filtered, array.dtype().is_nullable()))
242+
Ok(ArrayRef::from_arrow(
243+
filtered.as_ref(),
244+
array.dtype().is_nullable(),
245+
))
243246
}
244247

245248
#[cfg(test)]

vortex-array/src/compute/mask.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl ComputeFnVTable for MaskFn {
128128

129129
let masked = arrow_select::nullif::nullif(array_ref.as_ref(), &mask)?;
130130

131-
Ok(ArrayRef::from_arrow(masked, true).into())
131+
Ok(ArrayRef::from_arrow(masked.as_ref(), true).into())
132132
}
133133

134134
fn return_dtype(&self, args: &InvocationArgs) -> VortexResult<DType> {

vortex-array/src/compute/numeric.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ fn arrow_numeric(
253253
NumericOperator::RDiv => arrow_arith::numeric::div(&right, &left)?,
254254
};
255255

256-
from_arrow_array_with_len(array, len, nullable)
256+
from_arrow_array_with_len(array.as_ref(), len, nullable)
257257
}
258258

259259
#[cfg(test)]

vortex-duckdb/src/convert/array/array_ref.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ impl FromDuckDB<SizedFlatVector> for ArrayRef {
8383
let len = sized_vector.len;
8484
let arrow_arr = flat_vector_to_arrow_array(&mut sized_vector.vector, len)
8585
.map_err(|e| vortex_err!("Failed to convert duckdb array to vortex: {}", e))?;
86-
Ok(ArrayRef::from_arrow(arrow_arr, sized_vector.nullable))
86+
Ok(ArrayRef::from_arrow(
87+
arrow_arr.as_ref(),
88+
sized_vector.nullable,
89+
))
8790
}
8891
}

vortex-python/src/arrays/from_arrow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(super) fn from_arrow(obj: &Bound<'_, PyAny>) -> PyResult<PyArrayRef> {
2424
if obj.is_instance(&pa_array)? {
2525
let arrow_array = ArrowArrayData::from_pyarrow_bound(obj).map(make_array)?;
2626
let is_nullable = arrow_array.is_nullable();
27-
let enc_array = ArrayRef::from_arrow(arrow_array, is_nullable);
27+
let enc_array = ArrayRef::from_arrow(arrow_array.as_ref(), is_nullable);
2828
Ok(PyArrayRef::from(enc_array))
2929
} else if obj.is_instance(&chunked_array)? {
3030
let chunks: Vec<Bound<PyAny>> = obj.getattr("chunks")?.extract()?;
@@ -33,7 +33,7 @@ pub(super) fn from_arrow(obj: &Bound<'_, PyAny>) -> PyResult<PyArrayRef> {
3333
.map(|a| {
3434
ArrowArrayData::from_pyarrow_bound(a)
3535
.map(make_array)
36-
.map(|a| ArrayRef::from_arrow(a, false))
36+
.map(|a| ArrayRef::from_arrow(a.as_ref(), false))
3737
})
3838
.collect::<PyResult<Vec<_>>>()?;
3939
let dtype: DType = obj

0 commit comments

Comments
 (0)