|
| 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 core::fmt; |
| 19 | +use std::{cmp::Ordering, hash::Hash, sync::Arc}; |
| 20 | + |
| 21 | +use super::NativeType; |
| 22 | + |
| 23 | +/// Signature that uniquely identifies a type among other types. |
| 24 | +#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 25 | +pub enum TypeSignature<'a> { |
| 26 | + /// Represents a built-in native type. |
| 27 | + Native(&'a NativeType), |
| 28 | + /// Represents an arrow-compatible extension type. |
| 29 | + /// (<https://arrow.apache.org/docs/format/Columnar.html#extension-types>) |
| 30 | + /// |
| 31 | + /// The `name` should contain the same value as 'ARROW:extension:name'. |
| 32 | + Extension { |
| 33 | + name: &'a str, |
| 34 | + parameters: &'a [TypeParameter<'a>], |
| 35 | + }, |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 39 | +pub enum TypeParameter<'a> { |
| 40 | + Type(TypeSignature<'a>), |
| 41 | + Number(i128), |
| 42 | +} |
| 43 | + |
| 44 | +/// A reference counted [`LogicalType`]. |
| 45 | +pub type LogicalTypeRef = Arc<dyn LogicalType>; |
| 46 | + |
| 47 | +/// Representation of a logical type with its signature and its native backing |
| 48 | +/// type. |
| 49 | +/// |
| 50 | +/// The logical type is meant to be used during the DataFusion logical planning |
| 51 | +/// phase in order to reason about logical types without worrying about their |
| 52 | +/// underlying physical implementation. |
| 53 | +/// |
| 54 | +/// ### Extension types |
| 55 | +/// |
| 56 | +/// [`LogicalType`] is a trait in order to allow the possibility of declaring |
| 57 | +/// extension types: |
| 58 | +/// |
| 59 | +/// ``` |
| 60 | +/// use datafusion_common::types::{LogicalType, NativeType, TypeSignature}; |
| 61 | +/// |
| 62 | +/// struct JSON {} |
| 63 | +/// |
| 64 | +/// impl LogicalType for JSON { |
| 65 | +/// fn native(&self) -> &NativeType { |
| 66 | +/// &NativeType::Utf8 |
| 67 | +/// } |
| 68 | +/// |
| 69 | +/// fn signature(&self) -> TypeSignature<'_> { |
| 70 | +/// TypeSignature::Extension { |
| 71 | +/// name: "JSON", |
| 72 | +/// parameters: &[], |
| 73 | +/// } |
| 74 | +/// } |
| 75 | +/// } |
| 76 | +/// ``` |
| 77 | +pub trait LogicalType: Sync + Send { |
| 78 | + fn native(&self) -> &NativeType; |
| 79 | + fn signature(&self) -> TypeSignature<'_>; |
| 80 | +} |
| 81 | + |
| 82 | +impl fmt::Debug for dyn LogicalType { |
| 83 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 84 | + f.debug_tuple("LogicalType") |
| 85 | + .field(&self.signature()) |
| 86 | + .field(&self.native()) |
| 87 | + .finish() |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl PartialEq for dyn LogicalType { |
| 92 | + fn eq(&self, other: &Self) -> bool { |
| 93 | + self.native().eq(other.native()) && self.signature().eq(&other.signature()) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl Eq for dyn LogicalType {} |
| 98 | + |
| 99 | +impl PartialOrd for dyn LogicalType { |
| 100 | + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 101 | + Some(self.cmp(other)) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl Ord for dyn LogicalType { |
| 106 | + fn cmp(&self, other: &Self) -> Ordering { |
| 107 | + self.signature() |
| 108 | + .cmp(&other.signature()) |
| 109 | + .then(self.native().cmp(other.native())) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl Hash for dyn LogicalType { |
| 114 | + fn hash<H: std::hash::Hasher>(&self, state: &mut H) { |
| 115 | + self.signature().hash(state); |
| 116 | + self.native().hash(state); |
| 117 | + } |
| 118 | +} |
0 commit comments