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

    https://github.com/apache/spark/pull/14531#discussion_r76720950
  
    --- Diff: 
sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala 
---
    @@ -620,6 +623,219 @@ class HiveDDLSuite
         }
       }
     
    +  test("CREATE TABLE LIKE a temporary table") {
    +    val sourceTabName = "tab1"
    +    val targetTabName = "tab2"
    +    withTable(sourceTabName, targetTabName) {
    +      spark.range(10).select('id as 'a, 'id as 'b, 'id as 'c, 'id as 'd)
    +        .createTempView(sourceTabName)
    +      sql(s"CREATE TABLE $targetTabName LIKE $sourceTabName")
    +
    +      val sourceTable =
    +        
spark.sessionState.catalog.getTableMetadata(TableIdentifier(sourceTabName, 
None))
    +      val targetTable =
    +        
spark.sessionState.catalog.getTableMetadata(TableIdentifier(targetTabName, 
Some("default")))
    +
    +      assert(targetTable.storage.serde ==
    +        Option(classOf[LazySimpleSerDe].getCanonicalName))
    +
    +      checkCreateTableLike(sourceTable, targetTable)
    +    }
    +  }
    +
    +  test("CREATE TABLE LIKE a data source table") {
    +    val sourceTabName = "tab1"
    +    val targetTabName = "tab2"
    +    withTable(sourceTabName, targetTabName) {
    +      spark.range(10).select('id as 'a, 'id as 'b, 'id as 'c, 'id as 'd)
    +        .write.format("json").saveAsTable(sourceTabName)
    +      sql(s"CREATE TABLE $targetTabName LIKE $sourceTabName")
    +
    +      val sourceTable =
    +        
spark.sessionState.catalog.getTableMetadata(TableIdentifier(sourceTabName, 
Some("default")))
    +      val targetTable =
    +        
spark.sessionState.catalog.getTableMetadata(TableIdentifier(targetTabName, 
Some("default")))
    +      // The table type of the source table should be a Hive-managed data 
source table
    +      assert(DDLUtils.isDatasourceTable(sourceTable))
    +      assert(sourceTable.tableType == CatalogTableType.MANAGED)
    +
    +      checkCreateTableLike(sourceTable, targetTable)
    +    }
    +  }
    +
    +  test("CREATE TABLE LIKE an external data source table") {
    +    val sourceTabName = "tab1"
    +    val targetTabName = "tab2"
    +    withTable(sourceTabName, targetTabName) {
    +      withTempPath { dir =>
    +        val path = dir.getCanonicalPath
    +        spark.range(10).select('id as 'a, 'id as 'b, 'id as 'c, 'id as 'd)
    +          .write.format("parquet").save(path)
    +        sql(s"CREATE TABLE $sourceTabName USING parquet OPTIONS (PATH 
'$path')")
    +        sql(s"CREATE TABLE $targetTabName LIKE $sourceTabName")
    +
    +        // The source table should be an external data source table
    +        val sourceTable = spark.sessionState.catalog.getTableMetadata(
    +          TableIdentifier(sourceTabName, Some("default")))
    +        val targetTable = spark.sessionState.catalog.getTableMetadata(
    +          TableIdentifier(targetTabName, Some("default")))
    +        // The table type of the source table should be an external data 
source table
    +        assert(DDLUtils.isDatasourceTable(sourceTable))
    +        assert(sourceTable.tableType == CatalogTableType.EXTERNAL)
    +
    +        checkCreateTableLike(sourceTable, targetTable)
    +      }
    +    }
    +  }
    +
    +  test("CREATE TABLE LIKE a managed Hive serde table") {
    +    val catalog = spark.sessionState.catalog
    +    val sourceTabName = "tab1"
    +    val targetTabName = "tab2"
    +    withTable(sourceTabName, targetTabName) {
    +      sql(s"CREATE TABLE $sourceTabName TBLPROPERTIES('prop1'='value1') AS 
SELECT 1 key, 'a'")
    +      sql(s"CREATE TABLE $targetTabName LIKE $sourceTabName")
    +
    +      val sourceTable = 
catalog.getTableMetadata(TableIdentifier(sourceTabName, Some("default")))
    +      assert(sourceTable.tableType == CatalogTableType.MANAGED)
    +      assert(sourceTable.properties.get("prop1").nonEmpty)
    +      val targetTable = 
catalog.getTableMetadata(TableIdentifier(targetTabName, Some("default")))
    +
    +      checkCreateTableLike(sourceTable, targetTable)
    +    }
    +  }
    +
    +  test("CREATE TABLE LIKE an external Hive serde table") {
    +    val catalog = spark.sessionState.catalog
    +    withTempDir { tmpDir =>
    +      val basePath = tmpDir.getCanonicalPath
    +      val sourceTabName = "tab1"
    +      val targetTabName = "tab2"
    +      withTable(sourceTabName, targetTabName) {
    +        assert(tmpDir.listFiles.isEmpty)
    +        sql(
    +          s"""
    +             |CREATE EXTERNAL TABLE $sourceTabName (key INT comment 
'test', value STRING)
    +             |COMMENT 'Apache Spark'
    +             |PARTITIONED BY (ds STRING, hr STRING)
    +             |LOCATION '$basePath'
    +           """.stripMargin)
    +        for (ds <- Seq("2008-04-08", "2008-04-09"); hr <- Seq("11", "12")) 
{
    +          sql(
    +            s"""
    +               |INSERT OVERWRITE TABLE $sourceTabName
    +               |partition (ds='$ds',hr='$hr')
    +               |SELECT 1, 'a'
    +             """.stripMargin)
    +        }
    +        sql(s"CREATE TABLE $targetTabName LIKE $sourceTabName")
    +
    +        val sourceTable = 
catalog.getTableMetadata(TableIdentifier(sourceTabName, Some("default")))
    +        assert(sourceTable.tableType == CatalogTableType.EXTERNAL)
    +        assert(sourceTable.comment == Option("Apache Spark"))
    +        val targetTable = 
catalog.getTableMetadata(TableIdentifier(targetTabName, Some("default")))
    +
    +        checkCreateTableLike(sourceTable, targetTable)
    +      }
    +    }
    +  }
    +
    +  test("CREATE TABLE LIKE a view") {
    +    val sourceTabName = "tab1"
    +    val sourceViewName = "view"
    +    val targetTabName = "tab2"
    +    withTable(sourceTabName, targetTabName) {
    +      withView(sourceViewName) {
    +        spark.range(10).select('id as 'a, 'id as 'b, 'id as 'c, 'id as 'd)
    +          .write.format("json").saveAsTable(sourceTabName)
    +        sql(s"CREATE VIEW $sourceViewName AS SELECT * FROM $sourceTabName")
    +        sql(s"CREATE TABLE $targetTabName LIKE $sourceViewName")
    +
    +        val sourceView = spark.sessionState.catalog.getTableMetadata(
    +          TableIdentifier(sourceViewName, Some("default")))
    +        // The original source should be a VIEW with an empty path
    +        assert(sourceView.tableType == CatalogTableType.VIEW)
    +        assert(sourceView.viewText.nonEmpty && 
sourceView.viewOriginalText.nonEmpty)
    +        val targetTable = spark.sessionState.catalog.getTableMetadata(
    +          TableIdentifier(targetTabName, Some("default")))
    +
    +        checkCreateTableLike(sourceView, targetTable)
    +      }
    +    }
    +  }
    +
    +  private def getTablePath(table: CatalogTable): Option[String] = {
    +    if (DDLUtils.isDatasourceTable(table)) {
    +      new CaseInsensitiveMap(table.storage.properties).get("path")
    +    } else {
    +      table.storage.locationUri
    +    }
    +  }
    +
    +  private def checkCreateTableLike(sourceTable: CatalogTable, targetTable: 
CatalogTable): Unit = {
    +    // The original source should be a MANAGED table with empty view text 
and original text
    +    // The location of table should not be empty.
    --- End diff --
    
    This is set by Hive. We did not set it for Hive managed table. 


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