| 1 |
use crate::constants::MAX_PRECISION_U32; |
| 2 |
use crate::{ |
| 3 |
ops::array::{div_by_u32, is_all_zero, mul_by_u32}, |
| 4 |
Decimal, |
| 5 |
}; |
| 6 |
use core::fmt; |
| 7 |
use std::error; |
| 8 |
|
| 9 |
#[derive(Debug, Clone)] |
| 10 |
pub struct InvalidDecimal { |
| 11 |
inner: Option<String>, |
| 12 |
} |
| 13 |
|
| 14 |
impl fmt::Display for InvalidDecimal { |
| 15 |
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 16 |
if let Some(ref msg) = self.inner { |
| 17 |
fmt.write_fmt(format_args!("Invalid Decimal: {}", msg)) |
| 18 |
} else { |
| 19 |
fmt.write_str("Invalid Decimal") |
| 20 |
} |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
impl error::Error for InvalidDecimal {} |
| 25 |
|
| 26 |
pub(in crate::postgres) struct PostgresDecimal<D> { |
| 27 |
pub neg: bool, |
| 28 |
pub weight: i16, |
| 29 |
pub scale: u16, |
| 30 |
pub digits: D, |
| 31 |
} |
| 32 |
|
| 33 |
impl Decimal { |
| 34 |
pub(in crate::postgres) fn from_postgres<D: ExactSizeIterator<Item = u16>>( |
| 35 |
PostgresDecimal { |
| 36 |
neg, |
| 37 |
scale, |
| 38 |
digits, |
| 39 |
weight, |
| 40 |
}: PostgresDecimal<D>, |
| 41 |
) -> Self { |
| 42 |
let mut digits = digits.into_iter().collect::<Vec<_>>(); |
| 43 |
|
| 44 |
let fractionals_part_count = digits.len() as i32 + (-weight as i32) - 1; |
| 45 |
let integers_part_count = weight as i32 + 1; |
| 46 |
|
| 47 |
let mut result = Decimal::ZERO; |
| 48 |
// adding integer part |
| 49 |
if integers_part_count > 0 { |
| 50 |
let (start_integers, last) = if integers_part_count > digits.len() as i32 { |
| 51 |
(integers_part_count - digits.len() as i32, digits.len() as i32) |
| 52 |
} else { |
| 53 |
(0, integers_part_count) |
| 54 |
}; |
| 55 |
let integers: Vec<_> = digits.drain(..last as usize).collect(); |
| 56 |
for digit in integers { |
| 57 |
result *= Decimal::from_i128_with_scale(10i128.pow(4), 0); |
| 58 |
result += Decimal::new(digit as i64, 0); |
| 59 |
} |
| 60 |
result *= Decimal::from_i128_with_scale(10i128.pow(4 * start_integers as u32), 0); |
| 61 |
} |
| 62 |
// adding fractional part |
| 63 |
if fractionals_part_count > 0 { |
| 64 |
let start_fractionals = if weight < 0 { (-weight as u32) - 1 } else { 0 }; |
| 65 |
for (i, digit) in digits.into_iter().enumerate() { |
| 66 |
let fract_pow = 4 * (i as u32 + 1 + start_fractionals); |
| 67 |
if fract_pow <= MAX_PRECISION_U32 { |
| 68 |
result += Decimal::new(digit as i64, 0) / Decimal::from_i128_with_scale(10i128.pow(fract_pow), 0); |
| 69 |
} else if fract_pow == MAX_PRECISION_U32 + 4 { |
| 70 |
// rounding last digit |
| 71 |
if digit >= 5000 { |
| 72 |
result += |
| 73 |
Decimal::new(1_i64, 0) / Decimal::from_i128_with_scale(10i128.pow(MAX_PRECISION_U32), 0); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
result.set_sign_negative(neg); |
| 80 |
// Rescale to the postgres value, automatically rounding as needed. |
| 81 |
result.rescale(scale as u32); |
| 82 |
result |
| 83 |
} |
| 84 |
|
| 85 |
pub(in crate::postgres) fn to_postgres(self) -> PostgresDecimal<Vec<i16>> { |
| 86 |
if self.is_zero() { |
| 87 |
return PostgresDecimal { |
| 88 |
neg: false, |
| 89 |
weight: 0, |
| 90 |
scale: 0, |
| 91 |
digits: vec![0], |
| 92 |
}; |
| 93 |
} |
| 94 |
let scale = self.scale() as u16; |
| 95 |
|
| 96 |
let groups_diff = scale & 0x3; // groups_diff = scale % 4 |
| 97 |
|
| 98 |
let mut mantissa = self.mantissa_array4(); |
| 99 |
|
| 100 |
if groups_diff > 0 { |
| 101 |
let remainder = 4 - groups_diff; |
| 102 |
let power = 10u32.pow(u32::from(remainder)); |
| 103 |
mul_by_u32(&mut mantissa, power); |
| 104 |
} |
| 105 |
|
| 106 |
// array to store max mantissa of Decimal in Postgres decimal format |
| 107 |
const MAX_GROUP_COUNT: usize = 8; |
| 108 |
let mut digits = Vec::with_capacity(MAX_GROUP_COUNT); |
| 109 |
|
| 110 |
while !is_all_zero(&mantissa) { |
| 111 |
let digit = div_by_u32(&mut mantissa, 10000) as u16; |
| 112 |
digits.push(digit.try_into().unwrap()); |
| 113 |
} |
| 114 |
digits.reverse(); |
| 115 |
let digits_after_decimal = (scale + 3) as u16 / 4; |
| 116 |
let weight = digits.len() as i16 - digits_after_decimal as i16 - 1; |
| 117 |
|
| 118 |
let unnecessary_zeroes = if weight >= 0 { |
| 119 |
let index_of_decimal = (weight + 1) as usize; |
| 120 |
digits |
| 121 |
.get(index_of_decimal..) |
| 122 |
.expect("enough digits exist") |
| 123 |
.iter() |
| 124 |
.rev() |
| 125 |
.take_while(|i| **i == 0) |
| 126 |
.count() |
| 127 |
} else { |
| 128 |
0 |
| 129 |
}; |
| 130 |
let relevant_digits = digits.len() - unnecessary_zeroes; |
| 131 |
digits.truncate(relevant_digits); |
| 132 |
|
| 133 |
PostgresDecimal { |
| 134 |
neg: self.is_sign_negative(), |
| 135 |
digits, |
| 136 |
scale, |
| 137 |
weight, |
| 138 |
} |
| 139 |
} |
| 140 |
} |