feat(spark-3.5): [stacked] Add OPTIMIZE SQL extension (bin-pack + incremental clustering) - #662
Draft
mkuchenbecker wants to merge 2 commits into
Draft
Conversation
…ring) for Iceberg Adds an OPTIMIZE command to the OpenHouse spark-3.5 SQL extensions: OPTIMIZE <table> [FULL] [REWRITE MANIFESTS] With no optimize.cluster.keys configured, runs plain bin-pack compaction (system.rewrite_data_files). With clustering configured, runs a sort/z-order rewrite of the configured keys, incremental by default (only the forward slice of the leading key since the last run, tracked by an optimize.cluster.hwm-snapshot-id watermark); FULL reclusters up to the age floor. Snapshots younger than optimize.cluster.min-snapshot-age-minutes are held back; partial progress bounds committed snapshots per run. Durable clustering state (config-id + JSON interval state) is advanced atomically with the watermark in a single alterTable so it survives snapshot expiration. REWRITE MANIFESTS runs afterwards over the new layout. Emits files_before/after/removed and snapshots_committed output rows. Implemented as thin sugar over the Iceberg stored-procedure CALL path (OptimizeTableExec), with an OpenHouse-table guard (openhouse.tableId). The extension parser wrapper now implements Iceberg's ExtendedParser and delegates parseSortOrder, so the sort/z-order rewrite's sort_order argument parses (the wrapper is the session's outermost parser). New non-reserved keywords OPTIMIZE/FULL/REWRITE/MANIFESTS. Tested by OptimizeTableTest (pure helpers) and OptimizeStatementTest (real Iceberg, Hadoop catalog: bin-pack reduction, clustering writes durable state, incremental second run is a no-op, REWRITE MANIFESTS, FULL, non-OpenHouse rejection). Co-authored-by: Copilot <[email protected]>
After the data rewrite, OPTIMIZE now calls system.rewrite_position_delete_files to compact merge-on-read position delete files and drop dangling deletes (deletes that no longer apply to any live data, e.g. because the data files they targeted were rewritten). Runs after the data rewrite (so it also cleans deletes made dangling by that rewrite) and before REWRITE MANIFESTS (so manifest compaction sees the reduced delete-file set). No-op on copy-on-write or delete-free tables. Tested by OptimizeStatementTest.testOptimizeCompactsMergeOnReadDeletesAndKeepsRowsCorrect: a merge-on-read table with a position-delete, OPTIMIZE keeps the visible rows correct across the rewrite. Co-authored-by: Copilot <[email protected]>
mkuchenbecker
force-pushed
the
mkuchenb/spark35-sql-optimize
branch
from
July 31, 2026 23:25
73088a6 to
1add93d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
OpenHouse spark-3.5 SQL Extensions Stack
mainSummary
Adds an OPTIMIZE command to the OpenHouse spark-3.5 SQL extensions:
optimize.cluster.keys→ plain bin-pack compaction (system.rewrite_data_files);unchanged historical behavior.
by default (only the forward slice of the leading key since the last run, tracked by an
optimize.cluster.hwm-snapshot-idwatermark);FULLreclusters up to the age floor.Snapshots younger than
optimize.cluster.min-snapshot-age-minutesare held back;Iceberg partial progress bounds the snapshots committed per run.
optimize.cluster.config-id+optimize.cluster.stateJSON intervals) advances atomically with the watermark in a single
alterTable, so itsurvives snapshot expiration.
REWRITE MANIFESTS(system.rewrite_manifests) runs afterwards over the new layout.files_before/files_after/files_removed/snapshots_committedrows.Thin sugar over the
CALLpath (OptimizeTableExec), with an OpenHouse-table guard(
openhouse.tableId). The extension parser wrapper now implements Iceberg'sExtendedParserand delegatesparseSortOrder, so the sort / z-ordersort_orderargument parses (the wrapper is the session's outermost parser).
Changes
New non-reserved keywords
OPTIMIZE/FULL/REWRITE/MANIFESTS.Testing Done
Added new tests for the changes made.
OptimizeTableTest(pure helpers:parseClusterConfig/configId/parseState/advanceState) — 9 tests.OptimizeStatementTest(real Iceberg, Hadoop catalog): bin-pack reduction, clusteringwrites durable state, incremental second run is a no-op,
REWRITE MANIFESTS,FULL,non-OpenHouse rejection — 6 tests.
Additional Information