@@ -24,14 +24,14 @@ import scala.collection.JavaConverters._
24
24
import scala .language .reflectiveCalls
25
25
26
26
import org .apache .hadoop .fs .Path
27
+ import org .apache .hadoop .hive .cli .CliSessionState
27
28
import org .apache .hadoop .hive .conf .HiveConf
28
29
import org .apache .hadoop .hive .metastore .{TableType => HTableType }
29
30
import org .apache .hadoop .hive .metastore .api .{Database , FieldSchema }
30
31
import org .apache .hadoop .hive .ql .{metadata , Driver }
31
32
import org .apache .hadoop .hive .ql .metadata .Hive
32
33
import org .apache .hadoop .hive .ql .processors ._
33
34
import org .apache .hadoop .hive .ql .session .SessionState
34
- import org .apache .hadoop .hive .shims .{HadoopShims , ShimLoader }
35
35
import org .apache .hadoop .security .UserGroupInformation
36
36
37
37
import org .apache .spark .{Logging , SparkConf , SparkException }
@@ -104,29 +104,39 @@ private[hive] class HiveClientImpl(
104
104
}
105
105
106
106
val ret = try {
107
- val initialConf = new HiveConf (classOf [SessionState ])
108
- // HiveConf is a Hadoop Configuration, which has a field of classLoader and
109
- // the initial value will be the current thread's context class loader
110
- // (i.e. initClassLoader at here).
111
- // We call initialConf.setClassLoader(initClassLoader) at here to make
112
- // this action explicit.
113
- initialConf.setClassLoader(initClassLoader)
114
- config.foreach { case (k, v) =>
115
- if (k.toLowerCase.contains(" password" )) {
116
- logDebug(s " Hive Config: $k=xxx " )
117
- } else {
118
- logDebug(s " Hive Config: $k= $v" )
107
+ // originState will be created if not exists, will never be null
108
+ val originalState = SessionState .get()
109
+ if (originalState.isInstanceOf [CliSessionState ]) {
110
+ // In `SparkSQLCLIDriver`, we have already started a `CliSessionState`,
111
+ // which contains information like configurations from command line. Later
112
+ // we call `SparkSQLEnv.init()` there, which would run into this part again.
113
+ // so we should keep `conf` and reuse the existing instance of `CliSessionState`.
114
+ originalState
115
+ } else {
116
+ val initialConf = new HiveConf (classOf [SessionState ])
117
+ // HiveConf is a Hadoop Configuration, which has a field of classLoader and
118
+ // the initial value will be the current thread's context class loader
119
+ // (i.e. initClassLoader at here).
120
+ // We call initialConf.setClassLoader(initClassLoader) at here to make
121
+ // this action explicit.
122
+ initialConf.setClassLoader(initClassLoader)
123
+ config.foreach { case (k, v) =>
124
+ if (k.toLowerCase.contains(" password" )) {
125
+ logDebug(s " Hive Config: $k=xxx " )
126
+ } else {
127
+ logDebug(s " Hive Config: $k= $v" )
128
+ }
129
+ initialConf.set(k, v)
119
130
}
120
- initialConf.set(k, v)
121
- }
122
- val state = new SessionState (initialConf)
123
- if (clientLoader.cachedHive != null ) {
124
- Hive .set(clientLoader.cachedHive.asInstanceOf [Hive ])
131
+ val state = new SessionState (initialConf)
132
+ if (clientLoader.cachedHive != null ) {
133
+ Hive .set(clientLoader.cachedHive.asInstanceOf [Hive ])
134
+ }
135
+ SessionState .start(state)
136
+ state.out = new PrintStream (outputBuffer, true , " UTF-8" )
137
+ state.err = new PrintStream (outputBuffer, true , " UTF-8" )
138
+ state
125
139
}
126
- SessionState .start(state)
127
- state.out = new PrintStream (outputBuffer, true , " UTF-8" )
128
- state.err = new PrintStream (outputBuffer, true , " UTF-8" )
129
- state
130
140
} finally {
131
141
Thread .currentThread().setContextClassLoader(original)
132
142
}
0 commit comments