diff --git a/crates/proof-of-sql/src/base/math/big_decimal_ext.rs b/crates/proof-of-sql/src/base/math/big_decimal_ext.rs index e808d8f1b..ce2e791fa 100644 --- a/crates/proof-of-sql/src/base/math/big_decimal_ext.rs +++ b/crates/proof-of-sql/src/base/math/big_decimal_ext.rs @@ -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 { + 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; /// 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`, @@ -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 { - 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( &self, target_precision: Precision, target_scale: i8, ) -> DecimalResult { - 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(), + }) } } @@ -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::().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::().unwrap()); + assert_eq!(result, expected_result); + } + } } diff --git a/crates/proof-of-sql/src/base/math/big_decimal_ext_test.rs b/crates/proof-of-sql/src/base/math/big_decimal_ext_test.rs index abc6f934e..99332f7a0 100644 --- a/crates/proof-of-sql/src/base/math/big_decimal_ext_test.rs +++ b/crates/proof-of-sql/src/base/math/big_decimal_ext_test.rs @@ -3,7 +3,6 @@ use crate::base::{ scalar::Curve25519Scalar, }; use bigdecimal::BigDecimal; -use num_bigint::BigInt; #[test] fn we_cannot_scale_past_max_precision() { @@ -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::().unwrap();