vortex-array 0.68.0

Vortex in memory columnar data format
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

use std::sync::Arc;

use rstest::rstest;
use vortex_error::VortexExpect;

use crate::builders::ArrayBuilder;
use crate::builders::builder_with_capacity;
use crate::dtype::DType;
use crate::dtype::DecimalDType;
use crate::dtype::Nullability;
use crate::dtype::PType;
use crate::dtype::StructFields;
use crate::dtype::half::f16;
use crate::extension::datetime::TimeUnit;
use crate::extension::datetime::Timestamp;
use crate::scalar::Scalar;

/// Test that `append_zeros` produces the same result as manually appending `Scalar::default_value`.
///
/// This test verifies that the implementation of `append_zeros` correctly matches the behavior
/// defined by `Scalar::default_value` for each data type.
#[rstest]
#[case::bool(DType::Bool(Nullability::NonNullable))]
#[case::i8(DType::Primitive(PType::I8, Nullability::NonNullable))]
#[case::i16(DType::Primitive(PType::I16, Nullability::NonNullable))]
#[case::i32(DType::Primitive(PType::I32, Nullability::NonNullable))]
#[case::i64(DType::Primitive(PType::I64, Nullability::NonNullable))]
#[case::u8(DType::Primitive(PType::U8, Nullability::NonNullable))]
#[case::u16(DType::Primitive(PType::U16, Nullability::NonNullable))]
#[case::u32(DType::Primitive(PType::U32, Nullability::NonNullable))]
#[case::u64(DType::Primitive(PType::U64, Nullability::NonNullable))]
#[case::f32(DType::Primitive(PType::F32, Nullability::NonNullable))]
#[case::f64(DType::Primitive(PType::F64, Nullability::NonNullable))]
#[case::utf8(DType::Utf8(Nullability::NonNullable))]
#[case::binary(DType::Binary(Nullability::NonNullable))]
#[case::decimal128(DType::Decimal(DecimalDType::new(10, 2), Nullability::NonNullable))]
#[case::struct_simple(DType::Struct(
    StructFields::from_iter([
        ("a", DType::Primitive(PType::I32, Nullability::NonNullable)),
        ("b", DType::Utf8(Nullability::NonNullable)),
    ]),
    Nullability::NonNullable
))]
#[case::struct_nested(DType::Struct(
    StructFields::from_iter([
        ("field1", DType::Bool(Nullability::NonNullable)),
        ("field2", DType::Struct(
            StructFields::from_iter([
                ("nested", DType::Primitive(PType::F64, Nullability::NonNullable)),
            ]),
            Nullability::NonNullable
        )),
    ]),
    Nullability::NonNullable
))]
#[case::list(DType::List(
    Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)),
    Nullability::NonNullable
))]
#[case::fixed_size_list(DType::FixedSizeList(
    Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)),
    3,
    Nullability::NonNullable
))]
#[case::extension(DType::Extension(
    Timestamp::new(TimeUnit::Milliseconds, Nullability::NonNullable).erased()
))]
fn test_append_zeros_matches_default_value(#[case] dtype: DType) {
    let num_elements = 5;

    // Builder 1: Use append_zeros.
    let mut builder_zeros = builder_with_capacity(&dtype, num_elements);
    builder_zeros.append_zeros(num_elements);
    let array_zeros = builder_zeros.finish();

    // Builder 2: Manually append default values.
    let mut builder_manual = builder_with_capacity(&dtype, num_elements);
    let default_scalar = Scalar::zero_value(&dtype);
    for _ in 0..num_elements {
        builder_manual.append_scalar(&default_scalar).unwrap();
    }
    let array_manual = builder_manual.finish();

    // Both arrays should have the same length.
    assert_eq!(array_zeros.len(), array_manual.len());
    assert_eq!(array_zeros.len(), num_elements);

    // Compare each element.
    for i in 0..num_elements {
        let scalar_zeros = array_zeros.scalar_at(i).unwrap();
        let scalar_manual = array_manual.scalar_at(i).unwrap();

        assert_eq!(
            scalar_zeros, scalar_manual,
            "Element at index {} should be equal",
            i
        );
    }
}

