Skip to content

Commit a5687f8

Browse files
committed
add test for setting location for managed table
1 parent 18ee55d commit a5687f8

File tree

1 file changed

+28
-0
lines changed
  • sql/core/src/test/scala/org/apache/spark/sql/execution/command

1 file changed

+28
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.spark.sql.execution.command
1919

2020
import java.io.File
21+
import java.net.URI
2122

2223
import org.apache.hadoop.fs.Path
2324
import org.scalatest.BeforeAndAfterEach
@@ -1787,4 +1788,31 @@ class DDLSuite extends QueryTest with SharedSQLContext with BeforeAndAfterEach {
17871788
val rows: Seq[Row] = df.toLocalIterator().asScala.toSeq
17881789
assert(rows.length > 0)
17891790
}
1791+
1792+
test("SET LOCATION for managed table") {
1793+
withTable("src") {
1794+
withTempDir { dir =>
1795+
sql("CREATE TABLE tbl(i INT) USING parquet")
1796+
sql("INSERT INTO tbl SELECT 1")
1797+
checkAnswer(spark.table("tbl"), Row(1))
1798+
val defaultTablePath = spark.sessionState.catalog
1799+
.getTableMetadata(TableIdentifier("tbl")).storage.locationUri.get
1800+
1801+
sql(s"ALTER TABLE tbl SET LOCATION '${dir.getCanonicalPath}'")
1802+
// SET LOCATION won't move data from previous table path to new table path.
1803+
assert(spark.table("tbl").count() == 0)
1804+
// the previous table path should be still there.
1805+
assert(new File(new URI(defaultTablePath)).exists())
1806+
1807+
sql("INSERT INTO tbl SELECT 2")
1808+
checkAnswer(spark.table("tbl"), Row(2))
1809+
// newly inserted data will go to the new table path.
1810+
assert(dir.listFiles().nonEmpty)
1811+
1812+
sql("DROP TABLE tbl")
1813+
// the new table path will be removed after DROP TABLE.
1814+
assert(!dir.exists())
1815+
}
1816+
}
1817+
}
17901818
}

0 commit comments

Comments
 (0)