1use std::any::Any;
20use std::sync::Arc;
21
22use crate::error::Result;
23use crate::{
24 bitmap::{Bitmap, MutableBitmap},
25 datatypes::DataType,
26};
27
28pub(self) mod physical_binary;
29
30pub trait Array: Send + Sync + dyn_clone::DynClone + 'static {
33 fn as_any(&self) -> &dyn Any;
35
36 fn as_any_mut(&mut self) -> &mut dyn Any;
38
39 fn len(&self) -> usize;
42
43 fn is_empty(&self) -> bool {
45 self.len() == 0
46 }
47
48 fn data_type(&self) -> &DataType;
51
52 fn validity(&self) -> Option<&Bitmap>;
56
57 #[inline]
61 fn null_count(&self) -> usize {
62 if self.data_type() == &DataType::Null {
63 return self.len();
64 };
65 self.validity()
66 .as_ref()
67 .map(|x| x.unset_bits())
68 .unwrap_or(0)
69 }
70
71 #[inline]
75 fn is_null(&self, i: usize) -> bool {
76 assert!(i < self.len());
77 unsafe { self.is_null_unchecked(i) }
78 }
79
80 #[inline]
84 unsafe fn is_null_unchecked(&self, i: usize) -> bool {
85 self.validity()
86 .as_ref()
87 .map(|x| !x.get_bit_unchecked(i))
88 .unwrap_or(false)
89 }
90
91 #[inline]
95 fn is_valid(&self, i: usize) -> bool {
96 !self.is_null(i)
97 }
98
99 fn slice(&mut self, offset: usize, length: usize);
105
106 unsafe fn slice_unchecked(&mut self, offset: usize, length: usize);
112
113 #[must_use]
119 fn sliced(&self, offset: usize, length: usize) -> Box<dyn Array> {
120 let mut new = self.to_boxed();
121 new.slice(offset, length);
122 new
123 }
124
125 #[must_use]
132 unsafe fn sliced_unchecked(&self, offset: usize, length: usize) -> Box<dyn Array> {
133 let mut new = self.to_boxed();
134 new.slice_unchecked(offset, length);
135 new
136 }
137
138 fn with_validity(&self, validity: Option<Bitmap>) -> Box<dyn Array>;
142
143 fn to_boxed(&self) -> Box<dyn Array>;
145}
146
147dyn_clone::clone_trait_object!(Array);
148
149pub(crate) trait Container {
152 fn with_capacity(capacity: usize) -> Self
154 where
155 Self: Sized;
156}
157
158pub trait MutableArray: std::fmt::Debug + Send + Sync {
163 fn data_type(&self) -> &DataType;
165
166 fn len(&self) -> usize;
168
169 fn is_empty(&self) -> bool {
171 self.len() == 0
172 }
173
174 fn validity(&self) -> Option<&MutableBitmap>;
176
177 fn as_box(&mut self) -> Box<dyn Array>;
179
180 fn as_arc(&mut self) -> std::sync::Arc<dyn Array> {
185 self.as_box().into()
186 }
187
188 fn as_any(&self) -> &dyn Any;
190
191 fn as_mut_any(&mut self) -> &mut dyn Any;
193
194 fn push_null(&mut self);
196
197 #[inline]
201 fn is_valid(&self, index: usize) -> bool {
202 self.validity()
203 .as_ref()
204 .map(|x| x.get(index))
205 .unwrap_or(true)
206 }
207
208 fn reserve(&mut self, additional: usize);
210
211 fn shrink_to_fit(&mut self);
213}
214
215impl MutableArray for Box<dyn MutableArray> {
216 fn len(&self) -> usize {
217 self.as_ref().len()
218 }
219
220 fn validity(&self) -> Option<&MutableBitmap> {
221 self.as_ref().validity()
222 }
223
224 fn as_box(&mut self) -> Box<dyn Array> {
225 self.as_mut().as_box()
226 }
227
228 fn as_arc(&mut self) -> Arc<dyn Array> {
229 self.as_mut().as_arc()
230 }
231
232 fn data_type(&self) -> &DataType {
233 self.as_ref().data_type()
234 }
235
236 fn as_any(&self) -> &dyn std::any::Any {
237 self.as_ref().as_any()
238 }
239
240 fn as_mut_any(&mut self) -> &mut dyn std::any::Any {
241 self.as_mut().as_mut_any()
242 }
243
244 #[inline]
245 fn push_null(&mut self) {
246 self.as_mut().push_null()
247 }
248
249 fn shrink_to_fit(&mut self) {
250 self.as_mut().shrink_to_fit();
251 }
252
253 fn reserve(&mut self, additional: usize) {
254 self.as_mut().reserve(additional);
255 }
256}
257
258macro_rules! general_dyn {
259 ($array:expr, $ty:ty, $f:expr) => {{
260 let array = $array.as_any().downcast_ref::<$ty>().unwrap();
261 ($f)(array)
262 }};
263}
264
265macro_rules! fmt_dyn {
266 ($array:expr, $ty:ty, $f:expr) => {{
267 let mut f = |x: &$ty| x.fmt($f);
268 general_dyn!($array, $ty, f)
269 }};
270}
271
272macro_rules! match_integer_type {(
273 $key_type:expr, | $_:tt $T:ident | $($body:tt)*
274) => ({
275 macro_rules! __with_ty__ {( $_ $T:ident ) => ( $($body)* )}
276 use crate::datatypes::IntegerType::*;
277 match $key_type {
278 Int8 => __with_ty__! { i8 },
279 Int16 => __with_ty__! { i16 },
280 Int32 => __with_ty__! { i32 },
281 Int64 => __with_ty__! { i64 },
282 UInt8 => __with_ty__! { u8 },
283 UInt16 => __with_ty__! { u16 },
284 UInt32 => __with_ty__! { u32 },
285 UInt64 => __with_ty__! { u64 },
286 }
287})}
288
289macro_rules! with_match_primitive_type {(
290 $key_type:expr, | $_:tt $T:ident | $($body:tt)*
291) => ({
292 macro_rules! __with_ty__ {( $_ $T:ident ) => ( $($body)* )}
293 use crate::datatypes::PrimitiveType::*;
294 use crate::types::{days_ms, months_days_ns, f16, i256};
295 match $key_type {
296 Int8 => __with_ty__! { i8 },
297 Int16 => __with_ty__! { i16 },
298 Int32 => __with_ty__! { i32 },
299 Int64 => __with_ty__! { i64 },
300 Int128 => __with_ty__! { i128 },
301 Int256 => __with_ty__! { i256 },
302 DaysMs => __with_ty__! { days_ms },
303 MonthDayNano => __with_ty__! { months_days_ns },
304 UInt8 => __with_ty__! { u8 },
305 UInt16 => __with_ty__! { u16 },
306 UInt32 => __with_ty__! { u32 },
307 UInt64 => __with_ty__! { u64 },
308 Float16 => __with_ty__! { f16 },
309 Float32 => __with_ty__! { f32 },
310 Float64 => __with_ty__! { f64 },
311 }
312})}
313
314impl std::fmt::Debug for dyn Array + '_ {
315 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
316 use crate::datatypes::PhysicalType::*;
317 match self.data_type().to_physical_type() {
318 Null => fmt_dyn!(self, NullArray, f),
319 Boolean => fmt_dyn!(self, BooleanArray, f),
320 Primitive(primitive) => with_match_primitive_type!(primitive, |$T| {
321 fmt_dyn!(self, PrimitiveArray<$T>, f)
322 }),
323 Binary => fmt_dyn!(self, BinaryArray<i32>, f),
324 LargeBinary => fmt_dyn!(self, BinaryArray<i64>, f),
325 FixedSizeBinary => fmt_dyn!(self, FixedSizeBinaryArray, f),
326 Utf8 => fmt_dyn!(self, Utf8Array::<i32>, f),
327 LargeUtf8 => fmt_dyn!(self, Utf8Array::<i64>, f),
328 List => fmt_dyn!(self, ListArray::<i32>, f),
329 LargeList => fmt_dyn!(self, ListArray::<i64>, f),
330 FixedSizeList => fmt_dyn!(self, FixedSizeListArray, f),
331 Struct => fmt_dyn!(self, StructArray, f),
332 Union => fmt_dyn!(self, UnionArray, f),
333 Dictionary(key_type) => {
334 match_integer_type!(key_type, |$T| {
335 fmt_dyn!(self, DictionaryArray::<$T>, f)
336 })
337 }
338 Map => fmt_dyn!(self, MapArray, f),
339 }
340 }
341}
342
343pub fn new_empty_array(data_type: DataType) -> Box<dyn Array> {
345 use crate::datatypes::PhysicalType::*;
346 match data_type.to_physical_type() {
347 Null => Box::new(NullArray::new_empty(data_type)),
348 Boolean => Box::new(BooleanArray::new_empty(data_type)),
349 Primitive(primitive) => with_match_primitive_type!(primitive, |$T| {
350 Box::new(PrimitiveArray::<$T>::new_empty(data_type))
351 }),
352 Binary => Box::new(BinaryArray::<i32>::new_empty(data_type)),
353 LargeBinary => Box::new(BinaryArray::<i64>::new_empty(data_type)),
354 FixedSizeBinary => Box::new(FixedSizeBinaryArray::new_empty(data_type)),
355 Utf8 => Box::new(Utf8Array::<i32>::new_empty(data_type)),
356 LargeUtf8 => Box::new(Utf8Array::<i64>::new_empty(data_type)),
357 List => Box::new(ListArray::<i32>::new_empty(data_type)),
358 LargeList => Box::new(ListArray::<i64>::new_empty(data_type)),
359 FixedSizeList => Box::new(FixedSizeListArray::new_empty(data_type)),
360 Struct => Box::new(StructArray::new_empty(data_type)),
361 Union => Box::new(UnionArray::new_empty(data_type)),
362 Map => Box::new(MapArray::new_empty(data_type)),
363 Dictionary(key_type) => {
364 match_integer_type!(key_type, |$T| {
365 Box::new(DictionaryArray::<$T>::new_empty(data_type))
366 })
367 }
368 }
369}
370
371pub fn new_null_array(data_type: DataType, length: usize) -> Box<dyn Array> {
375 use crate::datatypes::PhysicalType::*;
376 match data_type.to_physical_type() {
377 Null => Box::new(NullArray::new_null(data_type, length)),
378 Boolean => Box::new(BooleanArray::new_null(data_type, length)),
379 Primitive(primitive) => with_match_primitive_type!(primitive, |$T| {
380 Box::new(PrimitiveArray::<$T>::new_null(data_type, length))
381 }),
382 Binary => Box::new(BinaryArray::<i32>::new_null(data_type, length)),
383 LargeBinary => Box::new(BinaryArray::<i64>::new_null(data_type, length)),
384 FixedSizeBinary => Box::new(FixedSizeBinaryArray::new_null(data_type, length)),
385 Utf8 => Box::new(Utf8Array::<i32>::new_null(data_type, length)),
386 LargeUtf8 => Box::new(Utf8Array::<i64>::new_null(data_type, length)),
387 List => Box::new(ListArray::<i32>::new_null(data_type, length)),
388 LargeList => Box::new(ListArray::<i64>::new_null(data_type, length)),
389 FixedSizeList => Box::new(FixedSizeListArray::new_null(data_type, length)),
390 Struct => Box::new(StructArray::new_null(data_type, length)),
391 Union => Box::new(UnionArray::new_null(data_type, length)),
392 Map => Box::new(MapArray::new_null(data_type, length)),
393 Dictionary(key_type) => {
394 match_integer_type!(key_type, |$T| {
395 Box::new(DictionaryArray::<$T>::new_null(data_type, length))
396 })
397 }
398 }
399}
400
401#[cfg(feature = "arrow")]
405pub trait Arrow2Arrow: Array {
406 fn to_data(&self) -> arrow_data::ArrayData;
408
409 fn from_data(data: &arrow_data::ArrayData) -> Self;
411}
412
413#[cfg(feature = "arrow")]
414macro_rules! to_data_dyn {
415 ($array:expr, $ty:ty) => {{
416 let f = |x: &$ty| x.to_data();
417 general_dyn!($array, $ty, f)
418 }};
419}
420
421#[cfg(feature = "arrow")]
422impl From<Box<dyn Array>> for arrow_array::ArrayRef {
423 fn from(value: Box<dyn Array>) -> Self {
424 value.as_ref().into()
425 }
426}
427
428#[cfg(feature = "arrow")]
429impl From<&dyn Array> for arrow_array::ArrayRef {
430 fn from(value: &dyn Array) -> Self {
431 arrow_array::make_array(to_data(value))
432 }
433}
434
435#[cfg(feature = "arrow")]
436impl From<arrow_array::ArrayRef> for Box<dyn Array> {
437 fn from(value: arrow_array::ArrayRef) -> Self {
438 value.as_ref().into()
439 }
440}
441
442#[cfg(feature = "arrow")]
443impl From<&dyn arrow_array::Array> for Box<dyn Array> {
444 fn from(value: &dyn arrow_array::Array) -> Self {
445 from_data(&value.to_data())
446 }
447}
448
449#[cfg(feature = "arrow")]
451pub fn to_data(array: &dyn Array) -> arrow_data::ArrayData {
452 use crate::datatypes::PhysicalType::*;
453 match array.data_type().to_physical_type() {
454 Null => to_data_dyn!(array, NullArray),
455 Boolean => to_data_dyn!(array, BooleanArray),
456 Primitive(primitive) => with_match_primitive_type!(primitive, |$T| {
457 to_data_dyn!(array, PrimitiveArray<$T>)
458 }),
459 Binary => to_data_dyn!(array, BinaryArray<i32>),
460 LargeBinary => to_data_dyn!(array, BinaryArray<i64>),
461 FixedSizeBinary => to_data_dyn!(array, FixedSizeBinaryArray),
462 Utf8 => to_data_dyn!(array, Utf8Array::<i32>),
463 LargeUtf8 => to_data_dyn!(array, Utf8Array::<i64>),
464 List => to_data_dyn!(array, ListArray::<i32>),
465 LargeList => to_data_dyn!(array, ListArray::<i64>),
466 FixedSizeList => to_data_dyn!(array, FixedSizeListArray),
467 Struct => to_data_dyn!(array, StructArray),
468 Union => to_data_dyn!(array, UnionArray),
469 Dictionary(key_type) => {
470 match_integer_type!(key_type, |$T| {
471 to_data_dyn!(array, DictionaryArray::<$T>)
472 })
473 }
474 Map => to_data_dyn!(array, MapArray),
475 }
476}
477
478#[cfg(feature = "arrow")]
480pub fn from_data(data: &arrow_data::ArrayData) -> Box<dyn Array> {
481 use crate::datatypes::PhysicalType::*;
482 let data_type: DataType = data.data_type().clone().into();
483 match data_type.to_physical_type() {
484 Null => Box::new(NullArray::from_data(data)),
485 Boolean => Box::new(BooleanArray::from_data(data)),
486 Primitive(primitive) => with_match_primitive_type!(primitive, |$T| {
487 Box::new(PrimitiveArray::<$T>::from_data(data))
488 }),
489 Binary => Box::new(BinaryArray::<i32>::from_data(data)),
490 LargeBinary => Box::new(BinaryArray::<i64>::from_data(data)),
491 FixedSizeBinary => Box::new(FixedSizeBinaryArray::from_data(data)),
492 Utf8 => Box::new(Utf8Array::<i32>::from_data(data)),
493 LargeUtf8 => Box::new(Utf8Array::<i64>::from_data(data)),
494 List => Box::new(ListArray::<i32>::from_data(data)),
495 LargeList => Box::new(ListArray::<i64>::from_data(data)),
496 FixedSizeList => Box::new(FixedSizeListArray::from_data(data)),
497 Struct => Box::new(StructArray::from_data(data)),
498 Union => Box::new(UnionArray::from_data(data)),
499 Dictionary(key_type) => {
500 match_integer_type!(key_type, |$T| {
501 Box::new(DictionaryArray::<$T>::from_data(data))
502 })
503 }
504 Map => Box::new(MapArray::from_data(data)),
505 }
506}
507
508macro_rules! clone_dyn {
509 ($array:expr, $ty:ty) => {{
510 let f = |x: &$ty| Box::new(x.clone());
511 general_dyn!($array, $ty, f)
512 }};
513}
514
515macro_rules! impl_sliced {
517 () => {
518 #[inline]
524 #[must_use]
525 pub fn sliced(self, offset: usize, length: usize) -> Self {
526 assert!(
527 offset + length <= self.len(),
528 "the offset of the new Buffer cannot exceed the existing length"
529 );
530 unsafe { self.sliced_unchecked(offset, length) }
531 }
532
533 #[inline]
539 #[must_use]
540 pub unsafe fn sliced_unchecked(mut self, offset: usize, length: usize) -> Self {
541 self.slice_unchecked(offset, length);
542 self
543 }
544 };
545}
546
547macro_rules! impl_mut_validity {
549 () => {
550 #[must_use]
554 #[inline]
555 pub fn with_validity(mut self, validity: Option<Bitmap>) -> Self {
556 self.set_validity(validity);
557 self
558 }
559
560 #[inline]
564 pub fn set_validity(&mut self, validity: Option<Bitmap>) {
565 if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) {
566 panic!("validity must be equal to the array's length")
567 }
568 self.validity = validity;
569 }
570 }
571}
572
573macro_rules! impl_mutable_array_mut_validity {
575 () => {
576 #[must_use]
580 #[inline]
581 pub fn with_validity(mut self, validity: Option<MutableBitmap>) -> Self {
582 self.set_validity(validity);
583 self
584 }
585
586 #[inline]
590 pub fn set_validity(&mut self, validity: Option<MutableBitmap>) {
591 if matches!(&validity, Some(bitmap) if bitmap.len() != self.len()) {
592 panic!("validity must be equal to the array's length")
593 }
594 self.validity = validity;
595 }
596
597 #[inline]
603 pub fn apply_validity<F: FnOnce(MutableBitmap) -> MutableBitmap>(&mut self, f: F) {
604 if let Some(validity) = std::mem::take(&mut self.validity) {
605 self.set_validity(Some(f(validity)))
606 }
607 }
608
609 }
610}
611
612macro_rules! impl_into_array {
614 () => {
615 pub fn boxed(self) -> Box<dyn Array> {
617 Box::new(self)
618 }
619
620 pub fn arced(self) -> std::sync::Arc<dyn Array> {
622 std::sync::Arc::new(self)
623 }
624 };
625}
626
627macro_rules! impl_common_array {
629 () => {
630 #[inline]
631 fn as_any(&self) -> &dyn std::any::Any {
632 self
633 }
634
635 #[inline]
636 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
637 self
638 }
639
640 #[inline]
641 fn len(&self) -> usize {
642 self.len()
643 }
644
645 #[inline]
646 fn data_type(&self) -> &DataType {
647 &self.data_type
648 }
649
650 #[inline]
651 fn slice(&mut self, offset: usize, length: usize) {
652 self.slice(offset, length);
653 }
654
655 #[inline]
656 unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) {
657 self.slice_unchecked(offset, length);
658 }
659
660 #[inline]
661 fn to_boxed(&self) -> Box<dyn Array> {
662 Box::new(self.clone())
663 }
664 };
665}
666
667pub fn clone(array: &dyn Array) -> Box<dyn Array> {
672 use crate::datatypes::PhysicalType::*;
673 match array.data_type().to_physical_type() {
674 Null => clone_dyn!(array, NullArray),
675 Boolean => clone_dyn!(array, BooleanArray),
676 Primitive(primitive) => with_match_primitive_type!(primitive, |$T| {
677 clone_dyn!(array, PrimitiveArray<$T>)
678 }),
679 Binary => clone_dyn!(array, BinaryArray<i32>),
680 LargeBinary => clone_dyn!(array, BinaryArray<i64>),
681 FixedSizeBinary => clone_dyn!(array, FixedSizeBinaryArray),
682 Utf8 => clone_dyn!(array, Utf8Array::<i32>),
683 LargeUtf8 => clone_dyn!(array, Utf8Array::<i64>),
684 List => clone_dyn!(array, ListArray::<i32>),
685 LargeList => clone_dyn!(array, ListArray::<i64>),
686 FixedSizeList => clone_dyn!(array, FixedSizeListArray),
687 Struct => clone_dyn!(array, StructArray),
688 Union => clone_dyn!(array, UnionArray),
689 Map => clone_dyn!(array, MapArray),
690 Dictionary(key_type) => {
691 match_integer_type!(key_type, |$T| {
692 clone_dyn!(array, DictionaryArray::<$T>)
693 })
694 }
695 }
696}
697
698impl<'a> AsRef<(dyn Array + 'a)> for dyn Array {
701 fn as_ref(&self) -> &(dyn Array + 'a) {
702 self
703 }
704}
705
706mod binary;
707mod boolean;
708mod dictionary;
709mod fixed_size_binary;
710mod fixed_size_list;
711mod list;
712mod map;
713mod null;
714mod primitive;
715mod specification;
716mod struct_;
717mod union;
718mod utf8;
719
720mod equal;
721mod ffi;
722mod fmt;
723#[doc(hidden)]
724pub mod indexable;
725mod iterator;
726
727pub mod growable;
728pub mod ord;
729
730pub(crate) use iterator::ArrayAccessor;
731pub use iterator::ArrayValuesIter;
732
733pub use equal::equal;
734pub use fmt::{get_display, get_value_display};
735
736pub use binary::{BinaryArray, BinaryValueIter, MutableBinaryArray, MutableBinaryValuesArray};
737pub use boolean::{BooleanArray, MutableBooleanArray};
738pub use dictionary::{DictionaryArray, DictionaryKey, MutableDictionaryArray};
739pub use fixed_size_binary::{FixedSizeBinaryArray, MutableFixedSizeBinaryArray};
740pub use fixed_size_list::{FixedSizeListArray, MutableFixedSizeListArray};
741pub use list::{ListArray, ListValuesIter, MutableListArray};
742pub use map::MapArray;
743pub use null::{MutableNullArray, NullArray};
744pub use primitive::*;
745pub use struct_::{MutableStructArray, StructArray};
746pub use union::UnionArray;
747pub use utf8::{MutableUtf8Array, MutableUtf8ValuesArray, Utf8Array, Utf8ValuesIter};
748
749pub(crate) use self::ffi::offset_buffers_children_dictionary;
750pub(crate) use self::ffi::FromFfi;
751pub(crate) use self::ffi::ToFfi;
752
753pub trait TryExtend<A> {
756 fn try_extend<I: IntoIterator<Item = A>>(&mut self, iter: I) -> Result<()>;
758}
759
760pub trait TryPush<A> {
762 fn try_push(&mut self, item: A) -> Result<()>;
764}
765
766pub trait PushUnchecked<A> {
768 unsafe fn push_unchecked(&mut self, item: A);
773}
774
775pub trait TryExtendFromSelf {
778 fn try_extend_from_self(&mut self, other: &Self) -> Result<()>;
780}
781
782pub unsafe trait GenericBinaryArray<O: crate::offset::Offset>: Array {
789 fn values(&self) -> &[u8];
791 fn offsets(&self) -> &[O];
793}