vortex-scalar 0.58.0

Vortex Scalars
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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! Tests for decimal scalar casting functionality.

#![allow(clippy::disallowed_types, clippy::panic)]

use rstest::rstest;
use vortex_dtype::DType;
use vortex_dtype::DecimalDType;
use vortex_dtype::DecimalType;
use vortex_dtype::NativeDecimalType;
use vortex_dtype::Nullability;
use vortex_dtype::PType;
use vortex_dtype::i256;
use vortex_utils::aliases::hash_set::HashSet;

use crate::DecimalValue;
use crate::Scalar;
use crate::decimal::DecimalScalar;

#[rstest]
#[case(DecimalValue::I8(100), DecimalValue::I8(100))]
#[case(DecimalValue::I16(0), DecimalValue::I256(i256::ZERO))]
#[case(DecimalValue::I8(100), DecimalValue::I128(100))]
fn test_decimal_value_eq(#[case] left: DecimalValue, #[case] right: DecimalValue) {
    assert_eq!(left, right);
}

#[rstest]
#[case(DecimalValue::I128(10), DecimalValue::I8(11))]
#[case(DecimalValue::I256(i256::ZERO), DecimalValue::I16(10))]
#[case(DecimalValue::I128(-1_000), DecimalValue::I8(1))]
fn test_decimal_value_cmp(#[case] lower: DecimalValue, #[case] upper: DecimalValue) {
    assert!(lower < upper, "expected {lower} < {upper}");
}

#[test]
fn test_hash() {
    let mut set = HashSet::new();
    set.insert(DecimalValue::I8(100));
    set.insert(DecimalValue::I16(100));
    set.insert(DecimalValue::I32(100));
    set.insert(DecimalValue::I64(100));
    set.insert(DecimalValue::I128(100));
    set.insert(DecimalValue::I256(i256::from_i128(100)));
    assert_eq!(set.len(), 1);
}

#[test]
fn test_decimal_cast_to_primitive() {
    // Create a decimal with value 123.45 (scale=2, so stored as 12345)
    let decimal_scalar = Scalar::decimal(
        DecimalValue::I32(12345),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );

    // Cast to f64 should give us 123.45
    let float_result = decimal_scalar
        .cast(&DType::Primitive(PType::F64, Nullability::NonNullable))
        .unwrap();
    let float_value: f64 = float_result.try_into().unwrap();
    assert!((float_value - 123.45).abs() < 0.001);

    // Cast to i32 should give us 123 (truncated)
    let int_result = decimal_scalar
        .cast(&DType::Primitive(PType::I32, Nullability::NonNullable))
        .unwrap();
    let int_value: i32 = int_result.try_into().unwrap();
    assert_eq!(int_value, 123);
}

#[test]
fn test_decimal_cast_null_handling() {
    // Null decimal
    let null_decimal = Scalar::null(DType::Decimal(
        DecimalDType::new(10, 2),
        Nullability::Nullable,
    ));

    // Cast null decimal to primitive should preserve null
    let result = null_decimal
        .cast(&DType::Primitive(PType::I32, Nullability::Nullable))
        .unwrap();
    assert!(result.is_null());

    // Cast null decimal to another decimal type should preserve null
    let result = null_decimal
        .cast(&DType::Decimal(
            DecimalDType::new(20, 4),
            Nullability::Nullable,
        ))
        .unwrap();
    assert!(result.is_null());
}

#[test]
fn test_decimal_cast_overflow() {
    // Large decimal value that won't fit in i8
    let decimal_scalar = Scalar::decimal(
        DecimalValue::I32(100000),
        DecimalDType::new(10, 0),
        Nullability::NonNullable,
    );

    // Cast to i8 should fail due to overflow
    let result = decimal_scalar.cast(&DType::Primitive(PType::I8, Nullability::NonNullable));
    assert!(result.is_err());
}

#[test]
fn test_decimal_cast_between_decimal_types() {
    // Decimal with different precision/scale
    let decimal_scalar = Scalar::decimal(
        DecimalValue::I32(12345),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );

    // Cast to different decimal type (currently just preserves value)
    let result = decimal_scalar
        .cast(&DType::Decimal(
            DecimalDType::new(20, 4),
            Nullability::NonNullable,
        ))
        .unwrap();

    // Value should be preserved (TODO: proper scaling logic)
    let decimal_value: Option<DecimalValue> = result.try_into().unwrap();
    assert_eq!(decimal_value, Some(DecimalValue::I32(12345)));
}

