Skip to content

Commit f0e3b71

Browse files
rxinmarmbrus
authored andcommitted
[SPARK-5840][SQL] HiveContext cannot be serialized due to tuple extraction
Also added test cases for checking the serializability of HiveContext and SQLContext. Author: Reynold Xin <[email protected]> Closes #4628 from rxin/SPARK-5840 and squashes the following commits: ecb3bcd [Reynold Xin] test cases and reviews. 55eb822 [Reynold Xin] [SPARK-5840][SQL] HiveContext cannot be serialized due to tuple extraction.
1 parent a8eb92d commit f0e3b71

File tree

3 files changed

+84
-16
lines changed

3 files changed

+84
-16
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql
19+
20+
import org.scalatest.FunSuite
21+
22+
import org.apache.spark.SparkConf
23+
import org.apache.spark.serializer.JavaSerializer
24+
import org.apache.spark.sql.test.TestSQLContext
25+
26+
class SerializationSuite extends FunSuite {
27+
28+
test("[SPARK-5235] SQLContext should be serializable") {
29+
val sqlContext = new SQLContext(TestSQLContext.sparkContext)
30+
new JavaSerializer(new SparkConf()).newInstance().serialize(sqlContext)
31+
}
32+
}

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveContext.scala

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -222,22 +222,25 @@ class HiveContext(sc: SparkContext) extends SQLContext(sc) {
222222
* SQLConf. Additionally, any properties set by set() or a SET command inside sql() will be
223223
* set in the SQLConf *as well as* in the HiveConf.
224224
*/
225-
@transient protected[hive] lazy val (hiveconf, sessionState) =
226-
Option(SessionState.get())
227-
.orElse {
228-
val newState = new SessionState(new HiveConf(classOf[SessionState]))
229-
// Only starts newly created `SessionState` instance. Any existing `SessionState` instance
230-
// returned by `SessionState.get()` must be the most recently started one.
231-
SessionState.start(newState)
232-
Some(newState)
233-
}
234-
.map { state =>
235-
setConf(state.getConf.getAllProperties)
236-
if (state.out == null) state.out = new PrintStream(outputBuffer, true, "UTF-8")
237-
if (state.err == null) state.err = new PrintStream(outputBuffer, true, "UTF-8")
238-
(state.getConf, state)
239-
}
240-
.get
225+
@transient protected[hive] lazy val sessionState: SessionState = {
226+
var state = SessionState.get()
227+
if (state == null) {
228+
state = new SessionState(new HiveConf(classOf[SessionState]))
229+
SessionState.start(state)
230+
}
231+
if (state.out == null) {
232+
state.out = new PrintStream(outputBuffer, true, "UTF-8")
233+
}
234+
if (state.err == null) {
235+
state.err = new PrintStream(outputBuffer, true, "UTF-8")
236+
}
237+
state
238+
}
239+
240+
@transient protected[hive] lazy val hiveconf: HiveConf = {
241+
setConf(sessionState.getConf.getAllProperties)
242+
sessionState.getConf
243+
}
241244

242245
override def setConf(key: String, value: String): Unit = {
243246
super.setConf(key, value)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.hive
19+
20+
import org.scalatest.FunSuite
21+
22+
import org.apache.spark.SparkConf
23+
import org.apache.spark.serializer.JavaSerializer
24+
import org.apache.spark.sql.hive.test.TestHive
25+
26+
class SerializationSuite extends FunSuite {
27+
28+
test("[SPARK-5840] HiveContext should be serializable") {
29+
val hiveContext = new HiveContext(TestHive.sparkContext)
30+
hiveContext.hiveconf
31+
new JavaSerializer(new SparkConf()).newInstance().serialize(hiveContext)
32+
}
33+
}

0 commit comments

Comments
 (0)