Skip to content

Commit 1f2ce17

Browse files
committed
add more tests
1 parent afa1313 commit 1f2ce17

File tree

2 files changed

+85
-7
lines changed

2 files changed

+85
-7
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,25 +1954,41 @@ class DDLSuite extends QueryTest with SharedSQLContext with BeforeAndAfterEach {
19541954
}
19551955

19561956
test("CTAS for data source table with a created location") {
1957-
withTable("t") {
1957+
withTable("t", "t1") {
19581958
withTempDir {
19591959
dir =>
19601960
spark.sql(
19611961
s"""
19621962
|CREATE TABLE t
19631963
|USING parquet
1964-
|PARTITIONED BY(a, b)
19651964
|LOCATION '$dir'
19661965
|AS SELECT 3 as a, 4 as b, 1 as c, 2 as d
19671966
""".stripMargin)
19681967

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

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+
19721988
val partDir = new File(dir.getAbsolutePath + "/a=3")
19731989
assert(partDir.exists())
19741990

1975-
checkAnswer(spark.table("t"), Row(1, 2, 3, 4))
1991+
checkAnswer(spark.table("t1"), Row(1, 2, 3, 4))
19761992
}
19771993
}
19781994
}

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

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,25 +1589,87 @@ class HiveDDLSuite
15891589
}
15901590

15911591
test("CTAS for data source table with a created location") {
1592-
withTable("t") {
1592+
withTable("t", "t1") {
15931593
withTempDir {
15941594
dir =>
15951595
spark.sql(
15961596
s"""
15971597
|CREATE TABLE t
15981598
|USING parquet
1599-
|PARTITIONED BY(a, b)
16001599
|LOCATION '$dir'
16011600
|AS SELECT 3 as a, 4 as b, 1 as c, 2 as d
16021601
""".stripMargin)
16031602

16041603
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
1605-
assert(table.location.stripSuffix("/") == dir.getAbsolutePath.stripSuffix("/"))
1604+
val expectedPath = s"file:${dir.getAbsolutePath.stripSuffix("/")}"
1605+
assert(table.location.stripSuffix("/") == expectedPath)
1606+
1607+
checkAnswer(spark.table("t"), Row(3, 4, 1, 2))
1608+
}
1609+
// partition table
1610+
withTempDir {
1611+
dir =>
1612+
spark.sql(
1613+
s"""
1614+
|CREATE TABLE t1
1615+
|USING parquet
1616+
|PARTITIONED BY(a, b)
1617+
|LOCATION 'file:$dir'
1618+
|AS SELECT 3 as a, 4 as b, 1 as c, 2 as d
1619+
""".stripMargin)
1620+
1621+
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1"))
1622+
val expectedPath = s"file:${dir.getAbsolutePath.stripSuffix("/")}"
1623+
assert(table.location.stripSuffix("/") == expectedPath)
16061624

16071625
val partDir = new File(dir.getAbsolutePath + "/a=3")
16081626
assert(partDir.exists())
16091627

1610-
checkAnswer(spark.table("t"), Row(1, 2, 3, 4))
1628+
checkAnswer(spark.table("t1"), Row(1, 2, 3, 4))
1629+
}
1630+
}
1631+
}
1632+
1633+
test("CTAS for hive table with a created location") {
1634+
withTable("t", "t1") {
1635+
withSQLConf("hive.exec.dynamic.partition.mode" -> "nonstrict") {
1636+
withTempDir {
1637+
dir =>
1638+
spark.sql(
1639+
s"""
1640+
|CREATE TABLE t
1641+
|USING hive
1642+
|LOCATION '$dir'
1643+
|AS SELECT 3 as a, 4 as b, 1 as c, 2 as d
1644+
""".stripMargin)
1645+
1646+
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
1647+
val expectedPath = s"file:${dir.getAbsolutePath.stripSuffix("/")}"
1648+
assert(table.location.stripSuffix("/") == expectedPath)
1649+
1650+
checkAnswer(spark.table("t"), Row(3, 4, 1, 2))
1651+
}
1652+
// partition table
1653+
withTempDir {
1654+
dir =>
1655+
spark.sql(
1656+
s"""
1657+
|CREATE TABLE t1
1658+
|USING hive
1659+
|PARTITIONED BY(a, b)
1660+
|LOCATION '$dir'
1661+
|AS SELECT 3 as a, 4 as b, 1 as c, 2 as d
1662+
""".stripMargin)
1663+
1664+
val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1"))
1665+
val expectedPath = s"file:${dir.getAbsolutePath.stripSuffix("/")}"
1666+
assert(table.location.stripSuffix("/") == expectedPath)
1667+
1668+
val partDir = new File(dir.getAbsolutePath + "/a=3")
1669+
assert(partDir.exists())
1670+
1671+
checkAnswer(spark.table("t1"), Row(1, 2, 3, 4))
1672+
}
16111673
}
16121674
}
16131675
}

0 commit comments

Comments
 (0)