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

fix(qe): json and top-level f32 coercion should be in sync #4961

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions libs/prisma-value/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ pub fn decode_bytes(s: impl AsRef<[u8]>) -> PrismaValueResult<Vec<u8>> {
base64::decode(s).map_err(|_| ConversionFailure::new("base64 encoded bytes", "PrismaValue::Bytes"))
}

pub fn parse_decimal(str: &str) -> std::result::Result<BigDecimal, bigdecimal::ParseBigDecimalError> {
BigDecimal::from_str(str).map(|bd| bd.normalized())
}

impl TryFrom<serde_json::Value> for PrismaValue {
type Error = crate::error::ConversionFailure;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod float {

insta::assert_snapshot!(
res,
@r###"{"data":{"findManyTestModel":[{"float":1.2},{"float":13.37},{"float":null}]}}"###
@r###"{"data":{"findManyTestModel":[{"float":1.2},{"float":13.37},{"float":1.234567891011121},{"float":null}]}}"###
);
}
EngineProtocol::Json => {
Expand All @@ -75,7 +75,7 @@ mod float {

insta::assert_snapshot!(
res.to_string(),
@r###"{"data":{"findManyTestModel":[{"float":1.2},{"float":13.37},{"float":null}]}}"###
@r###"{"data":{"findManyTestModel":[{"float":1.2},{"float":13.37},{"float":1.234567891011121},{"float":null}]}}"###
);
}
}
Expand All @@ -86,7 +86,8 @@ mod float {
async fn create_test_data(runner: &Runner) -> TestResult<()> {
create_row(runner, r#"{ id: 1, float: 1.2 }"#).await?;
create_row(runner, r#"{ id: 2, float: 13.37 }"#).await?;
create_row(runner, r#"{ id: 3 }"#).await?;
create_row(runner, r#"{ id: 3, float: 1.23456789101112131415161718192 }"#).await?;
create_row(runner, r#"{ id: 4 }"#).await?;

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,23 @@ mod decimal {
id: 1,
child: { create: {
id: 1,
float: 1.1,
dfloat: 2.2,
decFloat: 3.1234,
money: 3.51,
float: 1.23456789101112131415161718192,
dfloat: 1.23456789101112131415161718192,
decFloat: 1.23456789101112131415161718192,
money: 1.23456789101112131415161718192,
}}
}"#,
)
.await?;

insta::assert_snapshot!(
run_query!(&runner, r#"{ findManyParent { id child { float dfloat decFloat money } } }"#),
@r###"{"data":{"findManyParent":[{"id":1,"child":{"float":1.1,"dfloat":2.2,"decFloat":"3.1","money":"3.51"}}]}}"###
@r###"{"data":{"findManyParent":[{"id":1,"child":{"float":1.2345679,"dfloat":1.234567891011121,"decFloat":"1.2","money":"1.23"}}]}}"###
);

insta::assert_snapshot!(
run_query!(&runner, r#"{ findManyChild { float dfloat decFloat money } }"#),
@r###"{"data":{"findManyChild":[{"float":1.2345679,"dfloat":1.234567891011121,"decFloat":"1.2","money":"1.23"}]}}"###
);

Ok(())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use bigdecimal::{BigDecimal, ParseBigDecimalError};
use itertools::Itertools;
use query_structure::*;
use std::{borrow::Cow, io, str::FromStr};
use std::{borrow::Cow, io};

use crate::SqlError;

Expand Down Expand Up @@ -301,7 +300,3 @@ fn build_generic_conversion_error(message: String) -> SqlError {
let error = io::Error::new(io::ErrorKind::InvalidData, message);
SqlError::ConversionError(error.into())
}

fn parse_decimal(str: &str) -> std::result::Result<BigDecimal, ParseBigDecimalError> {
BigDecimal::from_str(str).map(|bd| bd.normalized())
}
3 changes: 2 additions & 1 deletion query-engine/connectors/sql-query-connector/src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{column_metadata::ColumnMetadata, error::SqlError, value::to_prisma_v
use bigdecimal::{BigDecimal, FromPrimitive, ToPrimitive};
use chrono::{DateTime, NaiveDate, Utc};
use connector_interface::{coerce_null_to_zero_value, AggregationResult, AggregationSelection};
use prisma_value::parse_decimal;
use quaint::{connector::ResultRow, Value, ValueType};
use query_structure::{ConversionFailure, FieldArity, PrismaValue, Record, TypeIdentifier};
use std::{io, str::FromStr};
Expand Down Expand Up @@ -209,7 +210,7 @@ fn row_value_to_prisma_value(p_value: Value, meta: ColumnMetadata<'_>) -> Result
ValueType::Float(Some(f)) => match f {
f if f.is_nan() => return Err(create_error(&p_value)),
f if f.is_infinite() => return Err(create_error(&p_value)),
_ => PrismaValue::Float(BigDecimal::from_f32(f).unwrap().normalized()),
_ => PrismaValue::Float(parse_decimal(&f.to_string()).unwrap()),
},
ValueType::Int32(Some(i)) => match BigDecimal::from_i32(i) {
Some(dec) => PrismaValue::Float(dec),
Expand Down
Loading