spark-test commented on code in PR #60579:
URL: https://github.com/apache/spark/pull/60579#discussion_r4595118666


##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/util/SparkSessionUtils.scala:
##########
@@ -38,4 +53,10 @@ object SparkSessionUtils {
       }
     }
   }
+
+  /** Whether `key` can be changed on a running session with `spark.conf.set`. 
*/
+  private def isRuntimeConf(key: String): Boolean = {
+    !SQLConf.isStaticConfigKey(key) &&
+      (SQLConf.containsConfigKey(key) || ConfigEntry.findEntry(key) == null)

Review Comment:
   **Non-blocking (P2):** This re-derives the check `spark.conf.set` runs, 
`classic.RuntimeConfig.requireNonStaticConf` (RuntimeConfig.scala:125-142), but 
leaves out two of its branches: that check returns early for 
`spark.default.parallelism`, and it rejects a registered Spark core key only 
while `spark.sql.legacy.setCommandRejectsSparkCoreConfs` is true. So when a 
session sets that flag to false, the base branch applied a core key from a 
flow's configuration for the flow's start, while this version skips it and, 
when the value differs, logs that it cannot be modified on a running session.
   
   Reading the code, that changes what SQL operators use for core keys they 
read from the session's SQL conf: the window, session-window, sort-merge-join 
and cartesian buffer spill-size thresholds fall back to 
`spark.shuffle.spill.maxSizeInBytesForSpillThreshold`, the Python and Pandas 
UDF buffer sizes fall back to `spark.buffer.size`, and script transformation 
reads `spark.task.cpus` directly. Because a streaming query clones the session 
conf when `startStream()` constructs it inside this block, every micro-batch 
then runs with the session's value instead of the requested one. With the flag 
at its default, only `spark.default.parallelism` is affected; the review found 
no SQL code that reads it from the session conf, so there only `spark.conf.get` 
and the warning text change. The new tests all run with the default flag, so 
none of them covers this.
   
   Calling `requireNonStaticConf` for each pair just before applying it, and 
skipping only the pairs it rejects, would keep the skip exactly in line with 
`spark.conf.set` instead of keeping a copy that can drift. Both callers in 
`FlowExecution` already pass a classic session, and the check is visible from 
this package.
   
   **Verification:**
   - **Regression:** With `spark.sql.legacy.setCommandRejectsSparkCoreConfs` 
set to false on the session, running the helper with a registered Spark core 
key such as `spark.buffer.size` shows the requested value inside the block, 
restores the previous value afterwards, and logs no skip warning.
   - **Compatibility:** With the flag at its default, a static SQL key and a 
registered Spark core key keep the session's values inside the block while a 
runtime SQL key is applied, and `spark.default.parallelism` is applied inside 
the block and restored afterwards.



##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/util/SparkSessionUtils.scala:
##########
@@ -17,17 +17,32 @@
 
 package org.apache.spark.sql.pipelines.util
 
+import org.apache.spark.internal.{Logging, LogKeys}
+import org.apache.spark.internal.config.ConfigEntry
 import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.internal.SQLConf
 
-object SparkSessionUtils {
+object SparkSessionUtils extends Logging {
 
   /**
    * Sets all SQL configurations specified in `pairs`, calls `f`, and then 
restores all SQL
    * configurations.
+   *
+   * Configurations that cannot be changed on a running session (static SQL 
configurations and
+   * Spark core configurations) are skipped: a pipeline's `configuration` is 
also used to create

Review Comment:
   **Nit (P3):** This rationale covers only the CLI's pipeline-level 
`configuration` in local mode. The helper receives each flow's merged map 
(`defaultSqlConf ++ flowDef.sqlConf`, GraphRegistrationContext.scala:62), so it 
also skips flow-level settings from Python `spark_conf` and pipeline SQL `SET`, 
which never reach session creation. With `--remote`, the CLI's session builder 
sends the spec's configuration to the remote session as a silent config set, 
which drops static and core keys, so those settings do not take effect at 
session creation either. For example, 
`@dp.materialized_view(spark_conf={"spark.executor.memory": "8g"})` now runs 
without the setting and only logs a driver warning.
   
   The public docs still promise the opposite: the `spark_conf` docstrings in 
`python/pyspark/pipelines/api.py` (for example lines 47-49) say "These confs 
will be set when the flow is executed", and the `spark_conf` row in 
`docs/declarative-pipelines-programming-guide.md` (line 627) says the setting 
overrides configuration set on the destination, the pipeline, or the cluster. 
Suggest rewording this comment to say that every pair a running session cannot 
take is ignored, flow-level ones included, with a driver warning when the 
requested value differs, and adding a sentence to each `spark_conf` description 
and to the guide row saying that settings a running session cannot change, such 
as static SQL configurations and, by default, Spark core configurations, are 
ignored with a warning in the driver log and must be set when the session is 
created.
   
   **Verification:**
   - **Inspection:** The helper comment, each `spark_conf` parameter 
description in the Python pipelines API, and the programming guide's 
`spark_conf` row all say that static SQL configurations and, by default, Spark 
core configurations given in pipeline or flow settings are ignored with a 
driver warning, and the helper comment no longer says such values took effect 
when the session was created.



##########
sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/util/SparkSessionUtilsSuite.scala:
##########
@@ -0,0 +1,60 @@
+/*
+ * 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.pipelines.util
+
+import org.apache.spark.internal.config.DRIVER_MEMORY
+import org.apache.spark.sql.QueryTest
+import org.apache.spark.sql.internal.{SQLConf, StaticSQLConf}
+import org.apache.spark.sql.test.SharedSparkSession
+
+class SparkSessionUtilsSuite extends QueryTest with SharedSparkSession {
+
+  test("withSqlConf sets the confs for the duration of the block and restores 
them") {
+    val setKey = SQLConf.SHUFFLE_PARTITIONS.key
+    val unsetKey = "spark.sql.pipelines.test.unsetConf"
+    val original = spark.conf.get(setKey)
+    val inside = SparkSessionUtils.withSqlConf(spark, setKey -> "7", unsetKey 
-> "x") {
+      (spark.conf.get(setKey), spark.conf.get(unsetKey))
+    }
+    assert(inside == ("7", "x"))
+    assert(spark.conf.get(setKey) == original)
+    assert(spark.conf.getOption(unsetKey).isEmpty)
+  }
+
+  test("withSqlConf skips confs that cannot be modified on a running session") 
{
+    val staticKey = StaticSQLConf.CATALOG_IMPLEMENTATION.key
+    val staticValue = spark.conf.get(staticKey)
+    val otherStaticValue = if (staticValue == "hive") "in-memory" else "hive"
+    val coreKey = DRIVER_MEMORY.key
+    val coreValue = spark.conf.getOption(coreKey)
+    Seq(staticValue, otherStaticValue).foreach { v =>
+      val inside = SparkSessionUtils.withSqlConf(
+        spark,
+        staticKey -> v,
+        coreKey -> "1234m",
+        SQLConf.SHUFFLE_PARTITIONS.key -> "7") {
+        (spark.conf.get(staticKey), spark.conf.getOption(coreKey),
+          spark.conf.get(SQLConf.SHUFFLE_PARTITIONS.key))
+      }
+      // Confs that cannot be modified keep the session's value; others are 
still applied.
+      assert(inside == ((staticValue, coreValue, "7")))

Review Comment:
   **Nit (P3):** This assertion holds for both values of `v`, because the 
effective conf keeps the session's value whether or not the requested value 
differs, and no test in this PR captures logs. So deleting the `logWarning`, 
inverting `conf.getOption(k).contains(v)`, or warning on every call would leave 
both `SparkSessionUtilsSuite` tests and the new `TriggeredGraphExecutionSuite` 
test passing. Now that this warning replaces the failure as the only signal 
that a requested setting was ignored, could the test capture the helper's log 
with `withLogAppender` (SparkTestSuite.scala:216) and assert one warning naming 
the key for the differing value and none for the matching one, both for the 
static key and for a core key the session already holds? Asserting on the key 
rather than the value keeps the test valid if the value is later redacted.
   
   **Verification:**
   - **Regression:** With the helper's log output captured, a static key 
requested at a value different from the session's yields exactly one skip 
warning naming the key, and the same key requested at the session's value 
yields none.
   - **Regression:** With the same capture, a registered Spark core key 
requested at the value the session already holds yields no skip warning, and 
the same key at a different value yields one.



##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/util/SparkSessionUtils.scala:
##########
@@ -17,17 +17,32 @@
 
 package org.apache.spark.sql.pipelines.util
 
+import org.apache.spark.internal.{Logging, LogKeys}
+import org.apache.spark.internal.config.ConfigEntry
 import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.internal.SQLConf
 
-object SparkSessionUtils {
+object SparkSessionUtils extends Logging {
 
   /**
    * Sets all SQL configurations specified in `pairs`, calls `f`, and then 
restores all SQL
    * configurations.
+   *
+   * Configurations that cannot be changed on a running session (static SQL 
configurations and
+   * Spark core configurations) are skipped: a pipeline's `configuration` is 
also used to create
+   * the session, which is the only place where those can take effect.
    */
   def withSqlConf[T](spark: SparkSession, pairs: (String, String)*)(f: => T): 
T = {
     val conf = spark.conf
-    val (keys, values) = pairs.unzip
+    val (runtimePairs, nonRuntimePairs) = pairs.partition { case (k, _) => 
isRuntimeConf(k) }
+    nonRuntimePairs.foreach { case (k, v) =>
+      if (!conf.getOption(k).contains(v)) {

Review Comment:
   **Nit (P3):** This compares raw strings, but Spark stores 
`spark.sql.warehouse.dir` qualified: when it builds the session, `SharedState` 
qualifies the requested directory with `qualifyWarehousePath` and writes, for 
example, `file:/tmp/wh` for `/tmp/wh` into the conf the session reads 
(SharedState.scala:80-92, 301-306). In the CLI's default local mode the spec's 
configuration reaches the local server's SparkConf, so the session is created 
with the requested directory, and the same raw `/tmp/wh` also arrives as the 
graph's default SQL conf. Reading the code, every flow start then compares 
`/tmp/wh` with `file:/tmp/wh` and logs `Ignoring 
spark.sql.warehouse.dir=/tmp/wh because it cannot be modified on a running 
session. Set it when the session is created, ...`, although the directory was 
set there and is in effect. A relative or `file:///` path behaves the same. 
Qualifying the requested directory with `SharedState.qualifyWarehousePath` and 
the session's Hadoop configuration before co
 mparing, and keeping the raw value if that fails, would warn only when the 
directory really differs.
   
   **Verification:**
   - **Regression:** With a log appender capturing the helper's warnings, 
running the helper with `spark.sql.warehouse.dir` set to an unqualified or 
`file:///` spelling of the session's own warehouse directory logs no skip 
warning.
   - **Behavior:** With the same capture, a `spark.sql.warehouse.dir` naming a 
different directory produces a skip warning that names the key.



##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/util/SparkSessionUtils.scala:
##########
@@ -17,17 +17,32 @@
 
 package org.apache.spark.sql.pipelines.util
 
+import org.apache.spark.internal.{Logging, LogKeys}
+import org.apache.spark.internal.config.ConfigEntry
 import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.internal.SQLConf
 
-object SparkSessionUtils {
+object SparkSessionUtils extends Logging {
 
   /**
    * Sets all SQL configurations specified in `pairs`, calls `f`, and then 
restores all SQL
    * configurations.
+   *
+   * Configurations that cannot be changed on a running session (static SQL 
configurations and
+   * Spark core configurations) are skipped: a pipeline's `configuration` is 
also used to create
+   * the session, which is the only place where those can take effect.
    */
   def withSqlConf[T](spark: SparkSession, pairs: (String, String)*)(f: => T): 
T = {
     val conf = spark.conf
-    val (keys, values) = pairs.unzip
+    val (runtimePairs, nonRuntimePairs) = pairs.partition { case (k, _) => 
isRuntimeConf(k) }
+    nonRuntimePairs.foreach { case (k, v) =>
+      if (!conf.getOption(k).contains(v)) {
+        logWarning(log"Ignoring ${MDC(LogKeys.CONFIG, k)}=${MDC(LogKeys.VALUE, 
v)} because it " +

Review Comment:
   **Non-blocking (P2):** Spark's `log` interpolator appends MDC values as they 
are and, with structured logging, also stores them in the `value` field, so 
this prints the requested value without redaction (Logging.scala:141-160). For 
a skipped key that holds a credential, such as `spark.authenticate.secret` (a 
core entry) or `spark.connect.authenticate.token` (a static SQL conf), both of 
which match the default `spark.redaction.regex`, the secret is written to the 
driver log at every flow start whenever the session holds a different value or 
none. Reading the code, that can happen with `--remote`, where the server's 
session was not created from the spec, and for flow-level `spark_conf` or 
pipeline SQL `SET` settings, which never reach session creation; in the CLI's 
local mode the spec's values reach the session's SparkConf and match, so 
nothing is logged. On the base branch the flow failed with an error that names 
only the key.
   
   The paths that already report settings a session cannot take keep values 
out: `SparkSession.applyModifiableSettings` warns without values and lists the 
ignored settings only at DEBUG through `conf.redactOptions`, and 
`SparkConnectConfigHandler.handleSet` with `silent` reports only the key and 
the error condition because that warning outlives the response. Passing the 
pair through the session SQL conf's `redactOptions`, which applies both 
`spark.sql.redaction.options.regex` and `spark.redaction.regex`, before logging 
would keep the key in the message and redact the value.
   
   **Verification:**
   - **Regression:** With a log appender capturing the helper's warnings, 
running the helper with `spark.authenticate.secret` set to a value that differs 
from the session's produces one warning that names the key and does not contain 
the requested secret.
   - **Behavior:** With the same capture, a differing value for a skipped key 
that matches no redaction pattern, such as `spark.sql.catalogImplementation`, 
still produces a warning that names the key and shows the requested value.



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