Github user liancheng commented on a diff in the pull request:
https://github.com/apache/spark/pull/2967#discussion_r19457392
--- Diff:
sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveContext.scala ---
@@ -230,13 +230,21 @@ class HiveContext(sc: SparkContext) extends
SQLContext(sc) {
* in the HiveConf.
*/
@transient 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")
+ /**
+ * If the thread local sessionstate is not set, start a new SessionState
+ * SessionState.start will put ss to thread local
+ * @return
+ */
+ def getSessionState() = {
+ var ss = SessionState.get
+ if (ss == null) {
+ 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")
+ }
--- End diff --
Several comments here:
1. `HiveContext.hiveconf` should be retrieved from the current
`SessionState` if `SessionState.get()` is non-null. That's why `hiveconf` and
`sessionState` are always initialized together in
[#2887](https://github.com/apache/spark/pull/2887/files#diff-ff50aea397a607b79df9bec6f2a841dbR233)
1. IO redirection and Hive properties propagation also need to be performed
when `SessionState.get()` returns a non-null `SessionState` started elsewhere.
This is required by Spark SQL CLI since `SparkSQLCLIDriver` creates a
`CliSessionState` before `HiveContext` initialization.
1. I guess you're trying to make `HiveContext` play well with multi-session
scenarios by introducing `getSessionState()` here? I had also considered this
issue earlier, but unfortunately IMO proper multiple Hive session support may
require major refactoring over `HiveContext` (e.g. make it absolutely thread
safe). Also, Spark SQL Hive support will probably be reimplemented against the
up coming foreign data source API. So I'd rather defer this major task later.
(As a globally used thread local object, Hive `SessionState` is really
annoying...)
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]