Skip to content

Commit

Permalink
Merge pull request #328 from ion-elgreco/fix/schema_metadata
Browse files Browse the repository at this point in the history
fix: metadata string conversion
  • Loading branch information
hntd187 authored Sep 9, 2024
2 parents 1459b30 + c2e525f commit 0791631
Showing 1 changed file with 34 additions and 2 deletions.
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)?)),
})
.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(())
}
}

0 comments on commit 0791631

Please sign in to comment.