Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: json schema serializes field metadata #3379

Merged
merged 3 commits into from
Jan 14, 2025
Merged
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
67 changes: 66 additions & 1 deletion rust/lance/src/arrow/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ pub struct JsonField {
#[serde(rename = "type")]
type_: JsonDataType,
nullable: bool,

#[serde(skip_serializing_if = "Option::is_none")]
Copy link
Contributor

Choose a reason for hiding this comment

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

Not super important but would it be possible to do skip_serializing_if = "HashMap::is_empty" and then skip the option? I'm not sure if that is more or less clear.]

Hmm...maybe this won't work on deserialization.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking we'd keep this as Option::is_none as it maintains consistency with how we're currently handling this for schema metadata

metadata: Option<HashMap<String, String>>,
}

impl TryFrom<&Field> for JsonField {
Expand All @@ -180,10 +183,17 @@ impl TryFrom<&Field> for JsonField {
fn try_from(field: &Field) -> Result<Self> {
let data_type = JsonDataType::try_new(field.data_type())?;

let metadata = if field.metadata().is_empty() {
None
} else {
Some(field.metadata().clone())
};

Ok(Self {
name: field.name().to_string(),
nullable: field.is_nullable(),
type_: data_type,
metadata,
})
}
}
Expand All @@ -193,7 +203,11 @@ impl TryFrom<&JsonField> for Field {

fn try_from(value: &JsonField) -> Result<Self> {
let data_type = DataType::try_from(&value.type_)?;
Ok(Self::new(&value.name, data_type, value.nullable))
let mut field = Self::new(&value.name, data_type, value.nullable);
if let Some(metadata) = value.metadata.clone() {
field.set_metadata(metadata);
}
Ok(field)
}
}

Expand Down Expand Up @@ -445,4 +459,55 @@ mod test {
let actual = Schema::from_json(&json_str).unwrap();
assert_eq!(schema, actual);
}

#[test]
fn test_metadata_roundtrip() {
let mut schema_metadata = HashMap::new();
schema_metadata.insert("sk_1".to_string(), "sv_1".to_string());

let mut field1_metadata = HashMap::new();
field1_metadata.insert("fk_1".to_string(), "fv_1".to_string());

let field1 = Field::new("a", DataType::UInt8, false).with_metadata(field1_metadata.clone());
let field2 = Field::new("b", DataType::Int32, true);

let schema = Schema::new_with_metadata(vec![field1, field2], schema_metadata.clone());

let json_str = schema.to_json().unwrap();
assert_eq!(
serde_json::from_str::<Value>(&json_str).unwrap(),
json!({
"fields": [
{
"name": "a",
"type": {
"type": "uint8"
},
"nullable": false,
"metadata": {
"fk_1": "fv_1"
}
},
{
"name": "b",
"type": {
"type": "int32"
},
"nullable": true
}
],
"metadata": {
"sk_1": "sv_1"
}
})
);

let actual = Schema::from_json(&json_str).unwrap();
assert_eq!(schema, actual);

assert_eq!(actual.metadata, schema_metadata);

assert_eq!(actual.field(0).metadata(), &field1_metadata);
assert_eq!(actual.field(1).metadata(), &HashMap::new());
}
}
Loading