← Back to Pull requests

[Spark] Make REORG rewrite all selected files on clustered tables #8083

Open
unknown opened this pull request with 554 lines diff across 6 files
View on GitHub
unknown unknown commented on Nov 10, 2026 17:22 UTC
Author

Which Delta project/connector is this regarding?

  • Spark

  • Standalone

  • Flink

  • Kernel

  • Other (fill in here)
  • 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 OptimizeTableCommand with reorg set:
    1. DeltaPurgeOperation.filterFilesToReorg selects the files to rewrite: those with deletion vectors or dropped columns.
    2. groupFilesIntoBins then passes them to ClusteringStrategy.prepareFilesPerPartition. That calls applyMinZCube, 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.

    CREATE TABLE t (col1 INT, col2 INT) USING delta CLUSTER BY (col1, col2)
      TBLPROPERTIES ('delta.enableDeletionVectors' = 'true');
    INSERT INTO t VALUES (1, 1), (2, 2), (3, 3), (4, 4);
    OPTIMIZE t;                    -- one ZCube
    DELETE FROM t WHERE col1 = 1;  -- adds a deletion vector
    REORG TABLE t APPLY (PURGE);   -- numFilesRemoved = 0, the deletion vector stays
    

    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.prepareFilesPerPartition keep every file REORG selected, sorted by ZCube id as before, and skip applyMinZCube for REORG. The rewritten files are still clustered and tagged with a new ZCube. OPTIMIZE without REORG is unchanged.

    Skipping applyMinZCube also skips its first step. Without OPTIMIZE FULL, that step holds back files clustered by different clustering columns and files with a different clusteringProvider. 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, with numFilesRemoved == 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 by col2 in 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 IncrementalZCubeClusteringSuite and DeltaReorgSuite tests 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

    Diff: 554 lines (+415 / -139) across 6 files Reviewer: sezruby Review duration: 29165m
    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
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48.1% (MEDIUM) · Higher = larger blast radius
    48.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.7% · Safe to merge
    25.7%
    Repository