-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Read Arrow C Stream from Arrow PyCapsule Interface #501
Closed
Closed
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0c39d5d
Read from Arrow C Data interface
kylebarron 3db45e0
add comment
kylebarron 9014157
Merge remote-tracking branch 'origin/main' into fork/kylebarron/kyle/…
jonmmease 98aae9c
Finish merge
jonmmease d690caa
update Cargo.toml
jonmmease 26340fa
fmt
jonmmease File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use arrow::array::{RecordBatch, RecordBatchReader}; | ||
use arrow::datatypes::SchemaRef; | ||
use arrow::ffi_stream::ArrowArrayStreamReader; | ||
use arrow::ffi_stream::FFI_ArrowArrayStream; | ||
use pyo3::exceptions::{PyTypeError, PyValueError}; | ||
use pyo3::prelude::*; | ||
use pyo3::types::PyCapsule; | ||
|
||
/// Validate PyCapsule has provided name | ||
fn validate_pycapsule_name(capsule: &PyCapsule, expected_name: &str) -> PyResult<()> { | ||
let capsule_name = capsule.name()?; | ||
if let Some(capsule_name) = capsule_name { | ||
let capsule_name = capsule_name.to_str()?; | ||
if capsule_name != expected_name { | ||
return Err(PyValueError::new_err(format!( | ||
"Expected name '{}' in PyCapsule, instead got '{}'", | ||
expected_name, capsule_name | ||
))); | ||
} | ||
} else { | ||
return Err(PyValueError::new_err( | ||
"Expected schema PyCapsule to have name set.", | ||
)); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Import `__arrow_c_stream__` across Python boundary. | ||
fn call_arrow_c_stream(ob: &'_ PyAny) -> PyResult<&'_ PyCapsule> { | ||
if !ob.hasattr("__arrow_c_stream__")? { | ||
return Err(PyValueError::new_err( | ||
"Expected an object with dunder __arrow_c_stream__", | ||
)); | ||
} | ||
|
||
let capsule = ob.getattr("__arrow_c_stream__")?.call0()?.downcast()?; | ||
Ok(capsule) | ||
} | ||
|
||
fn import_stream_pycapsule(capsule: &PyCapsule) -> PyResult<FFI_ArrowArrayStream> { | ||
validate_pycapsule_name(capsule, "arrow_array_stream")?; | ||
|
||
let stream = unsafe { FFI_ArrowArrayStream::from_raw(capsule.pointer() as _) }; | ||
Ok(stream) | ||
} | ||
|
||
pub(crate) fn import_arrow_c_stream(ob: &'_ PyAny) -> PyResult<(Vec<RecordBatch>, SchemaRef)> { | ||
let capsule = call_arrow_c_stream(ob)?; | ||
let stream = import_stream_pycapsule(capsule)?; | ||
let stream_reader = ArrowArrayStreamReader::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((batches, schema)) | ||
} |
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 |
---|---|---|
|
@@ -8,6 +8,8 @@ use arrow::{ | |
record_batch::RecordBatch, | ||
}; | ||
|
||
#[cfg(feature = "pyarrow")] | ||
use crate::data::ffi::import_arrow_c_stream; | ||
use crate::{ | ||
data::{ORDER_COL, ORDER_COL_DTYPE}, | ||
error::{Result, ResultWithContext, VegaFusionError}, | ||
|
@@ -36,7 +38,7 @@ use { | |
pyo3::{ | ||
prelude::PyModule, | ||
types::{PyList, PyTuple}, | ||
PyAny, PyErr, PyObject, Python, | ||
PyAny, PyErr, PyObject, PyResult, Python, | ||
}, | ||
}; | ||
|
||
|
@@ -267,6 +269,17 @@ impl VegaFusionTable { | |
} | ||
} | ||
|
||
// TODO: when updated to latest arrow version, the below function can change to | ||
// pub fn from_arrow_c_stream(table: pyo3_arrow::PyTable) -> PyResult<Self> { | ||
// let (batches, schema) = table.into_inner(); | ||
// Ok(VegaFusionTable::try_new(schema, batches)?) | ||
// } | ||
#[cfg(feature = "pyarrow")] | ||
pub fn from_arrow_c_stream(ob: &PyAny) -> PyResult<Self> { | ||
let (batches, schema) = import_arrow_c_stream(ob)?; | ||
Ok(VegaFusionTable::try_new(schema, batches)?) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I never know whether |
||
} | ||
|
||
#[cfg(feature = "pyarrow")] | ||
pub fn from_pyarrow(py: Python, pyarrow_table: &PyAny) -> std::result::Result<Self, PyErr> { | ||
// Extract table.schema as a Rust Schema | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only a couple lines of code with pyo3_arrow!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside: I never know whether there's consistency in the ecosystem between an ordering of
(schema, batches)
or(batches, schema)
🫠