Skip to content

Commit 1a119c7

Browse files
authored
Prevent scale of 29 from postgresql hydration (#647)
1 parent d72e787 commit 1a119c7

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/postgres/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Decimal {
7878

7979
result.set_sign_negative(neg);
8080
// Rescale to the postgres value, automatically rounding as needed.
81-
result.rescale(scale as u32);
81+
result.rescale((scale as u32).min(MAX_PRECISION_U32));
8282
result
8383
}
8484

src/postgres/driver.rs

+37
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,43 @@ mod test {
255255
assert_eq!(Decimal::ZERO, result);
256256
}
257257

258+
#[test]
259+
fn read_small_unconstrained_numeric_type() {
260+
let mut client = match Client::connect(&get_postgres_url(), NoTls) {
261+
Ok(x) => x,
262+
Err(err) => panic!("{:#?}", err),
263+
};
264+
let result: Decimal = match client.query("SELECT 0.100000000000000000000000000001::NUMERIC", &[]) {
265+
Ok(x) => x.first().unwrap().get(0),
266+
Err(err) => panic!("error - {:#?}", err),
267+
};
268+
269+
// This gets rounded to 28 decimal places. In the future we may want to introduce a global feature which
270+
// prevents rounding.
271+
assert_eq!(result.to_string(), "0.1000000000000000000000000000");
272+
assert_eq!(result.scale(), 28);
273+
}
274+
275+
#[test]
276+
fn read_small_unconstrained_numeric_type_addition() {
277+
let mut client = match Client::connect(&get_postgres_url(), NoTls) {
278+
Ok(x) => x,
279+
Err(err) => panic!("{:#?}", err),
280+
};
281+
let (a, b): (Decimal, Decimal) = match client.query(
282+
"SELECT 0.100000000000000000000000000001::NUMERIC, 0.00000000000014780214::NUMERIC",
283+
&[],
284+
) {
285+
Ok(x) => {
286+
let row = x.first().unwrap();
287+
(row.get(0), row.get(1))
288+
}
289+
Err(err) => panic!("error - {:#?}", err),
290+
};
291+
292+
assert_eq!(a + b, Decimal::from_str("0.1000000000001478021400000000").unwrap());
293+
}
294+
258295
#[test]
259296
fn read_numeric_type() {
260297
let mut client = match Client::connect(&get_postgres_url(), NoTls) {

0 commit comments

Comments
 (0)