Skip to content

Commit 4ffd909

Browse files
authored
fix(FIR-34534): Properly parsing bigint (#111)
1 parent 27248a5 commit 4ffd909

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

src/statement/hydrateResponse.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ const getHydratedValue = (
4646
) {
4747
return value.toString();
4848
}
49+
if (typeof value === "string" && type === "long") {
50+
return new BigNumber(value);
51+
}
4952
return value;
5053
}
5154
if (isByteAType(type) && value != null) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { Firebolt } from "../../../src/index";
2+
import BigNumber from "bignumber.js";
3+
4+
const connectionParams = {
5+
auth: {
6+
client_id: process.env.FIREBOLT_CLIENT_ID as string,
7+
client_secret: process.env.FIREBOLT_CLIENT_SECRET as string
8+
},
9+
account: process.env.FIREBOLT_ACCOUNT as string,
10+
database: process.env.FIREBOLT_DATABASE as string
11+
};
12+
13+
jest.setTimeout(100000);
14+
15+
describe("test type casting on fetch", () => {
16+
it("select boolean", async () => {
17+
const firebolt = Firebolt({
18+
apiEndpoint: process.env.FIREBOLT_API_ENDPOINT as string
19+
});
20+
21+
const connection = await firebolt.connect(connectionParams);
22+
23+
const statement = await connection.execute("select true::boolean");
24+
25+
const { data, meta } = await statement.fetchResult();
26+
expect(meta[0].type).toEqual("boolean");
27+
const row = data[0];
28+
expect((row as unknown[])[0]).toEqual(true);
29+
});
30+
it("select bigint", async () => {
31+
const firebolt = Firebolt({
32+
apiEndpoint: process.env.FIREBOLT_API_ENDPOINT as string
33+
});
34+
35+
const connection = await firebolt.connect(connectionParams);
36+
37+
// Max value for a signed 64-bit integer (bigint)
38+
const statement = await connection.execute("select 9223372036854775807");
39+
40+
const { data, meta } = await statement.fetchResult();
41+
expect(meta[0].type).toEqual("long");
42+
const row = data[0];
43+
expect((row as unknown[])[0]).toEqual(new BigNumber("9223372036854775807"));
44+
});
45+
it("select negative bigint", async () => {
46+
const firebolt = Firebolt({
47+
apiEndpoint: process.env.FIREBOLT_API_ENDPOINT as string
48+
});
49+
50+
const connection = await firebolt.connect(connectionParams);
51+
52+
// Max negative value for a signed 64-bit integer (bigint)
53+
const statement = await connection.execute("select -9223372036854775808");
54+
55+
const { data, meta } = await statement.fetchResult();
56+
expect(meta[0].type).toEqual("long");
57+
const row = data[0];
58+
expect((row as unknown[])[0]).toEqual(
59+
new BigNumber("-9223372036854775808")
60+
);
61+
});
62+
});

test/unit/statement.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import {
77
Tuple
88
} from "../../src/formatter";
99
import { hydrateRow } from "../../src/statement/hydrateResponse";
10+
import "allure-jest";
1011

11-
describe("format query", () => {
12+
describe("query formatting", () => {
1213
it("format", () => {
1314
const queryFormatter = new QueryFormatter();
1415
const query = "select ? from table";
@@ -298,6 +299,20 @@ describe("parse values", () => {
298299
expect(isNaN(res["pnan"])).toBe(true);
299300
expect(isNaN(res["nnan"])).toBe(true);
300301
});
302+
it("parses bigint into BigNumber container", () => {
303+
allure.description(
304+
"JS natively doesn't support bigint so we use BigNumber container"
305+
);
306+
const row = {
307+
big: "1000000000000000000000000000000000000"
308+
};
309+
const meta = [{ name: "big", type: "long" }];
310+
const res: Record<string, BigNumber> = hydrateRow(row, meta, {});
311+
expect(res["big"] instanceof BigNumber).toBe(true);
312+
expect(res["big"]).toEqual(
313+
new BigNumber(1000000000000000000000000000000000000)
314+
);
315+
});
301316
});
302317

303318
describe("set statements", () => {

0 commit comments

Comments
 (0)