use std::collections::BTreeMap;
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
use thiserror::Error as ThisError;
use uuid::Uuid;
#[derive(Clone, Debug, PartialEq)]
pub enum VariantValue {
Null,
Boolean(bool),
Int8(i8),
Int16(i16),
Int32(i32),
Int64(i64),
Float(f32),
Double(f64),
Decimal4(VariantDecimal4),
Decimal8(VariantDecimal8),
Decimal16(VariantDecimal16),
String(String),
Binary(Vec<u8>),
Date(NaiveDate),
Time(NaiveTime),
TimestampMicros(DateTime<Utc>),
TimestampNtzMicros(NaiveDateTime),
TimestampNanos(DateTime<Utc>),
TimestampNtzNanos(NaiveDateTime),
Uuid(Uuid),
List(Vec<VariantValue>),
Object(BTreeMap<String, VariantValue>),
}
#[derive(Clone, Debug, PartialEq, Eq, ThisError)]
pub enum VariantDecimalError {
#[error("the scale {scale} is larger than the maximum precision {max_precision}")]
ScaleTooLarge { scale: u8, max_precision: u8 },
#[error("{integer} has more digits than the maximum precision {max_precision}")]
TooManyDigits { integer: i128, max_precision: u8 },
}
macro_rules! variant_decimal {
($(#[$doc:meta])* $name:ident, $native:ty, $max_precision:expr) => {
$(#[$doc])*
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct $name {
integer: $native,
scale: u8,
}
impl $name {
pub const MAX_PRECISION: u8 = $max_precision;
pub fn try_new(integer: $native, scale: u8) -> Result<Self, VariantDecimalError> {
let max_precision = Self::MAX_PRECISION;
if scale > max_precision {
return Err(VariantDecimalError::ScaleTooLarge {
scale,
max_precision,
});
}
let max_integer = <$native>::pow(10, max_precision as u32) - 1;
if !(-max_integer..=max_integer).contains(&integer) {
return Err(VariantDecimalError::TooManyDigits {
integer: integer as i128,
max_precision,
});
}
Ok(Self { integer, scale })
}
pub fn integer(&self) -> $native {
self.integer
}
pub fn scale(&self) -> u8 {
self.scale
}
}
};
}
variant_decimal!(
VariantDecimal4,
i32,
9
);
variant_decimal!(
VariantDecimal8,
i64,
18
);
variant_decimal!(
VariantDecimal16,
i128,
38
);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decimals_keep_the_parquet_bounds() {
assert!(VariantDecimal4::try_new(999_999_999, 9).is_ok());
assert!(VariantDecimal4::try_new(-999_999_999, 0).is_ok());
assert_eq!(
VariantDecimal4::try_new(1_000_000_000, 0),
Err(VariantDecimalError::TooManyDigits {
integer: 1_000_000_000,
max_precision: 9
})
);
assert_eq!(
VariantDecimal4::try_new(-1_000_000_000, 0),
Err(VariantDecimalError::TooManyDigits {
integer: -1_000_000_000,
max_precision: 9
})
);
assert_eq!(
VariantDecimal4::try_new(1, 10),
Err(VariantDecimalError::ScaleTooLarge {
scale: 10,
max_precision: 9
})
);
assert!(VariantDecimal8::try_new(999_999_999_999_999_999, 18).is_ok());
assert!(VariantDecimal8::try_new(1_000_000_000_000_000_000, 0).is_err());
assert!(VariantDecimal8::try_new(1, 19).is_err());
let max16 = 10i128.pow(38) - 1;
assert!(VariantDecimal16::try_new(max16, 38).is_ok());
assert!(VariantDecimal16::try_new(-max16, 0).is_ok());
assert!(VariantDecimal16::try_new(max16 + 1, 0).is_err());
assert!(VariantDecimal16::try_new(i128::MIN, 0).is_err());
assert!(VariantDecimal16::try_new(1, 39).is_err());
}
#[test]
fn decimal_errors_name_the_bound() {
assert_eq!(
VariantDecimal4::try_new(1, 10).unwrap_err().to_string(),
"the scale 10 is larger than the maximum precision 9"
);
assert_eq!(
VariantDecimal8::try_new(-1_000_000_000_000_000_000, 0)
.unwrap_err()
.to_string(),
"-1000000000000000000 has more digits than the maximum precision 18"
);
}
}