minarrow 0.10.1

Apache Arrow-compatible, Rust-first columnar data library for high-performance computing, native streaming, and embedded workloads. Minimal dependencies, ultra-low-latency access, automatic 64-byte SIMD alignment, and fast compile times. Great for real-time analytics, HPC pipelines, and systems integration.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
// Copyright 2025 Peter Garfield Bower
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! # **NumericArray Module** - *High-Level Numerical Array Type for Unified Signature Dispatch*
//!
//! NumericArray unifies all integer and floating-point arrays
//! into a single enum for standardised numeric operations.
//!   
//! ## Features
//! - direct variant access
//! - zero-cost casts when the type is known
//! - lossless conversions between integer and float types.
//! - simplifies function signatures by accepting `impl Into<NumericArray>`
//! - centralises dispatch
//! - preserves SIMD-aligned buffers across all numeric variants.

use std::{
    fmt::{Display, Formatter},
    sync::Arc,
};

use crate::{Bitmask, FloatArray, IntegerArray, MaskedArray, Vec64};
use crate::{BooleanArray, StringArray};
use crate::{
    enums::{error::MinarrowError, shape_dim::ShapeDim},
    traits::{concatenate::Concatenate, shape::Shape},
};

/// # NumericArray
///
/// Unified numerical array container
///
/// ## Purpose
/// Exists to unify numerical operations,
/// simplify API's and streamline user ergonomics.
///
/// ## Usage:
/// - It is accessible from `Array` using `.num()`,
/// and provides typed variant access via for e.g.,
/// `.i64()`, so one can drill down to the required
/// granularity via `myarr.num().i64()`
/// - This streamlines function implementations,
/// and, despite the additional `enum` layer,
/// matching lanes in many real-world scenarios.
/// This is because one can for e.g., unify a
/// function signature with `impl Into<NumericArray>`,
/// and all of the subtypes, plus `Array` and `NumericalArray`,
/// all qualify.
/// - Additionally, you can then use one `Integer` implementation
/// on the enum dispatch arm for all `Integer` variants, or,
/// in many cases, for the entire numeric arm when they are the same.
///
/// ### Typecasting behaviour
/// - If the enum already holds the given type *(which should be known at compile-time)*,
/// then using accessors like `.i32()` is zero-cost, as it transfers ownership.
/// - If you want to keep the original, of course use `.clone()` beforehand.
/// - If you use an accessor to a different base type, e.g., `.f32()` when it's a
/// `.int32()` already in the enum, it will convert it. Therefore, be mindful
/// of performance when this occurs.
///
/// ## Also see:
/// - Under [crate::traits::type_unions] , we additionally
/// include minimal `Integer`, `Float`, `Numeric` and `Primitive` traits that
/// for which the base Rust primitive types already qualify.
/// These are loose wrappers over the `num-traits` crate to help improve
/// type ergonomics when traits are required, but without requiring
/// any downcasting.
#[repr(C, align(64))]
#[derive(PartialEq, Clone, Debug, Default)]
pub enum NumericArray {
    #[cfg(feature = "extended_numeric_types")]
    Int8(Arc<IntegerArray<i8>>),
    #[cfg(feature = "extended_numeric_types")]
    Int16(Arc<IntegerArray<i16>>),
    Int32(Arc<IntegerArray<i32>>),
    Int64(Arc<IntegerArray<i64>>),
    #[cfg(feature = "extended_numeric_types")]
    UInt8(Arc<IntegerArray<u8>>),
    #[cfg(feature = "extended_numeric_types")]
    UInt16(Arc<IntegerArray<u16>>),
    UInt32(Arc<IntegerArray<u32>>),
    UInt64(Arc<IntegerArray<u64>>),
    Float32(Arc<FloatArray<f32>>),
    Float64(Arc<FloatArray<f64>>),
    #[default]
    Null, // Default Marker for mem::take
}

impl NumericArray {
    /// Returns the logical length of the numeric array.
    #[inline]
    pub fn len(&self) -> usize {
        match self {
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int8(arr) => arr.len(),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int16(arr) => arr.len(),
            NumericArray::Int32(arr) => arr.len(),
            NumericArray::Int64(arr) => arr.len(),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt8(arr) => arr.len(),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt16(arr) => arr.len(),
            NumericArray::UInt32(arr) => arr.len(),
            NumericArray::UInt64(arr) => arr.len(),
            NumericArray::Float32(arr) => arr.len(),
            NumericArray::Float64(arr) => arr.len(),
            NumericArray::Null => 0,
        }
    }

