Skip to content

Commit

Permalink
Prevent scale of 29 from postgresql hydration (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
paupino authored Feb 10, 2024
1 parent d72e787 commit 1a119c7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/postgres/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
37 changes: 37 additions & 0 deletions src/postgres/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 1a119c7

Please sign in to comment.