Skip to content

Commit

Permalink
Add into_arro3 method
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron committed Feb 22, 2025
1 parent b3bbab4 commit 3a869bc
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 17 deletions.
23 changes: 12 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions pyo3-arrow/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ impl PyArray {
)
}

/// Export to an arro3.core.Array.
///
/// This requires that you depend on arro3-core from your Python package.
pub fn into_arro3(self, py: Python) -> PyResult<Bound<PyAny>> {
let arro3_mod = py.import(intern!(py, "arro3.core"))?;
let array_capsules = to_array_pycapsules(py, self.field.clone(), &self.array, None)?;
arro3_mod
.getattr(intern!(py, "Array"))?
.call_method1(intern!(py, "from_arrow_pycapsule"), array_capsules)
}

/// Export this to a Python `nanoarrow.Array`.
pub fn to_nanoarrow<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
to_nanoarrow_array(py, &self.__arrow_c_array__(py, None)?)
Expand Down
22 changes: 18 additions & 4 deletions pyo3-arrow/src/array_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ impl PyArrayReader {
}

/// Export this to a Python `arro3.core.ArrayReader`.
#[allow(clippy::wrong_self_convention)]
pub fn to_arro3<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
let arro3_mod = py.import(intern!(py, "arro3.core"))?;
arro3_mod.getattr(intern!(py, "ArrayReader"))?.call_method1(
Expand All @@ -87,8 +86,23 @@ impl PyArrayReader {
)
}

/// Export this to a Python `arro3.core.ArrayReader`.
pub fn into_arro3(self, py: Python) -> PyResult<Bound<PyAny>> {
let arro3_mod = py.import(intern!(py, "arro3.core"))?;
let array_reader = self
.0
.lock()
.unwrap()
.take()
.ok_or(PyIOError::new_err("Cannot read from closed stream"))?;
let stream_pycapsule = to_stream_pycapsule(py, array_reader, None)?;
arro3_mod.getattr(intern!(py, "ArrayReader"))?.call_method1(
intern!(py, "from_arrow_pycapsule"),
PyTuple::new(py, vec![stream_pycapsule])?,
)
}

/// Export this to a Python `nanoarrow.ArrayStream`.
#[allow(clippy::wrong_self_convention)]
pub fn to_nanoarrow<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
to_nanoarrow_array_stream(py, &self.__arrow_c_stream__(py, None)?)
}
Expand Down Expand Up @@ -135,8 +149,8 @@ impl PyArrayReader {

// Return self
// https://stackoverflow.com/a/52056290
fn __iter__<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
self.to_arro3(py)
fn __iter__(slf: PyRef<Self>) -> PyRef<Self> {
slf
}

fn __next__(&self) -> PyArrowResult<Arro3Array> {
Expand Down
11 changes: 11 additions & 0 deletions pyo3-arrow/src/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ impl PyChunkedArray {
)
}

/// Export this to a Python `arro3.core.ChunkedArray`.
pub fn into_arro3(self, py: Python) -> PyResult<Bound<PyAny>> {
let arro3_mod = py.import(intern!(py, "arro3.core"))?;
let capsule = Self::to_stream_pycapsule(py, self.chunks.clone(), self.field.clone(), None)?;
arro3_mod
.getattr(intern!(py, "ChunkedArray"))?
.call_method1(
intern!(py, "from_arrow_pycapsule"),
PyTuple::new(py, vec![capsule])?,
)
}
/// Export this to a Python `nanoarrow.ArrayStream`.
pub fn to_nanoarrow<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
to_nanoarrow_array_stream(py, &self.__arrow_c_stream__(py, None)?)
Expand Down
10 changes: 10 additions & 0 deletions pyo3-arrow/src/datatypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ impl PyDataType {
)
}

/// Export this to a Python `arro3.core.DataType`.
pub fn into_arro3(self, py: Python) -> PyResult<Bound<PyAny>> {
let arro3_mod = py.import(intern!(py, "arro3.core"))?;
let capsule = to_schema_pycapsule(py, &self.0)?;
arro3_mod.getattr(intern!(py, "DataType"))?.call_method1(
intern!(py, "from_arrow_pycapsule"),
PyTuple::new(py, vec![capsule])?,
)
}

/// Export this to a Python `nanoarrow.Schema`.
pub fn to_nanoarrow<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
to_nanoarrow_schema(py, &self.__arrow_c_schema__(py)?)
Expand Down
10 changes: 10 additions & 0 deletions pyo3-arrow/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ impl PyField {
)
}

/// Export this to a Python `arro3.core.Field`.
pub fn into_arro3(self, py: Python) -> PyResult<Bound<PyAny>> {
let arro3_mod = py.import(intern!(py, "arro3.core"))?;
let capsule = to_schema_pycapsule(py, self.0.as_ref())?;
arro3_mod.getattr(intern!(py, "Field"))?.call_method1(
intern!(py, "from_arrow_pycapsule"),
PyTuple::new(py, vec![capsule])?,
)
}

