From 9875740e625b78bfb0b6545eab63a17f47c6a122 Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Mon, 19 Aug 2024 11:48:52 -0400 Subject: [PATCH 1/2] Fix accessing name from ffi schema --- arrow-schema/src/ffi.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/arrow-schema/src/ffi.rs b/arrow-schema/src/ffi.rs index b0c6a9f520ed..9610466bab2f 100644 --- a/arrow-schema/src/ffi.rs +++ b/arrow-schema/src/ffi.rs @@ -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 { @@ -582,7 +587,7 @@ impl TryFrom<&FFI_ArrowSchema> for Field { fn try_from(c_schema: &FFI_ArrowSchema) -> Result { 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) } From ad3ba8e6f007477b8becb9f351de4a48195cf807 Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Mon, 19 Aug 2024 14:47:12 -0400 Subject: [PATCH 2/2] Add test --- arrow-schema/src/ffi.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arrow-schema/src/ffi.rs b/arrow-schema/src/ffi.rs index 9610466bab2f..c68849059f8c 100644 --- a/arrow-schema/src/ffi.rs +++ b/arrow-schema/src/ffi.rs @@ -923,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(), ""); + } }