Skip to content

Commit

Permalink
Fix precision loss issue on decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhoule committed Mar 9, 2020
1 parent 13fe58d commit 05dea10
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl ToString for ScalarType {
pub enum ScalarValue {
Int(i32),
Float(f32),
Decimal(f32),
Decimal(f64),
Boolean(bool),
String(String),
DateTime(DateTime<Utc>),
Expand Down
6 changes: 3 additions & 3 deletions libs/datamodel/core/src/common/value_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ impl ValueValidator {

// TODO: Ask which decimal type to take.
/// Tries to convert the wrapped value to a Prisma Decimal.
pub fn as_decimal(&self) -> Result<f32, DatamodelError> {
pub fn as_decimal(&self) -> Result<f64, DatamodelError> {
match &self.value {
ast::Expression::NumericValue(value, _) => self.wrap_error_from_result(value.parse::<f32>(), "numeric"),
ast::Expression::Any(value, _) => self.wrap_error_from_result(value.parse::<f32>(), "numeric"),
ast::Expression::NumericValue(value, _) => self.wrap_error_from_result(value.parse::<f64>(), "numeric"),
ast::Expression::Any(value, _) => self.wrap_error_from_result(value.parse::<f64>(), "numeric"),
_ => Err(self.construct_type_mismatch_error("numeric")),
}
}
Expand Down
61 changes: 61 additions & 0 deletions query-engine/prisma/src/tests/type_mappings/postgres_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,64 @@ async fn postgres_types_roundtrip(api: &TestApi) -> TestResult {

Ok(())
}

#[test_each_connector(tags("postgres"))]
async fn small_float_values_must_work(api: &TestApi) -> TestResult {
let schema = indoc! {
r#"
CREATE TABLE floatilla (
id SERIAL PRIMARY KEY,
f32 float4,
f64 float8,
decimal_column decimal
);
"#
};

api.execute(schema).await?;

let (datamodel, engine) = api.create_engine().await?;

datamodel.assert_model("floatilla", |model| {
model
.assert_field_type("f32", ScalarType::Float)?
.assert_field_type("f64", ScalarType::Float)?
.assert_field_type("decimal_column", ScalarType::Float)
})?;

let query = indoc! {
r##"
mutation {
createOnefloatilla(
data: {
f32: 0.00006927,
f64: 0.00006927,
decimal_column: 0.00006927
}
) {
id
f32
f64
decimal_column
}
}
"##
};

let response = engine.request(query).await;

let expected_response = json!({
"data": {
"createOnefloatilla": {
"id": 1,
"f32": 0.00006927,
"f64": 0.00006927,
"decimal_column": 0.00006927
}
}
});

assert_eq!(response, expected_response);

Ok(())
}

0 comments on commit 05dea10

Please sign in to comment.