/// Test that calling `append_nulls` on non-nullable builders panics.
/// Tests both single null (n=1) and multiple nulls (n=3).
#[rstest]
#[case::bool(DType::Bool(Nullability::NonNullable), 1)]
#[case::bool_multiple(DType::Bool(Nullability::NonNullable), 3)]
#[case::i32(DType::Primitive(PType::I32, Nullability::NonNullable), 1)]
#[case::i32_multiple(DType::Primitive(PType::I32, Nullability::NonNullable), 3)]
#[case::f64(DType::Primitive(PType::F64, Nullability::NonNullable), 1)]
#[case::f64_multiple(DType::Primitive(PType::F64, Nullability::NonNullable), 3)]
#[case::utf8(DType::Utf8(Nullability::NonNullable), 1)]
#[case::utf8_multiple(DType::Utf8(Nullability::NonNullable), 3)]
#[case::binary(DType::Binary(Nullability::NonNullable), 1)]
#[case::binary_multiple(DType::Binary(Nullability::NonNullable), 3)]
#[case::decimal(DType::Decimal(DecimalDType::new(10, 2), Nullability::NonNullable), 1)]
#[case::decimal_multiple(DType::Decimal(DecimalDType::new(10, 2), Nullability::NonNullable), 3)]
#[case::list(
    DType::List(
        Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)),
        Nullability::NonNullable
    ),
    1
)]
#[case::list_multiple(
    DType::List(
        Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)),
        Nullability::NonNullable
    ),
    3
)]
#[case::fixed_size_list(
    DType::FixedSizeList(
        Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)),
        3,
        Nullability::NonNullable
    ),
    1
)]
#[case::fixed_size_list_multiple(
    DType::FixedSizeList(
        Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)),
        3,
        Nullability::NonNullable
    ),
    3
)]
#[case::struct_type(DType::Struct(
    StructFields::from_iter([
        ("a", DType::Primitive(PType::I32, Nullability::NonNullable)),
    ]),
    Nullability::NonNullable
), 1)]
#[case::struct_type_multiple(DType::Struct(
    StructFields::from_iter([
        ("a", DType::Primitive(PType::I32, Nullability::NonNullable)),
    ]),
    Nullability::NonNullable
), 3)]
#[case::extension(
    DType::Extension(Timestamp::new(TimeUnit::Milliseconds, Nullability::NonNullable).erased()),
    1
)]
#[case::extension_multiple(
    DType::Extension(Timestamp::new(TimeUnit::Milliseconds, Nullability::NonNullable).erased()),
    3
)]
#[should_panic(expected = "non-nullable")]
fn test_append_nulls_panics_on_non_nullable(#[case] dtype: DType, #[case] count: usize) {
    let mut builder = builder_with_capacity(&dtype, count);
    builder.append_nulls(count);
}

/// Test that `append_defaults` behaves correctly for nullable and non-nullable types.
#[rstest]
#[case::nullable_bool(DType::Bool(Nullability::Nullable), true)]
#[case::non_nullable_bool(DType::Bool(Nullability::NonNullable), false)]
#[case::nullable_i32(DType::Primitive(PType::I32, Nullability::Nullable), true)]
#[case::non_nullable_i32(DType::Primitive(PType::I32, Nullability::NonNullable), false)]
#[case::nullable_utf8(DType::Utf8(Nullability::Nullable), true)]
#[case::non_nullable_utf8(DType::Utf8(Nullability::NonNullable), false)]
fn test_append_defaults_behavior(#[case] dtype: DType, #[case] should_be_null: bool) {
    let mut builder = builder_with_capacity(&dtype, 3);
    builder.append_defaults(3);
    let array = builder.finish();

    assert_eq!(array.len(), 3);

    for i in 0..3 {
        let scalar = array.scalar_at(i).unwrap();
        if should_be_null {
            assert!(scalar.is_null(), "Element at index {} should be null", i);
        } else {
            assert!(
                !scalar.is_null(),
                "Element at index {} should not be null",
                i
            );
            // For non-nullable, it should match the default value.
            let expected = Scalar::default_value(&dtype);
            // Skip list comparison due to known bug.
            if !matches!(dtype, DType::List(..)) {
                assert_eq!(
                    scalar, expected,
                    "Element at index {} should be the default value",
                    i
                );
            }
        }
    }
}

