|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use arrow_array::ffi_stream::ArrowArrayStreamReader; |
| 5 | +use arrow_array::{RecordBatchReader as _, make_array}; |
| 6 | +use arrow_data::ArrayData; |
| 7 | +use pyo3::exceptions::PyTypeError; |
| 8 | +use pyo3::types::PyAnyMethods; |
| 9 | +use pyo3::{Bound, FromPyObject, PyAny, PyResult}; |
| 10 | +use vortex::ArrayRef; |
| 11 | +use vortex::arrow::FromArrowArray as _; |
| 12 | +use vortex::dtype::DType; |
| 13 | +use vortex::dtype::arrow::FromArrowType as _; |
| 14 | +use vortex::error::VortexResult; |
| 15 | +use vortex::iter::{ArrayIteratorAdapter, ArrayIteratorExt}; |
| 16 | + |
| 17 | +use crate::PyVortex; |
| 18 | +use crate::arrays::PyArrayRef; |
| 19 | +use crate::arrays::native::PyNativeArray; |
| 20 | +use crate::arrays::py::PyPythonArray; |
| 21 | +use crate::arrow::FromPyArrow; |
| 22 | + |
| 23 | +/// Conversion type for converting Python objects into a [`vortex::Array`]. |
| 24 | +pub struct PyIntoArray(PyArrayRef); |
| 25 | + |
| 26 | +impl PyIntoArray { |
| 27 | + pub fn inner(&self) -> &ArrayRef { |
| 28 | + self.0.inner() |
| 29 | + } |
| 30 | + |
| 31 | + #[allow(dead_code)] |
| 32 | + pub fn into_inner(self) -> ArrayRef { |
| 33 | + self.0.into_inner() |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl<'py> FromPyObject<'py> for PyIntoArray { |
| 38 | + fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> { |
| 39 | + if ob.is_instance_of::<PyNativeArray>() || ob.is_instance_of::<PyPythonArray>() { |
| 40 | + return PyArrayRef::extract_bound(ob).map(PyIntoArray); |
| 41 | + } |
| 42 | + |
| 43 | + let py = ob.py(); |
| 44 | + let pa = py.import("pyarrow")?; |
| 45 | + |
| 46 | + if ob.is_instance(&pa.getattr("Array")?)? { |
| 47 | + let arrow_array_data = ArrayData::from_pyarrow_bound(ob)?; |
| 48 | + return Ok(PyIntoArray(PyVortex(ArrayRef::from_arrow( |
| 49 | + make_array(arrow_array_data).as_ref(), |
| 50 | + false, |
| 51 | + )))); |
| 52 | + } |
| 53 | + |
| 54 | + if ob.is_instance(&pa.getattr("Table")?)? { |
| 55 | + let arrow_stream = ArrowArrayStreamReader::from_pyarrow_bound(ob)?; |
| 56 | + let dtype = DType::from_arrow(arrow_stream.schema()); |
| 57 | + let vortex_iter = arrow_stream |
| 58 | + .into_iter() |
| 59 | + .map(|batch_result| -> VortexResult<_> { |
| 60 | + Ok(ArrayRef::from_arrow(batch_result?, false)) |
| 61 | + }); |
| 62 | + let array = ArrayIteratorAdapter::new(dtype, vortex_iter).read_all()?; |
| 63 | + return Ok(PyIntoArray(PyVortex(array))); |
| 64 | + } |
| 65 | + |
| 66 | + Err(PyTypeError::new_err( |
| 67 | + "Expected an object that can be converted to a Vortex ArrayRef (vortex.Array, pyarrow.Array, or pyarrow.Table)", |
| 68 | + )) |
| 69 | + } |
| 70 | +} |
0 commit comments