/// Export this to a Python `nanoarrow.Schema`.
pub fn to_nanoarrow<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
to_nanoarrow_schema(py, &self.__arrow_c_schema__(py)?)
Expand Down
9 changes: 9 additions & 0 deletions pyo3-arrow/src/record_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ impl PyRecordBatch {
)
}

/// Export this to a Python `arro3.core.RecordBatch`.
pub fn into_arro3(self, py: Python) -> PyResult<Bound<PyAny>> {
let arro3_mod = py.import(intern!(py, "arro3.core"))?;
let capsules = Self::to_array_pycapsules(py, self.0.clone(), None)?;
arro3_mod
.getattr(intern!(py, "RecordBatch"))?
.call_method1(intern!(py, "from_arrow_pycapsule"), capsules)
}

/// Export this to a Python `nanoarrow.Array`.
pub fn to_nanoarrow<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
to_nanoarrow_array(py, &self.__arrow_c_array__(py, None)?)
Expand Down
22 changes: 20 additions & 2 deletions pyo3-arrow/src/record_batch_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ impl PyRecordBatchReader {
)
}

/// Export this to a Python `arro3.core.RecordBatchReader`.
pub fn into_arro3(self, py: Python) -> PyResult<Bound<PyAny>> {
let arro3_mod = py.import(intern!(py, "arro3.core"))?;
let reader = self
.0
.lock()
.unwrap()
.take()
.ok_or(PyIOError::new_err("Cannot read from closed stream"))?;
let capsule = Self::to_stream_pycapsule(py, reader, None)?;
arro3_mod
.getattr(intern!(py, "RecordBatchReader"))?
.call_method1(
intern!(py, "from_arrow_pycapsule"),
PyTuple::new(py, vec![capsule])?,
)
}

/// Export this to a Python `nanoarrow.ArrayStream`.
pub fn to_nanoarrow<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
to_nanoarrow_array_stream(py, &self.__arrow_c_stream__(py, None)?)
Expand Down Expand Up @@ -175,8 +193,8 @@ impl PyRecordBatchReader {

// Return self
// https://stackoverflow.com/a/52056290
fn __iter__<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
self.to_arro3(py)
fn __iter__(slf: PyRef<Self>) -> PyRef<Self> {
slf
}

fn __next__(&self) -> PyArrowResult<Arro3RecordBatch> {
Expand Down
11 changes: 11 additions & 0 deletions pyo3-arrow/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ impl PyScalar {
self.__arrow_c_array__(py, None)?,
)
}

/// Export to an arro3.core.Scalar.
///
/// This requires that you depend on arro3-core from your Python package.
pub fn into_arro3(self, py: Python) -> PyResult<Bound<PyAny>> {
let arro3_mod = py.import(intern!(py, "arro3.core"))?;
let capsules = to_array_pycapsules(py, self.field.clone(), &self.array, None)?;
arro3_mod
.getattr(intern!(py, "Scalar"))?
.call_method1(intern!(py, "from_arrow_pycapsule"), capsules)
}
}

impl Display for PyScalar {
Expand Down
10 changes: 10 additions & 0 deletions pyo3-arrow/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ impl PySchema {
)
}

/// Export this to a Python `arro3.core.Schema`.
pub fn into_arro3(self, py: Python) -> PyResult<Bound<PyAny>> {
let arro3_mod = py.import(intern!(py, "arro3.core"))?;
let capsule = to_schema_pycapsule(py, self.0.as_ref())?;
arro3_mod.getattr(intern!(py, "Schema"))?.call_method1(
intern!(py, "from_arrow_pycapsule"),
PyTuple::new(py, vec![capsule])?,
)
}

/// Export this to a Python `nanoarrow.Schema`.
pub fn to_nanoarrow<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
to_nanoarrow_schema(py, &self.__arrow_c_schema__(py)?)
Expand Down
11 changes: 11 additions & 0 deletions pyo3-arrow/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ impl PyTable {
)
}

/// Export this to a Python `arro3.core.Table`.
pub fn into_arro3(self, py: Python) -> PyResult<Bound<PyAny>> {
let arro3_mod = py.import(intern!(py, "arro3.core"))?;
let capsule =
Self::to_stream_pycapsule(py, self.batches.clone(), self.schema.clone(), None)?;
arro3_mod.getattr(intern!(py, "Table"))?.call_method1(
intern!(py, "from_arrow_pycapsule"),
PyTuple::new(py, vec![capsule])?,
)
}

/// Export this to a Python `nanoarrow.ArrayStream`.
pub fn to_nanoarrow<'py>(&'py self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
to_nanoarrow_array_stream(py, &self.__arrow_c_stream__(py, None)?)
Expand Down

0 comments on commit 3a869bc

Please sign in to comment.