Skip to content
Merged
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
37 changes: 35 additions & 2 deletions src/statement/stream/jsonStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ export class JSONStream {
executeQueryOptions: ExecuteQueryOptions;
emitter: RowStream;
rowParser: RowParser;
state: "meta" | "meta-array" | "rootKeys" | "data" | "data-array" | null;
state:
| "meta"
| "meta-array"
| "rootKeys"
| "data"
| "data-array"
| "query"
| "query-object"
| null;

columns: Meta[];
rows: unknown[];
Expand Down Expand Up @@ -66,7 +74,11 @@ export class JSONStream {
}

handleRootKeys(line: string) {
if (line === '"meta":') {
if (line === "query") {
this.state = "query";
} else if (line === '"query": {') {
this.state = "query-object";
} else if (line === '"meta":') {
this.state = "meta";
} else if (line === '"data":') {
this.state = "data";
Expand Down Expand Up @@ -128,6 +140,23 @@ export class JSONStream {
}
}

handleQuery(line: string) {
if (line === "{") {
this.state = "query-object";
}
}

handleQueryObject(line: string) {
if (line.match(/^},?$/)) {
const queryStr = this.objBuffer + "}";
const query = JSONbig.parse(queryStr);
this.objBuffer = undefined;
this.state = "rootKeys";
} else {
this.objBuffer += line;
}
}

processLine(line: string) {
line = line.trim();

Expand All @@ -147,6 +176,10 @@ export class JSONStream {
this.handleMetaArray(line);
} else if (this.state === "data-array") {
this.handleDataArray(line);
} else if (this.state === "query") {
this.handleQuery(line);
} else if (this.state === "query-object") {
this.handleQueryObject(line);
}
}
}
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export type QueryResponse = {

export enum OutputFormat {
// JSON_COMPACT_LIMITED = "FB_JSONCompactLimited",
JSON_COMPACT = "JSONCompact",
COMPACT = "JSON_Compact", // supported in v3 of packdb
JSON_COMPACT = "JSONCompact", // to be as above, after ensure all clients has v3
JSON = "JSON"
}

Expand Down
37 changes: 37 additions & 0 deletions test/integration/outputFormat.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Firebolt } from "../../src/index";
import { OutputFormat } from "../../src/types";

const connectionParams = {
username: process.env.FIREBOLT_USERNAME as string,
password: process.env.FIREBOLT_PASSWORD as string,
database: process.env.FIREBOLT_DATABASE as string,
engineName: process.env.FIREBOLT_ENGINE_NAME as string
};

jest.setTimeout(50000);

describe("integration test", () => {
it("works", async () => {
const firebolt = Firebolt({
apiEndpoint: process.env.FIREBOLT_API_ENDPOINT as string
});

const connection = await firebolt.connect(connectionParams);

const statement = await connection.execute(
"select 1, 'a', 123.4, 9223372036854775806, [1, 2, 4]",
{
settings: { output_format: OutputFormat.COMPACT }
}
);
const { data, meta } = await statement.fetchResult();
expect(data.length).toEqual(1);
expect(meta.length).toEqual(5);
const [int_type, text_type, double_type, long_type, array_type] = meta;
expect(int_type.type).toEqual("int");
expect(text_type.type).toEqual("text");
expect(double_type.type).toEqual("double");
expect(long_type.type).toEqual("long");
expect(array_type.type).toEqual("array(int)");
});
});