Skip to content

TruncateTableCommand Logical Command

TruncateTableCommand is a leaf logical runnable command that represents TruncateTable or TruncatePartition (over a V1Table in the spark_catalog catalog) at execution time.

Creating Instance

TruncateTableCommand takes the following to be created:

  • TableIdentifier
  • Optional TablePartitionSpec

TruncateTableCommand is created when:

Executing Command

RunnableCommand
run(
  spark: SparkSession): Seq[Row]

run is part of the RunnableCommand abstraction.

run requests the given SparkSession for the SessionCatalog (via SessionState).

run requests the SessionCatalog for the table metadata by the given tableName.

run finds the paths of the partitions (if specified) or the main directory of the table and deletes them one by one, recursively.

Uses Apache Hadoop to Delete Paths

run uses Apache Hadoop's FileSystem.delete to delete the directories.

Once deleted, run re-creates the paths (so they are empty directories), possibly re-creating their file permissions (based on the spark.sql.truncateTable.ignorePermissionAcl.enabled configuration property).

Uses Apache Hadoop to Re-Create Paths

run uses Apache Hadoop's FileSystem.mkdirs to re-create the directories.

After deleting the data, run requests the Catalog to refresh the table followed by updating the table statistics.

run returns no Rows.

Exceptions

run throws an AnalysisException when executed on external tables:

Operation not allowed: TRUNCATE TABLE on external tables: [tableIdentWithDB]

run throws an AnalysisException when executed with the TablePartitionSpec on non-partitioned tables:

Operation not allowed: TRUNCATE TABLE ... PARTITION is not supported for tables that are not partitioned: [tableIdentWithDB]

run throws an AnalysisException when executed with the TablePartitionSpec with filesource partition disabled or partition metadata not in a Hive metastore.