Skip to content

Commit

Permalink
driver-adapters: conver Decimals to Strings when calling JS
Browse files Browse the repository at this point in the history
When forwarding the input to the driver adapter, we used to convert
decimal values to f64. On a large numbers, that lost some precision.
Changing this to convert them to strings instead.

Fix prisma/team-orm#497

TODO: after this gets to the client, unskip `decimal/presion` test
there.
  • Loading branch information
SevInf committed Nov 2, 2023
1 parent 49b44c5 commit 246e0ce
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,35 @@ mod mysql {
Ok(())
}

fn schema_decimal_vitess() -> String {
let schema = indoc! {
r#"model Model {
#id(id, String, @id, @default(cuid()))
decLarge Decimal @test.Decimal(20, 10)
}"#
};

schema.to_owned()
}

#[connector_test(only(Vitess), schema(schema_decimal_vitess))]
async fn native_decimal_vitess_precision(runner: Runner) -> TestResult<()> {
insta::assert_snapshot!(
run_query!(&runner, r#"mutation {
createOneModel(
data: {
decLarge: "131603421.38724228"
}
) {
decLarge
}
}"#),
@r###"{"data":{"createOneModel":{"decLarge":"131603421.38724228"}}}"###
);

Ok(())
}

fn schema_string() -> String {
let schema = indoc! {
r#"model Model {
Expand Down
7 changes: 2 additions & 5 deletions query-engine/driver-adapters/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,8 @@ pub fn values_to_js_args(values: &[quaint::Value<'_>]) -> serde_json::Result<Vec
Some(bytes) => JSArg::Buffer(bytes.to_vec()),
None => JsonValue::Null.into(),
},
quaint_value @ quaint::ValueType::Numeric(bd) => match bd {
Some(bd) => match bd.to_string().parse::<f64>() {
Ok(double) => JSArg::from(JsonValue::from(double)),
Err(_) => JSArg::from(JsonValue::from(quaint_value.clone())),
},
quaint::ValueType::Numeric(bd) => match bd {
Some(bd) => JSArg::RawString(bd.to_string()),
None => JsonValue::Null.into(),
},
quaint::ValueType::Array(Some(items)) => JSArg::Array(values_to_js_args(items)?),
Expand Down
4 changes: 0 additions & 4 deletions query-engine/driver-adapters/src/conversion/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ pub fn values_to_js_args(values: &[quaint::Value<'_>]) -> serde_json::Result<Vec
Some(bytes) => JSArg::Buffer(bytes.to_vec()),
None => JsonValue::Null.into(),
},
(quaint::ValueType::Numeric(bd), _) => match bd {
Some(bd) => JSArg::RawString(bd.to_string()),
None => JsonValue::Null.into(),
},
(quaint::ValueType::Array(Some(items)), _) => JSArg::Array(values_to_js_args(items)?),
(quaint_value, _) => JSArg::from(JsonValue::from(quaint_value.clone())),
};
Expand Down

0 comments on commit 246e0ce

Please sign in to comment.