#[test]
fn test_decimal_cast_negative_values() {
    // Negative decimal value
    let decimal_scalar = Scalar::decimal(
        DecimalValue::I32(-5678),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );

    // Cast to f64 should give us -56.78
    let float_result = decimal_scalar
        .cast(&DType::Primitive(PType::F64, Nullability::NonNullable))
        .unwrap();
    let float_value: f64 = float_result.try_into().unwrap();
    assert!((float_value - (-56.78)).abs() < 0.001);

    // Cast to unsigned should fail
    let result = decimal_scalar.cast(&DType::Primitive(PType::U32, Nullability::NonNullable));
    assert!(result.is_err());
}

#[rstest]
#[case(DecimalValue::I8(i8::MAX), DecimalDType::new(3, 0))]
#[case(DecimalValue::I8(i8::MIN), DecimalDType::new(3, 0))]
#[case(DecimalValue::I16(i16::MAX), DecimalDType::new(5, 0))]
#[case(DecimalValue::I16(i16::MIN), DecimalDType::new(5, 0))]
#[case(DecimalValue::I32(i32::MAX), DecimalDType::new(10, 0))]
#[case(DecimalValue::I32(i32::MIN), DecimalDType::new(10, 0))]
fn test_decimal_cast_edge_values(#[case] value: DecimalValue, #[case] dtype: DecimalDType) {
    let decimal_scalar = Scalar::decimal(value, dtype, Nullability::NonNullable);

    // Cast to f64 should always work for these ranges
    let result = decimal_scalar.cast(&DType::Primitive(PType::F64, Nullability::NonNullable));
    assert!(result.is_ok());
}

#[rstest]
#[case(1234, 0, 1234.0)] // No scale
#[case(1234, 1, 123.4)] // Scale 1
#[case(1234, 2, 12.34)] // Scale 2
#[case(1234, 3, 1.234)] // Scale 3
#[case(1234, 4, 0.1234)] // Scale 4
fn test_decimal_cast_with_scale(#[case] value: i32, #[case] scale: i8, #[case] expected: f64) {
    let decimal_scalar = Scalar::decimal(
        DecimalValue::I32(value),
        DecimalDType::new(10, scale),
        Nullability::NonNullable,
    );

    let float_result = decimal_scalar
        .cast(&DType::Primitive(PType::F64, Nullability::NonNullable))
        .unwrap();
    let float_value: f64 = float_result.try_into().unwrap();
    assert!(
        (float_value - expected).abs() < 0.0001,
        "Expected {expected} but got {float_value} for value={value} scale={scale}"
    );
}

#[test]
fn test_decimal_cast_unsupported_types() {
    let decimal_scalar = Scalar::decimal(
        DecimalValue::I32(1234),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );

    // Cast to unsupported types should fail
    let result = decimal_scalar.cast(&DType::Bool(Nullability::NonNullable));
    assert!(result.is_err());

    let result = decimal_scalar.cast(&DType::Utf8(Nullability::NonNullable));
    assert!(result.is_err());

    let result = decimal_scalar.cast(&DType::Binary(Nullability::NonNullable));
    assert!(result.is_err());
}

#[test]
fn test_decimal_to_primitive_i32() {
    // Create a decimal with value 42.50 (scale=2, so internal value is 4250)
    let decimal = Scalar::decimal(
        DecimalValue::I32(4250),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );

    // Cast to i32 - should truncate to 42
    let result = decimal.cast(&DType::Primitive(PType::I32, Nullability::NonNullable));
    assert!(result.is_ok());
    let i32_scalar = result.unwrap();
    assert_eq!(i32_scalar.as_primitive().typed_value::<i32>().unwrap(), 42);
}

#[test]
fn test_decimal_to_primitive_f64() {
    // Create a decimal with value 123.45 (scale=2, so internal value is 12345)
    let decimal = Scalar::decimal(
        DecimalValue::I32(12345),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );

    // Cast to f64 - should preserve decimal value
    let result = decimal.cast(&DType::Primitive(PType::F64, Nullability::NonNullable));
    assert!(result.is_ok());
    let f64_scalar = result.unwrap();
    assert_eq!(
        f64_scalar.as_primitive().typed_value::<f64>().unwrap(),
        123.45
    );
}

