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 accessing name from ffi schema #6273

Merged
merged 2 commits into from
Aug 20, 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
28 changes: 21 additions & 7 deletions arrow-schema/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,17 @@ impl FFI_ArrowSchema {
}

/// returns the name of this schema.
pub fn name(&self) -> &str {
assert!(!self.name.is_null());
// safe because the lifetime of `self.name` equals `self`
unsafe { CStr::from_ptr(self.name) }
.to_str()
.expect("The external API has a non-utf8 as name")
pub fn name(&self) -> Option<&str> {
if self.name.is_null() {
None
} else {
// safe because the lifetime of `self.name` equals `self`
Some(
unsafe { CStr::from_ptr(self.name) }
.to_str()
.expect("The external API has a non-utf8 as name"),
)
}
}

pub fn flags(&self) -> Option<Flags> {
Expand Down Expand Up @@ -582,7 +587,7 @@ impl TryFrom<&FFI_ArrowSchema> for Field {

fn try_from(c_schema: &FFI_ArrowSchema) -> Result<Self, ArrowError> {
let dtype = DataType::try_from(c_schema)?;
let mut field = Field::new(c_schema.name(), dtype, c_schema.nullable());
let mut field = Field::new(c_schema.name().unwrap_or(""), dtype, c_schema.nullable());
field.set_metadata(c_schema.metadata()?);
Ok(field)
}
Expand Down Expand Up @@ -918,4 +923,13 @@ mod tests {
assert_eq!(field.metadata(), &metadata);
}
}

#[test]
fn test_import_field_with_null_name() {
let dtype = DataType::Int16;
let c_schema = FFI_ArrowSchema::try_from(&dtype).unwrap();
assert!(c_schema.name().is_none());
let field = Field::try_from(&c_schema).unwrap();
assert_eq!(field.name(), "");
}
}
Loading