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
use std::error::Error;
use std::fmt;
use std::mem;
use serde::{Deserialize, Serialize};
use crate::row::DatumList;
use std::cmp::Ordering;
use lowertest::MzReflect;
pub const MAX_ARRAY_DIMENSIONS: u8 = 6;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Array<'a> {
pub(crate) elements: DatumList<'a>,
pub(crate) dims: ArrayDimensions<'a>,
}
impl<'a> Array<'a> {
pub fn dims(&self) -> ArrayDimensions<'a> {
self.dims
}
pub fn elements(&self) -> DatumList<'a> {
self.elements
}
}
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
pub struct ArrayDimensions<'a> {
pub(crate) data: &'a [u8],
}
impl ArrayDimensions<'_> {
pub fn ndims(&self) -> u8 {
let ndims = self.data.len() / (mem::size_of::<usize>() * 2);
ndims.try_into().expect("ndims is known to fit in a u8")
}
pub fn len(&self) -> usize {
self.ndims().into()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl Ord for ArrayDimensions<'_> {
fn cmp(&self, other: &Self) -> Ordering {
self.ndims().cmp(&other.ndims())
}
}
impl PartialOrd for ArrayDimensions<'_> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl fmt::Debug for ArrayDimensions<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.into_iter()).finish()
}
}
impl<'a> IntoIterator for ArrayDimensions<'a> {
type Item = ArrayDimension;
type IntoIter = ArrayDimensionsIter<'a>;
fn into_iter(self) -> ArrayDimensionsIter<'a> {
ArrayDimensionsIter { data: self.data }
}
}
#[derive(Debug)]
pub struct ArrayDimensionsIter<'a> {
data: &'a [u8],
}
impl Iterator for ArrayDimensionsIter<'_> {
type Item = ArrayDimension;
fn next(&mut self) -> Option<ArrayDimension> {
if self.data.is_empty() {
None
} else {
let sz = mem::size_of::<usize>();
let lower_bound = usize::from_ne_bytes(self.data[..sz].try_into().unwrap());
self.data = &self.data[sz..];
let length = usize::from_ne_bytes(self.data[..sz].try_into().unwrap());
self.data = &self.data[sz..];
Some(ArrayDimension {
lower_bound,
length,
})
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct ArrayDimension {
pub lower_bound: usize,
pub length: usize,
}
#[derive(
Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize, MzReflect,
)]
pub enum InvalidArrayError {
TooManyDimensions(usize),
WrongCardinality { actual: usize, expected: usize },
}
impl fmt::Display for InvalidArrayError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
InvalidArrayError::TooManyDimensions(n) => write!(
f,
"number of array dimensions ({}) exceeds the maximum allowed ({})",
n, MAX_ARRAY_DIMENSIONS
),
InvalidArrayError::WrongCardinality { actual, expected } => write!(
f,
"number of array elements ({}) does not match declared cardinality ({})",
actual, expected
),
}
}
}
impl Error for InvalidArrayError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
None
}
}