1use std::{any::Any, sync::Arc};
19
20use crate::{ArrayRef, ArrowPrimitiveType, RunArray, types::RunEndIndexType};
21
22use super::{ArrayBuilder, PrimitiveBuilder};
23
24use arrow_buffer::ArrowNativeType;
25
26#[derive(Debug)]
61pub struct PrimitiveRunBuilder<R, V>
62where
63 R: RunEndIndexType,
64 V: ArrowPrimitiveType,
65{
66 run_ends_builder: PrimitiveBuilder<R>,
67 values_builder: PrimitiveBuilder<V>,
68 current_value: Option<V::Native>,
69 current_run_end_index: usize,
70 prev_run_end_index: usize,
71}
72
73impl<R, V> Default for PrimitiveRunBuilder<R, V>
74where
75 R: RunEndIndexType,
76 V: ArrowPrimitiveType,
77{
78 fn default() -> Self {
79 Self::new()
80 }
81}
82
83impl<R, V> PrimitiveRunBuilder<R, V>
84where
85 R: RunEndIndexType,
86 V: ArrowPrimitiveType,
87{
88 pub fn new() -> Self {
90 Self {
91 run_ends_builder: PrimitiveBuilder::new(),
92 values_builder: PrimitiveBuilder::new(),
93 current_value: None,
94 current_run_end_index: 0,
95 prev_run_end_index: 0,
96 }
97 }
98
99 pub fn with_capacity(capacity: usize) -> Self {
103 Self {
104 run_ends_builder: PrimitiveBuilder::with_capacity(capacity),
105 values_builder: PrimitiveBuilder::with_capacity(capacity),
106 current_value: None,
107 current_run_end_index: 0,
108 prev_run_end_index: 0,
109 }
110 }
111
112 pub fn with_data_type(mut self, data_type: arrow_schema::DataType) -> Self {
122 self.values_builder = self.values_builder.with_data_type(data_type);
123 self
124 }
125}
126
127impl<R, V> ArrayBuilder for PrimitiveRunBuilder<R, V>
128where
129 R: RunEndIndexType,
130 V: ArrowPrimitiveType,
131{
132 fn as_any(&self) -> &dyn Any {
134 self
135 }
136
137 fn as_any_mut(&mut self) -> &mut dyn Any {
139 self
140 }
141
142 fn into_box_any(self: Box<Self>) -> Box<dyn Any> {
144 self
145 }
146
147 fn len(&self) -> usize {
150 self.current_run_end_index
151 }
152
153 fn finish(&mut self) -> ArrayRef {
155 Arc::new(self.finish())
156 }
157
158 fn finish_cloned(&self) -> ArrayRef {
160 Arc::new(self.finish_cloned())
161 }
162}
163
164impl<R, V> PrimitiveRunBuilder<R, V>
165where
166 R: RunEndIndexType,
167 V: ArrowPrimitiveType,
168{
169 pub fn append_option(&mut self, value: Option<V::Native>) {
171 if self.current_run_end_index == 0 {
172 self.current_run_end_index = 1;
173 self.current_value = value;
174 return;
175 }
176 if self.current_value != value {
177 self.append_run_end();
178 self.current_value = value;
179 }
180
181 self.current_run_end_index += 1;
182 }
183
184 pub fn append_value(&mut self, value: V::Native) {
186 self.append_option(Some(value))
187 }
188
189 pub fn append_null(&mut self) {
191 self.append_option(None)
192 }
193
194 pub fn finish(&mut self) -> RunArray<R> {
200 self.append_run_end();
202
203 self.current_value = None;
205 self.current_run_end_index = 0;
206
207 let run_ends_array = self.run_ends_builder.finish();
209 let values_array = self.values_builder.finish();
210 RunArray::<R>::try_new(&run_ends_array, &values_array).unwrap()
211 }
212
213 pub fn finish_cloned(&self) -> RunArray<R> {
219 let mut run_ends_array = self.run_ends_builder.finish_cloned();
220 let mut values_array = self.values_builder.finish_cloned();
221
222 if self.prev_run_end_index != self.current_run_end_index {
224 let mut run_end_builder = run_ends_array.into_builder().unwrap();
225 let mut values_builder = values_array.into_builder().unwrap();
226 self.append_run_end_with_builders(&mut run_end_builder, &mut values_builder);
227 run_ends_array = run_end_builder.finish();
228 values_array = values_builder.finish();
229 }
230
231 RunArray::try_new(&run_ends_array, &values_array).unwrap()
232 }
233
234 fn append_run_end(&mut self) {
236 if self.prev_run_end_index == self.current_run_end_index {
238 return;
239 }
240 let run_end_index = self.run_end_index_as_native();
241 self.run_ends_builder.append_value(run_end_index);
242 self.values_builder.append_option(self.current_value);
243 self.prev_run_end_index = self.current_run_end_index;
244 }
245
246 fn append_run_end_with_builders(
249 &self,
250 run_ends_builder: &mut PrimitiveBuilder<R>,
251 values_builder: &mut PrimitiveBuilder<V>,
252 ) {
253 let run_end_index = self.run_end_index_as_native();
254 run_ends_builder.append_value(run_end_index);
255 values_builder.append_option(self.current_value);
256 }
257
258 fn run_end_index_as_native(&self) -> R::Native {
259 R::Native::from_usize(self.current_run_end_index)
260 .unwrap_or_else(|| panic!(
261 "Cannot convert `current_run_end_index` {} from `usize` to native form of arrow datatype {}",
262 self.current_run_end_index,
263 R::DATA_TYPE
264 ))
265 }
266}
267
268impl<R, V> Extend<Option<V::Native>> for PrimitiveRunBuilder<R, V>
269where
270 R: RunEndIndexType,
271 V: ArrowPrimitiveType,
272{
273 fn extend<T: IntoIterator<Item = Option<V::Native>>>(&mut self, iter: T) {
274 for elem in iter {
275 self.append_option(elem);
276 }
277 }
278}
279
280#[cfg(test)]
281mod tests {
282 use arrow_schema::DataType;
283
284 use crate::builder::PrimitiveRunBuilder;
285 use crate::cast::AsArray;
286 use crate::types::{Decimal128Type, Int16Type, TimestampMicrosecondType, UInt32Type};
287 use crate::{Array, Decimal128Array, TimestampMicrosecondArray, UInt32Array};
288
289 #[test]
290 fn test_primitive_ree_array_builder() {
291 let mut builder = PrimitiveRunBuilder::<Int16Type, UInt32Type>::new();
292 builder.append_value(1234);
293 builder.append_value(1234);
294 builder.append_value(1234);
295 builder.append_null();
296 builder.append_value(5678);
297 builder.append_value(5678);
298
299 let array = builder.finish();
300
301 assert_eq!(array.null_count(), 0);
302 assert_eq!(array.logical_null_count(), 1);
303 assert_eq!(array.len(), 6);
304
305 assert_eq!(array.run_ends().values(), &[3, 4, 6]);
306
307 let av = array.values();
308
309 assert!(!av.is_null(0));
310 assert!(av.is_null(1));
311 assert!(!av.is_null(2));
312
313 let ava: &UInt32Array = av.as_primitive::<UInt32Type>();
315
316 assert_eq!(ava, &UInt32Array::from(vec![Some(1234), None, Some(5678)]));
317 }
318
319 #[test]
320 fn test_extend() {
321 let mut builder = PrimitiveRunBuilder::<Int16Type, Int16Type>::new();
322 builder.extend([1, 2, 2, 5, 5, 4, 4].into_iter().map(Some));
323 builder.extend([4, 4, 6, 2].into_iter().map(Some));
324 let array = builder.finish();
325
326 assert_eq!(array.len(), 11);
327 assert_eq!(array.null_count(), 0);
328 assert_eq!(array.logical_null_count(), 0);
329 assert_eq!(array.run_ends().values(), &[1, 3, 5, 9, 10, 11]);
330 assert_eq!(
331 array.values().as_primitive::<Int16Type>().values(),
332 &[1, 2, 5, 4, 6, 2]
333 );
334 }
335
336 #[test]
337 #[should_panic(expected = "incompatible data type for builder")]
338 fn test_override_data_type_invalid() {
339 PrimitiveRunBuilder::<Int16Type, UInt32Type>::new().with_data_type(DataType::UInt64);
340 }
341
342 #[test]
343 fn test_override_data_type() {
344 PrimitiveRunBuilder::<Int16Type, UInt32Type>::new().with_data_type(DataType::UInt32);
346
347 let mut builder = PrimitiveRunBuilder::<Int16Type, Decimal128Type>::new()
349 .with_data_type(DataType::Decimal128(1, 2));
350 builder.append_value(123);
351 let array = builder.finish();
352 let array = array.downcast::<Decimal128Array>().unwrap();
353 let values = array.values();
354 assert_eq!(values.precision(), 1);
355 assert_eq!(values.scale(), 2);
356
357 let mut builder = PrimitiveRunBuilder::<Int16Type, TimestampMicrosecondType>::new()
359 .with_data_type(DataType::Timestamp(
360 arrow_schema::TimeUnit::Microsecond,
361 Some("Europe/Paris".into()),
362 ));
363 builder.append_value(1);
364 let array = builder.finish();
365 let array = array.downcast::<TimestampMicrosecondArray>().unwrap();
366 let values = array.values();
367 assert_eq!(values.timezone(), Some("Europe/Paris"));
368 }
369}