#[test]
fn test_decimal_to_primitive_f32() {
    // Create a decimal with value 99.99 (scale=2, so internal value is 9999)
    let decimal = Scalar::decimal(
        DecimalValue::I16(9999),
        DecimalDType::new(4, 2),
        Nullability::NonNullable,
    );

    // Cast to f32
    let result = decimal.cast(&DType::Primitive(PType::F32, Nullability::NonNullable));
    assert!(result.is_ok());
    let f32_scalar = result.unwrap();
    assert!((f32_scalar.as_primitive().typed_value::<f32>().unwrap() - 99.99).abs() < 0.01);
}

#[test]
fn test_decimal_to_primitive_u8_overflow() {
    // Create a decimal with value 256.00 (scale=2, so internal value is 25600)
    let decimal = Scalar::decimal(
        DecimalValue::I32(25600),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );

    // Cast to u8 - should fail due to overflow
    let result = decimal.cast(&DType::Primitive(PType::U8, Nullability::NonNullable));
    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("out of range for u8")
    );
}

#[test]
fn test_decimal_to_decimal_same_type() {
    // Create a decimal with specific precision and scale
    let decimal = Scalar::decimal(
        DecimalValue::I64(123456),
        DecimalDType::new(10, 3),
        Nullability::NonNullable,
    );

    // Cast to same decimal type but nullable
    let target_dtype = DType::Decimal(DecimalDType::new(10, 3), Nullability::Nullable);
    let result = decimal.cast(&target_dtype);
    assert!(result.is_ok());

    let casted = result.unwrap();
    assert_eq!(casted.dtype(), &target_dtype);
    assert_eq!(
        casted.as_decimal().decimal_value(),
        Some(DecimalValue::I64(123456))
    );
}

#[test]
fn test_decimal_to_decimal_different_scale() {
    // Create a decimal with scale=2
    let decimal = Scalar::decimal(
        DecimalValue::I32(10000), // Represents 100.00
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );

    // Cast to decimal with scale=4
    // TODO: This should properly rescale, but for now it preserves the raw value
    let target_dtype = DType::Decimal(DecimalDType::new(10, 4), Nullability::NonNullable);
    let result = decimal.cast(&target_dtype);
    assert!(result.is_ok());

    let casted = result.unwrap();
    assert_eq!(
        casted.as_decimal().decimal_value(),
        Some(DecimalValue::I32(10000))
    );
}

#[test]
fn test_null_decimal_cast() {
    // Create a null decimal
    let null_decimal = Scalar::null(DType::Decimal(
        DecimalDType::new(10, 2),
        Nullability::Nullable,
    ));

    // Cast to i32 - should produce null i32
    let result = null_decimal.cast(&DType::Primitive(PType::I32, Nullability::Nullable));
    assert!(result.is_ok());
    let i32_scalar = result.unwrap();
    assert!(i32_scalar.is_null());
    assert_eq!(
        i32_scalar.dtype(),
        &DType::Primitive(PType::I32, Nullability::Nullable)
    );
}

#[test]
fn test_decimal_i256_to_primitive() {
    // Create a decimal with i256 value
    use vortex_dtype::i256;
    let large_value = i256::from_i128(1234567890);
    let decimal = Scalar::decimal(
        DecimalValue::I256(large_value),
        DecimalDType::new(20, 6), // scale=6
        Nullability::NonNullable,
    );

    // Cast to f64 - value is 1234.567890
    let result = decimal.cast(&DType::Primitive(PType::F64, Nullability::NonNullable));
    assert!(result.is_ok());
    let f64_scalar = result.unwrap();
    assert!(
        (f64_scalar.as_primitive().typed_value::<f64>().unwrap() - 1234.567890).abs() < 0.000001
    );
}

#[test]
fn test_decimal_negative_value_cast() {
    // Create a negative decimal value
    let decimal = Scalar::decimal(
        DecimalValue::I32(-5000), // Represents -50.00 with scale=2
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );

    // Cast to i32
    let result = decimal.cast(&DType::Primitive(PType::I32, Nullability::NonNullable));
    assert!(result.is_ok());
    let i32_scalar = result.unwrap();
    assert_eq!(i32_scalar.as_primitive().typed_value::<i32>().unwrap(), -50);

    // Cast to u32 - should fail due to negative value
    let result = decimal.cast(&DType::Primitive(PType::U32, Nullability::NonNullable));
    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("out of range for u32")
    );
}

