Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 73 additions & 8 deletions crates/catalog/glue/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,9 @@ impl GlueSchemaBuilder {

visit_schema(current_schema, &mut builder)?;

// Keeping the returned builder state unchanged for compatibility with callers that may inspect it before calling `build`.
builder.is_current = false;

for schema in metadata.schemas_iter() {
if schema.schema_id() == current_schema.schema_id() {
continue;
}

visit_schema(schema, &mut builder)?;
}

Ok(builder)
}

Expand Down Expand Up @@ -338,6 +331,78 @@ mod tests {
Ok(())
}

#[test]
fn test_schema_evolution_only_converts_current_schema_fields() -> Result<()> {
let initial_schema = serde_json::from_str::<Schema>(
r#"{
"type": "struct",
"schema-id": 1,
"fields": [
{
"id": 1,
"name": "id",
"required": true,
"type": "long"
},
{
"id": 2,
"name": "name",
"required": false,
"type": "string"
}
]
}"#,
)?;
let metadata = create_metadata(initial_schema)?;

let evolved_schema = serde_json::from_str::<Schema>(
r#"{
"type": "struct",
"schema-id": 2,
"fields": [
{
"id": 1,
"name": "id",
"required": true,
"type": "long"
},
{
"id": 2,
"name": "name",
"required": false,
"type": "string"
},
{
"id": 3,
"name": "age",
"required": false,
"type": "int"
}
]
}"#,
)?;

let metadata = TableMetadataBuilder::new_from_metadata(metadata, None)
.add_schema(evolved_schema)?
.set_current_schema(TableMetadataBuilder::LAST_ADDED)?
.build()?
.metadata;

assert_eq!(metadata.schemas_iter().len(), 2);

let result = GlueSchemaBuilder::from_iceberg(&metadata)?.build();

let expected = vec![
create_column("id", "bigint", "1", false)?,
create_column("name", "string", "2", true)?,
create_column("age", "int", "3", true)?,
];

assert_eq!(result, expected);

Ok(())
}

#[test]
fn test_schema_with_structs() -> Result<()> {
let record = r#"{
Expand Down
Loading