Github user zhzhan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2887#discussion_r19443373
  
    --- Diff: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveContext.scala ---
    @@ -223,21 +223,23 @@ class HiveContext(sc: SparkContext) extends 
SQLContext(sc) {
       }
     
       /**
    -   * SQLConf and HiveConf contracts: when the hive session is first 
initialized, params in
    -   * HiveConf will get picked up by the SQLConf.  Additionally, any 
properties set by
    -   * set() or a SET command inside sql() will be set in the SQLConf *as 
well as*
    -   * in the HiveConf.
    +   * SQLConf and HiveConf contracts:
    +   *
    +   * 1. reuse existing started SessionState if any
    +   * 2. when the Hive session is first initialized, params in HiveConf 
will get picked up by the
    +   *    SQLConf.  Additionally, any properties set by set() or a SET 
command inside sql() will be
    +   *    set in the SQLConf *as well as* in the HiveConf.
        */
    -  @transient protected[hive] lazy val hiveconf = new 
HiveConf(classOf[SessionState])
    -  @transient protected[hive] lazy val sessionState = {
    -    val ss = new SessionState(hiveconf)
    -    setConf(hiveconf.getAllProperties)  // Have SQLConf pick up the 
initial set of HiveConf.
    -    SessionState.start(ss)
    -    ss.err = new PrintStream(outputBuffer, true, "UTF-8")
    -    ss.out = new PrintStream(outputBuffer, true, "UTF-8")
    -
    -    ss
    -  }
    +  @transient protected[hive] lazy val (hiveconf, sessionState) =
    +    Option(SessionState.get())
    +      .orElse(Some(new SessionState(new HiveConf(classOf[SessionState]))))
    +      .map { state =>
    +        setConf(state.getConf.getAllProperties)
    +        if (state.out == null) state.out = new PrintStream(outputBuffer, 
true, "UTF-8")
    +        if (state.err == null) state.err = new PrintStream(outputBuffer, 
true, "UTF-8")
    +        (state.getConf, state)
    +      }
    +      .get
    --- End diff --
    
    Should SessionState.start(state) be invoked here? Otherwise, it relies on 
other code to initialize the state, and we lose the track whether the state is 
initialized, and other code may call SessionState.start() multiple times, 
resulting in unexpected behavior. Correct me if I am wrong.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to