rich7420 commented on code in PR #6130:
URL: https://github.com/apache/datafusion-comet/pull/6130#discussion_r4245992000


##########
native/core/src/execution/python_udf.rs:
##########
@@ -0,0 +1,593 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! In-process bridge for Spark 4.1+ scalar Arrow UDFs. Each instance owns one
+//! unpickled Python callable and must be created for one Spark task/partition.
+//! The public API deliberately deals in Arrow arrays; the physical operator is
+//! responsible for evaluating Catalyst arguments and preserving input columns.
+
+use arrow::array::{make_array, Array, ArrayRef};
+use arrow::datatypes::DataType;
+use arrow::error::{ArrowError, Result};
+use arrow::ffi::{from_ffi, FFI_ArrowArray, FFI_ArrowSchema};
+use datafusion_comet_common::decode_string_arrays;
+use pyo3::ffi::Py_uintptr_t;
+use pyo3::prelude::*;
+use pyo3::sync::MutexExt;
+use pyo3::types::{PyBytes, PyList, PyTuple};
+use std::sync::{Mutex, PoisonError};
+
+// Tasks create their callables concurrently. Concurrent first imports of a 
package
+// can hand a thread a partially initialized module (observed with PyArrow on 
Python
+// 3.10), so callables are unpickled one at a time. Take it with 
`lock_py_attached`,
+// which waits for the lock without holding the GIL.
+static IMPORT_LOCK: Mutex<()> = Mutex::new(());
+
+fn initialize_python() -> Result<()> {
+    use std::ffi::CStr;
+    use std::sync::OnceLock;
+
+    static RESULT: OnceLock<std::result::Result<(), String>> = OnceLock::new();
+    RESULT
+        .get_or_init(|| {
+            // Spark sets PYTHONHASHSEED=0 on its Python workers by default.
+            // Match that seed before any Python object is created in the 
embedded
+            // interpreter, without changing the JVM process environment.
+            // SAFETY: OnceLock serializes initialization by Comet. No other 
Comet
+            // code accesses the Python C API before this function returns.
+            unsafe {
+                if pyo3::ffi::Py_IsInitialized() != 0 {
+                    return Ok(());
+                }
+                let mut config = 
std::mem::MaybeUninit::<pyo3::ffi::PyConfig>::uninit();
+                pyo3::ffi::PyConfig_InitPythonConfig(config.as_mut_ptr());
+                let mut config = config.assume_init();
+                config.install_signal_handlers = 0;

Review Comment:
   Could we match Spark’s unbuffered Python stdout? Buffered `print()` output 
can be lost at JVM exit. `configure_c_stdio = 0` leaves the JVM’s C streams 
unchanged.
   
   ```suggestion
                   config.install_signal_handlers = 0;
                   config.buffered_stdio = 0;
                   config.configure_c_stdio = 0;
   ```
   
   Please add a regression that captures a printed marker without an explicit 
flush.
   



-- 
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]

Reply via email to