Skip to main content

datafusion_common/types/
extension.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use crate::error::Result;
6use arrow::array::Array;
7use arrow::util::display::{ArrayFormatter, FormatOptions};
8use arrow_schema::DataType;
9use std::borrow::Cow;
10use std::fmt::Debug;
11use std::sync::Arc;
12
13/// A cheaply cloneable pointer to a [`DFExtensionType`].
14pub type DFExtensionTypeRef = Arc<dyn DFExtensionType>;
15
16/// Represents an implementation of a DataFusion extension type, including the storage [`DataType`].
17/// While, in general, an extension type can support several different storage types, a specific
18/// instance of it is always locked into just one exact storage type and metadata pairing.
19///
20/// This trait allows users to customize the behavior of DataFusion for certain types. Having this
21/// ability is necessary because extension types affect how columns should be treated by the query
22/// engine. This effect includes which operations are possible on a column and what are the expected
23/// results from these operations. The extension type mechanism allows users to define how these
24/// operations apply to a particular extension type.
25///
26/// For example, adding two values of [`Int64`](arrow::datatypes::DataType::Int64) is a sensible
27/// thing to do. However, if the same column is annotated with an extension type like `custom.id`,
28/// the correct interpretation of a column changes. Adding together two `custom.id` values, even
29/// though they are stored as integers, may no longer make sense.
30///
31/// Note that DataFusion's extension type support is still young and therefore might not cover all
32/// relevant use cases. Currently, the following operations can be customized:
33/// - Pretty-printing values in record batches
34/// - The type name shown by schema introspection (`DESCRIBE`, `information_schema.columns`)
35///
36/// # Relation to Arrow's [`ExtensionType`](arrow_schema::extension::ExtensionType)
37///
38/// The purpose of Arrow's [`ExtensionType`](arrow_schema::extension::ExtensionType) trait, for the
39/// time being, is to allow reading and writing extension type metadata in a type-safe manner. The
40/// trait does not provide any customization options. Therefore, downstream users (such as
41/// DataFusion) have the flexibility to implement the extension type mechanism according to their
42/// needs. [`DFExtensionType`] is DataFusion's implementation of this extension type mechanism.
43///
44/// Furthermore, the current trait in arrow-rs is not dyn-compatible, which we need for implementing
45/// extension type registries. In the future, the two implementations may increasingly converge.
46///
47/// Another difference is that [`DFExtensionType`] represents a fully resolved extension type that
48/// has a fixed storage type (i.e., [`DataType`]). This is different from arrow-rs, which only
49/// stores the extension type's metadata. For example, an instance of DataFusion's JSON extension
50/// type fixes one of the three possible storage types: [`DataType::Utf8`],
51/// [`DataType::LargeUtf8`], or [`DataType::Utf8View`]. This fixed storaga type is returned in
52/// [`DFExtensionType::storage_type`]. This is not possible in arrow-rs' extension type instances.
53/// This is the reason why we have different types in DataFusion that usually delegate the metadata
54/// processing to the underlying arrow-rs extension type instance
55/// (e.g., [`DFJson`](crate::types::DFJson) instead of [`Json`](arrow_schema::extension::Json)).
56///
57/// # Examples
58///
59/// Examples for using the extension type machinery can be found in the DataFusion examples
60/// directory.
61pub trait DFExtensionType: Debug + Send + Sync {
62    /// Returns the underlying storage type.
63    fn storage_type(&self) -> DataType;
64
65    /// Returns the serialized metadata.
66    fn serialize_metadata(&self) -> Option<String>;
67
68    /// Returns the type's presentation label for schema introspection, such as
69    /// `DESCRIBE`, `SHOW COLUMNS` and `information_schema.columns`.
70    ///
71    /// Names may borrow from this instance or be computed from its metadata.
72    /// The label is not a unique type identifier or SQL syntax guaranteed to round-trip.
73    /// If `None` is returned, the storage type is shown.
74    fn display_name(&self) -> Option<Cow<'_, str>> {
75        None
76    }
77
78    /// Returns an [`ArrayFormatter`] that can format values of this type.
79    ///
80    /// If `Ok(None)` is returned, the default implementation will be used.
81    /// If an error is returned, there was an error creating the formatter.
82    fn create_array_formatter<'fmt>(
83        &self,
84        _array: &'fmt dyn Array,
85        _options: &FormatOptions<'fmt>,
86    ) -> Result<Option<ArrayFormatter<'fmt>>> {
87        Ok(None)
88    }
89}