|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use crate::types::{ |
| 19 | + LogicalType, NativeType, TypeParameter, TypeSignature, ValuePrettyPrinter, |
| 20 | +}; |
| 21 | +use crate::Result; |
| 22 | +use crate::ScalarValue; |
| 23 | +use std::sync::{Arc, LazyLock}; |
| 24 | + |
| 25 | +/// Represents the canonical [UUID extension type](https://arrow.apache.org/docs/format/CanonicalExtensions.html#uuid). |
| 26 | +pub struct UuidType {} |
| 27 | + |
| 28 | +impl UuidType { |
| 29 | + /// Creates a new [UuidType]. |
| 30 | + pub fn new() -> Self { |
| 31 | + Self {} |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl Default for UuidType { |
| 36 | + fn default() -> Self { |
| 37 | + Self::new() |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl LogicalType for UuidType { |
| 42 | + fn native(&self) -> &NativeType { |
| 43 | + &NativeType::FixedSizeBinary(16) |
| 44 | + } |
| 45 | + |
| 46 | + fn signature(&self) -> TypeSignature<'_> { |
| 47 | + TypeSignature::Extension { |
| 48 | + name: "arrow.uuid", |
| 49 | + parameters: vec![], |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + fn pretty_printer(&self) -> &Arc<dyn ValuePrettyPrinter> { |
| 54 | + static PRETTY_PRINTER: LazyLock<Arc<dyn ValuePrettyPrinter>> = |
| 55 | + LazyLock::new(|| Arc::new(UuidValuePrettyPrinter {})); |
| 56 | + &PRETTY_PRINTER |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] |
| 61 | +struct UuidValuePrettyPrinter; |
| 62 | + |
| 63 | +impl ValuePrettyPrinter for UuidValuePrettyPrinter { |
| 64 | + fn pretty_print_scalar(&self, value: &ScalarValue) -> Result<String> { |
| 65 | + Ok(format!("arrow.uuid({})", value)) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// Represents the canonical [Opaque extension type](https://arrow.apache.org/docs/format/CanonicalExtensions.html#opaque). |
| 70 | +/// |
| 71 | +/// In the context of DataFusion, a common use case of the opaque type is when an extension type |
| 72 | +/// is unknown to DataFusion. Contrary to [UnresolvedExtensionType], the extension type has |
| 73 | +/// already been checked against the extension type registry and was not found. |
| 74 | +pub struct OpaqueType { |
| 75 | + /// The underlying native type. |
| 76 | + native_type: NativeType, |
| 77 | +} |
| 78 | + |
| 79 | +impl OpaqueType { |
| 80 | + /// Creates a new [OpaqueType]. |
| 81 | + pub fn new(native_type: NativeType) -> Self { |
| 82 | + Self { native_type } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl LogicalType for OpaqueType { |
| 87 | + fn native(&self) -> &NativeType { |
| 88 | + &NativeType::FixedSizeBinary(16) |
| 89 | + } |
| 90 | + |
| 91 | + fn signature(&self) -> TypeSignature<'_> { |
| 92 | + let parameter = TypeParameter::Type(TypeSignature::Native(&self.native_type)); |
| 93 | + TypeSignature::Extension { |
| 94 | + name: "arrow.opaque", |
| 95 | + parameters: vec![parameter], |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + fn pretty_printer(&self) -> &Arc<dyn ValuePrettyPrinter> { |
| 100 | + static PRETTY_PRINTER: LazyLock<Arc<dyn ValuePrettyPrinter>> = |
| 101 | + LazyLock::new(|| Arc::new(OpaqueValuePrettyPrinter {})); |
| 102 | + &PRETTY_PRINTER |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] |
| 107 | +struct OpaqueValuePrettyPrinter; |
| 108 | + |
| 109 | +impl ValuePrettyPrinter for OpaqueValuePrettyPrinter { |
| 110 | + fn pretty_print_scalar(&self, value: &ScalarValue) -> Result<String> { |
| 111 | + Ok(format!("arrow.opaque({})", value)) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/// Represents an unresolved extension type with a given native type and name. |
| 116 | +/// |
| 117 | +/// This does not necessarily indicate that DataFusion does not understand the extension type. For |
| 118 | +/// this purpose, see [OpaqueType]. However, it does indicate that the extension type was not yet |
| 119 | +/// checked against the extension type registry. |
| 120 | +/// |
| 121 | +/// This extension type exists because it is often challenging to gain access to an extension type |
| 122 | +/// registry. Especially because extension type support is relatively new, and therefore this |
| 123 | +/// consideration was not taken into account by users. This provides a workaround such that |
| 124 | +/// unresolved extension types can be resolved at a later point in time where access to the registry |
| 125 | +/// is available. |
| 126 | +pub struct UnresolvedExtensionType { |
| 127 | + /// The name of the underlying extension type. |
| 128 | + name: String, |
| 129 | + /// The metadata of the underlying extension type. |
| 130 | + metadata: Option<String>, |
| 131 | + /// The underlying native type. |
| 132 | + native_type: NativeType, |
| 133 | +} |
| 134 | + |
| 135 | +impl UnresolvedExtensionType { |
| 136 | + /// Creates a new [UnresolvedExtensionType]. |
| 137 | + pub fn new(name: String, metadata: Option<String>, native_type: NativeType) -> Self { |
| 138 | + Self { |
| 139 | + name, |
| 140 | + metadata, |
| 141 | + native_type, |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /// The name of the unresolved extension type. |
| 146 | + pub fn name(&self) -> &str { |
| 147 | + &self.name |
| 148 | + } |
| 149 | + |
| 150 | + /// The metadata of the unresolved extension type. |
| 151 | + pub fn metadata(&self) -> Option<&str> { |
| 152 | + self.metadata.as_deref() |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +impl LogicalType for UnresolvedExtensionType { |
| 157 | + fn native(&self) -> &NativeType { |
| 158 | + &self.native_type |
| 159 | + } |
| 160 | + |
| 161 | + fn signature(&self) -> TypeSignature<'_> { |
| 162 | + TypeSignature::Extension { |
| 163 | + name: &"datafusion.unresolved", |
| 164 | + parameters: vec![], |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + fn pretty_printer(&self) -> &Arc<dyn ValuePrettyPrinter> { |
| 169 | + static PRETTY_PRINTER: LazyLock<Arc<dyn ValuePrettyPrinter>> = |
| 170 | + LazyLock::new(|| Arc::new(UnresolvedValuePrettyPrinter {})); |
| 171 | + &PRETTY_PRINTER |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] |
| 176 | +struct UnresolvedValuePrettyPrinter {} |
| 177 | + |
| 178 | +impl ValuePrettyPrinter for UnresolvedValuePrettyPrinter { |
| 179 | + fn pretty_print_scalar(&self, value: &ScalarValue) -> Result<String> { |
| 180 | + Ok(format!("datafusion.unresolved({})", value)) |
| 181 | + } |
| 182 | +} |
0 commit comments