Skip to content

Commit

Permalink
fix: Make rust_decimal and bigdecimal decoding more lenient
Browse files Browse the repository at this point in the history
fixes readysettech/readyset#143

When using readyset as a caching proxy - readyset returns a decimal with the following type info `MySqlTypeInfo { type: Decimal, flags: ColumnFlags(0x0), char_set: 33, max_size: Some(1024) }`

Currently rust_decimal and bigdecimal expect an exact match for the type info `MySqlTypeInfo { type: NewDecimal, flags: ColumnFlags(BINARY), char_set: 63, max_size: None }`

Therefore the following error  occurs when readyset sends a valid decimal type

```
    error occurred while decoding column "price": mismatched types; Rust type `core::option::Option<rust_decimal::decimal::Decimal>` (as SQL type `DECIMAL`) is not compatible with SQL type `DECIMAL`
```

This patch makes the `Type<MySql> for Decimal` more lenient by matching `MySqlTypeInfo` that has ColumType::Decimal |  ColumnType::NewDecimal to be parsed by both rust_decimal and bigdecimal types
  • Loading branch information
cameronbraid committed Nov 7, 2023
1 parent ee73c84 commit 11e2e28
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sqlx-mysql/src/types/bigdecimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ impl Type<MySql> for BigDecimal {
fn type_info() -> MySqlTypeInfo {
MySqlTypeInfo::binary(ColumnType::NewDecimal)
}

fn compatible(ty: &MySqlTypeInfo) -> bool {
matches!(ty.r#type, ColumnType::Decimal | ColumnType::NewDecimal)
}
}

impl Encode<'_, MySql> for BigDecimal {
Expand Down
4 changes: 4 additions & 0 deletions sqlx-mysql/src/types/rust_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ impl Type<MySql> for Decimal {
fn type_info() -> MySqlTypeInfo {
MySqlTypeInfo::binary(ColumnType::NewDecimal)
}

fn compatible(ty: &MySqlTypeInfo) -> bool {
matches!(ty.r#type, ColumnType::Decimal | ColumnType::NewDecimal)
}
}

impl Encode<'_, MySql> for Decimal {
Expand Down

0 comments on commit 11e2e28

Please sign in to comment.