[Spark] Make REORG rewrite all selected files on clustered tables #8083
All Vouch review confidence checks passed
Residual risk 0.26 = Change Risk (0.48) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 554 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.378
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #8083"
general
weight: 0.35
sezruby
Which Delta project/connector is this regarding?
Description
Resolves #8082
On a liquid-clustered table,
REORG TABLE ... APPLY (PURGE)can finish without rewriting anything. The deletion vectors stay, and the deleted rows stay in the data files.REORG runs through
OptimizeTableCommandwithreorgset:1.
DeltaPurgeOperation.filterFilesToReorgselects the files to rewrite: those with deletion vectors or dropped columns.2.
groupFilesIntoBinsthen passes them toClusteringStrategy.prepareFilesPerPartition. That callsapplyMinZCube, which picks the files incremental clustering should re-cluster. Applied to REORG's file list, it drops:- all selected files that belong to a single ZCube (
ZCube.filterOutSingleZCubes);- the selected files of a ZCube whose size reaches
optimize.clustering.mergeStrategy.minCubeSize.threshold(ZCube.filterOutLargeZCubes).The REORG exemption in
groupFilesIntoBins(bin.size == 1 && optimizeContext.reorg.nonEmpty) only applies to files that are left after that. So a REORG whose files with deletion vectors all sit in one ZCube is a no-op. A small table becomes one ZCube after its first OPTIMIZE, so this is the common case.The same path skips files for the other REORG operations on clustered tables, such as
UPGRADE UNIFORM.Databricks Runtime 16.4 LTS, 18 LTS and 18.2 purge the deletion vectors in these cases: tables clustered by one or two columns, with or without a prior OPTIMIZE, and a partitioned table converted with
ALTER TABLE ... REPLACE PARTITIONED BY WITH CLUSTER BY. After REORG, no file has a deletion vector and the physical row count equals the table's row count. This PR brings OSS Delta to the same result.This PR makes
ClusteringStrategy.prepareFilesPerPartitionkeep every file REORG selected, sorted by ZCube id as before, and skipapplyMinZCubefor REORG. The rewritten files are still clustered and tagged with a new ZCube. OPTIMIZE without REORG is unchanged.Skipping
applyMinZCubealso skips its first step. WithoutOPTIMIZE FULL, that step holds back files clustered by different clustering columns and files with a differentclusteringProvider. REORG now rewrites the selected files among them as well, and clusters them by the current clustering columns into a new ZCube. REORG has to rewrite those files to purge their deletion vectors.How was this patch tested?
New tests in
IncrementalZCubeClusteringSuite:REORG APPLY (PURGE) rewrites deletion vector files of a single ZCube: OPTIMIZE into one ZCube, then DELETE, then REORG PURGE. Checks that no deletion vectors remain, only the file with a deletion vector was rewritten (numFilesRemoved == 1), the physical record count matches, the files are clustered, and the result is correct.REORG APPLY (PURGE) rewrites deletion vector files of large ZCubes: two ZCubes, each with a file that has a deletion vector, and REORG PURGE run with a small min cube size. Same checks, withnumFilesRemoved == 2.REORG APPLY (PURGE) rewrites deletion vector files clustered by other columns:CLUSTER BY (col1), OPTIMIZE,ALTER TABLE ... CLUSTER BY (col2), DELETE, then REORG PURGE. Checks that no deletion vectors remain, only the file with a deletion vector was rewritten, the rewritten file is clustered bycol2in a new ZCube, and the result is correct.All three tests fail without the change (REORG removes no files) and pass with it. The existing
IncrementalZCubeClusteringSuiteandDeltaReorgSuitetests pass.Does this PR introduce _any_ user-facing changes?
Yes. On clustered tables,
REORG TABLE ... APPLY (PURGE)(and the other REORG operations) now rewrites every file it selects. Before, it could skip some or all of those files and leave deletion vectors and deleted rows in place. Selected files that were clustered by other clustering columns are re-clustered by the current ones.🤖 Generated with Claude Code