You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Sign loss while parsing rust_decimal::Decimal to sqlx::postgres::types::PgMoney #1321
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?
not thread-safe
changes the output of a bunch of C functions across the entire process
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?
Activity
abonander commentedon Jul 21, 2021
This is likely due to
PgMoney::from_decimal()taking bytes4..12ofDecimal::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-L62However the sign is the MSB of byte 3, so the value isn't actually twos-complement; we also shouldn't be ignoring
scalewhich an integer in bytes1..2.It actually doesn't make much sense that
scalebeing 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:
SHOW lc_monetary;on the database serverfrac_digitsis in that locale (assuming the client and server even have the same definition)libc::set_locale()andlocaleconv()->frac_digits?locale <locale name> frac_digitsand read the response?localeconv()->frac_digits?It would be easier to execute
select 0::money::textand 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
scaleshould be the precision that the Postgres server is expecting or else it'll interpret the value incorrectly.I defended
moneyin 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?fix(pg_money): handle negative values correctly in `PgMoney::from_dec…
fix(pg_money): handle negative values correctly in `PgMoney::from_dec…
Merge launchbadge/sqlx master into JetASAP/sqlx master (#12)
Update ws-support (#15)