Skip to content

[Spark] Support TRUNCATE TABLE for DeltaTableV2#6747

Open
foss-contributor wants to merge 12 commits into
delta-io:masterfrom
foss-contributor:feat/delta-truncate-table-v2
Open

[Spark] Support TRUNCATE TABLE for DeltaTableV2#6747
foss-contributor wants to merge 12 commits into
delta-io:masterfrom
foss-contributor:feat/delta-truncate-table-v2

Conversation

@foss-contributor
Copy link
Copy Markdown
Collaborator

@foss-contributor foss-contributor commented May 8, 2026

Which Delta project/connector is this regarding?

  • Spark
  • Standalone
  • Flink
  • Kernel
  • Other (fill in here)

Description

This PR adds TRUNCATE TABLE support for Delta tables backed by DeltaTableV2.

DeltaTableV2 already advertises the TRUNCATE table capability, but it did not implement Spark's
TruncatableTable interface. This change wires truncate through the Delta transaction path so the
operation removes active files, records a TRUNCATE operation in Delta history, preserves row
tracking metadata tags, and keeps table metadata intact.

The PR also adds coverage for core Delta tables and Unity Catalog Delta tables:

  • Adds DeltaTruncateTableSuite for Spark/Delta truncate behavior.
  • Adds UCDeltaTableDDLTest as a UC DDL integration suite with truncate coverage for both external
    and managed UC Delta tables.

How was this patch tested?

Added unit and integration tests covering TRUNCATE TABLE cases:

  • Covering Delta tables referenced by catalog, by path, as well as UC managed and external tables
  • Delta unit tests cover truncating non-empty tables, empty-table no-op behavior, repeated/idempotent truncate tokens, append-only atomic failure, partitioned-table metadata preservation, and partition-spec rejection.
  • UC integration tests cover truncate across managed and external UC Delta tables
  • Metadata preservation checks verify truncate keeps protocol/features, schema, partition metadata, stable DESCRIBE DETAIL fields, UC table id, and stable SHOW TBLPROPERTIES values unchanged while removing active files.
  • Metrics are emitted with relevant data for TRUNCATE TABLE operation

Test commands run:

build/sbt 'spark/testOnly org.apache.spark.sql.delta.DeltaTruncateTableSuite org.apache.spark.sql.delta.DeltaTruncateTableWithCatalogOwnedBatch1Suite'
build/sbt "spark/testOnly org.apache.spark.sql.delta.TruncateMetricsSuite"
build/sbt sparkUnityCatalog/'testOnly io.sparkuctest.UCDeltaTableDDLTest'
build/sbt 'spark/testOnly org.apache.spark.sql.delta.catalog.DeltaTableV2Suite'
build/sbt 'spark/testOnly org.apache.spark.sql.delta.CustomCatalogSuite'
build/sbt 'sparkUnityCatalog/test'

Does this PR introduce any user-facing changes?

Yes.

Delta tables backed by DeltaTableV2 now support Spark SQL TRUNCATE TABLE. The operation removes
all active data files through a Delta transaction while preserving table metadata.

Before change on spark-sql error:

spark-sql ()> TRUNCATE TABLE main.test.table;
Table does not support truncates: main.test.table

After change:

spark-sql ()> TRUNCATE TABLE main.test.table;
Time taken: 13.308 seconds

@foss-contributor foss-contributor force-pushed the feat/delta-truncate-table-v2 branch from 33c1cd0 to 19d0d56 Compare May 8, 2026 01:13
@foss-contributor foss-contributor marked this pull request as ready for review May 8, 2026 01:13
@foss-contributor foss-contributor marked this pull request as draft May 8, 2026 01:24
@foss-contributor foss-contributor marked this pull request as ready for review May 8, 2026 01:24
@seewishnew seewishnew requested a review from openinx May 8, 2026 03:13
Comment thread spark/src/main/scala/org/apache/spark/sql/delta/catalog/DeltaTableV2.scala Outdated
Comment thread spark/src/main/scala/org/apache/spark/sql/delta/catalog/DeltaTableV2.scala Outdated
Comment thread spark/unitycatalog/src/test/java/io/sparkuctest/UCDeltaTableDDLTest.java Outdated
Comment thread spark/unitycatalog/src/test/java/io/sparkuctest/UCDeltaTableDDLTest.java Outdated
Comment thread spark/src/test/scala/org/apache/spark/sql/delta/DeltaTruncateTableSuite.scala Outdated
@foss-contributor foss-contributor force-pushed the feat/delta-truncate-table-v2 branch from 19d0d56 to fa8a086 Compare May 8, 2026 16:09
@foss-contributor foss-contributor force-pushed the feat/delta-truncate-table-v2 branch from 8a65391 to d150104 Compare May 8, 2026 22:51
@raveeram-db raveeram-db requested a review from seewishnew May 8, 2026 22:58
Comment thread spark/src/main/scala/org/apache/spark/sql/delta/catalog/DeltaTableV2.scala Outdated
Copy link
Copy Markdown
Collaborator

@openinx openinx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good to me, pls address the remaining comments. I will merge it once all are ready. thanks @foss-contributor for the great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants