Skip to content

Commit df65535

Browse files
committed
bumps
Signed-off-by: Robert Kruszewski <[email protected]>
1 parent 6b0fc4a commit df65535

File tree

16 files changed

+196
-144
lines changed

16 files changed

+196
-144
lines changed

vortex-python/src/arrays/from_arrow.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ use crate::arrays::PyArrayRef;
1919
use crate::arrow::FromPyArrow;
2020

2121
/// Convert an Arrow object to a Vortex array.
22-
pub(super) fn from_arrow(obj: &Bound<'_, PyAny>) -> PyResult<PyArrayRef> {
22+
pub(super) fn from_arrow(obj: &Borrowed<'_, '_, PyAny>) -> PyResult<PyArrayRef> {
2323
let pa = obj.py().import("pyarrow")?;
2424
let pa_array = pa.getattr("Array")?;
2525
let chunked_array = pa.getattr("ChunkedArray")?;
2626
let table = pa.getattr("Table")?;
2727

2828
if obj.is_instance(&pa_array)? {
29-
let arrow_array = ArrowArrayData::from_pyarrow_bound(obj).map(make_array)?;
29+
let arrow_array = ArrowArrayData::from_pyarrow(&obj.as_borrowed()).map(make_array)?;
3030
let is_nullable = arrow_array.is_nullable();
3131
let enc_array = ArrayRef::from_arrow(arrow_array.as_ref(), is_nullable);
3232
Ok(PyArrayRef::from(enc_array))
@@ -35,20 +35,20 @@ pub(super) fn from_arrow(obj: &Bound<'_, PyAny>) -> PyResult<PyArrayRef> {
3535
let encoded_chunks = chunks
3636
.iter()
3737
.map(|a| {
38-
ArrowArrayData::from_pyarrow_bound(a)
38+
ArrowArrayData::from_pyarrow(&a.as_borrowed())
3939
.map(make_array)
4040
.map(|a| ArrayRef::from_arrow(a.as_ref(), false))
4141
})
4242
.collect::<PyResult<Vec<_>>>()?;
4343
let dtype: DType = obj
4444
.getattr("type")
45-
.and_then(|v| DataType::from_pyarrow_bound(&v))
45+
.and_then(|v| DataType::from_pyarrow(&v.as_borrowed()))
4646
.map(|dt| DType::from_arrow(&Field::new("_", dt, false)))?;
4747
Ok(PyArrayRef::from(
4848
ChunkedArray::try_new(encoded_chunks, dtype)?.into_array(),
4949
))
5050
} else if obj.is_instance(&table)? {
51-
let array_stream = ArrowArrayStreamReader::from_pyarrow_bound(obj)?;
51+
let array_stream = ArrowArrayStreamReader::from_pyarrow(&obj.as_borrowed())?;
5252
let dtype = DType::from_arrow(array_stream.schema());
5353
let chunks = array_stream
5454
.into_iter()

vortex-python/src/arrays/into_array.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use arrow_array::{RecordBatchReader as _, make_array};
66
use arrow_data::ArrayData;
77
use pyo3::exceptions::PyTypeError;
88
use pyo3::types::PyAnyMethods;
9-
use pyo3::{Bound, FromPyObject, PyAny, PyResult};
9+
use pyo3::{Borrowed, FromPyObject, PyAny, PyErr};
1010
use vortex::ArrayRef;
1111
use vortex::arrow::FromArrowArray as _;
1212
use vortex::dtype::DType;
@@ -34,25 +34,27 @@ impl PyIntoArray {
3434
}
3535
}
3636

37-
impl<'py> FromPyObject<'py> for PyIntoArray {
38-
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
37+
impl<'py> FromPyObject<'_, 'py> for PyIntoArray {
38+
type Error = PyErr;
39+
40+
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> Result<Self, Self::Error> {
3941
if ob.is_instance_of::<PyNativeArray>() || ob.is_instance_of::<PyPythonArray>() {
40-
return PyArrayRef::extract_bound(ob).map(PyIntoArray);
42+
return PyArrayRef::extract(ob).map(PyIntoArray);
4143
}
4244

4345
let py = ob.py();
4446
let pa = py.import("pyarrow")?;
4547

4648
if ob.is_instance(&pa.getattr("Array")?)? {
47-
let arrow_array_data = ArrayData::from_pyarrow_bound(ob)?;
49+
let arrow_array_data = ArrayData::from_pyarrow(&ob.as_borrowed())?;
4850
return Ok(PyIntoArray(PyVortex(ArrayRef::from_arrow(
4951
make_array(arrow_array_data).as_ref(),
5052
false,
5153
))));
5254
}
5355

5456
if ob.is_instance(&pa.getattr("Table")?)? {
55-
let arrow_stream = ArrowArrayStreamReader::from_pyarrow_bound(ob)?;
57+
let arrow_stream = ArrowArrayStreamReader::from_pyarrow(&ob.as_borrowed())?;
5658
let dtype = DType::from_arrow(arrow_stream.schema());
5759
let vortex_iter = arrow_stream
5860
.into_iter()

vortex-python/src/arrays/mod.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,17 @@ pub(crate) fn init(py: Python, parent: &Bound<PyModule>) -> PyResult<()> {
8080
/// A type adapter used to extract an ArrayRef from a Python object.
8181
pub type PyArrayRef = PyVortex<ArrayRef>;
8282

83-
impl<'py> FromPyObject<'py> for PyArrayRef {
84-
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
83+
impl<'py> FromPyObject<'_, 'py> for PyArrayRef {
84+
type Error = PyErr;
85+
86+
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> Result<Self, Self::Error> {
8587
// If it's already native, then we're done.
86-
if let Ok(native) = ob.downcast::<PyNativeArray>() {
88+
if let Ok(native) = ob.cast::<PyNativeArray>() {
8789
return Ok(Self(native.get().inner().clone()));
8890
}
8991

9092
// Otherwise, if it's a subclass of `PyArray`, then we can extract the inner array.
91-
PythonArray::extract_bound(ob).map(|instance| Self(instance.to_array()))
93+
PythonArray::extract(ob).map(|instance| Self(instance.to_array()))
9294
}
9395
}
9496

@@ -198,7 +200,7 @@ impl PyArray {
198200
/// :class:`~vortex.Array`
199201
#[staticmethod]
200202
fn from_arrow(obj: Bound<'_, PyAny>) -> PyResult<PyArrayRef> {
201-
from_arrow::from_arrow(&obj)
203+
from_arrow::from_arrow(&obj.as_borrowed())
202204
}
203205

204206
/// Convert a Python range into a Vortex array.
@@ -241,13 +243,13 @@ impl PyArray {
241243
#[staticmethod]
242244
#[pyo3(signature = (range, *, dtype = None))]
243245
fn from_range(range: Bound<PyAny>, dtype: Option<Bound<PyDType>>) -> PyResult<PyArrayRef> {
244-
let range = range.downcast::<PyRange>()?;
246+
let range = range.cast::<PyRange>()?;
245247
let start = range.start()?;
246248
let stop = range.stop()?;
247249
let step = range.step()?;
248250

249251
let (ptype, dtype) = if let Some(dtype) = dtype {
250-
let dtype = dtype.downcast::<PyDType>()?.get().inner().clone();
252+
let dtype = dtype.cast::<PyDType>()?.get().inner().clone();
251253
let DType::Primitive(ptype, ..) = &dtype else {
252254
return Err(PyValueError::new_err(
253255
"Cannot construct non-numeric array from a range.",
@@ -297,7 +299,7 @@ impl PyArray {
297299
/// ```
298300
fn to_arrow_array<'py>(self_: &'py Bound<'py, Self>) -> PyResult<Bound<'py, PyAny>> {
299301
// NOTE(ngates): for struct arrays, we could also return a RecordBatchStreamReader.
300-
let array = PyArrayRef::extract_bound(self_.as_any())?.into_inner();
302+
let array = PyArrayRef::extract(self_.as_any().as_borrowed())?.into_inner();
301303
let py = self_.py();
302304

303305
if let Some(chunked_array) = array.as_opt::<ChunkedVTable>() {
@@ -347,15 +349,15 @@ impl PyArray {
347349
/// Returns the encoding ID of this array.
348350
#[getter]
349351
fn id(slf: &Bound<Self>) -> PyResult<String> {
350-
Ok(PyArrayRef::extract_bound(slf.as_any())?
352+
Ok(PyArrayRef::extract(slf.as_any().as_borrowed())?
351353
.encoding_id()
352354
.to_string())
353355
}
354356

355357
/// Returns the number of bytes used by this array.
356358
#[getter]
357359
fn nbytes(slf: &Bound<Self>) -> PyResult<u64> {
358-
Ok(PyArrayRef::extract_bound(slf.as_any())?.nbytes())
360+
Ok(PyArrayRef::extract(slf.as_any().as_borrowed())?.nbytes())
359361
}
360362

361363
/// Returns the data type of this array.
@@ -392,48 +394,50 @@ impl PyArray {
392394
fn dtype<'py>(slf: &'py Bound<'py, Self>) -> PyResult<Bound<'py, PyDType>> {
393395
PyDType::init(
394396
slf.py(),
395-
PyArrayRef::extract_bound(slf.as_any())?.dtype().clone(),
397+
PyArrayRef::extract(slf.as_any().as_borrowed())?
398+
.dtype()
399+
.clone(),
396400
)
397401
}
398402

399403
///Rust docs are *not* copied into Python for __lt__: https://github.com/PyO3/pyo3/issues/4326
400404
fn __lt__(slf: Bound<Self>, other: PyArrayRef) -> PyResult<PyArrayRef> {
401-
let slf = PyArrayRef::extract_bound(slf.as_any())?.into_inner();
405+
let slf = PyArrayRef::extract(slf.as_any().as_borrowed())?.into_inner();
402406
let inner = compare(&slf, &*other, Operator::Lt)?;
403407
Ok(PyArrayRef::from(inner))
404408
}
405409

406410
///Rust docs are *not* copied into Python for __le__: https://github.com/PyO3/pyo3/issues/4326
407411
fn __le__(slf: Bound<Self>, other: PyArrayRef) -> PyResult<PyArrayRef> {
408-
let slf = PyArrayRef::extract_bound(slf.as_any())?.into_inner();
412+
let slf = PyArrayRef::extract(slf.as_any().as_borrowed())?.into_inner();
409413
let inner = compare(&*slf, &*other, Operator::Lte)?;
410414
Ok(PyArrayRef::from(inner))
411415
}
412416

413417
///Rust docs are *not* copied into Python for __eq__: https://github.com/PyO3/pyo3/issues/4326
414418
fn __eq__(slf: Bound<Self>, other: PyArrayRef) -> PyResult<PyArrayRef> {
415-
let slf = PyArrayRef::extract_bound(slf.as_any())?.into_inner();
419+
let slf = PyArrayRef::extract(slf.as_any().as_borrowed())?.into_inner();
416420
let inner = compare(&*slf, &*other, Operator::Eq)?;
417421
Ok(PyArrayRef::from(inner))
418422
}
419423

420424
///Rust docs are *not* copied into Python for __ne__: https://github.com/PyO3/pyo3/issues/4326
421425
fn __ne__(slf: Bound<Self>, other: PyArrayRef) -> PyResult<PyArrayRef> {
422-
let slf = PyArrayRef::extract_bound(slf.as_any())?.into_inner();
426+
let slf = PyArrayRef::extract(slf.as_any().as_borrowed())?.into_inner();
423427
let inner = compare(&*slf, &*other, Operator::NotEq)?;
424428
Ok(PyArrayRef::from(inner))
425429
}
426430

427431
///Rust docs are *not* copied into Python for __ge__: https://github.com/PyO3/pyo3/issues/4326
428432
fn __ge__(slf: Bound<Self>, other: PyArrayRef) -> PyResult<PyArrayRef> {
429-
let slf = PyArrayRef::extract_bound(slf.as_any())?.into_inner();
433+
let slf = PyArrayRef::extract(slf.as_any().as_borrowed())?.into_inner();
430434
let inner = compare(&*slf, &*other, Operator::Gte)?;
431435
Ok(PyArrayRef::from(inner))
432436
}
433437

434438
///Rust docs are *not* copied into Python for __gt__: https://github.com/PyO3/pyo3/issues/4326
435439
fn __gt__(slf: Bound<Self>, other: PyArrayRef) -> PyResult<PyArrayRef> {
436-
let slf = PyArrayRef::extract_bound(slf.as_any())?.into_inner();
440+
let slf = PyArrayRef::extract(slf.as_any().as_borrowed())?.into_inner();
437441
let inner = compare(&*slf, &*other, Operator::Gt)?;
438442
Ok(PyArrayRef::from(inner))
439443
}
@@ -467,7 +471,7 @@ impl PyArray {
467471
/// ]
468472
/// ```
469473
fn filter(slf: Bound<Self>, mask: PyArrayRef) -> PyResult<PyArrayRef> {
470-
let slf = PyArrayRef::extract_bound(slf.as_any())?.into_inner();
474+
let slf = PyArrayRef::extract(slf.as_any().as_borrowed())?.into_inner();
471475
let mask = (&*mask as &dyn Array).to_bool().to_mask_fill_null_false();
472476
let inner = vortex::compute::filter(&*slf, &mask)?;
473477
Ok(PyArrayRef::from(inner))
@@ -546,7 +550,7 @@ impl PyArray {
546550
// TODO(ngates): return a vortex.Scalar
547551
fn scalar_at(slf: Bound<Self>, index: usize) -> PyResult<Bound<PyScalar>> {
548552
let py = slf.py();
549-
let slf = PyArrayRef::extract_bound(slf.as_any())?.into_inner();
553+
let slf = PyArrayRef::extract(slf.as_any().as_borrowed())?.into_inner();
550554
PyScalar::init(py, slf.scalar_at(index))
551555
}
552556

@@ -592,7 +596,7 @@ impl PyArray {
592596
/// ]
593597
/// ```
594598
fn take(slf: Bound<Self>, indices: PyArrayRef) -> PyResult<PyArrayRef> {
595-
let slf = PyArrayRef::extract_bound(slf.as_any())?.into_inner();
599+
let slf = PyArrayRef::extract(slf.as_any().as_borrowed())?.into_inner();
596600

597601
if !indices.dtype().is_int() {
598602
return Err(PyValueError::new_err(format!(
@@ -608,7 +612,7 @@ impl PyArray {
608612

609613
#[pyo3(signature = (start, end))]
610614
fn slice(slf: Bound<Self>, start: usize, end: usize) -> PyResult<PyArrayRef> {
611-
let slf = PyArrayRef::extract_bound(slf.as_any())?.into_inner();
615+
let slf = PyArrayRef::extract(slf.as_any().as_borrowed())?.into_inner();
612616
let inner = slf.slice(start..end);
613617
Ok(PyArrayRef::from(inner))
614618
}
@@ -643,14 +647,14 @@ impl PyArray {
643647
///
644648
/// Compressed arrays often have more complex, deeply nested encoding trees.
645649
fn display_tree(slf: &Bound<Self>) -> PyResult<String> {
646-
Ok(PyArrayRef::extract_bound(slf.as_any())?
650+
Ok(PyArrayRef::extract(slf.as_any().as_borrowed())?
647651
.display_tree()
648652
.to_string())
649653
}
650654

651655
fn serialize(slf: &Bound<Self>, ctx: &PyArrayContext) -> PyResult<Vec<Vec<u8>>> {
652656
// FIXME(ngates): do not copy to vec, use buffer protocol
653-
let array = PyArrayRef::extract_bound(slf.as_any())?;
657+
let array = PyArrayRef::extract(slf.as_any().as_borrowed())?;
654658
Ok(array
655659
.serialize(ctx, &Default::default())?
656660
.into_iter()
@@ -666,7 +670,7 @@ impl PyArray {
666670
slf: &'py Bound<'py, Self>,
667671
) -> PyResult<(Bound<'py, PyAny>, Bound<'py, PyAny>)> {
668672
let py = slf.py();
669-
let array = PyArrayRef::extract_bound(slf.as_any())?.into_inner();
673+
let array = PyArrayRef::extract(slf.as_any().as_borrowed())?.into_inner();
670674

671675
let mut encoder = MessageEncoder::default();
672676
let buffers = encoder.encode(EncoderMessage::Array(&*array));
@@ -698,7 +702,7 @@ impl PyArray {
698702
return Self::__reduce__(slf);
699703
}
700704

701-
let array = PyArrayRef::extract_bound(slf.as_any())?.into_inner();
705+
let array = PyArrayRef::extract(slf.as_any().as_borrowed())?.into_inner();
702706

703707
let mut encoder = MessageEncoder::default();
704708
let array_buffers = encoder.encode(EncoderMessage::Array(&*array));

vortex-python/src/arrays/native.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl PyNativeArray {
172172
.add_subclass(subclass),
173173
)?
174174
.into_any()
175-
.downcast_into::<PyNativeArray>()?)
175+
.cast_into::<PyNativeArray>()?)
176176
}
177177

178178
pub fn inner(&self) -> &ArrayRef {

vortex-python/src/arrays/py/array.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::sync::Arc;
55

66
use pyo3::prelude::*;
7-
use pyo3::{Bound, FromPyObject, Py, PyAny, PyResult};
7+
use pyo3::{Bound, FromPyObject, Py, PyAny};
88
use vortex::EncodingRef;
99
use vortex::dtype::DType;
1010
use vortex::error::VortexError;
@@ -26,11 +26,14 @@ pub struct PythonArray {
2626
pub(super) stats: ArrayStats,
2727
}
2828

29-
impl<'py> FromPyObject<'py> for PythonArray {
30-
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
31-
let python_array = ob.downcast::<PyPythonArray>()?.get();
29+
impl<'py> FromPyObject<'_, 'py> for PythonArray {
30+
type Error = PyErr;
31+
32+
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> Result<Self, Self::Error> {
33+
let ob_cast = ob.cast::<PyPythonArray>()?;
34+
let python_array = ob_cast.get();
3235
Ok(Self {
33-
object: Arc::new(ob.clone().unbind()),
36+
object: Arc::new(ob.to_owned().unbind()),
3437
encoding: python_array.encoding.clone(),
3538
len: python_array.len,
3639
dtype: python_array.dtype.clone(),

vortex-python/src/arrays/py/encoding.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::sync::Arc;
66
use pyo3::exceptions::PyValueError;
77
use pyo3::prelude::*;
88
use pyo3::types::PyType;
9-
use pyo3::{Bound, FromPyObject, Py, PyAny, PyResult};
9+
use pyo3::{FromPyObject, Py, PyAny};
1010
use vortex::EncodingId;
1111

1212
/// Wrapper struct encapsulating a Python encoding.
@@ -18,15 +18,17 @@ pub struct PythonEncoding {
1818
}
1919

2020
/// Convert a Python class into a [`PythonEncoding`].
21-
impl<'py> FromPyObject<'py> for PythonEncoding {
22-
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
23-
let cls = ob.downcast::<PyType>()?;
21+
impl<'py> FromPyObject<'_, 'py> for PythonEncoding {
22+
type Error = PyErr;
23+
24+
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> Result<Self, Self::Error> {
25+
let cls = ob.cast::<PyType>()?;
2426

2527
let id = EncodingId::new_arc(
2628
cls.getattr("id")
2729
.map_err(|_| {
2830
PyValueError::new_err(format!(
29-
"PyEncoding subclass {ob} must have an 'id' attribute"
31+
"PyEncoding subclass {cls:?} must have an 'id' attribute"
3032
))
3133
})?
3234
.extract::<String>()
@@ -36,7 +38,7 @@ impl<'py> FromPyObject<'py> for PythonEncoding {
3638

3739
Ok(PythonEncoding {
3840
id,
39-
cls: Arc::new(cls.clone().unbind()),
41+
cls: Arc::new(cls.to_owned().unbind()),
4042
})
4143
}
4244
}

vortex-python/src/arrays/py/python.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4-
use pyo3::conversion::FromPyObjectBound;
4+
use pyo3::conversion::FromPyObject;
55
use pyo3::prelude::*;
66
use pyo3::types::PyType;
77
use vortex::EncodingRef;
@@ -33,8 +33,7 @@ impl PyPythonArray {
3333
len: usize,
3434
dtype: PyDType,
3535
) -> PyResult<PyClassInitializer<Self>> {
36-
let encoding =
37-
PythonEncoding::from_py_object_bound(cls.as_any().as_borrowed())?.to_encoding();
36+
let encoding = PythonEncoding::extract(cls.as_any().as_borrowed())?.to_encoding();
3837
Ok(PyClassInitializer::from(PyArray).add_subclass(Self {
3938
encoding,
4039
len,

vortex-python/src/arrays/py/vtable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl VTable for PythonVTable {
6060

6161
let bytes = obj
6262
.call_method("__vx_metadata__", (), None)?
63-
.downcast::<PyBytes>()
63+
.cast::<PyBytes>()
6464
.map_err(|_| vortex_err!("Expected array metadata to be Python bytes"))?
6565
.as_bytes()
6666
.to_vec();

0 commit comments

Comments
 (0)