@@ -52,7 +52,10 @@ fn create_session_context() -> Result<SessionContext> {
5252 // The registration creates a new instance of the extension type with the deserialized metadata.
5353 let temp_registration =
5454 DefaultExtensionTypeRegistration :: new_arc ( |storage_type, metadata| {
55- Ok ( TemperatureExtensionType ( storage_type. clone ( ) , metadata) )
55+ Ok ( TemperatureExtensionType :: new (
56+ storage_type. clone ( ) ,
57+ metadata,
58+ ) )
5659 } ) ;
5760 registry. add_extension_type_registration ( temp_registration) ?;
5861
@@ -94,13 +97,13 @@ fn example_schema() -> SchemaRef {
9497 Arc :: new ( Schema :: new ( vec ! [
9598 Field :: new( "city" , DataType :: Utf8 , false ) ,
9699 Field :: new( "celsius" , DataType :: Float64 , false ) . with_extension_type(
97- TemperatureExtensionType ( DataType :: Float64 , TemperatureUnit :: Celsius ) ,
100+ TemperatureExtensionType :: new ( DataType :: Float64 , TemperatureUnit :: Celsius ) ,
98101 ) ,
99102 Field :: new( "fahrenheit" , DataType :: Float64 , false ) . with_extension_type(
100- TemperatureExtensionType ( DataType :: Float64 , TemperatureUnit :: Fahrenheit ) ,
103+ TemperatureExtensionType :: new ( DataType :: Float64 , TemperatureUnit :: Fahrenheit ) ,
101104 ) ,
102105 Field :: new( "kelvin" , DataType :: Float32 , false ) . with_extension_type(
103- TemperatureExtensionType ( DataType :: Float32 , TemperatureUnit :: Kelvin ) ,
106+ TemperatureExtensionType :: new ( DataType :: Float32 , TemperatureUnit :: Kelvin ) ,
104107 ) ,
105108 ] ) )
106109}
@@ -131,7 +134,23 @@ pub enum TemperatureUnit {
131134/// See [the official Arrow documentation](https://arrow.apache.org/docs/format/Columnar.html#extension-types)
132135/// for more details on the extension type mechanism.
133136#[ derive( Debug ) ]
134- pub struct TemperatureExtensionType ( DataType , TemperatureUnit ) ;
137+ pub struct TemperatureExtensionType {
138+ /// Extension type instances are always for a specific storage type and metadata pairing.
139+ /// Therefore, we store the storage type.
140+ storage_type : DataType ,
141+ /// The unit of the temperature.
142+ temperature_unit : TemperatureUnit ,
143+ }
144+
145+ impl TemperatureExtensionType {
146+ /// Creates a new [`TemperatureExtensionType`].
147+ pub fn new ( storage_type : DataType , temperature_unit : TemperatureUnit ) -> Self {
148+ Self {
149+ storage_type,
150+ temperature_unit,
151+ }
152+ }
153+ }
135154
136155/// Implementation of [`ExtensionType`] for [`TemperatureExtensionType`].
137156///
@@ -142,15 +161,15 @@ impl ExtensionType for TemperatureExtensionType {
142161 type Metadata = TemperatureUnit ;
143162
144163 fn metadata ( & self ) -> & Self :: Metadata {
145- & self . 1
164+ & self . temperature_unit
146165 }
147166
148167 /// Arrow extension type metadata is encoded as a string and stored using the
149168 /// `ARROW:extension:metadata` key. As we only store the name of the unit, a simple string
150169 /// suffices. Extension types can store more complex metadata using serialization formats like
151170 /// JSON.
152171 fn serialize_metadata ( & self ) -> Option < String > {
153- let s = match self . 1 {
172+ let s = match self . temperature_unit {
154173 TemperatureUnit :: Celsius => "celsius" ,
155174 TemperatureUnit :: Fahrenheit => "fahrenheit" ,
156175 TemperatureUnit :: Kelvin => "kelvin" ,
@@ -193,7 +212,7 @@ impl ExtensionType for TemperatureExtensionType {
193212 data_type : & DataType ,
194213 metadata : Self :: Metadata ,
195214 ) -> std:: result:: Result < Self , ArrowError > {
196- let instance = Self ( data_type. clone ( ) , metadata) ;
215+ let instance = Self :: new ( data_type. clone ( ) , metadata) ;
197216 instance. supports_data_type ( data_type) ?;
198217 Ok ( instance)
199218 }
@@ -204,7 +223,7 @@ impl ExtensionType for TemperatureExtensionType {
204223/// This implements the trait for customizing DataFusion.
205224impl DFExtensionType for TemperatureExtensionType {
206225 fn storage_type ( & self ) -> DataType {
207- self . 0 . clone ( )
226+ self . storage_type . clone ( )
208227 }
209228
210229 fn serialize_metadata ( & self ) -> Option < String > {
@@ -216,12 +235,12 @@ impl DFExtensionType for TemperatureExtensionType {
216235 array : & ' fmt dyn Array ,
217236 options : & FormatOptions < ' fmt > ,
218237 ) -> Result < Option < ArrayFormatter < ' fmt > > > {
219- match self . 0 {
238+ match self . storage_type {
220239 DataType :: Float32 => {
221240 let display_index = TemperatureDisplayIndex {
222241 array : array. as_primitive :: < Float32Type > ( ) ,
223242 null_str : options. null ( ) ,
224- unit : self . 1 ,
243+ unit : self . temperature_unit ,
225244 } ;
226245 Ok ( Some ( ArrayFormatter :: new (
227246 Box :: new ( display_index) ,
@@ -232,7 +251,7 @@ impl DFExtensionType for TemperatureExtensionType {
232251 let display_index = TemperatureDisplayIndex {
233252 array : array. as_primitive :: < Float64Type > ( ) ,
234253 null_str : options. null ( ) ,
235- unit : self . 1 ,
254+ unit : self . temperature_unit ,
236255 } ;
237256 Ok ( Some ( ArrayFormatter :: new (
238257 Box :: new ( display_index) ,
0 commit comments