arrow_array/builder/mod.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Defines push-based APIs for constructing arrays
6//!
7//! # Basic Usage
8//!
9//! Builders can be used to build simple, non-nested arrays
10//!
11//! ```
12//! # use arrow_array::builder::Int32Builder;
13//! # use arrow_array::PrimitiveArray;
14//! let mut a = Int32Builder::new();
15//! a.append_value(1);
16//! a.append_null();
17//! a.append_value(2);
18//! let a = a.finish();
19//!
20//! assert_eq!(a, PrimitiveArray::from(vec![Some(1), None, Some(2)]));
21//! ```
22//!
23//! ```
24//! # use arrow_array::builder::StringBuilder;
25//! # use arrow_array::{Array, StringArray};
26//! let mut a = StringBuilder::new();
27//! a.append_value("foo");
28//! a.append_value("bar");
29//! a.append_null();
30//! let a = a.finish();
31//!
32//! assert_eq!(a, StringArray::from_iter([Some("foo"), Some("bar"), None]));
33//! ```
34//!
35//! # Nested Usage
36//!
37//! Builders can also be used to build more complex nested arrays, such as lists
38//!
39//! ```
40//! # use arrow_array::builder::{Int32Builder, ListBuilder};
41//! # use arrow_array::ListArray;
42//! # use arrow_array::types::Int32Type;
43//! let mut a = ListBuilder::new(Int32Builder::new());
44//! // [1, 2]
45//! a.values().append_value(1);
46//! a.values().append_value(2);
47//! a.append(true);
48//! // null
49//! a.append(false);
50//! // []
51//! a.append(true);
52//! // [3, null]
53//! a.values().append_value(3);
54//! a.values().append_null();
55//! a.append(true);
56//!
57//! // [[1, 2], null, [], [3, null]]
58//! let a = a.finish();
59//!
60//! assert_eq!(a, ListArray::from_iter_primitive::<Int32Type, _, _>([
61//! Some(vec![Some(1), Some(2)]),
62//! None,
63//! Some(vec![]),
64//! Some(vec![Some(3), None])]
65//! ))
66//! ```
67//!
68//! # Using the [`Extend`] trait to append values from an iterable:
69//!
70//! ```
71//! # use arrow_array::{Array};
72//! # use arrow_array::builder::{ArrayBuilder, StringBuilder};
73//!
74//! let mut builder = StringBuilder::new();
75//! builder.extend(vec![Some("🍐"), Some("🍎"), None]);
76//! assert_eq!(builder.finish().len(), 3);
77//! ```
78//!
79//! # Using the [`Extend`] trait to write generic functions:
80//!
81//! ```
82//! # use arrow_array::{Array, ArrayRef, StringArray};
83//! # use arrow_array::builder::{ArrayBuilder, Int32Builder, ListBuilder, StringBuilder};
84//!
85//! // For generic methods that fill a list of values for an [`ArrayBuilder`], use the [`Extend`] trait.
86//! fn filter_and_fill<V, I: IntoIterator<Item = V>>(builder: &mut impl Extend<V>, values: I, filter: V)
87//! where V: PartialEq
88//! {
89//! builder.extend(values.into_iter().filter(|v| *v == filter));
90//! }
91//! let mut string_builder = StringBuilder::new();
92//! filter_and_fill(
93//! &mut string_builder,
94//! vec![Some("🍐"), Some("🍎"), None],
95//! Some("🍎"),
96//! );
97//! assert_eq!(string_builder.finish().len(), 1);
98//!
99//! let mut int_builder = Int32Builder::new();
100//! filter_and_fill(
101//! &mut int_builder,
102//! vec![Some(11), Some(42), None],
103//! Some(42),
104//! );
105//! assert_eq!(int_builder.finish().len(), 1);
106//!
107//! // For generic methods that fill lists-of-lists for an [`ArrayBuilder`], use the [`Extend`] trait.
108//! fn filter_and_fill_if_contains<T, V, I: IntoIterator<Item = Option<V>>>(
109//! list_builder: &mut impl Extend<Option<V>>,
110//! values: I,
111//! filter: Option<T>,
112//! ) where
113//! T: PartialEq,
114//! for<'a> &'a V: IntoIterator<Item = &'a Option<T>>,
115//! {
116//! list_builder.extend(values.into_iter().filter(|string: &Option<V>| {
117//! string
118//! .as_ref()
119//! .map(|str: &V| str.into_iter().any(|ch: &Option<T>| ch == &filter))
120//! .unwrap_or(false)
121//! }));
122//! }
123//! let builder = StringBuilder::new();
124//! let mut list_builder = ListBuilder::new(builder);
125//! let pear_pear = vec![Some("🍐"),Some("🍐")];
126//! let pear_app = vec![Some("🍐"),Some("🍎")];
127//! filter_and_fill_if_contains(
128//! &mut list_builder,
129//! vec![Some(pear_pear), Some(pear_app), None],
130//! Some("🍎"),
131//! );
132//! assert_eq!(list_builder.finish().len(), 1);
133//! ```
134//!
135//! # Custom Builders
136//!
137//! It is common to have a collection of statically defined Rust types that
138//! you want to convert to Arrow arrays.
139//!
140//! An example of doing so is below
141//!
142//! ```
143//! # use std::any::Any;
144//! # use arrow_array::builder::{ArrayBuilder, Int32Builder, ListBuilder, StringBuilder};
145//! # use arrow_array::{ArrayRef, RecordBatch, StructArray};
146//! # use arrow_schema::{DataType, Field};
147//! # use std::sync::Arc;
148//! /// A custom row representation
149//! struct MyRow {
150//! i32: i32,
151//! optional_i32: Option<i32>,
152//! string: Option<String>,
153//! i32_list: Option<Vec<Option<i32>>>,
154//! }
155//!
156//! /// Converts `Vec<Row>` into `StructArray`
157//! #[derive(Debug, Default)]
158//! struct MyRowBuilder {
159//! i32: Int32Builder,
160//! string: StringBuilder,
161//! i32_list: ListBuilder<Int32Builder>,
162//! }
163//!
164//! impl MyRowBuilder {
165//! fn append(&mut self, row: &MyRow) {
166//! self.i32.append_value(row.i32);
167//! self.string.append_option(row.string.as_ref());
168//! self.i32_list.append_option(row.i32_list.as_ref().map(|x| x.iter().copied()));
169//! }
170//!
171//! /// Note: returns StructArray to allow nesting within another array if desired
172//! fn finish(&mut self) -> StructArray {
173//! let i32 = Arc::new(self.i32.finish()) as ArrayRef;
174//! let i32_field = Arc::new(Field::new("i32", DataType::Int32, false));
175//!
176//! let string = Arc::new(self.string.finish()) as ArrayRef;
177//! let string_field = Arc::new(Field::new("i32", DataType::Utf8, false));
178//!
179//! let i32_list = Arc::new(self.i32_list.finish()) as ArrayRef;
180//! let value_field = Arc::new(Field::new_list_field(DataType::Int32, true));
181//! let i32_list_field = Arc::new(Field::new("i32_list", DataType::List(value_field), true));
182//!
183//! StructArray::from(vec![
184//! (i32_field, i32),
185//! (string_field, string),
186//! (i32_list_field, i32_list),
187//! ])
188//! }
189//! }
190//!
191//! /// For building arrays in generic code, use Extend instead of the append_* methods
192//! /// e.g. append_value, append_option, append_null
193//! impl<'a> Extend<&'a MyRow> for MyRowBuilder {
194//! fn extend<T: IntoIterator<Item = &'a MyRow>>(&mut self, iter: T) {
195//! iter.into_iter().for_each(|row| self.append(row));
196//! }
197//! }
198//!
199//! /// Converts a slice of [`MyRow`] to a [`RecordBatch`]
200//! fn rows_to_batch(rows: &[MyRow]) -> RecordBatch {
201//! let mut builder = MyRowBuilder::default();
202//! builder.extend(rows);
203//! RecordBatch::from(&builder.finish())
204//! }
205//! ```
206//!
207//! # Null / Validity Masks
208//!
209//! The [`NullBufferBuilder`] is optimized for creating the null mask for an array.
210//!
211//! ```
212//! # use arrow_array::builder::NullBufferBuilder;
213//! let mut builder = NullBufferBuilder::new(8);
214//! let mut builder = NullBufferBuilder::new(8);
215//! builder.append_n_non_nulls(7);
216//! builder.append_null();
217//! let buffer = builder.finish().unwrap();
218//! assert_eq!(buffer.len(), 8);
219//! assert_eq!(buffer.iter().collect::<Vec<_>>(), vec![true, true, true, true, true, true, true, false]);
220//! ```
221
222pub use arrow_buffer::BooleanBufferBuilder;
223pub use arrow_buffer::NullBufferBuilder;
224
225mod boolean_builder;
226pub use boolean_builder::*;
227mod buffer_builder;
228pub use buffer_builder::*;
229mod fixed_size_binary_builder;
230pub use fixed_size_binary_builder::*;
231mod fixed_size_list_builder;
232pub use fixed_size_list_builder::*;
233mod fixed_size_binary_dictionary_builder;
234pub use fixed_size_binary_dictionary_builder::*;
235mod generic_bytes_builder;
236pub use generic_bytes_builder::*;
237mod generic_list_builder;
238pub use generic_list_builder::*;
239mod map_builder;
240pub use map_builder::*;
241mod null_builder;
242pub use null_builder::*;
243mod primitive_builder;
244pub use primitive_builder::*;
245mod primitive_dictionary_builder;
246pub use primitive_dictionary_builder::*;
247mod primitive_run_builder;
248pub use primitive_run_builder::*;
249mod struct_builder;
250pub use struct_builder::*;
251mod struct_array_assembler;
252pub use struct_array_assembler::*;
253mod generic_bytes_dictionary_builder;
254pub use generic_bytes_dictionary_builder::*;
255mod generic_byte_run_builder;
256pub use generic_byte_run_builder::*;
257mod generic_bytes_view_builder;
258pub use generic_bytes_view_builder::*;
259mod generic_list_view_builder;
260pub use generic_list_view_builder::*;
261mod union_builder;
262
263pub use union_builder::*;
264
265use crate::ArrayRef;
266use crate::types::{Int8Type, Int16Type, Int32Type, Int64Type};
267use arrow_schema::{DataType, IntervalUnit, TimeUnit};
268use std::any::Any;
269
270/// Trait for dealing with different array builders at runtime
271///
272/// # Example
273///
274/// ```
275/// // Create
276/// # use arrow_array::{ArrayRef, StringArray};
277/// # use arrow_array::builder::{ArrayBuilder, Float64Builder, Int64Builder, StringBuilder};
278///
279/// let mut data_builders: Vec<Box<dyn ArrayBuilder>> = vec![
280/// Box::new(Float64Builder::new()),
281/// Box::new(Int64Builder::new()),
282/// Box::new(StringBuilder::new()),
283/// ];
284///
285/// // Fill
286/// data_builders[0]
287/// .as_any_mut()
288/// .downcast_mut::<Float64Builder>()
289/// .unwrap()
290/// .append_value(3.14);
291/// data_builders[1]
292/// .as_any_mut()
293/// .downcast_mut::<Int64Builder>()
294/// .unwrap()
295/// .append_value(-1);
296/// data_builders[2]
297/// .as_any_mut()
298/// .downcast_mut::<StringBuilder>()
299/// .unwrap()
300/// .append_value("🍎");
301///
302/// // Finish
303/// let array_refs: Vec<ArrayRef> = data_builders
304/// .iter_mut()
305/// .map(|builder| builder.finish())
306/// .collect();
307/// assert_eq!(array_refs[0].len(), 1);
308/// assert_eq!(array_refs[1].is_null(0), false);
309/// assert_eq!(
310/// array_refs[2]
311/// .as_any()
312/// .downcast_ref::<StringArray>()
313/// .unwrap()
314/// .value(0),
315/// "🍎"
316/// );
317/// ```
318pub trait ArrayBuilder: Any + Send + Sync {
319 /// Returns the number of array slots in the builder
320 fn len(&self) -> usize;
321
322 /// Returns whether number of array slots is zero
323 fn is_empty(&self) -> bool {
324 self.len() == 0
325 }
326
327 /// Builds the array
328 fn finish(&mut self) -> ArrayRef;
329
330 /// Builds the array without resetting the underlying builder.
331 fn finish_cloned(&self) -> ArrayRef;
332
333 /// Builds the array without resetting the values builder.
334 ///
335 /// This is relevant for dictionary builders but also for composite builders.
336 /// Those are not affected directly, but will call the corresponding method
337 /// on their constituent builders.
338 ///
339 /// The default implementation just calls [`finish`][Self::finish] which is sufficient
340 /// for all but the above mentioned builders.
341 fn finish_preserve_values(&mut self) -> ArrayRef {
342 self.finish()
343 }
344
345 /// Returns the builder as a non-mutable `Any` reference.
346 ///
347 /// This is most useful when one wants to call non-mutable APIs on a specific builder
348 /// type. In this case, one can first cast this into a `Any`, and then use
349 /// `downcast_ref` to get a reference on the specific builder.
350 fn as_any(&self) -> &dyn Any;
351
352 /// Returns the builder as a mutable `Any` reference.
353 ///
354 /// This is most useful when one wants to call mutable APIs on a specific builder
355 /// type. In this case, one can first cast this into a `Any`, and then use
356 /// `downcast_mut` to get a reference on the specific builder.
357 fn as_any_mut(&mut self) -> &mut dyn Any;
358
359 /// Returns the boxed builder as a box of `Any`.
360 fn into_box_any(self: Box<Self>) -> Box<dyn Any>;
361}
362
363impl ArrayBuilder for Box<dyn ArrayBuilder> {
364 fn len(&self) -> usize {
365 (**self).len()
366 }
367
368 fn is_empty(&self) -> bool {
369 (**self).is_empty()
370 }
371
372 fn finish(&mut self) -> ArrayRef {
373 (**self).finish()
374 }
375
376 fn finish_cloned(&self) -> ArrayRef {
377 (**self).finish_cloned()
378 }
379
380 fn finish_preserve_values(&mut self) -> ArrayRef {
381 (**self).finish_preserve_values()
382 }
383
384 fn as_any(&self) -> &dyn Any {
385 (**self).as_any()
386 }
387
388 fn as_any_mut(&mut self) -> &mut dyn Any {
389 (**self).as_any_mut()
390 }
391
392 fn into_box_any(self: Box<Self>) -> Box<dyn Any> {
393 self
394 }
395}
396
397/// Builder for [`ListArray`](crate::array::ListArray)
398pub type ListBuilder<T> = GenericListBuilder<i32, T>;
399
400/// Builder for [`LargeListArray`](crate::array::LargeListArray)
401pub type LargeListBuilder<T> = GenericListBuilder<i64, T>;
402
403/// Builder for [`ListViewArray`](crate::array::ListViewArray)
404pub type ListViewBuilder<T> = GenericListViewBuilder<i32, T>;
405
406/// Builder for [`LargeListViewArray`](crate::array::LargeListViewArray)
407pub type LargeListViewBuilder<T> = GenericListViewBuilder<i64, T>;
408
409/// Builder for [`BinaryArray`](crate::array::BinaryArray)
410///
411/// See examples on [`GenericBinaryBuilder`]
412pub type BinaryBuilder = GenericBinaryBuilder<i32>;
413
414/// Builder for [`LargeBinaryArray`](crate::array::LargeBinaryArray)
415///
416/// See examples on [`GenericBinaryBuilder`]
417pub type LargeBinaryBuilder = GenericBinaryBuilder<i64>;
418
419/// Builder for [`StringArray`](crate::array::StringArray)
420///
421/// See examples on [`GenericStringBuilder`]
422pub type StringBuilder = GenericStringBuilder<i32>;
423
424/// Builder for [`LargeStringArray`](crate::array::LargeStringArray)
425///
426/// See examples on [`GenericStringBuilder`]
427pub type LargeStringBuilder = GenericStringBuilder<i64>;
428
429/// Returns a builder with capacity for `capacity` elements of datatype
430/// `DataType`.
431///
432/// This function is useful to construct arrays from an arbitrary vectors with
433/// known/expected schema.
434///
435/// See comments on [StructBuilder] for retrieving collection builders built by
436/// make_builder.
437///
438/// # Panics
439///
440/// Panics if `datatype` is not supported, see [`ArrayBuilder`] implementations
441pub fn make_builder(datatype: &DataType, capacity: usize) -> Box<dyn ArrayBuilder> {
442 use crate::builder::*;
443 match datatype {
444 DataType::Null => Box::new(NullBuilder::new()),
445 DataType::Boolean => Box::new(BooleanBuilder::with_capacity(capacity)),
446 DataType::Int8 => Box::new(Int8Builder::with_capacity(capacity)),
447 DataType::Int16 => Box::new(Int16Builder::with_capacity(capacity)),
448 DataType::Int32 => Box::new(Int32Builder::with_capacity(capacity)),
449 DataType::Int64 => Box::new(Int64Builder::with_capacity(capacity)),
450 DataType::UInt8 => Box::new(UInt8Builder::with_capacity(capacity)),
451 DataType::UInt16 => Box::new(UInt16Builder::with_capacity(capacity)),
452 DataType::UInt32 => Box::new(UInt32Builder::with_capacity(capacity)),
453 DataType::UInt64 => Box::new(UInt64Builder::with_capacity(capacity)),
454 DataType::Float16 => Box::new(Float16Builder::with_capacity(capacity)),
455 DataType::Float32 => Box::new(Float32Builder::with_capacity(capacity)),
456 DataType::Float64 => Box::new(Float64Builder::with_capacity(capacity)),
457 DataType::Binary => Box::new(BinaryBuilder::with_capacity(capacity, 1024)),
458 DataType::LargeBinary => Box::new(LargeBinaryBuilder::with_capacity(capacity, 1024)),
459 DataType::BinaryView => Box::new(BinaryViewBuilder::with_capacity(capacity)),
460 DataType::FixedSizeBinary(len) => {
461 Box::new(FixedSizeBinaryBuilder::with_capacity(capacity, *len))
462 }
463 DataType::Decimal32(p, s) => Box::new(
464 Decimal32Builder::with_capacity(capacity).with_data_type(DataType::Decimal32(*p, *s)),
465 ),
466 DataType::Decimal64(p, s) => Box::new(
467 Decimal64Builder::with_capacity(capacity).with_data_type(DataType::Decimal64(*p, *s)),
468 ),
469 DataType::Decimal128(p, s) => Box::new(
470 Decimal128Builder::with_capacity(capacity).with_data_type(DataType::Decimal128(*p, *s)),
471 ),
472 DataType::Decimal256(p, s) => Box::new(
473 Decimal256Builder::with_capacity(capacity).with_data_type(DataType::Decimal256(*p, *s)),
474 ),
475 DataType::Utf8 => Box::new(StringBuilder::with_capacity(capacity, 1024)),
476 DataType::LargeUtf8 => Box::new(LargeStringBuilder::with_capacity(capacity, 1024)),
477 DataType::Utf8View => Box::new(StringViewBuilder::with_capacity(capacity)),
478 DataType::Date32 => Box::new(Date32Builder::with_capacity(capacity)),
479 DataType::Date64 => Box::new(Date64Builder::with_capacity(capacity)),
480 DataType::Time32(TimeUnit::Second) => {
481 Box::new(Time32SecondBuilder::with_capacity(capacity))
482 }
483 DataType::Time32(TimeUnit::Millisecond) => {
484 Box::new(Time32MillisecondBuilder::with_capacity(capacity))
485 }
486 DataType::Time64(TimeUnit::Microsecond) => {
487 Box::new(Time64MicrosecondBuilder::with_capacity(capacity))
488 }
489 DataType::Time64(TimeUnit::Nanosecond) => {
490 Box::new(Time64NanosecondBuilder::with_capacity(capacity))
491 }
492 DataType::Timestamp(TimeUnit::Second, tz) => Box::new(
493 TimestampSecondBuilder::with_capacity(capacity)
494 .with_data_type(DataType::Timestamp(TimeUnit::Second, tz.clone())),
495 ),
496 DataType::Timestamp(TimeUnit::Millisecond, tz) => Box::new(
497 TimestampMillisecondBuilder::with_capacity(capacity)
498 .with_data_type(DataType::Timestamp(TimeUnit::Millisecond, tz.clone())),
499 ),
500 DataType::Timestamp(TimeUnit::Microsecond, tz) => Box::new(
501 TimestampMicrosecondBuilder::with_capacity(capacity)
502 .with_data_type(DataType::Timestamp(TimeUnit::Microsecond, tz.clone())),
503 ),
504 DataType::Timestamp(TimeUnit::Nanosecond, tz) => Box::new(
505 TimestampNanosecondBuilder::with_capacity(capacity)
506 .with_data_type(DataType::Timestamp(TimeUnit::Nanosecond, tz.clone())),
507 ),
508 DataType::Interval(IntervalUnit::YearMonth) => {
509 Box::new(IntervalYearMonthBuilder::with_capacity(capacity))
510 }
511 DataType::Interval(IntervalUnit::DayTime) => {
512 Box::new(IntervalDayTimeBuilder::with_capacity(capacity))
513 }
514 DataType::Interval(IntervalUnit::MonthDayNano) => {
515 Box::new(IntervalMonthDayNanoBuilder::with_capacity(capacity))
516 }
517 DataType::Duration(TimeUnit::Second) => {
518 Box::new(DurationSecondBuilder::with_capacity(capacity))
519 }
520 DataType::Duration(TimeUnit::Millisecond) => {
521 Box::new(DurationMillisecondBuilder::with_capacity(capacity))
522 }
523 DataType::Duration(TimeUnit::Microsecond) => {
524 Box::new(DurationMicrosecondBuilder::with_capacity(capacity))
525 }
526 DataType::Duration(TimeUnit::Nanosecond) => {
527 Box::new(DurationNanosecondBuilder::with_capacity(capacity))
528 }
529 DataType::List(field) => {
530 let builder = make_builder(field.data_type(), capacity);
531 Box::new(ListBuilder::with_capacity(builder, capacity).with_field(field.clone()))
532 }
533 DataType::LargeList(field) => {
534 let builder = make_builder(field.data_type(), capacity);
535 Box::new(LargeListBuilder::with_capacity(builder, capacity).with_field(field.clone()))
536 }
537 DataType::FixedSizeList(field, size) => {
538 let size = *size;
539 let values_builder_capacity = {
540 let size: usize = size.try_into().unwrap();
541 capacity * size
542 };
543 let builder = make_builder(field.data_type(), values_builder_capacity);
544 Box::new(
545 FixedSizeListBuilder::with_capacity(builder, size, capacity)
546 .with_field(field.clone()),
547 )
548 }
549 DataType::ListView(field) => {
550 let builder = make_builder(field.data_type(), capacity);
551 Box::new(ListViewBuilder::with_capacity(builder, capacity).with_field(field.clone()))
552 }
553 DataType::LargeListView(field) => {
554 let builder = make_builder(field.data_type(), capacity);
555 Box::new(
556 LargeListViewBuilder::with_capacity(builder, capacity).with_field(field.clone()),
557 )
558 }
559 DataType::Map(field, _) => match field.data_type() {
560 DataType::Struct(fields) => {
561 let map_field_names = MapFieldNames {
562 key: fields[0].name().clone(),
563 value: fields[1].name().clone(),
564 entry: field.name().clone(),
565 };
566 let key_builder = make_builder(fields[0].data_type(), capacity);
567 let value_builder = make_builder(fields[1].data_type(), capacity);
568 Box::new(
569 MapBuilder::with_capacity(
570 Some(map_field_names),
571 key_builder,
572 value_builder,
573 capacity,
574 )
575 .with_keys_field(fields[0].clone())
576 .with_values_field(fields[1].clone()),
577 )
578 }
579 t => panic!("The field of Map data type {t} should have a child Struct field"),
580 },
581 DataType::Struct(fields) => Box::new(StructBuilder::from_fields(fields.clone(), capacity)),
582 t @ DataType::Dictionary(key_type, value_type) => {
583 macro_rules! dict_builder {
584 ($key_type:ty) => {
585 match &**value_type {
586 DataType::Utf8 => {
587 let dict_builder: StringDictionaryBuilder<$key_type> =
588 StringDictionaryBuilder::with_capacity(capacity, 256, 1024);
589 Box::new(dict_builder)
590 }
591 DataType::LargeUtf8 => {
592 let dict_builder: LargeStringDictionaryBuilder<$key_type> =
593 LargeStringDictionaryBuilder::with_capacity(capacity, 256, 1024);
594 Box::new(dict_builder)
595 }
596 DataType::Binary => {
597 let dict_builder: BinaryDictionaryBuilder<$key_type> =
598 BinaryDictionaryBuilder::with_capacity(capacity, 256, 1024);
599 Box::new(dict_builder)
600 }
601 DataType::LargeBinary => {
602 let dict_builder: LargeBinaryDictionaryBuilder<$key_type> =
603 LargeBinaryDictionaryBuilder::with_capacity(capacity, 256, 1024);
604 Box::new(dict_builder)
605 }
606 t => unimplemented!("Dictionary value type {t} is not currently supported"),
607 }
608 };
609 }
610 match &**key_type {
611 DataType::Int8 => dict_builder!(Int8Type),
612 DataType::Int16 => dict_builder!(Int16Type),
613 DataType::Int32 => dict_builder!(Int32Type),
614 DataType::Int64 => dict_builder!(Int64Type),
615 _ => {
616 unimplemented!(
617 "Data type {t} with key type {key_type} is not currently supported"
618 )
619 }
620 }
621 }
622 t => unimplemented!("Data type {t} is not currently supported"),
623 }
624}
625
626#[cfg(test)]
627mod tests {
628 use super::*;
629
630 #[derive(Default)]
631 pub struct PreserveValuesMock {
632 pub called: usize,
633 pub inner: Int32Builder,
634 }
635
636 impl ArrayBuilder for PreserveValuesMock {
637 fn as_any(&self) -> &dyn Any {
638 self
639 }
640
641 fn as_any_mut(&mut self) -> &mut dyn Any {
642 self
643 }
644
645 fn into_box_any(self: Box<Self>) -> Box<dyn Any> {
646 self
647 }
648
649 fn len(&self) -> usize {
650 self.inner.len()
651 }
652
653 fn finish(&mut self) -> ArrayRef {
654 panic!("finish should never be called on PreserveValuesMock")
655 }
656
657 fn finish_cloned(&self) -> ArrayRef {
658 panic!("finish_cloned should never be called on PreserveValuesMock")
659 }
660
661 fn finish_preserve_values(&mut self) -> ArrayRef {
662 self.called += 1;
663 self.inner.finish_preserve_values()
664 }
665 }
666}