Skip to main content

datafusion_common/types/
field.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 arrow::datatypes::{Field, Fields, UnionFields};
6use std::hash::{Hash, Hasher};
7use std::{ops::Deref, sync::Arc};
8
9use super::{LogicalTypeRef, NativeType};
10
11/// A record of a logical type, its name and its nullability.
12#[derive(Debug, Clone, Eq, PartialOrd, Ord)]
13pub struct LogicalField {
14    pub name: String,
15    pub logical_type: LogicalTypeRef,
16    pub nullable: bool,
17}
18
19impl PartialEq for LogicalField {
20    fn eq(&self, other: &Self) -> bool {
21        self.name == other.name
22            && self.logical_type.eq(&other.logical_type)
23            && self.nullable == other.nullable
24    }
25}
26
27impl Hash for LogicalField {
28    fn hash<H: Hasher>(&self, state: &mut H) {
29        self.name.hash(state);
30        self.logical_type.hash(state);
31        self.nullable.hash(state);
32    }
33}
34
35impl From<&Field> for LogicalField {
36    fn from(value: &Field) -> Self {
37        Self {
38            name: value.name().clone(),
39            logical_type: Arc::new(NativeType::from(value.data_type().clone())),
40            nullable: value.is_nullable(),
41        }
42    }
43}
44
45/// A reference counted [`LogicalField`].
46pub type LogicalFieldRef = Arc<LogicalField>;
47
48/// A cheaply cloneable, owned collection of [`LogicalFieldRef`].
49#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
50pub struct LogicalFields(Arc<[LogicalFieldRef]>);
51
52impl Deref for LogicalFields {
53    type Target = [LogicalFieldRef];
54
55    fn deref(&self) -> &Self::Target {
56        self.0.as_ref()
57    }
58}
59
60impl From<&Fields> for LogicalFields {
61    fn from(value: &Fields) -> Self {
62        value
63            .iter()
64            .map(|field| Arc::new(LogicalField::from(field.as_ref())))
65            .collect()
66    }
67}
68
69impl FromIterator<LogicalFieldRef> for LogicalFields {
70    fn from_iter<T: IntoIterator<Item = LogicalFieldRef>>(iter: T) -> Self {
71        Self(iter.into_iter().collect())
72    }
73}
74
75/// A cheaply cloneable, owned collection of [`LogicalFieldRef`] and their
76/// corresponding type ids.
77#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
78pub struct LogicalUnionFields(Arc<[(i8, LogicalFieldRef)]>);
79
80impl Deref for LogicalUnionFields {
81    type Target = [(i8, LogicalFieldRef)];
82
83    fn deref(&self) -> &Self::Target {
84        self.0.as_ref()
85    }
86}
87
88impl From<&UnionFields> for LogicalUnionFields {
89    fn from(value: &UnionFields) -> Self {
90        value
91            .iter()
92            .map(|(i, field)| (i, Arc::new(LogicalField::from(field.as_ref()))))
93            .collect()
94    }
95}
96
97impl FromIterator<(i8, LogicalFieldRef)> for LogicalUnionFields {
98    fn from_iter<T: IntoIterator<Item = (i8, LogicalFieldRef)>>(iter: T) -> Self {
99        Self(iter.into_iter().collect())
100    }
101}