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

Fixes rust_decimal scale for Postgres #2838

Merged
merged 1 commit into from
Nov 7, 2023
Merged
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
65 changes: 47 additions & 18 deletions sqlx-postgres/src/types/rust_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ impl TryFrom<PgNumeric> for Decimal {
type Error = BoxDynError;

fn try_from(numeric: PgNumeric) -> Result<Self, BoxDynError> {
let (digits, sign, mut weight) = match numeric {
let (digits, sign, mut weight, scale) = match numeric {
PgNumeric::Number {
digits,
sign,
weight,
..
} => (digits, sign, weight),
scale,
} => (digits, sign, weight, scale),

PgNumeric::NotANumber => {
return Err("Decimal does not support NaN values".into());
Expand Down Expand Up @@ -65,6 +65,8 @@ impl TryFrom<PgNumeric> for Decimal {
PgNumericSign::Negative => value.set_sign_negative(true),
}

value.rescale(scale as u32);

Ok(value)
}
}
Expand Down Expand Up @@ -311,29 +313,38 @@ mod decimal_to_pgnumeric {
#[test]
fn decimal_4() {
let decimal: Decimal = "12345.67890".parse().unwrap();
assert_eq!(
PgNumeric::try_from(&decimal).unwrap(),
PgNumeric::Number {
sign: PgNumericSign::Positive,
scale: 5,
weight: 1,
digits: vec![1, 2345, 6789]
}
);
let expected_numeric = PgNumeric::Number {
sign: PgNumericSign::Positive,
scale: 5,
weight: 1,
digits: vec![1, 2345, 6789],
};
assert_eq!(PgNumeric::try_from(&decimal).unwrap(), expected_numeric);

let actual_decimal = Decimal::try_from(expected_numeric).unwrap();
assert_eq!(actual_decimal, decimal);
assert_eq!(actual_decimal.mantissa(), 1234567890);
assert_eq!(actual_decimal.scale(), 5);
}

#[test]
fn one_digit_decimal() {
let one_digit_decimal: Decimal = "0.00001234".parse().unwrap();
let expected_numeric = PgNumeric::Number {
sign: PgNumericSign::Positive,
scale: 8,
weight: -2,
digits: vec![1234],
};
assert_eq!(
PgNumeric::try_from(&one_digit_decimal).unwrap(),
PgNumeric::Number {
sign: PgNumericSign::Positive,
scale: 8,
weight: -2,
digits: vec![1234]
}
expected_numeric
);

let actual_decimal = Decimal::try_from(expected_numeric).unwrap();
assert_eq!(actual_decimal, one_digit_decimal);
assert_eq!(actual_decimal.mantissa(), 1234);
assert_eq!(actual_decimal.scale(), 8);
}

#[test]
Expand Down Expand Up @@ -396,6 +407,24 @@ mod decimal_to_pgnumeric {
);
}

#[test]
fn issue_2247_trailing_zeros() {
// This is a regression test for https://github.com/launchbadge/sqlx/issues/2247
let one_hundred: Decimal = "100.00".parse().unwrap();
let expected_numeric = PgNumeric::Number {
sign: PgNumericSign::Positive,
scale: 2,
weight: 0,
digits: vec![100],
};
assert_eq!(PgNumeric::try_from(&one_hundred).unwrap(), expected_numeric);

let actual_decimal = Decimal::try_from(expected_numeric).unwrap();
assert_eq!(actual_decimal, one_hundred);
assert_eq!(actual_decimal.mantissa(), 10000);
assert_eq!(actual_decimal.scale(), 2);
}

#[test]
fn issue_666_trailing_zeroes_at_max_precision() {}
}
Loading