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
use parquet2::{
encoding::{hybrid_rle, Encoding},
page::{DataPage, PrimitivePageDict},
types::NativeType,
};
use super::super::utils as other_utils;
use super::utils::ExactChunksIter;
use super::ColumnDescriptor;
use crate::{
bitmap::{utils::BitmapIter, MutableBitmap},
buffer::MutableBuffer,
error::Result,
types::NativeType as ArrowNativeType,
};
fn read_dict_buffer_optional<T, A, F>(
validity_buffer: &[u8],
indices_buffer: &[u8],
additional: usize,
dict: &PrimitivePageDict<T>,
values: &mut MutableBuffer<A>,
validity: &mut MutableBitmap,
op: F,
) where
T: NativeType,
A: ArrowNativeType,
F: Fn(T) -> A,
{
let length = additional + values.len();
let dict_values = dict.values();
let bit_width = indices_buffer[0];
let indices_buffer = &indices_buffer[1..];
let mut indices =
hybrid_rle::HybridRleDecoder::new(indices_buffer, bit_width as u32, additional);
let validity_iterator = hybrid_rle::Decoder::new(validity_buffer, 1);
for run in validity_iterator {
match run {
hybrid_rle::HybridEncoded::Bitpacked(packed) => {
let remaining = length - values.len();
let len = std::cmp::min(packed.len() * 8, remaining);
for is_valid in BitmapIter::new(packed, 0, len) {
let value = if is_valid {
op(dict_values[indices.next().unwrap() as usize])
} else {
A::default()
};
values.push(value);
}
validity.extend_from_slice(packed, 0, len);
}
hybrid_rle::HybridEncoded::Rle(value, additional) => {
let is_set = value[0] == 1;
validity.extend_constant(additional, is_set);
if is_set {
(0..additional).for_each(|_| {
let index = indices.next().unwrap() as usize;
let value = op(dict_values[index]);
values.push(value)
})
} else {
values.extend_constant(additional, A::default())
}
}
}
}
}
fn read_dict_buffer_required<T, A, F>(
indices_buffer: &[u8],
additional: usize,
dict: &PrimitivePageDict<T>,
values: &mut MutableBuffer<A>,
validity: &mut MutableBitmap,
op: F,
) where
T: NativeType,
A: ArrowNativeType,
F: Fn(T) -> A,
{
let dict_values = dict.values();
let bit_width = indices_buffer[0];
let indices_buffer = &indices_buffer[1..];
let indices = hybrid_rle::HybridRleDecoder::new(indices_buffer, bit_width as u32, additional);
values.extend(indices.map(|index| op(dict_values[index as usize])));
validity.extend_constant(additional, true);
}
fn read_nullable<T, A, F>(
validity_buffer: &[u8],
values_buffer: &[u8],
additional: usize,
values: &mut MutableBuffer<A>,
validity: &mut MutableBitmap,
op: F,
) where
T: NativeType,
A: ArrowNativeType,
F: Fn(T) -> A,
{
let length = additional + values.len();
let mut chunks = ExactChunksIter::<T>::new(values_buffer);
let validity_iterator = hybrid_rle::Decoder::new(validity_buffer, 1);
for run in validity_iterator {
match run {
hybrid_rle::HybridEncoded::Bitpacked(packed) => {
let remaining = length - values.len();
let len = std::cmp::min(packed.len() * 8, remaining);
for is_valid in BitmapIter::new(packed, 0, len) {
let value = if is_valid {
op(chunks.next().unwrap())
} else {
A::default()
};
values.push(value);
}
validity.extend_from_slice(packed, 0, len);
}
hybrid_rle::HybridEncoded::Rle(value, additional) => {
let is_set = value[0] == 1;
validity.extend_constant(additional, is_set);
if is_set {
(0..additional).for_each(|_| {
let value = op(chunks.next().unwrap());
values.push(value)
})
} else {
values.extend_constant(additional, A::default())
}
}
}
}
}
fn read_required<T, A, F>(
values_buffer: &[u8],
additional: usize,
values: &mut MutableBuffer<A>,
op: F,
) where
T: NativeType,
A: ArrowNativeType,
F: Fn(T) -> A,
{
assert_eq!(values_buffer.len(), additional * std::mem::size_of::<T>());
let iterator = ExactChunksIter::<T>::new(values_buffer);
let iterator = iterator.map(op);
values.extend_from_trusted_len_iter(iterator);
}
pub fn extend_from_page<T, A, F>(
page: &DataPage,
descriptor: &ColumnDescriptor,
values: &mut MutableBuffer<A>,
validity: &mut MutableBitmap,
op: F,
) -> Result<()>
where
T: NativeType,
A: ArrowNativeType,
F: Fn(T) -> A,
{
let additional = page.num_values();
assert_eq!(descriptor.max_rep_level(), 0);
let is_optional = descriptor.max_def_level() == 1;
let (_, validity_buffer, values_buffer, version) = other_utils::split_buffer(page, descriptor);
match (&page.encoding(), page.dictionary_page(), is_optional) {
(Encoding::PlainDictionary | Encoding::RleDictionary, Some(dict), true) => {
read_dict_buffer_optional(
validity_buffer,
values_buffer,
additional,
dict.as_any().downcast_ref().unwrap(),
values,
validity,
op,
)
}
(Encoding::PlainDictionary | Encoding::RleDictionary, Some(dict), false) => {
read_dict_buffer_required(
values_buffer,
additional,
dict.as_any().downcast_ref().unwrap(),
values,
validity,
op,
)
}
(Encoding::Plain, _, true) => read_nullable(
validity_buffer,
values_buffer,
additional,
values,
validity,
op,
),
(Encoding::Plain, _, false) => read_required(page.buffer(), additional, values, op),
_ => {
return Err(other_utils::not_implemented(
&page.encoding(),
is_optional,
page.dictionary_page().is_some(),
version,
"primitive",
))
}
}
Ok(())
}