Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent scale of 29 when hydrating from unbound numeric data types in Postgresql #647

Merged
merged 1 commit into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading