-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[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
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5ec7dd6
[SPARK-19329][SQL]insert data to a not exist location datasource tabl…
windpiger 2196648
modify a test name desc
windpiger abc57dd
add a param name
windpiger c3439ff
add read from an non exist path
windpiger 409f7a4
add more test case
windpiger 542f86b
modify a comment
windpiger 2cbb9d6
remove some test code
windpiger c3e87d3
modify a comment
windpiger 5ebd596
remove hive serde related and add partition test cases
windpiger 334e89f
remove some import
windpiger b238e8d
move test case to DDLSuit
windpiger dee844c
remove an redundant import
windpiger 0d947a5
fix some code style
windpiger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1816,4 +1816,123 @@ 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 "$dir") | ||
| """.stripMargin) | ||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) | ||
| val expectedPath = 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 "$dir" | ||
| """.stripMargin) | ||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) | ||
| val expectedPath = 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) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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 "$dir") | ||
| """.stripMargin) | ||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) | ||
| val expectedPath = 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 "$dir" | ||
| """.stripMargin) | ||
| 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) | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
what if the path doesn't exist when create? will we succeed or fail?
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.
currently, it will throw an exception that the path does not existed. maybe we can check if the path is a dir or not, dir can not exist and file must be exist?
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.
what's the behavior of hive?