From 95312dd16c0d5b70996fcdc3a49bcdab29fd99d2 Mon Sep 17 00:00:00 2001 From: Sunitha Kambhampati Date: Wed, 26 Oct 2016 13:11:56 -0700 Subject: [PATCH 1/2] Add check for global temp tables in HiveSessionCatalog lookupRelation --- .../apache/spark/sql/hive/HiveSessionCatalog.scala | 10 ++++++++-- .../spark/sql/hive/execution/SQLQuerySuite.scala | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveSessionCatalog.scala b/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveSessionCatalog.scala index 85ecf0ce70756..4f2910abfd216 100644 --- a/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveSessionCatalog.scala +++ b/sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveSessionCatalog.scala @@ -27,7 +27,7 @@ import org.apache.hadoop.hive.ql.udf.generic.{AbstractGenericUDAFResolver, Gener import org.apache.spark.sql.{AnalysisException, SparkSession} import org.apache.spark.sql.catalyst.{FunctionIdentifier, TableIdentifier} -import org.apache.spark.sql.catalyst.analysis.FunctionRegistry +import org.apache.spark.sql.catalyst.analysis.{FunctionRegistry, NoSuchTableException} import org.apache.spark.sql.catalyst.analysis.FunctionRegistry.FunctionBuilder import org.apache.spark.sql.catalyst.catalog.{FunctionResourceLoader, GlobalTempViewManager, SessionCatalog} import org.apache.spark.sql.catalyst.expressions.{Cast, Expression, ExpressionInfo} @@ -57,7 +57,13 @@ private[sql] class HiveSessionCatalog( override def lookupRelation(name: TableIdentifier, alias: Option[String]): LogicalPlan = { val table = formatTableName(name.table) - if (name.database.isDefined || !tempTables.contains(table)) { + val db = formatDatabaseName(name.database.getOrElse(currentDb)) + if (db == globalTempViewManager.database) { + val relationAlias = alias.getOrElse(table) + globalTempViewManager.get(table).map { viewDef => + SubqueryAlias(relationAlias, viewDef, Some(name)) + }.getOrElse(throw new NoSuchTableException(db, table)) + } else if (name.database.isDefined || !tempTables.contains(table)) { val database = name.database.map(formatDatabaseName) val newName = name.copy(database = database, table = table) metastoreCatalog.lookupRelation(newName, alias) diff --git a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala index 6f2a16662bf10..a98338518faf7 100644 --- a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala +++ b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala @@ -65,6 +65,20 @@ class SQLQuerySuite extends QueryTest with SQLTestUtils with TestHiveSingleton { import hiveContext._ import spark.implicits._ + test("query global temp view") { + val df = Seq(1).toDF("i1") + df.createGlobalTempView("tbl1") + checkAnswer(spark.sql("select * from global_temp.tbl1"), Row(1)) + spark.sql("drop view global_temp.tbl1") + } + + test("non-existent global temp view") { + val message = intercept[AnalysisException] { + spark.sql("select * from global_temp.nonexistentview") + }.getMessage + assert(message.contains("Table or view not found")) + } + test("script") { val scriptFilePath = getTestResourcePath("test_script.sh") if (testCommandAvailable("bash") && testCommandAvailable("echo | sed")) { From 387b28a1e339187de82fa9befa63eb573ba5d1dc Mon Sep 17 00:00:00 2001 From: Sunitha Kambhampati Date: Thu, 27 Oct 2016 09:17:51 -0700 Subject: [PATCH 2/2] Test only change - Get the global temp db from the conf instead of hardcoding it --- .../apache/spark/sql/hive/execution/SQLQuerySuite.scala | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala index a98338518faf7..b1b84643fffd3 100644 --- a/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala +++ b/sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala @@ -68,13 +68,15 @@ class SQLQuerySuite extends QueryTest with SQLTestUtils with TestHiveSingleton { test("query global temp view") { val df = Seq(1).toDF("i1") df.createGlobalTempView("tbl1") - checkAnswer(spark.sql("select * from global_temp.tbl1"), Row(1)) - spark.sql("drop view global_temp.tbl1") + val global_temp_db = spark.conf.get("spark.sql.globalTempDatabase") + checkAnswer(spark.sql(s"select * from ${global_temp_db}.tbl1"), Row(1)) + spark.sql(s"drop view ${global_temp_db}.tbl1") } test("non-existent global temp view") { + val global_temp_db = spark.conf.get("spark.sql.globalTempDatabase") val message = intercept[AnalysisException] { - spark.sql("select * from global_temp.nonexistentview") + spark.sql(s"select * from ${global_temp_db}.nonexistentview") }.getMessage assert(message.contains("Table or view not found")) }