Skip to content

feat(RTAS): add TableMetadataBuilder::build_replacement - #3057

Merged
laskoviymishka merged 9 commits into
apache:mainfrom
xanderbailey:xb/build_replacement
Nov 16, 2026
Merged

laskoviymishka merged 9 commits into
apache:mainfrom
xanderbailey:xb/build_replacement

Conversation

@xanderbailey

@xanderbailey xanderbailey commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

What changes are included in this PR?

Replaces a table's schema, partition spec and sort order in place, leaving it with no current snapshot but keeping its uuid, properties and snapshots. This is the metadata side of REPLACE TABLE, where the caller writes the new contents and commits them together with the replacement.

V1 table replace is out of scope to start with since it will take a little more effort to implement including #3555 but it can be done as a follow-up to this PR.

Are these changes tested?

AI Disclosure

Used to review and write tests

@xanderbailey xanderbailey changed the title feat: add TableMetadataBuilder::build_replacement feat(RTAS): add TableMetadataBuilder::build_replacement Aug 23, 2026
@xanderbailey
xanderbailey marked this pull request as draft August 25, 2026 14:20
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that’s incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the [email protected] list. Thank you for your contributions.

@github-actions github-actions Bot added the stale label Sep 25, 2026
@xanderbailey

Copy link
Copy Markdown
Contributor Author

Not stale, still working on this

@xanderbailey

Copy link
Copy Markdown
Contributor Author

@laskoviymishka would you be able to give a review here?

@laskoviymishka
laskoviymishka self-requested a review October 30, 2026 22:49
@laskoviymishka

Copy link
Copy Markdown
Contributor

@xanderbailey i'll take first pass somewhere over weekend / next week

@xanderbailey

Copy link
Copy Markdown
Contributor Author

Very gentle ping here @laskoviymishka

@mbutrovich
mbutrovich self-requested a review November 5, 2026 15:47

@mbutrovich mbutrovich left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @xanderbailey for splitting the metadata side of REPLACE TABLE into its own PR.

Comment on lines +211 to +216
/// # Errors
/// - The table is format version 1, which is not supported.
/// - `format-version` is invalid or would downgrade the table.
/// - The partition spec or sort order names a column missing from `schema`.
/// - `schema` takes the name of a partition field in one of the table's specs without being
/// that field's source column.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could we add tests for the three errors in this list that no test covers yet? They are a downgrade through format-version (for example a v3 table given format-version=2), a sort order on a column missing from schema, and a replacement schema that adds a column named after a partition field of an existing spec, such as x_bucket on a table partitioned by bucket(8, x). The existing tests cover the v1, invalid format-version, and missing partition source cases.

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.

