Skip to content

Commit

Permalink
Implement Field FFI (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron authored May 3, 2024
1 parent 344748b commit 0d5e672
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
20 changes: 12 additions & 8 deletions arro3-core/src/ffi/from_python/field.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use std::sync::Arc;

use crate::ffi::from_python::utils::import_arrow_c_schema;
use crate::field::PyField;
use arrow_schema::Field;
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
use pyo3::{PyAny, PyResult};

// TODO: need to update import_arrow_c_schema to not always coerce to a Schema
// impl<'a> FromPyObject<'a> for PyField {
// fn extract(ob: &'a PyAny) -> PyResult<Self> {
// let schema = import_arrow_c_schema(ob)?;
// schema.fi
// Ok(Self(schema))
// }
// }
impl<'a> FromPyObject<'a> for PyField {
fn extract(ob: &'a PyAny) -> PyResult<Self> {
let schema_ptr = import_arrow_c_schema(ob)?;
let field =
Field::try_from(schema_ptr).map_err(|err| PyTypeError::new_err(err.to_string()))?;
Ok(Self::new(Arc::new(field)))
}
}
2 changes: 1 addition & 1 deletion arro3-core/src/ffi/from_python/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod array;
pub mod chunked;
pub mod ffi_stream;
// pub mod field;
pub mod field;
pub mod record_batch;
pub mod schema;
pub mod table;
Expand Down
10 changes: 8 additions & 2 deletions arro3-core/src/ffi/from_python/schema.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use std::sync::Arc;

use crate::ffi::from_python::utils::import_arrow_c_schema;
use crate::schema::PySchema;
use arrow_schema::Schema;
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
use pyo3::{PyAny, PyResult};

impl<'a> FromPyObject<'a> for PySchema {
fn extract(ob: &'a PyAny) -> PyResult<Self> {
let schema = import_arrow_c_schema(ob)?;
Ok(Self::new(schema))
let schema_ptr = import_arrow_c_schema(ob)?;
let schema =
Schema::try_from(schema_ptr).map_err(|err| PyTypeError::new_err(err.to_string()))?;
Ok(Self::new(Arc::new(schema)))
}
}
10 changes: 3 additions & 7 deletions arro3-core/src/ffi/from_python/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::sync::Arc;

use arrow::datatypes::{Field, Schema, SchemaRef};
use arrow::datatypes::Field;
use arrow::ffi::{FFI_ArrowArray, FFI_ArrowSchema};
use arrow::ffi_stream::FFI_ArrowArrayStream;
use arrow_array::{make_array, ArrayRef};
Expand Down Expand Up @@ -30,7 +28,7 @@ pub fn validate_pycapsule_name(capsule: &PyCapsule, expected_name: &str) -> PyRe
}

/// Import `__arrow_c_schema__` across Python boundary
pub(crate) fn import_arrow_c_schema(ob: &PyAny) -> PyResult<SchemaRef> {
pub(crate) fn import_arrow_c_schema(ob: &PyAny) -> PyResult<&FFI_ArrowSchema> {
if !ob.hasattr("__arrow_c_schema__")? {
return Err(PyValueError::new_err(
"Expected an object with dunder __arrow_c_schema__",
Expand All @@ -41,9 +39,7 @@ pub(crate) fn import_arrow_c_schema(ob: &PyAny) -> PyResult<SchemaRef> {
validate_pycapsule_name(capsule, "arrow_schema")?;

let schema_ptr = unsafe { capsule.reference::<FFI_ArrowSchema>() };
let schema =
Schema::try_from(schema_ptr).map_err(|err| PyTypeError::new_err(err.to_string()))?;
Ok(Arc::new(schema))
Ok(schema_ptr)
}

/// Import `__arrow_c_array__` across Python boundary
Expand Down
6 changes: 0 additions & 6 deletions arro3-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,5 @@ fn _rust(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<schema::PySchema>()?;
m.add_class::<table::PyTable>()?;

// Top-level array/chunked array functions
// m.add_function(wrap_pyfunction!(
// crate::algorithm::geo::affine_ops::affine_transform,
// m
// )?)?;

Ok(())
}

0 comments on commit 0d5e672

Please sign in to comment.