## Coverage Report

#### Created: 2025-11-17 14:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)

    /Users/andrewlamb/Software/arrow-rs/arrow-array/src/array/primitive_array.rs

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