Closed thecheatah closed 4 years ago
The native D mysql driver seems to have run into a similar issue: https://github.com/mysql-d/mysql-native/issues/39
newdecimal appears to be a length encoded string as documented here: https://mariadb.com/kb/en/resultset-row/#decimal-binary-encoding
From observation a 0
is represented as a nil buffer. 1
is represented as the string 1
. From the documentation above, numbers can also be 123.456
.
Example encoding of 1 as a new decimal
(lldb) po self
▿ <MYSQL_TYPE_NEWDECIMAL>
▿ type : MYSQL_TYPE_NEWDECIMAL
- rawValue : 246
- format : MySQLNIO.MySQLData.Format.binary
▿ buffer : Optional<ByteBuffer>
▿ some : ByteBuffer { readerIndex: 0, writerIndex: 1, readableBytes: 1, capacity: 1, slice: _ByteBufferSlice { 7..<8 }, storage: 0x00000001018d8600 (32768 bytes) }
▿ _storage : <_Storage: 0x100cad320>
- _readerIndex : 0
- _writerIndex : 1
▿ _slice : _ByteBufferSlice { 7..<8 }
- upperBound : 8
▿ _begin : 7
- b12 : 7
- b3 : 0
- isUnsigned : false
(lldb) po buffer.readString(length: buffer.readableBytes)
▿ Optional<String>
- some : "1"
I am not able to decode an integer for a query like
where
unread_messages_count
is anint(11)
in mysql. Going through the debugger, it seems that we hit thedefault
case on line 313 ofMySQLData.swift
.The value of
MySQLData.type
that the switch statement is switching off of has a raw value of246
. Type246
isnewdecimal
from MySQLProtocol+DataType.swift line 68. Should we handle convertingnewdecimal
toInt
? The default.sum()
aggregation function tries to decode toInt
as well and I was having trouble with the following code:I was getting the same decoding error as the raw query.
I can see why they might use decimal as the summed up columns might not be integers. Looking at the MySQLNIO decoding logic, it doesn't seem like we handle decoding of
newdecimal
anywhere.newdecimal
seems to be unimplemented.Also, does decoding sum as int by default make sense? (I can be convinced either way)This is dynamic based on the type of the field. The "issue" is that for an int column, mysql is returning anewdecimal
that cannot be parsed as an int.I am using the latest docker image for MySQL 5.7.
My work around for this bug was to cast the sum aggregate as
SIGNED
Added test case that fails:
Debugger returns