Skip to main content

parquet_variant/variant/
decimal.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use arrow_schema::ArrowError;
6use std::fmt;
7
8/// Trait for variant decimal types, enabling generic code across Decimal4/8/16
9///
10/// This trait provides a common interface for the three variant decimal types,
11/// allowing generic functions and data structures to work with any decimal width.
12/// It is modeled after Arrow's `DecimalType` trait but adapted for variant semantics.
13///
14/// # Example
15///
16/// ```
17/// # use parquet_variant::{VariantDecimal4, VariantDecimal8, VariantDecimalType};
18/// #
19/// fn extract_scale<D: VariantDecimalType>(decimal: D) -> u8 {
20///     decimal.scale()
21/// }
22///
23/// let dec4 = VariantDecimal4::try_new(12345, 2).unwrap();
24/// let dec8 = VariantDecimal8::try_new(67890, 3).unwrap();
25///
26/// assert_eq!(extract_scale(dec4), 2);
27/// assert_eq!(extract_scale(dec8), 3);
28/// ```
29pub trait VariantDecimalType: Into<super::Variant<'static, 'static>> {
30    /// The underlying signed integer type (i32, i64, or i128)
31    type Native;
32
33    /// Maximum number of significant digits this decimal type can represent (9, 18, or 38)
34    const MAX_PRECISION: u8;
35    /// The largest positive unscaled value that fits in [`Self::MAX_PRECISION`] digits.
36    const MAX_UNSCALED_VALUE: Self::Native;
37
38    /// True if the given precision and scale are valid for this variant decimal type.
39    ///
40    /// NOTE: By a strict reading of the "decimal table" in the [variant spec], one might conclude that
41    /// each decimal type has both lower and upper bounds on precision (i.e. Decimal16 with precision 5
42    /// is invalid because Decimal4 "covers" it). But the variant shredding integration tests
43    /// specifically expect such cases to succeed, so we only enforce the upper bound here.
44    ///
45    /// [shredding spec]: https://github.com/apache/parquet-format/blob/master/VariantEncoding.md#encoding-types
46    ///
47    /// # Example
48    /// ```
49    /// # use parquet_variant::{VariantDecimal4, VariantDecimalType};
50    /// #
51    /// assert!(VariantDecimal4::is_valid_precision_and_scale(&5, &2));
52    /// assert!(!VariantDecimal4::is_valid_precision_and_scale(&10, &2)); // too wide
53    /// assert!(!VariantDecimal4::is_valid_precision_and_scale(&5, &-1)); // negative scale
54    /// assert!(!VariantDecimal4::is_valid_precision_and_scale(&5, &7)); // scale too big
55    /// ```
56    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    /// Creates a new decimal value from the given unscaled integer and scale, failing if the
61    /// integer's width, or the requested scale, exceeds `MAX_PRECISION`.
62    ///
63    /// NOTE: For compatibility with arrow decimal types, negative scale is allowed as long
64    /// as the rescaled value fits in the available precision.
65    ///
66    /// # Example
67    ///
68    /// ```
69    /// # use parquet_variant::{VariantDecimal4, VariantDecimalType};
70    /// #
71    /// // Valid: 123.45 (5 digits, scale 2)
72    /// let d = VariantDecimal4::try_new(12345, 2).unwrap();
73    /// assert_eq!(d.integer(), 12345);
74    /// assert_eq!(d.scale(), 2);
75    ///
76    /// VariantDecimal4::try_new(123, 10).expect_err("scale exceeds MAX_PRECISION");
77    /// VariantDecimal4::try_new(1234567890, 10).expect_err("value's width exceeds MAX_PRECISION");
78    /// ```
79    fn try_new(integer: Self::Native, scale: u8) -> Result<Self, ArrowError>;
80
81    /// Attempts to convert an unscaled arrow decimal value to the indicated variant decimal type.
82    ///
83    /// Unlike [`Self::try_new`], this function accepts a signed scale, and attempts to rescale
84    /// negative-scale values to their equivalent (larger) scale-0 values. For example, a decimal
85    /// value of 123 with scale -2 becomes 12300 with scale 0.
86    ///
87    /// Fails if rescaling fails, or for any of the reasons [`Self::try_new`] could fail.
88    fn try_new_with_signed_scale(integer: Self::Native, scale: i8) -> Result<Self, ArrowError>;
89
90    /// Returns the unscaled integer value
91    fn integer(&self) -> Self::Native;
92
93    /// Returns the scale (number of digits after the decimal point)
94    fn scale(&self) -> u8;
95
96    /// Converts the decimal as an integer if possible,
97    ///
98    /// Return `Some(integer value)` if scale is 0 or the unscaled integer is divisible by 10^scale.
99    /// None for other values.
100    fn as_integer(&self) -> Option<Self::Native>;
101}
102
103/// Implements the complete variant decimal type: methods, Display, and VariantDecimalType trait
104macro_rules! impl_variant_decimal {
105    ($struct_name:ident, $native:ty) => {
106        impl $struct_name {
107            /// Attempts to create a new instance of this decimal type, failing if the value is too
108            /// wide or the scale is too large.
109            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            /// Returns the unscaled integer value of the decimal.
126            ///
127            /// For example, if the decimal is `123.45`, this will return `12345`.
128            pub fn integer(&self) -> $native {
129                self.integer
130            }
131
132            /// Returns the scale of the decimal (how many digits after the decimal point).
133            ///
134            /// For example, if the decimal is `123.45`, this will return `2`.
135            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                        // Track the sign explicitly, in case the quotient is zero
224                        let sign = if self.integer < 0 { "-" } else { "" };
225                        // Format an unsigned remainder with leading zeros and strip trailing zeros
226                        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/// Represents a 4-byte decimal value in the Variant format.
241///
242/// This struct stores a decimal number using a 32-bit signed integer for the coefficient
243/// and an 8-bit unsigned integer for the scale (number of decimal places). Its precision is limited to 9 digits.
244///
245/// For valid precision and scale values, see the Variant specification:
246/// <https://github.com/apache/parquet-format/blob/87f2c8bf77eefb4c43d0ebaeea1778bd28ac3609/VariantEncoding.md?plain=1#L418-L420>
247///
248/// # Example: Create a VariantDecimal4
249/// ```
250/// # use parquet_variant::VariantDecimal4;
251/// // Create a value representing the decimal 123.4567
252/// let decimal = VariantDecimal4::try_new(1234567, 4).expect("Failed to create decimal");
253/// ```
254#[derive(Debug, Clone, Copy, PartialEq)]
255pub struct VariantDecimal4 {
256    integer: i32,
257    scale: u8,
258}
259
260impl VariantDecimal4 {
261    /// Maximum number of significant digits (9 for 4-byte decimals)
262    pub const MAX_PRECISION: u8 = arrow_schema::DECIMAL32_MAX_PRECISION;
263}
264
265impl_variant_decimal!(VariantDecimal4, i32);
266
267/// Represents an 8-byte decimal value in the Variant format.
268///
269/// This struct stores a decimal number using a 64-bit signed integer for the coefficient
270/// and an 8-bit unsigned integer for the scale (number of decimal places). Its precision is between 10 and 18 digits.
271///
272/// For valid precision and scale values, see the Variant specification:
273///
274/// <https://github.com/apache/parquet-format/blob/87f2c8bf77eefb4c43d0ebaeea1778bd28ac3609/VariantEncoding.md?plain=1#L418-L420>
275///
276/// # Example: Create a VariantDecimal8
277/// ```
278/// # use parquet_variant::VariantDecimal8;
279/// // Create a value representing the decimal 123456.78
280/// let decimal = VariantDecimal8::try_new(12345678, 2).expect("Failed to create decimal");
281/// ```
282#[derive(Debug, Clone, Copy, PartialEq)]
283pub struct VariantDecimal8 {
284    integer: i64,
285    scale: u8,
286}
287
288impl VariantDecimal8 {
289    /// Maximum number of significant digits (18 for 8-byte decimals)
290    pub const MAX_PRECISION: u8 = arrow_schema::DECIMAL64_MAX_PRECISION;
291}
292
293impl_variant_decimal!(VariantDecimal8, i64);
294
295/// Represents an 16-byte decimal value in the Variant format.
296///
297/// This struct stores a decimal number using a 128-bit signed integer for the coefficient
298/// and an 8-bit unsigned integer for the scale (number of decimal places). Its precision is between 19 and 38 digits.
299///
300/// For valid precision and scale values, see the Variant specification:
301///
302/// <https://github.com/apache/parquet-format/blob/87f2c8bf77eefb4c43d0ebaeea1778bd28ac3609/VariantEncoding.md?plain=1#L418-L420>
303///
304/// # Example: Create a VariantDecimal16
305/// ```
306/// # use parquet_variant::VariantDecimal16;
307/// // Create a value representing the decimal 12345678901234567.890
308/// let decimal = VariantDecimal16::try_new(12345678901234567890, 3).unwrap();
309/// ```
310#[derive(Debug, Clone, Copy, PartialEq)]
311pub struct VariantDecimal16 {
312    integer: i128,
313    scale: u8,
314}
315
316impl VariantDecimal16 {
317    /// Maximum number of significant digits (38 for 16-byte decimals)
318    pub const MAX_PRECISION: u8 = arrow_schema::DECIMAL128_MAX_PRECISION;
319}
320
321impl_variant_decimal!(VariantDecimal16, i128);
322
323// Infallible conversion from a narrower decimal type to a wider one
324macro_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
341// Fallible conversion from a wider decimal type to a narrower one
342macro_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
365// Fallible conversion from a decimal's underlying integer type
366macro_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        // Test precision validation for Decimal4
414        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        // Test valid edge cases for Decimal4
439        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        // Test precision validation for Decimal8
452        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        // Test valid edge cases for Decimal8
477        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        // Test precision validation for Decimal16
490        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        // Test valid edge cases for Decimal16
517        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        // Test invalid scale for Decimal4 (scale > 9)
535        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        // Test valid scale edge case for Decimal4
554        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        // Test invalid scale for Decimal8 (scale > 18)
561        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        // Test valid scale edge case for Decimal8
580        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        // Test invalid scale for Decimal16 (scale > 38)
587        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        // Test valid scale edge case for Decimal16
606        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        // Test zero scale (integers)
616        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        // Test basic decimal formatting
623        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        // Test trailing zeros are trimmed
630        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        // Test leading zeros in decimal part
637        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        // Test number smaller than scale (leading zero before decimal)
644        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        // Test zero
651        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        // Test max scale
658        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        // Test max precision
665        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        // Test trailing zeros with mixed decimal places
672        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        // Test zero scale (integers)
682        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        // Test basic decimal formatting
689        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        // Test trailing zeros are trimmed
696        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        // Test leading zeros in decimal part
703        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        // Test number smaller than scale
710        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        // Test zero
717        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        // Test max scale
724        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        // Test max precision
731        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        // Test complex trailing zeros
738        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        // Test zero scale (integers)
748        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        // Test basic decimal formatting
755        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        // Test trailing zeros are trimmed
762        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        // Test leading zeros in decimal part
769        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        // Test number smaller than scale
776        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        // Test zero
783        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        // Test max scale
790        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        // Test max precision integer
798        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        // Test complex trailing zeros
805        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        // Test large integer that would overflow i64 but fits in i128
812        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}