1use arrow_schema::ArrowError;
6use std::fmt;
7
8pub trait VariantDecimalType: Into<super::Variant<'static, 'static>> {
30 type Native;
32
33 const MAX_PRECISION: u8;
35 const MAX_UNSCALED_VALUE: Self::Native;
37
38 fn is_valid_precision_and_scale(precision: &u8, scale: &i8) -> bool {
57 (1..=Self::MAX_PRECISION).contains(precision) && (0..=*precision as i8).contains(scale)
58 }
59
60 fn try_new(integer: Self::Native, scale: u8) -> Result<Self, ArrowError>;
80
81 fn try_new_with_signed_scale(integer: Self::Native, scale: i8) -> Result<Self, ArrowError>;
89
90 fn integer(&self) -> Self::Native;
92
93 fn scale(&self) -> u8;
95
96 fn as_integer(&self) -> Option<Self::Native>;
101}
102
103macro_rules! impl_variant_decimal {
105 ($struct_name:ident, $native:ty) => {
106 impl $struct_name {
107 pub fn try_new(integer: $native, scale: u8) -> Result<Self, ArrowError> {
110 let max_precision = Self::MAX_PRECISION;
111 if scale > max_precision {
112 return Err(ArrowError::InvalidArgumentError(format!(
113 "Scale {scale} is larger than max precision {max_precision}",
114 )));
115 }
116 if !(-Self::MAX_UNSCALED_VALUE..=Self::MAX_UNSCALED_VALUE).contains(&integer) {
117 return Err(ArrowError::InvalidArgumentError(format!(
118 "{integer} is wider than max precision {max_precision}",
119 )));
120 }
121
122 Ok(Self { integer, scale })
123 }
124
125 pub fn integer(&self) -> $native {
129 self.integer
130 }
131
132 pub fn scale(&self) -> u8 {
136 self.scale
137 }
138
139 #[doc = concat!(
140 "Returns Some(`",
141 stringify!($native),
142 "`) if scale is zero or integer of the decimal is divisible by 10^scale,\n",
143 "None for other values.\n\n",
144 "",
145 "# Examples\n",
146 "```rust\n",
147 "use parquet_variant::", stringify!($struct_name), ";\n",
148 "//Return the integer if scale is 0\n",
149 "let d1 = ", stringify!($struct_name), "::try_new(123, 0).unwrap();\n",
150 "assert_eq!(d1.as_integer(), Some(123));\n",
151 "// or if the integer is divisible by 10^scale\n",
152 "let d2 = ", stringify!($struct_name), "::try_new(100, 2).unwrap();\n",
153 "assert_eq!(d2.as_integer(), Some(1));\n",
154 "// or the integer is negative and divisible by 10^scale\n",
155 "let d3 = ", stringify!($struct_name), "::try_new(-100, 2).unwrap();\n",
156 "assert_eq!(d3.as_integer(), Some(-1));\n",
157 "// or if the integer is 0\n",
158 "let d4 = ", stringify!($struct_name), "::try_new(0, 4).unwrap();\n",
159 "assert_eq!(d4.as_integer(), Some(0));\n",
160 "// but not if the integer is not divisible by 10^scale\n",
161 "let d5 = ", stringify!($struct_name), "::try_new(123, 2).unwrap();\n",
162 "assert_eq!(d5.as_integer(), None);\n",
163 "// or the integer is negative and not divisible by 10^scale\n",
164 "let d6 = ", stringify!($struct_name), "::try_new(-123, 2).unwrap();\n",
165 "assert_eq!(d6.as_integer(), None);\n",
166 "```\n",
167 )]
168 pub fn as_integer(&self) -> Option<$native> {
169 if self.scale == 0 {
170 return Some(self.integer);
171 }
172 let divisor = <$native>::pow(10, self.scale as u32);
173 (self.integer % divisor == 0).then(|| self.integer / divisor)
174 }
175 }
176
177 impl VariantDecimalType for $struct_name {
178 type Native = $native;
179 const MAX_PRECISION: u8 = Self::MAX_PRECISION;
180 const MAX_UNSCALED_VALUE: $native = <$native>::pow(10, Self::MAX_PRECISION as u32) - 1;
181
182 fn try_new(integer: $native, scale: u8) -> Result<Self, ArrowError> {
183 Self::try_new(integer, scale)
184 }
185
186 fn try_new_with_signed_scale(integer: $native, scale: i8) -> Result<Self, ArrowError> {
187 let (integer, scale) = if scale < 0 {
188 let multiplier =
189 <$native>::checked_pow(10, u32::from(scale.unsigned_abs()));
190 let Some(rescaled) = multiplier.and_then(|m| integer.checked_mul(m)) else {
191 return Err(ArrowError::InvalidArgumentError(format!(
192 "Overflow when rescaling {integer} with scale {scale}"
193 )));
194 };
195 (rescaled, 0u8)
196 } else {
197 (integer, scale as u8)
198 };
199 Self::try_new(integer, scale)
200 }
201
202 fn integer(&self) -> $native {
203 self.integer()
204 }
205
206 fn scale(&self) -> u8 {
207 self.scale()
208 }
209
210 fn as_integer(&self) -> Option<$native> {
211 self.as_integer()
212 }
213 }
214
215 impl fmt::Display for $struct_name {
216 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
217 let integer = if self.scale == 0 {
218 self.integer
219 } else {
220 let divisor = <$native>::pow(10, self.scale as u32);
221 let remainder = self.integer % divisor;
222 if remainder != 0 {
223 let sign = if self.integer < 0 { "-" } else { "" };
225 let remainder =
227 format!("{:0width$}", remainder.abs(), width = self.scale as usize);
228 let remainder = remainder.trim_end_matches('0');
229 let quotient = (self.integer / divisor).abs();
230 return write!(f, "{sign}{quotient}.{remainder}");
231 }
232 self.integer / divisor
233 };
234 write!(f, "{integer}")
235 }
236 }
237 };
238}
239
240#[derive(Debug, Clone, Copy, PartialEq)]
255pub struct VariantDecimal4 {
256 integer: i32,
257 scale: u8,
258}
259
260impl VariantDecimal4 {
261 pub const MAX_PRECISION: u8 = arrow_schema::DECIMAL32_MAX_PRECISION;
263}
264
265impl_variant_decimal!(VariantDecimal4, i32);
266
267#[derive(Debug, Clone, Copy, PartialEq)]
283pub struct VariantDecimal8 {
284 integer: i64,
285 scale: u8,
286}
287
288impl VariantDecimal8 {
289 pub const MAX_PRECISION: u8 = arrow_schema::DECIMAL64_MAX_PRECISION;
291}
292
293impl_variant_decimal!(VariantDecimal8, i64);
294
295#[derive(Debug, Clone, Copy, PartialEq)]
311pub struct VariantDecimal16 {
312 integer: i128,
313 scale: u8,
314}
315
316impl VariantDecimal16 {
317 pub const MAX_PRECISION: u8 = arrow_schema::DECIMAL128_MAX_PRECISION;
319}
320
321impl_variant_decimal!(VariantDecimal16, i128);
322
323macro_rules! impl_from_decimal_for_decimal {
325 ($from_ty:ty, $for_ty:ty) => {
326 impl From<$from_ty> for $for_ty {
327 fn from(decimal: $from_ty) -> Self {
328 Self {
329 integer: decimal.integer.into(),
330 scale: decimal.scale,
331 }
332 }
333 }
334 };
335}
336
337impl_from_decimal_for_decimal!(VariantDecimal4, VariantDecimal8);
338impl_from_decimal_for_decimal!(VariantDecimal4, VariantDecimal16);
339impl_from_decimal_for_decimal!(VariantDecimal8, VariantDecimal16);
340
341macro_rules! impl_try_from_decimal_for_decimal {
343 ($from_ty:ty, $for_ty:ty) => {
344 impl TryFrom<$from_ty> for $for_ty {
345 type Error = ArrowError;
346
347 fn try_from(decimal: $from_ty) -> Result<Self, ArrowError> {
348 let Ok(integer) = decimal.integer.try_into() else {
349 return Err(ArrowError::InvalidArgumentError(format!(
350 "Value {} is wider than max precision {}",
351 decimal.integer,
352 Self::MAX_PRECISION
353 )));
354 };
355 Self::try_new(integer, decimal.scale)
356 }
357 }
358 };
359}
360
361impl_try_from_decimal_for_decimal!(VariantDecimal8, VariantDecimal4);
362impl_try_from_decimal_for_decimal!(VariantDecimal16, VariantDecimal4);
363impl_try_from_decimal_for_decimal!(VariantDecimal16, VariantDecimal8);
364
365macro_rules! impl_try_from_int_for_decimal {
367 ($from_ty:ty, $for_ty:ty) => {
368 impl TryFrom<$from_ty> for $for_ty {
369 type Error = ArrowError;
370
371 fn try_from(integer: $from_ty) -> Result<Self, ArrowError> {
372 Self::try_new(integer, 0)
373 }
374 }
375 };
376}
377
378impl_try_from_int_for_decimal!(i32, VariantDecimal4);
379impl_try_from_int_for_decimal!(i64, VariantDecimal8);
380impl_try_from_int_for_decimal!(i128, VariantDecimal16);
381
382#[cfg(test)]
383mod tests {
384 use super::*;
385
386 #[test]
387 fn test_variant_decimal_signed_scale() {
388 fn check<D: VariantDecimalType>()
389 where
390 D::Native: From<i32> + std::fmt::Debug + PartialEq,
391 {
392 for scale in [i8::MIN, -127] {
393 assert!(matches!(
394 D::try_new_with_signed_scale(1.into(), scale),
395 Err(ArrowError::InvalidArgumentError(_))
396 ));
397 }
398
399 for value in [-12, 12] {
400 let decimal = D::try_new_with_signed_scale(value.into(), -2).unwrap();
401 assert_eq!(decimal.integer(), (value * 100).into());
402 assert_eq!(decimal.scale(), 0);
403 }
404 }
405
406 check::<VariantDecimal4>();
407 check::<VariantDecimal8>();
408 check::<VariantDecimal16>();
409 }
410
411 #[test]
412 fn test_variant_decimal_invalid_precision() {
413 let decimal4_too_large = VariantDecimal4::try_new(1_000_000_000_i32, 2);
415 assert!(
416 decimal4_too_large.is_err(),
417 "Decimal4 precision overflow should fail"
418 );
419 assert!(
420 decimal4_too_large
421 .unwrap_err()
422 .to_string()
423 .contains("wider than max precision")
424 );
425
426 let decimal4_too_small = VariantDecimal4::try_new(-1_000_000_000_i32, 2);
427 assert!(
428 decimal4_too_small.is_err(),
429 "Decimal4 precision underflow should fail"
430 );
431 assert!(
432 decimal4_too_small
433 .unwrap_err()
434 .to_string()
435 .contains("wider than max precision")
436 );
437
438 let decimal4_max_valid = VariantDecimal4::try_new(999_999_999_i32, 2);
440 assert!(
441 decimal4_max_valid.is_ok(),
442 "Decimal4 max valid value should succeed"
443 );
444
445 let decimal4_min_valid = VariantDecimal4::try_new(-999_999_999_i32, 2);
446 assert!(
447 decimal4_min_valid.is_ok(),
448 "Decimal4 min valid value should succeed"
449 );
450
451 let decimal8_too_large = VariantDecimal8::try_new(1_000_000_000_000_000_000_i64, 2);
453 assert!(
454 decimal8_too_large.is_err(),
455 "Decimal8 precision overflow should fail"
456 );
457 assert!(
458 decimal8_too_large
459 .unwrap_err()
460 .to_string()
461 .contains("wider than max precision")
462 );
463
464 let decimal8_too_small = VariantDecimal8::try_new(-1_000_000_000_000_000_000_i64, 2);
465 assert!(
466 decimal8_too_small.is_err(),
467 "Decimal8 precision underflow should fail"
468 );
469 assert!(
470 decimal8_too_small
471 .unwrap_err()
472 .to_string()
473 .contains("wider than max precision")
474 );
475
476 let decimal8_max_valid = VariantDecimal8::try_new(999_999_999_999_999_999_i64, 2);
478 assert!(
479 decimal8_max_valid.is_ok(),
480 "Decimal8 max valid value should succeed"
481 );
482
483 let decimal8_min_valid = VariantDecimal8::try_new(-999_999_999_999_999_999_i64, 2);
484 assert!(
485 decimal8_min_valid.is_ok(),
486 "Decimal8 min valid value should succeed"
487 );
488
489 let decimal16_too_large =
491 VariantDecimal16::try_new(100000000000000000000000000000000000000_i128, 2);
492 assert!(
493 decimal16_too_large.is_err(),
494 "Decimal16 precision overflow should fail"
495 );
496 assert!(
497 decimal16_too_large
498 .unwrap_err()
499 .to_string()
500 .contains("wider than max precision")
501 );
502
503 let decimal16_too_small =
504 VariantDecimal16::try_new(-100000000000000000000000000000000000000_i128, 2);
505 assert!(
506 decimal16_too_small.is_err(),
507 "Decimal16 precision underflow should fail"
508 );
509 assert!(
510 decimal16_too_small
511 .unwrap_err()
512 .to_string()
513 .contains("wider than max precision")
514 );
515
516 let decimal16_max_valid =
518 VariantDecimal16::try_new(99999999999999999999999999999999999999_i128, 2);
519 assert!(
520 decimal16_max_valid.is_ok(),
521 "Decimal16 max valid value should succeed"
522 );
523
524 let decimal16_min_valid =
525 VariantDecimal16::try_new(-99999999999999999999999999999999999999_i128, 2);
526 assert!(
527 decimal16_min_valid.is_ok(),
528 "Decimal16 min valid value should succeed"
529 );
530 }
531
532 #[test]
533 fn test_variant_decimal_invalid_scale() {
534 let decimal4_invalid_scale = VariantDecimal4::try_new(123_i32, 10);
536 assert!(
537 decimal4_invalid_scale.is_err(),
538 "Decimal4 with scale > 9 should fail"
539 );
540 assert!(
541 decimal4_invalid_scale
542 .unwrap_err()
543 .to_string()
544 .contains("larger than max precision")
545 );
546
547 let decimal4_invalid_scale_large = VariantDecimal4::try_new(123_i32, 20);
548 assert!(
549 decimal4_invalid_scale_large.is_err(),
550 "Decimal4 with scale > 9 should fail"
551 );
552
553 let decimal4_valid_scale = VariantDecimal4::try_new(123_i32, 9);
555 assert!(
556 decimal4_valid_scale.is_ok(),
557 "Decimal4 with scale = 9 should succeed"
558 );
559
560 let decimal8_invalid_scale = VariantDecimal8::try_new(123_i64, 19);
562 assert!(
563 decimal8_invalid_scale.is_err(),
564 "Decimal8 with scale > 18 should fail"
565 );
566 assert!(
567 decimal8_invalid_scale
568 .unwrap_err()
569 .to_string()
570 .contains("larger than max precision")
571 );
572
573 let decimal8_invalid_scale_large = VariantDecimal8::try_new(123_i64, 25);
574 assert!(
575 decimal8_invalid_scale_large.is_err(),
576 "Decimal8 with scale > 18 should fail"
577 );
578
579 let decimal8_valid_scale = VariantDecimal8::try_new(123_i64, 18);
581 assert!(
582 decimal8_valid_scale.is_ok(),
583 "Decimal8 with scale = 18 should succeed"
584 );
585
586 let decimal16_invalid_scale = VariantDecimal16::try_new(123_i128, 39);
588 assert!(
589 decimal16_invalid_scale.is_err(),
590 "Decimal16 with scale > 38 should fail"
591 );
592 assert!(
593 decimal16_invalid_scale
594 .unwrap_err()
595 .to_string()
596 .contains("larger than max precision")
597 );
598
599 let decimal16_invalid_scale_large = VariantDecimal16::try_new(123_i128, 50);
600 assert!(
601 decimal16_invalid_scale_large.is_err(),
602 "Decimal16 with scale > 38 should fail"
603 );
604
605 let decimal16_valid_scale = VariantDecimal16::try_new(123_i128, 38);
607 assert!(
608 decimal16_valid_scale.is_ok(),
609 "Decimal16 with scale = 38 should succeed"
610 );
611 }
612
613 #[test]
614 fn test_variant_decimal4_display() {
615 let d = VariantDecimal4::try_new(42, 0).unwrap();
617 assert_eq!(d.to_string(), "42");
618
619 let d = VariantDecimal4::try_new(-42, 0).unwrap();
620 assert_eq!(d.to_string(), "-42");
621
622 let d = VariantDecimal4::try_new(12345, 2).unwrap();
624 assert_eq!(d.to_string(), "123.45");
625
626 let d = VariantDecimal4::try_new(-12345, 2).unwrap();
627 assert_eq!(d.to_string(), "-123.45");
628
629 let d = VariantDecimal4::try_new(12300, 2).unwrap();
631 assert_eq!(d.to_string(), "123");
632
633 let d = VariantDecimal4::try_new(-12300, 2).unwrap();
634 assert_eq!(d.to_string(), "-123");
635
636 let d = VariantDecimal4::try_new(1005, 3).unwrap();
638 assert_eq!(d.to_string(), "1.005");
639
640 let d = VariantDecimal4::try_new(-1005, 3).unwrap();
641 assert_eq!(d.to_string(), "-1.005");
642
643 let d = VariantDecimal4::try_new(123, 4).unwrap();
645 assert_eq!(d.to_string(), "0.0123");
646
647 let d = VariantDecimal4::try_new(-123, 4).unwrap();
648 assert_eq!(d.to_string(), "-0.0123");
649
650 let d = VariantDecimal4::try_new(0, 0).unwrap();
652 assert_eq!(d.to_string(), "0");
653
654 let d = VariantDecimal4::try_new(0, 3).unwrap();
655 assert_eq!(d.to_string(), "0");
656
657 let d = VariantDecimal4::try_new(123456789, 9).unwrap();
659 assert_eq!(d.to_string(), "0.123456789");
660
661 let d = VariantDecimal4::try_new(-123456789, 9).unwrap();
662 assert_eq!(d.to_string(), "-0.123456789");
663
664 let d = VariantDecimal4::try_new(999999999, 0).unwrap();
666 assert_eq!(d.to_string(), "999999999");
667
668 let d = VariantDecimal4::try_new(-999999999, 0).unwrap();
669 assert_eq!(d.to_string(), "-999999999");
670
671 let d = VariantDecimal4::try_new(120050, 4).unwrap();
673 assert_eq!(d.to_string(), "12.005");
674
675 let d = VariantDecimal4::try_new(-120050, 4).unwrap();
676 assert_eq!(d.to_string(), "-12.005");
677 }
678
679 #[test]
680 fn test_variant_decimal8_display() {
681 let d = VariantDecimal8::try_new(42, 0).unwrap();
683 assert_eq!(d.to_string(), "42");
684
685 let d = VariantDecimal8::try_new(-42, 0).unwrap();
686 assert_eq!(d.to_string(), "-42");
687
688 let d = VariantDecimal8::try_new(1234567890, 3).unwrap();
690 assert_eq!(d.to_string(), "1234567.89");
691
692 let d = VariantDecimal8::try_new(-1234567890, 3).unwrap();
693 assert_eq!(d.to_string(), "-1234567.89");
694
695 let d = VariantDecimal8::try_new(123000000, 6).unwrap();
697 assert_eq!(d.to_string(), "123");
698
699 let d = VariantDecimal8::try_new(-123000000, 6).unwrap();
700 assert_eq!(d.to_string(), "-123");
701
702 let d = VariantDecimal8::try_new(100005, 6).unwrap();
704 assert_eq!(d.to_string(), "0.100005");
705
706 let d = VariantDecimal8::try_new(-100005, 6).unwrap();
707 assert_eq!(d.to_string(), "-0.100005");
708
709 let d = VariantDecimal8::try_new(123, 10).unwrap();
711 assert_eq!(d.to_string(), "0.0000000123");
712
713 let d = VariantDecimal8::try_new(-123, 10).unwrap();
714 assert_eq!(d.to_string(), "-0.0000000123");
715
716 let d = VariantDecimal8::try_new(0, 0).unwrap();
718 assert_eq!(d.to_string(), "0");
719
720 let d = VariantDecimal8::try_new(0, 10).unwrap();
721 assert_eq!(d.to_string(), "0");
722
723 let d = VariantDecimal8::try_new(123456789012345678, 18).unwrap();
725 assert_eq!(d.to_string(), "0.123456789012345678");
726
727 let d = VariantDecimal8::try_new(-123456789012345678, 18).unwrap();
728 assert_eq!(d.to_string(), "-0.123456789012345678");
729
730 let d = VariantDecimal8::try_new(999999999999999999, 0).unwrap();
732 assert_eq!(d.to_string(), "999999999999999999");
733
734 let d = VariantDecimal8::try_new(-999999999999999999, 0).unwrap();
735 assert_eq!(d.to_string(), "-999999999999999999");
736
737 let d = VariantDecimal8::try_new(1200000050000, 10).unwrap();
739 assert_eq!(d.to_string(), "120.000005");
740
741 let d = VariantDecimal8::try_new(-1200000050000, 10).unwrap();
742 assert_eq!(d.to_string(), "-120.000005");
743 }
744
745 #[test]
746 fn test_variant_decimal16_display() {
747 let d = VariantDecimal16::try_new(42, 0).unwrap();
749 assert_eq!(d.to_string(), "42");
750
751 let d = VariantDecimal16::try_new(-42, 0).unwrap();
752 assert_eq!(d.to_string(), "-42");
753
754 let d = VariantDecimal16::try_new(123456789012345, 4).unwrap();
756 assert_eq!(d.to_string(), "12345678901.2345");
757
758 let d = VariantDecimal16::try_new(-123456789012345, 4).unwrap();
759 assert_eq!(d.to_string(), "-12345678901.2345");
760
761 let d = VariantDecimal16::try_new(12300000000, 8).unwrap();
763 assert_eq!(d.to_string(), "123");
764
765 let d = VariantDecimal16::try_new(-12300000000, 8).unwrap();
766 assert_eq!(d.to_string(), "-123");
767
768 let d = VariantDecimal16::try_new(10000005, 8).unwrap();
770 assert_eq!(d.to_string(), "0.10000005");
771
772 let d = VariantDecimal16::try_new(-10000005, 8).unwrap();
773 assert_eq!(d.to_string(), "-0.10000005");
774
775 let d = VariantDecimal16::try_new(123, 20).unwrap();
777 assert_eq!(d.to_string(), "0.00000000000000000123");
778
779 let d = VariantDecimal16::try_new(-123, 20).unwrap();
780 assert_eq!(d.to_string(), "-0.00000000000000000123");
781
782 let d = VariantDecimal16::try_new(0, 0).unwrap();
784 assert_eq!(d.to_string(), "0");
785
786 let d = VariantDecimal16::try_new(0, 20).unwrap();
787 assert_eq!(d.to_string(), "0");
788
789 let d = VariantDecimal16::try_new(12345678901234567890123456789012345678_i128, 38).unwrap();
791 assert_eq!(d.to_string(), "0.12345678901234567890123456789012345678");
792
793 let d =
794 VariantDecimal16::try_new(-12345678901234567890123456789012345678_i128, 38).unwrap();
795 assert_eq!(d.to_string(), "-0.12345678901234567890123456789012345678");
796
797 let d = VariantDecimal16::try_new(99999999999999999999999999999999999999_i128, 0).unwrap();
799 assert_eq!(d.to_string(), "99999999999999999999999999999999999999");
800
801 let d = VariantDecimal16::try_new(-99999999999999999999999999999999999999_i128, 0).unwrap();
802 assert_eq!(d.to_string(), "-99999999999999999999999999999999999999");
803
804 let d = VariantDecimal16::try_new(12000000000000050000000000000_i128, 25).unwrap();
806 assert_eq!(d.to_string(), "1200.000000000005");
807
808 let d = VariantDecimal16::try_new(-12000000000000050000000000000_i128, 25).unwrap();
809 assert_eq!(d.to_string(), "-1200.000000000005");
810
811 let large_int = 12345678901234567890123456789_i128;
813 let d = VariantDecimal16::try_new(large_int, 0).unwrap();
814 assert_eq!(d.to_string(), "12345678901234567890123456789");
815
816 let d = VariantDecimal16::try_new(-large_int, 0).unwrap();
817 assert_eq!(d.to_string(), "-12345678901234567890123456789");
818 }
819}