Github user windpiger commented on a diff in the pull request:

    https://github.com/apache/spark/pull/16910#discussion_r102394591
  
    --- Diff: 
sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala 
---
    @@ -1494,4 +1495,148 @@ class HiveDDLSuite
           }
         }
       }
    +
    +  test("insert data to a hive serde table which has a not existed location 
should succeed") {
    +    withTable("t") {
    +      withTempDir { dir =>
    +        spark.sql(
    +          s"""
    +             |CREATE TABLE t(a string, b int)
    +             |USING hive
    +             |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)
    +
    +        val tableLocFile = new File(table.location.stripPrefix("file:"))
    +        tableLocFile.delete()
    +        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 newDirFile = new File(dir, "x")
    +        spark.sql(s"ALTER TABLE t SET LOCATION 
'${newDirFile.getAbsolutePath}'")
    +        spark.sessionState.catalog.refreshTable(TableIdentifier("t"))
    +
    +        val table1 = 
spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
    +        assert(table1.location.stripSuffix("/") == 
newDirFile.getAbsolutePath.stripSuffix("/"))
    +        assert(!newDirFile.exists())
    +
    +        spark.sql("INSERT INTO TABLE t SELECT 'c', 1")
    +        checkAnswer(spark.table("t"), Row("c", 1) :: Nil)
    +        assert(newDirFile.exists())
    +      }
    +    }
    +  }
    +
    +  test("insert into a hive serde 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 hive
    +             |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)
    +
    +        val newDirFile = new File(dir, "x")
    +        spark.sql(s"ALTER TABLE t PARTITION(a=1, b=2) SET LOCATION " +
    +          s"'${newDirFile.getAbsolutePath}'")
    +        assert(!newDirFile.exists())
    +
    +        // insert into a partition which location does not exists.
    +        spark.sql("INSERT INTO TABLE t PARTITION(a=1, b=2) SELECT 9, 10")
    +        assert(newDirFile.exists())
    +        checkAnswer(spark.table("t"), Row(9, 10, 1, 2) :: Nil)
    +      }
    +    }
    +  }
    +
    +  test("read data from a hive serde table which has a not existed location 
should succeed") {
    +    withTable("t") {
    +      withTempDir { dir =>
    +        spark.sql(
    +          s"""
    +             |CREATE TABLE t(a string, b int)
    +             |USING hive
    +             |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 newDirFile = new File(dir, "x")
    +        spark.sql(s"ALTER TABLE t SET LOCATION 
'${newDirFile.getAbsolutePath}'")
    +
    +        val table1 = 
spark.sessionState.catalog.getTableMetadata(TableIdentifier("t"))
    +        assert(table1.location.stripSuffix("/") == 
newDirFile.getAbsolutePath.stripSuffix("/"))
    +        assert(!newDirFile.exists())
    +        checkAnswer(spark.table("t"), Nil)
    +      }
    +    }
    +  }
    +
    +  test("read data from a hive serde 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 hive
    +             |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)
    +
    +        val newDirFile = new File(dir, "x")
    +        spark.sql(s"ALTER TABLE t PARTITION(a=1, b=2) SET LOCATION " +
    +          s"'${newDirFile.getAbsolutePath}'")
    --- End diff --
    
    101 characters...
    let me modify some code.


---
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

Reply via email to