-
Notifications
You must be signed in to change notification settings - Fork 753
/
data_schema.rs
205 lines (175 loc) · 5.69 KB
/
data_schema.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2021 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use core::fmt;
use std::collections::BTreeMap;
use std::sync::Arc;
use common_arrow::arrow::datatypes::Schema as ArrowSchema;
use common_arrow::arrow::datatypes::SchemaRef as ArrowSchemaRef;
use common_exception::ErrorCode;
use common_exception::Result;
use crate::DataField;
/// memory layout.
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct DataSchema {
pub(crate) fields: Vec<DataField>,
pub(crate) metadata: BTreeMap<String, String>,
}
impl DataSchema {
pub fn empty() -> Self {
Self {
fields: vec![],
metadata: BTreeMap::new(),
}
}
pub fn new(fields: Vec<DataField>) -> Self {
Self {
fields,
metadata: BTreeMap::new(),
}
}
pub fn new_from(fields: Vec<DataField>, metadata: BTreeMap<String, String>) -> Self {
Self { fields, metadata }
}
/// Returns an immutable reference of the vector of `Field` instances.
#[inline]
pub const fn fields(&self) -> &Vec<DataField> {
&self.fields
}
#[inline]
pub fn num_fields(&self) -> usize {
self.fields.len()
}
#[inline]
pub fn has_field(&self, name: &str) -> bool {
for i in 0..self.fields.len() {
if self.fields[i].name() == name {
return true;
}
}
false
}
pub fn fields_map(&self) -> BTreeMap<usize, DataField> {
let x = self.fields().iter().cloned().enumerate();
x.collect::<BTreeMap<_, _>>()
}
/// Returns an immutable reference of a specific `Field` instance selected using an
/// offset within the internal `fields` vector.
pub fn field(&self, i: usize) -> &DataField {
&self.fields[i]
}
/// Returns an immutable reference of a specific `Field` instance selected by name.
pub fn field_with_name(&self, name: &str) -> Result<&DataField> {
Ok(&self.fields[self.index_of(name)?])
}
/// Returns an immutable reference to field `metadata`.
#[inline]
pub const fn meta(&self) -> &BTreeMap<String, String> {
&self.metadata
}
/// Find the index of the column with the given name.
pub fn index_of(&self, name: &str) -> Result<usize> {
for i in 0..self.fields.len() {
if self.fields[i].name() == name {
return Ok(i);
}
}
let valid_fields: Vec<String> = self.fields.iter().map(|f| f.name().clone()).collect();
Err(ErrorCode::BadArguments(format!(
"Unable to get field named \"{}\". Valid fields: {:?}",
name, valid_fields
)))
}
/// Look up a column by name and return a immutable reference to the column along with
/// its index.
pub fn column_with_name(&self, name: &str) -> Option<(usize, &DataField)> {
self.fields
.iter()
.enumerate()
.find(|&(_, c)| c.name() == name)
}
/// Check to see if `self` is a superset of `other` schema. Here are the comparision rules:
pub fn contains(&self, other: &DataSchema) -> bool {
if self.fields.len() != other.fields.len() {
return false;
}
for (i, field) in other.fields.iter().enumerate() {
if !self.fields[i].contains(field) {
return false;
}
}
true
}
/// project will do column pruning.
#[must_use]
pub fn project(&self, projection: Vec<usize>) -> Self {
let fields = projection
.iter()
.map(|idx| self.fields()[*idx].clone())
.collect();
Self::new_from(fields, self.meta().clone())
}
/// project will do column pruning.
#[must_use]
pub fn project_by_fields(&self, fields: Vec<DataField>) -> Self {
Self::new_from(fields, self.meta().clone())
}
pub fn to_arrow(&self) -> ArrowSchema {
let fields = self
.fields()
.iter()
.map(|f| f.to_arrow())
.collect::<Vec<_>>();
ArrowSchema::from(fields).with_metadata(self.metadata.clone())
}
}
pub type DataSchemaRef = Arc<DataSchema>;
pub struct DataSchemaRefExt;
impl DataSchemaRefExt {
pub fn create(fields: Vec<DataField>) -> DataSchemaRef {
Arc::new(DataSchema::new(fields))
}
}
impl From<&ArrowSchema> for DataSchema {
fn from(a_schema: &ArrowSchema) -> Self {
let fields = a_schema
.fields
.iter()
.map(|arrow_f| arrow_f.into())
.collect::<Vec<_>>();
DataSchema::new(fields)
}
}
#[allow(clippy::needless_borrow)]
impl From<ArrowSchema> for DataSchema {
fn from(a_schema: ArrowSchema) -> Self {
(&a_schema).into()
}
}
impl From<ArrowSchemaRef> for DataSchema {
fn from(a_schema: ArrowSchemaRef) -> Self {
(a_schema.as_ref()).into()
}
}
impl fmt::Display for DataSchema {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(
&self
.fields
.iter()
.map(|c| c.to_string())
.collect::<Vec<String>>()
.join(", "),
)
}
}