@@ -47,7 +47,7 @@ use crate::catalog::PyTable;
4747use crate :: errors:: { py_datafusion_err, PyDataFusionError } ;
4848use crate :: expr:: sort_expr:: to_sort_expressions;
4949use crate :: physical_plan:: PyExecutionPlan ;
50- use crate :: record_batch:: { poll_next_batch, PyRecordBatchStream } ;
50+ use crate :: record_batch:: { poll_next_batch, record_batches_to_pyarrow , PyRecordBatchStream } ;
5151use crate :: sql:: logical:: PyLogicalPlan ;
5252use crate :: utils:: {
5353 get_tokio_runtime, is_ipython_env, py_obj_to_scalar_value, spawn_future, validate_pycapsule,
@@ -323,11 +323,9 @@ impl PyDataFrame {
323323
324324 let table_uuid = uuid:: Uuid :: new_v4 ( ) . to_string ( ) ;
325325
326- // Convert record batches to PyObject list
327- let py_batches = batches
328- . iter ( )
329- . map ( |rb| rb. to_pyarrow ( py) )
330- . collect :: < PyResult < Vec < PyObject > > > ( ) ?;
326+ // Convert record batches to PyArrow objects, keeping the underlying
327+ // Rust data alive for the lifetime of the Python values.
328+ let py_batches = record_batches_to_pyarrow ( batches. clone ( ) , py) ?;
331329
332330 let py_schema = self . schema ( ) . into_pyobject ( py) ?;
333331
@@ -581,9 +579,9 @@ impl PyDataFrame {
581579 fn collect ( & self , py : Python ) -> PyResult < Vec < PyObject > > {
582580 let batches = wait_for_future ( py, self . df . as_ref ( ) . clone ( ) . collect ( ) ) ?
583581 . map_err ( PyDataFusionError :: from) ?;
584- // cannot use PyResult<Vec<RecordBatch>> return type due to
585- // https://github.com/PyO3/pyo3/issues/1813
586- batches . into_iter ( ) . map ( |rb| rb . to_pyarrow ( py ) ) . collect ( )
582+ // Convert to PyArrow, ensuring the Rust-backed memory outlives
583+ // the Python objects.
584+ record_batches_to_pyarrow ( batches , py )
587585 }
588586
589587 /// Cache DataFrame.
@@ -600,7 +598,7 @@ impl PyDataFrame {
600598
601599 batches
602600 . into_iter ( )
603- . map ( |rbs| rbs . into_iter ( ) . map ( |rb| rb . to_pyarrow ( py ) ) . collect ( ) )
601+ . map ( |rbs| record_batches_to_pyarrow ( rbs , py ) )
604602 . collect ( )
605603 }
606604
0 commit comments