Github user gatorsmile commented on a diff in the pull request: https://github.com/apache/spark/pull/17001#discussion_r105564912 --- Diff: sql/hive/src/test/scala/org/apache/spark/sql/hive/HiveSparkSubmitSuite.scala --- @@ -907,3 +936,97 @@ object SPARK_18989_DESC_TABLE { } } } + +object SPARK_19667_CREATE_TABLE { + def main(args: Array[String]): Unit = { + val spark = SparkSession.builder().enableHiveSupport().getOrCreate() + try { + val warehousePath = new Path(spark.sharedState.warehousePath) + val fs = warehousePath.getFileSystem(spark.sessionState.newHadoopConf()) + val defaultDB = spark.sessionState.catalog.getDatabaseMetadata("default") + // default database use warehouse path as its location + assert(new Path(defaultDB.locationUri) == fs.makeQualified(warehousePath)) + spark.sql("CREATE TABLE t(a string)") + + val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) + // table in default database use the location of default database which is also warehouse path + assert(new Path(table.location) == fs.makeQualified(new Path(warehousePath, "t"))) + spark.sql("INSERT INTO TABLE t SELECT 1") + assert(spark.sql("SELECT * FROM t").count == 1) + + spark.sql("CREATE DATABASE not_default") + spark.sql("USE not_default") + spark.sql("CREATE TABLE t1(b string)") + val table1 = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1")) + // table in not default database use the location of its own database + assert(new Path(table1.location) == fs.makeQualified( + new Path(warehousePath, "not_default.db/t1"))) + } finally { + spark.sql("USE default") + } + } +} + +object SPARK_19667_VERIFY_TABLE_PATH { + def main(args: Array[String]): Unit = { + val spark = SparkSession.builder().enableHiveSupport().getOrCreate() + try { + val warehousePath = new Path(spark.sharedState.warehousePath) + val fs = warehousePath.getFileSystem(spark.sessionState.newHadoopConf()) + val defaultDB = spark.sessionState.catalog.getDatabaseMetadata("default") + // default database use warehouse path as its location + assert(new Path(defaultDB.locationUri) == fs.makeQualified(warehousePath)) + + val table = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t")) + // the table in default database created in job(SPARK_19667_CREATE_TABLE) above, + // which has different warehouse path from this job, its location still equals to + // the location when it's created. + assert(new Path(table.location) != fs.makeQualified(new Path(warehousePath, "t"))) + assert(spark.sql("SELECT * FROM t").count == 1) + + spark.sql("CREATE TABLE t3(d string)") + val table3 = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t3")) + // the table in default database created here in this job, it will use the warehouse path + // of this job as its location + assert(new Path(table3.location) == fs.makeQualified(new Path(warehousePath, "t3"))) + + spark.sql("USE not_default") + val table1 = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t1")) + // the table in not default database create in job(SPARK_19667_CREATE_TABLE) above, + // which has different warehouse path from this job, its location still equals to + // the location when it's created. + assert(new Path(table1.location) != fs.makeQualified( + new Path(warehousePath, "not_default.db/t1"))) + assert(!new File(warehousePath.toString, "not_default.db/t1").exists()) + + spark.sql("CREATE TABLE t2(c string)") + val table2 = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t2")) + // the table in not default database created here in this job, it will use the location + // of the database as its location, not the warehouse path in this job + assert(new Path(table2.location) != fs.makeQualified( + new Path(warehousePath, "not_default.db/t2"))) + + spark.sql("CREATE DATABASE not_default_1") + spark.sql("USE not_default_1") + spark.sql("CREATE TABLE t4(e string)") + val table4 = spark.sessionState.catalog.getTableMetadata(TableIdentifier("t4")) + // the table created in the database which created in this job, it will use the location + // of the database. --- End diff -- -> ``` the table created in the non-default database is under the database location. ```
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org