Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,21 @@ case class PreprocessTableInsertion(conf: SQLConf) extends Rule[LogicalPlan] {
}
}

/**
* A rule to check whether the functions are supported only when Hive support is enabled
*/
object HiveOnlyCheck extends (LogicalPlan => Unit) {
def apply(plan: LogicalPlan): Unit = {
plan.foreach {
case CreateTable(tableDesc, _, Some(_))
if tableDesc.provider.get == "hive" =>
throw new AnalysisException("Hive support is required to use CREATE Hive TABLE AS SELECT")

case _ => // OK
}
}
}

/**
* A rule to do various checks before inserting into or writing to a data source table.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ private[sql] class SessionState(sparkSession: SparkSession) {
DataSourceAnalysis(conf) ::
(if (conf.runSQLonFile) new ResolveDataSource(sparkSession) :: Nil else Nil)

override val extendedCheckRules = Seq(datasources.PreWriteCheck(conf, catalog))
override val extendedCheckRules =
Seq(PreWriteCheck(conf, catalog), HiveOnlyCheck)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,34 @@ class DDLSuite extends QueryTest with SharedSQLContext with BeforeAndAfterEach {
"WITH SERDEPROPERTIES ('spark.sql.sources.me'='anything')")
}

test("Create Hive Table As Select") {
import testImplicits._
Copy link
Member

@viirya viirya Aug 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: import testImplicits._ is used in many test cases in DDLSuite. We can import it only once.

withTable("t", "t1") {
var e = intercept[AnalysisException] {
sql("CREATE TABLE t SELECT 1 as a, 1 as b")
}.getMessage
assert(e.contains("Hive support is required to use CREATE Hive TABLE AS SELECT"))

spark.range(1).select('id as 'a, 'id as 'b).write.saveAsTable("t1")
e = intercept[AnalysisException] {
sql("CREATE TABLE t SELECT a, b from t1")
}.getMessage
assert(e.contains("Hive support is required to use CREATE Hive TABLE AS SELECT"))
}
}

test("Create Data Source Table As Select") {
import testImplicits._
withTable("t", "t1", "t2") {
sql("CREATE TABLE t USING parquet SELECT 1 as a, 1 as b")
checkAnswer(spark.table("t"), Row(1, 1) :: Nil)

spark.range(1).select('id as 'a, 'id as 'b).write.saveAsTable("t1")
sql("CREATE TABLE t2 USING parquet SELECT a, b from t1")
checkAnswer(spark.table("t2"), spark.table("t1"))
}
}

test("drop current database") {
sql("CREATE DATABASE temp")
sql("USE temp")
Expand Down