/// - The partition spec or sort order names a column missing from `schema`.
/// - `schema` takes the name of a partition field in one of the table's specs without being
/// that field's source column.
pub fn build_replacement(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could we add a rustdoc example for build_replacement that runs as a doctest? It's a new public entry point with five positional arguments. An example that replaces the schema and spec, then calls build(), would show callers how it's meant to be used. It would also show where the caller adds the snapshot with the new contents before building.

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.

Comment on lines +232 to +244
let format_version = properties
.get(TableProperties::PROPERTY_FORMAT_VERSION)
.map(|value| {
serde_json::from_str::<FormatVersion>(value).map_err(|err| {
invalid_data!("Unsupported format version: {value}").with_source(err)
})
})
.transpose()?
.unwrap_or(self.metadata.format_version);
let properties = properties
.into_iter()
.filter(|(key, _)| !TableProperties::RESERVED_PROPERTIES.contains(&key.as_str()))
.collect();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should build_replacement take the target version as a typed format_version: Option<FormatVersion>, and pass properties to set_properties unchanged?

As I read it, this would be the only entry point in the crate that reads format-version out of a property map. TableMetadataBuilder::new takes a typed FormatVersion. Both new and set_properties return an error for any reserved key. So the same property map behaves differently depending on the entry point. With uuid in properties, new returns Table properties should not contain reserved properties, but got: [uuid], while build_replacement drops it without an error. A typed argument would also remove the serde_json parse of a string.

Something like:

pub fn build_replacement(
    self,
    schema: Schema,
    spec: impl Into<UnboundPartitionSpec>,
    sort_order: SortOrder,
    location: Option<String>,
    format_version: Option<FormatVersion>,
    properties: HashMap<String, String>,
) -> Result<Self> {
    // ...
    let format_version = format_version.unwrap_or(self.metadata.format_version);

Once this ships, the stability policy keeps whichever way it handles reserved keys, so it would be good to settle that in this PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1 on a typed format_version. Java splits the same way: the map-based buildReplacement and newTableMetadata(…, Map) read format-version from the map and drop reserved keys in persistedProperties, while the internal typed overload rejects them. new here already follows the typed side, so a typed argument plus set_properties erroring on reserved keys would line up with it.

One parity question on properties: persistedProperties also writes write.parquet.compression-codec=zstd before the caller's keys, so a Java REPLACE turns a gzip table into zstd, while here gzip survives. Keeping the user's codec seems right to me, and new doesn't write that default either. Should the rustdoc say the existing codec is kept?

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.

@laskoviymishka laskoviymishka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry this took longer than the weekend. As I read it, the id reuse matches Java's buildReplacement: full names for columns, the highest equivalent id for partition fields, and only main dropped. It needs a bit more work before I'd approve: pub(crate) until the RTAS consumer exists, plus the refs test and the field-id doc wording.

The partition-id question is smaller.

@mbutrovich's three threads still gate merge, and I've added the Java side to the format_version one.

Comment on lines +200 to +202
/// Columns the table already has keep their field ids, so data written before the replacement
/// stays readable through the columns both schemas share; new columns get ids above
/// `last_column_id`, never a dropped column's id.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this say ids are matched by full name only? resolve_or_assign_id doesn't look at the type, so a replacement that turns x: long into x: string keeps x's id and the readability promise doesn't hold for it. Java's AssignFreshIds matches by name too, so this is the doc, not the code.

Suggested change
/// Columns the table already has keep their field ids, so data written before the replacement
/// stays readable through the columns both schemas share; new columns get ids above
/// `last_column_id`, never a dropped column's id.
/// Columns are matched to the current schema by full name and keep their field ids, even
/// when their type changes; new columns get ids above `last_column_id`, never a dropped
/// column's id.

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.

/// - The partition spec or sort order names a column missing from `schema`.
/// - `schema` takes the name of a partition field in one of the table's specs without being
/// that field's source column.
pub fn build_replacement(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could this be pub(crate) until the RTAS consumer lands? At this head only tests call it, and #3056 doesn't link a Transaction or catalog PR yet. The signature is still moving in the format_version thread, and once it's in public-api.txt the stability policy keeps it. #3066 landed assign_fresh_ids the same way, with #[allow(dead_code)] and a TODO pointing at #3056.

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.

@mbutrovich asked for a rust doc example, would you prefer to have this pub(crate) and drop that for now and add it later or keep it pub and have the example? @mbutrovich any thoughts?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@mbutrovich asked for a rust doc example, would you prefer to have this pub(crate) and drop that for now and add it later or keep it pub and have the example?

I'd go with pub(crate) for now. Nothing outside the tests calls it yet, and the RTAS consumer may want a different signature. Once it ships as pub, changing it is a breaking change. Marking it #[allow(dead_code)] with a TODO pointing at #3056, as #3066 did for assign_fresh_ids, builds without warnings. make generate-public-api then drops it from public-api.txt.

A doctest compiles as an external crate, so it can't call a pub(crate) function. Could you move the example's flow into a unit test? The doctest is the only place that adds a snapshot and sets main after build_replacement, which is how the RTAS commit will use it. The doctest can come back in the PR that makes it pub.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thanks, pub(crate) with the #3056 TODO works for me.

let fresh_spec = self.reassign_partition_field_ids(fresh_spec)?;

let mut builder = self.upgrade_format_version(format_version)?;
if builder.metadata.refs.contains_key(MAIN_BRANCH) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we add a test where the table has a second branch or tag, and say in the rustdoc that refs other than main are kept? Only main is removed here, as in Java's buildReplacement, but populated_builder sets only MAIN_BRANCH, so nothing pins the rest.

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.

let field_id = *field_ids
.entry((vec![field.source_id], field.transform))
.or_insert_with(|| {
last_partition_id += 1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could this hand id-less fields to add_default_partition_spec and drop the second copy of the id rule? As I read it, add_partition_spec reuses through the same highest_equivalent_field_ids over all specs and allocates above last_partition_id, so the ids should match. The copies already differ at the edge: += 1 here panics in debug at i32::MAX, while set_field_ids returns an error.

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.

Tried this, but add_partition_spec records AddSpec from the unbound spec, so new fields would reach a REST catalog without ids, and a Java server would number them from 1000, colliding with reused ids. Java's buildReplacement sends the bound ids, so I've kept the assignment here. Making add_partition_spec record the ids it assigns would change it for every caller, so I'd rather do that separately.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The copies already differ at the edge: += 1 here panics in debug at i32::MAX, while set_field_ids returns an error.

Keeping the assignment here makes sense given how AddSpec records ids. The overflow is still there at this head, though. With last_partition_id set to i32::MAX and a replacement spec that adds bucket(8, z), build_replacement panics with attempt to add with overflow at line 1408 in a test build. A release build without overflow checks wraps to i32::MIN and gives the new field that id. The spec assigns new ids based on the last assigned partition ID so that partition fields are always assigned an unused ID, and a wrapped id has no such guarantee.

Could this return an error when no id is left, with a test that sets last_partition_id to i32::MAX? One option also drops the second copy of the id rule. reuse_partition_field_ids could fill in only the reused ids, then PartitionSpecBuilder::new_from_unbound(...).with_last_assigned_field_id(self.metadata.last_partition_id) assigns the rest. That goes through set_field_ids, which already returns Cannot assign more partition ids. Overflow.. Calling into_unbound() on the bound spec keeps every id, so AddSpec still carries them.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you're right, withdrawn. With id-less fields passed to add_default_partition_spec, the bound ids come out the same, but the recorded AddSpec has no field-id for the new field (serde skips None), so a Java server would number it from 1000. The set_field_ids route keeps every id in AddSpec (pinned here) and errors at i32::MAX.

Comment on lines +232 to +244
let format_version = properties
.get(TableProperties::PROPERTY_FORMAT_VERSION)
.map(|value| {
serde_json::from_str::<FormatVersion>(value).map_err(|err| {
invalid_data!("Unsupported format version: {value}").with_source(err)
})
})
.transpose()?
.unwrap_or(self.metadata.format_version);
let properties = properties
.into_iter()
.filter(|(key, _)| !TableProperties::RESERVED_PROPERTIES.contains(&key.as_str()))
.collect();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1 on a typed format_version. Java splits the same way: the map-based buildReplacement and newTableMetadata(…, Map) read format-version from the map and drop reserved keys in persistedProperties, while the internal typed overload rejects them. new here already follows the typed side, so a typed argument plus set_properties erroring on reserved keys would line up with it.

One parity question on properties: persistedProperties also writes write.parquet.compression-codec=zstd before the caller's keys, so a Java REPLACE turns a gzip table into zstd, while here gzip survives. Keeping the user's codec seems right to me, and new doesn't write that default either. Should the rustdoc say the existing codec is kept?

Backs `REPLACE TABLE`: swaps the schema, partition spec and sort order
while keeping the table's uuid, properties and snapshots, and drops the
main branch so the replaced table has no current snapshot.

Columns the table already has keep their field ids, so data files written
before the replacement stay readable through the shared columns. New
columns draw ids above `last_column_id`, never reusing a dropped column's
id. Partition field ids are likewise reused across historical specs.

Extracts `rebind_to_schema` out of `reassign_ids` so the partition spec
and sort order can be rebound to a schema whose ids were assigned either
from scratch or by `assign_fresh_ids`.

Format version 1 is not supported.
@xanderbailey

Copy link
Copy Markdown
Contributor Author

@laskoviymishka @mbutrovich this is ready for another look I think

@mbutrovich mbutrovich left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @xanderbailey for the quick turnaround. The typed format_version and the new error-path tests look good. I stuck some replies:

Moves the doc example into a unit test, since a doctest cannot call a
pub(crate) function.
Reuses equivalent ids through reuse_partition_field_ids and assigns the
rest with PartitionSpecBuilder, which errors on overflow instead of
wrapping. The AddSpec change still carries every field id.

@laskoviymishka laskoviymishka left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thx @xanderbailey, both of my round-1 threads are in, and you were right about AddSpec (replied in that thread). Nothing else from me. I'll merge this once @mbutrovich has signed off on the builder as a whole, not just his own threads.

let field_id = *field_ids
.entry((vec![field.source_id], field.transform))
.or_insert_with(|| {
last_partition_id += 1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you're right, withdrawn. With id-less fields passed to add_default_partition_spec, the bound ids come out the same, but the recorded AddSpec has no field-id for the new field (serde skips None), so a Java server would number it from 1000. The set_field_ids route keeps every id in AddSpec (pinned here) and errors at i32::MAX.

/// - The partition spec or sort order names a column missing from `schema`.
/// - `schema` takes the name of a partition field in one of the table's specs without being
/// that field's source column.
pub fn build_replacement(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thanks, pub(crate) with the #3056 TODO works for me.

@xanderbailey

Copy link
Copy Markdown
Contributor Author

@mbutrovich gentle ping here if you've got a moment to look

@mbutrovich

Copy link
Copy Markdown
Collaborator

@mbutrovich gentle ping here if you've got a moment to look

Sorry, catching up after US holiday yesterday. Looking now.

@mbutrovich mbutrovich left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @xanderbailey. This looks good to me now.

@xanderbailey

Copy link
Copy Markdown
Contributor Author

Super speedy, appreciate it!

@xanderbailey

Copy link
Copy Markdown
Contributor Author

@laskoviymishka think this is ready to ship when you have a moment! Thanks for the reviews everyone

@xanderbailey

Copy link
Copy Markdown
Contributor Author

@laskoviymishka very gentle reminder here

@laskoviymishka laskoviymishka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@mbutrovich has signed off on the whole builder, which was what I was waiting on, and nothing has been pushed since 14f87b6. Merging this now. Thanks for the patience @xanderbailey.

@laskoviymishka
laskoviymishka added this pull request to the merge queue Nov 16, 2026
Merged via the queue into apache:main with commit f52ec21 Nov 16, 2026
24 checks passed
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.

3 participants