Skip to content

feat(transaction): add ReplaceTableAction for REPLACE TABLE - #3778

Open
xanderbailey wants to merge 5 commits into
apache:mainfrom
xanderbailey:xb/rtas-replace-table
Open

xanderbailey wants to merge 5 commits into
apache:mainfrom
xanderbailey:xb/rtas-replace-table

Conversation

@xanderbailey

@xanderbailey xanderbailey commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

What changes are included in this PR?

Adds Transaction::replace_table, which replaces the schema, partition spec and sort order using build_replacement. Write the new data against staged_table() and add it with a fast append in the same transaction.

On retry the staged replacement is committed again, as in Java. The commit fails if the table was recreated, a concurrent change assigned new ids, or the catalog would renumber the staged spec or sort order.

Unlike the other actions, ReplaceTableAction keeps the table it was created from. Data files are written against staged_table(), so the commit has to pin the replacement to that table, but on retry actions only see the refreshed one. Java has a separate replace transaction for this. Here it follows the stateful transaction RFC: the replacement is derived from that base and reused on retry behind the uuid, last assigned id and layout checks.

Are these changes tested?

Yes, unit tests and MemoryCatalog tests.

AI Disclosure

Used to investigate and write tests

Replaces the schema, partition spec and sort order through
build_replacement and asserts the base it replaced, including main, so
the first-requirement rule drops the main assertion of the append that
adds the new contents. staged_table gives writers the replaced schema
and spec. A retry recomputes the replacement against the refreshed table
and fails without retrying if the table was recreated or the ids would
differ from the staged table.

Part of apache#3056.
Retries send the replacement computed against the transaction's table
instead of recomputing it, so concurrent column drops no longer change
its ids. Like Java, it resets main, the current schema, default spec and
default sort order when the staged changes leave them out, and fails if
the last assigned column or partition ids moved. Unlike Java, it also
fails when the catalog would renumber the staged spec or sort order.
let staged = self.replace(original)?;
let sets = |matches: fn(&TableUpdate) -> bool| staged.changes.iter().any(matches);
let mut updates = Vec::new();
// main didn't exist when staged, but a concurrent commit such as a first append created it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm normally anti-comment but i've added comments for each of these cases to make it clear what case we're covering

@xanderbailey

Copy link
Copy Markdown
Contributor Author

@CTTY very interested to see what you think of this setup and how it fits into your vision for the new Txn work.

ReplaceTableAction is the first action whose result has to stay pinned to the table the caller staged against, since the data files are written with the ids from staged_table(). I've kept that table on the action and reuse the replacement on retry behind the uuid, last assigned id and layout checks, which seemed closest to the RFC's "reuse under a validity check". The alternative I could see is Transaction remembering the table it started from and passing it to actions, but that changes the action interface for every action and seems to contradict some of what you were aiming for in that RFC.

@xanderbailey
xanderbailey marked this pull request as ready for review November 26, 2026 21:11

This branch has not been deployed

No deployments
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.

1 participant