HeartSaVioR commented on code in PR #60674:
URL: https://github.com/apache/spark/pull/60674#discussion_r4622085560


##########
python/pyspark/sql/tests/connect/client/test_artifact.py:
##########
@@ -179,6 +180,99 @@ def test_add_file(self):
         )
 
 
+class GlobalPyFilesTestsMixin:
+    """
+    The Python files that the application was started with 
(spark.submit.pyFiles, as with
+    --py-files) stay importable in Python UDFs.
+    """
+
+    @classmethod
+    def with_global_py_files(cls, conf):
+        cls.global_py_files_dir = tempfile.mkdtemp(prefix="global_py_files")
+        paths = []
+        for name, value in [

Review Comment:
   > [!NOTE]
   > **DISCLAIMER:** This comment was written by an LLM: Claude Opus 5.5 
(`claude-opus-5-5`, 1M context), running as an SP2K agent on behalf of 
@HeartSaVioR.
   
   Thanks, added in 0ccc47082db:
   - the PySpark fixture now also lists a zip package;
   - a new batch-only test, `test_global_py_files_batch_udf_package`, imports 
it, separate from the UDF that the streaming test shares;
   - `LocalClusterGlobalPyFilesTests` runs it on executors in their own JVMs.
   
   If the archive path is built from the relative files root instead of 
`getAbsoluteFile`, the test fails there with `ModuleNotFoundError`.
   



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/python/PythonGlobalPyFilesSuite.scala:
##########
@@ -0,0 +1,157 @@
+/*
+ * 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.
+ */
+
+package org.apache.spark.sql.execution.python
+
+import java.io.File
+import java.nio.file.{Files, Paths}
+import java.util.zip.{ZipEntry, ZipOutputStream}
+
+import org.apache.spark.SparkConf
+import 
org.apache.spark.internal.config.Python.PYTHON_ISOLATED_SESSION_GLOBAL_PY_FILES_ENABLED
+import org.apache.spark.sql.{QueryTest, Row}
+import 
org.apache.spark.sql.IntegratedUDFTestUtils.{createUserDefinedPythonTableFunction,
 shouldTestPythonUDFs}
+import org.apache.spark.sql.classic.SparkSession
+import org.apache.spark.sql.test.SharedSparkSession
+import org.apache.spark.util.Utils
+
+/**
+ * Tests that the Python workers of a session with artifact isolation, as in 
Spark Connect, can
+ * import the Python files that the application was started with 
(`spark.submit.pyFiles`).
+ */
+class PythonGlobalPyFilesSuite extends QueryTest with SharedSparkSession {
+
+  private lazy val filesDir = Utils.createTempDir()
+
+  // The application's Python files: a module, a package in a zip file, and a 
module whose name
+  // a session can also use.
+  private lazy val globalPyFiles: Seq[String] = {
+    val dir = filesDir
+    val module = new File(dir, "global_py_files_module.py")
+    Files.writeString(module.toPath, "value = 'global module'")
+    val clash = new File(dir, "global_py_files_clash.py")
+    Files.writeString(clash.toPath, "value = 'global clash'")
+    val pkg = new File(dir, "global_py_files_pkg.zip")
+    val out = new ZipOutputStream(Files.newOutputStream(pkg.toPath))
+    try {
+      out.putNextEntry(new ZipEntry("global_py_files_pkg/__init__.py"))
+      out.write("value = 'global package'".getBytes("UTF-8"))
+      out.closeEntry()
+    } finally {
+      out.close()
+    }
+    Seq(module, clash, pkg).map(_.getAbsolutePath)
+  }
+
+  // Other files of the application: a module that is not one of its Python 
files, and one
+  // named like a module of the standard library that a Python worker imports 
when it starts.
+  private lazy val otherFiles: Seq[String] = {
+    val unlisted = new File(filesDir, "global_py_files_unlisted.py")
+    Files.writeString(unlisted.toPath, "value = 'unlisted'")
+    val token = new File(filesDir, "token.py")
+    Files.writeString(token.toPath, "raise ImportError('shadowed the standard 
library')")
+    Seq(unlisted, token).map(_.getAbsolutePath)
+  }
+
+  // As spark-submit does with --py-files and --files outside YARN, except 
that the Python files
+  // are named by paths that do not exist: the workers must use the copies in 
the default files
+  // directory, as on the executors of other hosts.
+  override protected def sparkConf: SparkConf = super.sparkConf
+    .set("spark.files", (globalPyFiles ++ otherFiles).mkString(","))
+    .set("spark.submit.pyFiles",

Review Comment:
   > [!NOTE]
   > **DISCLAIMER:** This comment was written by an LLM: Claude Opus 5.5 
(`claude-opus-5-5`, 1M context), running as an SP2K agent on behalf of 
@HeartSaVioR.
   
   Thanks, added in 0ccc47082db. `spark.submit.pyFiles` in 
`PythonGlobalPyFilesSuite` now also names `global_py_files_without_copy.py`, 
which is not in `spark.files`, and the existing imports in `analyze` and `eval` 
still succeed. Without the `.filter(_.isFile)`, every test of the suite fails 
with `NoSuchFileException`.
   



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