#[test]
fn test_decimal_cast_preserves_nullability() {
    // Non-nullable decimal
    let decimal = Scalar::decimal(
        DecimalValue::I32(100),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );

    // Cast to nullable i32
    let result = decimal.cast(&DType::Primitive(PType::I32, Nullability::Nullable));
    assert!(result.is_ok());
    let nullable_scalar = result.unwrap();
    assert_eq!(nullable_scalar.dtype().nullability(), Nullability::Nullable);
    assert!(!nullable_scalar.is_null());
}

#[test]
fn test_decimal_to_unsupported_type() {
    let decimal = Scalar::decimal(
        DecimalValue::I32(100),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );

    // Try to cast to string - should fail
    let result = decimal.cast(&DType::Utf8(Nullability::NonNullable));
    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("Cannot cast decimal to")
    );
}

#[rstest]
#[case(PType::U8, 5u64)]
#[case(PType::U16, 5)]
#[case(PType::U32, 5)]
#[case(PType::U64, 5)]
#[case(PType::I8, 5)]
#[case(PType::I16, 5)]
#[case(PType::I32, 5)]
#[case(PType::I64, 5)]
fn test_decimal_i8_all_primitive_casts(#[case] ptype: PType, #[case] expected: u64) {
    // Test casting from smallest decimal type to all primitive types
    let decimal = Scalar::decimal(
        DecimalValue::I8(50), // Represents 5.0 with scale=1
        DecimalDType::new(3, 1),
        Nullability::NonNullable,
    );

    let result = decimal.cast(&DType::Primitive(ptype, Nullability::NonNullable));
    assert!(result.is_ok(), "Failed to cast to {ptype:?}");
    let scalar = result.unwrap();

    // Check the value matches expected
    match ptype {
        PType::U8 => assert_eq!(
            scalar.as_primitive().typed_value::<u8>().unwrap() as u64,
            expected
        ),
        PType::U16 => assert_eq!(
            scalar.as_primitive().typed_value::<u16>().unwrap() as u64,
            expected
        ),
        PType::U32 => assert_eq!(
            scalar.as_primitive().typed_value::<u32>().unwrap() as u64,
            expected
        ),
        PType::U64 => assert_eq!(
            scalar.as_primitive().typed_value::<u64>().unwrap(),
            expected
        ),
        PType::I8 => assert_eq!(
            scalar.as_primitive().typed_value::<i8>().unwrap() as u64,
            expected
        ),
        PType::I16 => assert_eq!(
            scalar.as_primitive().typed_value::<i16>().unwrap() as u64,
            expected
        ),
        PType::I32 => assert_eq!(
            scalar.as_primitive().typed_value::<i32>().unwrap() as u64,
            expected
        ),
        PType::I64 => assert_eq!(
            scalar.as_primitive().typed_value::<i64>().unwrap() as u64,
            expected
        ),
        PType::F16 | PType::F32 | PType::F64 => panic!("Unexpected type {ptype}"),
    }
}

#[test]
fn test_decimal_cast_f16() {
    use vortex_dtype::half::f16;

    // Create a decimal with value 12.5 (scale=1, so stored as 125)
    let decimal = Scalar::decimal(
        DecimalValue::I16(125),
        DecimalDType::new(4, 1),
        Nullability::NonNullable,
    );

    // Cast to f16
    let result = decimal.cast(&DType::Primitive(PType::F16, Nullability::NonNullable));
    assert!(result.is_ok());
    let f16_scalar = result.unwrap();
    let f16_value: f16 = f16_scalar.as_primitive().typed_value::<f16>().unwrap();
    assert!((f16_value.to_f64() - 12.5).abs() < 0.01);
}

