Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ case class CreateDataSourceTableCommand(table: CatalogTable, ignoreIfExists: Boo
className = table.provider.get,
bucketSpec = table.bucketSpec,
options = table.storage.properties ++ pathOption,
catalogTable = Some(tableWithDefaultOptions)).resolveRelation()
// As discussed in SPARK-19583, we don't check if the location is existed
catalogTable = Some(tableWithDefaultOptions)).resolveRelation(checkFilesExist = false)

val partitionColumnNames = if (table.schema.nonEmpty) {
table.partitionColumnNames
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1952,4 +1952,49 @@ class DDLSuite extends QueryTest with SharedSQLContext with BeforeAndAfterEach {
}
}
}

test("create datasource table with an non-existent location") {
withTable("t", "t1") {
withTempDir {
dir =>
Copy link
Member

Choose a reason for hiding this comment

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

Nit: style issue. the above two lines can be combined together.

dir.delete()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

there are lots of dir existed test case in DDLSuit or HiveDDLSuit, so we just add non-existent test cases

Copy link
Member

Choose a reason for hiding this comment

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

withTempPath?

spark.sql(
s"""
|CREATE TABLE t(a int, b int)
|USING parquet
|LOCATION '$dir'
""".stripMargin)
Copy link
Member

Choose a reason for hiding this comment

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

The test case is already pretty long. Please reduce the sql statement to a single line.


val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
assert(table.location.stripSuffix("/") == dir.getAbsolutePath.stripSuffix("/"))

spark.sql("INSERT INTO TABLE t SELECT 1, 2")
assert(dir.exists())

checkAnswer(spark.table("t"), Row(1, 2))
}
// partition table
withTempDir {
dir =>
dir.delete()
spark.sql(
s"""
|CREATE TABLE t1(a int, b int)
|USING parquet
|PARTITIONED BY(a)
|LOCATION '$dir'
""".stripMargin)

val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1"))
assert(table.location.stripSuffix("/") == dir.getAbsolutePath.stripSuffix("/"))

spark.sql("INSERT INTO TABLE t1 PARTITION(a=1) SELECT 2")

val partDir = new File(dir, "a=1")
assert(partDir.exists())

checkAnswer(spark.table("t1"), Row(2, 1))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1587,4 +1587,96 @@ class HiveDDLSuite
}
}
}

test("create datasource table with an non-existent location") {
withTable("t", "t1") {
withTempDir {
dir =>
dir.delete()
spark.sql(
s"""
|CREATE TABLE t(a int, b int)
|USING parquet
|LOCATION '$dir'
""".stripMargin)

val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
assert(table.location.stripSuffix("/") == dir.getAbsolutePath.stripSuffix("/"))

spark.sql("INSERT INTO TABLE t SELECT 1, 2")
assert(dir.exists())

checkAnswer(spark.table("t"), Row(1, 2))
}
// partition table
withTempDir {
dir =>
dir.delete()
spark.sql(
s"""
|CREATE TABLE t1(a int, b int)
|USING parquet
|PARTITIONED BY(a)
|LOCATION '$dir'
""".stripMargin)

val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1"))
assert(table.location.stripSuffix("/") == dir.getAbsolutePath.stripSuffix("/"))

spark.sql("INSERT INTO TABLE t1 PARTITION(a=1) SELECT 2")

val partDir = new File(dir, "a=1")
assert(partDir.exists())

checkAnswer(spark.table("t1"), Row(2, 1))
}
}
}

test("create hive table with an non-existent location") {
withTable("t", "t1") {
withTempDir {
dir =>
dir.delete()
spark.sql(
s"""
|CREATE TABLE t(a int, b int)
|USING hive
|LOCATION '$dir'
""".stripMargin)

val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
val expectedPath = s"file:${dir.getAbsolutePath.stripSuffix("/")}"
assert(table.location.stripSuffix("/") == expectedPath)

spark.sql("INSERT INTO TABLE t SELECT 1, 2")
assert(dir.exists())

checkAnswer(spark.table("t"), Row(1, 2))
}
// partition table
withTempDir {
dir =>
dir.delete()
spark.sql(
s"""
|CREATE TABLE t1(a int, b int)
|USING hive
|PARTITIONED BY(a)
|LOCATION '$dir'
""".stripMargin)

val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1"))
val expectedPath = s"file:${dir.getAbsolutePath.stripSuffix("/")}"
assert(table.location.stripSuffix("/") == expectedPath)

spark.sql("INSERT INTO TABLE t1 PARTITION(a=1) SELECT 2")

val partDir = new File(dir, "a=1")
assert(partDir.exists())

checkAnswer(spark.table("t1"), Row(2, 1))
}
}
}
}