Skip to main content

arrow_array/array/
primitive_array.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
5use crate::array::print_long_array;
6use crate::builder::{BooleanBufferBuilder, PrimitiveBuilder};
7use crate::iterator::PrimitiveIter;
8use crate::temporal_conversions::{
9    as_datetime, as_datetime_with_data_type, as_datetime_with_timezone,
10    as_datetime_with_timezone_and_data_type, as_duration, as_time, as_time_with_data_type,
11};
12use crate::timezone::Tz;
13use crate::trusted_len::trusted_len_unzip;
14use crate::types::*;
15use crate::{Array, ArrayAccessor, ArrayRef, Scalar};
16use arrow_buffer::{ArrowNativeType, Buffer, NullBuffer, NullBufferBuilder, ScalarBuffer, i256};
17use arrow_data::bit_iterator::try_for_each_valid_idx;
18use arrow_data::{ArrayData, ArrayDataBuilder};
19use arrow_schema::{ArrowError, DataType};
20use chrono::{DateTime, Duration, NaiveDate, NaiveDateTime, NaiveTime};
21use half::f16;
22use std::any::Any;
23use std::sync::Arc;
24
25/// A [`PrimitiveArray`] of `i8`
26///
27/// # Examples
28///
29/// Construction
30///
31/// ```
32/// # use arrow_array::Int8Array;
33/// // Create from Vec<Option<i8>>
34/// let arr = Int8Array::from(vec![Some(1), None, Some(2)]);
35/// // Create from Vec<i8>
36/// let arr = Int8Array::from(vec![1, 2, 3]);
37/// // Create iter/collect
38/// let arr: Int8Array = std::iter::repeat(42).take(10).collect();
39/// ```
40///
41/// See [`PrimitiveArray`] for more information and examples
42pub type Int8Array = PrimitiveArray<Int8Type>;
43
44/// A [`PrimitiveArray`] of `i16`
45///
46/// # Examples
47///
48/// Construction
49///
50/// ```
51/// # use arrow_array::Int16Array;
52/// // Create from Vec<Option<i16>>
53/// let arr = Int16Array::from(vec![Some(1), None, Some(2)]);
54/// // Create from Vec<i16>
55/// let arr = Int16Array::from(vec![1, 2, 3]);
56/// // Create iter/collect
57/// let arr: Int16Array = std::iter::repeat(42).take(10).collect();
58/// ```
59///
60/// See [`PrimitiveArray`] for more information and examples
61pub type Int16Array = PrimitiveArray<Int16Type>;
62
63/// A [`PrimitiveArray`] of `i32`
64///
65/// # Examples
66///
67/// Construction
68///
69/// ```
70/// # use arrow_array::Int32Array;
71/// // Create from Vec<Option<i32>>
72/// let arr = Int32Array::from(vec![Some(1), None, Some(2)]);
73/// // Create from Vec<i32>
74/// let arr = Int32Array::from(vec![1, 2, 3]);
75/// // Create iter/collect
76/// let arr: Int32Array = std::iter::repeat(42).take(10).collect();
77/// ```
78///
79/// See [`PrimitiveArray`] for more information and examples
80pub type Int32Array = PrimitiveArray<Int32Type>;
81
82/// A [`PrimitiveArray`] of `i64`
83///
84/// # Examples
85///
86/// Construction
87///
88/// ```
89/// # use arrow_array::Int64Array;
90/// // Create from Vec<Option<i64>>
91/// let arr = Int64Array::from(vec![Some(1), None, Some(2)]);
92/// // Create from Vec<i64>
93/// let arr = Int64Array::from(vec![1, 2, 3]);
94/// // Create iter/collect
95/// let arr: Int64Array = std::iter::repeat(42).take(10).collect();
96/// ```
97///
98/// See [`PrimitiveArray`] for more information and examples
99pub type Int64Array = PrimitiveArray<Int64Type>;
100
101/// A [`PrimitiveArray`] of `u8`
102///
103/// # Examples
104///
105/// Construction
106///
107/// ```
108/// # use arrow_array::UInt8Array;
109/// // Create from Vec<Option<u8>>
110/// let arr = UInt8Array::from(vec![Some(1), None, Some(2)]);
111/// // Create from Vec<u8>
112/// let arr = UInt8Array::from(vec![1, 2, 3]);
113/// // Create iter/collect
114/// let arr: UInt8Array = std::iter::repeat(42).take(10).collect();
115/// ```
116///
117/// See [`PrimitiveArray`] for more information and examples
118pub type UInt8Array = PrimitiveArray<UInt8Type>;
119
120/// A [`PrimitiveArray`] of `u16`
121///
122/// # Examples
123///
124/// Construction
125///
126/// ```
127/// # use arrow_array::UInt16Array;
128/// // Create from Vec<Option<u16>>
129/// let arr = UInt16Array::from(vec![Some(1), None, Some(2)]);
130/// // Create from Vec<u16>
131/// let arr = UInt16Array::from(vec![1, 2, 3]);
132/// // Create iter/collect
133/// let arr: UInt16Array = std::iter::repeat(42).take(10).collect();
134/// ```
135///
136/// See [`PrimitiveArray`] for more information and examples
137pub type UInt16Array = PrimitiveArray<UInt16Type>;
138
139/// A [`PrimitiveArray`] of `u32`
140///
141/// # Examples
142///
143/// Construction
144///
145/// ```
146/// # use arrow_array::UInt32Array;
147/// // Create from Vec<Option<u32>>
148/// let arr = UInt32Array::from(vec![Some(1), None, Some(2)]);
149/// // Create from Vec<u32>
150/// let arr = UInt32Array::from(vec![1, 2, 3]);
151/// // Create iter/collect
152/// let arr: UInt32Array = std::iter::repeat(42).take(10).collect();
153/// ```
154///
155/// See [`PrimitiveArray`] for more information and examples
156pub type UInt32Array = PrimitiveArray<UInt32Type>;
157
158/// A [`PrimitiveArray`] of `u64`
159///
160/// # Examples
161///
162/// Construction
163///
164/// ```
165/// # use arrow_array::UInt64Array;
166/// // Create from Vec<Option<u64>>
167/// let arr = UInt64Array::from(vec![Some(1), None, Some(2)]);
168/// // Create from Vec<u64>
169/// let arr = UInt64Array::from(vec![1, 2, 3]);
170/// // Create iter/collect
171/// let arr: UInt64Array = std::iter::repeat(42).take(10).collect();
172/// ```
173///
174/// See [`PrimitiveArray`] for more information and examples
175pub type UInt64Array = PrimitiveArray<UInt64Type>;
176
177/// A [`PrimitiveArray`] of `f16`
178///
179/// # Examples
180///
181/// Construction
182///
183/// ```
184/// # use arrow_array::Float16Array;
185/// use half::f16;
186/// // Create from Vec<Option<f16>>
187/// let arr = Float16Array::from(vec![Some(f16::from_f64(1.0)), Some(f16::from_f64(2.0))]);
188/// // Create from Vec<i8>
189/// let arr = Float16Array::from(vec![f16::from_f64(1.0), f16::from_f64(2.0), f16::from_f64(3.0)]);
190/// // Create iter/collect
191/// let arr: Float16Array = std::iter::repeat(f16::from_f64(1.0)).take(10).collect();
192/// ```
193///
194/// # Example: Using `collect`
195/// ```
196/// # use arrow_array::Float16Array;
197/// use half::f16;
198/// let arr : Float16Array = [Some(f16::from_f64(1.0)), Some(f16::from_f64(2.0))].into_iter().collect();
199/// ```
200///
201/// See [`PrimitiveArray`] for more information and examples
202pub type Float16Array = PrimitiveArray<Float16Type>;
203
204/// A [`PrimitiveArray`] of `f32`
205///
206/// # Examples
207///
208/// Construction
209///
210/// ```
211/// # use arrow_array::Float32Array;
212/// // Create from Vec<Option<f32>>
213/// let arr = Float32Array::from(vec![Some(1.0), None, Some(2.0)]);
214/// // Create from Vec<f32>
215/// let arr = Float32Array::from(vec![1.0, 2.0, 3.0]);
216/// // Create iter/collect
217/// let arr: Float32Array = std::iter::repeat(42.0).take(10).collect();
218/// ```
219///
220/// See [`PrimitiveArray`] for more information and examples
221pub type Float32Array = PrimitiveArray<Float32Type>;
222
223/// A [`PrimitiveArray`] of `f64`
224///
225/// # Examples
226///
227/// Construction
228///
229/// ```
230/// # use arrow_array::Float64Array;
231/// // Create from Vec<Option<f32>>
232/// let arr = Float64Array::from(vec![Some(1.0), None, Some(2.0)]);
233/// // Create from Vec<f32>
234/// let arr = Float64Array::from(vec![1.0, 2.0, 3.0]);
235/// // Create iter/collect
236/// let arr: Float64Array = std::iter::repeat(42.0).take(10).collect();
237/// ```
238///
239/// See [`PrimitiveArray`] for more information and examples
240pub type Float64Array = PrimitiveArray<Float64Type>;
241
242/// A [`PrimitiveArray`] of seconds since UNIX epoch stored as `i64`
243///
244/// This type is similar to the [`chrono::DateTime`] type and can hold
245/// values such as `1970-05-09 14:25:11 +01:00`
246///
247/// See also [`Timestamp`](arrow_schema::DataType::Timestamp).
248///
249/// # Example: UTC timestamps post epoch
250/// ```
251/// # use arrow_array::TimestampSecondArray;
252/// use arrow_array::timezone::Tz;
253/// // Corresponds to single element array with entry 1970-05-09T14:25:11+0:00
254/// let arr = TimestampSecondArray::from(vec![11111111]);
255/// // OR
256/// let arr = TimestampSecondArray::from(vec![Some(11111111)]);
257/// let utc_tz: Tz = "+00:00".parse().unwrap();
258///
259/// assert_eq!(arr.value_as_datetime_with_tz(0, utc_tz).map(|v| v.to_string()).unwrap(), "1970-05-09 14:25:11 +00:00")
260/// ```
261///
262/// # Example: UTC timestamps pre epoch
263/// ```
264/// # use arrow_array::TimestampSecondArray;
265/// use arrow_array::timezone::Tz;
266/// // Corresponds to single element array with entry 1969-08-25T09:34:49+0:00
267/// let arr = TimestampSecondArray::from(vec![-11111111]);
268/// // OR
269/// let arr = TimestampSecondArray::from(vec![Some(-11111111)]);
270/// let utc_tz: Tz = "+00:00".parse().unwrap();
271///
272/// assert_eq!(arr.value_as_datetime_with_tz(0, utc_tz).map(|v| v.to_string()).unwrap(), "1969-08-25 09:34:49 +00:00")
273/// ```
274///
275/// # Example: With timezone specified
276/// ```
277/// # use arrow_array::TimestampSecondArray;
278/// use arrow_array::timezone::Tz;
279/// // Corresponds to single element array with entry 1970-05-10T00:25:11+10:00
280/// let arr = TimestampSecondArray::from(vec![11111111]).with_timezone("+10:00".to_string());
281/// // OR
282/// let arr = TimestampSecondArray::from(vec![Some(11111111)]).with_timezone("+10:00".to_string());
283/// let sydney_tz: Tz = "+10:00".parse().unwrap();
284///
285/// assert_eq!(arr.value_as_datetime_with_tz(0, sydney_tz).map(|v| v.to_string()).unwrap(), "1970-05-10 00:25:11 +10:00")
286/// ```
287///
288/// See [`PrimitiveArray`] for more information and examples
289pub type TimestampSecondArray = PrimitiveArray<TimestampSecondType>;
290
291/// A [`PrimitiveArray`] of milliseconds since UNIX epoch stored as `i64`
292///
293/// See examples for [`TimestampSecondArray`]
294pub type TimestampMillisecondArray = PrimitiveArray<TimestampMillisecondType>;
295
296/// A [`PrimitiveArray`] of microseconds since UNIX epoch stored as `i64`
297///
298/// See examples for [`TimestampSecondArray`]
299pub type TimestampMicrosecondArray = PrimitiveArray<TimestampMicrosecondType>;
300
301/// A [`PrimitiveArray`] of nanoseconds since UNIX epoch stored as `i64`
302///
303/// See examples for [`TimestampSecondArray`]
304pub type TimestampNanosecondArray = PrimitiveArray<TimestampNanosecondType>;
305
306/// A [`PrimitiveArray`] of days since UNIX epoch stored as `i32`
307///
308/// This type is similar to the [`chrono::NaiveDate`] type and can hold
309/// values such as `2018-11-13`
310pub type Date32Array = PrimitiveArray<Date32Type>;
311
312/// A [`PrimitiveArray`] of milliseconds since UNIX epoch stored as `i64`
313///
314/// This type is similar to the [`chrono::NaiveDate`] type and can hold
315/// values such as `2018-11-13`
316pub type Date64Array = PrimitiveArray<Date64Type>;
317
318/// A [`PrimitiveArray`] of seconds since midnight stored as `i32`
319///
320/// This type is similar to the [`chrono::NaiveTime`] type and can
321/// hold values such as `00:02:00`
322pub type Time32SecondArray = PrimitiveArray<Time32SecondType>;
323
324/// A [`PrimitiveArray`] of milliseconds since midnight stored as `i32`
325///
326/// This type is similar to the [`chrono::NaiveTime`] type and can
327/// hold values such as `00:02:00.123`
328pub type Time32MillisecondArray = PrimitiveArray<Time32MillisecondType>;
329
330/// A [`PrimitiveArray`] of microseconds since midnight stored as `i64`
331///
332/// This type is similar to the [`chrono::NaiveTime`] type and can
333/// hold values such as `00:02:00.123456`
334pub type Time64MicrosecondArray = PrimitiveArray<Time64MicrosecondType>;
335
336/// A [`PrimitiveArray`] of nanoseconds since midnight stored as `i64`
337///
338/// This type is similar to the [`chrono::NaiveTime`] type and can
339/// hold values such as `00:02:00.123456789`
340pub type Time64NanosecondArray = PrimitiveArray<Time64NanosecondType>;
341
342/// A [`PrimitiveArray`] of “calendar” intervals in whole months
343///
344/// See [`IntervalYearMonthType`] for details on representation and caveats.
345///
346/// # Example
347/// ```
348/// # use arrow_array::IntervalYearMonthArray;
349/// let array = IntervalYearMonthArray::from(vec![
350///   2,  // 2 months
351///   25, // 2 years and 1 month
352///   -1  // -1 months
353/// ]);
354/// ```
355pub type IntervalYearMonthArray = PrimitiveArray<IntervalYearMonthType>;
356
357/// A [`PrimitiveArray`] of “calendar” intervals in days and milliseconds
358///
359/// See [`IntervalDayTime`] for details on representation and caveats.
360///
361/// # Example
362/// ```
363/// # use arrow_array::IntervalDayTimeArray;
364/// use arrow_array::types::IntervalDayTime;
365/// let array = IntervalDayTimeArray::from(vec![
366///   IntervalDayTime::new(1, 1000),                 // 1 day, 1000 milliseconds
367///   IntervalDayTime::new(33, 0),                  // 33 days, 0 milliseconds
368///   IntervalDayTime::new(0, 12 * 60 * 60 * 1000), // 0 days, 12 hours
369/// ]);
370/// ```
371pub type IntervalDayTimeArray = PrimitiveArray<IntervalDayTimeType>;
372
373/// A [`PrimitiveArray`] of “calendar” intervals in  months, days, and nanoseconds.
374///
375/// See [`IntervalMonthDayNano`] for details on representation and caveats.
376///
377/// # Example
378/// ```
379/// # use arrow_array::IntervalMonthDayNanoArray;
380/// use arrow_array::types::IntervalMonthDayNano;
381/// let array = IntervalMonthDayNanoArray::from(vec![
382///   IntervalMonthDayNano::new(1, 2, 1000),             // 1 month, 2 days, 1 nanosecond
383///   IntervalMonthDayNano::new(12, 1, 0),               // 12 months, 1 days, 0 nanoseconds
384///   IntervalMonthDayNano::new(0, 0, 12 * 1000 * 1000), // 0 days, 12 milliseconds
385/// ]);
386/// ```
387pub type IntervalMonthDayNanoArray = PrimitiveArray<IntervalMonthDayNanoType>;
388
389/// A [`PrimitiveArray`] of elapsed durations in seconds
390pub type DurationSecondArray = PrimitiveArray<DurationSecondType>;
391
392/// A [`PrimitiveArray`] of elapsed durations in milliseconds
393pub type DurationMillisecondArray = PrimitiveArray<DurationMillisecondType>;
394
395/// A [`PrimitiveArray`] of elapsed durations in microseconds
396pub type DurationMicrosecondArray = PrimitiveArray<DurationMicrosecondType>;
397
398/// A [`PrimitiveArray`] of elapsed durations in nanoseconds
399pub type DurationNanosecondArray = PrimitiveArray<DurationNanosecondType>;
400
401/// A [`PrimitiveArray`] of 32-bit fixed point decimals
402///
403/// # Examples
404///
405/// Construction
406///
407/// ```
408/// # use arrow_array::Decimal32Array;
409/// // Create from Vec<Option<i32>>
410/// let arr = Decimal32Array::from(vec![Some(1), None, Some(2)]);
411/// // Create from Vec<i32>
412/// let arr = Decimal32Array::from(vec![1, 2, 3]);
413/// // Create iter/collect
414/// let arr: Decimal32Array = std::iter::repeat(42).take(10).collect();
415/// ```
416///
417/// See [`PrimitiveArray`] for more information and examples
418pub type Decimal32Array = PrimitiveArray<Decimal32Type>;
419
420/// A [`PrimitiveArray`] of 64-bit fixed point decimals
421///
422/// # Examples
423///
424/// Construction
425///
426/// ```
427/// # use arrow_array::Decimal64Array;
428/// // Create from Vec<Option<i64>>
429/// let arr = Decimal64Array::from(vec![Some(1), None, Some(2)]);
430/// // Create from Vec<i64>
431/// let arr = Decimal64Array::from(vec![1, 2, 3]);
432/// // Create iter/collect
433/// let arr: Decimal64Array = std::iter::repeat(42).take(10).collect();
434/// ```
435///
436/// See [`PrimitiveArray`] for more information and examples
437pub type Decimal64Array = PrimitiveArray<Decimal64Type>;
438
439/// A [`PrimitiveArray`] of 128-bit fixed point decimals
440///
441/// # Examples
442///
443/// Construction
444///
445/// ```
446/// # use arrow_array::Decimal128Array;
447/// // Create from Vec<Option<i128>>
448/// let arr = Decimal128Array::from(vec![Some(1), None, Some(2)]);
449/// // Create from Vec<i128>
450/// let arr = Decimal128Array::from(vec![1, 2, 3]);
451/// // Create iter/collect
452/// let arr: Decimal128Array = std::iter::repeat(42).take(10).collect();
453/// ```
454///
455/// See [`PrimitiveArray`] for more information and examples
456pub type Decimal128Array = PrimitiveArray<Decimal128Type>;
457
458/// A [`PrimitiveArray`] of 256-bit fixed point decimals
459///
460/// # Examples
461///
462/// Construction
463///
464/// ```
465/// # use arrow_array::Decimal256Array;
466/// use arrow_buffer::i256;
467/// // Create from Vec<Option<i256>>
468/// let arr = Decimal256Array::from(vec![Some(i256::from(1)), None, Some(i256::from(2))]);
469/// // Create from Vec<i256>
470/// let arr = Decimal256Array::from(vec![i256::from(1), i256::from(2), i256::from(3)]);
471/// // Create iter/collect
472/// let arr: Decimal256Array = std::iter::repeat(i256::from(42)).take(10).collect();
473/// ```
474///
475/// See [`PrimitiveArray`] for more information and examples
476pub type Decimal256Array = PrimitiveArray<Decimal256Type>;
477
478pub use crate::types::ArrowPrimitiveType;
479
480/// An array of primitive values, of type [`ArrowPrimitiveType`]
481///
482/// # Example: From a Vec
483///
484/// *Note*: Converting a `Vec` to a `PrimitiveArray` does not copy the data.
485/// The new `PrimitiveArray` uses the same underlying allocation from the `Vec`.
486///
487/// ```
488/// # use arrow_array::{Array, PrimitiveArray, types::Int32Type};
489/// let arr: PrimitiveArray<Int32Type> = vec![1, 2, 3, 4].into();
490/// assert_eq!(4, arr.len());
491/// assert_eq!(0, arr.null_count());
492/// assert_eq!(arr.values(), &[1, 2, 3, 4])
493/// ```
494///
495/// # Example: To a `Vec<T>`
496///
497/// *Note*: In some cases, converting `PrimitiveArray` to a `Vec` is zero-copy
498/// and does not copy the data (see [`Buffer::into_vec`] for conditions). In
499/// such cases, the `Vec` will use the same underlying memory allocation from
500/// the `PrimitiveArray`.
501///
502/// The Rust compiler generates highly optimized code for operations on
503/// Vec, so using a Vec can often be faster than using a PrimitiveArray directly.
504///
505/// ```
506/// # use arrow_array::{Array, PrimitiveArray, types::Int32Type};
507/// let arr = PrimitiveArray::<Int32Type>::from(vec![1, 2, 3, 4]);
508/// let starting_ptr = arr.values().as_ptr();
509/// // split into its parts
510/// let (datatype, buffer, nulls) = arr.into_parts();
511/// // Convert the buffer to a Vec<i32> (zero copy)
512/// // (note this requires that there are no other references)
513/// let mut vec: Vec<i32> = buffer.into();
514/// vec[2] = 300;
515/// // put the parts back together
516/// let arr = PrimitiveArray::<Int32Type>::try_new(vec.into(), nulls).unwrap();
517/// assert_eq!(arr.values(), &[1, 2, 300, 4]);
518/// // The same allocation was used
519/// assert_eq!(starting_ptr, arr.values().as_ptr());
520/// ```
521///
522/// # Example: From an optional Vec
523///
524/// ```
525/// # use arrow_array::{Array, PrimitiveArray, types::Int32Type};
526/// let arr: PrimitiveArray<Int32Type> = vec![Some(1), None, Some(3), None].into();
527/// assert_eq!(4, arr.len());
528/// assert_eq!(2, arr.null_count());
529/// // Note: values for null indexes are arbitrary
530/// assert_eq!(arr.values(), &[1, 0, 3, 0])
531/// ```
532///
533/// # Example: From an iterator of values
534///
535/// ```
536/// # use arrow_array::{Array, PrimitiveArray, types::Int32Type};
537/// let arr: PrimitiveArray<Int32Type> = (0..10).map(|x| x + 1).collect();
538/// assert_eq!(10, arr.len());
539/// assert_eq!(0, arr.null_count());
540/// for i in 0..10i32 {
541///     assert_eq!(i + 1, arr.value(i as usize));
542/// }
543/// ```
544///
545/// # Example: From an iterator of option
546///
547/// ```
548/// # use arrow_array::{Array, PrimitiveArray, types::Int32Type};
549/// let arr: PrimitiveArray<Int32Type> = (0..10).map(|x| (x % 2 == 0).then_some(x)).collect();
550/// assert_eq!(10, arr.len());
551/// assert_eq!(5, arr.null_count());
552/// // Note: values for null indexes are arbitrary
553/// assert_eq!(arr.values(), &[0, 0, 2, 0, 4, 0, 6, 0, 8, 0])
554/// ```
555///
556/// # Example: Using Builder
557///
558/// ```
559/// # use arrow_array::Array;
560/// # use arrow_array::builder::PrimitiveBuilder;
561/// # use arrow_array::types::Int32Type;
562/// let mut builder = PrimitiveBuilder::<Int32Type>::new();
563/// builder.append_value(1);
564/// builder.append_null();
565/// builder.append_value(2);
566/// let array = builder.finish();
567/// // Note: values for null indexes are arbitrary
568/// assert_eq!(array.values(), &[1, 0, 2]);
569/// assert!(array.is_null(1));
570/// ```
571///
572/// # Performance: Choosing Between `from` and [`PrimitiveBuilder`]
573///
574/// Rust's `Vec` is highly optimized, and Arrow's conversion from `Vec` to
575/// `PrimitiveArray` is zero-copy. Prefer [`PrimitiveArray::from`] or
576/// [`PrimitiveArray::new`] whenever values are already in a `Vec` or can be collected
577/// into one. [`PrimitiveBuilder`] is backed by a `Vec` and a `NullBufferBuilder`
578/// internally, so it offers no performance advantage. Use it when the array may
579/// contain nulls whose positions aren't known upfront, since it keeps values and the
580/// null bitmask in sync automatically.
581///
582/// # Example: Get a `PrimitiveArray` from an [`ArrayRef`]
583/// ```
584/// # use std::sync::Arc;
585/// # use arrow_array::{Array, cast::AsArray, ArrayRef, Float32Array, PrimitiveArray};
586/// # use arrow_array::types::{Float32Type};
587/// # use arrow_schema::DataType;
588/// # let array: ArrayRef =  Arc::new(Float32Array::from(vec![1.2, 2.3]));
589/// // will panic if the array is not a Float32Array
590/// assert_eq!(&DataType::Float32, array.data_type());
591/// let f32_array: Float32Array  = array.as_primitive().clone();
592/// assert_eq!(f32_array, Float32Array::from(vec![1.2, 2.3]));
593/// ```
594pub struct PrimitiveArray<T: ArrowPrimitiveType> {
595    data_type: DataType,
596    /// Values data
597    values: ScalarBuffer<T::Native>,
598    nulls: Option<NullBuffer>,
599}
600
601impl<T: ArrowPrimitiveType> Clone for PrimitiveArray<T> {
602    fn clone(&self) -> Self {
603        Self {
604            data_type: self.data_type.clone(),
605            values: self.values.clone(),
606            nulls: self.nulls.clone(),
607        }
608    }
609}
610
611impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
612    /// Create a new [`PrimitiveArray`] from the provided values and nulls
613    ///
614    /// # Panics
615    ///
616    /// Panics if [`Self::try_new`] returns an error
617    ///
618    /// # Example
619    ///
620    /// Creating a [`PrimitiveArray`] directly from a [`ScalarBuffer`] and [`NullBuffer`] using
621    /// this constructor is the most performant approach, avoiding any additional allocations
622    ///
623    /// ```
624    /// # use arrow_array::Int32Array;
625    /// # use arrow_array::types::Int32Type;
626    /// # use arrow_buffer::NullBuffer;
627    /// // [1, 2, 3, 4]
628    /// let array = Int32Array::new(vec![1, 2, 3, 4].into(), None);
629    /// // [1, null, 3, 4]
630    /// let nulls = NullBuffer::from(vec![true, false, true, true]);
631    /// let array = Int32Array::new(vec![1, 2, 3, 4].into(), Some(nulls));
632    /// ```
633    pub fn new(values: ScalarBuffer<T::Native>, nulls: Option<NullBuffer>) -> Self {
634        Self::try_new(values, nulls).unwrap()
635    }
636
637    /// Create a new [`PrimitiveArray`] from the provided values and nulls without validation.
638    ///
639    /// # Safety
640    /// - `values.len() == nulls.len()` if `nulls` is `Some`
641    pub unsafe fn new_unchecked(
642        values: ScalarBuffer<T::Native>,
643        nulls: Option<NullBuffer>,
644    ) -> Self {
645        if cfg!(feature = "force_validate") {
646            return Self::new(values, nulls);
647        }
648        Self {
649            data_type: T::DATA_TYPE,
650            values,
651            nulls,
652        }
653    }
654
655    /// Create a new [`PrimitiveArray`] of the given length where all values are null
656    pub fn new_null(length: usize) -> Self {
657        Self {
658            data_type: T::DATA_TYPE,
659            values: vec![T::Native::usize_as(0); length].into(),
660            nulls: Some(NullBuffer::new_null(length)),
661        }
662    }
663
664    /// Create a new [`PrimitiveArray`] from the provided values and nulls
665    ///
666    /// # Errors
667    ///
668    /// Errors if:
669    /// - `values.len() != nulls.len()`
670    pub fn try_new(
671        values: ScalarBuffer<T::Native>,
672        nulls: Option<NullBuffer>,
673    ) -> Result<Self, ArrowError> {
674        if let Some(n) = nulls.as_ref()
675            && n.len() != values.len()
676        {
677            return Err(ArrowError::InvalidArgumentError(format!(
678                "Incorrect length of null buffer for PrimitiveArray, expected {} got {}",
679                values.len(),
680                n.len(),
681            )));
682        }
683
684        Ok(Self {
685            data_type: T::DATA_TYPE,
686            values,
687            nulls,
688        })
689    }
690
691    /// Create a new [`Scalar`] from `value`
692    pub fn new_scalar(value: T::Native) -> Scalar<Self> {
693        Scalar::new(Self {
694            data_type: T::DATA_TYPE,
695            values: vec![value].into(),
696            nulls: None,
697        })
698    }
699
700    /// Deconstruct this array into its constituent parts
701    pub fn into_parts(self) -> (DataType, ScalarBuffer<T::Native>, Option<NullBuffer>) {
702        (self.data_type, self.values, self.nulls)
703    }
704
705    /// Overrides the [`DataType`] of this [`PrimitiveArray`]
706    ///
707    /// Prefer using [`Self::with_timezone`] or [`Self::with_precision_and_scale`] where
708    /// the primitive type is suitably constrained, as these cannot panic
709    ///
710    /// # Panics
711    ///
712    /// Panics if ![Self::is_compatible]
713    pub fn with_data_type(self, data_type: DataType) -> Self {
714        Self::assert_compatible(&data_type);
715        Self { data_type, ..self }
716    }
717
718    /// Asserts that `data_type` is compatible with `Self`
719    fn assert_compatible(data_type: &DataType) {
720        assert!(
721            Self::is_compatible(data_type),
722            "PrimitiveArray expected data type {} got {}",
723            T::DATA_TYPE,
724            data_type
725        );
726    }
727
728    /// Returns the length of this array.
729    #[inline]
730    pub fn len(&self) -> usize {
731        self.values.len()
732    }
733
734    /// Returns whether this array is empty.
735    pub fn is_empty(&self) -> bool {
736        self.values.is_empty()
737    }
738
739    /// Returns the values of this array
740    #[inline]
741    pub fn values(&self) -> &ScalarBuffer<T::Native> {
742        &self.values
743    }
744
745    /// Returns a new primitive array builder
746    pub fn builder(capacity: usize) -> PrimitiveBuilder<T> {
747        PrimitiveBuilder::<T>::with_capacity(capacity)
748    }
749
750    /// Returns if this [`PrimitiveArray`] is compatible with the provided [`DataType`]
751    ///
752    /// This is equivalent to `data_type == T::DATA_TYPE`, however ignores timestamp
753    /// timezones and decimal precision and scale
754    pub fn is_compatible(data_type: &DataType) -> bool {
755        match T::DATA_TYPE {
756            DataType::Timestamp(t1, _) => {
757                matches!(data_type, DataType::Timestamp(t2, _) if &t1 == t2)
758            }
759            DataType::Decimal32(_, _) => matches!(data_type, DataType::Decimal32(_, _)),
760            DataType::Decimal64(_, _) => matches!(data_type, DataType::Decimal64(_, _)),
761            DataType::Decimal128(_, _) => matches!(data_type, DataType::Decimal128(_, _)),
762            DataType::Decimal256(_, _) => matches!(data_type, DataType::Decimal256(_, _)),
763            _ => T::DATA_TYPE.eq(data_type),
764        }
765    }
766
767    /// Returns the primitive value at index `i`.
768    ///
769    /// Note: This method does not check for nulls and the value is arbitrary
770    /// if [`is_null`](Self::is_null) returns true for the index.
771    ///
772    /// # Safety
773    ///
774    /// caller must ensure that the passed in offset is less than the array len()
775    #[inline]
776    pub unsafe fn value_unchecked(&self, i: usize) -> T::Native {
777        unsafe { *self.values.get_unchecked(i) }
778    }
779
780    /// Returns the primitive value at index `i`.
781    ///
782    /// Note: This method does not check for nulls and the value is arbitrary
783    /// if [`is_null`](Self::is_null) returns true for the index.
784    ///
785    /// # Panics
786    /// Panics if index `i` is out of bounds
787    #[inline]
788    pub fn value(&self, i: usize) -> T::Native {
789        assert!(
790            i < self.len(),
791            "Trying to access an element at index {} from a PrimitiveArray of length {}",
792            i,
793            self.len()
794        );
795        unsafe { self.value_unchecked(i) }
796    }
797
798    /// Creates a PrimitiveArray based on an iterator of values without nulls
799    pub fn from_iter_values<I: IntoIterator<Item = T::Native>>(iter: I) -> Self {
800        let val_buf: Buffer = iter.into_iter().collect();
801        let len = val_buf.len() / std::mem::size_of::<T::Native>();
802        Self {
803            data_type: T::DATA_TYPE,
804            values: ScalarBuffer::new(val_buf, 0, len),
805            nulls: None,
806        }
807    }
808
809    /// Creates a PrimitiveArray based on an iterator of values with provided nulls
810    pub fn from_iter_values_with_nulls<I: IntoIterator<Item = T::Native>>(
811        iter: I,
812        nulls: Option<NullBuffer>,
813    ) -> Self {
814        let val_buf: Buffer = iter.into_iter().collect();
815        let len = val_buf.len() / std::mem::size_of::<T::Native>();
816        Self {
817            data_type: T::DATA_TYPE,
818            values: ScalarBuffer::new(val_buf, 0, len),
819            nulls,
820        }
821    }
822
823    /// Creates a PrimitiveArray based on a constant value with `count` elements
824    pub fn from_value(value: T::Native, count: usize) -> Self {
825        let val_buf: Vec<_> = vec![value; count];
826        Self::new(val_buf.into(), None)
827    }
828
829    /// Returns an iterator that returns the values of `array.value(i)` for an iterator with each element `i`
830    pub fn take_iter<'a>(
831        &'a self,
832        indexes: impl Iterator<Item = Option<usize>> + 'a,
833    ) -> impl Iterator<Item = Option<T::Native>> + 'a {
834        indexes.map(|opt_index| opt_index.map(|index| self.value(index)))
835    }
836
837    /// Returns an iterator that returns the values of `array.value(i)` for an iterator with each element `i`
838    /// # Safety
839    ///
840    /// caller must ensure that the offsets in the iterator are less than the array len()
841    pub unsafe fn take_iter_unchecked<'a>(
842        &'a self,
843        indexes: impl Iterator<Item = Option<usize>> + 'a,
844    ) -> impl Iterator<Item = Option<T::Native>> + 'a {
845        indexes.map(|opt_index| opt_index.map(|index| unsafe { self.value_unchecked(index) }))
846    }
847
848    /// Returns a zero-copy slice of this array with the indicated offset and length.
849    ///
850    /// # Panics
851    /// Panics if `offset + length > self.len()`
852    pub fn slice(&self, offset: usize, length: usize) -> Self {
853        Self {
854            data_type: self.data_type.clone(),
855            values: self.values.slice(offset, length),
856            nulls: self.nulls.as_ref().map(|n| n.slice(offset, length)),
857        }
858    }
859
860    /// Reinterprets this array's contents as a different data type without copying
861    ///
862    /// This can be used to efficiently convert between primitive arrays with the
863    /// same underlying representation
864    ///
865    /// Note: this will not modify the underlying values, and therefore may change
866    /// the semantic values of the array, e.g. 100 milliseconds in a [`TimestampNanosecondArray`]
867    /// will become 100 seconds in a [`TimestampSecondArray`].
868    ///
869    /// For casts that preserve the semantic value, check out the
870    /// [compute kernels](https://docs.rs/arrow/latest/arrow/compute/kernels/cast/index.html).
871    ///
872    /// ```
873    /// # use arrow_array::{Int64Array, TimestampNanosecondArray};
874    /// let a = Int64Array::from_iter_values([1, 2, 3, 4]);
875    /// let b: TimestampNanosecondArray = a.reinterpret_cast();
876    /// ```
877    pub fn reinterpret_cast<K>(&self) -> PrimitiveArray<K>
878    where
879        K: ArrowPrimitiveType<Native = T::Native>,
880    {
881        PrimitiveArray::new(self.values.clone(), self.nulls.clone())
882    }
883
884    /// Applies a unary infallible function to a primitive array, producing a
885    /// new array of potentially different type.
886    ///
887    /// This is the fastest way to perform an operation on a primitive array
888    /// when the benefits of a vectorized operation outweigh the cost of
889    /// branching nulls and non-nulls.
890    ///
891    /// See also
892    /// * [`Self::unary_mut`] for in place modification.
893    /// * [`Self::try_unary`] for fallible operations.
894    /// * [`arrow::compute::binary`] for binary operations
895    ///
896    /// [`arrow::compute::binary`]: https://docs.rs/arrow/latest/arrow/compute/fn.binary.html
897    /// # Null Handling
898    ///
899    /// Applies the function for all values, including those on null slots. This
900    /// will often allow the compiler to generate faster vectorized code, but
901    /// requires that the operation must be infallible (not error/panic) for any
902    /// value of the corresponding type or this function may panic.
903    ///
904    /// # Example
905    /// ```rust
906    /// # use arrow_array::{Int32Array, Float32Array, types::Int32Type};
907    /// # fn main() {
908    /// let array = Int32Array::from(vec![Some(5), Some(7), None]);
909    /// // Create a new array with the value of applying sqrt
910    /// let c = array.unary(|x| f32::sqrt(x as f32));
911    /// assert_eq!(c, Float32Array::from(vec![Some(2.236068), Some(2.6457512), None]));
912    /// # }
913    /// ```
914    pub fn unary<F, O>(&self, op: F) -> PrimitiveArray<O>
915    where
916        O: ArrowPrimitiveType,
917        F: Fn(T::Native) -> O::Native,
918    {
919        let nulls = self.nulls().cloned();
920        // `move` puts `op`'s captures in the iterator rather than behind a reference, so the
921        // loop doesn't reload them for each value when `collect` isn't inlined
922        let values = self.values().into_iter().map(move |v| op(*v));
923        let buffer: Vec<_> = values.collect();
924        PrimitiveArray::new(buffer.into(), nulls)
925    }
926
927    /// Applies a unary and infallible function to the array in place if possible.
928    ///
929    /// # Buffer Reuse
930    ///
931    /// If the underlying buffers are not shared with other arrays,  mutates the
932    /// underlying buffer in place, without allocating.
933    ///
934    /// If the underlying buffer is shared, returns Err(self)
935    ///
936    /// # Null Handling
937    ///
938    /// See [`Self::unary`] for more information on null handling.
939    ///
940    /// # Example
941    ///
942    /// ```rust
943    /// # use arrow_array::{Int32Array, types::Int32Type};
944    /// let array = Int32Array::from(vec![Some(5), Some(7), None]);
945    /// // Apply x*2+1 to the data in place, no allocations
946    /// let c = array.unary_mut(|x| x * 2 + 1).unwrap();
947    /// assert_eq!(c, Int32Array::from(vec![Some(11), Some(15), None]));
948    /// ```
949    ///
950    /// # Example: modify [`ArrayRef`] in place, if not shared
951    ///
952    /// It is also possible to modify an [`ArrayRef`] if there are no other
953    /// references to the underlying buffer.
954    ///
955    /// ```rust
956    /// # use std::sync::Arc;
957    /// # use arrow_array::{Array, cast::AsArray, ArrayRef, Int32Array, PrimitiveArray, types::Int32Type};
958    /// # let array: ArrayRef = Arc::new(Int32Array::from(vec![Some(5), Some(7), None]));
959    /// // Convert to Int32Array (panic's if array.data_type is not Int32)
960    /// let a = array.as_primitive::<Int32Type>().clone();
961    /// // Try to apply x*2+1 to the data in place, fails because array is still shared
962    /// a.unary_mut(|x| x * 2 + 1).unwrap_err();
963    /// // Try again, this time dropping the last remaining reference
964    /// let a = array.as_primitive::<Int32Type>().clone();
965    /// drop(array);
966    /// // Now we can apply the operation in place
967    /// let c = a.unary_mut(|x| x * 2 + 1).unwrap();
968    /// assert_eq!(c, Int32Array::from(vec![Some(11), Some(15), None]));
969    /// ```
970    pub fn unary_mut<F>(self, op: F) -> Result<PrimitiveArray<T>, PrimitiveArray<T>>
971    where
972        F: Fn(T::Native) -> T::Native,
973    {
974        let mut builder = self.into_builder()?;
975        builder
976            .values_slice_mut()
977            .iter_mut()
978            .for_each(|v| *v = op(*v));
979        Ok(builder.finish())
980    }
981
982    /// Applies a unary fallible function to all valid values in a primitive
983    /// array, producing a new array of potentially different type.
984    ///
985    /// Applies `op` to only rows that are valid, which is often significantly
986    /// slower than [`Self::unary`], which should be preferred if `op` is
987    /// fallible.
988    ///
989    /// Note: LLVM is currently unable to effectively vectorize fallible operations
990    pub fn try_unary<F, O, E>(&self, op: F) -> Result<PrimitiveArray<O>, E>
991    where
992        O: ArrowPrimitiveType,
993        F: Fn(T::Native) -> Result<O::Native, E>,
994    {
995        let len = self.len();
996
997        let nulls = self.nulls().cloned();
998        let mut values = vec![O::Native::default(); len];
999        let slice = values.as_mut_slice();
1000
1001        let f = |idx| {
1002            unsafe { *slice.get_unchecked_mut(idx) = op(self.value_unchecked(idx))? };
1003            Ok::<_, E>(())
1004        };
1005
1006        match &nulls {
1007            Some(nulls) => nulls.try_for_each_valid_idx(f)?,
1008            None => (0..len).try_for_each(f)?,
1009        }
1010
1011        let values = values.into();
1012        Ok(PrimitiveArray::new(values, nulls))
1013    }
1014
1015    /// Applies a unary fallible function to all valid values in a mutable
1016    /// primitive array.
1017    ///
1018    /// # Null Handling
1019    ///
1020    /// See [`Self::try_unary`] for more information on null handling.
1021    ///
1022    /// # Buffer Reuse
1023    ///
1024    /// See [`Self::unary_mut`] for more information on buffer reuse.
1025    ///
1026    /// This returns an `Err` when the input array is shared buffer with other
1027    /// array. In the case, returned `Err` wraps input array. If the function
1028    /// encounters an error during applying on values. In the case, this returns an `Err` within
1029    /// an `Ok` which wraps the actual error.
1030    ///
1031    /// Note: LLVM is currently unable to effectively vectorize fallible operations
1032    pub fn try_unary_mut<F, E>(
1033        self,
1034        op: F,
1035    ) -> Result<Result<PrimitiveArray<T>, E>, PrimitiveArray<T>>
1036    where
1037        F: Fn(T::Native) -> Result<T::Native, E>,
1038    {
1039        let len = self.len();
1040        let null_count = self.null_count();
1041        let mut builder = self.into_builder()?;
1042
1043        let (slice, null_buffer) = builder.slices_mut();
1044
1045        let r = try_for_each_valid_idx(len, 0, null_count, null_buffer.as_deref(), |idx| {
1046            unsafe { *slice.get_unchecked_mut(idx) = op(*slice.get_unchecked(idx))? };
1047            Ok::<_, E>(())
1048        });
1049
1050        if let Err(err) = r {
1051            return Ok(Err(err));
1052        }
1053
1054        Ok(Ok(builder.finish()))
1055    }
1056
1057    /// Applies a unary and nullable function to all valid values in a primitive array
1058    ///
1059    /// Applies `op` to only rows that are valid, which is often significantly
1060    /// slower than [`Self::unary`], which should be preferred if `op` is
1061    /// fallible.
1062    ///
1063    /// Note: LLVM is currently unable to effectively vectorize fallible operations
1064    pub fn unary_opt<F, O>(&self, op: F) -> PrimitiveArray<O>
1065    where
1066        O: ArrowPrimitiveType,
1067        F: Fn(T::Native) -> Option<O::Native>,
1068    {
1069        let len = self.len();
1070        let (nulls, null_count, offset) = match self.nulls() {
1071            Some(n) => (Some(n.validity()), n.null_count(), n.offset()),
1072            None => (None, 0, 0),
1073        };
1074
1075        let mut null_builder = BooleanBufferBuilder::new(len);
1076        match nulls {
1077            Some(b) => null_builder.append_packed_range(offset..offset + len, b),
1078            None => null_builder.append_n(len, true),
1079        }
1080
1081        let mut values = vec![O::Native::default(); len];
1082        let slice = values.as_mut_slice();
1083
1084        let mut out_null_count = null_count;
1085
1086        let _ = try_for_each_valid_idx(len, offset, null_count, nulls, |idx| {
1087            match op(unsafe { self.value_unchecked(idx) }) {
1088                Some(v) => unsafe { *slice.get_unchecked_mut(idx) = v },
1089                None => {
1090                    out_null_count += 1;
1091                    null_builder.set_bit(idx, false);
1092                }
1093            }
1094            Ok::<_, ()>(())
1095        });
1096
1097        let nulls = null_builder.finish();
1098        let values = values.into();
1099        let nulls = unsafe { NullBuffer::new_unchecked(nulls, out_null_count) };
1100        PrimitiveArray::new(values, Some(nulls))
1101    }
1102
1103    /// Applies a unary infallible function to each value in an array, producing a
1104    /// new primitive array.
1105    ///
1106    /// # Null Handling
1107    ///
1108    /// See [`Self::unary`] for more information on null handling.
1109    ///
1110    /// # Example: create an [`Int16Array`] from an [`ArrayAccessor`] with item type `&[u8]`
1111    /// ```
1112    /// use arrow_array::{Array, FixedSizeBinaryArray, Int16Array};
1113    /// let input_arg = vec![ vec![1, 0], vec![2, 0], vec![3, 0] ];
1114    /// let arr = FixedSizeBinaryArray::try_from_iter(input_arg.into_iter()).unwrap();
1115    /// let c = Int16Array::from_unary(&arr, |x| i16::from_le_bytes(x[..2].try_into().unwrap()));
1116    /// assert_eq!(c, Int16Array::from(vec![Some(1i16), Some(2i16), Some(3i16)]));
1117    /// ```
1118    pub fn from_unary<U: ArrayAccessor, F>(left: U, mut op: F) -> Self
1119    where
1120        F: FnMut(U::Item) -> T::Native,
1121    {
1122        let nulls = left.logical_nulls();
1123        let buffer: Vec<_> = (0..left.len())
1124            // SAFETY: i in range 0..left.len()
1125            .map(|i| op(unsafe { left.value_unchecked(i) }))
1126            .collect();
1127        PrimitiveArray::new(buffer.into(), nulls)
1128    }
1129
1130    /// Returns a `PrimitiveBuilder` for this array, suitable for mutating values
1131    /// in place.
1132    ///
1133    /// # Buffer Reuse
1134    ///
1135    /// If the underlying data buffer has no other outstanding references, the
1136    /// buffer is used without copying.
1137    ///
1138    /// If the underlying data buffer does have outstanding references, returns
1139    /// `Err(self)`
1140    pub fn into_builder(self) -> Result<PrimitiveBuilder<T>, Self> {
1141        let len = self.len();
1142        let data_type = self.data_type.clone();
1143        let data = self.into_data();
1144        let null_bit_buffer = data.nulls().map(|b| b.inner().sliced());
1145
1146        let element_len = std::mem::size_of::<T::Native>();
1147        let buffer =
1148            data.buffers()[0].slice_with_length(data.offset() * element_len, len * element_len);
1149
1150        drop(data);
1151
1152        let try_mutable_null_buffer = match null_bit_buffer {
1153            None => Ok(None),
1154            Some(null_buffer) => {
1155                // Null buffer exists, tries to make it mutable
1156                null_buffer.into_mutable().map(Some)
1157            }
1158        };
1159
1160        let try_mutable_buffers = match try_mutable_null_buffer {
1161            Ok(mutable_null_buffer) => {
1162                // Got mutable null buffer, tries to get mutable value buffer
1163                let try_mutable_buffer = buffer.into_mutable();
1164
1165                // try_mutable_buffer.map(...).map_err(...) doesn't work as the compiler complains
1166                // mutable_null_buffer is moved into map closure.
1167                match try_mutable_buffer {
1168                    Ok(mutable_buffer) => Ok(PrimitiveBuilder::<T>::new_from_buffer(
1169                        mutable_buffer,
1170                        mutable_null_buffer,
1171                    )),
1172                    Err(buffer) => Err((buffer, mutable_null_buffer.map(|b| b.into()))),
1173                }
1174            }
1175            Err(mutable_null_buffer) => {
1176                // Unable to get mutable null buffer
1177                Err((buffer, Some(mutable_null_buffer)))
1178            }
1179        };
1180
1181        match try_mutable_buffers {
1182            Ok(builder) => Ok(builder.with_data_type(data_type)),
1183            Err((buffer, null_bit_buffer)) => {
1184                let builder = ArrayData::builder(data_type)
1185                    .len(len)
1186                    .add_buffer(buffer)
1187                    .null_bit_buffer(null_bit_buffer);
1188
1189                let array_data = unsafe { builder.build_unchecked() };
1190                let array = PrimitiveArray::<T>::from(array_data);
1191
1192                Err(array)
1193            }
1194        }
1195    }
1196}
1197
1198impl<T: ArrowPrimitiveType> From<PrimitiveArray<T>> for ArrayData {
1199    fn from(array: PrimitiveArray<T>) -> Self {
1200        /// Converts the parts of a `PrimitiveArray` into [`ArrayData`].
1201        /// Use an inner function to avoid code duplication over all
1202        /// generic callsites as the body is the same.
1203        ///
1204        /// # Safety
1205        /// The parts must come from a valid `PrimitiveArray`
1206        unsafe fn inner(
1207            data_type: DataType,
1208            len: usize,
1209            values: Buffer,
1210            nulls: Option<NullBuffer>,
1211        ) -> ArrayData {
1212            let builder = ArrayDataBuilder::new(data_type)
1213                .len(len)
1214                .nulls(nulls)
1215                .buffers(vec![values]);
1216
1217            // SAFETY: arguments are valid, per contract
1218            unsafe { builder.build_unchecked() }
1219        }
1220        let len = array.values.len();
1221        // SAFETY: the parts come from a valid PrimitiveArray
1222        unsafe { inner(array.data_type, len, array.values.into_inner(), array.nulls) }
1223    }
1224}
1225
1226/// SAFETY: Correctly implements the contract of Arrow Arrays
1227unsafe impl<T: ArrowPrimitiveType> Array for PrimitiveArray<T> {
1228    fn as_any(&self) -> &dyn Any {
1229        self
1230    }
1231
1232    fn to_data(&self) -> ArrayData {
1233        self.clone().into()
1234    }
1235
1236    fn into_data(self) -> ArrayData {
1237        self.into()
1238    }
1239
1240    fn data_type(&self) -> &DataType {
1241        &self.data_type
1242    }
1243
1244    fn slice(&self, offset: usize, length: usize) -> ArrayRef {
1245        Arc::new(self.slice(offset, length))
1246    }
1247
1248    fn len(&self) -> usize {
1249        self.values.len()
1250    }
1251
1252    fn is_empty(&self) -> bool {
1253        self.values.is_empty()
1254    }
1255
1256    fn shrink_to_fit(&mut self) {
1257        self.values.shrink_to_fit();
1258        if let Some(nulls) = &mut self.nulls {
1259            nulls.shrink_to_fit();
1260        }
1261    }
1262
1263    fn offset(&self) -> usize {
1264        0
1265    }
1266
1267    fn nulls(&self) -> Option<&NullBuffer> {
1268        self.nulls.as_ref()
1269    }
1270
1271    fn logical_null_count(&self) -> usize {
1272        self.null_count()
1273    }
1274
1275    fn get_buffer_memory_size(&self) -> usize {
1276        let mut size = self.values.inner().capacity();
1277        if let Some(n) = self.nulls.as_ref() {
1278            size += n.buffer().capacity();
1279        }
1280        size
1281    }
1282
1283    fn get_array_memory_size(&self) -> usize {
1284        std::mem::size_of::<Self>() + self.get_buffer_memory_size()
1285    }
1286
1287    #[cfg(feature = "pool")]
1288    fn claim(&self, pool: &dyn arrow_buffer::MemoryPool) {
1289        self.values.claim(pool);
1290        if let Some(nulls) = &self.nulls {
1291            nulls.claim(pool);
1292        }
1293    }
1294}
1295
1296impl<T: ArrowPrimitiveType> ArrayAccessor for &PrimitiveArray<T> {
1297    type Item = T::Native;
1298
1299    fn value(&self, index: usize) -> Self::Item {
1300        PrimitiveArray::value(self, index)
1301    }
1302
1303    #[inline]
1304    unsafe fn value_unchecked(&self, index: usize) -> Self::Item {
1305        unsafe { PrimitiveArray::value_unchecked(self, index) }
1306    }
1307}
1308
1309impl<T: ArrowTemporalType> PrimitiveArray<T>
1310where
1311    i64: From<T::Native>,
1312{
1313    /// Returns value as a chrono `NaiveDateTime`, handling time resolution
1314    ///
1315    /// If a data type cannot be converted to `NaiveDateTime`, a `None` is returned.
1316    /// A valid value is expected, thus the user should first check for validity.
1317    ///
1318    /// See notes on [`PrimitiveArray::value`] regarding nulls and panics
1319    ///
1320    /// # Panics
1321    ///
1322    /// Panics if `i >= self.len()`
1323    pub fn value_as_datetime(&self, i: usize) -> Option<NaiveDateTime> {
1324        as_datetime::<T>(i64::from(self.value(i)))
1325    }
1326
1327    /// Returns value as a chrono `NaiveDateTime`, handling time resolution with the provided tz
1328    ///
1329    /// functionally it is same as `value_as_datetime`, however it adds
1330    /// the passed tz to the to-be-returned NaiveDateTime
1331    ///
1332    /// See notes on [`PrimitiveArray::value`] regarding nulls and panics
1333    ///
1334    /// # Panics
1335    ///
1336    /// Panics if `i >= self.len()`
1337    pub fn value_as_datetime_with_tz(&self, i: usize, tz: Tz) -> Option<DateTime<Tz>> {
1338        as_datetime_with_timezone::<T>(i64::from(self.value(i)), tz)
1339    }
1340
1341    /// Returns value as a chrono `NaiveDate` by using `Self::datetime()`
1342    ///
1343    /// If a data type cannot be converted to `NaiveDate`, a `None` is returned
1344    ///
1345    /// See notes on [`PrimitiveArray::value`] regarding nulls and panics
1346    ///
1347    /// # Panics
1348    ///
1349    /// Panics if `i >= self.len()`
1350    pub fn value_as_date(&self, i: usize) -> Option<NaiveDate> {
1351        self.value_as_datetime(i).map(|datetime| datetime.date())
1352    }
1353
1354    /// Returns a value as a chrono `NaiveTime`
1355    ///
1356    /// `Date32` and `Date64` return UTC midnight as they do not have time resolution
1357    ///
1358    /// See notes on [`PrimitiveArray::value`] regarding nulls and panics
1359    ///
1360    /// # Panics
1361    ///
1362    /// Panics if `i >= self.len()`
1363    pub fn value_as_time(&self, i: usize) -> Option<NaiveTime> {
1364        as_time::<T>(i64::from(self.value(i)))
1365    }
1366
1367    /// Returns a value as a chrono `Duration`
1368    ///
1369    /// If a data type cannot be converted to `Duration`, a `None` is returned
1370    ///
1371    /// See notes on [`PrimitiveArray::value`] regarding nulls and panics
1372    ///
1373    /// # Panics
1374    ///
1375    /// Panics if `i >= self.len()`
1376    pub fn value_as_duration(&self, i: usize) -> Option<Duration> {
1377        as_duration::<T>(i64::from(self.value(i)))
1378    }
1379}
1380
1381/// Writes the `Debug` representation of a single temporal value (converted to
1382/// `i64`) of the given [`DataType`] to `f`
1383fn write_temporal_value(
1384    f: &mut std::fmt::Formatter,
1385    data_type: &DataType,
1386    v: i64,
1387) -> std::fmt::Result {
1388    match data_type {
1389        DataType::Date32 | DataType::Date64 => {
1390            match as_datetime_with_data_type(data_type, v).map(|datetime| datetime.date()) {
1391                Some(date) => write!(f, "{date:?}"),
1392                None => {
1393                    write!(
1394                        f,
1395                        "Cast error: Failed to convert {v} to temporal for {data_type}"
1396                    )
1397                }
1398            }
1399        }
1400        DataType::Time32(_) | DataType::Time64(_) => match as_time_with_data_type(data_type, v) {
1401            Some(time) => write!(f, "{time:?}"),
1402            None => {
1403                write!(
1404                    f,
1405                    "Cast error: Failed to convert {v} to temporal for {data_type}"
1406                )
1407            }
1408        },
1409        DataType::Timestamp(_, tz_string_opt) => {
1410            match tz_string_opt {
1411                // for Timestamp with TimeZone
1412                Some(tz_string) => {
1413                    match tz_string.parse::<Tz>() {
1414                        // if the time zone is valid, construct a DateTime<Tz> and format it as rfc3339
1415                        Ok(tz) => match as_datetime_with_timezone_and_data_type(data_type, v, tz) {
1416                            Some(datetime) => write!(f, "{}", datetime.to_rfc3339()),
1417                            None => write!(
1418                                f,
1419                                "Cast error: Failed to convert {v} to timestamp for {data_type}"
1420                            ),
1421                        },
1422                        // if the time zone is invalid, shows NaiveDateTime with an error message
1423                        Err(_) => match as_datetime_with_data_type(data_type, v) {
1424                            Some(datetime) => {
1425                                write!(f, "{datetime:?} (Unknown Time Zone '{tz_string}')")
1426                            }
1427                            None => write!(
1428                                f,
1429                                "Cast error: Failed to convert {v} to timestamp for {data_type}"
1430                            ),
1431                        },
1432                    }
1433                }
1434                // for Timestamp without TimeZone
1435                None => match as_datetime_with_data_type(data_type, v) {
1436                    Some(datetime) => write!(f, "{datetime:?}"),
1437                    None => write!(
1438                        f,
1439                        "Cast error: Failed to convert {v} to timestamp for {data_type}"
1440                    ),
1441                },
1442            }
1443        }
1444        _ => unreachable!("write_temporal_value called with non-temporal type {data_type}"),
1445    }
1446}
1447
1448impl<T: ArrowPrimitiveType> std::fmt::Debug for PrimitiveArray<T> {
1449    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1450        let data_type = self.data_type();
1451
1452        write!(f, "PrimitiveArray<{data_type}>\n[\n")?;
1453        // Keep the per-value closure as small as possible: temporal formatting
1454        // is dispatched to the non-generic `write_temporal_value` so it is not
1455        // instantiated for every primitive type (see #10889)
1456        print_long_array(self, f, &mut |index, f| match data_type {
1457            DataType::Date32
1458            | DataType::Date64
1459            | DataType::Time32(_)
1460            | DataType::Time64(_)
1461            | DataType::Timestamp(_, _) => {
1462                write_temporal_value(f, data_type, self.value(index).to_i64().unwrap())
1463            }
1464            _ => std::fmt::Debug::fmt(&self.value(index), f),
1465        })?;
1466        write!(f, "]")
1467    }
1468}
1469
1470impl<'a, T: ArrowPrimitiveType> IntoIterator for &'a PrimitiveArray<T> {
1471    type Item = Option<<T as ArrowPrimitiveType>::Native>;
1472    type IntoIter = PrimitiveIter<'a, T>;
1473
1474    fn into_iter(self) -> Self::IntoIter {
1475        PrimitiveIter::<'a, T>::new(self)
1476    }
1477}
1478
1479impl<'a, T: ArrowPrimitiveType> PrimitiveArray<T> {
1480    /// constructs a new iterator
1481    pub fn iter(&'a self) -> PrimitiveIter<'a, T> {
1482        PrimitiveIter::<'a, T>::new(self)
1483    }
1484}
1485
1486/// An optional primitive value
1487///
1488/// This struct is used as an adapter when creating `PrimitiveArray` from an iterator.
1489/// `FromIterator` for `PrimitiveArray` takes an iterator where the elements can be `into`
1490/// this struct. So once implementing `From` or `Into` trait for a type, an iterator of
1491/// the type can be collected to `PrimitiveArray`.
1492#[derive(Debug)]
1493pub struct NativeAdapter<T: ArrowPrimitiveType> {
1494    /// Corresponding Rust native type if available
1495    pub native: Option<T::Native>,
1496}
1497
1498macro_rules! def_from_for_primitive {
1499    ( $ty:ident, $tt:tt) => {
1500        impl From<$tt> for NativeAdapter<$ty> {
1501            fn from(value: $tt) -> Self {
1502                NativeAdapter {
1503                    native: Some(value),
1504                }
1505            }
1506        }
1507    };
1508}
1509
1510def_from_for_primitive!(Int8Type, i8);
1511def_from_for_primitive!(Int16Type, i16);
1512def_from_for_primitive!(Int32Type, i32);
1513def_from_for_primitive!(Int64Type, i64);
1514def_from_for_primitive!(UInt8Type, u8);
1515def_from_for_primitive!(UInt16Type, u16);
1516def_from_for_primitive!(UInt32Type, u32);
1517def_from_for_primitive!(UInt64Type, u64);
1518def_from_for_primitive!(Float16Type, f16);
1519def_from_for_primitive!(Float32Type, f32);
1520def_from_for_primitive!(Float64Type, f64);
1521def_from_for_primitive!(Decimal32Type, i32);
1522def_from_for_primitive!(Decimal64Type, i64);
1523def_from_for_primitive!(Decimal128Type, i128);
1524def_from_for_primitive!(Decimal256Type, i256);
1525
1526impl<T: ArrowPrimitiveType> From<Option<<T as ArrowPrimitiveType>::Native>> for NativeAdapter<T> {
1527    fn from(value: Option<<T as ArrowPrimitiveType>::Native>) -> Self {
1528        NativeAdapter { native: value }
1529    }
1530}
1531
1532impl<T: ArrowPrimitiveType> From<&Option<<T as ArrowPrimitiveType>::Native>> for NativeAdapter<T> {
1533    fn from(value: &Option<<T as ArrowPrimitiveType>::Native>) -> Self {
1534        NativeAdapter { native: *value }
1535    }
1536}
1537
1538impl<T: ArrowPrimitiveType, Ptr: Into<NativeAdapter<T>>> FromIterator<Ptr> for PrimitiveArray<T> {
1539    fn from_iter<I: IntoIterator<Item = Ptr>>(iter: I) -> Self {
1540        let iter = iter.into_iter();
1541        let (lower, _) = iter.size_hint();
1542
1543        let mut null_builder = NullBufferBuilder::new(lower);
1544
1545        let buffer: Buffer = iter
1546            .map(|item| {
1547                if let Some(a) = item.into().native {
1548                    null_builder.append_non_null();
1549                    a
1550                } else {
1551                    null_builder.append_null();
1552                    // this ensures that null items on the buffer are not arbitrary.
1553                    // This is important because fallible operations can use null values (e.g. a vectorized "add")
1554                    // which may panic (e.g. overflow if the number on the slots happen to be very large).
1555                    T::Native::default()
1556                }
1557            })
1558            .collect();
1559
1560        let maybe_nulls = null_builder.finish();
1561        PrimitiveArray::new(ScalarBuffer::from(buffer), maybe_nulls)
1562    }
1563}
1564
1565impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
1566    /// Creates a [`PrimitiveArray`] from an iterator of trusted length.
1567    /// # Safety
1568    /// The iterator must be [`TrustedLen`](https://doc.rust-lang.org/std/iter/trait.TrustedLen.html).
1569    /// I.e. that `size_hint().1` correctly reports its length.
1570    #[inline]
1571    pub unsafe fn from_trusted_len_iter<I, P>(iter: I) -> Self
1572    where
1573        P: std::borrow::Borrow<Option<<T as ArrowPrimitiveType>::Native>>,
1574        I: IntoIterator<Item = P>,
1575    {
1576        let iterator = iter.into_iter();
1577        let (_, upper) = iterator.size_hint();
1578        let len = upper.expect("trusted_len_unzip requires an upper limit");
1579
1580        let (null, buffer) = unsafe { trusted_len_unzip(iterator) };
1581
1582        let nulls = NullBuffer::from_unsliced_buffer(null, len);
1583        PrimitiveArray::new(ScalarBuffer::from(buffer), nulls)
1584    }
1585}
1586
1587// TODO: the macro is needed here because we'd get "conflicting implementations" error
1588// otherwise with both `From<Vec<T::Native>>` and `From<Vec<Option<T::Native>>>`.
1589// We should revisit this in future.
1590macro_rules! def_numeric_from_vec {
1591    ( $ty:ident ) => {
1592        impl From<Vec<<$ty as ArrowPrimitiveType>::Native>> for PrimitiveArray<$ty> {
1593            fn from(data: Vec<<$ty as ArrowPrimitiveType>::Native>) -> Self {
1594                let buffer = ScalarBuffer::from(Buffer::from_vec(data));
1595                let nulls = None;
1596                PrimitiveArray::new(buffer, nulls)
1597            }
1598        }
1599
1600        // Constructs a primitive array from a vector. Should only be used for testing.
1601        impl From<Vec<Option<<$ty as ArrowPrimitiveType>::Native>>> for PrimitiveArray<$ty> {
1602            fn from(data: Vec<Option<<$ty as ArrowPrimitiveType>::Native>>) -> Self {
1603                PrimitiveArray::from_iter(data.iter())
1604            }
1605        }
1606    };
1607}
1608
1609def_numeric_from_vec!(Int8Type);
1610def_numeric_from_vec!(Int16Type);
1611def_numeric_from_vec!(Int32Type);
1612def_numeric_from_vec!(Int64Type);
1613def_numeric_from_vec!(UInt8Type);
1614def_numeric_from_vec!(UInt16Type);
1615def_numeric_from_vec!(UInt32Type);
1616def_numeric_from_vec!(UInt64Type);
1617def_numeric_from_vec!(Float16Type);
1618def_numeric_from_vec!(Float32Type);
1619def_numeric_from_vec!(Float64Type);
1620def_numeric_from_vec!(Decimal32Type);
1621def_numeric_from_vec!(Decimal64Type);
1622def_numeric_from_vec!(Decimal128Type);
1623def_numeric_from_vec!(Decimal256Type);
1624
1625def_numeric_from_vec!(Date32Type);
1626def_numeric_from_vec!(Date64Type);
1627def_numeric_from_vec!(Time32SecondType);
1628def_numeric_from_vec!(Time32MillisecondType);
1629def_numeric_from_vec!(Time64MicrosecondType);
1630def_numeric_from_vec!(Time64NanosecondType);
1631def_numeric_from_vec!(IntervalYearMonthType);
1632def_numeric_from_vec!(IntervalDayTimeType);
1633def_numeric_from_vec!(IntervalMonthDayNanoType);
1634def_numeric_from_vec!(DurationSecondType);
1635def_numeric_from_vec!(DurationMillisecondType);
1636def_numeric_from_vec!(DurationMicrosecondType);
1637def_numeric_from_vec!(DurationNanosecondType);
1638def_numeric_from_vec!(TimestampSecondType);
1639def_numeric_from_vec!(TimestampMillisecondType);
1640def_numeric_from_vec!(TimestampMicrosecondType);
1641def_numeric_from_vec!(TimestampNanosecondType);
1642
1643impl<T: ArrowTimestampType> PrimitiveArray<T> {
1644    /// Returns the timezone of this array if any
1645    pub fn timezone(&self) -> Option<&str> {
1646        match self.data_type() {
1647            DataType::Timestamp(_, tz) => tz.as_deref(),
1648            _ => unreachable!(),
1649        }
1650    }
1651
1652    /// Construct a timestamp array with new timezone
1653    pub fn with_timezone(self, timezone: impl Into<Arc<str>>) -> Self {
1654        self.with_timezone_opt(Some(timezone.into()))
1655    }
1656
1657    /// Construct a timestamp array with UTC
1658    pub fn with_timezone_utc(self) -> Self {
1659        self.with_timezone("+00:00")
1660    }
1661
1662    /// Construct a timestamp array with an optional timezone
1663    pub fn with_timezone_opt<S: Into<Arc<str>>>(self, timezone: Option<S>) -> Self {
1664        Self {
1665            data_type: DataType::Timestamp(T::UNIT, timezone.map(Into::into)),
1666            ..self
1667        }
1668    }
1669}
1670
1671/// Constructs a `PrimitiveArray` from an array data reference.
1672impl<T: ArrowPrimitiveType> From<ArrayData> for PrimitiveArray<T> {
1673    fn from(data: ArrayData) -> Self {
1674        let (data_type, len, nulls, offset, mut buffers, _child_data) = data.into_parts();
1675
1676        Self::assert_compatible(&data_type);
1677        assert_eq!(
1678            buffers.len(),
1679            1,
1680            "PrimitiveArray data should contain a single buffer only (values buffer)"
1681        );
1682        let buffer = buffers.pop().expect("checked above");
1683
1684        let values = ScalarBuffer::new(buffer, offset, len);
1685        Self {
1686            data_type,
1687            values,
1688            nulls,
1689        }
1690    }
1691}
1692
1693impl<T: DecimalType + ArrowPrimitiveType> PrimitiveArray<T> {
1694    /// Returns a Decimal array with the same data as self, with the
1695    /// specified precision and scale.
1696    ///
1697    /// See [`validate_decimal_precision_and_scale`]
1698    pub fn with_precision_and_scale(self, precision: u8, scale: i8) -> Result<Self, ArrowError> {
1699        validate_decimal_precision_and_scale::<T>(precision, scale)?;
1700        Ok(Self {
1701            data_type: T::TYPE_CONSTRUCTOR(precision, scale),
1702            ..self
1703        })
1704    }
1705
1706    /// Validates values in this array can be properly interpreted
1707    /// with the specified precision.
1708    pub fn validate_decimal_precision(&self, precision: u8) -> Result<(), ArrowError> {
1709        if self.scale() > 0 && precision < self.scale() as u8 {
1710            return Err(ArrowError::InvalidArgumentError(format!(
1711                "Decimal precision {precision} is less than scale {}",
1712                self.scale()
1713            )));
1714        }
1715        (0..self.len()).try_for_each(|idx| {
1716            if self.is_valid(idx) {
1717                let decimal = unsafe { self.value_unchecked(idx) };
1718                T::validate_decimal_precision(decimal, precision, self.scale())
1719            } else {
1720                Ok(())
1721            }
1722        })
1723    }
1724
1725    /// Validates the Decimal Array, if the value of slot is overflow for the specified precision, and
1726    /// will be casted to Null
1727    pub fn null_if_overflow_precision(&self, precision: u8) -> Self {
1728        self.unary_opt::<_, T>(|v| T::is_valid_decimal_precision(v, precision).then_some(v))
1729    }
1730
1731    /// Returns [`Self::value`] formatted as a string
1732    pub fn value_as_string(&self, row: usize) -> String {
1733        T::format_decimal(self.value(row), self.precision(), self.scale())
1734    }
1735
1736    /// Returns the decimal precision of this array
1737    pub fn precision(&self) -> u8 {
1738        match T::BYTE_LENGTH {
1739            4 => {
1740                if let DataType::Decimal32(p, _) = self.data_type() {
1741                    *p
1742                } else {
1743                    unreachable!(
1744                        "Decimal32Array datatype is not DataType::Decimal32 but {}",
1745                        self.data_type()
1746                    )
1747                }
1748            }
1749            8 => {
1750                if let DataType::Decimal64(p, _) = self.data_type() {
1751                    *p
1752                } else {
1753                    unreachable!(
1754                        "Decimal64Array datatype is not DataType::Decimal64 but {}",
1755                        self.data_type()
1756                    )
1757                }
1758            }
1759            16 => {
1760                if let DataType::Decimal128(p, _) = self.data_type() {
1761                    *p
1762                } else {
1763                    unreachable!(
1764                        "Decimal128Array datatype is not DataType::Decimal128 but {}",
1765                        self.data_type()
1766                    )
1767                }
1768            }
1769            32 => {
1770                if let DataType::Decimal256(p, _) = self.data_type() {
1771                    *p
1772                } else {
1773                    unreachable!(
1774                        "Decimal256Array datatype is not DataType::Decimal256 but {}",
1775                        self.data_type()
1776                    )
1777                }
1778            }
1779            other => unreachable!("Unsupported byte length for decimal array {}", other),
1780        }
1781    }
1782
1783    /// Returns the decimal scale of this array
1784    pub fn scale(&self) -> i8 {
1785        match T::BYTE_LENGTH {
1786            4 => {
1787                if let DataType::Decimal32(_, s) = self.data_type() {
1788                    *s
1789                } else {
1790                    unreachable!(
1791                        "Decimal32Array datatype is not DataType::Decimal32 but {}",
1792                        self.data_type()
1793                    )
1794                }
1795            }
1796            8 => {
1797                if let DataType::Decimal64(_, s) = self.data_type() {
1798                    *s
1799                } else {
1800                    unreachable!(
1801                        "Decimal64Array datatype is not DataType::Decimal64 but {}",
1802                        self.data_type()
1803                    )
1804                }
1805            }
1806            16 => {
1807                if let DataType::Decimal128(_, s) = self.data_type() {
1808                    *s
1809                } else {
1810                    unreachable!(
1811                        "Decimal128Array datatype is not DataType::Decimal128 but {}",
1812                        self.data_type()
1813                    )
1814                }
1815            }
1816            32 => {
1817                if let DataType::Decimal256(_, s) = self.data_type() {
1818                    *s
1819                } else {
1820                    unreachable!(
1821                        "Decimal256Array datatype is not DataType::Decimal256 but {}",
1822                        self.data_type()
1823                    )
1824                }
1825            }
1826            other => unreachable!("Unsupported byte length for decimal array {}", other),
1827        }
1828    }
1829}
1830
1831#[cfg(test)]
1832mod tests {
1833    use super::*;
1834    use crate::BooleanArray;
1835    use crate::builder::{
1836        Decimal32Builder, Decimal64Builder, Decimal128Builder, Decimal256Builder,
1837    };
1838    use crate::cast::downcast_array;
1839    use arrow_buffer::{IntervalDayTime, IntervalMonthDayNano};
1840    use arrow_schema::TimeUnit;
1841
1842    #[test]
1843    fn test_primitive_array_from_vec() {
1844        let buf = Buffer::from_slice_ref([0, 1, 2, 3, 4]);
1845        let arr = Int32Array::from(vec![0, 1, 2, 3, 4]);
1846        assert_eq!(&buf, arr.values.inner());
1847        assert_eq!(5, arr.len());
1848        assert_eq!(0, arr.offset());
1849        assert_eq!(0, arr.null_count());
1850        for i in 0..5 {
1851            assert!(!arr.is_null(i));
1852            assert!(arr.is_valid(i));
1853            assert_eq!(i as i32, arr.value(i));
1854        }
1855    }
1856
1857    #[test]
1858    fn test_primitive_array_from_vec_option() {
1859        // Test building a primitive array with null values
1860        let arr = Int32Array::from(vec![Some(0), None, Some(2), None, Some(4)]);
1861        assert_eq!(5, arr.len());
1862        assert_eq!(0, arr.offset());
1863        assert_eq!(2, arr.null_count());
1864        for i in 0..5 {
1865            if i % 2 == 0 {
1866                assert!(!arr.is_null(i));
1867                assert!(arr.is_valid(i));
1868                assert_eq!(i as i32, arr.value(i));
1869            } else {
1870                assert!(arr.is_null(i));
1871                assert!(!arr.is_valid(i));
1872            }
1873        }
1874    }
1875
1876    #[test]
1877    fn test_date64_array_from_vec_option() {
1878        // Test building a primitive array with null values
1879        // we use Int32 and Int64 as a backing array, so all Int32 and Int64 conventions
1880        // work
1881        let arr: PrimitiveArray<Date64Type> =
1882            vec![Some(1550902545147), None, Some(1550902545147)].into();
1883        assert_eq!(3, arr.len());
1884        assert_eq!(0, arr.offset());
1885        assert_eq!(1, arr.null_count());
1886        for i in 0..3 {
1887            if i % 2 == 0 {
1888                assert!(!arr.is_null(i));
1889                assert!(arr.is_valid(i));
1890                assert_eq!(1550902545147, arr.value(i));
1891                // roundtrip to and from datetime
1892                assert_eq!(
1893                    1550902545147,
1894                    arr.value_as_datetime(i)
1895                        .unwrap()
1896                        .and_utc()
1897                        .timestamp_millis()
1898                );
1899            } else {
1900                assert!(arr.is_null(i));
1901                assert!(!arr.is_valid(i));
1902            }
1903        }
1904    }
1905
1906    #[test]
1907    fn test_time32_millisecond_array_from_vec() {
1908        // 1:        00:00:00.001
1909        // 37800005: 10:30:00.005
1910        // 86399210: 23:59:59.210
1911        let arr: PrimitiveArray<Time32MillisecondType> = vec![1, 37_800_005, 86_399_210].into();
1912        assert_eq!(3, arr.len());
1913        assert_eq!(0, arr.offset());
1914        assert_eq!(0, arr.null_count());
1915        let formatted = ["00:00:00.001", "10:30:00.005", "23:59:59.210"];
1916        for (i, formatted) in formatted.iter().enumerate().take(3) {
1917            // check that we can't create dates or datetimes from time instances
1918            assert_eq!(None, arr.value_as_datetime(i));
1919            assert_eq!(None, arr.value_as_date(i));
1920            let time = arr.value_as_time(i).unwrap();
1921            assert_eq!(*formatted, time.format("%H:%M:%S%.3f").to_string());
1922        }
1923    }
1924
1925    #[test]
1926    fn test_time64_nanosecond_array_from_vec() {
1927        // Test building a primitive array with null values
1928        // we use Int32 and Int64 as a backing array, so all Int32 and Int64 conventions
1929        // work
1930
1931        // 1e6:        00:00:00.001
1932        // 37800005e6: 10:30:00.005
1933        // 86399210e6: 23:59:59.210
1934        let arr: PrimitiveArray<Time64NanosecondType> =
1935            vec![1_000_000, 37_800_005_000_000, 86_399_210_000_000].into();
1936        assert_eq!(3, arr.len());
1937        assert_eq!(0, arr.offset());
1938        assert_eq!(0, arr.null_count());
1939        let formatted = ["00:00:00.001", "10:30:00.005", "23:59:59.210"];
1940        for (i, item) in formatted.iter().enumerate().take(3) {
1941            // check that we can't create dates or datetimes from time instances
1942            assert_eq!(None, arr.value_as_datetime(i));
1943            assert_eq!(None, arr.value_as_date(i));
1944            let time = arr.value_as_time(i).unwrap();
1945            assert_eq!(*item, time.format("%H:%M:%S%.3f").to_string());
1946        }
1947    }
1948
1949    #[test]
1950    fn test_interval_array_from_vec() {
1951        // intervals are currently not treated specially, but are Int32 and Int64 arrays
1952        let arr = IntervalYearMonthArray::from(vec![Some(1), None, Some(-5)]);
1953        assert_eq!(3, arr.len());
1954        assert_eq!(0, arr.offset());
1955        assert_eq!(1, arr.null_count());
1956        assert_eq!(1, arr.value(0));
1957        assert_eq!(1, arr.values()[0]);
1958        assert!(arr.is_null(1));
1959        assert_eq!(-5, arr.value(2));
1960        assert_eq!(-5, arr.values()[2]);
1961
1962        let v0 = IntervalDayTime {
1963            days: 34,
1964            milliseconds: 1,
1965        };
1966        let v2 = IntervalDayTime {
1967            days: -2,
1968            milliseconds: -5,
1969        };
1970
1971        let arr = IntervalDayTimeArray::from(vec![Some(v0), None, Some(v2)]);
1972
1973        assert_eq!(3, arr.len());
1974        assert_eq!(0, arr.offset());
1975        assert_eq!(1, arr.null_count());
1976        assert_eq!(v0, arr.value(0));
1977        assert_eq!(v0, arr.values()[0]);
1978        assert!(arr.is_null(1));
1979        assert_eq!(v2, arr.value(2));
1980        assert_eq!(v2, arr.values()[2]);
1981
1982        let v0 = IntervalMonthDayNano {
1983            months: 2,
1984            days: 34,
1985            nanoseconds: -1,
1986        };
1987        let v2 = IntervalMonthDayNano {
1988            months: -3,
1989            days: -2,
1990            nanoseconds: 4,
1991        };
1992
1993        let arr = IntervalMonthDayNanoArray::from(vec![Some(v0), None, Some(v2)]);
1994        assert_eq!(3, arr.len());
1995        assert_eq!(0, arr.offset());
1996        assert_eq!(1, arr.null_count());
1997        assert_eq!(v0, arr.value(0));
1998        assert_eq!(v0, arr.values()[0]);
1999        assert!(arr.is_null(1));
2000        assert_eq!(v2, arr.value(2));
2001        assert_eq!(v2, arr.values()[2]);
2002    }
2003
2004    #[test]
2005    fn test_duration_array_from_vec() {
2006        let arr = DurationSecondArray::from(vec![Some(1), None, Some(-5)]);
2007        assert_eq!(3, arr.len());
2008        assert_eq!(0, arr.offset());
2009        assert_eq!(1, arr.null_count());
2010        assert_eq!(1, arr.value(0));
2011        assert_eq!(1, arr.values()[0]);
2012        assert!(arr.is_null(1));
2013        assert_eq!(-5, arr.value(2));
2014        assert_eq!(-5, arr.values()[2]);
2015
2016        let arr = DurationMillisecondArray::from(vec![Some(1), None, Some(-5)]);
2017        assert_eq!(3, arr.len());
2018        assert_eq!(0, arr.offset());
2019        assert_eq!(1, arr.null_count());
2020        assert_eq!(1, arr.value(0));
2021        assert_eq!(1, arr.values()[0]);
2022        assert!(arr.is_null(1));
2023        assert_eq!(-5, arr.value(2));
2024        assert_eq!(-5, arr.values()[2]);
2025
2026        let arr = DurationMicrosecondArray::from(vec![Some(1), None, Some(-5)]);
2027        assert_eq!(3, arr.len());
2028        assert_eq!(0, arr.offset());
2029        assert_eq!(1, arr.null_count());
2030        assert_eq!(1, arr.value(0));
2031        assert_eq!(1, arr.values()[0]);
2032        assert!(arr.is_null(1));
2033        assert_eq!(-5, arr.value(2));
2034        assert_eq!(-5, arr.values()[2]);
2035
2036        let arr = DurationNanosecondArray::from(vec![Some(1), None, Some(-5)]);
2037        assert_eq!(3, arr.len());
2038        assert_eq!(0, arr.offset());
2039        assert_eq!(1, arr.null_count());
2040        assert_eq!(1, arr.value(0));
2041        assert_eq!(1, arr.values()[0]);
2042        assert!(arr.is_null(1));
2043        assert_eq!(-5, arr.value(2));
2044        assert_eq!(-5, arr.values()[2]);
2045    }
2046
2047    #[test]
2048    fn test_timestamp_array_from_vec() {
2049        let arr = TimestampSecondArray::from(vec![1, -5]);
2050        assert_eq!(2, arr.len());
2051        assert_eq!(0, arr.offset());
2052        assert_eq!(0, arr.null_count());
2053        assert_eq!(1, arr.value(0));
2054        assert_eq!(-5, arr.value(1));
2055        assert_eq!(&[1, -5], arr.values());
2056
2057        let arr = TimestampMillisecondArray::from(vec![1, -5]);
2058        assert_eq!(2, arr.len());
2059        assert_eq!(0, arr.offset());
2060        assert_eq!(0, arr.null_count());
2061        assert_eq!(1, arr.value(0));
2062        assert_eq!(-5, arr.value(1));
2063        assert_eq!(&[1, -5], arr.values());
2064
2065        let arr = TimestampMicrosecondArray::from(vec![1, -5]);
2066        assert_eq!(2, arr.len());
2067        assert_eq!(0, arr.offset());
2068        assert_eq!(0, arr.null_count());
2069        assert_eq!(1, arr.value(0));
2070        assert_eq!(-5, arr.value(1));
2071        assert_eq!(&[1, -5], arr.values());
2072
2073        let arr = TimestampNanosecondArray::from(vec![1, -5]);
2074        assert_eq!(2, arr.len());
2075        assert_eq!(0, arr.offset());
2076        assert_eq!(0, arr.null_count());
2077        assert_eq!(1, arr.value(0));
2078        assert_eq!(-5, arr.value(1));
2079        assert_eq!(&[1, -5], arr.values());
2080    }
2081
2082    #[test]
2083    fn test_primitive_array_slice() {
2084        let arr = Int32Array::from(vec![
2085            Some(0),
2086            None,
2087            Some(2),
2088            None,
2089            Some(4),
2090            Some(5),
2091            Some(6),
2092            None,
2093            None,
2094        ]);
2095        assert_eq!(9, arr.len());
2096        assert_eq!(0, arr.offset());
2097        assert_eq!(4, arr.null_count());
2098
2099        let arr2 = arr.slice(2, 5);
2100        assert_eq!(5, arr2.len());
2101        assert_eq!(1, arr2.null_count());
2102
2103        for i in 0..arr2.len() {
2104            assert_eq!(i == 1, arr2.is_null(i));
2105            assert_eq!(i != 1, arr2.is_valid(i));
2106        }
2107        let int_arr2 = arr2.as_any().downcast_ref::<Int32Array>().unwrap();
2108        assert_eq!(2, int_arr2.values()[0]);
2109        assert_eq!(&[4, 5, 6], &int_arr2.values()[2..5]);
2110
2111        let arr3 = arr2.slice(2, 3);
2112        assert_eq!(3, arr3.len());
2113        assert_eq!(0, arr3.null_count());
2114
2115        let int_arr3 = arr3.as_any().downcast_ref::<Int32Array>().unwrap();
2116        assert_eq!(&[4, 5, 6], int_arr3.values());
2117        assert_eq!(4, int_arr3.value(0));
2118        assert_eq!(5, int_arr3.value(1));
2119        assert_eq!(6, int_arr3.value(2));
2120    }
2121
2122    #[test]
2123    fn test_boolean_array_slice() {
2124        let arr = BooleanArray::from(vec![
2125            Some(true),
2126            None,
2127            Some(false),
2128            None,
2129            Some(true),
2130            Some(false),
2131            Some(true),
2132            Some(false),
2133            None,
2134            Some(true),
2135        ]);
2136
2137        assert_eq!(10, arr.len());
2138        assert_eq!(0, arr.offset());
2139        assert_eq!(3, arr.null_count());
2140
2141        let arr2 = arr.slice(3, 5);
2142        assert_eq!(5, arr2.len());
2143        assert_eq!(3, arr2.offset());
2144        assert_eq!(1, arr2.null_count());
2145
2146        let bool_arr = arr2.as_any().downcast_ref::<BooleanArray>().unwrap();
2147
2148        assert!(!bool_arr.is_valid(0));
2149
2150        assert!(bool_arr.is_valid(1));
2151        assert!(bool_arr.value(1));
2152
2153        assert!(bool_arr.is_valid(2));
2154        assert!(!bool_arr.value(2));
2155
2156        assert!(bool_arr.is_valid(3));
2157        assert!(bool_arr.value(3));
2158
2159        assert!(bool_arr.is_valid(4));
2160        assert!(!bool_arr.value(4));
2161    }
2162
2163    #[test]
2164    fn test_int32_fmt_debug() {
2165        let arr = Int32Array::from(vec![0, 1, 2, 3, 4]);
2166        assert_eq!(
2167            "PrimitiveArray<Int32>\n[\n  0,\n  1,\n  2,\n  3,\n  4,\n]",
2168            format!("{arr:?}")
2169        );
2170    }
2171
2172    #[test]
2173    fn test_fmt_debug_up_to_20_elements() {
2174        (1..=20).for_each(|i| {
2175            let values = (0..i).collect::<Vec<i16>>();
2176            let array_expected = format!(
2177                "PrimitiveArray<Int16>\n[\n{}\n]",
2178                values
2179                    .iter()
2180                    .map(|v| { format!("  {v},") })
2181                    .collect::<Vec<String>>()
2182                    .join("\n")
2183            );
2184            let array = Int16Array::from(values);
2185
2186            assert_eq!(array_expected, format!("{array:?}"));
2187        })
2188    }
2189
2190    #[test]
2191    fn test_int32_with_null_fmt_debug() {
2192        let mut builder = Int32Array::builder(3);
2193        builder.append_slice(&[0, 1]);
2194        builder.append_null();
2195        builder.append_slice(&[3, 4]);
2196        let arr = builder.finish();
2197        assert_eq!(
2198            "PrimitiveArray<Int32>\n[\n  0,\n  1,\n  null,\n  3,\n  4,\n]",
2199            format!("{arr:?}")
2200        );
2201    }
2202
2203    #[test]
2204    fn test_timestamp_fmt_debug() {
2205        let arr: PrimitiveArray<TimestampMillisecondType> =
2206            TimestampMillisecondArray::from(vec![1546214400000, 1546214400000, -1546214400000]);
2207        assert_eq!(
2208            "PrimitiveArray<Timestamp(ms)>\n[\n  2018-12-31T00:00:00,\n  2018-12-31T00:00:00,\n  1921-01-02T00:00:00,\n]",
2209            format!("{arr:?}")
2210        );
2211    }
2212
2213    #[test]
2214    fn test_timestamp_fmt_debug_out_of_range() {
2215        // Include a true null into dataset to ensure we don't write that as an error
2216        let data = Int64Array::new(
2217            vec![i64::MAX, i64::MIN, i64::MAX].into(),
2218            Some(vec![true, true, false].into()),
2219        );
2220
2221        let arr = data.reinterpret_cast::<TimestampSecondType>();
2222        assert_eq!(
2223            "PrimitiveArray<Timestamp(s)>
2224[
2225  Cast error: Failed to convert 9223372036854775807 to timestamp for Timestamp(s),
2226  Cast error: Failed to convert -9223372036854775808 to timestamp for Timestamp(s),
2227  null,
2228]",
2229            format!("{arr:?}")
2230        );
2231
2232        let arr = data.reinterpret_cast::<TimestampMillisecondType>();
2233        assert_eq!(
2234            "PrimitiveArray<Timestamp(ms)>
2235[
2236  Cast error: Failed to convert 9223372036854775807 to timestamp for Timestamp(ms),
2237  Cast error: Failed to convert -9223372036854775808 to timestamp for Timestamp(ms),
2238  null,
2239]",
2240            format!("{arr:?}")
2241        );
2242
2243        let arr = data.reinterpret_cast::<TimestampMicrosecondType>();
2244        assert_eq!(
2245            "PrimitiveArray<Timestamp(µs)>
2246[
2247  Cast error: Failed to convert 9223372036854775807 to timestamp for Timestamp(µs),
2248  Cast error: Failed to convert -9223372036854775808 to timestamp for Timestamp(µs),
2249  null,
2250]",
2251            format!("{arr:?}")
2252        );
2253
2254        // Nanoseconds always in range
2255        let arr = data.reinterpret_cast::<TimestampNanosecondType>();
2256        assert_eq!(
2257            "PrimitiveArray<Timestamp(ns)>
2258[
2259  2262-04-11T23:47:16.854775807,
2260  1677-09-21T00:12:43.145224192,
2261  null,
2262]",
2263            format!("{arr:?}")
2264        );
2265    }
2266
2267    #[test]
2268    fn test_timestamp_utc_fmt_debug() {
2269        let arr: PrimitiveArray<TimestampMillisecondType> =
2270            TimestampMillisecondArray::from(vec![1546214400000, 1546214400000, -1546214400000])
2271                .with_timezone_utc();
2272        assert_eq!(
2273            "PrimitiveArray<Timestamp(ms, \"+00:00\")>\n[\n  2018-12-31T00:00:00+00:00,\n  2018-12-31T00:00:00+00:00,\n  1921-01-02T00:00:00+00:00,\n]",
2274            format!("{arr:?}")
2275        );
2276    }
2277
2278    #[test]
2279    #[cfg(feature = "chrono-tz")]
2280    fn test_timestamp_with_named_tz_fmt_debug() {
2281        let arr: PrimitiveArray<TimestampMillisecondType> =
2282            TimestampMillisecondArray::from(vec![1546214400000, 1546214400000, -1546214400000])
2283                .with_timezone("Asia/Taipei".to_string());
2284        assert_eq!(
2285            "PrimitiveArray<Timestamp(ms, \"Asia/Taipei\")>\n[\n  2018-12-31T08:00:00+08:00,\n  2018-12-31T08:00:00+08:00,\n  1921-01-02T08:00:00+08:00,\n]",
2286            format!("{arr:?}")
2287        );
2288    }
2289
2290    #[test]
2291    #[cfg(not(feature = "chrono-tz"))]
2292    fn test_timestamp_with_named_tz_fmt_debug() {
2293        let arr: PrimitiveArray<TimestampMillisecondType> =
2294            TimestampMillisecondArray::from(vec![1546214400000, 1546214400000, -1546214400000])
2295                .with_timezone("Asia/Taipei".to_string());
2296
2297        println!("{arr:?}");
2298
2299        assert_eq!(
2300            "PrimitiveArray<Timestamp(ms, \"Asia/Taipei\")>\n[\n  2018-12-31T00:00:00 (Unknown Time Zone 'Asia/Taipei'),\n  2018-12-31T00:00:00 (Unknown Time Zone 'Asia/Taipei'),\n  1921-01-02T00:00:00 (Unknown Time Zone 'Asia/Taipei'),\n]",
2301            format!("{arr:?}")
2302        );
2303    }
2304
2305    #[test]
2306    fn test_timestamp_with_fixed_offset_tz_fmt_debug() {
2307        let arr: PrimitiveArray<TimestampMillisecondType> =
2308            TimestampMillisecondArray::from(vec![1546214400000, 1546214400000, -1546214400000])
2309                .with_timezone("+08:00".to_string());
2310        assert_eq!(
2311            "PrimitiveArray<Timestamp(ms, \"+08:00\")>\n[\n  2018-12-31T08:00:00+08:00,\n  2018-12-31T08:00:00+08:00,\n  1921-01-02T08:00:00+08:00,\n]",
2312            format!("{arr:?}")
2313        );
2314    }
2315
2316    #[test]
2317    fn test_timestamp_with_incorrect_tz_fmt_debug() {
2318        let arr: PrimitiveArray<TimestampMillisecondType> =
2319            TimestampMillisecondArray::from(vec![1546214400000, 1546214400000, -1546214400000])
2320                .with_timezone("xxx".to_string());
2321        assert_eq!(
2322            "PrimitiveArray<Timestamp(ms, \"xxx\")>\n[\n  2018-12-31T00:00:00 (Unknown Time Zone 'xxx'),\n  2018-12-31T00:00:00 (Unknown Time Zone 'xxx'),\n  1921-01-02T00:00:00 (Unknown Time Zone 'xxx'),\n]",
2323            format!("{arr:?}")
2324        );
2325    }
2326
2327    #[test]
2328    #[cfg(feature = "chrono-tz")]
2329    fn test_timestamp_with_tz_with_daylight_saving_fmt_debug() {
2330        let arr: PrimitiveArray<TimestampMillisecondType> = TimestampMillisecondArray::from(vec![
2331            1647161999000,
2332            1647162000000,
2333            1667717999000,
2334            1667718000000,
2335        ])
2336        .with_timezone("America/Denver".to_string());
2337        assert_eq!(
2338            "PrimitiveArray<Timestamp(ms, \"America/Denver\")>\n[\n  2022-03-13T01:59:59-07:00,\n  2022-03-13T03:00:00-06:00,\n  2022-11-06T00:59:59-06:00,\n  2022-11-06T01:00:00-06:00,\n]",
2339            format!("{arr:?}")
2340        );
2341    }
2342
2343    #[test]
2344    fn test_date32_fmt_debug() {
2345        let arr: PrimitiveArray<Date32Type> = vec![12356, 13548, -365].into();
2346        assert_eq!(
2347            "PrimitiveArray<Date32>\n[\n  2003-10-31,\n  2007-02-04,\n  1969-01-01,\n]",
2348            format!("{arr:?}")
2349        );
2350    }
2351
2352    #[test]
2353    fn test_time32second_fmt_debug() {
2354        let arr: PrimitiveArray<Time32SecondType> = vec![7201, 60054].into();
2355        assert_eq!(
2356            "PrimitiveArray<Time32(s)>\n[\n  02:00:01,\n  16:40:54,\n]",
2357            format!("{arr:?}")
2358        );
2359    }
2360
2361    #[test]
2362    fn test_time32second_invalid_neg() {
2363        // chrono::NaiveDatetime::from_timestamp_opt returns None while input is invalid
2364        let arr: PrimitiveArray<Time32SecondType> = vec![-7201, -60054].into();
2365        assert_eq!(
2366            "PrimitiveArray<Time32(s)>\n[\n  Cast error: Failed to convert -7201 to temporal for Time32(s),\n  Cast error: Failed to convert -60054 to temporal for Time32(s),\n]",
2367            // "PrimitiveArray<Time32(s)>\n[\n  null,\n  null,\n]",
2368            format!("{arr:?}")
2369        )
2370    }
2371
2372    #[test]
2373    fn test_primitive_array_builder() {
2374        // Test building a primitive array with ArrayData builder and offset
2375        let buf = Buffer::from_slice_ref([0i32, 1, 2, 3, 4, 5, 6]);
2376        let buf2 = buf.slice_with_length(8, 20);
2377        let data = ArrayData::builder(DataType::Int32)
2378            .len(5)
2379            .offset(2)
2380            .add_buffer(buf)
2381            .build()
2382            .unwrap();
2383        let arr = Int32Array::from(data);
2384        assert_eq!(&buf2, arr.values.inner());
2385        assert_eq!(5, arr.len());
2386        assert_eq!(0, arr.null_count());
2387        for i in 0..3 {
2388            assert_eq!((i + 2) as i32, arr.value(i));
2389        }
2390    }
2391
2392    #[test]
2393    fn test_primitive_from_iter_values() {
2394        // Test building a primitive array with from_iter_values
2395        let arr: PrimitiveArray<Int32Type> = PrimitiveArray::from_iter_values(0..10);
2396        assert_eq!(10, arr.len());
2397        assert_eq!(0, arr.null_count());
2398        for i in 0..10i32 {
2399            assert_eq!(i, arr.value(i as usize));
2400        }
2401    }
2402
2403    #[test]
2404    fn test_primitive_array_from_unbound_iter() {
2405        // iterator that doesn't declare (upper) size bound
2406        let value_iter = (0..)
2407            .scan(0usize, |pos, i| {
2408                if *pos < 10 {
2409                    *pos += 1;
2410                    Some(Some(i))
2411                } else {
2412                    // actually returns up to 10 values
2413                    None
2414                }
2415            })
2416            // limited using take()
2417            .take(100);
2418
2419        let (_, upper_size_bound) = value_iter.size_hint();
2420        // the upper bound, defined by take above, is 100
2421        assert_eq!(upper_size_bound, Some(100));
2422        let primitive_array: PrimitiveArray<Int32Type> = value_iter.collect();
2423        // but the actual number of items in the array should be 10
2424        assert_eq!(primitive_array.len(), 10);
2425    }
2426
2427    #[test]
2428    fn test_primitive_array_from_non_null_iter() {
2429        let iter = (0..10_i32).map(Some);
2430        let primitive_array = PrimitiveArray::<Int32Type>::from_iter(iter);
2431        assert_eq!(primitive_array.len(), 10);
2432        assert_eq!(primitive_array.null_count(), 0);
2433        assert!(primitive_array.nulls().is_none());
2434        assert_eq!(primitive_array.values(), &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
2435    }
2436
2437    #[test]
2438    #[should_panic(expected = "PrimitiveArray data should contain a single buffer only \
2439                               (values buffer)")]
2440    // Different error messages, so skip for now
2441    // https://github.com/apache/arrow-rs/issues/1545
2442    #[cfg(not(feature = "force_validate"))]
2443    fn test_primitive_array_invalid_buffer_len() {
2444        let buffer = Buffer::from_slice_ref([0i32, 1, 2, 3, 4]);
2445        let data = unsafe {
2446            ArrayData::builder(DataType::Int32)
2447                .add_buffer(buffer.clone())
2448                .add_buffer(buffer)
2449                .len(5)
2450                .build_unchecked()
2451        };
2452
2453        drop(Int32Array::from(data));
2454    }
2455
2456    #[test]
2457    fn test_access_array_concurrently() {
2458        let a = Int32Array::from(vec![5, 6, 7, 8, 9]);
2459        let ret = std::thread::spawn(move || a.value(3)).join();
2460
2461        assert!(ret.is_ok());
2462        assert_eq!(8, ret.ok().unwrap());
2463    }
2464
2465    #[test]
2466    fn test_primitive_array_creation() {
2467        let array1: Int8Array = [10_i8, 11, 12, 13, 14].into_iter().collect();
2468        let array2: Int8Array = [10_i8, 11, 12, 13, 14].into_iter().map(Some).collect();
2469
2470        assert_eq!(array1, array2);
2471    }
2472
2473    #[test]
2474    #[should_panic(
2475        expected = "Trying to access an element at index 4 from a PrimitiveArray of length 3"
2476    )]
2477    fn test_string_array_get_value_index_out_of_bound() {
2478        let array: Int8Array = [10_i8, 11, 12].into_iter().collect();
2479
2480        array.value(4);
2481    }
2482
2483    #[test]
2484    #[should_panic(expected = "PrimitiveArray expected data type Int64 got Int32")]
2485    fn test_from_array_data_validation() {
2486        let foo = PrimitiveArray::<Int32Type>::from_iter([1, 2, 3]);
2487        let _ = PrimitiveArray::<Int64Type>::from(foo.into_data());
2488    }
2489
2490    #[test]
2491    fn test_decimal32() {
2492        let values: Vec<_> = vec![0, 1, -1, i32::MIN, i32::MAX];
2493        let array: PrimitiveArray<Decimal32Type> =
2494            PrimitiveArray::from_iter(values.iter().copied());
2495        assert_eq!(array.values(), &values);
2496
2497        let array: PrimitiveArray<Decimal32Type> =
2498            PrimitiveArray::from_iter_values(values.iter().copied());
2499        assert_eq!(array.values(), &values);
2500
2501        let array = PrimitiveArray::<Decimal32Type>::from(values.clone());
2502        assert_eq!(array.values(), &values);
2503
2504        let array = PrimitiveArray::<Decimal32Type>::from(array.to_data());
2505        assert_eq!(array.values(), &values);
2506    }
2507
2508    #[test]
2509    fn test_decimal64() {
2510        let values: Vec<_> = vec![0, 1, -1, i64::MIN, i64::MAX];
2511        let array: PrimitiveArray<Decimal64Type> =
2512            PrimitiveArray::from_iter(values.iter().copied());
2513        assert_eq!(array.values(), &values);
2514
2515        let array: PrimitiveArray<Decimal64Type> =
2516            PrimitiveArray::from_iter_values(values.iter().copied());
2517        assert_eq!(array.values(), &values);
2518
2519        let array = PrimitiveArray::<Decimal64Type>::from(values.clone());
2520        assert_eq!(array.values(), &values);
2521
2522        let array = PrimitiveArray::<Decimal64Type>::from(array.to_data());
2523        assert_eq!(array.values(), &values);
2524    }
2525
2526    #[test]
2527    fn test_decimal128() {
2528        let values: Vec<_> = vec![0, 1, -1, i128::MIN, i128::MAX];
2529        let array: PrimitiveArray<Decimal128Type> =
2530            PrimitiveArray::from_iter(values.iter().copied());
2531        assert_eq!(array.values(), &values);
2532
2533        let array: PrimitiveArray<Decimal128Type> =
2534            PrimitiveArray::from_iter_values(values.iter().copied());
2535        assert_eq!(array.values(), &values);
2536
2537        let array = PrimitiveArray::<Decimal128Type>::from(values.clone());
2538        assert_eq!(array.values(), &values);
2539
2540        let array = PrimitiveArray::<Decimal128Type>::from(array.to_data());
2541        assert_eq!(array.values(), &values);
2542    }
2543
2544    #[test]
2545    fn test_decimal256() {
2546        let values: Vec<_> = vec![i256::ZERO, i256::ONE, i256::MINUS_ONE, i256::MIN, i256::MAX];
2547
2548        let array: PrimitiveArray<Decimal256Type> =
2549            PrimitiveArray::from_iter(values.iter().copied());
2550        assert_eq!(array.values(), &values);
2551
2552        let array: PrimitiveArray<Decimal256Type> =
2553            PrimitiveArray::from_iter_values(values.iter().copied());
2554        assert_eq!(array.values(), &values);
2555
2556        let array = PrimitiveArray::<Decimal256Type>::from(values.clone());
2557        assert_eq!(array.values(), &values);
2558
2559        let array = PrimitiveArray::<Decimal256Type>::from(array.to_data());
2560        assert_eq!(array.values(), &values);
2561    }
2562
2563    #[test]
2564    fn test_decimal_array() {
2565        // let val_8887: [u8; 16] = [192, 219, 180, 17, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
2566        // let val_neg_8887: [u8; 16] = [64, 36, 75, 238, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255];
2567        let values: [u8; 32] = [
2568            192, 219, 180, 17, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 36, 75, 238, 253, 255, 255,
2569            255, 255, 255, 255, 255, 255, 255, 255, 255,
2570        ];
2571        let array_data = ArrayData::builder(DataType::Decimal128(38, 6))
2572            .len(2)
2573            .add_buffer(Buffer::from(&values))
2574            .build()
2575            .unwrap();
2576        let decimal_array = Decimal128Array::from(array_data);
2577        assert_eq!(8_887_000_000_i128, decimal_array.value(0));
2578        assert_eq!(-8_887_000_000_i128, decimal_array.value(1));
2579    }
2580
2581    #[test]
2582    fn test_decimal_append_error_value() {
2583        let mut decimal_builder = Decimal128Builder::with_capacity(10);
2584        decimal_builder.append_value(123456);
2585        decimal_builder.append_value(12345);
2586        let result = decimal_builder.finish().with_precision_and_scale(5, 3);
2587        assert!(result.is_ok());
2588        let arr = result.unwrap();
2589        assert_eq!("12.345", arr.value_as_string(1));
2590
2591        // Validate it explicitly
2592        let result = arr.validate_decimal_precision(5);
2593        let error = result.unwrap_err();
2594        assert_eq!(
2595            "Invalid argument error: 123.456 is too large to store in a Decimal128 of precision 5. Max is 99.999",
2596            error.to_string()
2597        );
2598
2599        decimal_builder = Decimal128Builder::new();
2600        decimal_builder.append_value(100);
2601        decimal_builder.append_value(99);
2602        decimal_builder.append_value(-100);
2603        decimal_builder.append_value(-99);
2604        let result = decimal_builder.finish().with_precision_and_scale(2, 1);
2605        assert!(result.is_ok());
2606        let arr = result.unwrap();
2607        assert_eq!("9.9", arr.value_as_string(1));
2608        assert_eq!("-9.9", arr.value_as_string(3));
2609
2610        // Validate it explicitly
2611        let result = arr.validate_decimal_precision(2);
2612        let error = result.unwrap_err();
2613        assert_eq!(
2614            "Invalid argument error: 10.0 is too large to store in a Decimal128 of precision 2. Max is 9.9",
2615            error.to_string()
2616        );
2617    }
2618
2619    #[test]
2620    fn test_decimal_validate_precision_with_negative_scale() {
2621        fn check<T: DecimalType>()
2622        where
2623            T::Native: From<i32>,
2624        {
2625            for scale in [i8::MIN, -2, -1, 0, 2] {
2626                let array = PrimitiveArray::<T>::from_iter(
2627                    [Some(-99), Some(0), Some(1), Some(99), None]
2628                        .into_iter()
2629                        .map(|v| v.map(T::Native::from)),
2630                )
2631                .with_precision_and_scale(2, scale)
2632                .unwrap();
2633                array.validate_decimal_precision(2).unwrap();
2634
2635                for value in [-100, 100] {
2636                    let array = PrimitiveArray::<T>::from_iter_values([T::Native::from(value)])
2637                        .with_precision_and_scale(3, scale)
2638                        .unwrap();
2639                    assert!(array.validate_decimal_precision(2).is_err());
2640                }
2641            }
2642
2643            let array = PrimitiveArray::<T>::from_iter_values([T::Native::from(100)])
2644                .with_precision_and_scale(3, -2)
2645                .unwrap();
2646            assert_eq!(
2647                array.validate_decimal_precision(2).unwrap_err().to_string(),
2648                format!(
2649                    "Invalid argument error: 10000 is too large to store in a {} of precision 2. Max is 9900",
2650                    T::PREFIX
2651                )
2652            );
2653
2654            let array = PrimitiveArray::<T>::from_iter_values([T::Native::from(0)])
2655                .with_precision_and_scale(2, 2)
2656                .unwrap();
2657            assert_eq!(
2658                array.validate_decimal_precision(1).unwrap_err().to_string(),
2659                "Invalid argument error: Decimal precision 1 is less than scale 2"
2660            );
2661        }
2662
2663        check::<Decimal32Type>();
2664        check::<Decimal64Type>();
2665        check::<Decimal128Type>();
2666        check::<Decimal256Type>();
2667    }
2668
2669    #[test]
2670    fn test_decimal_from_iter_values() {
2671        let array = Decimal128Array::from_iter_values(vec![-100, 0, 101]);
2672        assert_eq!(array.len(), 3);
2673        assert_eq!(array.data_type(), &DataType::Decimal128(38, 10));
2674        assert_eq!(-100_i128, array.value(0));
2675        assert!(!array.is_null(0));
2676        assert_eq!(0_i128, array.value(1));
2677        assert!(!array.is_null(1));
2678        assert_eq!(101_i128, array.value(2));
2679        assert!(!array.is_null(2));
2680    }
2681
2682    #[test]
2683    fn test_decimal_from_iter() {
2684        let array: Decimal128Array = vec![Some(-100), None, Some(101)].into_iter().collect();
2685        assert_eq!(array.len(), 3);
2686        assert_eq!(array.data_type(), &DataType::Decimal128(38, 10));
2687        assert_eq!(-100_i128, array.value(0));
2688        assert!(!array.is_null(0));
2689        assert!(array.is_null(1));
2690        assert_eq!(101_i128, array.value(2));
2691        assert!(!array.is_null(2));
2692    }
2693
2694    #[test]
2695    fn test_decimal_iter_sized() {
2696        let data = vec![Some(-100), None, Some(101)];
2697        let array: Decimal128Array = data.into_iter().collect();
2698        let mut iter = array.into_iter();
2699
2700        // is exact sized
2701        assert_eq!(array.len(), 3);
2702
2703        // size_hint is reported correctly
2704        assert_eq!(iter.size_hint(), (3, Some(3)));
2705        iter.next().unwrap();
2706        assert_eq!(iter.size_hint(), (2, Some(2)));
2707        iter.next().unwrap();
2708        iter.next().unwrap();
2709        assert_eq!(iter.size_hint(), (0, Some(0)));
2710        assert!(iter.next().is_none());
2711        assert_eq!(iter.size_hint(), (0, Some(0)));
2712    }
2713
2714    #[test]
2715    fn test_decimal_array_value_as_string() {
2716        let arr = [123450, -123450, 100, -100, 10, -10, 0]
2717            .into_iter()
2718            .map(Some)
2719            .collect::<Decimal128Array>()
2720            .with_precision_and_scale(6, 3)
2721            .unwrap();
2722
2723        assert_eq!("123.450", arr.value_as_string(0));
2724        assert_eq!("-123.450", arr.value_as_string(1));
2725        assert_eq!("0.100", arr.value_as_string(2));
2726        assert_eq!("-0.100", arr.value_as_string(3));
2727        assert_eq!("0.010", arr.value_as_string(4));
2728        assert_eq!("-0.010", arr.value_as_string(5));
2729        assert_eq!("0.000", arr.value_as_string(6));
2730    }
2731
2732    #[test]
2733    fn test_decimal_array_with_precision_and_scale() {
2734        let arr = Decimal128Array::from_iter_values([12345, 456, 7890, -123223423432432])
2735            .with_precision_and_scale(20, 2)
2736            .unwrap();
2737
2738        assert_eq!(arr.data_type(), &DataType::Decimal128(20, 2));
2739        assert_eq!(arr.precision(), 20);
2740        assert_eq!(arr.scale(), 2);
2741
2742        let actual: Vec<_> = (0..arr.len()).map(|i| arr.value_as_string(i)).collect();
2743        let expected = vec!["123.45", "4.56", "78.90", "-1232234234324.32"];
2744
2745        assert_eq!(actual, expected);
2746    }
2747
2748    #[test]
2749    #[should_panic(
2750        expected = "-1232234234324.32 is too small to store in a Decimal128 of precision 5. Min is -999.99"
2751    )]
2752    fn test_decimal_array_with_precision_and_scale_out_of_range() {
2753        let arr = Decimal128Array::from_iter_values([12345, 456, 7890, -123223423432432])
2754            // precision is too small to hold value
2755            .with_precision_and_scale(5, 2)
2756            .unwrap();
2757        arr.validate_decimal_precision(5).unwrap();
2758    }
2759
2760    #[test]
2761    #[should_panic(expected = "precision cannot be 0, has to be between [1, 38]")]
2762    fn test_decimal_array_with_precision_zero() {
2763        Decimal128Array::from_iter_values([12345, 456])
2764            .with_precision_and_scale(0, 2)
2765            .unwrap();
2766    }
2767
2768    #[test]
2769    #[should_panic(expected = "precision 40 is greater than max 38")]
2770    fn test_decimal_array_with_precision_and_scale_invalid_precision() {
2771        Decimal128Array::from_iter_values([12345, 456])
2772            .with_precision_and_scale(40, 2)
2773            .unwrap();
2774    }
2775
2776    #[test]
2777    #[should_panic(expected = "scale 40 is greater than max 38")]
2778    fn test_decimal_array_with_precision_and_scale_invalid_scale() {
2779        Decimal128Array::from_iter_values([12345, 456])
2780            .with_precision_and_scale(20, 40)
2781            .unwrap();
2782    }
2783
2784    #[test]
2785    #[should_panic(expected = "scale 10 is greater than precision 4")]
2786    fn test_decimal_array_with_precision_and_scale_invalid_precision_and_scale() {
2787        Decimal128Array::from_iter_values([12345, 456])
2788            .with_precision_and_scale(4, 10)
2789            .unwrap();
2790    }
2791
2792    #[test]
2793    fn test_decimal_array_set_null_if_overflow_with_precision() {
2794        let array = Decimal128Array::from(vec![Some(123456), Some(123), None, Some(123456)]);
2795        let result = array.null_if_overflow_precision(5);
2796        let expected = Decimal128Array::from(vec![None, Some(123), None, None]);
2797        assert_eq!(result, expected);
2798    }
2799
2800    #[test]
2801    fn test_decimal256_iter() {
2802        let mut builder = Decimal256Builder::with_capacity(30);
2803        let decimal1 = i256::from_i128(12345);
2804        builder.append_value(decimal1);
2805
2806        builder.append_null();
2807
2808        let decimal2 = i256::from_i128(56789);
2809        builder.append_value(decimal2);
2810
2811        let array: Decimal256Array = builder.finish().with_precision_and_scale(76, 6).unwrap();
2812
2813        let collected: Vec<_> = array.iter().collect();
2814        assert_eq!(vec![Some(decimal1), None, Some(decimal2)], collected);
2815    }
2816
2817    #[test]
2818    fn test_from_iter_decimal256array() {
2819        let value1 = i256::from_i128(12345);
2820        let value2 = i256::from_i128(56789);
2821
2822        let mut array: Decimal256Array =
2823            vec![Some(value1), None, Some(value2)].into_iter().collect();
2824        array = array.with_precision_and_scale(76, 10).unwrap();
2825        assert_eq!(array.len(), 3);
2826        assert_eq!(array.data_type(), &DataType::Decimal256(76, 10));
2827        assert_eq!(value1, array.value(0));
2828        assert!(!array.is_null(0));
2829        assert!(array.is_null(1));
2830        assert_eq!(value2, array.value(2));
2831        assert!(!array.is_null(2));
2832    }
2833
2834    #[test]
2835    fn test_from_iter_decimal128array() {
2836        let mut array: Decimal128Array = vec![Some(-100), None, Some(101)].into_iter().collect();
2837        array = array.with_precision_and_scale(38, 10).unwrap();
2838        assert_eq!(array.len(), 3);
2839        assert_eq!(array.data_type(), &DataType::Decimal128(38, 10));
2840        assert_eq!(-100_i128, array.value(0));
2841        assert!(!array.is_null(0));
2842        assert!(array.is_null(1));
2843        assert_eq!(101_i128, array.value(2));
2844        assert!(!array.is_null(2));
2845    }
2846
2847    #[test]
2848    fn test_decimal64_iter() {
2849        let mut builder = Decimal64Builder::with_capacity(30);
2850        let decimal1 = 12345;
2851        builder.append_value(decimal1);
2852
2853        builder.append_null();
2854
2855        let decimal2 = 56789;
2856        builder.append_value(decimal2);
2857
2858        let array: Decimal64Array = builder.finish().with_precision_and_scale(18, 4).unwrap();
2859
2860        let collected: Vec<_> = array.iter().collect();
2861        assert_eq!(vec![Some(decimal1), None, Some(decimal2)], collected);
2862    }
2863
2864    #[test]
2865    fn test_from_iter_decimal64array() {
2866        let value1 = 12345;
2867        let value2 = 56789;
2868
2869        let mut array: Decimal64Array =
2870            vec![Some(value1), None, Some(value2)].into_iter().collect();
2871        array = array.with_precision_and_scale(18, 4).unwrap();
2872        assert_eq!(array.len(), 3);
2873        assert_eq!(array.data_type(), &DataType::Decimal64(18, 4));
2874        assert_eq!(value1, array.value(0));
2875        assert!(!array.is_null(0));
2876        assert!(array.is_null(1));
2877        assert_eq!(value2, array.value(2));
2878        assert!(!array.is_null(2));
2879    }
2880
2881    #[test]
2882    fn test_decimal32_iter() {
2883        let mut builder = Decimal32Builder::with_capacity(30);
2884        let decimal1 = 12345;
2885        builder.append_value(decimal1);
2886
2887        builder.append_null();
2888
2889        let decimal2 = 56789;
2890        builder.append_value(decimal2);
2891
2892        let array: Decimal32Array = builder.finish().with_precision_and_scale(9, 2).unwrap();
2893
2894        let collected: Vec<_> = array.iter().collect();
2895        assert_eq!(vec![Some(decimal1), None, Some(decimal2)], collected);
2896    }
2897
2898    #[test]
2899    fn test_from_iter_decimal32array() {
2900        let value1 = 12345;
2901        let value2 = 56789;
2902
2903        let mut array: Decimal32Array =
2904            vec![Some(value1), None, Some(value2)].into_iter().collect();
2905        array = array.with_precision_and_scale(9, 2).unwrap();
2906        assert_eq!(array.len(), 3);
2907        assert_eq!(array.data_type(), &DataType::Decimal32(9, 2));
2908        assert_eq!(value1, array.value(0));
2909        assert!(!array.is_null(0));
2910        assert!(array.is_null(1));
2911        assert_eq!(value2, array.value(2));
2912        assert!(!array.is_null(2));
2913    }
2914
2915    #[test]
2916    fn test_unary_opt() {
2917        let array = Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7]);
2918        let r = array.unary_opt::<_, Int32Type>(|x| (x % 2 != 0).then_some(x));
2919
2920        let expected = Int32Array::from(vec![Some(1), None, Some(3), None, Some(5), None, Some(7)]);
2921        assert_eq!(r, expected);
2922
2923        let r = expected.unary_opt::<_, Int32Type>(|x| (x % 3 != 0).then_some(x));
2924        let expected = Int32Array::from(vec![Some(1), None, None, None, Some(5), None, Some(7)]);
2925        assert_eq!(r, expected);
2926    }
2927
2928    #[test]
2929    #[should_panic(
2930        expected = "Trying to access an element at index 4 from a PrimitiveArray of length 3"
2931    )]
2932    fn test_fixed_size_binary_array_get_value_index_out_of_bound() {
2933        let array = Decimal128Array::from(vec![-100, 0, 101]);
2934        array.value(4);
2935    }
2936
2937    #[test]
2938    fn test_into_builder() {
2939        let array: Int32Array = vec![1, 2, 3].into_iter().map(Some).collect();
2940
2941        let boxed: ArrayRef = Arc::new(array);
2942        let col: Int32Array = downcast_array(&boxed);
2943        drop(boxed);
2944
2945        let mut builder = col.into_builder().unwrap();
2946
2947        let slice = builder.values_slice_mut();
2948        assert_eq!(slice, &[1, 2, 3]);
2949
2950        slice[0] = 4;
2951        slice[1] = 2;
2952        slice[2] = 1;
2953
2954        let expected: Int32Array = vec![Some(4), Some(2), Some(1)].into_iter().collect();
2955
2956        let new_array = builder.finish();
2957        assert_eq!(expected, new_array);
2958    }
2959
2960    #[test]
2961    fn test_into_builder_cloned_array() {
2962        let array: Int32Array = vec![1, 2, 3].into_iter().map(Some).collect();
2963
2964        let boxed: ArrayRef = Arc::new(array);
2965
2966        let col: Int32Array = PrimitiveArray::<Int32Type>::from(boxed.to_data());
2967        let err = col.into_builder();
2968
2969        match err {
2970            Ok(_) => panic!("Should not get builder from cloned array"),
2971            Err(returned) => {
2972                let expected: Int32Array = vec![1, 2, 3].into_iter().map(Some).collect();
2973                assert_eq!(expected, returned)
2974            }
2975        }
2976    }
2977
2978    #[test]
2979    fn test_into_builder_keeps_data_type() {
2980        let array = Decimal128Array::from(vec![1, 2])
2981            .with_precision_and_scale(10, 2)
2982            .unwrap();
2983        let data_type = array.data_type().clone();
2984
2985        // Shared buffers: the array is returned
2986        let shared = array.clone();
2987        let array = array.into_builder().unwrap_err();
2988        assert_eq!(array.data_type(), &data_type);
2989        drop(shared);
2990
2991        // Unshared buffers: the buffers are reused by the builder
2992        let mut builder = array.into_builder().unwrap();
2993        builder.append_value(3);
2994        assert_eq!(builder.finish().data_type(), &data_type);
2995    }
2996
2997    #[test]
2998    fn test_into_builder_on_sliced_array() {
2999        let array: Int32Array = vec![1, 2, 3].into_iter().map(Some).collect();
3000        let slice = array.slice(1, 2);
3001        let col: Int32Array = downcast_array(&slice);
3002
3003        drop(slice);
3004
3005        col.into_builder()
3006            .expect_err("Should not build builder from sliced array");
3007    }
3008
3009    #[test]
3010    fn test_unary_mut() {
3011        let array: Int32Array = vec![1, 2, 3].into_iter().map(Some).collect();
3012
3013        let c = array.unary_mut(|x| x * 2 + 1).unwrap();
3014        let expected: Int32Array = vec![3, 5, 7].into_iter().map(Some).collect();
3015
3016        assert_eq!(expected, c);
3017
3018        let array: Int32Array = Int32Array::from(vec![Some(5), Some(7), None]);
3019        let c = array.unary_mut(|x| x * 2 + 1).unwrap();
3020        assert_eq!(c, Int32Array::from(vec![Some(11), Some(15), None]));
3021    }
3022
3023    #[test]
3024    #[should_panic(
3025        expected = "PrimitiveArray expected data type Interval(MonthDayNano) got Interval(DayTime)"
3026    )]
3027    fn test_invalid_interval_type() {
3028        let array = IntervalDayTimeArray::from(vec![IntervalDayTime::ZERO]);
3029        let _ = IntervalMonthDayNanoArray::from(array.into_data());
3030    }
3031
3032    #[test]
3033    fn test_timezone() {
3034        let array = TimestampNanosecondArray::from_iter_values([1, 2]);
3035        assert_eq!(array.timezone(), None);
3036
3037        let array = array.with_timezone("+02:00");
3038        assert_eq!(array.timezone(), Some("+02:00"));
3039    }
3040
3041    #[test]
3042    fn test_try_new() {
3043        Int32Array::new(vec![1, 2, 3, 4].into(), None);
3044        Int32Array::new(vec![1, 2, 3, 4].into(), Some(NullBuffer::new_null(4)));
3045
3046        let err = Int32Array::try_new(vec![1, 2, 3, 4].into(), Some(NullBuffer::new_null(3)))
3047            .unwrap_err();
3048
3049        assert_eq!(
3050            err.to_string(),
3051            "Invalid argument error: Incorrect length of null buffer for PrimitiveArray, expected 4 got 3"
3052        );
3053
3054        TimestampNanosecondArray::new(vec![1, 2, 3, 4].into(), None).with_data_type(
3055            DataType::Timestamp(TimeUnit::Nanosecond, Some("03:00".into())),
3056        );
3057    }
3058
3059    #[test]
3060    #[should_panic(expected = "PrimitiveArray expected data type Int32 got Date32")]
3061    fn test_with_data_type() {
3062        Int32Array::new(vec![1, 2, 3, 4].into(), None).with_data_type(DataType::Date32);
3063    }
3064
3065    #[test]
3066    fn test_time_32second_output() {
3067        let array: Time32SecondArray = vec![
3068            Some(-1),
3069            Some(0),
3070            Some(86_399),
3071            Some(86_400),
3072            Some(86_401),
3073            None,
3074        ]
3075        .into();
3076        let debug_str = format!("{array:?}");
3077        assert_eq!(
3078            "PrimitiveArray<Time32(s)>\n[\n  Cast error: Failed to convert -1 to temporal for Time32(s),\n  00:00:00,\n  23:59:59,\n  Cast error: Failed to convert 86400 to temporal for Time32(s),\n  Cast error: Failed to convert 86401 to temporal for Time32(s),\n  null,\n]",
3079            debug_str
3080        );
3081    }
3082
3083    #[test]
3084    fn test_time_32millisecond_debug_output() {
3085        let array: Time32MillisecondArray = vec![
3086            Some(-1),
3087            Some(0),
3088            Some(86_399_000),
3089            Some(86_400_000),
3090            Some(86_401_000),
3091            None,
3092        ]
3093        .into();
3094        let debug_str = format!("{array:?}");
3095        assert_eq!(
3096            "PrimitiveArray<Time32(ms)>\n[\n  Cast error: Failed to convert -1 to temporal for Time32(ms),\n  00:00:00,\n  23:59:59,\n  Cast error: Failed to convert 86400000 to temporal for Time32(ms),\n  Cast error: Failed to convert 86401000 to temporal for Time32(ms),\n  null,\n]",
3097            debug_str
3098        );
3099    }
3100
3101    #[test]
3102    fn test_time_64nanosecond_debug_output() {
3103        let array: Time64NanosecondArray = vec![
3104            Some(-1),
3105            Some(0),
3106            Some(86_399 * 1_000_000_000),
3107            Some(86_400 * 1_000_000_000),
3108            Some(86_401 * 1_000_000_000),
3109            None,
3110        ]
3111        .into();
3112        let debug_str = format!("{array:?}");
3113        assert_eq!(
3114            "PrimitiveArray<Time64(ns)>\n[\n  Cast error: Failed to convert -1 to temporal for Time64(ns),\n  00:00:00,\n  23:59:59,\n  Cast error: Failed to convert 86400000000000 to temporal for Time64(ns),\n  Cast error: Failed to convert 86401000000000 to temporal for Time64(ns),\n  null,\n]",
3115            debug_str
3116        );
3117    }
3118
3119    #[test]
3120    fn test_time_64microsecond_debug_output() {
3121        let array: Time64MicrosecondArray = vec![
3122            Some(-1),
3123            Some(0),
3124            Some(86_399 * 1_000_000),
3125            Some(86_400 * 1_000_000),
3126            Some(86_401 * 1_000_000),
3127            None,
3128        ]
3129        .into();
3130        let debug_str = format!("{array:?}");
3131        assert_eq!(
3132            "PrimitiveArray<Time64(µs)>\n[\n  Cast error: Failed to convert -1 to temporal for Time64(µs),\n  00:00:00,\n  23:59:59,\n  Cast error: Failed to convert 86400000000 to temporal for Time64(µs),\n  Cast error: Failed to convert 86401000000 to temporal for Time64(µs),\n  null,\n]",
3133            debug_str
3134        );
3135    }
3136
3137    #[test]
3138    fn test_primitive_with_nulls_into_builder() {
3139        let array: Int32Array = vec![
3140            Some(1),
3141            None,
3142            Some(3),
3143            Some(4),
3144            None,
3145            Some(7),
3146            None,
3147            Some(8),
3148        ]
3149        .into_iter()
3150        .collect();
3151        let _ = array.into_builder();
3152    }
3153}