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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use std::{
cmp::{Ord, Ordering},
convert::TryFrom,
};
mod bit_chunk;
pub use bit_chunk::{BitChunk, BitChunkIter};
pub mod simd;
use crate::datatypes::{DataType, IntervalUnit, TimeUnit};
pub trait NaturalDataType {
const DATA_TYPE: DataType;
}
pub unsafe trait Relation {
fn is_valid(data_type: &DataType) -> bool;
}
macro_rules! create_relation {
($native_ty:ty, $($impl_pattern:pat)|+) => {
unsafe impl Relation for $native_ty {
#[inline]
fn is_valid(data_type: &DataType) -> bool {
matches!(data_type, $($impl_pattern)|+)
}
}
};
}
macro_rules! natural_type {
($type:ty, $data_type:expr) => {
impl NaturalDataType for $type {
const DATA_TYPE: DataType = $data_type;
}
};
}
pub unsafe trait NativeType:
Relation
+ NaturalDataType
+ Send
+ Sync
+ Sized
+ Copy
+ std::fmt::Debug
+ std::fmt::Display
+ PartialEq
+ Default
+ Sized
+ 'static
{
type Bytes: AsRef<[u8]> + for<'a> TryFrom<&'a [u8]>;
fn to_le_bytes(&self) -> Self::Bytes;
fn to_be_bytes(&self) -> Self::Bytes;
fn from_be_bytes(bytes: Self::Bytes) -> Self;
}
macro_rules! native {
($type:ty) => {
unsafe impl NativeType for $type {
type Bytes = [u8; std::mem::size_of::<Self>()];
#[inline]
fn to_le_bytes(&self) -> Self::Bytes {
Self::to_le_bytes(*self)
}
#[inline]
fn to_be_bytes(&self) -> Self::Bytes {
Self::to_be_bytes(*self)
}
#[inline]
fn from_be_bytes(bytes: Self::Bytes) -> Self {
Self::from_be_bytes(bytes)
}
}
};
}
native!(u8);
native!(u16);
native!(u32);
native!(u64);
native!(i8);
native!(i16);
native!(i32);
native!(i64);
native!(i128);
native!(f32);
native!(f64);
natural_type!(u8, DataType::UInt8);
natural_type!(u16, DataType::UInt16);
natural_type!(u32, DataType::UInt32);
natural_type!(u64, DataType::UInt64);
natural_type!(i8, DataType::Int8);
natural_type!(i16, DataType::Int16);
natural_type!(i32, DataType::Int32);
natural_type!(i64, DataType::Int64);
natural_type!(f32, DataType::Float32);
natural_type!(f64, DataType::Float64);
natural_type!(days_ms, DataType::Interval(IntervalUnit::DayTime));
natural_type!(i128, DataType::Decimal(32, 32));
create_relation!(u8, &DataType::UInt8);
create_relation!(u16, &DataType::UInt16);
create_relation!(u32, &DataType::UInt32);
create_relation!(u64, &DataType::UInt64);
create_relation!(i8, &DataType::Int8);
create_relation!(i16, &DataType::Int16);
create_relation!(
i32,
&DataType::Int32
| &DataType::Date32
| &DataType::Time32(TimeUnit::Millisecond)
| &DataType::Time32(TimeUnit::Second)
| &DataType::Interval(IntervalUnit::YearMonth)
);
create_relation!(
i64,
&DataType::Int64
| &DataType::Date64
| &DataType::Time64(TimeUnit::Microsecond)
| &DataType::Time64(TimeUnit::Nanosecond)
| &DataType::Timestamp(_, _)
| &DataType::Duration(_)
);
create_relation!(i128, &DataType::Decimal(_, _));
create_relation!(f32, &DataType::Float32);
create_relation!(f64, &DataType::Float64);
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash)]
#[allow(non_camel_case_types)]
pub struct days_ms([i32; 2]);
impl std::fmt::Display for days_ms {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}d {}ms", self.days(), self.milliseconds())
}
}
unsafe impl NativeType for days_ms {
type Bytes = [u8; 8];
#[inline]
fn to_le_bytes(&self) -> Self::Bytes {
let days = self.0[0].to_le_bytes();
let ms = self.0[1].to_le_bytes();
let mut result = [0; 8];
result[0] = days[0];
result[1] = days[1];
result[2] = days[2];
result[3] = days[3];
result[4] = ms[0];
result[5] = ms[1];
result[6] = ms[2];
result[7] = ms[3];
result
}
#[inline]
fn to_be_bytes(&self) -> Self::Bytes {
let days = self.0[0].to_be_bytes();
let ms = self.0[1].to_be_bytes();
let mut result = [0; 8];
result[0] = days[0];
result[1] = days[1];
result[2] = days[2];
result[3] = days[3];
result[4] = ms[0];
result[5] = ms[1];
result[6] = ms[2];
result[7] = ms[3];
result
}
#[inline]
fn from_be_bytes(bytes: Self::Bytes) -> Self {
let mut days = [0; 4];
days[0] = bytes[0];
days[1] = bytes[1];
days[2] = bytes[2];
days[3] = bytes[3];
let mut ms = [0; 4];
ms[0] = bytes[4];
ms[1] = bytes[5];
ms[2] = bytes[6];
ms[3] = bytes[7];
Self([i32::from_be_bytes(days), i32::from_be_bytes(ms)])
}
}
create_relation!(days_ms, &DataType::Interval(IntervalUnit::DayTime));
impl days_ms {
#[inline]
pub fn new(days: i32, milliseconds: i32) -> Self {
Self([days, milliseconds])
}
#[inline]
pub fn days(&self) -> i32 {
self.0[0]
}
#[inline]
pub fn milliseconds(&self) -> i32 {
self.0[1]
}
}
impl Ord for days_ms {
fn cmp(&self, other: &Self) -> Ordering {
match self.days().cmp(&other.days()) {
Ordering::Equal => self.milliseconds().cmp(&other.milliseconds()),
other => other,
}
}
}
impl PartialOrd for days_ms {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}