Skip to content

Commit c2c67d6

Browse files
committed
Improve example and fix docs issue
1 parent 794025f commit c2c67d6

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

datafusion-examples/examples/extension_types/temperature.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
205224
impl 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),

datafusion/expr/src/registry.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,8 @@ pub trait ExtensionTypeRegistry: Debug + Send + Sync {
327327
}
328328

329329
/// A factory that creates instances of extension types from a storage [`DataType`] and the
330-
/// metadata. [`TExtensionType`] should implement both, the arrow-rs [`ExtensionType`] and the
331-
/// DataFusion [`DFExtensionType`] traits.
332-
pub type DefaultExtensionTypeFactory<TExtensionType> = dyn Fn(&DataType, <TExtensionType as ExtensionType>::Metadata) -> Result<TExtensionType>
330+
/// metadata.
331+
pub type ExtensionTypeFactory<TExtensionType> = dyn Fn(&DataType, <TExtensionType as ExtensionType>::Metadata) -> Result<TExtensionType>
333332
+ Send
334333
+ Sync;
335334

@@ -340,7 +339,7 @@ pub struct DefaultExtensionTypeRegistration<
340339
> {
341340
/// A function that creates an instance of [`DFExtensionTypeRef`] from the storage type and the
342341
/// metadata.
343-
factory: Box<DefaultExtensionTypeFactory<TExtensionType>>,
342+
factory: Box<ExtensionTypeFactory<TExtensionType>>,
344343
}
345344

346345
impl<TExtensionType: ExtensionType + DFExtensionType + 'static>

0 commit comments

Comments
 (0)