aboutsummaryrefslogtreecommitdiff
path: root/sql/core/src/test
diff options
context:
space:
mode:
authorWenchen Fan <wenchen@databricks.com>2017-01-17 12:54:50 +0800
committerWenchen Fan <wenchen@databricks.com>2017-01-17 12:54:50 +0800
commit18ee55dd5de0597d7fb69e8e16ac3744356a6918 (patch)
treeeb2b6ac905b50ff621923da1d601c104f2cbb418 /sql/core/src/test
parentf8db8945f25cb884278ff8841bac5f6f28f0dec6 (diff)
downloadspark-18ee55dd5de0597d7fb69e8e16ac3744356a6918.tar.gz
spark-18ee55dd5de0597d7fb69e8e16ac3744356a6918.tar.bz2
spark-18ee55dd5de0597d7fb69e8e16ac3744356a6918.zip
[SPARK-19148][SQL] do not expose the external table concept in Catalog
## What changes were proposed in this pull request? In https://github.com/apache/spark/pull/16296 , we reached a consensus that we should hide the external/managed table concept to users and only expose custom table path. This PR renames `Catalog.createExternalTable` to `createTable`(still keep the old versions for backward compatibility), and only set the table type to EXTERNAL if `path` is specified in options. ## How was this patch tested? new tests in `CatalogSuite` Author: Wenchen Fan <wenchen@databricks.com> Closes #16528 from cloud-fan/create-table.
Diffstat (limited to 'sql/core/src/test')
-rw-r--r--sql/core/src/test/scala/org/apache/spark/sql/internal/CatalogSuite.scala66
1 files changed, 49 insertions, 17 deletions
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/internal/CatalogSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/internal/CatalogSuite.scala
index 5dd04543ed..801912f441 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/internal/CatalogSuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/internal/CatalogSuite.scala
@@ -17,6 +17,9 @@
package org.apache.spark.sql.internal
+import java.io.File
+import java.net.URI
+
import org.scalatest.BeforeAndAfterEach
import org.apache.spark.SparkFunSuite
@@ -27,7 +30,7 @@ import org.apache.spark.sql.catalyst.catalog._
import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionInfo}
import org.apache.spark.sql.catalyst.plans.logical.Range
import org.apache.spark.sql.test.SharedSQLContext
-import org.apache.spark.sql.types.{IntegerType, StructType}
+import org.apache.spark.sql.types.StructType
/**
@@ -37,6 +40,7 @@ class CatalogSuite
extends SparkFunSuite
with BeforeAndAfterEach
with SharedSQLContext {
+ import testImplicits._
private def sessionCatalog: SessionCatalog = spark.sessionState.catalog
@@ -306,22 +310,6 @@ class CatalogSuite
columnFields.foreach { f => assert(columnString.contains(f.toString)) }
}
- test("createExternalTable should fail if path is not given for file-based data source") {
- val e = intercept[AnalysisException] {
- spark.catalog.createExternalTable("tbl", "json", Map.empty[String, String])
- }
- assert(e.message.contains("Unable to infer schema"))
-
- val e2 = intercept[AnalysisException] {
- spark.catalog.createExternalTable(
- "tbl",
- "json",
- new StructType().add("i", IntegerType),
- Map.empty[String, String])
- }
- assert(e2.message == "Cannot create a file-based external data source table without path")
- }
-
test("dropTempView should not un-cache and drop metastore table if a same-name table exists") {
withTable("same_name") {
spark.range(10).write.saveAsTable("same_name")
@@ -460,6 +448,50 @@ class CatalogSuite
}
}
+ test("createTable with 'path' in options") {
+ withTable("t") {
+ withTempDir { dir =>
+ spark.catalog.createTable(
+ tableName = "t",
+ source = "json",
+ schema = new StructType().add("i", "int"),
+ options = Map("path" -> dir.getAbsolutePath))
+ val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
+ assert(table.tableType == CatalogTableType.EXTERNAL)
+ assert(table.storage.locationUri.get == dir.getAbsolutePath)
+
+ Seq((1)).toDF("i").write.insertInto("t")
+ assert(dir.exists() && dir.listFiles().nonEmpty)
+
+ sql("DROP TABLE t")
+ // the table path and data files are still there after DROP TABLE, if custom table path is
+ // specified.
+ assert(dir.exists() && dir.listFiles().nonEmpty)
+ }
+ }
+ }
+
+ test("createTable without 'path' in options") {
+ withTable("t") {
+ spark.catalog.createTable(
+ tableName = "t",
+ source = "json",
+ schema = new StructType().add("i", "int"),
+ options = Map.empty[String, String])
+ val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
+ assert(table.tableType == CatalogTableType.MANAGED)
+ val tablePath = new File(new URI(table.storage.locationUri.get))
+ assert(tablePath.exists() && tablePath.listFiles().isEmpty)
+
+ Seq((1)).toDF("i").write.insertInto("t")
+ assert(tablePath.listFiles().nonEmpty)
+
+ sql("DROP TABLE t")
+ // the table path is removed after DROP TABLE, if custom table path is not specified.
+ assert(!tablePath.exists())
+ }
+ }
+
// TODO: add tests for the rest of them
}