Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion minarrow-py/Cargo.lock

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

32 changes: 32 additions & 0 deletions minarrow-py/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use minarrow::{arr_i8_opt, arr_i16_opt, arr_u8_opt, arr_u16_opt};
use minarrow::enums::array::extract_option_values64;
#[cfg(feature = "datetime")]
use minarrow::enums::time_units::TimeUnit;
#[cfg(feature = "value_type")]
use minarrow::Value;
#[cfg(feature = "decimal")]
use minarrow::DecimalArray;
#[cfg(feature = "datetime")]
Expand All @@ -40,6 +42,7 @@ use pyo3::exceptions::{PyIndexError, PyTypeError, PyValueError};
use pyo3::ffi;
use pyo3::prelude::*;
use pyo3::types::{PyBool, PyString};
use pyo3::Borrowed;
use pyo3::IntoPyObjectExt;

/// Reads a Python sequence into a single 64-byte aligned `Vec64` buffer.
Expand Down Expand Up @@ -547,6 +550,35 @@ pub fn py_to_scalar(value: &Bound<'_, PyAny>) -> PyResult<Scalar> {
))
}

/// A Minarrow `Scalar` extracted from a Python value through [`py_to_scalar`].
///
/// Used as a pyo3 parameter type where a function takes a `Scalar`, since
/// `FromPyObject` cannot be implemented for `Scalar` outside Minarrow.
#[repr(transparent)]
#[derive(Debug, Clone)]
pub struct PyScalar(pub Scalar);

impl<'py> FromPyObject<'_, 'py> for PyScalar {
type Error = PyErr;

fn extract(ob: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
py_to_scalar(&ob).map(PyScalar)
}
}

impl From<PyScalar> for Scalar {
fn from(value: PyScalar) -> Self {
value.0
}
}

#[cfg(feature = "value_type")]
impl From<PyScalar> for Value {
fn from(value: PyScalar) -> Self {
Value::Scalar(value.0)
}
}

pub fn resolve_index(i: isize, len: usize) -> PyResult<usize> {
let resolved = if i < 0 { i + len as isize } else { i };
if resolved < 0 || resolved as usize >= len {
Expand Down
2 changes: 1 addition & 1 deletion minarrow-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub use chunked_array::PyChunkedArray;
#[cfg(feature = "ndarray")]
pub use chunked_ndarray::{PyChunkedNdArray, PyChunkedNdArrayInner};
pub use chunked_table::PyChunkedTable;
pub use convert::{build_array, py_to_scalar, resolve_index, scalar_to_py};
pub use convert::{build_array, py_to_scalar, resolve_index, scalar_to_py, PyScalar};
#[cfg(feature = "cube")]
pub use cube::PyCube;
pub use dtype::{dtype_from_arrow, width_from_arrow, DType, TypeClass};
Expand Down
3 changes: 3 additions & 0 deletions pyo3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ keywords = [
]
categories = ["external-ffi-bindings", "api-bindings"]
description = "PyO3 bindings for MinArrow - zero-copy Arrow interop with Python via PyArrow"
# Gives dependent build scripts the source root as `DEP_MINARROW_PYO3_ROOT`, for
# generators that read the Python conversions with `syn`.
links = "minarrow_pyo3"

[workspace]

Expand Down
5 changes: 5 additions & 0 deletions pyo3/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@

fn main() {
pyo3_build_config::add_extension_module_link_args();
// The source root, published to dependent build scripts as `DEP_MINARROW_PYO3_ROOT`.
println!(
"cargo:root={}",
std::env::var("CARGO_MANIFEST_DIR").expect("cargo sets CARGO_MANIFEST_DIR")
);
}
2 changes: 1 addition & 1 deletion pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ use crate::ffi::dlpack::PyNdArrayInner;
pub use error::{PyMinarrowError, PyMinarrowResult};
pub use types::{
PyArray, PyArrayView, PyChunkedArray, PyChunkedArrayView, PyField, PyRecordBatch,
PyRecordBatchView, PyTable, PyTableView,
PyRecordBatchView, PyTable, PyTableView, PyTextArrayView,
};

// Re-export minarrow types that users might need
Expand Down
55 changes: 54 additions & 1 deletion pyo3/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

use minarrow::{
Array, ArrayV, Field, FieldArray, SuperArray, SuperArrayV, SuperTable, SuperTableV, Table,
TableV,
TableV, TextArrayV,
};
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
use pyo3::Borrowed;
use std::sync::Arc;
Expand Down Expand Up @@ -109,6 +110,12 @@ impl From<PyArray> for Arc<Array> {
}
}

impl From<PyArray> for Array {
fn from(value: PyArray) -> Self {
value.0.array
}
}

impl AsRef<Array> for PyArray {
fn as_ref(&self) -> &Array {
&self.0.array
Expand All @@ -135,6 +142,52 @@ impl<'py> IntoPyObject<'py> for PyArray {
}
}

// PyTextArrayView - Wrapper around MinArrow's TextArrayV

/// Transparent wrapper around MinArrow's [`TextArrayV`].
///
/// Extracts from a Python list of `str`, or from any object exporting a string
/// or categorical array through the Arrow PyCapsule interface, such as a
/// PyArrow string array.
///
/// # Example (Rust)
/// ```ignore
/// use minarrow_pyo3::PyTextArrayView;
/// use minarrow::TextArrayV;
///
/// #[pyfunction]
/// fn add_labels(labels: PyTextArrayView) -> PyResult<()> {
/// let labels: TextArrayV = labels.into();
/// // Process...
/// Ok(())
/// }
/// ```
#[repr(transparent)]
#[derive(Debug, Clone)]
pub struct PyTextArrayView(pub TextArrayV);

impl From<PyTextArrayView> for TextArrayV {
fn from(value: PyTextArrayView) -> Self {
value.0
}
}

impl<'py> FromPyObject<'_, 'py> for PyTextArrayView {
type Error = PyErr;

fn extract(ob: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
if let Ok(strings) = ob.extract::<Vec<String>>() {
return Ok(PyTextArrayView(TextArrayV::from(strings)));
}
match to_rust::array_to_rust(&ob)?.array {
Array::TextArray(text) => Ok(PyTextArrayView(TextArrayV::from(text))),
_ => Err(PyTypeError::new_err(
"expected a list of str or an Arrow string or categorical array",
)),
}
}
}

// PyRecordBatch - Wrapper around MinArrow's Table

/// Transparent wrapper around MinArrow's Table.
Expand Down
Loading