suhuruli commented on code in PR #60579:
URL: https://github.com/apache/spark/pull/60579#discussion_r4595992330


##########
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:
   I removed the copied `isRuntimeConf` predicate. `withSqlConf` now takes the 
classic session and, for each pair right before it is set, runs the check 
`spark.conf.set` runs itself, `RuntimeConfig.requireNonStaticConf`, wrapped in 
`Try`; only the pairs that check rejects are skipped, so the two cannot drift 
apart. With that, `spark.default.parallelism` is applied, and a registered 
Spark core key is applied whenever 
`spark.sql.legacy.setCommandRejectsSparkCoreConfs` is false. I added two tests 
to `SparkSessionUtilsSuite`: "SPARK-61825: core confs are applied when the 
session does not reject them" sets that flag to false and checks that 
`spark.buffer.size` holds the requested value inside the block, is restored 
afterwards and logs no skip warning; "SPARK-61825: spark.default.parallelism is 
applied and restored" does the same with the default flag. Both fail on the 
previous revision, and the existing test that keeps the session's values for 
`spark.sql.catalogImplementation` and `spark.
 driver.memory` still passes.



##########
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:
   I reworded the Scaladoc. It now says that every pair a running session 
cannot take is ignored, whether it comes from a pipeline's `configuration` or 
from flow-level settings such as Python `spark_conf` and SQL `SET` statements 
in pipeline source files, that such settings can only be given to the session 
when it is created, and that a driver warning is logged when the requested 
value differs. The sentence claiming those values took effect at session 
creation is gone. I also added one sentence to each of the five `spark_conf` 
parameter descriptions in `python/pyspark/pipelines/api.py` and to the 
`spark_conf` row in `docs/declarative-pipelines-programming-guide.md`: static 
SQL configurations and, by default, Spark core configurations cannot be changed 
on a running session, are ignored with a warning in the driver log and must be 
set when the session is created. The Python file passes `ruff check`.



##########
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:
   `SparkSessionUtilsSuite` now has a `withSkipWarnings` helper that captures 
the `SparkSessionUtils` logger with `withLogAppender` and returns the block's 
result together with the warnings logged. The new test "SPARK-61825: a skip 
warning names the key only when the requested value differs" checks, for 
`spark.sql.catalogImplementation` and for `spark.unsafe.exceptionOnMemoryLeak` 
(a registered core key the test session's `SparkConf` sets), that a value 
different from the session's produces exactly one warning containing the key 
and that the session's own value produces none. It asserts on the key only, so 
the redaction change leaves it valid. I confirmed it fails when the 
`logWarning` call is removed and when its condition is inverted, and passes on 
the final code. The redaction and warehouse tests reuse the same helper.



##########
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:
   The warning now runs the pair through 
`spark.sessionState.conf.redactOptions` before logging, so both 
`spark.sql.redaction.options.regex` and `spark.redaction.regex` apply, the key 
stays in the message and a matching value is replaced by the usual redaction 
text. Two tests in `SparkSessionUtilsSuite` cover it: "SPARK-61825: the skip 
warning redacts a sensitive requested value" requests 
`spark.authenticate.secret` with a value the session does not hold and checks 
that exactly one warning names the key and the value does not appear; it fails 
on the previous revision. "SPARK-61825: the skip warning shows a requested 
value that is not sensitive" requests a different 
`spark.sql.catalogImplementation` and checks that the one warning shows 
`key=value`.



##########
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:
   I added a small `isInEffect` helper in `SparkSessionUtils`. For 
`spark.sql.warehouse.dir` it qualifies the requested directory with 
`SharedState.qualifyWarehousePath` and the session's Hadoop configuration 
(`spark.sessionState.newHadoopConf()`) before comparing it with the session's 
value, and keeps the raw value if qualification throws; every other key is 
still compared as a plain string. "SPARK-61825: no skip warning for another 
spelling of the session's warehouse directory" in `SparkSessionUtilsSuite` 
requests the unqualified path and the `file:///` spelling of the session's own 
warehouse directory and checks that no warning is logged; it fails on the 
previous revision. "SPARK-61825: a skip warning for a warehouse directory that 
differs from the session's" requests a temporary directory and checks for 
exactly one warning naming the key.



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