RamonZhou commented on code in PR #58264:
URL: https://github.com/apache/spark/pull/58264#discussion_r3866770584


##########
sql/connect/server/src/main/scala/org/apache/spark/sql/connect/service/PythonWorkerEnvironment.scala:
##########
@@ -0,0 +1,219 @@
+/*
+ * 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.connect.service
+
+import java.nio.charset.StandardCharsets
+
+import org.apache.spark.{SparkEnv, SparkException}
+import org.apache.spark.sql.RuntimeConfig
+import org.apache.spark.sql.connect.config.Connect
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * The environment variables that Python worker processes launched for a 
session's Python
+ * functions should inherit.
+ *
+ * The environment is carried by session configurations under a reserved 
prefix, one configuration
+ * per variable: `spark.pythonWorkerEnv.FOO=bar` makes `FOO` visible as `bar` 
in `os.environ`
+ * inside a Python UDF. The configurations are the authoritative session state 
-- no second copy
+ * of the environment is maintained as session state -- so the environment 
follows the session
+ * wherever ordinary session configurations follow it, including into a 
session created by
+ * `cloneSession`. A request's snapshot is also held in the plan cache keys of 
the plans it
+ * caches, since a cached plan is only reusable by a request carrying the same 
environment.
+ *
+ * Names are preserved case-sensitively by Spark. On a case-sensitive 
operating system `FOO` and
+ * `foo` are therefore distinct variables; Windows process environments are 
case-insensitive, so
+ * what a worker observes there is the platform's business rather than Spark's.
+ *
+ * A request reads the environment once and uses that one snapshot for 
everything it does, because
+ * the configurations can change underneath it: another request may set them 
while this one is
+ * still planning. Re-reading would let a plan be built with one environment 
and cached under
+ * another.
+ */
+private[connect] object PythonWorkerEnvironment {
+
+  /** Prefix of the session configurations that carry the environment. */
+  val confPrefix: String = "spark.pythonWorkerEnv."
+
+  /**
+   * Environment variable names accepted under [[confPrefix]].
+   *
+   * This is deliberately stricter than the operating system requires. A POSIX 
environment permits
+   * any byte except `=` and NUL in a name, and container platforms accept 
their own broader sets,
+   * but a name outside this pattern cannot be referenced portably from a 
shell, so accepting one
+   * would let a session install a variable that some consumers can never 
read. It is a
+   * portability policy, not a description of what a process environment can 
hold.
+   */
+  val namePattern: String = "^[A-Za-z_][A-Za-z0-9_]*$"

Review Comment:
   The plan is to not set a blocklist or collision tests and the user who sets 
those env vars should be responsible for UDFs behavior.



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