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

    https://github.com/apache/spark/pull/13170#discussion_r63915008
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala ---
    @@ -271,6 +274,55 @@ case class LoadData(
     }
     
     /**
    + * A command to truncate table.
    + *
    + * The syntax of this command is:
    + * {{{
    + *  TRUNCATE TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)]
    + * }}}
    + */
    +case class TruncateTable(
    +    tableName: TableIdentifier,
    +    partitionSpec: Option[TablePartitionSpec]) extends RunnableCommand {
    +
    +  override def run(sparkSession: SparkSession): Seq[Row] = {
    +    val catalog = sparkSession.sessionState.catalog
    +    if (!catalog.tableExists(tableName)) {
    +      logWarning(s"table '$tableName' in TRUNCATE TABLE does not exist.")
    +    } else if (catalog.isTemporaryTable(tableName)) {
    +      logWarning(s"table '$tableName' in TRUNCATE TABLE is a temporary 
table.")
    +    } else {
    +      val locations = if (partitionSpec.isDefined) {
    +        catalog.listPartitions(tableName, 
partitionSpec).map(_.storage.locationUri)
    +      } else {
    +        val table = catalog.getTableMetadata(tableName)
    +        if (table.partitionColumnNames.nonEmpty) {
    +          catalog.listPartitions(tableName).map(_.storage.locationUri)
    +        } else {
    +          Seq(table.storage.locationUri)
    +        }
    +      }
    +      val hadoopConf = sparkSession.sessionState.newHadoopConf()
    +      locations.foreach { location =>
    +        if (location.isDefined) {
    +          val path = new Path(location.get)
    +          try {
    +            val fs = path.getFileSystem(hadoopConf)
    +            fs.delete(path, true)
    +            fs.mkdirs(path)
    +          } catch {
    +            case NonFatal(e) =>
    +              throw new AnalysisException(
    +                s"Failed to truncate table '$tableName' because of 
${e.toString}")
    --- End diff --
    
    Minor: add location to the error string. Unless you are sure the exception 
message contains it.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to