mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.22k stars 2.53k forks source link

Better type casting of BIT values #2559

Open sidorares opened 1 year ago

sidorares commented 1 year ago

see similar discussion in https://github.com/sidorares/node-mysql2/issues/1597

Currently BIT fields are always returned as single byte Buffer:

https://github.com/mysqljs/mysql/blob/dc9c152a87ec51a1f647447268917243d2eab1fd/lib/protocol/packets/RowDataPacket.js#L107

I think this should be returned as a number. Might be good to coordinate both changes here in mysql2, this is potentially a breaking backwards incompatible change

dougwilson commented 1 year ago

Good idea. Need to make sure we don't run in to Javascript number issues. The BIT field is up to 64 bits wide and of course a plain javascript number can only be 53 bits wide. Perhaps it would just always return a BigInt?

sidorares commented 1 year ago

Oh, for some reason I assumed it's always just one byte. Not so sure about this change now, really depends on what's the most common use case in real life. If it's actually used as a "bit field" a Buffer might be a good container for it

dougwilson commented 1 year ago

Gotcha. Yes, I don't personally use the field, so not sure all the uses folks have for it. The field itself comes back as a length-encoded string field over the text protocol and the BIT type in the database can be between 1 and 64 bits. The string contents is just the binary data of the bits, not really much different from like a BINARY field, just that the length is defined in bits instead of bytes. I guess there is probably some kind of storage optimization possible for this but 🤷