Skip to content

Commit 416ea37

Browse files
committed
add non-existent test case
1 parent 5a3e5ac commit 416ea37

File tree

2 files changed

+109
-85
lines changed

2 files changed

+109
-85
lines changed

sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,42 +1953,51 @@ class DDLSuite extends QueryTest with SharedSQLContext with BeforeAndAfterEach {
19531953
}
19541954
}
19551955

1956-
test("CTAS for data source table with a created location") {
1957-
withTable("t", "t1") {
1958-
withTempDir {
1959-
dir =>
1960-
spark.sql(
1961-
s"""
1962-
|CREATE TABLE t
1963-
|USING parquet
1964-
|LOCATION '$dir'
1965-
|AS SELECT 3 as a, 4 as b, 1 as c, 2 as d
1966-
""".stripMargin)
1967-
1968-
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
1969-
assert(table.location.stripSuffix("/") == dir.getAbsolutePath.stripSuffix("/"))
1970-
1971-
checkAnswer(spark.table("t"), Row(3, 4, 1, 2))
1972-
}
1973-
// partition table
1974-
withTempDir {
1975-
dir =>
1976-
spark.sql(
1977-
s"""
1978-
|CREATE TABLE t1
1979-
|USING parquet
1980-
|PARTITIONED BY(a, b)
1981-
|LOCATION '$dir'
1982-
|AS SELECT 3 as a, 4 as b, 1 as c, 2 as d
1983-
""".stripMargin)
1984-
1985-
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1"))
1986-
assert(table.location.stripSuffix("/") == dir.getAbsolutePath.stripSuffix("/"))
1987-
1988-
val partDir = new File(dir, "a=3")
1989-
assert(partDir.exists())
1990-
1991-
checkAnswer(spark.table("t1"), Row(1, 2, 3, 4))
1956+
Seq(true, false).foreach { shouldDelete =>
1957+
val tcName = if (shouldDelete) "non-existent" else "existed"
1958+
test(s"CTAS for external data source table with a $tcName location") {
1959+
withTable("t", "t1") {
1960+
withTempDir {
1961+
dir =>
1962+
if (shouldDelete) {
1963+
dir.delete()
1964+
}
1965+
spark.sql(
1966+
s"""
1967+
|CREATE TABLE t
1968+
|USING parquet
1969+
|LOCATION '$dir'
1970+
|AS SELECT 3 as a, 4 as b, 1 as c, 2 as d
1971+
""".stripMargin)
1972+
1973+
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
1974+
assert(table.location.stripSuffix("/") == dir.getAbsolutePath.stripSuffix("/"))
1975+
1976+
checkAnswer(spark.table("t"), Row(3, 4, 1, 2))
1977+
}
1978+
// partition table
1979+
withTempDir {
1980+
dir =>
1981+
if (shouldDelete) {
1982+
dir.delete()
1983+
}
1984+
spark.sql(
1985+
s"""
1986+
|CREATE TABLE t1
1987+
|USING parquet
1988+
|PARTITIONED BY(a, b)
1989+
|LOCATION '$dir'
1990+
|AS SELECT 3 as a, 4 as b, 1 as c, 2 as d
1991+
""".stripMargin)
1992+
1993+
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1"))
1994+
assert(table.location.stripSuffix("/") == dir.getAbsolutePath.stripSuffix("/"))
1995+
1996+
val partDir = new File(dir, "a=3")
1997+
assert(partDir.exists())
1998+
1999+
checkAnswer(spark.table("t1"), Row(1, 2, 3, 4))
2000+
}
19922001
}
19932002
}
19942003
}

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,80 +1588,45 @@ class HiveDDLSuite
15881588
}
15891589
}
15901590

1591-
test("CTAS for data source table with a created location") {
1592-
withTable("t", "t1") {
1593-
withTempDir {
1594-
dir =>
1595-
spark.sql(
1596-
s"""
1597-
|CREATE TABLE t
1598-
|USING parquet
1599-
|LOCATION '$dir'
1600-
|AS SELECT 3 as a, 4 as b, 1 as c, 2 as d
1601-
""".stripMargin)
1602-
1603-
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
1604-
assert(table.location.stripSuffix("/") == dir.getAbsolutePath.stripSuffix("/"))
1605-
1606-
checkAnswer(spark.table("t"), Row(3, 4, 1, 2))
1607-
}
1608-
// partition table
1609-
withTempDir {
1610-
dir =>
1611-
spark.sql(
1612-
s"""
1613-
|CREATE TABLE t1
1614-
|USING parquet
1615-
|PARTITIONED BY(a, b)
1616-
|LOCATION '$dir'
1617-
|AS SELECT 3 as a, 4 as b, 1 as c, 2 as d
1618-
""".stripMargin)
1619-
1620-
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1"))
1621-
assert(table.location.stripSuffix("/") == dir.getAbsolutePath.stripSuffix("/"))
1622-
1623-
val partDir = new File(dir, "a=3")
1624-
assert(partDir.exists())
1625-
1626-
checkAnswer(spark.table("t1"), Row(1, 2, 3, 4))
1627-
}
1628-
}
1629-
}
1630-
1631-
test("CTAS for hive table with a created location") {
1632-
withTable("t", "t1") {
1633-
withSQLConf("hive.exec.dynamic.partition.mode" -> "nonstrict") {
1591+
Seq(true, false).foreach { shouldDelete =>
1592+
val tcName = if (shouldDelete) "non-existent" else "existed"
1593+
test(s"CTAS for external data source table with a $tcName location") {
1594+
withTable("t", "t1") {
16341595
withTempDir {
16351596
dir =>
1597+
if (shouldDelete) {
1598+
dir.delete()
1599+
}
16361600
spark.sql(
16371601
s"""
16381602
|CREATE TABLE t
1639-
|USING hive
1603+
|USING parquet
16401604
|LOCATION '$dir'
16411605
|AS SELECT 3 as a, 4 as b, 1 as c, 2 as d
16421606
""".stripMargin)
16431607

16441608
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
1645-
val expectedPath = s"file:${dir.getAbsolutePath.stripSuffix("/")}"
1646-
assert(table.location.stripSuffix("/") == expectedPath)
1609+
assert(table.location.stripSuffix("/") == dir.getAbsolutePath.stripSuffix("/"))
16471610

16481611
checkAnswer(spark.table("t"), Row(3, 4, 1, 2))
16491612
}
16501613
// partition table
16511614
withTempDir {
16521615
dir =>
1616+
if (shouldDelete) {
1617+
dir.delete()
1618+
}
16531619
spark.sql(
16541620
s"""
16551621
|CREATE TABLE t1
1656-
|USING hive
1622+
|USING parquet
16571623
|PARTITIONED BY(a, b)
16581624
|LOCATION '$dir'
16591625
|AS SELECT 3 as a, 4 as b, 1 as c, 2 as d
16601626
""".stripMargin)
16611627

16621628
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1"))
1663-
val expectedPath = s"file:${dir.getAbsolutePath.stripSuffix("/")}"
1664-
assert(table.location.stripSuffix("/") == expectedPath)
1629+
assert(table.location.stripSuffix("/") == dir.getAbsolutePath.stripSuffix("/"))
16651630

16661631
val partDir = new File(dir, "a=3")
16671632
assert(partDir.exists())
@@ -1670,5 +1635,55 @@ class HiveDDLSuite
16701635
}
16711636
}
16721637
}
1638+
1639+
test(s"CTAS for external hive table with a $tcName location") {
1640+
withTable("t", "t1") {
1641+
withSQLConf("hive.exec.dynamic.partition.mode" -> "nonstrict") {
1642+
withTempDir {
1643+
dir =>
1644+
if (shouldDelete) {
1645+
dir.delete()
1646+
}
1647+
spark.sql(
1648+
s"""
1649+
|CREATE TABLE t
1650+
|USING hive
1651+
|LOCATION '$dir'
1652+
|AS SELECT 3 as a, 4 as b, 1 as c, 2 as d
1653+
""".stripMargin)
1654+
1655+
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
1656+
val expectedPath = s"file:${dir.getAbsolutePath.stripSuffix("/")}"
1657+
assert(table.location.stripSuffix("/") == expectedPath)
1658+
1659+
checkAnswer(spark.table("t"), Row(3, 4, 1, 2))
1660+
}
1661+
// partition table
1662+
withTempDir {
1663+
dir =>
1664+
if (shouldDelete) {
1665+
dir.delete()
1666+
}
1667+
spark.sql(
1668+
s"""
1669+
|CREATE TABLE t1
1670+
|USING hive
1671+
|PARTITIONED BY(a, b)
1672+
|LOCATION '$dir'
1673+
|AS SELECT 3 as a, 4 as b, 1 as c, 2 as d
1674+
""".stripMargin)
1675+
1676+
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1"))
1677+
val expectedPath = s"file:${dir.getAbsolutePath.stripSuffix("/")}"
1678+
assert(table.location.stripSuffix("/") == expectedPath)
1679+
1680+
val partDir = new File(dir, "a=3")
1681+
assert(partDir.exists())
1682+
1683+
checkAnswer(spark.table("t1"), Row(1, 2, 3, 4))
1684+
}
1685+
}
1686+
}
1687+
}
16731688
}
16741689
}

0 commit comments

Comments
 (0)