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: enable field_with_name to support nested fields with '.' delimiter #2519

Merged
merged 2 commits into from
May 17, 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
37 changes: 36 additions & 1 deletion crates/core/src/kernel/models/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,19 @@ impl StructType {

/// Returns a reference of a specific [`StructField`] instance selected by name.
pub fn field_with_name(&self, name: &str) -> Result<&StructField, Error> {
Ok(&self.fields[self.index_of(name)?])
match name.split_once('.') {
Some((parent, children)) => {
let parent_field = &self.fields[self.index_of(parent)?];
match parent_field.data_type {
DataType::Struct(ref inner) => Ok(inner.field_with_name(children)?),
_ => Err(Error::Schema(format!(
"Field {} is not a struct type",
parent_field.name()
))),
}
}
None => Ok(&self.fields[self.index_of(name)?]),
}
}

/// Get all invariants in the schemas
Expand Down Expand Up @@ -983,4 +995,27 @@ mod tests {
);
assert_eq!(get_hash(&field_1), get_hash(&field_2));
}

#[test]
fn test_field_with_name() {
let schema = StructType::new(vec![
StructField::new("a", DataType::STRING, true),
StructField::new("b", DataType::INTEGER, true),
]);
let field = schema.field_with_name("b").unwrap();
assert_eq!(*field, StructField::new("b", DataType::INTEGER, true));
}

#[test]
fn test_field_with_name_nested() {
let nested = StructType::new(vec![StructField::new("a", DataType::BOOLEAN, true)]);
let schema = StructType::new(vec![
StructField::new("a", DataType::STRING, true),
StructField::new("b", DataType::Struct(Box::new(nested)), true),
]);

let field = schema.field_with_name("b.a").unwrap();

assert_eq!(*field, StructField::new("a", DataType::BOOLEAN, true));
}
}
Loading