Skip to content

Commit 861a8b7

Browse files
notfilippojonahgao
andauthored
[logical-types branch] Backport native and logical types (#13016)
* Backport native and logical types from #12853 * Fix clippy error on wasmtest (#12844) --------- Co-authored-by: Jonah Gao <[email protected]>
1 parent 22cb506 commit 861a8b7

File tree

7 files changed

+513
-3
lines changed

7 files changed

+513
-3
lines changed

datafusion/common/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub mod scalar;
4343
pub mod stats;
4444
pub mod test_util;
4545
pub mod tree_node;
46+
pub mod types;
4647
pub mod utils;
4748

4849
/// Reexport arrow crate
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 super::{LogicalType, NativeType};
19+
20+
#[derive(Debug)]
21+
pub struct BuiltinType {
22+
native: NativeType,
23+
}
24+
25+
impl LogicalType for BuiltinType {
26+
fn native(&self) -> &NativeType {
27+
&self.native
28+
}
29+
30+
fn name(&self) -> Option<&str> {
31+
None
32+
}
33+
}
34+
35+
impl From<NativeType> for BuiltinType {
36+
fn from(native: NativeType) -> Self {
37+
Self { native }
38+
}
39+
}
+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}

datafusion/common/src/types/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
mod logical;
19+
mod native;
20+
21+
pub use logical::*;
22+
pub use native::*;

0 commit comments

Comments
 (0)