ggyuchive opened a new issue, #3785:
URL: https://github.com/apache/iceberg-rust/issues/3785

   ### Apache Iceberg Rust version
   
   v0.11.0
   
   ### Describe the bug
   
   `TableMetadataBuilder::set_ref` only checks that the snapshot exists. It 
accepts a `SnapshotReference` with `SnapshotRetention::Tag` for `main`, so 
`main` becomes a tag:
   
   
https://github.com/apache/iceberg-rust/blob/3cef7b693e53a3f8c31c9ee1172de298546a8969/crates/iceberg/src/spec/table_metadata_builder.rs#L573-L588
   
   `main` is the table's current-snapshot branch, and later commits append to 
it. `set_branch_snapshot` already refuses to append to a non-branch ref 
(`Cannot append snapshot to non-branch reference 'main'`), so once `main` is a 
tag, the next `set_branch_snapshot(.., "main")` fails (last line of the output 
below).
   
   Iceberg Java added this check in apache/iceberg#16753:
   
   ```java
   ValidationException.check(
       !SnapshotRef.MAIN_BRANCH.equals(name) || ref.isBranch(),
       "Cannot set %s to a tag, it must be a branch",
       SnapshotRef.MAIN_BRANCH);
   ```
   
   REST catalogs built on the builder inherit the gap. Lakekeeper 0.13.9 
accepted 
`{"action":"set-snapshot-ref","ref-name":"main","type":"tag","snapshot-id":1}` 
with 200; Lakekeeper 0.15.0 added its own guard, but the builder still allows 
it.
   
   ### To Reproduce
   
   Cargo.toml: iceberg = "0.11.0", serde_json = "1".
   
   ```rust
   use std::collections::HashMap;
   use iceberg::spec::*;
   
   fn main_ref(m: &TableMetadata) -> serde_json::Value {
       serde_json::to_value(m).unwrap()["refs"][MAIN_BRANCH].clone() // refs 
has no public getter
   }
   
   fn main() {
       let schema = Schema::builder()
           .with_fields(vec![NestedField::optional(1, "id", 
Type::Primitive(PrimitiveType::Long)).into()])
           .build().unwrap();
       let metadata = TableMetadataBuilder::new(schema, 
PartitionSpec::unpartition_spec(),
               SortOrder::unsorted_order(), "s3://bucket/t".into(), 
FormatVersion::V2, HashMap::new())
           .unwrap().build().unwrap().metadata;
       let snapshot = Snapshot::builder()
           .with_snapshot_id(1).with_sequence_number(1)
           .with_timestamp_ms(metadata.last_updated_ms() + 1).with_schema_id(0)
           .with_manifest_list("s3://bucket/t/metadata/snap-1.avro")
           .with_summary(Summary { operation: Operation::Append, 
additional_properties: HashMap::new() })
           .build();
       let metadata = metadata.into_builder(None)
           .set_branch_snapshot(snapshot, 
MAIN_BRANCH).unwrap().build().unwrap().metadata;
       println!("before: refs.main = {}", main_ref(&metadata));
   
       let tag = SnapshotReference { snapshot_id: 1, retention: 
SnapshotRetention::Tag { max_ref_age_ms: None } };
       let result = metadata.into_builder(None).set_ref(MAIN_BRANCH, 
tag).unwrap().build().unwrap();
       println!("after:  refs.main = {}", main_ref(&result.metadata));
       // the next append to main now fails:
       // result.metadata.into_builder(None).set_branch_snapshot(<snapshot 2>, 
MAIN_BRANCH) -> Err
   }
   ```
   
   Output (0.11.0 and main @ 3cef7b6):
   
   ```
   before: refs.main = {"snapshot-id":1,"type":"branch"}
   set_ref(main, tag) -> Ok; after: refs.main = {"snapshot-id":1,"type":"tag"}
   next append to main -> Err: DataInvalid => Cannot append snapshot to 
non-branch reference 'main'
   ```
   
   ### Expected behavior
   
   `set_ref(MAIN_BRANCH, <tag>)` returns an error such as `Cannot set main to a 
tag, it must be a branch`, as in Iceberg Java.
   
   ### Willingness to contribute
   
   I can contribute a fix for this bug independently


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to