Skip to content

Commit 17570d4

Browse files
UsmanYasinUsman Yasin
andauthored
fix(client-core): Fix for the issue with Generated SQL tab in playground (#9675)
* fix: Suggested fix for the issue with Generated SQL tab in playground * fix: Revised fixed to keep the same semantics * fix: correct SqlQuery types and add unit tests --------- Co-authored-by: Usman Yasin <usman.yasin@rystadenergy.com>
1 parent 3f8c3e6 commit 17570d4

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

packages/cubejs-client-core/src/SqlQuery.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
export type SqlQueryTuple = [string, any[], any];
1+
export type SqlQueryTuple = [string, any[], any?];
22

33
export type SqlData = {
44
aliasNameToMember: Record<string, string>;
55
cacheKeyQueries: SqlQueryTuple[];
6-
dataSource: boolean;
6+
dataSource: string;
77
external: boolean;
88
sql: SqlQueryTuple;
99
preAggregations: any[];
1010
rollupMatchResults: any[];
1111
};
1212

13+
export type SqlQueryWrapper = { sql: SqlData };
14+
1315
export default class SqlQuery {
14-
private readonly sqlQuery: SqlData;
16+
private readonly sqlQuery: SqlQueryWrapper;
1517

16-
public constructor(sqlQuery: SqlData) {
18+
public constructor(sqlQuery: SqlQueryWrapper) {
1719
this.sqlQuery = sqlQuery;
1820
}
1921

2022
public rawQuery(): SqlData {
21-
return this.sqlQuery;
23+
return this.sqlQuery.sql;
2224
}
2325

2426
public sql(): string {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @license Apache-2.0
3+
* @copyright Cube Dev, Inc.
4+
* @fileoverview SqlQuery class unit tests.
5+
*/
6+
7+
/* globals describe,it,expect */
8+
9+
import SqlQuery, { SqlQueryTuple, SqlData, SqlQueryWrapper } from '../src/SqlQuery';
10+
11+
describe('SqlQuery', () => {
12+
const mockCacheKeyQueriesTuple: SqlQueryTuple = [
13+
'SELECT FLOOR((-25200 + EXTRACT(EPOCH FROM NOW())) / 600) as refresh_key',
14+
[],
15+
{
16+
external: false,
17+
renewalThreshold: 60
18+
}
19+
];
20+
21+
const mockSqlTuple: SqlQueryTuple = [
22+
'SELECT count(*) "base_orders__count" FROM base_orders WHERE base_orders.continent = ?',
23+
['Europe'],
24+
];
25+
26+
const mockSqlData: SqlData = {
27+
aliasNameToMember: { base_orders__count: 'base_orders.count' },
28+
cacheKeyQueries: [mockCacheKeyQueriesTuple],
29+
dataSource: 'default',
30+
external: false,
31+
sql: mockSqlTuple,
32+
preAggregations: [],
33+
rollupMatchResults: [],
34+
};
35+
36+
const mockWrapper: SqlQueryWrapper = {
37+
sql: mockSqlData,
38+
};
39+
40+
it('should construct without error', () => {
41+
expect(() => new SqlQuery(mockWrapper)).not.toThrow();
42+
});
43+
44+
it('rawQuery should return the original SqlData', () => {
45+
const query = new SqlQuery(mockWrapper);
46+
expect(query.rawQuery()).toEqual(mockSqlData);
47+
});
48+
49+
it('sql should return the first element (SQL string) from the sql tuple', () => {
50+
const query = new SqlQuery(mockWrapper);
51+
expect(query.sql()).toBe('SELECT count(*) "base_orders__count" FROM base_orders WHERE base_orders.continent = ?');
52+
});
53+
});

0 commit comments

Comments
 (0)