/// Helper function that fills two builders with the same values and compares the results
/// of `to_canonical()` vs `finish().to_canonical()`.
fn compare_to_canonical_methods<F>(dtype: &DType, mut fill_builder: F)
where
    F: FnMut(&mut dyn ArrayBuilder),
{
    use crate::IntoArray;

    // Create two identical builders.
    let mut builder1 = builder_with_capacity(dtype, 10);
    let mut builder2 = builder_with_capacity(dtype, 10);

    // Fill both builders with the same data.
    fill_builder(builder1.as_mut());
    fill_builder(builder2.as_mut());

    // Get canonical arrays using both methods.
    let canonical_direct = builder1.finish_into_canonical();
    let canonical_indirect = builder2
        .finish()
        .to_canonical()
        .vortex_expect("to_canonical failed");

    // Convert both to arrays for comparison.
    let array_direct = canonical_direct.into_array();
    let array_indirect = canonical_indirect.into_array();

    // Verify they have the same length.
    assert_eq!(array_direct.len(), array_indirect.len());

    // Compare each element.
    for i in 0..array_direct.len() {
        let scalar_direct = array_direct.scalar_at(i).unwrap();
        let scalar_indirect = array_indirect.scalar_at(i).unwrap();

        assert_eq!(
            scalar_direct, scalar_indirect,
            "Element at index {} should be equal for dtype {:?}",
            i, dtype
        );
    }
}

#[test]
fn test_to_canonical_bool() {
    let dtype = DType::Bool(Nullability::NonNullable);
    compare_to_canonical_methods(&dtype, |builder| {
        for i in 0..5 {
            let value = Scalar::bool(i % 2 == 0, Nullability::NonNullable);
            builder.append_scalar(&value).unwrap();
        }
    });
}

#[test]
fn test_to_canonical_bool_nullable() {
    let dtype = DType::Bool(Nullability::Nullable);
    compare_to_canonical_methods(&dtype, |builder| {
        for i in 0..5 {
            let value = Scalar::bool(i % 2 == 0, Nullability::Nullable);
            builder.append_scalar(&value).unwrap();
        }
        builder.append_nulls(1);
    });
}

#[test]
fn test_to_canonical_i32() {
    let dtype = DType::Primitive(PType::I32, Nullability::NonNullable);
    compare_to_canonical_methods(&dtype, |builder| {
        for i in 0..5 {
            let value = Scalar::primitive(i, Nullability::NonNullable);
            builder.append_scalar(&value).unwrap();
        }
    });
}

#[test]
fn test_to_canonical_i32_nullable() {
    let dtype = DType::Primitive(PType::I32, Nullability::Nullable);
    compare_to_canonical_methods(&dtype, |builder| {
        for i in 0..5 {
            let value = Scalar::primitive(i, Nullability::Nullable);
            builder.append_scalar(&value).unwrap();
        }
        builder.append_nulls(1);
    });
}

#[test]
fn test_to_canonical_f64() {
    let dtype = DType::Primitive(PType::F64, Nullability::NonNullable);
    compare_to_canonical_methods(&dtype, |builder| {
        for i in 0..5 {
            let value = Scalar::primitive(i as f64, Nullability::NonNullable);
            builder.append_scalar(&value).unwrap();
        }
    });
}

#[test]
fn test_to_canonical_utf8() {
    let dtype = DType::Utf8(Nullability::NonNullable);
    compare_to_canonical_methods(&dtype, |builder| {
        let values = ["hello", "world", "test", "data", "vortex"];
        for value in &values {
            let scalar = Scalar::utf8(*value, Nullability::NonNullable);
            builder.append_scalar(&scalar).unwrap();
        }
    });
}

#[test]
fn test_to_canonical_utf8_nullable() {
    let dtype = DType::Utf8(Nullability::Nullable);
    compare_to_canonical_methods(&dtype, |builder| {
        let values = ["hello", "world", "test"];
        for value in &values {
            let scalar = Scalar::utf8(*value, Nullability::Nullable);
            builder.append_scalar(&scalar).unwrap();
        }
        builder.append_nulls(1);
    });
}

