VariantDecimalType

Trait VariantDecimalType 

pub trait VariantDecimalType: Into<Variant<'static, 'static>> {
    type Native;

    const MAX_PRECISION: u8;
    const MAX_UNSCALED_VALUE: Self::Native;

    // Required methods
    fn try_new(integer: Self::Native, scale: u8) -> Result<Self, ArrowError>;
    fn try_new_with_signed_scale(
        integer: Self::Native,
        scale: i8,
    ) -> Result<Self, ArrowError>;
    fn integer(&self) -> Self::Native;
    fn scale(&self) -> u8;

    // Provided method
    fn is_valid_precision_and_scale(precision: &u8, scale: &i8) -> bool { ... }
}
Expand description

Trait for variant decimal types, enabling generic code across Decimal4/8/16

This trait provides a common interface for the three variant decimal types, allowing generic functions and data structures to work with any decimal width. It is modeled after Arrow’s DecimalType trait but adapted for variant semantics.

§Example

fn extract_scale<D: VariantDecimalType>(decimal: D) -> u8 {
    decimal.scale()
}

let dec4 = VariantDecimal4::try_new(12345, 2).unwrap();
let dec8 = VariantDecimal8::try_new(67890, 3).unwrap();

assert_eq!(extract_scale(dec4), 2);
assert_eq!(extract_scale(dec8), 3);

Required Associated Constants§

const MAX_PRECISION: u8

Maximum number of significant digits this decimal type can represent (9, 18, or 38)

const MAX_UNSCALED_VALUE: Self::Native

The largest positive unscaled value that fits in Self::MAX_PRECISION digits.

Required Associated Types§

type Native

The underlying signed integer type (i32, i64, or i128)

Required Methods§

fn try_new(integer: Self::Native, scale: u8) -> Result<Self, ArrowError>

Creates a new decimal value from the given unscaled integer and scale, failing if the integer’s width, or the requested scale, exceeds MAX_PRECISION.

NOTE: For compatibility with arrow decimal types, negative scale is allowed as long as the rescaled value fits in the available precision.

§Example
// Valid: 123.45 (5 digits, scale 2)
let d = VariantDecimal4::try_new(12345, 2).unwrap();
assert_eq!(d.integer(), 12345);
assert_eq!(d.scale(), 2);

VariantDecimal4::try_new(123, 10).expect_err("scale exceeds MAX_PRECISION");
VariantDecimal4::try_new(1234567890, 10).expect_err("value's width exceeds MAX_PRECISION");

fn try_new_with_signed_scale( integer: Self::Native, scale: i8, ) -> Result<Self, ArrowError>

Attempts to convert an unscaled arrow decimal value to the indicated variant decimal type.

Unlike Self::try_new, this function accepts a signed scale, and attempts to rescale negative-scale values to their equivalent (larger) scale-0 values. For example, a decimal value of 123 with scale -2 becomes 12300 with scale 0.

Fails if rescaling fails, or for any of the reasons Self::try_new could fail.

fn integer(&self) -> Self::Native

Returns the unscaled integer value

fn scale(&self) -> u8

Returns the scale (number of digits after the decimal point)

Provided Methods§

fn is_valid_precision_and_scale(precision: &u8, scale: &i8) -> bool

True if the given precision and scale are valid for this variant decimal type.

NOTE: By a strict reading of the “decimal table” in the [variant spec], one might conclude that each decimal type has both lower and upper bounds on precision (i.e. Decimal16 with precision 5 is invalid because Decimal4 “covers” it). But the variant shredding integration tests specifically expect such cases to succeed, so we only enforce the upper bound here.

§Example
assert!(VariantDecimal4::is_valid_precision_and_scale(&5, &2));
assert!(!VariantDecimal4::is_valid_precision_and_scale(&10, &2)); // too wide
assert!(!VariantDecimal4::is_valid_precision_and_scale(&5, &-1)); // negative scale
assert!(!VariantDecimal4::is_valid_precision_and_scale(&5, &7)); // scale too big

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

§

impl VariantDecimalType for VariantDecimal4

§

const MAX_PRECISION: u8 = 9u8

§

const MAX_UNSCALED_VALUE: i32 = 999_999_999i32

§

type Native = i32

§

impl VariantDecimalType for VariantDecimal8

§

const MAX_PRECISION: u8 = 18u8

§

const MAX_UNSCALED_VALUE: i64 = 999_999_999_999_999_999i64

§

type Native = i64

§

impl VariantDecimalType for VariantDecimal16

§

const MAX_PRECISION: u8 = 38u8

§

const MAX_UNSCALED_VALUE: i128 = 99_999_999_999_999_999_999_999_999_999_999_999_999i128

§

type Native = i128