Skip to content

Commit 62e6a97

Browse files
authored
Merge pull request #22 from firebolt-db/new-data-format
JSON_COMPACT data format
2 parents cbf25ff + 94bebe0 commit 62e6a97

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

src/statement/stream/jsonStream.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ export class JSONStream {
1010
executeQueryOptions: ExecuteQueryOptions;
1111
emitter: RowStream;
1212
rowParser: RowParser;
13-
state: "meta" | "meta-array" | "rootKeys" | "data" | "data-array" | null;
13+
state:
14+
| "meta"
15+
| "meta-array"
16+
| "rootKeys"
17+
| "data"
18+
| "data-array"
19+
| "query"
20+
| "query-object"
21+
| null;
1422

1523
columns: Meta[];
1624
rows: unknown[];
@@ -66,7 +74,11 @@ export class JSONStream {
6674
}
6775

6876
handleRootKeys(line: string) {
69-
if (line === '"meta":') {
77+
if (line === "query") {
78+
this.state = "query";
79+
} else if (line === '"query": {') {
80+
this.state = "query-object";
81+
} else if (line === '"meta":') {
7082
this.state = "meta";
7183
} else if (line === '"data":') {
7284
this.state = "data";
@@ -128,6 +140,23 @@ export class JSONStream {
128140
}
129141
}
130142

143+
handleQuery(line: string) {
144+
if (line === "{") {
145+
this.state = "query-object";
146+
}
147+
}
148+
149+
handleQueryObject(line: string) {
150+
if (line.match(/^},?$/)) {
151+
const queryStr = this.objBuffer + "}";
152+
const query = JSONbig.parse(queryStr);
153+
this.objBuffer = undefined;
154+
this.state = "rootKeys";
155+
} else {
156+
this.objBuffer += line;
157+
}
158+
}
159+
131160
processLine(line: string) {
132161
line = line.trim();
133162

@@ -147,6 +176,10 @@ export class JSONStream {
147176
this.handleMetaArray(line);
148177
} else if (this.state === "data-array") {
149178
this.handleDataArray(line);
179+
} else if (this.state === "query") {
180+
this.handleQuery(line);
181+
} else if (this.state === "query-object") {
182+
this.handleQueryObject(line);
150183
}
151184
}
152185
}

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export type QueryResponse = {
2424

2525
export enum OutputFormat {
2626
// JSON_COMPACT_LIMITED = "FB_JSONCompactLimited",
27-
JSON_COMPACT = "JSONCompact",
27+
COMPACT = "JSON_Compact", // supported in v3 of packdb
28+
JSON_COMPACT = "JSONCompact", // to be as above, after ensure all clients has v3
2829
JSON = "JSON"
2930
}
3031

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Firebolt } from "../../src/index";
2+
import { OutputFormat } from "../../src/types";
3+
4+
const connectionParams = {
5+
username: process.env.FIREBOLT_USERNAME as string,
6+
password: process.env.FIREBOLT_PASSWORD as string,
7+
database: process.env.FIREBOLT_DATABASE as string,
8+
engineName: process.env.FIREBOLT_ENGINE_NAME as string
9+
};
10+
11+
jest.setTimeout(50000);
12+
13+
describe("integration test", () => {
14+
it("works", async () => {
15+
const firebolt = Firebolt({
16+
apiEndpoint: process.env.FIREBOLT_API_ENDPOINT as string
17+
});
18+
19+
const connection = await firebolt.connect(connectionParams);
20+
21+
const statement = await connection.execute(
22+
"select 1, 'a', 123.4, 9223372036854775806, [1, 2, 4]",
23+
{
24+
settings: { output_format: OutputFormat.COMPACT }
25+
}
26+
);
27+
const { data, meta } = await statement.fetchResult();
28+
expect(data.length).toEqual(1);
29+
expect(meta.length).toEqual(5);
30+
const [int_type, text_type, double_type, long_type, array_type] = meta;
31+
expect(int_type.type).toEqual("int");
32+
expect(text_type.type).toEqual("text");
33+
expect(double_type.type).toEqual("double");
34+
expect(long_type.type).toEqual("long");
35+
expect(array_type.type).toEqual("array(int)");
36+
});
37+
});

0 commit comments

Comments
 (0)