#[test]
fn test_to_canonical_binary() {
    let dtype = DType::Binary(Nullability::NonNullable);
    compare_to_canonical_methods(&dtype, |builder| {
        let values = [b"hello", b"world", b"vortx", b"bytes", b"tests"];
        for value in &values {
            let scalar = Scalar::binary(value.to_vec(), Nullability::NonNullable);
            builder.append_scalar(&scalar).unwrap();
        }
    });
}

#[test]
fn test_to_canonical_struct() {
    let dtype = DType::Struct(
        StructFields::from_iter([
            ("a", DType::Primitive(PType::I32, Nullability::NonNullable)),
            ("b", DType::Utf8(Nullability::NonNullable)),
        ]),
        Nullability::NonNullable,
    );
    compare_to_canonical_methods(&dtype, |builder| {
        for _ in 0..3 {
            let value = Scalar::default_value(&dtype);
            builder.append_scalar(&value).unwrap();
        }
    });
}

#[test]
fn test_to_canonical_extension() {
    let dtype =
        DType::Extension(Timestamp::new(TimeUnit::Milliseconds, Nullability::NonNullable).erased());
    compare_to_canonical_methods(&dtype, |builder| {
        let ext_dtype = match &dtype {
            DType::Extension(ext) => ext.clone(),
            _ => unreachable!(),
        };
        for i in 0..5 {
            let storage_value = Scalar::from(i as i64);
            let ext_scalar = Scalar::extension_ref(ext_dtype.clone(), storage_value);
            builder.append_scalar(&ext_scalar).unwrap();
        }
    });
}

#[test]
fn test_to_canonical_null() {
    let dtype = DType::Null;
    compare_to_canonical_methods(&dtype, |builder| {
        builder.append_nulls(5);
    });
}

#[test]
fn test_to_canonical_decimal() {
    let dtype = DType::Decimal(DecimalDType::new(10, 2), Nullability::NonNullable);
    compare_to_canonical_methods(&dtype, |builder| {
        for _ in 0..5 {
            let value = Scalar::default_value(&dtype);
            builder.append_scalar(&value).unwrap();
        }
    });
}

#[test]
fn test_to_canonical_i8() {
    let dtype = DType::Primitive(PType::I8, Nullability::NonNullable);
    compare_to_canonical_methods(&dtype, |builder| {
        for i in 0..5i8 {
            let value = Scalar::primitive(i, Nullability::NonNullable);
            builder.append_scalar(&value).unwrap();
        }
    });
}

#[test]
fn test_to_canonical_u64() {
    let dtype = DType::Primitive(PType::U64, Nullability::NonNullable);
    compare_to_canonical_methods(&dtype, |builder| {
        for i in 0..5 {
            let value = Scalar::primitive(i as u64, Nullability::NonNullable);
            builder.append_scalar(&value).unwrap();
        }
    });
}

#[test]
fn test_to_canonical_f32() {
    let dtype = DType::Primitive(PType::F32, Nullability::NonNullable);
    compare_to_canonical_methods(&dtype, |builder| {
        for i in 0..5 {
            let value = Scalar::primitive(i as f32, Nullability::NonNullable);
            builder.append_scalar(&value).unwrap();
        }
    });
}

