paleolimbot commented on code in PR #20312:
URL: https://github.com/apache/datafusion/pull/20312#discussion_r2995241541
##########
datafusion-examples/examples/extension_types/temperature.rs:
##########
@@ -181,45 +212,69 @@ impl ExtensionType for TemperatureExtensionType {
data_type: &DataType,
metadata: Self::Metadata,
) -> std::result::Result<Self, ArrowError> {
- let instance = Self(metadata);
+ let instance = Self::new(data_type.clone(), metadata);
instance.supports_data_type(data_type)?;
Ok(instance)
}
}
/// Implementation of [`DFExtensionType`] for [`TemperatureExtensionType`].
+///
+/// This implements the trait for customizing DataFusion.
impl DFExtensionType for TemperatureExtensionType {
+ fn storage_type(&self) -> DataType {
+ self.storage_type.clone()
+ }
+
+ fn serialize_metadata(&self) -> Option<String> {
+ ExtensionType::serialize_metadata(self)
+ }
+
fn create_array_formatter<'fmt>(
&self,
array: &'fmt dyn Array,
options: &FormatOptions<'fmt>,
) -> Result<Option<ArrayFormatter<'fmt>>> {
- if array.data_type() != &DataType::Float64 {
- return internal_err!("Wrong array type for Temperature");
+ match self.storage_type {
+ DataType::Float32 => {
Review Comment:
This is indeed more verbose...if I were writing this today I would probably
just cast `array` to `Float64` and format since that particular cast isn't
expensive in the context of printing stuff, but I think this example is great
as is.
##########
datafusion/core/src/extension_types/array_formatter_factory.rs:
##########
@@ -0,0 +1,67 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow::array::Array;
+use arrow::util::display::{ArrayFormatter, ArrayFormatterFactory,
FormatOptions};
+use arrow_schema::{ArrowError, Field};
+use datafusion_expr::registry::ExtensionTypeRegistryRef;
+
+/// A factory for creating [`ArrayFormatter`]s that checks whether a
registered extension type can
+/// format a given array based on its metadata.
+#[derive(Debug)]
+pub struct DFArrayFormatterFactory {
+ /// The extension type registry
+ registry: ExtensionTypeRegistryRef,
+}
Review Comment:
Sorry for not catching this on the previous pass, but this feels like an odd
place for the formatter factory (printing values happen in lower level crates
quite a lot)...I think this would make more sense next to the registry in
datafusion-expr.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]