Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/15996#discussion_r92597284
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/DataFrameWriter.scala ---
    @@ -363,48 +365,125 @@ final class DataFrameWriter[T] private[sql](ds: 
Dataset[T]) {
           throw new AnalysisException("Cannot create hive serde table with 
saveAsTable API")
         }
     
    -    val tableExists = 
df.sparkSession.sessionState.catalog.tableExists(tableIdent)
    -
    -    (tableExists, mode) match {
    -      case (true, SaveMode.Ignore) =>
    -        // Do nothing
    -
    -      case (true, SaveMode.ErrorIfExists) =>
    -        throw new AnalysisException(s"Table $tableIdent already exists.")
    -
    -      case _ =>
    -        val existingTable = if (tableExists) {
    -          
Some(df.sparkSession.sessionState.catalog.getTableMetadata(tableIdent))
    -        } else {
    -          None
    -        }
    -        val storage = if (tableExists) {
    -          existingTable.get.storage
    -        } else {
    -          DataSource.buildStorageFormatFromOptions(extraOptions.toMap)
    -        }
    -        val tableType = if (tableExists) {
    -          existingTable.get.tableType
    -        } else if (storage.locationUri.isDefined) {
    -          CatalogTableType.EXTERNAL
    -        } else {
    -          CatalogTableType.MANAGED
    +    val catalog = df.sparkSession.sessionState.catalog
    +    val db = tableIdent.database.getOrElse(catalog.getCurrentDatabase)
    +    val tableIdentWithDB = tableIdent.copy(database = Some(db))
    +    val tableName = tableIdent.unquotedString
    +
    +    catalog.getTableMetadataOption(tableIdent) match {
    +      // If the table already exists...
    +      case Some(tableMeta) =>
    +        mode match {
    +          case SaveMode.Ignore => // Do nothing
    +
    +          case SaveMode.ErrorIfExists =>
    +            throw new AnalysisException(s"Table $tableName already exists. 
You can set SaveMode " +
    +              "to SaveMode.Append to insert data into the table or set 
SaveMode to " +
    +              "SaveMode.Overwrite to overwrite the existing data.")
    +
    +          case SaveMode.Append =>
    +            // Check if the specified data source match the data source of 
the existing table.
    --- End diff --
    
    This is mostly moved from 
https://github.com/apache/spark/pull/15996/files#diff-945e51801b84b92da242fcb42f83f5f5L148


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