yaooqinn opened a new pull request #30045:
URL: https://github.com/apache/spark/pull/30045


   
   ### What changes were proposed in this pull request?
   
   ####  case
   
   the case here covers the static and dynamic SQL configs behavior in 
`sharedState` and `sessionState`,  and the specially handled config 
`spark.sql.warehouse.dir`
   
   ```scala
   
   import java.lang.reflect.Field
   
   import org.apache.spark.sql.SparkSession
   import org.apache.spark.{SparkConf, SparkContext}
   
   object WarehouseSCBeforeSS extends App {
   
     val wh = "spark.sql.warehouse.dir"
     val td = "spark.sql.globalTempDatabase"
     val custom = "spark.sql.custom"
   
     val conf = new SparkConf()
       .setMaster("local")
       .setAppName("SPARK-32991")
       .set(wh, "./data1")
       .set(td, "bob")
   
     val sc = new SparkContext(conf)
   
     val spark = SparkSession.builder()
       .config(wh, "./data2")
       .config(td, "alice")
       .config(custom, "kyao")
       .getOrCreate()
   
     val confField: Field = spark.sharedState.getClass.getDeclaredField("conf")
     confField.setAccessible(true)
     private val shared: SparkConf = 
confField.get(spark.sharedState).asInstanceOf[SparkConf]
     println()
     println(s"=====> SharedState: $wh=${shared.get(wh)}")
     println(s"=====> SharedState: $td=${shared.get(td)}")
     println(s"=====> SharedState: $custom=${shared.get(custom, "")}")
   
     println(s"=====> SessionState: $wh=${spark.conf.get(wh)}")
     println(s"=====> SessionState: $td=${spark.conf.get(td)}")
     println(s"=====> SessionState: $custom=${spark.conf.get(custom, "")}")
   
     val spark2 = SparkSession.builder().config(td, "fred").getOrCreate()
   
     println(s"=====> SessionState 2: $wh=${spark2.conf.get(wh)}")
     println(s"=====> SessionState 2: $td=${spark2.conf.get(td)}")
     println(s"=====> SessionState 2: $custom=${spark2.conf.get(custom, "")}")
   
     SparkSession.setActiveSession(spark)
     spark.sql("RESET")
   
     println(s"=====> SessionState RESET: $wh=${spark.conf.get(wh)}")
     println(s"=====> SessionState RESET: $td=${spark.conf.get(td)}")
     println(s"=====> SessionState RESET: $custom=${spark.conf.get(custom, 
"")}")
   
     val spark3 = SparkSession.builder().getOrCreate()
   
     println(s"=====> SessionState 3: $wh=${spark2.conf.get(wh)}")
     println(s"=====> SessionState 3: $td=${spark2.conf.get(td)}")
     println(s"=====> SessionState 3: $custom=${spark2.conf.get(custom, "")}")
   }
   ```
   
   #### outputs and analysis
   ```
   // 1. Make the cloned spark conf in shared state respect the warehouse dir 
from the 1st SparkSession
   //=====> SharedState: spark.sql.warehouse.dir=./data1
   // 2. ⏬
   //=====> SharedState: spark.sql.globalTempDatabase=alice
   //=====> SharedState: spark.sql.custom=kyao
   //=====> SessionState: spark.sql.warehouse.dir=./data2
   //=====> SessionState: spark.sql.globalTempDatabase=alice
   //=====> SessionState: spark.sql.custom=kyao
   //=====> SessionState 2: spark.sql.warehouse.dir=./data2
   //=====> SessionState 2: spark.sql.globalTempDatabase=alice
   //=====> SessionState 2: spark.sql.custom=kyao
   // 2'.🔼 OK until here
   // 3. Make the below 3 ones respect the cloned spark conf in shared state 
with issue 1 fixed
   //=====> SessionState RESET: spark.sql.warehouse.dir=./data1
   //=====> SessionState RESET: spark.sql.globalTempDatabase=bob
   //=====> SessionState RESET: spark.sql.custom=
   // 4. Then the SparkSessions created after RESET will be corrected.
   //=====> SessionState 3: spark.sql.warehouse.dir=./data1
   //=====> SessionState 3: spark.sql.globalTempDatabase=bob
   //=====> SessionState 3: spark.sql.custom=
   ```
   
   In this PR, we gather all valid config to the cloned conf of `sharedState` 
during being constructed, well, actually only `spark.sql.warehouse.dir` is 
missing. Then we use this conf as defaults for `RESET` Command.
   
   `SparkSession.clearActiveSession/clearDefaultSession` will make the shared 
state invisible and unsharable. They will be internal only soon (confirmed with 
Wenchen), so cases with them called will not be a problem.
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   bugfix for programming API to call RESET while users creating SparkContext 
first and config SparkSession later.
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as 
the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes 
- provide the console output, description and/or an example to show the 
behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to 
the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   yes, before this change when you use programming API and call RESET, all 
configs will be reset to  SparkContext.conf, now they go to 
SparkSession.sharedState.conf
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some 
test cases that check the changes thoroughly including negative and positive 
cases if possible.
   If it was tested in a way different from regular unit tests, please clarify 
how you tested step by step, ideally copy and paste-able, so that other 
reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why 
it was difficult to add.
   -->
   new tests


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

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