#[test]
fn test_decimal_cast_boundary_values() {
    // Test with U16 boundary
    let decimal = Scalar::decimal(
        DecimalValue::I32(6_553_500), // 65535.00 with scale=2
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );

    // Should succeed for U16
    let result = decimal.cast(&DType::Primitive(PType::U16, Nullability::NonNullable));
    assert!(result.is_ok());
    assert_eq!(
        result.unwrap().as_primitive().typed_value::<u16>().unwrap(),
        65535
    );

    // Should fail for U16 with value 65536
    let decimal = Scalar::decimal(
        DecimalValue::I32(6_553_600), // 65536.00 with scale=2
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let result = decimal.cast(&DType::Primitive(PType::U16, Nullability::NonNullable));
    assert!(result.is_err());

    // Test with I16 boundaries
    let decimal = Scalar::decimal(
        DecimalValue::I32(3_276_700), // 32767.00 with scale=2
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let result = decimal.cast(&DType::Primitive(PType::I16, Nullability::NonNullable));
    assert!(result.is_ok());
    assert_eq!(
        result.unwrap().as_primitive().typed_value::<i16>().unwrap(),
        32767
    );

    let decimal = Scalar::decimal(
        DecimalValue::I32(-3_276_800), // -32768.00 with scale=2
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let result = decimal.cast(&DType::Primitive(PType::I16, Nullability::NonNullable));
    assert!(result.is_ok());
    assert_eq!(
        result.unwrap().as_primitive().typed_value::<i16>().unwrap(),
        -32768
    );

    // Should fail for I16 with value 32768
    let decimal = Scalar::decimal(
        DecimalValue::I32(3_276_800), // 32768.00 with scale=2
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let result = decimal.cast(&DType::Primitive(PType::I16, Nullability::NonNullable));
    assert!(result.is_err());
}

#[test]
fn test_decimal_partial_ord() {
    let decimal1 = Scalar::decimal(
        DecimalValue::I32(100),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar1 = DecimalScalar::try_from(&decimal1).unwrap();

    let decimal2 = Scalar::decimal(
        DecimalValue::I32(200),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar2 = DecimalScalar::try_from(&decimal2).unwrap();

    // Same type comparison should work
    assert!(scalar1 < scalar2);
    assert!(scalar2 > scalar1);
    assert_eq!(
        scalar1.partial_cmp(&scalar1),
        Some(std::cmp::Ordering::Equal)
    );

    // Different type comparison should return None
    let decimal3 = Scalar::decimal(
        DecimalValue::I32(100),
        DecimalDType::new(20, 4), // Different precision/scale
        Nullability::NonNullable,
    );
    let scalar3 = DecimalScalar::try_from(&decimal3).unwrap();
    assert_eq!(scalar1.partial_cmp(&scalar3), None);
}

#[test]
fn test_decimal_eq() {
    let decimal1 = Scalar::decimal(
        DecimalValue::I32(100),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar1 = DecimalScalar::try_from(&decimal1).unwrap();

    let decimal2 = Scalar::decimal(
        DecimalValue::I32(100),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar2 = DecimalScalar::try_from(&decimal2).unwrap();

    assert_eq!(scalar1, scalar2);

    // Different values
    let decimal3 = Scalar::decimal(
        DecimalValue::I32(200),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar3 = DecimalScalar::try_from(&decimal3).unwrap();
    assert_ne!(scalar1, scalar3);
}

#[test]
fn test_decimal_value_from_unsigned() {
    // Test From implementations for unsigned types
    let v1: DecimalValue = 255u8.into();
    assert_eq!(v1, DecimalValue::I16(255));

    let v2: DecimalValue = 65535u16.into();
    assert_eq!(v2, DecimalValue::I32(65535));

    let v3: DecimalValue = 4294967295u32.into();
    assert_eq!(v3, DecimalValue::I64(4294967295));

    let v4: DecimalValue = 18446744073709551615u64.into();
    assert_eq!(v4, DecimalValue::I128(18446744073709551615));
}

#[test]
fn test_decimal_scalar_try_from_errors() {
    // Test error cases for TryFrom<DecimalScalar> for primitive types
    let decimal = Scalar::decimal(
        DecimalValue::I16(1234),
        DecimalDType::new(5, 2),
        Nullability::NonNullable,
    );
    let scalar = DecimalScalar::try_from(&decimal).unwrap();

    // Try to extract as wrong type
    let result: Result<i8, _> = scalar.try_into();
    assert!(result.is_err());

    // Try to extract from null
    let null_decimal = Scalar::null(DType::Decimal(
        DecimalDType::new(10, 2),
        Nullability::Nullable,
    ));
    let null_scalar = DecimalScalar::try_from(&null_decimal).unwrap();
    let result: Result<i32, _> = null_scalar.try_into();
    assert!(result.is_err());

    // Extract as Option from null should succeed
    let result: Result<Option<i32>, _> = null_scalar.try_into();
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), None);
}

#[test]
fn test_decimal_cast_large_scale() {
    // Test with very large scale factors
    let decimal = Scalar::decimal(
        DecimalValue::I64(123456789012345), // 1234.56789012345 with scale=11
        DecimalDType::new(20, 11),
        Nullability::NonNullable,
    );

    // Cast to f64
    let result = decimal.cast(&DType::Primitive(PType::F64, Nullability::NonNullable));
    assert!(result.is_ok());
    let f64_value: f64 = result.unwrap().try_into().unwrap();
    assert!((f64_value - 1234.56789012345).abs() < 0.0000000001);
}

#[test]
fn test_decimal_cast_zero_scale() {
    // Test with zero scale (integer values)
    let decimal = Scalar::decimal(
        DecimalValue::I32(123456),
        DecimalDType::new(10, 0),
        Nullability::NonNullable,
    );

    // Cast to i32 should give exact value
    let result = decimal.cast(&DType::Primitive(PType::I32, Nullability::NonNullable));
    assert!(result.is_ok());
    let i32_value: i32 = result.unwrap().try_into().unwrap();
    assert_eq!(i32_value, 123456);
}

#[test]
fn test_native_decimal_type_values_type() {
    // Test DECIMAL_TYPE constant for each type
    assert_eq!(i8::DECIMAL_TYPE, DecimalType::I8);
    assert_eq!(i16::DECIMAL_TYPE, DecimalType::I16);
    assert_eq!(i32::DECIMAL_TYPE, DecimalType::I32);
    assert_eq!(i64::DECIMAL_TYPE, DecimalType::I64);
    assert_eq!(i128::DECIMAL_TYPE, DecimalType::I128);
    assert_eq!(i256::DECIMAL_TYPE, DecimalType::I256);
}

#[test]
fn test_decimal_cast_u64_boundary() {
    // Test U64 boundary case
    let decimal = Scalar::decimal(
        DecimalValue::I128(18446744073709551615_i128), // U64::MAX
        DecimalDType::new(20, 0),
        Nullability::NonNullable,
    );

    let result = decimal.cast(&DType::Primitive(PType::U64, Nullability::NonNullable));
    assert!(result.is_ok());
    assert_eq!(
        result.unwrap().as_primitive().typed_value::<u64>().unwrap(),
        u64::MAX
    );

    // Test overflow - This value exceeds U64::MAX when cast
    // Note: The cast logic checks the float value against U64::MAX
    let decimal = Scalar::decimal(
        DecimalValue::I128(i128::MAX), // Much larger than U64::MAX
        DecimalDType::new(38, 0),
        Nullability::NonNullable,
    );

    let result = decimal.cast(&DType::Primitive(PType::U64, Nullability::NonNullable));
    assert!(result.is_err());
}

#[test]
fn test_decimal_i256_overflow_cast() {
    // Test that decimal values too large for i128 are properly handled
    let large_value = i256::from_i128(i128::MAX) + i256::from_i128(1);
    let decimal = Scalar::decimal(
        DecimalValue::I256(large_value),
        DecimalDType::new(40, 0),
        Nullability::NonNullable,
    );

    // This should fail when trying to convert to primitive types
    let result = decimal.cast(&DType::Primitive(PType::I64, Nullability::NonNullable));
    assert!(result.is_err());
}

// Tests for checked_binary_numeric
#[test]
fn test_decimal_scalar_checked_add() {
    use crate::NumericOperator;

    let decimal1 = Scalar::decimal(
        DecimalValue::I64(100),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar1 = DecimalScalar::try_from(&decimal1).unwrap();

    let decimal2 = Scalar::decimal(
        DecimalValue::I64(200),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar2 = DecimalScalar::try_from(&decimal2).unwrap();

    let result = scalar1
        .checked_binary_numeric(&scalar2, NumericOperator::Add)
        .unwrap();
    assert_eq!(
        result.decimal_value(),
        Some(DecimalValue::I256(i256::from_i128(300)))
    );
}

#[test]
fn test_decimal_scalar_checked_sub() {
    use crate::NumericOperator;

    let decimal1 = Scalar::decimal(
        DecimalValue::I64(500),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar1 = DecimalScalar::try_from(&decimal1).unwrap();

    let decimal2 = Scalar::decimal(
        DecimalValue::I64(200),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar2 = DecimalScalar::try_from(&decimal2).unwrap();

    let result = scalar1
        .checked_binary_numeric(&scalar2, NumericOperator::Sub)
        .unwrap();
    assert_eq!(
        result.decimal_value(),
        Some(DecimalValue::I256(i256::from_i128(300)))
    );
}

#[test]
fn test_decimal_scalar_checked_mul() {
    use crate::NumericOperator;

    let decimal1 = Scalar::decimal(
        DecimalValue::I32(50),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar1 = DecimalScalar::try_from(&decimal1).unwrap();

    let decimal2 = Scalar::decimal(
        DecimalValue::I32(10),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar2 = DecimalScalar::try_from(&decimal2).unwrap();

    let result = scalar1
        .checked_binary_numeric(&scalar2, NumericOperator::Mul)
        .unwrap();
    assert_eq!(
        result.decimal_value(),
        Some(DecimalValue::I256(i256::from_i128(500)))
    );
}

#[test]
fn test_decimal_scalar_checked_div() {
    use crate::NumericOperator;

    let decimal1 = Scalar::decimal(
        DecimalValue::I64(1000),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar1 = DecimalScalar::try_from(&decimal1).unwrap();

    let decimal2 = Scalar::decimal(
        DecimalValue::I64(10),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar2 = DecimalScalar::try_from(&decimal2).unwrap();

    let result = scalar1
        .checked_binary_numeric(&scalar2, NumericOperator::Div)
        .unwrap();
    assert_eq!(
        result.decimal_value(),
        Some(DecimalValue::I256(i256::from_i128(100)))
    );
}

#[test]
fn test_decimal_scalar_checked_div_by_zero() {
    use crate::NumericOperator;

    let decimal1 = Scalar::decimal(
        DecimalValue::I64(1000),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar1 = DecimalScalar::try_from(&decimal1).unwrap();

    let decimal2 = Scalar::decimal(
        DecimalValue::I64(0),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar2 = DecimalScalar::try_from(&decimal2).unwrap();

    let result = scalar1.checked_binary_numeric(&scalar2, NumericOperator::Div);
    assert_eq!(result, None);
}

#[test]
fn test_decimal_scalar_null_handling() {
    use crate::NumericOperator;

    let decimal1 = Scalar::null(DType::Decimal(
        DecimalDType::new(10, 2),
        Nullability::Nullable,
    ));
    let scalar1 = DecimalScalar::try_from(&decimal1).unwrap();

    let decimal2 = Scalar::decimal(
        DecimalValue::I64(200),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar2 = DecimalScalar::try_from(&decimal2).unwrap();

    let result = scalar1
        .checked_binary_numeric(&scalar2, NumericOperator::Add)
        .unwrap();
    assert_eq!(result.decimal_value(), None);
}

#[test]
fn test_decimal_scalar_precision_overflow() {
    use crate::NumericOperator;

    // Create decimals with precision 3 (max value 999)
    let decimal1 = Scalar::decimal(
        DecimalValue::I16(999),
        DecimalDType::new(3, 0),
        Nullability::NonNullable,
    );
    let scalar1 = DecimalScalar::try_from(&decimal1).unwrap();

    let decimal2 = Scalar::decimal(
        DecimalValue::I16(2),
        DecimalDType::new(3, 0),
        Nullability::NonNullable,
    );
    let scalar2 = DecimalScalar::try_from(&decimal2).unwrap();

    // 999 + 2 = 1001 which exceeds precision 3
    let result = scalar1.checked_binary_numeric(&scalar2, NumericOperator::Add);
    assert_eq!(result, None);
}

#[test]
fn test_decimal_scalar_rsub_and_rdiv() {
    use crate::NumericOperator;

    let decimal1 = Scalar::decimal(
        DecimalValue::I64(100),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar1 = DecimalScalar::try_from(&decimal1).unwrap();

    let decimal2 = Scalar::decimal(
        DecimalValue::I64(300),
        DecimalDType::new(10, 2),
        Nullability::NonNullable,
    );
    let scalar2 = DecimalScalar::try_from(&decimal2).unwrap();

    // RSub: 300 - 100 = 200
    let result = scalar1
        .checked_binary_numeric(&scalar2, NumericOperator::RSub)
        .unwrap();
    assert_eq!(
        result.decimal_value(),
        Some(DecimalValue::I256(i256::from_i128(200)))
    );

    // RDiv: 300 / 100 = 3
    let result = scalar1
        .checked_binary_numeric(&scalar2, NumericOperator::RDiv)
        .unwrap();
    assert_eq!(
        result.decimal_value(),
        Some(DecimalValue::I256(i256::from_i128(3)))
    );
}