mysqljs / mysql

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

Column expressions are always being returned as strings when bigNumberStrings is enabled #1988

Closed kirliam closed 6 years ago

kirliam commented 6 years ago

Hello. I have enabled this option, but I'm getting my expression columns always being returned as strings. Example:

"SELECT 1*1 as test"

I would expect [{test: 1}] to be returned, but it's returning [{test: "1"}]

Even if I cast the column to a INT type, it still returns string:

"SELECT (1*1 as unsigned) as test"

According to the documentation, this should happen only with BIGINT and DECIMAL types.

dougwilson commented 6 years ago

So the way it works is that we know if it's a bignumber based on what the MySQL server sends back in the packets. For each result column, MySQL tells the type of the column. In both of those cases, the MySQL server is sending back the column type as bigint, thus why they are coming to you as strings when you have bignumbers as strings turned on.

You can read about this in the MySQL manual for math operations: https://dev.mysql.com/doc/refman/5.7/en/arithmetic-functions.html

In the case of -, +, and *, the result is calculated with BIGINT (64-bit) precision if both operands are integers.