-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-19329][SQL]Reading from or writing to a datasource table with a non pre-existing location should succeed #16672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
5ec7dd6
2196648
abc57dd
c3439ff
409f7a4
542f86b
2cbb9d6
c3e87d3
5ebd596
334e89f
b238e8d
dee844c
0d947a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1816,4 +1816,127 @@ class DDLSuite extends QueryTest with SharedSQLContext with BeforeAndAfterEach { | |
| } | ||
| } | ||
| } | ||
|
|
||
| test("insert data to a data source table which has a not existed location should succeed") { | ||
| withTable("t") { | ||
| withTempDir { dir => | ||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t(a string, b int) | ||
| |USING parquet | ||
| |OPTIONS(path "file:${dir.getCanonicalPath}") | ||
| """.stripMargin) | ||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) | ||
| val expectedPath = s"file:${dir.getAbsolutePath.stripSuffix("/")}" | ||
| assert(table.location.stripSuffix("/") == expectedPath) | ||
|
|
||
| dir.delete | ||
| val tableLocFile = new File(table.location.stripPrefix("file:")) | ||
| assert(!tableLocFile.exists) | ||
| spark.sql("INSERT INTO TABLE t SELECT 'c', 1") | ||
| assert(tableLocFile.exists) | ||
| checkAnswer(spark.table("t"), Row("c", 1) :: Nil) | ||
|
|
||
| Utils.deleteRecursively(dir) | ||
| assert(!tableLocFile.exists) | ||
| spark.sql("INSERT OVERWRITE TABLE t SELECT 'c', 1") | ||
| assert(tableLocFile.exists) | ||
| checkAnswer(spark.table("t"), Row("c", 1) :: Nil) | ||
|
|
||
| val newDir = dir.getAbsolutePath.stripSuffix("/") + "/x" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, I will fix this when I do another pr, thanks~ |
||
| val newDirFile = new File(newDir) | ||
| spark.sql(s"ALTER TABLE t SET LOCATION '$newDir'") | ||
| spark.sessionState.catalog.refreshTable(TableIdentifier("t")) | ||
|
|
||
| val table1 = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) | ||
| assert(table1.location == newDir) | ||
| assert(!newDirFile.exists) | ||
|
|
||
| spark.sql("INSERT INTO TABLE t SELECT 'c', 1") | ||
| assert(newDirFile.exists) | ||
| checkAnswer(spark.table("t"), Row("c", 1) :: Nil) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("insert into a data source table with no existed partition location should succeed") { | ||
| withTable("t") { | ||
| withTempDir { dir => | ||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t(a int, b int, c int, d int) | ||
| |USING parquet | ||
| |PARTITIONED BY(a, b) | ||
| |LOCATION "file:${dir.getCanonicalPath}" | ||
| """.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 PARTITION(a=1, b=2) SELECT 3, 4") | ||
| checkAnswer(spark.table("t"), Row(3, 4, 1, 2) :: Nil) | ||
|
|
||
| val partLoc = new File(s"${dir.getAbsolutePath}/a=1") | ||
| Utils.deleteRecursively(partLoc) | ||
| assert(!partLoc.exists()) | ||
| // insert overwrite into a partition which location has been deleted. | ||
| spark.sql("INSERT OVERWRITE TABLE t PARTITION(a=1, b=2) SELECT 7, 8") | ||
| assert(partLoc.exists()) | ||
| checkAnswer(spark.table("t"), Row(7, 8, 1, 2) :: Nil) | ||
|
|
||
| // TODO:insert into a partition after alter the partition location by alter command | ||
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| test("read data from a data source table which has a not existed location should succeed") { | ||
| withTable("t") { | ||
| withTempDir { dir => | ||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t(a string, b int) | ||
| |USING parquet | ||
| |OPTIONS(path "file:${dir.getAbsolutePath}") | ||
| """.stripMargin) | ||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) | ||
| val expectedPath = s"file:${dir.getAbsolutePath.stripSuffix("/")}" | ||
| assert(table.location.stripSuffix("/") == expectedPath) | ||
|
|
||
| dir.delete() | ||
| checkAnswer(spark.table("t"), Nil) | ||
|
|
||
| val newDir = dir.getAbsolutePath.stripSuffix("/") + "/x" | ||
| spark.sql(s"ALTER TABLE t SET LOCATION '$newDir'") | ||
|
|
||
| val table1 = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) | ||
| assert(table1.location == newDir) | ||
| assert(!new File(newDir).exists()) | ||
| checkAnswer(spark.table("t"), Nil) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("read data from a data source table with no existed partition location should succeed") { | ||
| withTable("t") { | ||
| withTempDir { dir => | ||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t(a int, b int, c int, d int) | ||
| |USING parquet | ||
| |PARTITIONED BY(a, b) | ||
| |LOCATION "file:${dir.getCanonicalPath}" | ||
| """.stripMargin) | ||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) | ||
|
||
|
|
||
| spark.sql("INSERT INTO TABLE t PARTITION(a=1, b=2) SELECT 3, 4") | ||
| checkAnswer(spark.table("t"), Row(3, 4, 1, 2) :: Nil) | ||
|
|
||
| // select from a partition which location has been deleted. | ||
| Utils.deleteRecursively(dir) | ||
| assert(!dir.exists()) | ||
| spark.sql("REFRESH TABLE t") | ||
| checkAnswer(spark.sql("select * from t where a=1 and b=2"), Nil) | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
file:spark.sql( s""" |CREATE TABLE t(a int, b int, c int, d int) |USING parquet |PARTITIONED BY(a, b) |LOCATION '$dir' """.stripMargin) val expectedPath = dir.getAbsolutePath.stripSuffix("/")