/// Comprehensive test for `append_scalar` across all supported data types.
/// This test verifies that `append_scalar` works correctly for each type by:
/// 1. Creating a builder with the given dtype
/// 2. Appending various scalars (including nulls for nullable types)
/// 3. Verifying the resulting array matches expectations
#[rstest]
#[case::bool_non_nullable(DType::Bool(Nullability::NonNullable))]
#[case::bool_nullable(DType::Bool(Nullability::Nullable))]
#[case::i8(DType::Primitive(PType::I8, Nullability::NonNullable))]
#[case::i16(DType::Primitive(PType::I16, Nullability::NonNullable))]
#[case::i32(DType::Primitive(PType::I32, Nullability::NonNullable))]
#[case::i64(DType::Primitive(PType::I64, Nullability::NonNullable))]
#[case::u8(DType::Primitive(PType::U8, Nullability::NonNullable))]
#[case::u16(DType::Primitive(PType::U16, Nullability::NonNullable))]
#[case::u32(DType::Primitive(PType::U32, Nullability::NonNullable))]
#[case::u64(DType::Primitive(PType::U64, Nullability::NonNullable))]
#[case::f32(DType::Primitive(PType::F32, Nullability::NonNullable))]
#[case::f64(DType::Primitive(PType::F64, Nullability::NonNullable))]
#[case::i32_nullable(DType::Primitive(PType::I32, Nullability::Nullable))]
#[case::f64_nullable(DType::Primitive(PType::F64, Nullability::Nullable))]
#[case::utf8_non_nullable(DType::Utf8(Nullability::NonNullable))]
#[case::utf8_nullable(DType::Utf8(Nullability::Nullable))]
#[case::binary_non_nullable(DType::Binary(Nullability::NonNullable))]
#[case::binary_nullable(DType::Binary(Nullability::Nullable))]
#[case::null(DType::Null)]
#[case::decimal128_non_nullable(DType::Decimal(
    DecimalDType::new(10, 2),
    Nullability::NonNullable
))]
#[case::decimal128_nullable(DType::Decimal(DecimalDType::new(10, 2), Nullability::Nullable))]
#[case::struct_simple(DType::Struct(
    StructFields::from_iter([
        ("a", DType::Primitive(PType::I32, Nullability::NonNullable)),
        ("b", DType::Utf8(Nullability::NonNullable)),
    ]),
    Nullability::NonNullable
))]
#[case::struct_nullable(DType::Struct(
    StructFields::from_iter([
        ("x", DType::Primitive(PType::F64, Nullability::NonNullable)),
    ]),
    Nullability::Nullable
))]
#[case::list_non_nullable(DType::List(
    Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)),
    Nullability::NonNullable
))]
#[case::list_nullable(DType::List(
    Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)),
    Nullability::Nullable
))]
#[case::fixed_size_list_non_nullable(DType::FixedSizeList(
    Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)),
    3,
    Nullability::NonNullable
))]
#[case::fixed_size_list_nullable(DType::FixedSizeList(
    Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)),
    3,
    Nullability::Nullable
))]
#[case::extension_non_nullable(DType::Extension(
    Timestamp::new(TimeUnit::Milliseconds, Nullability::NonNullable).erased()
))]
fn test_append_scalar_comprehensive(#[case] dtype: DType) {
    let num_elements = 3;
    let mut builder = builder_with_capacity(&dtype, num_elements * 2);

    // Create test scalars based on the dtype.
    let scalars = create_test_scalars_for_dtype(&dtype, num_elements);

    // Append each scalar.
    for scalar in &scalars {
        builder.append_scalar(scalar).unwrap();
    }

    // If nullable, append a null (special handling for fixed-size lists).
    if dtype.is_nullable() {
        // Fixed-size lists require special handling for nulls.
        if matches!(dtype, DType::FixedSizeList(..)) {
            builder.append_nulls(1);
        } else {
            let null_scalar = Scalar::null(dtype.clone());
            builder.append_scalar(&null_scalar).unwrap();
        }
    }

    let array = builder.finish();

    // Verify the array length.
    let expected_len = if dtype.is_nullable() {
        num_elements + 1
    } else {
        num_elements
    };
    assert_eq!(array.len(), expected_len);

    // Verify each scalar matches.
    for (i, expected_scalar) in scalars.iter().enumerate() {
        let actual_scalar = array.scalar_at(i).unwrap();
        assert_scalars_equal(&actual_scalar, expected_scalar, &dtype, i);
    }

    // If nullable, verify the last element is null.
    if dtype.is_nullable() {
        let null_scalar = array.scalar_at(num_elements).unwrap();
        assert!(
            null_scalar.is_null(),
            "Last element should be null for nullable dtype"
        );
    }
}

