From 338c0b09276bbfce7a75bc439914947bde6dc750 Mon Sep 17 00:00:00 2001 From: Paul Mason Date: Sat, 10 Feb 2024 12:08:06 -0800 Subject: [PATCH] Prevent scale of 29 from postgresql hydration --- src/postgres/common.rs | 2 +- src/postgres/driver.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/postgres/common.rs b/src/postgres/common.rs index 3922b7d..485a259 100644 --- a/src/postgres/common.rs +++ b/src/postgres/common.rs @@ -78,7 +78,7 @@ impl Decimal { result.set_sign_negative(neg); // Rescale to the postgres value, automatically rounding as needed. - result.rescale(scale as u32); + result.rescale((scale as u32).min(MAX_PRECISION_U32)); result } diff --git a/src/postgres/driver.rs b/src/postgres/driver.rs index 1aafd78..1adac5a 100644 --- a/src/postgres/driver.rs +++ b/src/postgres/driver.rs @@ -255,6 +255,43 @@ mod test { assert_eq!(Decimal::ZERO, result); } + #[test] + fn read_small_unconstrained_numeric_type() { + let mut client = match Client::connect(&get_postgres_url(), NoTls) { + Ok(x) => x, + Err(err) => panic!("{:#?}", err), + }; + let result: Decimal = match client.query("SELECT 0.100000000000000000000000000001::NUMERIC", &[]) { + Ok(x) => x.first().unwrap().get(0), + Err(err) => panic!("error - {:#?}", err), + }; + + // This gets rounded to 28 decimal places. In the future we may want to introduce a global feature which + // prevents rounding. + assert_eq!(result.to_string(), "0.1000000000000000000000000000"); + assert_eq!(result.scale(), 28); + } + + #[test] + fn read_small_unconstrained_numeric_type_addition() { + let mut client = match Client::connect(&get_postgres_url(), NoTls) { + Ok(x) => x, + Err(err) => panic!("{:#?}", err), + }; + let (a, b): (Decimal, Decimal) = match client.query( + "SELECT 0.100000000000000000000000000001::NUMERIC, 0.00000000000014780214::NUMERIC", + &[], + ) { + Ok(x) => { + let row = x.first().unwrap(); + (row.get(0), row.get(1)) + } + Err(err) => panic!("error - {:#?}", err), + }; + + assert_eq!(a + b, Decimal::from_str("0.1000000000001478021400000000").unwrap()); + } + #[test] fn read_numeric_type() { let mut client = match Client::connect(&get_postgres_url(), NoTls) {