-
Notifications
You must be signed in to change notification settings - Fork 417
Expand file tree
/
Copy pathget_lit.rs
More file actions
278 lines (248 loc) · 8.21 KB
/
get_lit.rs
File metadata and controls
278 lines (248 loc) · 8.21 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use common_image::Image;
use crate::{
array::ops::image::AsImageObj, datatypes::FileArray, file::DaftMediaType, lit::Literal,
prelude::*,
};
fn map_or_null<T, U, F>(o: Option<T>, f: F) -> Literal
where
F: FnOnce(U) -> Literal,
U: From<T>,
{
match o {
Some(v) => f(v.into()),
None => Literal::Null,
}
}
impl NullArray {
pub fn get_lit(&self, idx: usize) -> Literal {
assert!(
idx < self.len(),
"Out of bounds: {} vs len: {}",
idx,
self.len()
);
Literal::Null
}
}
impl StructArray {
pub fn get_lit(&self, idx: usize) -> Literal {
assert!(
idx < self.len(),
"Out of bounds: {} vs len: {}",
idx,
self.len()
);
if self.is_valid(idx) {
Literal::Struct(
self.children
.iter()
// Below line is commented out because it significantly complicates Series <-> Literal conversion to do this.
// Instead, we'll filter these empty fields out at the Literal to Python object boundary.
// .filter(|child| !child.name().is_empty() && !child.data_type().is_null())
.map(|child| (child.name().to_string(), child.get_lit(idx)))
.collect(),
)
} else {
Literal::Null
}
}
}
impl TensorArray {
pub fn get_lit(&self, idx: usize) -> Literal {
assert!(
idx < self.len(),
"Out of bounds: {} vs len: {}",
idx,
self.len()
);
if self.physical.is_valid(idx)
&& let (Some(data), Some(shape)) =
(self.data_array().get(idx), self.shape_array().get(idx))
{
let shape_array = shape
.u64()
.expect("TensorArray::get_lit: shape array must be UInt64")
.as_arrow()
.expect("TensorArray::get_lit: failed to convert shape to Arrow array");
let shape = shape_array.values().to_vec();
Literal::Tensor { data, shape }
} else {
Literal::Null
}
}
}
impl SparseTensorArray {
pub fn get_lit(&self, idx: usize) -> Literal {
assert!(
idx < self.len(),
"Out of bounds: {} vs len: {}",
idx,
self.len()
);
let indices_offset = match self.data_type() {
DataType::SparseTensor(_, indices_offset) => *indices_offset,
dtype => unreachable!("Unexpected data type for SparseTensorArray: {dtype}"),
};
if self.physical.is_valid(idx)
&& let (Some(values), Some(indices), Some(shape)) = (
self.values_array().get(idx),
self.indices_array().get(idx),
self.shape_array().get(idx),
)
{
let shape_array = shape
.u64()
.expect("SparseTensorArray::get_lit: shape array must be UInt64")
.as_arrow()
.expect("SparseTensorArray::get_lit: failed to convert shape to Arrow array");
let shape = shape_array.values().to_vec();
Literal::SparseTensor {
values,
indices,
shape,
indices_offset,
}
} else {
Literal::Null
}
}
}
impl FixedShapeSparseTensorArray {
pub fn get_lit(&self, idx: usize) -> Literal {
assert!(
idx < self.len(),
"Out of bounds: {} vs len: {}",
idx,
self.len()
);
let (shape, indices_offset) = match self.data_type() {
DataType::FixedShapeSparseTensor(_, shape, indices_offset) => {
(shape.clone(), *indices_offset)
}
dtype => unreachable!("Unexpected data type for FixedShapeSparseTensorArray: {dtype}",),
};
if self.physical.is_valid(idx)
&& let (Some(values), Some(indices)) =
(self.values_array().get(idx), self.indices_array().get(idx))
{
Literal::SparseTensor {
values,
indices,
shape,
indices_offset,
}
} else {
Literal::Null
}
}
}
impl MapArray {
pub fn get_lit(&self, idx: usize) -> Literal {
assert!(
idx < self.len(),
"Out of bounds: {} vs len: {}",
idx,
self.len()
);
map_or_null(self.get(idx), |entry: Series| {
let entry = entry.struct_().unwrap();
let keys = entry.get("key").unwrap();
let values = entry.get("value").unwrap();
Literal::Map { keys, values }
})
}
}
impl ExtensionArray {
pub fn get_lit(&self, idx: usize) -> Literal {
assert!(
idx < self.len(),
"Out of bounds: {} vs len: {}",
idx,
self.len()
);
if self.physical.is_valid(idx) {
Literal::Extension(self.slice(idx, idx + 1).unwrap().into_series())
} else {
Literal::Null
}
}
}
macro_rules! impl_array_get_lit {
($type:ty, $variant:ident) => {
impl $type {
pub fn get_lit(&self, idx: usize) -> Literal {
// don't need to do assertions here because it also happens in `self.get`
map_or_null(self.get(idx), Literal::$variant)
}
}
};
($type:ty, $dtype:pat => $mapper:expr) => {
impl $type {
pub fn get_lit(&self, idx: usize) -> Literal {
assert!(
idx < self.len(),
"Out of bounds: {} vs len: {}",
idx,
self.len()
);
match self.data_type() {
$dtype => map_or_null(self.get(idx), $mapper),
other => {
unreachable!("Unexpected data type for {}: {}", stringify!($type), other)
}
}
}
}
};
}
macro_rules! impl_image_array_get_lit {
($type:ty) => {
impl $type {
pub fn get_lit(&self, idx: usize) -> Literal {
assert!(
idx < self.len(),
"Out of bounds: {} vs len: {}",
idx,
self.len()
);
map_or_null(self.as_image_obj(idx), |obj| Literal::Image(Image(obj)))
}
}
};
}
impl_array_get_lit!(BooleanArray, Boolean);
impl_array_get_lit!(BinaryArray, Binary);
impl_array_get_lit!(FixedSizeBinaryArray, Binary);
impl_array_get_lit!(Int8Array, Int8);
impl_array_get_lit!(Int16Array, Int16);
impl_array_get_lit!(Int32Array, Int32);
impl_array_get_lit!(Int64Array, Int64);
impl_array_get_lit!(UInt8Array, UInt8);
impl_array_get_lit!(UInt16Array, UInt16);
impl_array_get_lit!(UInt32Array, UInt32);
impl_array_get_lit!(UInt64Array, UInt64);
impl_array_get_lit!(Float32Array, Float32);
impl_array_get_lit!(Float64Array, Float64);
impl_array_get_lit!(Utf8Array, Utf8);
impl_array_get_lit!(IntervalArray, Interval);
impl_array_get_lit!(DateArray, Date);
impl_array_get_lit!(ListArray, List);
impl_array_get_lit!(FixedSizeListArray, List);
impl_array_get_lit!(EmbeddingArray, Embedding);
impl<T> FileArray<T>
where
T: DaftMediaType,
{
pub fn get_lit(&self, idx: usize) -> Literal {
map_or_null(self.get(idx), Literal::File)
}
}
#[cfg(feature = "python")]
impl_array_get_lit!(PythonArray, Python);
impl_array_get_lit!(Decimal128Array, DataType::Decimal128(precision, scale) => |v| Literal::Decimal(v, *precision as _, *scale as _));
impl_array_get_lit!(TimestampArray, DataType::Timestamp(tu, tz) => |v| Literal::Timestamp(v, *tu, tz.clone()));
impl_array_get_lit!(TimeArray, DataType::Time(tu) => |v| Literal::Time(v, *tu));
impl_array_get_lit!(DurationArray, DataType::Duration(tu) => |v| Literal::Duration(v, *tu));
impl_array_get_lit!(FixedShapeTensorArray, DataType::FixedShapeTensor(_, shape) => |data| Literal::Tensor { data, shape: shape.clone() });
impl_image_array_get_lit!(ImageArray);
impl_image_array_get_lit!(FixedShapeImageArray);