Skip to content

Commit

Permalink
fix: properly load field metadata from a table with identity columns
Browse files Browse the repository at this point in the history
Fixes #2152
  • Loading branch information
rtyler committed Feb 1, 2024
1 parent 3ec28cc commit bdb03a3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions crates/core/src/kernel/models/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub enum MetadataValue {
Number(i32),
/// A string value
String(String),
/// A Boolean value
Boolean(bool),
}

impl From<String> for MetadataValue {
Expand All @@ -45,6 +47,12 @@ impl From<i32> for MetadataValue {
}
}

impl From<bool> for MetadataValue {
fn from(value: bool) -> Self {
Self::Boolean(value)
}
}

impl From<Value> for MetadataValue {
fn from(value: Value) -> Self {
Self::String(value.to_string())
Expand Down Expand Up @@ -184,6 +192,7 @@ impl StructField {
let phys_name = self.get_config_value(&ColumnMetadataKey::ColumnMappingPhysicalName);
match phys_name {
None => Ok(&self.name),
Some(MetadataValue::Boolean(_)) => Ok(&self.name),
Some(MetadataValue::String(s)) => Ok(s),
Some(MetadataValue::Number(_)) => Err(Error::MetadataError(
"Unexpected type for physical name".to_string(),
Expand Down Expand Up @@ -850,4 +859,11 @@ mod tests {
Invariant::new("a_map.value.element.d", "a_map.value.element.d < 4")
);
}

/// <https://github.com/delta-io/delta-rs/issues/2152>
#[test]
fn test_identity_columns() {
let buf = r#"{"type":"struct","fields":[{"name":"ID_D_DATE","type":"long","nullable":true,"metadata":{"delta.identity.start":1,"delta.identity.step":1,"delta.identity.allowExplicitInsert":false}},{"name":"TXT_DateKey","type":"string","nullable":true,"metadata":{}}]}"#;
let schema: StructType = serde_json::from_str(buf).expect("Failed to load");
}
}
9 changes: 9 additions & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,4 +676,13 @@ mod tests {
DeltaTableError::InvalidTableLocation(_expected_error_msg),
))
}

/// <https://github.com/delta-io/delta-rs/issues/2152>
#[tokio::test]
async fn test_identity_column() {
let path = "../test/tests/data/issue-2152";
let _ = crate::open_table(path)
.await
.expect("Failed to load the table");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"commitInfo":{"timestamp":1706770085847,"operation":"CREATE TABLE","operationParameters":{"partitionBy":"[]","description":null,"isManaged":"true","properties":"{}","statsOnLoad":false},"isolationLevel":"WriteSerializable","isBlindAppend":true,"operationMetrics":{},"tags":{"restoresDeletedRows":"false"},"engineInfo":"Databricks-Runtime/14.1.x-photon-scala2.12","txnId":"5ba2d1f4-09e0-4013-920a-92b057185128"}}
{"metaData":{"id":"7791991a-60e9-4a8f-bff0-ccffec779dc4","format":{"provider":"parquet","options":{}},"schemaString":"{\"type\":\"struct\",\"fields\":[{\"name\":\"ID_D_DATE\",\"type\":\"long\",\"nullable\":true,\"metadata\":{\"delta.identity.start\":1,\"delta.identity.step\":1,\"delta.identity.allowExplicitInsert\":false}},{\"name\":\"TXT_DateKey\",\"type\":\"string\",\"nullable\":true,\"metadata\":{}}]}","partitionColumns":[],"configuration":{},"createdTime":1706770085202}}
{"protocol":{"minReaderVersion":1,"minWriterVersion":6}}

0 comments on commit bdb03a3

Please sign in to comment.