-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-19723][SQL]create datasource table with an non-existent location should work #17055
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 13 commits
89eb03a
0e09b92
d4378dd
4c82dfb
3ea3e9d
b26cce1
6f7045c
d53f7ab
3ae5b2a
bc7f08d
5699059
207448d
4024d7c
e1c891e
ada275c
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 |
|---|---|---|
|
|
@@ -230,7 +230,7 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils { | |
| } | ||
|
|
||
| private def getDBPath(dbName: String): URI = { | ||
| val warehousePath = makeQualifiedPath(s"${spark.sessionState.conf.warehousePath}") | ||
| val warehousePath = makeQualifiedPath(spark.sessionState.conf.warehousePath) | ||
| new Path(CatalogUtils.URIToString(warehousePath), s"$dbName.db").toUri | ||
| } | ||
|
|
||
|
|
@@ -1899,7 +1899,7 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils { | |
| } | ||
| } | ||
|
|
||
| test("insert data to a data source table which has a not existed location should succeed") { | ||
| test("insert data to a data source table which has a non-existing location should succeed") { | ||
| withTable("t") { | ||
| withTempDir { dir => | ||
| spark.sql( | ||
|
|
@@ -1939,7 +1939,7 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils { | |
| } | ||
| } | ||
|
|
||
| test("insert into a data source table with no existed partition location should succeed") { | ||
| test("insert into a data source table with a non-existing partition location should succeed") { | ||
| withTable("t") { | ||
| withTempDir { dir => | ||
| spark.sql( | ||
|
|
@@ -1966,7 +1966,7 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils { | |
| } | ||
| } | ||
|
|
||
| test("read data from a data source table which has a not existed location should succeed") { | ||
| test("read data from a data source table which has a non-existing location should succeed") { | ||
| withTable("t") { | ||
| withTempDir { dir => | ||
| spark.sql( | ||
|
|
@@ -1994,7 +1994,7 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils { | |
| } | ||
| } | ||
|
|
||
| test("read data from a data source table with no existed partition location should succeed") { | ||
| test("read data from a data source table with non-existing partition location should succeed") { | ||
| withTable("t") { | ||
| withTempDir { dir => | ||
| spark.sql( | ||
|
|
@@ -2016,48 +2016,79 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils { | |
| } | ||
| } | ||
|
|
||
| test("create datasource table with a non-existing location") { | ||
| withTable("t", "t1") { | ||
| withTempPath { dir => | ||
| spark.sql(s"CREATE TABLE t(a int, b int) USING parquet LOCATION '$dir'") | ||
|
|
||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) | ||
| assert(table.location == makeQualifiedPath(dir.getAbsolutePath)) | ||
|
|
||
| spark.sql("INSERT INTO TABLE t SELECT 1, 2") | ||
| assert(dir.exists()) | ||
|
|
||
| checkAnswer(spark.table("t"), Row(1, 2)) | ||
| } | ||
| // partition table | ||
| withTempPath { dir => | ||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t1(a int, b int) USING parquet PARTITIONED BY(a) LOCATION '$dir' | ||
| """.stripMargin) | ||
|
|
||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1")) | ||
| assert(table.location == makeQualifiedPath(dir.getAbsolutePath)) | ||
|
|
||
| spark.sql("INSERT INTO TABLE t1 PARTITION(a=1) SELECT 2") | ||
|
|
||
| val partDir = new File(dir, "a=1") | ||
| assert(partDir.exists()) | ||
|
|
||
| checkAnswer(spark.table("t1"), Row(2, 1)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Seq(true, false).foreach { shouldDelete => | ||
| val tcName = if (shouldDelete) "non-existent" else "existed" | ||
| val tcName = if (shouldDelete) "non-existing" else "existed" | ||
| test(s"CTAS for external data source table with a $tcName location") { | ||
| withTable("t", "t1") { | ||
| withTempDir { | ||
| dir => | ||
| if (shouldDelete) { | ||
| dir.delete() | ||
| } | ||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t | ||
| |USING parquet | ||
| |LOCATION '$dir' | ||
| |AS SELECT 3 as a, 4 as b, 1 as c, 2 as d | ||
| """.stripMargin) | ||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) | ||
| assert(table.location == makeQualifiedPath(dir.getAbsolutePath)) | ||
| withTempDir { dir => | ||
| if (shouldDelete) { | ||
| dir.delete() | ||
| } | ||
|
||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t | ||
| |USING parquet | ||
| |LOCATION '$dir' | ||
| |AS SELECT 3 as a, 4 as b, 1 as c, 2 as d | ||
| """.stripMargin) | ||
|
Member
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. The test case is already pretty long. Please reduce the sql statement to a single line. |
||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) | ||
| assert(table.location == makeQualifiedPath(dir.getAbsolutePath)) | ||
|
|
||
| checkAnswer(spark.table("t"), Row(3, 4, 1, 2)) | ||
| checkAnswer(spark.table("t"), Row(3, 4, 1, 2)) | ||
| } | ||
| // partition table | ||
| withTempDir { | ||
| dir => | ||
| if (shouldDelete) { | ||
| dir.delete() | ||
| } | ||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t1 | ||
| |USING parquet | ||
| |PARTITIONED BY(a, b) | ||
| |LOCATION '$dir' | ||
| |AS SELECT 3 as a, 4 as b, 1 as c, 2 as d | ||
| """.stripMargin) | ||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1")) | ||
| assert(table.location == makeQualifiedPath(dir.getAbsolutePath)) | ||
|
|
||
| val partDir = new File(dir, "a=3") | ||
| assert(partDir.exists()) | ||
|
|
||
| checkAnswer(spark.table("t1"), Row(1, 2, 3, 4)) | ||
| withTempDir { dir => | ||
| if (shouldDelete) { | ||
| dir.delete() | ||
| } | ||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t1 | ||
| |USING parquet | ||
| |PARTITIONED BY(a, b) | ||
| |LOCATION '$dir' | ||
| |AS SELECT 3 as a, 4 as b, 1 as c, 2 as d | ||
| """.stripMargin) | ||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1")) | ||
| assert(table.location == makeQualifiedPath(dir.getAbsolutePath)) | ||
|
|
||
| val partDir = new File(dir, "a=3") | ||
| assert(partDir.exists()) | ||
|
|
||
| checkAnswer(spark.table("t1"), Row(1, 2, 3, 4)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1663,95 +1663,127 @@ class HiveDDLSuite | |
| } | ||
| } | ||
|
|
||
| test("create hive table with a non-existing location") { | ||
| withTable("t", "t1") { | ||
| withTempPath { dir => | ||
| spark.sql(s"CREATE TABLE t(a int, b int) USING hive LOCATION '$dir'") | ||
|
|
||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) | ||
| assert(table.location == makeQualifiedPath(dir.getAbsolutePath)) | ||
|
|
||
| spark.sql("INSERT INTO TABLE t SELECT 1, 2") | ||
| assert(dir.exists()) | ||
|
|
||
| checkAnswer(spark.table("t"), Row(1, 2)) | ||
| } | ||
| // partition table | ||
| withTempPath { dir => | ||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t1(a int, b int) | ||
| |USING hive | ||
| |PARTITIONED BY(a) | ||
| |LOCATION '$dir' | ||
| """.stripMargin) | ||
|
|
||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1")) | ||
| assert(table.location == makeQualifiedPath(dir.getAbsolutePath)) | ||
|
|
||
| spark.sql("INSERT INTO TABLE t1 PARTITION(a=1) SELECT 2") | ||
|
|
||
| val partDir = new File(dir, "a=1") | ||
| assert(partDir.exists()) | ||
|
|
||
| checkAnswer(spark.table("t1"), Row(2, 1)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Seq(true, false).foreach { shouldDelete => | ||
| val tcName = if (shouldDelete) "non-existent" else "existed" | ||
| val tcName = if (shouldDelete) "non-existing" else "existed" | ||
| test(s"CTAS for external data source table with a $tcName location") { | ||
|
||
| withTable("t", "t1") { | ||
| withTempDir { | ||
| dir => | ||
| withTempDir { dir => | ||
| if (shouldDelete) { | ||
| dir.delete() | ||
| } | ||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t | ||
| |USING parquet | ||
| |LOCATION '$dir' | ||
| |AS SELECT 3 as a, 4 as b, 1 as c, 2 as d | ||
| """.stripMargin) | ||
|
|
||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) | ||
| assert(table.location == makeQualifiedPath(dir.getAbsolutePath)) | ||
|
|
||
| checkAnswer(spark.table("t"), Row(3, 4, 1, 2)) | ||
| } | ||
| // partition table | ||
| withTempDir { dir => | ||
| if (shouldDelete) { | ||
| dir.delete() | ||
| } | ||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t1 | ||
| |USING parquet | ||
| |PARTITIONED BY(a, b) | ||
| |LOCATION '$dir' | ||
| |AS SELECT 3 as a, 4 as b, 1 as c, 2 as d | ||
| """.stripMargin) | ||
|
|
||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1")) | ||
| assert(table.location == makeQualifiedPath(dir.getAbsolutePath)) | ||
|
|
||
| val partDir = new File(dir, "a=3") | ||
| assert(partDir.exists()) | ||
|
|
||
| checkAnswer(spark.table("t1"), Row(1, 2, 3, 4)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test(s"CTAS for external hive table with a $tcName location") { | ||
| withTable("t", "t1") { | ||
| withSQLConf("hive.exec.dynamic.partition.mode" -> "nonstrict") { | ||
| withTempDir { dir => | ||
| if (shouldDelete) { | ||
| dir.delete() | ||
| } | ||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t | ||
| |USING parquet | ||
| |USING hive | ||
| |LOCATION '$dir' | ||
| |AS SELECT 3 as a, 4 as b, 1 as c, 2 as d | ||
| """.stripMargin) | ||
|
|
||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) | ||
| assert(table.location == makeQualifiedPath(dir.getAbsolutePath)) | ||
|
|
||
| checkAnswer(spark.table("t"), Row(3, 4, 1, 2)) | ||
| } | ||
| // partition table | ||
| withTempDir { | ||
| dir => | ||
| } | ||
| // partition table | ||
| withTempDir { dir => | ||
| if (shouldDelete) { | ||
| dir.delete() | ||
| } | ||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t1 | ||
| |USING parquet | ||
| |USING hive | ||
| |PARTITIONED BY(a, b) | ||
| |LOCATION '$dir' | ||
| |AS SELECT 3 as a, 4 as b, 1 as c, 2 as d | ||
| """.stripMargin) | ||
|
|
||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1")) | ||
| assert(table.location == makeQualifiedPath(dir.getAbsolutePath)) | ||
|
|
||
| val partDir = new File(dir, "a=3") | ||
| assert(partDir.exists()) | ||
|
|
||
| checkAnswer(spark.table("t1"), Row(1, 2, 3, 4)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test(s"CTAS for external hive table with a $tcName location") { | ||
| withTable("t", "t1") { | ||
| withSQLConf("hive.exec.dynamic.partition.mode" -> "nonstrict") { | ||
| withTempDir { | ||
| dir => | ||
| if (shouldDelete) { | ||
| dir.delete() | ||
| } | ||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t | ||
| |USING hive | ||
| |LOCATION '$dir' | ||
| |AS SELECT 3 as a, 4 as b, 1 as c, 2 as d | ||
| """.stripMargin) | ||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) | ||
| assert(table.location == makeQualifiedPath(dir.getAbsolutePath)) | ||
|
|
||
| checkAnswer(spark.table("t"), Row(3, 4, 1, 2)) | ||
| } | ||
| // partition table | ||
| withTempDir { | ||
| dir => | ||
| if (shouldDelete) { | ||
| dir.delete() | ||
| } | ||
| spark.sql( | ||
| s""" | ||
| |CREATE TABLE t1 | ||
| |USING hive | ||
| |PARTITIONED BY(a, b) | ||
| |LOCATION '$dir' | ||
| |AS SELECT 3 as a, 4 as b, 1 as c, 2 as d | ||
| """.stripMargin) | ||
| val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1")) | ||
| assert(table.location == makeQualifiedPath(dir.getAbsolutePath)) | ||
|
|
||
| val partDir = new File(dir, "a=3") | ||
| assert(partDir.exists()) | ||
|
|
||
| checkAnswer(spark.table("t1"), Row(1, 2, 3, 4)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.
Nit: one line?