    /// Returns the underlying null mask, if any.
    #[inline]
    pub fn null_mask(&self) -> Option<&Bitmask> {
        match self {
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int8(arr) => arr.null_mask.as_ref(),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int16(arr) => arr.null_mask.as_ref(),
            NumericArray::Int32(arr) => arr.null_mask.as_ref(),
            NumericArray::Int64(arr) => arr.null_mask.as_ref(),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt8(arr) => arr.null_mask.as_ref(),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt16(arr) => arr.null_mask.as_ref(),
            NumericArray::UInt32(arr) => arr.null_mask.as_ref(),
            NumericArray::UInt64(arr) => arr.null_mask.as_ref(),
            NumericArray::Float32(arr) => arr.null_mask.as_ref(),
            NumericArray::Float64(arr) => arr.null_mask.as_ref(),
            NumericArray::Null => None,
        }
    }

    /// Appends all values (and null mask if present) from `other` into `self`.
    ///
    /// Panics if the two arrays are of different variants or incompatible types.
    ///
    /// This function uses copy-on-write semantics for arrays wrapped in `Arc`.
    /// If `self` is the only owner of its data, appends are performed in place without copying.
    /// If the array data is shared (`Arc` reference count > 1), the data is first cloned
    /// (so the mutation does not affect other owners), and the append is then performed on the unique copy.
    ///
    /// This ensures that calling `append_array` never mutates data referenced elsewhere,
    /// but also avoids unnecessary cloning when the data is uniquely owned.
    pub fn append_array(&mut self, other: &Self) {
        match (self, other) {
            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::Int8(a), NumericArray::Int8(b)) => Arc::make_mut(a).append_array(b),
            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::Int16(a), NumericArray::Int16(b)) => Arc::make_mut(a).append_array(b),
            (NumericArray::Int32(a), NumericArray::Int32(b)) => Arc::make_mut(a).append_array(b),
            (NumericArray::Int64(a), NumericArray::Int64(b)) => Arc::make_mut(a).append_array(b),

            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::UInt8(a), NumericArray::UInt8(b)) => Arc::make_mut(a).append_array(b),
            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::UInt16(a), NumericArray::UInt16(b)) => Arc::make_mut(a).append_array(b),
            (NumericArray::UInt32(a), NumericArray::UInt32(b)) => Arc::make_mut(a).append_array(b),
            (NumericArray::UInt64(a), NumericArray::UInt64(b)) => Arc::make_mut(a).append_array(b),

            (NumericArray::Float32(a), NumericArray::Float32(b)) => {
                Arc::make_mut(a).append_array(b)
            }
            (NumericArray::Float64(a), NumericArray::Float64(b)) => {
                Arc::make_mut(a).append_array(b)
            }

            (NumericArray::Null, NumericArray::Null) => (),
            (lhs, rhs) => panic!("Cannot append {:?} into {:?}", rhs, lhs),
        }
    }

    pub fn append_range(&mut self, other: &Self, offset: usize, len: usize) -> Result<(), MinarrowError> {
        match (self, other) {
            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::Int8(a), NumericArray::Int8(b)) => Arc::make_mut(a).append_range(b, offset, len),
            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::Int16(a), NumericArray::Int16(b)) => Arc::make_mut(a).append_range(b, offset, len),
            (NumericArray::Int32(a), NumericArray::Int32(b)) => Arc::make_mut(a).append_range(b, offset, len),
            (NumericArray::Int64(a), NumericArray::Int64(b)) => Arc::make_mut(a).append_range(b, offset, len),
            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::UInt8(a), NumericArray::UInt8(b)) => Arc::make_mut(a).append_range(b, offset, len),
            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::UInt16(a), NumericArray::UInt16(b)) => Arc::make_mut(a).append_range(b, offset, len),
            (NumericArray::UInt32(a), NumericArray::UInt32(b)) => Arc::make_mut(a).append_range(b, offset, len),
            (NumericArray::UInt64(a), NumericArray::UInt64(b)) => Arc::make_mut(a).append_range(b, offset, len),
            (NumericArray::Float32(a), NumericArray::Float32(b)) => Arc::make_mut(a).append_range(b, offset, len),
            (NumericArray::Float64(a), NumericArray::Float64(b)) => Arc::make_mut(a).append_range(b, offset, len),
            (NumericArray::Null, NumericArray::Null) => Ok(()),
            (lhs, rhs) => Err(MinarrowError::TypeError {
                from: "NumericArray",
                to: "NumericArray",
                message: Some(format!("Cannot append_range {:?} into {:?}", rhs, lhs)),
            }),
        }
    }

    /// Inserts all values (and null mask if present) from `other` into `self` at the specified index.
    ///
    /// This is an **O(n)** operation.
    ///
    /// Returns an error if the two arrays are of different variants or incompatible types,
    /// or if the index is out of bounds.
    ///
    /// This function uses copy-on-write semantics for arrays wrapped in `Arc`.
    pub fn insert_rows(&mut self, index: usize, other: &Self) -> Result<(), MinarrowError> {
        match (self, other) {
            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::Int8(a), NumericArray::Int8(b)) => {
                Arc::make_mut(a).insert_rows(index, b)
            }
            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::Int16(a), NumericArray::Int16(b)) => {
                Arc::make_mut(a).insert_rows(index, b)
            }
            (NumericArray::Int32(a), NumericArray::Int32(b)) => {
                Arc::make_mut(a).insert_rows(index, b)
            }
            (NumericArray::Int64(a), NumericArray::Int64(b)) => {
                Arc::make_mut(a).insert_rows(index, b)
            }

            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::UInt8(a), NumericArray::UInt8(b)) => {
                Arc::make_mut(a).insert_rows(index, b)
            }
            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::UInt16(a), NumericArray::UInt16(b)) => {
                Arc::make_mut(a).insert_rows(index, b)
            }
            (NumericArray::UInt32(a), NumericArray::UInt32(b)) => {
                Arc::make_mut(a).insert_rows(index, b)
            }
            (NumericArray::UInt64(a), NumericArray::UInt64(b)) => {
                Arc::make_mut(a).insert_rows(index, b)
            }

            (NumericArray::Float32(a), NumericArray::Float32(b)) => {
                Arc::make_mut(a).insert_rows(index, b)
            }
            (NumericArray::Float64(a), NumericArray::Float64(b)) => {
                Arc::make_mut(a).insert_rows(index, b)
            }

            (NumericArray::Null, NumericArray::Null) => Ok(()),
            (lhs, rhs) => Err(MinarrowError::TypeError {
                from: "NumericArray",
                to: "NumericArray",
                message: Some(format!(
                    "Cannot insert {} into {}: incompatible types",
                    rhs, lhs
                )),
            }),
        }
    }

    /// Splits the NumericArray at the specified index, consuming self and returning two arrays.
    pub fn split(self, index: usize) -> Result<(Self, Self), MinarrowError> {
        use std::sync::Arc;

        match self {
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int8(a) => {
                let (left, right) = Arc::try_unwrap(a)
                    .unwrap_or_else(|arc| (*arc).clone())
                    .split(index)?;
                Ok((
                    NumericArray::Int8(Arc::new(left)),
                    NumericArray::Int8(Arc::new(right)),
                ))
            }
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt8(a) => {
                let (left, right) = Arc::try_unwrap(a)
                    .unwrap_or_else(|arc| (*arc).clone())
                    .split(index)?;
                Ok((
                    NumericArray::UInt8(Arc::new(left)),
                    NumericArray::UInt8(Arc::new(right)),
                ))
            }
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int16(a) => {
                let (left, right) = Arc::try_unwrap(a)
                    .unwrap_or_else(|arc| (*arc).clone())
                    .split(index)?;
                Ok((
                    NumericArray::Int16(Arc::new(left)),
                    NumericArray::Int16(Arc::new(right)),
                ))
            }
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt16(a) => {
                let (left, right) = Arc::try_unwrap(a)
                    .unwrap_or_else(|arc| (*arc).clone())
                    .split(index)?;
                Ok((
                    NumericArray::UInt16(Arc::new(left)),
                    NumericArray::UInt16(Arc::new(right)),
                ))
            }
            NumericArray::Int32(a) => {
                let (left, right) = Arc::try_unwrap(a)
                    .unwrap_or_else(|arc| (*arc).clone())
                    .split(index)?;
                Ok((
                    NumericArray::Int32(Arc::new(left)),
                    NumericArray::Int32(Arc::new(right)),
                ))
            }
            NumericArray::Int64(a) => {
                let (left, right) = Arc::try_unwrap(a)
                    .unwrap_or_else(|arc| (*arc).clone())
                    .split(index)?;
                Ok((
                    NumericArray::Int64(Arc::new(left)),
                    NumericArray::Int64(Arc::new(right)),
                ))
            }
            NumericArray::UInt32(a) => {
                let (left, right) = Arc::try_unwrap(a)
                    .unwrap_or_else(|arc| (*arc).clone())
                    .split(index)?;
                Ok((
                    NumericArray::UInt32(Arc::new(left)),
                    NumericArray::UInt32(Arc::new(right)),
                ))
            }
            NumericArray::UInt64(a) => {
                let (left, right) = Arc::try_unwrap(a)
                    .unwrap_or_else(|arc| (*arc).clone())
                    .split(index)?;
                Ok((
                    NumericArray::UInt64(Arc::new(left)),
                    NumericArray::UInt64(Arc::new(right)),
                ))
            }
            NumericArray::Float32(a) => {
                let (left, right) = Arc::try_unwrap(a)
                    .unwrap_or_else(|arc| (*arc).clone())
                    .split(index)?;
                Ok((
                    NumericArray::Float32(Arc::new(left)),
                    NumericArray::Float32(Arc::new(right)),
                ))
            }
            NumericArray::Float64(a) => {
                let (left, right) = Arc::try_unwrap(a)
                    .unwrap_or_else(|arc| (*arc).clone())
                    .split(index)?;
                Ok((
                    NumericArray::Float64(Arc::new(left)),
                    NumericArray::Float64(Arc::new(right)),
                ))
            }
            NumericArray::Null => Err(MinarrowError::IndexError(
                "Cannot split Null array".to_string(),
            )),
        }
    }

    /// Returns a reference to the inner `IntegerArray<i32>` if the variant matches.
    /// No conversion or cloning is performed.
    pub fn i32_ref(&self) -> Result<&IntegerArray<i32>, MinarrowError> {
        match self {
            NumericArray::Int32(a) => Ok(a),
            NumericArray::Null => Err(MinarrowError::NullError { message: None }),
            _ => Err(MinarrowError::TypeError {
                from: "NumericArray",
                to: "IntegerArray<i32>",
                message: None,
            }),
        }
    }

    /// Returns a reference to the inner `IntegerArray<i64>` if the variant matches.
    /// No conversion or cloning is performed.
    pub fn i64_ref(&self) -> Result<&IntegerArray<i64>, MinarrowError> {
        match self {
            NumericArray::Int64(a) => Ok(a),
            NumericArray::Null => Err(MinarrowError::NullError { message: None }),
            _ => Err(MinarrowError::TypeError {
                from: "NumericArray",
                to: "IntegerArray<i64>",
                message: None,
            }),
        }
    }

    /// Returns a reference to the inner `IntegerArray<u32>` if the variant matches.
    /// No conversion or cloning is performed.
    pub fn u32_ref(&self) -> Result<&IntegerArray<u32>, MinarrowError> {
        match self {
            NumericArray::UInt32(a) => Ok(a),
            NumericArray::Null => Err(MinarrowError::NullError { message: None }),
            _ => Err(MinarrowError::TypeError {
                from: "NumericArray",
                to: "IntegerArray<u32>",
                message: None,
            }),
        }
    }

    /// Returns a reference to the inner `IntegerArray<u64>` if the variant matches.
    /// No conversion or cloning is performed.
    pub fn u64_ref(&self) -> Result<&IntegerArray<u64>, MinarrowError> {
        match self {
            NumericArray::UInt64(a) => Ok(a),
            NumericArray::Null => Err(MinarrowError::NullError { message: None }),
            _ => Err(MinarrowError::TypeError {
                from: "NumericArray",
                to: "IntegerArray<u64>",
                message: None,
            }),
        }
    }

    /// Returns a reference to the inner `FloatArray<f32>` if the variant matches.
    /// No conversion or cloning is performed.
    pub fn f32_ref(&self) -> Result<&FloatArray<f32>, MinarrowError> {
        match self {
            NumericArray::Float32(a) => Ok(a),
            NumericArray::Null => Err(MinarrowError::NullError { message: None }),
            _ => Err(MinarrowError::TypeError {
                from: "NumericArray",
                to: "FloatArray<f32>",
                message: None,
            }),
        }
    }

    /// Returns a reference to the inner `FloatArray<f64>` if the variant matches.
    /// No conversion or cloning is performed.
    pub fn f64_ref(&self) -> Result<&FloatArray<f64>, MinarrowError> {
        match self {
            NumericArray::Float64(a) => Ok(a),
            NumericArray::Null => Err(MinarrowError::NullError { message: None }),
            _ => Err(MinarrowError::TypeError {
                from: "NumericArray",
                to: "FloatArray<f64>",
                message: None,
            }),
        }
    }

    /// Convert to IntegerArray<i32> using From/TryFrom as appropriate per conversion.
    pub fn i32(self) -> Result<IntegerArray<i32>, MinarrowError> {
        match self {
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int8(a) => Ok(IntegerArray::<i32>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int16(a) => Ok(IntegerArray::<i32>::from(&*a)),
            NumericArray::Int32(a) => match Arc::try_unwrap(a) {
                Ok(inner) => Ok(inner),
                Err(shared) => Ok((*shared).clone()),
            },
            NumericArray::Int64(a) => Ok(IntegerArray::<i32>::try_from(&*a)?),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt8(a) => Ok(IntegerArray::<i32>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt16(a) => Ok(IntegerArray::<i32>::from(&*a)),
            NumericArray::UInt32(a) => Ok(IntegerArray::<i32>::try_from(&*a)?),
            NumericArray::UInt64(a) => Ok(IntegerArray::<i32>::try_from(&*a)?),
            NumericArray::Float32(a) => Ok(IntegerArray::<i32>::try_from(&*a)?),
            NumericArray::Float64(a) => Ok(IntegerArray::<i32>::try_from(&*a)?),
            NumericArray::Null => Err(MinarrowError::NullError { message: None }),
        }
    }

    /// Convert to IntegerArray<i64> using From/TryFrom as appropriate per conversion.
    pub fn i64(self) -> Result<IntegerArray<i64>, MinarrowError> {
        match self {
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int8(a) => Ok(IntegerArray::<i64>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int16(a) => Ok(IntegerArray::<i64>::from(&*a)),
            NumericArray::Int32(a) => Ok(IntegerArray::<i64>::from(&*a)),
            NumericArray::Int64(a) => match Arc::try_unwrap(a) {
                Ok(inner) => Ok(inner),
                Err(shared) => Ok((*shared).clone()),
            },
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt8(a) => Ok(IntegerArray::<i64>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt16(a) => Ok(IntegerArray::<i64>::from(&*a)),
            NumericArray::UInt32(a) => Ok(IntegerArray::<i64>::from(&*a)),
            NumericArray::UInt64(a) => Ok(IntegerArray::<i64>::try_from(&*a)?),
            NumericArray::Float32(a) => Ok(IntegerArray::<i64>::try_from(&*a)?),
            NumericArray::Float64(a) => Ok(IntegerArray::<i64>::try_from(&*a)?),
            NumericArray::Null => Err(MinarrowError::NullError { message: None }),
        }
    }

    /// Convert to IntegerArray<u32> using From/TryFrom as appropriate per conversion.
    pub fn u32(self) -> Result<IntegerArray<u32>, MinarrowError> {
        match self {
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int8(a) => Ok(IntegerArray::<u32>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int16(a) => Ok(IntegerArray::<u32>::from(&*a)),
            NumericArray::Int32(a) => Ok(IntegerArray::<u32>::try_from(&*a)?),
            NumericArray::Int64(a) => Ok(IntegerArray::<u32>::try_from(&*a)?),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt8(a) => Ok(IntegerArray::<u32>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt16(a) => Ok(IntegerArray::<u32>::from(&*a)),
            NumericArray::UInt32(a) => match Arc::try_unwrap(a) {
                Ok(inner) => Ok(inner),
                Err(shared) => Ok((*shared).clone()),
            },
            NumericArray::UInt64(a) => Ok(IntegerArray::<u32>::try_from(&*a)?),
            NumericArray::Float32(a) => Ok(IntegerArray::<u32>::try_from(&*a)?),
            NumericArray::Float64(a) => Ok(IntegerArray::<u32>::try_from(&*a)?),
            NumericArray::Null => Err(MinarrowError::NullError { message: None }),
        }
    }

    /// Convert to IntegerArray<u64> using From/TryFrom as appropriate per conversion.
    pub fn u64(self) -> Result<IntegerArray<u64>, MinarrowError> {
        match self {
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int8(a) => Ok(IntegerArray::<u64>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int16(a) => Ok(IntegerArray::<u64>::from(&*a)),
            NumericArray::Int32(a) => Ok(IntegerArray::<u64>::from(&*a)),
            NumericArray::Int64(a) => Ok(IntegerArray::<u64>::try_from(&*a)?),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt8(a) => Ok(IntegerArray::<u64>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt16(a) => Ok(IntegerArray::<u64>::from(&*a)),
            NumericArray::UInt32(a) => Ok(IntegerArray::<u64>::from(&*a)),
            NumericArray::UInt64(a) => match Arc::try_unwrap(a) {
                Ok(inner) => Ok(inner),
                Err(shared) => Ok((*shared).clone()),
            },
            NumericArray::Float32(a) => Ok(IntegerArray::<u64>::try_from(&*a)?),
            NumericArray::Float64(a) => Ok(IntegerArray::<u64>::try_from(&*a)?),
            NumericArray::Null => Err(MinarrowError::NullError { message: None }),
        }
    }

    /// Convert to FloatArray<f32> using From.
    pub fn f32(self) -> Result<FloatArray<f32>, MinarrowError> {
        match self {
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int8(a) => Ok(FloatArray::<f32>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int16(a) => Ok(FloatArray::<f32>::from(&*a)),
            NumericArray::Int32(a) => Ok(FloatArray::<f32>::from(&*a)),
            NumericArray::Int64(a) => Ok(FloatArray::<f32>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt8(a) => Ok(FloatArray::<f32>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt16(a) => Ok(FloatArray::<f32>::from(&*a)),
            NumericArray::UInt32(a) => Ok(FloatArray::<f32>::from(&*a)),
            NumericArray::UInt64(a) => Ok(FloatArray::<f32>::from(&*a)),
            NumericArray::Float32(a) => match Arc::try_unwrap(a) {
                Ok(inner) => Ok(inner),
                Err(shared) => Ok((*shared).clone()),
            },
            NumericArray::Float64(a) => Ok(FloatArray::<f32>::from(&*a)),
            NumericArray::Null => Err(MinarrowError::NullError { message: None }),
        }
    }

    /// Cast this NumericArray to Float64, staying wrapped as NumericArray.
    ///
    /// If already Float64, returns self unchanged. Otherwise casts element
    /// data to f64, preserving the null mask. Uses `Arc::try_unwrap` so that
    /// if this is the sole owner of the backing Arc, the old data is consumed
    /// and freed rather than cloned.
    pub fn cow_into_f64(self) -> Self {
        macro_rules! cast_arc {
            ($arc:expr) => {
                match Arc::try_unwrap($arc) {
                    Ok(owned) => {
                        let data: Vec64<f64> =
                            owned.data.as_slice().iter().map(|&v| v as f64).collect();
                        NumericArray::Float64(Arc::new(FloatArray::new(data, owned.null_mask)))
                    }
                    Err(shared) => {
                        let data: Vec64<f64> =
                            shared.data.as_slice().iter().map(|&v| v as f64).collect();
                        NumericArray::Float64(Arc::new(FloatArray::new(
                            data,
                            shared.null_mask.clone(),
                        )))
                    }
                }
            };
        }

        match self {
            NumericArray::Float64(_) => self,
            NumericArray::Float32(arc) => cast_arc!(arc),
            NumericArray::Int32(arc) => cast_arc!(arc),
            NumericArray::Int64(arc) => cast_arc!(arc),
            NumericArray::UInt32(arc) => cast_arc!(arc),
            NumericArray::UInt64(arc) => cast_arc!(arc),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int8(arc) => cast_arc!(arc),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int16(arc) => cast_arc!(arc),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt8(arc) => cast_arc!(arc),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt16(arc) => cast_arc!(arc),
            NumericArray::Null => {
                NumericArray::Float64(Arc::new(FloatArray::new(Vec64::new(), None)))
            }
        }
    }

    /// Convert to FloatArray<f64> using From.
    pub fn f64(self) -> Result<FloatArray<f64>, MinarrowError> {
        match self {
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int8(a) => Ok(FloatArray::<f64>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int16(a) => Ok(FloatArray::<f64>::from(&*a)),
            NumericArray::Int32(a) => Ok(FloatArray::<f64>::from(&*a)),
            NumericArray::Int64(a) => Ok(FloatArray::<f64>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt8(a) => Ok(FloatArray::<f64>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt16(a) => Ok(FloatArray::<f64>::from(&*a)),
            NumericArray::UInt32(a) => Ok(FloatArray::<f64>::from(&*a)),
            NumericArray::UInt64(a) => Ok(FloatArray::<f64>::from(&*a)),
            NumericArray::Float32(a) => Ok(FloatArray::<f64>::from(&*a)),
            NumericArray::Float64(a) => match Arc::try_unwrap(a) {
                Ok(inner) => Ok(inner),
                Err(shared) => Ok((*shared).clone()),
            },
            NumericArray::Null => Err(MinarrowError::NullError { message: None }),
        }
    }

    /// Converts to BooleanArray<u8>.
    ///
    /// All non-zero values become `true`, but the null mask is preserved.
    pub fn bool(self) -> Result<BooleanArray<u8>, MinarrowError> {
        match self {
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int8(a) => Ok(BooleanArray::<u8>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int16(a) => Ok(BooleanArray::<u8>::from(&*a)),
            NumericArray::Int32(a) => Ok(BooleanArray::<u8>::from(&*a)),
            NumericArray::Int64(a) => Ok(BooleanArray::<u8>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt8(a) => Ok(BooleanArray::<u8>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt16(a) => Ok(BooleanArray::<u8>::from(&*a)),
            NumericArray::UInt32(a) => Ok(BooleanArray::<u8>::from(&*a)),
            NumericArray::UInt64(a) => Ok(BooleanArray::<u8>::from(&*a)),
            NumericArray::Float32(a) => Ok(BooleanArray::<u8>::from(&*a)),
            NumericArray::Float64(a) => Ok(BooleanArray::<u8>::from(&*a)),
            NumericArray::Null => Err(MinarrowError::NullError { message: None }),
        }
    }

    /// Converts to StringArray<u32> by formatting each value as string.
    ///
    /// Preserves Null mask.
    pub fn str(self) -> Result<StringArray<u32>, MinarrowError> {
        match self {
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int8(a) => Ok(StringArray::<u32>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int16(a) => Ok(StringArray::<u32>::from(&*a)),
            NumericArray::Int32(a) => Ok(StringArray::<u32>::from(&*a)),
            NumericArray::Int64(a) => Ok(StringArray::<u32>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt8(a) => Ok(StringArray::<u32>::from(&*a)),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt16(a) => Ok(StringArray::<u32>::from(&*a)),
            NumericArray::UInt32(a) => Ok(StringArray::<u32>::from(&*a)),
            NumericArray::UInt64(a) => Ok(StringArray::<u32>::from(&*a)),
            NumericArray::Float32(a) => Ok(StringArray::<u32>::from(&*a)),
            NumericArray::Float64(a) => Ok(StringArray::<u32>::from(&*a)),
            NumericArray::Null => Err(MinarrowError::NullError { message: None }),
        }
    }
}

impl Display for NumericArray {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int8(arr) => write_numeric_array_with_header(f, "Int8", arr.as_ref()),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::Int16(arr) => write_numeric_array_with_header(f, "Int16", arr.as_ref()),
            NumericArray::Int32(arr) => write_numeric_array_with_header(f, "Int32", arr.as_ref()),
            NumericArray::Int64(arr) => write_numeric_array_with_header(f, "Int64", arr.as_ref()),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt8(arr) => write_numeric_array_with_header(f, "UInt8", arr.as_ref()),
            #[cfg(feature = "extended_numeric_types")]
            NumericArray::UInt16(arr) => write_numeric_array_with_header(f, "UInt16", arr.as_ref()),
            NumericArray::UInt32(arr) => write_numeric_array_with_header(f, "UInt32", arr.as_ref()),
            NumericArray::UInt64(arr) => write_numeric_array_with_header(f, "UInt64", arr.as_ref()),
            NumericArray::Float32(arr) => {
                write_numeric_array_with_header(f, "Float32", arr.as_ref())
            }
            NumericArray::Float64(arr) => {
                write_numeric_array_with_header(f, "Float64", arr.as_ref())
            }
            NumericArray::Null => writeln!(f, "NullNumericArray [0 values]"),
        }
    }
}

/// Writes the standard header, then delegates to the contained array's Display.
fn write_numeric_array_with_header<T>(
    f: &mut Formatter<'_>,
    dtype_name: &str,
    arr: &(impl MaskedArray<CopyType = T> + Display + ?Sized),
) -> std::fmt::Result {
    writeln!(
        f,
        "NumericArray [{dtype_name}] [{} values] (null count: {})",
        arr.len(),
        arr.null_count()
    )?;
    // Delegate row formatting
    Display::fmt(arr, f)
}

impl Shape for NumericArray {
    fn shape(&self) -> ShapeDim {
        ShapeDim::Rank1(self.len())
    }
}

// TODO: Add cross-type casting
impl Concatenate for NumericArray {
    fn concat(self, other: Self) -> Result<Self, MinarrowError> {
        match (self, other) {
            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::Int8(a), NumericArray::Int8(b)) => {
                let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
                let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
                Ok(NumericArray::Int8(Arc::new(a.concat(b)?)))
            }
            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::Int16(a), NumericArray::Int16(b)) => {
                let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
                let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
                Ok(NumericArray::Int16(Arc::new(a.concat(b)?)))
            }
            (NumericArray::Int32(a), NumericArray::Int32(b)) => {
                let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
                let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
                Ok(NumericArray::Int32(Arc::new(a.concat(b)?)))
            }
            (NumericArray::Int64(a), NumericArray::Int64(b)) => {
                let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
                let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
                Ok(NumericArray::Int64(Arc::new(a.concat(b)?)))
            }
            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::UInt8(a), NumericArray::UInt8(b)) => {
                let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
                let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
                Ok(NumericArray::UInt8(Arc::new(a.concat(b)?)))
            }
            #[cfg(feature = "extended_numeric_types")]
            (NumericArray::UInt16(a), NumericArray::UInt16(b)) => {
                let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
                let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
                Ok(NumericArray::UInt16(Arc::new(a.concat(b)?)))
            }
            (NumericArray::UInt32(a), NumericArray::UInt32(b)) => {
                let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
                let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
                Ok(NumericArray::UInt32(Arc::new(a.concat(b)?)))
            }
            (NumericArray::UInt64(a), NumericArray::UInt64(b)) => {
                let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
                let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
                Ok(NumericArray::UInt64(Arc::new(a.concat(b)?)))
            }
            (NumericArray::Float32(a), NumericArray::Float32(b)) => {
                let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
                let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
                Ok(NumericArray::Float32(Arc::new(a.concat(b)?)))
            }
            (NumericArray::Float64(a), NumericArray::Float64(b)) => {
                let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
                let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
                Ok(NumericArray::Float64(Arc::new(a.concat(b)?)))
            }
            (NumericArray::Null, NumericArray::Null) => Ok(NumericArray::Null),
            (lhs, rhs) => Err(MinarrowError::IncompatibleTypeError {
                from: "NumericArray",
                to: "NumericArray",
                message: Some(format!(
                    "Cannot concatenate mismatched NumericArray variants: {:?} and {:?}",
                    variant_name(&lhs),
                    variant_name(&rhs)
                )),
            }),
        }
    }
}

/// Helper function to get the variant name for error messages
fn variant_name(arr: &NumericArray) -> &'static str {
    match arr {
        #[cfg(feature = "extended_numeric_types")]
        NumericArray::Int8(_) => "Int8",
        #[cfg(feature = "extended_numeric_types")]
        NumericArray::Int16(_) => "Int16",
        NumericArray::Int32(_) => "Int32",
        NumericArray::Int64(_) => "Int64",
        #[cfg(feature = "extended_numeric_types")]
        NumericArray::UInt8(_) => "UInt8",
        #[cfg(feature = "extended_numeric_types")]
        NumericArray::UInt16(_) => "UInt16",
        NumericArray::UInt32(_) => "UInt32",
        NumericArray::UInt64(_) => "UInt64",
        NumericArray::Float32(_) => "Float32",
        NumericArray::Float64(_) => "Float64",
        NumericArray::Null => "Null",
    }
}