Skip to content

Sign loss while parsing rust_decimal::Decimal to sqlx::postgres::types::PgMoney #1321

Closed

Description

@Kengyen
    let foo = Decimal::new(-314, 2);
    let bar = PgMoney::from_decimal(foo, 2);
    let baz = bar.to_decimal(2);
    println!("foo={} bar={:?} baz={}", foo, bar, baz); // foo=-3.14 bar=PgMoney(314) baz=3.14

Activity

  1. abonander commented on Jul 21, 2021

    @abonander
    Collaborator

    This is likely due to PgMoney::from_decimal() taking bytes 4..12 of Decimal::serialize() and just treating that as a little-endian twos-complement integer: https://github.com/launchbadge/sqlx/blob/be189bd11e6bdd14c45c70bdad477e780a82b050/sqlx-core/src/postgres/types/money.rs#L59-L62

    However the sign is the MSB of byte 3, so the value isn't actually twos-complement; we also shouldn't be ignoring scale which an integer in bytes 1..2.

    It actually doesn't make much sense that scale being passed in here, I think that's supposed to be the precision that the Postgres database is expecting.

    The docs make it sound like SQLx is just going to figure that out for you, but that's honestly way out of scope here because that would mean:

    • Executing SHOW lc_monetary; on the database server
    • Figure out what frac_digits is in that locale (assuming the client and server even have the same definition)
      • Use libc::set_locale() and localeconv()->frac_digits?
      • Shell out to locale <locale name> frac_digits and read the response?
        • non-portable
      • Just assume the locale on the client is the same as on the server and use localeconv()->frac_digits?
        • error-prone
        • still highly unsafe

    It would be easier to execute select 0::money::text and then count the number of zeroes before a non-digit character (which would cover locales that use a different decimal separator than .) but what about locales that don't use Arabic numerals or use right-to-left writing? It'd be a nightmare to cover everything and this is all still assuming we're talking to the database and patching the value up on-the-fly.

    So yeah, instead we should make it clear that scale should be the precision that the Postgres server is expecting or else it'll interpret the value incorrectly.

    I defended money in a previous issue but now that I'm digging more into it, it's honestly super gross. To be fair, a lot of that is also the POSIX locale system which is what, a 20 year old API?

  2. added a commit that references this issue on Jul 21, 2021
    f22a77a
  3. added a commit that references this issue on Jul 21, 2021
    a8544fd
  4. added 2 commits that reference this issue on Nov 19, 2021
    6cdb35d
    b5a95c4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions