Skip to content

Commit 1a119c7

Browse files
authored
Prevent scale of 29 from postgresql hydration (paupino#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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 37 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy