Skip to content

Commit 7ed7891

Browse files
committed
From NativeField to LogicalField
1 parent 6150ea9 commit 7ed7891

File tree

4 files changed

+130
-103
lines changed

4 files changed

+130
-103
lines changed

datafusion/common/src/types/field.rs

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

datafusion/common/src/types/logical.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub trait LogicalType: Sync + Send {
8080
}
8181

8282
impl fmt::Debug for dyn LogicalType {
83-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8484
f.debug_tuple("LogicalType")
8585
.field(&self.signature())
8686
.field(&self.native())

datafusion/common/src/types/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
mod field;
1819
mod logical;
1920
mod native;
2021

22+
pub use field::*;
2123
pub use logical::*;
2224
pub use native::*;

datafusion/common/src/types/native.rs

Lines changed: 13 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -15,61 +15,13 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use std::{ops::Deref, sync::Arc};
18+
use std::sync::Arc;
1919

20-
use arrow_schema::{DataType, Field, Fields, IntervalUnit, TimeUnit, UnionFields};
20+
use arrow_schema::{DataType, IntervalUnit, TimeUnit};
2121

22-
use super::{LogicalType, TypeSignature};
23-
24-
/// A record of a native type, its name and its nullability.
25-
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
26-
pub struct NativeField {
27-
name: String,
28-
native_type: NativeType,
29-
nullable: bool,
30-
}
31-
32-
impl NativeField {
33-
pub fn name(&self) -> &str {
34-
&self.name
35-
}
36-
37-
pub fn native_type(&self) -> &NativeType {
38-
&self.native_type
39-
}
40-
41-
pub fn nullable(&self) -> bool {
42-
self.nullable
43-
}
44-
}
45-
46-
/// A reference counted [`NativeField`].
47-
pub type NativeFieldRef = Arc<NativeField>;
48-
49-
/// A cheaply cloneable, owned collection of [`NativeFieldRef`].
50-
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
51-
pub struct NativeFields(Arc<[NativeFieldRef]>);
52-
53-
impl Deref for NativeFields {
54-
type Target = [NativeFieldRef];
55-
56-
fn deref(&self) -> &Self::Target {
57-
self.0.as_ref()
58-
}
59-
}
60-
61-
/// A cheaply cloneable, owned collection of [`NativeFieldRef`] and their
62-
/// corresponding type ids.
63-
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
64-
pub struct NativeUnionFields(Arc<[(i8, NativeFieldRef)]>);
65-
66-
impl Deref for NativeUnionFields {
67-
type Target = [(i8, NativeFieldRef)];
68-
69-
fn deref(&self) -> &Self::Target {
70-
self.0.as_ref()
71-
}
72-
}
22+
use super::{
23+
LogicalFieldRef, LogicalFields, LogicalType, LogicalUnionFields, TypeSignature,
24+
};
7325

7426
/// Representation of a type that DataFusion can handle natively. It is a subset
7527
/// of the physical variants in Arrow's native [`DataType`].
@@ -196,13 +148,13 @@ pub enum NativeType {
196148
/// A variable-length string in Unicode with UTF-8 encoding.
197149
Utf8,
198150
/// A list of some logical data type with variable length.
199-
List(NativeFieldRef),
151+
List(LogicalFieldRef),
200152
/// A list of some logical data type with fixed length.
201-
FixedSizeList(NativeFieldRef, i32),
153+
FixedSizeList(LogicalFieldRef, i32),
202154
/// A nested type that contains a number of sub-fields.
203-
Struct(NativeFields),
155+
Struct(LogicalFields),
204156
/// A nested type that can represent slots of differing types.
205-
Union(NativeUnionFields),
157+
Union(LogicalUnionFields),
206158
/// Decimal value with precision and scale
207159
///
208160
/// * precision is the total number of digits
@@ -222,11 +174,10 @@ pub enum NativeType {
222174
/// The key and value types are not constrained, but keys should be
223175
/// hashable and unique.
224176
///
225-
/// In a field with Map type, the field has a child Struct field, which then
226-
/// has two children: key type and the second the value type. The names of the
177+
/// In a field with Map type, key type and the second the value type. The names of the
227178
/// child fields may be respectively "entries", "key", and "value", but this is
228179
/// not enforced.
229-
Map(NativeFieldRef),
180+
Map(LogicalFieldRef),
230181
}
231182

232183
impl LogicalType for NativeType {
@@ -275,9 +226,9 @@ impl From<DataType> for NativeType {
275226
DataType::FixedSizeList(field, size) => {
276227
FixedSizeList(Arc::new(field.as_ref().into()), size)
277228
}
278-
DataType::Struct(fields) => Struct(NativeFields::from(&fields)),
229+
DataType::Struct(fields) => Struct(LogicalFields::from(&fields)),
279230
DataType::Union(union_fields, _) => {
280-
Union(NativeUnionFields::from(&union_fields))
231+
Union(LogicalUnionFields::from(&union_fields))
281232
}
282233
DataType::Dictionary(_, data_type) => data_type.as_ref().clone().into(),
283234
DataType::Decimal128(p, s) | DataType::Decimal256(p, s) => Decimal(p, s),
@@ -286,43 +237,3 @@ impl From<DataType> for NativeType {
286237
}
287238
}
288239
}
289-
290-
impl From<&Field> for NativeField {
291-
fn from(value: &Field) -> Self {
292-
Self {
293-
name: value.name().clone(),
294-
native_type: value.data_type().clone().into(),
295-
nullable: value.is_nullable(),
296-
}
297-
}
298-
}
299-
300-
impl From<&Fields> for NativeFields {
301-
fn from(value: &Fields) -> Self {
302-
value
303-
.iter()
304-
.map(|field| Arc::new(NativeField::from(field.as_ref())))
305-
.collect()
306-
}
307-
}
308-
309-
impl FromIterator<NativeFieldRef> for NativeFields {
310-
fn from_iter<T: IntoIterator<Item = NativeFieldRef>>(iter: T) -> Self {
311-
Self(iter.into_iter().collect())
312-
}
313-
}
314-
315-
impl From<&UnionFields> for NativeUnionFields {
316-
fn from(value: &UnionFields) -> Self {
317-
value
318-
.iter()
319-
.map(|(i, field)| (i, Arc::new(NativeField::from(field.as_ref()))))
320-
.collect()
321-
}
322-
}
323-
324-
impl FromIterator<(i8, NativeFieldRef)> for NativeUnionFields {
325-
fn from_iter<T: IntoIterator<Item = (i8, NativeFieldRef)>>(iter: T) -> Self {
326-
Self(iter.into_iter().collect())
327-
}
328-
}

0 commit comments

Comments
 (0)