Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix big number accuracy #2276

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions lib/packets/packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,9 @@ class Packet {
return word1 * 0x100000000 + word0;
}
res = new Long(word0, word1, !signed); // Long need unsigned
const resNumber = res.toNumber();
const resString = res.toString();
res = resNumber.toString() === resString ? resNumber : resString;
return bigNumberStrings ? resString : res;
const safeResNumberPreferred = res.greaterThan(Number.MAX_SAFE_INTEGER) || res.lessThan(Number.MIN_SAFE_INTEGER) ? resString : res.toNumber();
return bigNumberStrings ? resString : safeResNumberPreferred;
}
// eslint-disable-next-line no-console
console.trace();
Expand Down Expand Up @@ -425,7 +424,7 @@ class Packet {
return StringParser.decode(
this.buffer,
encoding,
this.offset - len,
this.offset - len,
this.offset
);
}
Expand All @@ -450,21 +449,18 @@ class Packet {
this.offset++;
sign = -1;
}
// max precise int is 9007199254740992
// max precise int is Number.MAX_SAFE_INTEGER 9007199254740991
let str;
const numDigits = end - this.offset;
if (supportBigNumbers) {
if (numDigits >= 15) {
str = this.readString(end - this.offset, 'binary');
result = parseInt(str, 10);
if (result.toString() === str) {
return sign * result;
}
return sign === -1 ? `-${str}` : str;
}
if (numDigits > 16) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead code?

str = this.readString(end - this.offset);
return sign === -1 ? `-${str}` : str;
str = sign === -1 ? `-${str}` : str;
result = Long.fromString(str, false, 10);
if (result.greaterThan(Number.MAX_SAFE_INTEGER) || result.lessThan(Number.MIN_SAFE_INTEGER)) {
return result.toString();
}
return result.toNumber();
}
}
if (this.buffer[this.offset] === plus) {
Expand Down
16 changes: 8 additions & 8 deletions test/integration/connection/test-insert-bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ connection.query("INSERT INTO bigs SET title='test1'", (err, result) => {
connection.query("INSERT INTO bigs SET title='test2'", (err, result) => {
assert.strictEqual(result.insertId, 123456790);
// big int
connection.query("INSERT INTO bigs SET title='test', id=9007199254740992");
connection.query("INSERT INTO bigs SET title='test', id=9007199254740991");
connection.query("INSERT INTO bigs SET title='test3'", (err, result) => {
assert.strictEqual(
Long.fromString('9007199254740993').compare(result.insertId),
Long.fromString('9007199254740992').compare(result.insertId),
0
);
connection.query(
"INSERT INTO bigs SET title='test', id=90071992547409924"
"INSERT INTO bigs SET title='test', id=90071992547409923"
);
connection.query("INSERT INTO bigs SET title='test4'", (err, result) => {
assert.strictEqual(
Long.fromString('90071992547409925').compare(result.insertId),
Long.fromString('90071992547409924').compare(result.insertId),
0
);
connection.query(
Expand All @@ -51,10 +51,10 @@ connection.query("INSERT INTO bigs SET title='test1'", (err, result) => {
assert.strictEqual(result[1].id, 124);
assert.strictEqual(result[2].id, 123456789);
assert.strictEqual(result[3].id, 123456790);
assert.strictEqual(result[4].id, 9007199254740992);
assert.strictEqual(result[5].id, '9007199254740993');
assert.strictEqual(result[6].id, '90071992547409924');
assert.strictEqual(result[7].id, '90071992547409925');
assert.strictEqual(result[4].id, 9007199254740991);
assert.strictEqual(result[5].id, '9007199254740992');
assert.strictEqual(result[6].id, '90071992547409923');
assert.strictEqual(result[7].id, '90071992547409924');
connection.end();
}
);
Expand Down
Loading