[spark] Reject ALTER TABLE REPLACE COLUMNS to avoid silent data corruption#8246
Open
huangxiaopingRD wants to merge 1 commit into
Open
[spark] Reject ALTER TABLE REPLACE COLUMNS to avoid silent data corruption#8246huangxiaopingRD wants to merge 1 commit into
huangxiaopingRD wants to merge 1 commit into
Conversation
0bba7f8 to
69e7fd9
Compare
…ption Spark translates REPLACE COLUMNS into a DeleteColumn + AddColumn batch. Re-adding columns assigns new field ids while existing data files keep the old ids, so same-named columns are read back as null. Detect this pattern and throw UnsupportedOperationException instead.
69e7fd9 to
d1d7c96
Compare
JingsongLi
reviewed
Jun 16, 2026
| } | ||
| } | ||
|
|
||
| return hasDeleteColumn && hasAddColumn; |
Contributor
There was a problem hiding this comment.
This heuristic also rejects any programmatic TableCatalog.alterTable call that batches a supported drop and add together, for example deleteColumn("b") plus addColumn("d", ...). That is not necessarily ALTER TABLE ... REPLACE COLUMNS and it used to be a valid combination of existing schema changes. Can we make the detection narrower, e.g. only reject the Spark replace pattern where all current top-level columns are deleted before the new columns are added, or otherwise avoid blocking ordinary drop+add batches?
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.
Summary
Spark translates
ALTER TABLE ... REPLACE COLUMNSinto a batch that drops every existing column and re-adds the new set (a combination ofDeleteColumn+AddColumn). For Paimon this is unsafe: re-adding columns assigns brand-new field ids while existing data files keep the old ids, so same-named columns are treated as new columns and read back asnull— a silent data corruption.This PR detects that change pattern in
SparkCatalog.alterTableand throws anUnsupportedOperationExceptionwith a clear message pointing users toRENAME COLUMN/ALTER COLUMN TYPE/DROP COLUMN/ADD COLUMNinstead.The detection matches exclusively on
DeleteColumn+AddColumnso a legitimate mixed batch (e.g. a programmatic DROP + RENAME) is not mistaken for a replace.Tests
Added
SparkSchemaEvolutionITCase#testReplaceColumnsUnsupportedverifying the operation is rejected with the expected exception.