From c285e2867069ef967da4a8cd3e381012c0f34414 Mon Sep 17 00:00:00 2001 From: Austin Bonander Date: Mon, 27 Apr 2020 14:30:47 -0700 Subject: [PATCH] fix and test handling of 0 for BigDecimal in Postgres/MySQL closes #283 --- sqlx-core/src/postgres/types/bigdecimal.rs | 6 +++++- tests/mysql-types.rs | 1 + tests/postgres-types.rs | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/sqlx-core/src/postgres/types/bigdecimal.rs b/sqlx-core/src/postgres/types/bigdecimal.rs index 47c8522ab..a5d610f6a 100644 --- a/sqlx-core/src/postgres/types/bigdecimal.rs +++ b/sqlx-core/src/postgres/types/bigdecimal.rs @@ -122,8 +122,12 @@ impl TryFrom for BigDecimal { } }; + if digits.is_empty() { + // Postgres returns an empty digit array for 0 but BigInt expects at least one zero + return Ok(0u64.into()); + } + let sign = match sign { - _ if digits.is_empty() => Sign::NoSign, PgNumericSign::Positive => Sign::Plus, PgNumericSign::Negative => Sign::Minus, }; diff --git a/tests/mysql-types.rs b/tests/mysql-types.rs index e4b25e0e2..a393e3e3b 100644 --- a/tests/mysql-types.rs +++ b/tests/mysql-types.rs @@ -128,6 +128,7 @@ mod time_tests { test_type!(decimal( MySql, sqlx::types::BigDecimal, + "CAST(0 as DECIMAL(0, 0))" == "0".parse::().unwrap(), "CAST(1 AS DECIMAL(1, 0))" == "1".parse::().unwrap(), "CAST(10000 AS DECIMAL(5, 0))" == "10000".parse::().unwrap(), "CAST(0.1 AS DECIMAL(2, 1))" == "0.1".parse::().unwrap(), diff --git a/tests/postgres-types.rs b/tests/postgres-types.rs index 83f1370fc..4514b6bb2 100644 --- a/tests/postgres-types.rs +++ b/tests/postgres-types.rs @@ -166,6 +166,8 @@ test_prepared_type!(numeric( test_type!(decimal( Postgres, sqlx::types::BigDecimal, + // https://github.com/launchbadge/sqlx/issues/283 + "0::numeric" == "0".parse::().unwrap(), "1::numeric" == "1".parse::().unwrap(), "10000::numeric" == "10000".parse::().unwrap(), "0.1::numeric" == "0.1".parse::().unwrap(),