Skip to content

fix(mysql): decode DECIMAL columns into integer types correctly - #4503

Open
Map130 wants to merge 1 commit into
transact-rs:mainfrom
Map130:fix/mysql-decimal-integer-decode
Open

Map130 wants to merge 1 commit into
transact-rs:mainfrom
Map130:fix/mysql-decimal-integer-decode

Conversation

@Map130

@Map130 Map130 commented Nov 3, 2026

Copy link
Copy Markdown

This reworks #4422, which you suggested I reopen as a separate PR. Same fix,
with the issues from last time sorted out.

When decoding a MySQL DECIMAL/NEWDECIMAL column into an integer type, you get
the wrong value. MySQL sends these columns as ASCII text even over the binary
protocol (a SUM() over integers is one way to hit this), but int_decode and
uint_decode read the bytes as a little-endian integer.

So a DECIMAL(4,0) of 13 comes over the wire as the bytes 0x31 0x33 ("13"), and
reading that as little-endian gives 0x3331 = 13105. It's silent: nothing errors,
you just get a garbage number. I ran into it in production reading an aggregate
into u64 via try_get_unchecked. The checked query_scalar! path infers Decimal
and isn't affected, which is why it slipped through.

The fix special-cases DECIMAL/NEWDECIMAL in int_decode/uint_decode: parse the
text and require it to be integral. Integral values decode correctly; a value
with an actual fractional part returns an error instead of a corrupted result.
Negative values only decode into signed types. Type::compatible is left alone,
so the macros still map DECIMAL to Decimal/BigDecimal as before.

Is this a breaking change?

Yes, but a mild one. Decoding a DECIMAL into an integer used to give a corrupted
value (or a ParseIntError over the text protocol); now it gives the correct
value, or a clean error on a fractional part. Someone could in theory have
depended on the old output, but it was data corruption. The checked macros are
unaffected since Type::compatible is unchanged.

MySQL sends DECIMAL/NEWDECIMAL as ASCII text even over the binary
protocol, but int_decode/uint_decode read those bytes as a little-endian
integer. Decoding e.g. SUM(int_col) into u64 via a type override or
try_get_unchecked silently produced garbage (b"13" read as 0x3331 =
13105 instead of 13).

Parse the text for DECIMAL columns and require it to be integral:
integral values decode correctly, while fractional ones now return an
error instead of a corrupted result.

Type::compatible is left unchanged, so query! inference still maps
DECIMAL columns to Decimal/BigDecimal.
@Map130
Map130 marked this pull request as ready for review November 3, 2026 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant