Skip to content

Commit

Permalink
refactor: remove try_into_bigint_with_precision_and_scale from `Big…
Browse files Browse the repository at this point in the history
…DecimalExt` trait and make it private
  • Loading branch information
JayWhite2357 committed Oct 22, 2024
1 parent 385411f commit 8c3986c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 47 deletions.
90 changes: 54 additions & 36 deletions crates/proof-of-sql/src/base/math/big_decimal_ext.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
use super::{
DecimalError, DecimalResult,
IntermediateDecimalError::{self, LossyCast},
Precision,
};
use super::{DecimalError, DecimalResult, Precision};
use crate::base::scalar::{Scalar, ScalarConversionError};
use bigdecimal::BigDecimal;
use num_bigint::BigInt;

fn try_convert_bigdecimal_into_bigint_with_precision_and_scale(
decimal: &BigDecimal,
precision: u64,
scale: i64,
) -> Option<BigInt> {
let normalized = decimal.normalized();
if normalized.fractional_digit_count() > scale {
return None;
}
let scaled_decimal = normalized.with_scale(scale);
if scaled_decimal.digits() > precision {
return None;
}
Some(scaled_decimal.into_bigint_and_exponent().0)
}

pub trait BigDecimalExt {
fn precision(&self) -> u64;
fn scale(&self) -> i64;
fn try_into_bigint_with_precision_and_scale(
&self,
precision: u8,
scale: i8,
) -> Result<BigInt, IntermediateDecimalError>;
/// Fallibly attempts to convert an `IntermediateDecimal` into the
/// native proof-of-sql [Scalar] backing store. This function adjusts
/// the decimal to the specified `target_precision` and `target_scale`,
Expand Down Expand Up @@ -51,37 +58,23 @@ impl BigDecimalExt for BigDecimal {
self.normalized().fractional_digit_count()
}

/// Attempts to convert the decimal to `BigInt` while adjusting it to the specified precision and scale.
/// Returns an error if the conversion cannot be performed due to precision or scale constraints.
///
/// # Errors
/// Returns an `IntermediateDecimalError::LossyCast` error if the number of digits in the scaled decimal exceeds the specified precision.
fn try_into_bigint_with_precision_and_scale(
&self,
precision: u8,
scale: i8,
) -> Result<BigInt, IntermediateDecimalError> {
if self.scale() > scale.into() {
Err(IntermediateDecimalError::ConversionFailure)?;
}
let scaled_decimal = self.normalized().with_scale(scale.into());
if scaled_decimal.digits() > precision.into() {
return Err(LossyCast);
}
let (d, _) = scaled_decimal.into_bigint_and_exponent();
Ok(d)
}

fn try_into_scalar_with_precision_and_scale<S: Scalar>(
&self,
target_precision: Precision,
target_scale: i8,
) -> DecimalResult<S> {
self.try_into_bigint_with_precision_and_scale(target_precision.value(), target_scale)?
.try_into()
.map_err(|e: ScalarConversionError| DecimalError::InvalidDecimal {
error: e.to_string(),
})
try_convert_bigdecimal_into_bigint_with_precision_and_scale(
self,
target_precision.value().into(),
target_scale.into(),
)
.ok_or(DecimalError::RoundingError {
error: self.to_string(),
})?
.try_into()
.map_err(|e: ScalarConversionError| DecimalError::InvalidDecimal {
error: e.to_string(),
})
}
}

Expand Down Expand Up @@ -117,4 +110,29 @@ mod tests {
assert_eq!(decimal.precision(), 6);
assert_eq!(decimal.scale(), 3);
}

#[test]
fn we_can_convert_bigdecimal_into_bigint_with_precision_and_scale() {
let test_cases = vec![
("123.45", 2, 20, Some("12345")),
("123.45000", 2, 20, Some("12345")),
("000123.45", 2, 20, Some("12345")),
("123.45", 6, 20, Some("123450000")),
("123.45", 2, 5, Some("12345")),
("0.0012345", 7, 20, Some("12345")),
("0.0012345", 9, 20, Some("1234500")),
("123.45", 2, 4, None),
("123.45", 1, 20, None),
];
for (bigdecimal_str, target_scale, target_precision, expected_result_str) in test_cases {
let decimal = bigdecimal_str.parse::<BigDecimal>().unwrap();
let result = try_convert_bigdecimal_into_bigint_with_precision_and_scale(
&decimal,
target_precision,
target_scale,
);
let expected_result = expected_result_str.map(|s| s.parse::<BigInt>().unwrap());
assert_eq!(result, expected_result);
}
}
}
11 changes: 0 additions & 11 deletions crates/proof-of-sql/src/base/math/big_decimal_ext_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::base::{
scalar::Curve25519Scalar,
};
use bigdecimal::BigDecimal;
use num_bigint::BigInt;

#[test]
fn we_cannot_scale_past_max_precision() {
Expand All @@ -21,16 +20,6 @@ fn we_cannot_scale_past_max_precision() {
.is_err());
}

#[test]
fn we_can_match_exact_decimals_from_queries_to_db() {
let decimal: BigDecimal = "123.45".parse().unwrap();
let target_scale = 2;
let target_precision = 20;
let big_int = decimal.try_into_bigint_with_precision_and_scale(target_precision, target_scale);
let expected_big_int: BigInt = "12345".parse().unwrap();
assert_eq!(big_int, Ok(expected_big_int));
}

#[test]
fn we_can_match_decimals_with_negative_scale() {
let decimal = "120.00".parse::<BigDecimal>().unwrap();
Expand Down

0 comments on commit 8c3986c

Please sign in to comment.