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


##########
native/core/src/execution/python_udf.rs:
##########
@@ -0,0 +1,453 @@
+// 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 pyo3::ffi::Py_uintptr_t;
+use pyo3::prelude::*;
+use pyo3::types::{PyBytes, PyTuple};
+
+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;
+                config.use_hash_seed = 1;
+                config.hash_seed = 0;
+                let status = pyo3::ffi::Py_InitializeFromConfig(&config);
+                let error = if pyo3::ffi::PyStatus_Exception(status) != 0 {
+                    if status.err_msg.is_null() {
+                        "Python interpreter initialization failed".to_string()
+                    } else {
+                        CStr::from_ptr(status.err_msg)
+                            .to_string_lossy()
+                            .into_owned()
+                    }
+                } else {
+                    String::new()
+                };
+                pyo3::ffi::PyConfig_Clear(&mut config);
+                if !error.is_empty() {
+                    return Err(error);
+                }
+                pyo3::ffi::PyEval_SaveThread();
+                Ok(())
+            }
+        })
+        .clone()
+        .map_err(ArrowError::ComputeError)
+}
+
+#[cfg(target_os = "linux")]
+fn make_python_symbols_global() -> Result<()> {
+    use std::ffi::CStr;
+    use std::sync::OnceLock;
+
+    static RESULT: OnceLock<std::result::Result<(), String>> = OnceLock::new();
+    RESULT
+        .get_or_init(|| {
+            // The JVM loads libcomet with RTLD_LOCAL. Its libpython 
dependency is
+            // local too, but CPython extension modules resolve Python C API
+            // symbols from the global namespace when they are imported.
+            let mut info = std::mem::MaybeUninit::<libc::Dl_info>::uninit();
+            // SAFETY: Py_Initialize is a linked function address and info is
+            // writable storage for dladdr's result.
+            if unsafe {
+                libc::dladdr(
+                    pyo3::ffi::Py_Initialize as *const () as *const 
libc::c_void,
+                    info.as_mut_ptr(),
+                )
+            } == 0
+            {
+                return Err("cannot locate the linked Python 
library".to_string());
+            }
+            // SAFETY: dladdr initialized info on success and dli_fname is a
+            // null-terminated path valid for the duration of this call.
+            let info = unsafe { info.assume_init() };
+            if info.dli_fname.is_null() {
+                return Err("linked Python library has no path".to_string());
+            }
+            let path = unsafe { CStr::from_ptr(info.dli_fname) };
+            // RTLD_NOLOAD promotes the already-loaded libpython rather than
+            // loading a second copy with separate interpreter state. Keep the
+            // handle for the executor lifetime so its symbols remain global.
+            // SAFETY: path points to a valid C string returned by dladdr.
+            if unsafe {
+                libc::dlopen(
+                    path.as_ptr(),
+                    libc::RTLD_NOW | libc::RTLD_GLOBAL | libc::RTLD_NOLOAD,
+                )
+            }
+            .is_null()
+            {
+                // SAFETY: dlerror returns a null-terminated message, if any.
+                let error = unsafe { libc::dlerror() };
+                let detail = if error.is_null() {
+                    "unknown dynamic loader error".to_string()
+                } else {
+                    unsafe { CStr::from_ptr(error) }
+                        .to_string_lossy()
+                        .into_owned()
+                };
+                return Err(format!("cannot expose Python C API symbols: 
{detail}"));
+            }
+            Ok(())
+        })
+        .clone()
+        .map_err(ArrowError::ComputeError)
+}
+
+#[cfg(not(target_os = "linux"))]
+fn make_python_symbols_global() -> Result<()> {
+    Ok(())
+}
+
+/// A scalar Arrow UDF loaded from Spark's pickled `(function, returnType)` 
command.
+/// Spark serializes the return type for its worker; Comet uses the separately
+/// serialized Arrow type from the physical plan instead.
+pub struct ArrowPythonUdf {
+    callable: Py<PyAny>,
+    return_type: DataType,
+    allow_cast: bool,
+    safe_cast: bool,
+}
+
+impl ArrowPythonUdf {
+    pub fn from_command(
+        command: &[u8],
+        return_type: DataType,
+        allow_cast: bool,
+        safe_cast: bool,
+        python_version: &str,
+    ) -> Result<Self> {
+        make_python_symbols_global()?;
+        initialize_python()?;
+        Python::attach(|py| {
+            if !python_version.is_empty() {
+                let info = py
+                    .import("sys")
+                    .map_err(python_error)?
+                    .getattr("version_info")
+                    .map_err(python_error)?;
+                let major: u8 = info
+                    .get_item(0)
+                    .map_err(python_error)?
+                    .extract()
+                    .map_err(python_error)?;
+                let minor: u8 = info
+                    .get_item(1)
+                    .map_err(python_error)?
+                    .extract()
+                    .map_err(python_error)?;
+                let actual = format!("{major}.{minor}");
+                if actual != python_version {
+                    return Err(ArrowError::ComputeError(format!(
+                        "Arrow UDF requires Python {python_version}, embedded 
interpreter is {actual}"
+                    )));
+                }
+            }
+            let pickle = py.import("pickle").map_err(python_error)?;
+            let loaded = pickle

Review Comment:
   [P2] Could accumulator-bearing UDFs remain on Spark’s worker path until 
embedded execution can forward their updates? With `acc = sc.accumulator(0)` 
and a scalar `@arrow_udf('long')` that calls `acc.add(len(values))` before 
returning `values`, collecting four rows should update `acc.value` to `4`. This 
path successfully unpickles and executes the callable, but only updates the 
embedded interpreter’s `_accumulatorRegistry`. It never performs Spark’s 
accumulator reporting, so the driver value remains `0` despite successful 
evaluation. Subsequent operator instances also reuse the retained accumulator 
state. This silently loses application counters. Forward updates through 
Spark’s accumulator mechanism with task isolation and cleanup, or fall back for 
these UDFs.
   
   Evidence: Spark 4.1.3 executed `spark.range(1, 5, 1, 
1).select(f('id')).collect()` and returned [1, 2, 3, 4] with accumulator value 
4. A rebuilt harness importing this exact head’s unmodified 
`ArrowPythonUdfExec` and `ArrowPythonUdf` used a real PySpark command with 
eligible metadata: PYTHONHASHSEED=0, no includes, and no broadcasts. Two 
operator instances returned the correct rows while the embedded registry 
retained values 4 then 8. The driver accumulator stayed 0. Spark 4.1.3 and 
4.2.0 worker sources clear the registry per task and call 
`send_accumulator_updates`; `PythonRunner.handleEndOfDataSection` receives 
those updates. The new native path has no corresponding forwarding or cleanup. 
Reproduction: `/tmp/comet-6130-b0dab-review-current-1rkq0_j5/reference.py`, 
`src/bin/accumulator.rs`, and `reference.log`. This validates the native 
component and Spark reference, not a full Comet query.



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