From 26970be8168758c6b638c52bd17f47a751c7b625 Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Fri, 26 Sep 2025 12:05:38 -0400 Subject: [PATCH 1/2] Add examples of using Field::try_extension_type --- arrow-schema/src/field.rs | 59 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/arrow-schema/src/field.rs b/arrow-schema/src/field.rs index c0dcaff45cac..d96836fb8c7a 100644 --- a/arrow-schema/src/field.rs +++ b/arrow-schema/src/field.rs @@ -491,7 +491,12 @@ impl Field { /// Returns an instance of the given [`ExtensionType`] of this [`Field`], /// if set in the [`Field::metadata`]. /// - /// # Error + /// Note that using `try_extension_type` with an extension type that does + /// not match the name in the metadata will return an `ArrowError` which can + /// be slow due to string allocations. If you only want to check if a + /// [`Field`] has a specific [`ExtensionType`], see the example below. + /// + /// # Errors /// /// Returns an error if /// - this field does not have the name of this extension type @@ -502,6 +507,58 @@ impl Field { /// - the construction of the extension type ([`ExtensionType::try_new`]) /// fail (for example when the [`Field::data_type`] is not supported by /// the extension type ([`ExtensionType::supports_data_type`])) + /// + /// # Examples: Check and retrieve an extension type + /// You can use this to check if a [`Field`] has a specific + /// [`ExtensionType`] and retrieve it: + /// ``` + /// # use arrow_schema::{DataType, Field, ArrowError}; + /// # use arrow_schema::extension::ExtensionType; + /// # struct MyExtensionType; + /// # impl ExtensionType for MyExtensionType { + /// # const NAME: &'static str = "my_extension"; + /// # type Metadata = String; + /// # fn supports_data_type(&self, data_type: &DataType) -> Result<(), ArrowError> { Ok(()) } + /// # fn try_new(data_type: &DataType, metadata: Self::Metadata) -> Result { Ok(Self) } + /// # fn serialize_metadata(&self) -> Option { unimplemented!() } + /// # fn deserialize_metadata(s: Option<&str>) -> Result { unimplemented!() } + /// # fn metadata(&self) -> &::Metadata { todo!() } + /// # } + /// # fn get_field() -> Field { Field::new("field", DataType::Null, false) } + /// let field = get_field(); + /// if let Ok(extension_type) = field.try_extension_type::() { + /// // do something with extension_type + /// } + /// ``` + /// + /// # Example: Checking if a field has a specific extension type first + /// + /// Since `try_extension_type` returns an error, it is more + /// efficient to first check if the name matches before calling + /// `try_extension_type`: + /// ``` + /// # use arrow_schema::{DataType, Field, ArrowError}; + /// # use arrow_schema::extension::ExtensionType; + /// # struct MyExtensionType; + /// # impl ExtensionType for MyExtensionType { + /// # const NAME: &'static str = "my_extension"; + /// # type Metadata = String; + /// # fn supports_data_type(&self, data_type: &DataType) -> Result<(), ArrowError> { Ok(()) } + /// # fn try_new(data_type: &DataType, metadata: Self::Metadata) -> Result { Ok(Self) } + /// # fn serialize_metadata(&self) -> Option { unimplemented!() } + /// # fn deserialize_metadata(s: Option<&str>) -> Result { unimplemented!() } + /// # fn metadata(&self) -> &::Metadata { todo!() } + /// # } + /// # fn get_field() -> Field { Field::new("field", DataType::Null, false) } + /// let field = get_field(); + /// // First check if the name matches before calling the potentially expensive `try_extension_type` + /// if field.extension_type_name() == Some(MyExtensionType::NAME) { + /// if let Ok(extension_type) = field.try_extension_type::() { + /// // do something with extension_type + /// } + /// } + /// ``` + pub fn try_extension_type(&self) -> Result { // Check the extension name in the metadata match self.extension_type_name() { From 52e06142d02790d58aee0cfc66e7aa64ed55e865 Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Fri, 26 Sep 2025 12:13:20 -0400 Subject: [PATCH 2/2] fmt --- arrow-schema/src/field.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/arrow-schema/src/field.rs b/arrow-schema/src/field.rs index d96836fb8c7a..e40917614b23 100644 --- a/arrow-schema/src/field.rs +++ b/arrow-schema/src/field.rs @@ -558,7 +558,6 @@ impl Field { /// } /// } /// ``` - pub fn try_extension_type(&self) -> Result { // Check the extension name in the metadata match self.extension_type_name() {