Michael-J-Ward commented on code in PR #825:
URL: https://github.com/apache/datafusion-python/pull/825#discussion_r1731832490
##########
src/dataframe.rs:
##########
@@ -451,6 +457,40 @@ impl PyDataFrame {
Ok(table)
}
+ fn __arrow_c_stream__<'py>(
+ &'py mut self,
+ py: Python<'py>,
+ requested_schema: Option<Bound<'py, PyCapsule>>,
+ ) -> PyResult<Bound<'py, PyCapsule>> {
+ let mut batches = wait_for_future(py,
self.df.as_ref().clone().collect())?;
+ let mut schema: Schema = self.df.schema().to_owned().into();
+
+ if let Some(schema_capsule) = requested_schema {
+ validate_pycapsule(&schema_capsule, "arrow_schema")?;
+
+ let schema_ptr = unsafe {
schema_capsule.reference::<FFI_ArrowSchema>() };
+ let desired_schema =
Schema::try_from(schema_ptr).map_err(DataFusionError::from)?;
+
+ schema = project_schema(schema, desired_schema)
+ .map_err(|e| DataFusionError::ArrowError(e))?;
+
+ batches = batches
+ .into_iter()
+ .map(|record_batch| record_batch_into_schema(record_batch,
&schema))
+ .collect::<Result<Vec<RecordBatch>, ArrowError>>()
+ .map_err(|e| DataFusionError::ArrowError(e))?;
Review Comment:
```suggestion
.map_err(DataFusionError::ArrowError)?;
```
##########
src/dataframe.rs:
##########
@@ -451,6 +457,40 @@ impl PyDataFrame {
Ok(table)
}
+ fn __arrow_c_stream__<'py>(
+ &'py mut self,
+ py: Python<'py>,
+ requested_schema: Option<Bound<'py, PyCapsule>>,
+ ) -> PyResult<Bound<'py, PyCapsule>> {
+ let mut batches = wait_for_future(py,
self.df.as_ref().clone().collect())?;
+ let mut schema: Schema = self.df.schema().to_owned().into();
+
+ if let Some(schema_capsule) = requested_schema {
+ validate_pycapsule(&schema_capsule, "arrow_schema")?;
+
+ let schema_ptr = unsafe {
schema_capsule.reference::<FFI_ArrowSchema>() };
+ let desired_schema =
Schema::try_from(schema_ptr).map_err(DataFusionError::from)?;
+
+ schema = project_schema(schema, desired_schema)
+ .map_err(|e| DataFusionError::ArrowError(e))?;
Review Comment:
```suggestion
.map_err(DataFusionError::ArrowError)?;
```
##########
src/dataframe.rs:
##########
@@ -539,3 +579,78 @@ fn print_dataframe(py: Python, df: DataFrame) ->
PyResult<()> {
print.call1((result,))?;
Ok(())
}
+
+fn project_schema(from_schema: Schema, to_schema: Schema) -> Result<Schema,
ArrowError> {
+ let merged_schema = Schema::try_merge(vec![from_schema,
to_schema.clone()])?;
+
+ let project_indices: Vec<usize> = to_schema
+ .fields
+ .iter()
+ .map(|field| field.name())
+ .filter_map(|field_name| merged_schema.index_of(field_name).ok())
+ .collect();
+
+ merged_schema.project(&project_indices)
+}
+
+fn record_batch_into_schema(
Review Comment:
I am surprised that `arrow-rs` nor `datafusion` have such a utility for
converting a record-batch, but I did take a quick look around and didn't find
anything.
##########
src/dataframe.rs:
##########
@@ -451,6 +457,40 @@ impl PyDataFrame {
Ok(table)
}
+ fn __arrow_c_stream__<'py>(
+ &'py mut self,
+ py: Python<'py>,
+ requested_schema: Option<Bound<'py, PyCapsule>>,
+ ) -> PyResult<Bound<'py, PyCapsule>> {
+ let mut batches = wait_for_future(py,
self.df.as_ref().clone().collect())?;
+ let mut schema: Schema = self.df.schema().to_owned().into();
+
+ if let Some(schema_capsule) = requested_schema {
+ validate_pycapsule(&schema_capsule, "arrow_schema")?;
+
+ let schema_ptr = unsafe {
schema_capsule.reference::<FFI_ArrowSchema>() };
+ let desired_schema =
Schema::try_from(schema_ptr).map_err(DataFusionError::from)?;
+
+ schema = project_schema(schema, desired_schema)
+ .map_err(|e| DataFusionError::ArrowError(e))?;
+
+ batches = batches
+ .into_iter()
+ .map(|record_batch| record_batch_into_schema(record_batch,
&schema))
+ .collect::<Result<Vec<RecordBatch>, ArrowError>>()
+ .map_err(|e| DataFusionError::ArrowError(e))?;
+ }
+
+ let batches_wrapped = batches.into_iter().map(|r| Ok(r));
Review Comment:
```suggestion
let batches_wrapped = batches.into_iter().map(Ok);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]