1616// under the License.
1717
1818use std:: collections:: HashMap ;
19- use std:: ffi:: { c_void, CString } ;
20- use std:: sync:: { Arc , OnceLock } ;
19+ use std:: ffi:: { c_void, CStr , CString } ;
20+ use std:: sync:: Arc ;
2121
2222use arrow:: array:: { new_null_array, RecordBatch , RecordBatchReader } ;
2323use arrow:: compute:: can_cast_types;
@@ -59,14 +59,15 @@ use crate::{
5959 expr:: { sort_expr:: PySortExpr , PyExpr } ,
6060} ;
6161
62- static ARROW_STREAM_NAME : OnceLock < CString > = OnceLock :: new ( ) ;
62+ static ARROW_STREAM_NAME : & CStr =
63+ unsafe { CStr :: from_bytes_with_nul_unchecked ( b"arrow_array_stream\0 " ) } ;
6364
6465unsafe extern "C" fn drop_stream ( capsule : * mut ffi:: PyObject ) {
6566 if capsule. is_null ( ) {
6667 return ;
6768 }
68- let name = ARROW_STREAM_NAME . get_or_init ( || CString :: new ( "arrow_array_stream" ) . unwrap ( ) ) ;
69- let stream_ptr = ffi:: PyCapsule_GetPointer ( capsule, name . as_ptr ( ) ) as * mut FFI_ArrowArrayStream ;
69+ let stream_ptr =
70+ ffi:: PyCapsule_GetPointer ( capsule, ARROW_STREAM_NAME . as_ptr ( ) ) as * mut FFI_ArrowArrayStream ;
7071 if !stream_ptr. is_null ( ) {
7172 drop ( Box :: from_raw ( stream_ptr) ) ;
7273 }
@@ -945,7 +946,7 @@ impl PyDataFrame {
945946 let stream = spawn_stream ( py, async move { df. execute_stream ( ) . await } ) ?;
946947
947948 let mut schema: Schema = self . df . schema ( ) . to_owned ( ) . into ( ) ;
948- let mut projection: Option < SchemaRef > = None ;
949+ let mut projection: Option < SchemaRef > = Some ( Arc :: new ( schema . clone ( ) ) ) ;
949950
950951 if let Some ( schema_capsule) = requested_schema {
951952 validate_pycapsule ( & schema_capsule, "arrow_schema" ) ?;
@@ -957,7 +958,7 @@ impl PyDataFrame {
957958 projection = Some ( Arc :: new ( schema. clone ( ) ) ) ;
958959 }
959960
960- let schema_ref = projection . clone ( ) . unwrap_or_else ( || Arc :: new ( schema) ) ;
961+ let schema_ref = Arc :: new ( schema. clone ( ) ) ;
961962
962963 let reader = DataFrameStreamReader {
963964 stream,
@@ -972,9 +973,12 @@ impl PyDataFrame {
972973 !stream_ptr. is_null( ) ,
973974 "ArrowArrayStream pointer should never be null"
974975 ) ;
975- let name = ARROW_STREAM_NAME . get_or_init ( || CString :: new ( "arrow_array_stream" ) . unwrap ( ) ) ;
976976 let capsule = unsafe {
977- ffi:: PyCapsule_New ( stream_ptr as * mut c_void , name. as_ptr ( ) , Some ( drop_stream) )
977+ ffi:: PyCapsule_New (
978+ stream_ptr as * mut c_void ,
979+ ARROW_STREAM_NAME . as_ptr ( ) ,
980+ Some ( drop_stream) ,
981+ )
978982 } ;
979983 if capsule. is_null ( ) {
980984 unsafe { drop ( Box :: from_raw ( stream_ptr) ) } ;
0 commit comments