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: metadata string conversion #328

Merged
merged 1 commit into from
Sep 9, 2024
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
36 changes: 34 additions & 2 deletions kernel/src/engine/arrow_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use arrow_schema::{
use itertools::Itertools;

use crate::error::Error;
use crate::schema::{ArrayType, DataType, MapType, PrimitiveType, StructField, StructType};
use crate::schema::{
ArrayType, DataType, MapType, MetadataValue, PrimitiveType, StructField, StructType,
};

pub(crate) const LIST_ARRAY_ROOT: &str = "element";
pub(crate) const MAP_ROOT_DEFAULT: &str = "key_value";
Expand All @@ -32,7 +34,10 @@ impl TryFrom<&StructField> for ArrowField {
let metadata = f
.metadata()
.iter()
.map(|(key, val)| Ok((key.clone(), serde_json::to_string(val)?)))
.map(|(key, val)| match &val {
&MetadataValue::String(val) => Ok((key.clone(), val.clone())),
_ => Ok((key.clone(), serde_json::to_string(val)?)),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Am I understanding correctly that the first match arm is for metadata values that were already strings, and the second is for metadata values of other types, that need to become strings so ArrowField can grok them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup! That's what's happening

})
.collect::<Result<_, serde_json::Error>>()
.map_err(|err| ArrowError::JsonError(err.to_string()))?;

Expand Down Expand Up @@ -250,3 +255,30 @@ impl TryFrom<&ArrowDataType> for DataType {
}
}
}

#[cfg(test)]
mod tests {
use crate::engine::arrow_conversion::ArrowField;
use crate::{
schema::{DataType, StructField},
DeltaResult,
};
use std::collections::HashMap;

#[test]
fn test_metadata_string_conversion() -> DeltaResult<()> {
let mut metadata = HashMap::new();
metadata.insert("description", "hello world".to_owned());
let struct_field =
StructField::new("name", DataType::STRING, false).with_metadata(metadata);

let arrow_field = ArrowField::try_from(&struct_field)?;
let new_metadata = arrow_field.metadata();

assert_eq!(
new_metadata.get("description").unwrap(),
&"hello world".to_owned()
);
Ok(())
}
}
Loading