/// Helper function to create test scalars for a given dtype.
#[allow(clippy::cast_possible_truncation)]
fn create_test_scalars_for_dtype(dtype: &DType, count: usize) -> Vec<Scalar> {
    let mut scalars = Vec::with_capacity(count);

    for i in 0..count {
        let scalar = match dtype {
            DType::Null => Scalar::null(dtype.clone()),
            DType::Bool(n) => Scalar::bool(i % 2 == 0, *n),
            DType::Primitive(ptype, n) => match ptype {
                PType::I8 => Scalar::primitive(i as i8, *n),
                PType::I16 => Scalar::primitive(i as i16, *n),
                PType::I32 => Scalar::primitive(i as i32, *n),
                PType::I64 => Scalar::primitive(i as i64, *n),
                PType::U8 => Scalar::primitive(i as u8, *n),
                PType::U16 => Scalar::primitive(i as u16, *n),
                PType::U32 => Scalar::primitive(i as u32, *n),
                PType::U64 => Scalar::primitive(i as u64, *n),
                PType::F16 => Scalar::primitive(f16::from_f32(i as f32 * 1.5), *n),
                PType::F32 => Scalar::primitive(i as f32 * 1.5, *n),
                PType::F64 => Scalar::primitive(i as f64 * 1.5, *n),
            },
            DType::Utf8(n) => Scalar::utf8(format!("test_string_{}", i), *n),
            DType::Binary(n) => Scalar::binary(format!("bytes_{}", i).into_bytes(), *n),
            DType::Decimal(dec_dtype, n) => {
                // Create decimal scalars based on the decimal dtype.
                use crate::scalar::DecimalValue;
                let value = DecimalValue::I128((i as i128 + 1) * 100); // Simple decimal values.
                Scalar::decimal(value, *dec_dtype, *n)
            }
            DType::Struct(fields, n) => {
                // Create struct scalars with field values.
                let field_values: Vec<Scalar> = fields
                    .fields()
                    .enumerate()
                    .map(|(j, field_dtype)| {
                        // Create simple values for each field.
                        match &field_dtype {
                            DType::Primitive(PType::I32, n) => {
                                Scalar::primitive((i as i32).saturating_add(j as i32), *n)
                            }
                            DType::Primitive(PType::F64, n) => {
                                Scalar::primitive((i + j) as f64, *n)
                            }
                            DType::Utf8(n) => Scalar::utf8(format!("field_{}", i + j), *n),
                            _ => Scalar::default_value(&field_dtype),
                        }
                    })
                    .collect();
                Scalar::struct_(DType::Struct(fields.clone(), *n), field_values)
            }
            DType::List(element_dtype, n) => {
                // Create list scalars with a few elements.
                let elements: Vec<Scalar> = (0..=i)
                    .map(|j| match element_dtype.as_ref() {
                        DType::Primitive(PType::I32, n) => {
                            Scalar::primitive(j.min(i32::MAX as usize) as i32, *n)
                        }
                        _ => Scalar::default_value(element_dtype.as_ref()),
                    })
                    .collect();
                Scalar::list(Arc::clone(element_dtype), elements, *n)
            }
            DType::FixedSizeList(element_dtype, size, n) => {
                // Create fixed-size list scalars.
                let elements: Vec<Scalar> = (0..*size)
                    .map(|j| match element_dtype.as_ref() {
                        DType::Primitive(PType::I32, n) => {
                            Scalar::primitive((i as i32).saturating_add(j as i32), *n)
                        }
                        _ => Scalar::default_value(element_dtype.as_ref()),
                    })
                    .collect();
                Scalar::fixed_size_list(Arc::clone(element_dtype), elements, *n)
            }
            DType::Extension(ext_dtype) => {
                // Create extension scalars with storage values.
                let storage_scalar = match ext_dtype.storage_dtype() {
                    DType::Primitive(PType::I64, n) => Scalar::primitive(i as i64, *n),
                    _ => Scalar::default_value(ext_dtype.storage_dtype()),
                };
                Scalar::extension_ref(ext_dtype.clone(), storage_scalar)
            }
            DType::Variant(_) => continue,
        };
        scalars.push(scalar);
    }

    scalars
}

/// Helper function to compare scalars, handling special cases like lists.
fn assert_scalars_equal(actual: &Scalar, expected: &Scalar, dtype: &DType, index: usize) {
    // For lists, we need special handling due to known issues.
    if matches!(dtype, DType::List(..)) {
        // Just check nullability matches.
        assert_eq!(
            actual.is_null(),
            expected.is_null(),
            "Null status mismatch at index {}",
            index
        );
        // Skip detailed comparison for lists due to known bugs.
        return;
    }

    assert_eq!(
        actual, expected,
        "Scalar mismatch at index {} for dtype {:?}",
        index, dtype
    );
}

