-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add from_arrow_pycapsule method on each Python class (#11)
- Loading branch information
1 parent
949cf90
commit ee244ae
Showing
20 changed files
with
296 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::array::*; | ||
use crate::ffi::from_python::utils::import_arrow_c_array; | ||
use crate::ffi::from_python::utils::call_arrow_c_array; | ||
use pyo3::prelude::*; | ||
use pyo3::{PyAny, PyResult}; | ||
|
||
impl<'a> FromPyObject<'a> for PyArray { | ||
fn extract(ob: &'a PyAny) -> PyResult<Self> { | ||
let (array, field) = import_arrow_c_array(ob)?; | ||
Ok(PyArray::new(array, Arc::new(field))) | ||
let (schema_capsule, array_capsule) = call_arrow_c_array(ob)?; | ||
Python::with_gil(|py| { | ||
Self::from_arrow_pycapsule(py.get_type::<PyArray>(), schema_capsule, array_capsule) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,11 @@ | ||
use crate::chunked::PyChunkedArray; | ||
use crate::ffi::from_python::ffi_stream::ArrowArrayStreamReader; | ||
use crate::ffi::from_python::utils::import_arrow_c_stream; | ||
use pyo3::exceptions::{PyTypeError, PyValueError}; | ||
use crate::ffi::from_python::utils::call_arrow_c_stream; | ||
use pyo3::prelude::*; | ||
use pyo3::{PyAny, PyResult}; | ||
|
||
impl<'a> FromPyObject<'a> for PyChunkedArray { | ||
fn extract(ob: &'a PyAny) -> PyResult<Self> { | ||
let stream = import_arrow_c_stream(ob)?; | ||
let stream_reader = ArrowArrayStreamReader::try_new(stream) | ||
.map_err(|err| PyValueError::new_err(err.to_string()))?; | ||
|
||
let field = stream_reader.field(); | ||
|
||
let mut chunks = vec![]; | ||
for array in stream_reader { | ||
let array = array.map_err(|err| PyTypeError::new_err(err.to_string()))?; | ||
chunks.push(array); | ||
} | ||
|
||
Ok(PyChunkedArray::new(chunks, field)) | ||
let capsule = call_arrow_c_stream(ob)?; | ||
Python::with_gil(|py| Self::from_arrow_pycapsule(py.get_type::<PyChunkedArray>(), capsule)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,11 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::ffi::from_python::utils::import_arrow_c_schema; | ||
use crate::ffi::from_python::utils::call_arrow_c_schema; | ||
use crate::field::PyField; | ||
use arrow_schema::Field; | ||
use pyo3::exceptions::PyTypeError; | ||
use pyo3::prelude::*; | ||
use pyo3::{PyAny, PyResult}; | ||
|
||
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))) | ||
let capsule = call_arrow_c_schema(ob)?; | ||
Python::with_gil(|py| Self::from_arrow_pycapsule(py.get_type::<PyField>(), capsule)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,17 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::ffi::from_python::utils::import_arrow_c_array; | ||
use crate::ffi::from_python::utils::call_arrow_c_array; | ||
use crate::record_batch::PyRecordBatch; | ||
use arrow::array::AsArray; | ||
use arrow::datatypes::{DataType, SchemaBuilder}; | ||
use arrow_array::Array; | ||
use arrow_array::RecordBatch; | ||
use pyo3::exceptions::PyValueError; | ||
use pyo3::prelude::*; | ||
use pyo3::{PyAny, PyResult}; | ||
|
||
impl<'a> FromPyObject<'a> for PyRecordBatch { | ||
fn extract(ob: &'a PyAny) -> PyResult<Self> { | ||
let (array, field) = import_arrow_c_array(ob)?; | ||
match field.data_type() { | ||
DataType::Struct(fields) => { | ||
let struct_array = array.as_struct(); | ||
let schema = SchemaBuilder::from(fields) | ||
.finish() | ||
.with_metadata(field.metadata().clone()); | ||
assert_eq!( | ||
struct_array.null_count(), | ||
0, | ||
"Cannot convert nullable StructArray to RecordBatch" | ||
); | ||
|
||
let columns = struct_array.columns().to_vec(); | ||
let batch = RecordBatch::try_new(Arc::new(schema), columns) | ||
.map_err(|err| PyValueError::new_err(err.to_string()))?; | ||
Ok(Self::new(batch)) | ||
} | ||
dt => Err(PyValueError::new_err(format!( | ||
"Unexpected data type {}", | ||
dt | ||
))), | ||
} | ||
let (schema_capsule, array_capsule) = call_arrow_c_array(ob)?; | ||
Python::with_gil(|py| { | ||
Self::from_arrow_pycapsule( | ||
py.get_type::<PyRecordBatch>(), | ||
schema_capsule, | ||
array_capsule, | ||
) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
use crate::ffi::from_python::utils::import_arrow_c_stream; | ||
use crate::ffi::from_python::utils::call_arrow_c_stream; | ||
use crate::record_batch_reader::PyRecordBatchReader; | ||
use pyo3::exceptions::PyValueError; | ||
use pyo3::prelude::*; | ||
use pyo3::{PyAny, PyResult}; | ||
|
||
impl<'a> FromPyObject<'a> for PyRecordBatchReader { | ||
fn extract(ob: &'a PyAny) -> PyResult<Self> { | ||
let stream = import_arrow_c_stream(ob)?; | ||
let stream_reader = arrow::ffi_stream::ArrowArrayStreamReader::try_new(stream) | ||
.map_err(|err| PyValueError::new_err(err.to_string()))?; | ||
|
||
Ok(Self(Some(Box::new(stream_reader)))) | ||
let capsule = call_arrow_c_stream(ob)?; | ||
Python::with_gil(|py| { | ||
Self::from_arrow_pycapsule(py.get_type::<PyRecordBatchReader>(), capsule) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,11 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::ffi::from_python::utils::import_arrow_c_schema; | ||
use crate::ffi::from_python::utils::call_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_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))) | ||
let schema_ptr = call_arrow_c_schema(ob)?; | ||
Python::with_gil(|py| Self::from_arrow_pycapsule(py.get_type::<PySchema>(), schema_ptr)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,11 @@ | ||
use crate::ffi::from_python::utils::import_arrow_c_stream; | ||
use crate::ffi::from_python::utils::call_arrow_c_stream; | ||
use crate::table::PyTable; | ||
use arrow::ffi_stream::ArrowArrayStreamReader as ArrowRecordBatchStreamReader; | ||
use arrow_array::RecordBatchReader; | ||
use pyo3::exceptions::{PyTypeError, PyValueError}; | ||
use pyo3::prelude::*; | ||
use pyo3::{PyAny, PyResult}; | ||
|
||
impl<'a> FromPyObject<'a> for PyTable { | ||
fn extract(ob: &'a PyAny) -> PyResult<Self> { | ||
let stream = import_arrow_c_stream(ob)?; | ||
let stream_reader = ArrowRecordBatchStreamReader::try_new(stream) | ||
.map_err(|err| PyValueError::new_err(err.to_string()))?; | ||
let schema = stream_reader.schema(); | ||
|
||
let mut batches = vec![]; | ||
for batch in stream_reader { | ||
let batch = batch.map_err(|err| PyTypeError::new_err(err.to_string()))?; | ||
batches.push(batch); | ||
} | ||
|
||
Ok(PyTable::new(schema, batches)) | ||
let capsule = call_arrow_c_stream(ob)?; | ||
Python::with_gil(|py| Self::from_arrow_pycapsule(py.get_type::<PyTable>(), capsule)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
pub mod array; | ||
pub mod chunked; | ||
pub mod ffi_stream; | ||
pub mod schema; | ||
pub mod stream; |
Oops, something went wrong.