forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-26794][SQL] SparkSession enableHiveSupport does not point to h…
…ive but in-memory while the SparkContext exists ## What changes were proposed in this pull request? ```java public class SqlDemo { public static void main(final String[] args) throws Exception { SparkConf conf = new SparkConf().setAppName("spark-sql-demo"); JavaSparkContext sc = new JavaSparkContext(conf); SparkSession ss = SparkSession.builder().enableHiveSupport().getOrCreate(); ss.sql("show databases").show(); } } ``` Before https://issues.apache.org/jira/browse/SPARK-20946, the demo above point to the right hive metastore if the hive-site.xml is present. But now it can only point to the default in-memory one. Catalog is now as a variable shared across SparkSessions, it is instantiated with SparkContext's conf. After https://issues.apache.org/jira/browse/SPARK-20946, Session level configs are not pass to SparkContext's conf anymore, so the enableHiveSupport API takes no affect on the catalog instance. You can set spark.sql.catalogImplementation=hive application wide to solve the problem, or never create a sc before you call SparkSession.builder().enableHiveSupport().getOrCreate() Here we respect the SparkSession level configuration at the first time to generate catalog within SharedState ## How was this patch tested? 1. add ut 2. manually ```scala test("enableHiveSupport has right to determine the catalog while using an existing sc") { val conf = new SparkConf().setMaster("local").setAppName("SharedState Test") val sc = SparkContext.getOrCreate(conf) val ss = SparkSession.builder().enableHiveSupport().getOrCreate() assert(ss.sharedState.externalCatalog.unwrapped.isInstanceOf[HiveExternalCatalog], "The catalog should be hive ") val ss2 = SparkSession.builder().getOrCreate() assert(ss2.sharedState.externalCatalog.unwrapped.isInstanceOf[HiveExternalCatalog], "The catalog should be shared across sessions") } ``` Without this fix, the above test will fail. You can apply it to `org.apache.spark.sql.hive.HiveSharedStateSuite`, and run, ```sbt ./build/sbt -Phadoop-2.7 -Phive "hive/testOnly org.apache.spark.sql.hive.HiveSharedStateSuite" ``` to verify. Closes apache#23709 from yaooqinn/SPARK-26794. Authored-by: Kent Yao <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
- Loading branch information
Showing
4 changed files
with
99 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
sql/hive/src/test/scala/org/apache/spark/sql/hive/HiveSharedStateSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.hive | ||
|
||
import org.apache.hadoop.hive.conf.HiveConf.ConfVars | ||
|
||
import org.apache.spark.{SparkConf, SparkContext, SparkFunSuite} | ||
import org.apache.spark.sql.internal.SharedState | ||
import org.apache.spark.sql.internal.StaticSQLConf._ | ||
import org.apache.spark.util.Utils | ||
|
||
class HiveSharedStateSuite extends SparkFunSuite { | ||
|
||
test("initial configs should be passed to SharedState but not SparkContext") { | ||
val conf = new SparkConf().setMaster("local").setAppName("SharedState Test") | ||
val sc = SparkContext.getOrCreate(conf) | ||
val invalidPath = "invalid/path" | ||
val metastorePath = Utils.createTempDir() | ||
val tmpDb = "tmp_db" | ||
|
||
// The initial configs used to generate SharedState, none of these should affect the global | ||
// shared SparkContext's configurations. Especially, all these configs are passed to the cloned | ||
// confs inside SharedState except metastore warehouse dir. | ||
val initialConfigs = Map("spark.foo" -> "bar", | ||
WAREHOUSE_PATH.key -> invalidPath, | ||
ConfVars.METASTOREWAREHOUSE.varname -> invalidPath, | ||
CATALOG_IMPLEMENTATION.key -> "hive", | ||
ConfVars.METASTORECONNECTURLKEY.varname -> | ||
s"jdbc:derby:;databaseName=$metastorePath/metastore_db;create=true", | ||
GLOBAL_TEMP_DATABASE.key -> tmpDb) | ||
|
||
val state = new SharedState(sc, initialConfigs) | ||
assert(state.warehousePath !== invalidPath, "warehouse path can't determine by session options") | ||
assert(sc.conf.get(WAREHOUSE_PATH.key) !== invalidPath, | ||
"warehouse conf in session options can't affect application wide spark conf") | ||
assert(sc.hadoopConfiguration.get(ConfVars.METASTOREWAREHOUSE.varname) !== invalidPath, | ||
"warehouse conf in session options can't affect application wide hadoop conf") | ||
|
||
assert(!state.sparkContext.conf.contains("spark.foo"), | ||
"static spark conf should not be affected by session") | ||
assert(state.externalCatalog.unwrapped.isInstanceOf[HiveExternalCatalog], | ||
"Initial SparkSession options can determine the catalog") | ||
val client = state.externalCatalog.unwrapped.asInstanceOf[HiveExternalCatalog].client | ||
assert(client.getConf("spark.foo", "") === "bar", | ||
"session level conf should be passed to catalog") | ||
assert(client.getConf(ConfVars.METASTOREWAREHOUSE.varname, invalidPath) !== invalidPath, | ||
"session level conf should be passed to catalog except warehouse dir") | ||
|
||
assert(state.globalTempViewManager.database === tmpDb) | ||
} | ||
} |