/// Test that `append_scalar` correctly handles mixed valid and null values
/// for nullable types.
#[rstest]
#[case::bool(DType::Bool(Nullability::Nullable))]
#[case::i32(DType::Primitive(PType::I32, Nullability::Nullable))]
#[case::f64(DType::Primitive(PType::F64, Nullability::Nullable))]
#[case::utf8(DType::Utf8(Nullability::Nullable))]
#[case::binary(DType::Binary(Nullability::Nullable))]
fn test_append_scalar_mixed_nulls(#[case] dtype: DType) {
    let mut builder = builder_with_capacity(&dtype, 6);

    // Create a pattern of valid, null, valid, null, valid.
    let test_scalars = create_test_scalars_for_dtype(&dtype, 3);
    let null_scalar = Scalar::null(dtype.clone());

    builder.append_scalar(&test_scalars[0]).unwrap();
    builder.append_scalar(&null_scalar).unwrap();
    builder.append_scalar(&test_scalars[1]).unwrap();
    builder.append_scalar(&null_scalar).unwrap();
    builder.append_scalar(&test_scalars[2]).unwrap();

    let array = builder.finish();
    assert_eq!(array.len(), 5);

    // Check the pattern.
    assert!(!array.scalar_at(0).unwrap().is_null());
    assert!(array.scalar_at(1).unwrap().is_null());
    assert!(!array.scalar_at(2).unwrap().is_null());
    assert!(array.scalar_at(3).unwrap().is_null());
    assert!(!array.scalar_at(4).unwrap().is_null());

    // Verify non-null values match.
    assert_scalars_equal(&array.scalar_at(0).unwrap(), &test_scalars[0], &dtype, 0);
    assert_scalars_equal(&array.scalar_at(2).unwrap(), &test_scalars[1], &dtype, 2);
    assert_scalars_equal(&array.scalar_at(4).unwrap(), &test_scalars[2], &dtype, 4);
}

/// Test that `append_scalar` correctly rejects scalars with wrong dtype.
#[test]
fn test_append_scalar_wrong_dtype_rejection() {
    // Test bool builder rejecting i32 scalar.
    let mut bool_builder = builder_with_capacity(&DType::Bool(Nullability::NonNullable), 1);
    let i32_scalar = Scalar::from(42i32);
    assert!(
        bool_builder.append_scalar(&i32_scalar).is_err(),
        "Bool builder should reject i32 scalar"
    );

    // Test i32 builder rejecting string scalar.
    let mut i32_builder =
        builder_with_capacity(&DType::Primitive(PType::I32, Nullability::NonNullable), 1);
    let string_scalar = Scalar::utf8("test", Nullability::NonNullable);
    assert!(
        i32_builder.append_scalar(&string_scalar).is_err(),
        "I32 builder should reject string scalar"
    );

    // Test string builder rejecting binary scalar.
    let mut string_builder = builder_with_capacity(&DType::Utf8(Nullability::NonNullable), 1);
    let binary_scalar = Scalar::binary(vec![0u8, 1, 2], Nullability::NonNullable);
    assert!(
        string_builder.append_scalar(&binary_scalar).is_err(),
        "String builder should reject binary scalar"
    );
}

/// Test that `append_scalar` works correctly when called repeatedly
/// with the same scalar instance.
#[test]
fn test_append_scalar_repeated_same_instance() {
    let dtype = DType::Primitive(PType::I32, Nullability::NonNullable);
    let mut builder = builder_with_capacity(&dtype, 5);

    let scalar = Scalar::primitive(42i32, Nullability::NonNullable);

    // Append the same scalar instance multiple times.
    for _ in 0..5 {
        builder.append_scalar(&scalar).unwrap();
    }

    let array = builder.finish();
    assert_eq!(array.len(), 5);

    // All values should be 42.
    for i in 0..5 {
        let actual = array.scalar_at(i).unwrap();
        assert_eq!(
            actual.as_primitive().typed_value::<i32>(),
            Some(42),
            "Value at index {} should be 42",
            i
        );
    }
}