Skip to content

Commit

Permalink
feat(query-orchestrator): detects bigint in readOnly mode, when it's …
Browse files Browse the repository at this point in the history
…Number
  • Loading branch information
ovr committed Dec 17, 2020
1 parent d9af942 commit a21cc10
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/cubejs-query-orchestrator/src/driver/BaseDriver.js
Expand Up @@ -30,10 +30,14 @@ const DbTypeToGenericType = {
'double precision': 'decimal'
};

const DB_INT_MAX = 2147483647;
const DB_INT_MIN = -2147483648;

// Order of keys is important here: from more specific to less specific
const DbTypeValueMatcher = {
timestamp: (v) => v instanceof Date || v.toString().match(/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d/),
date: (v) => v instanceof Date || v.toString().match(/^\d\d\d\d-\d\d-\d\d$/),
bigint: (v) => Number.isInteger(v) && (v > DB_INT_MAX || v < DB_INT_MIN),
int: (v) => Number.isInteger(v) || v.toString().match(/^\d+$/),
decimal: (v) => v instanceof Number || v.toString().match(/^\d+(\.\d+)?$/),
boolean: (v) => v === false || v === true || v.toString().toLowerCase() === 'true' || v.toString().toLowerCase() === 'false',
Expand Down
31 changes: 31 additions & 0 deletions packages/cubejs-query-orchestrator/test/unit/BaseDriver.test.ts
@@ -0,0 +1,31 @@
import { BaseDriver } from '../../src';

class BaseDriverImplementedMock extends BaseDriver {
public constructor(protected readonly response: any) {
super();
}

public async query(query, values) {
return this.response;
}
}

describe('BaseDriver', () => {
test('downloadQueryResults - test type detection', async () => {
const rows = [{
bigint: 21474836479,
int: 1,
intstr: '1',
string: 'str',
}];

const driver = new BaseDriverImplementedMock(rows);

expect((await driver.downloadQueryResults()).types).toEqual([
{ name: 'bigint', type: 'bigint' },
{ name: 'int', type: 'int' },
{ name: 'intstr', type: 'int' },
{ name: 'string', type: 'string' }
]);
});
});

0 comments on commit a21cc10

Please sign in to comment.