Skip to content

Commit 61708d0

Browse files
Chore: Updated CubeToSql interface (#114)
* updated interface * bumped version * test failure fix
1 parent 4fc61c1 commit 61708d0

17 files changed

+100
-87
lines changed

examples/meerkat-node-example/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ app.post('/api-v1', async (req, res) => {
2323
const { cube, table_schema } = req.body;
2424
// const query = await cubeQueryToSQL(sql, cube);
2525

26-
const data = await cubeQueryToSQL(cube, table_schema);
26+
const data = await cubeQueryToSQL({ query: cube, tableSchemas: table_schema });
2727

2828
res.json({ data });
2929
});

meerkat-browser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devrev/meerkat-browser",
3-
"version": "0.0.82",
3+
"version": "0.0.83",
44
"dependencies": {
55
"@swc/helpers": "~0.5.0",
66
"@devrev/meerkat-core": "*",

meerkat-browser/src/browser-cube-to-sql/browser-cube-to-sql.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ import {
1717
import { AsyncDuckDBConnection } from '@duckdb/duckdb-wasm';
1818

1919
const getFilterParamsSQL = async ({
20-
cubeQuery,
20+
query,
2121
tableSchema,
2222
filterType,
2323
connection,
2424
}: {
25-
cubeQuery: Query;
25+
query: Query;
2626
tableSchema: TableSchema;
2727
filterType?: FilterType;
2828
connection: AsyncDuckDBConnection;
2929
}) => {
3030
const filterParamsAST = getFilterParamsAST(
31-
cubeQuery,
31+
query,
3232
tableSchema,
3333
filterType
3434
);
@@ -56,7 +56,7 @@ const getFilterParamsSQL = async ({
5656
};
5757

5858
const getFinalBaseSQL = async (
59-
cubeQuery: Query,
59+
query: Query,
6060
tableSchema: TableSchema,
6161
connection: AsyncDuckDBConnection
6262
) => {
@@ -65,7 +65,7 @@ const getFinalBaseSQL = async (
6565
* This involves updating the filter placeholder with the actual filter values.
6666
*/
6767
const baseFilterParamsSQL = await getFilterParamsSQL({
68-
cubeQuery: cubeQuery,
68+
query: query,
6969
tableSchema,
7070
filterType: 'BASE_FILTER',
7171
connection,
@@ -77,21 +77,26 @@ const getFinalBaseSQL = async (
7777
const baseSQLWithFilterProjection = getWrappedBaseQueryWithProjections({
7878
baseQuery: baseSQL,
7979
tableSchema,
80-
query: cubeQuery,
80+
query: query,
8181
});
8282
return baseSQLWithFilterProjection;
8383
};
8484

85-
export const cubeQueryToSQL = async (
85+
export const cubeQueryToSQL = async ({
86+
connection,
87+
query,
88+
tableSchemas,
89+
contextParams
90+
}: {
8691
connection: AsyncDuckDBConnection,
87-
cubeQuery: Query,
92+
query: Query,
8893
tableSchemas: TableSchema[],
8994
contextParams?: ContextParams
90-
) => {
95+
}) => {
9196
const updatedTableSchemas: TableSchema[] = await Promise.all(
9297
tableSchemas.map(async (schema: TableSchema) => {
9398
const baseFilterParamsSQL = await getFinalBaseSQL(
94-
cubeQuery,
99+
query,
95100
schema,
96101
connection
97102
);
@@ -104,10 +109,10 @@ export const cubeQueryToSQL = async (
104109

105110
const updatedTableSchema = await getCombinedTableSchema(
106111
updatedTableSchemas,
107-
cubeQuery
112+
query
108113
);
109114

110-
const ast = cubeToDuckdbAST(cubeQuery, updatedTableSchema);
115+
const ast = cubeToDuckdbAST(query, updatedTableSchema);
111116
if (!ast) {
112117
throw new Error('Could not generate AST');
113118
}
@@ -120,7 +125,7 @@ export const cubeQueryToSQL = async (
120125
const preBaseQuery = deserializeQuery(parsedOutputQuery);
121126
const filterParamsSQL = await getFilterParamsSQL({
122127
connection,
123-
cubeQuery,
128+
query,
124129
tableSchema: updatedTableSchema,
125130
filterType: 'BASE_FILTER',
126131
});
@@ -149,8 +154,8 @@ export const cubeQueryToSQL = async (
149154
/**
150155
* Add measures to the query
151156
*/
152-
const measures = cubeQuery.measures;
153-
const dimensions = cubeQuery.dimensions || [];
157+
const measures = query.measures;
158+
const dimensions = query.dimensions || [];
154159
const finalQuery = applyProjectionToSQLQuery(
155160
dimensions,
156161
measures,

meerkat-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devrev/meerkat-core",
3-
"version": "0.0.82",
3+
"version": "0.0.83",
44
"dependencies": {
55
"@swc/helpers": "~0.5.0"
66
},

meerkat-node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devrev/meerkat-node",
3-
"version": "0.0.82",
3+
"version": "0.0.83",
44
"dependencies": {
55
"@swc/helpers": "~0.5.0",
66
"@devrev/meerkat-core": "*",

meerkat-node/src/__tests__/cube-array-type.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('cube-to-sql', () => {
6868
],
6969
dimensions: [],
7070
};
71-
const sql = await cubeQueryToSQL(query, [SCHEMA]);
71+
const sql = await cubeQueryToSQL({ query, tableSchemas: [SCHEMA] });
7272
console.info('SQL: ', sql);
7373
const output: any = await duckdbExec(sql);
7474
expect(output).toHaveLength(1);
@@ -87,7 +87,7 @@ describe('cube-to-sql', () => {
8787
],
8888
dimensions: [],
8989
};
90-
const sql = await cubeQueryToSQL(query, [SCHEMA]);
90+
const sql = await cubeQueryToSQL({ query, tableSchemas: [SCHEMA] });
9191
expect(sql).toBe(
9292
`SELECT person.* FROM (SELECT *, activities AS person__activities FROM (select * from person) AS person) AS person WHERE ('Hiking' = ANY(SELECT unnest(person__activities)))`
9393
);
@@ -117,7 +117,7 @@ describe('cube-to-sql', () => {
117117
],
118118
dimensions: [],
119119
};
120-
const sql = await cubeQueryToSQL(query, [SCHEMA]);
120+
const sql = await cubeQueryToSQL({ query, tableSchemas: [SCHEMA] });
121121
expect(sql).toBe(
122122
`SELECT person.* FROM (SELECT *, activities AS person__activities, id AS person__id FROM (select * from person) AS person) AS person WHERE (('Running' = ANY(SELECT unnest(person__activities))) AND (person__id = '2'))`
123123
);
@@ -138,7 +138,7 @@ describe('cube-to-sql', () => {
138138
],
139139
dimensions: [],
140140
};
141-
const sql = await cubeQueryToSQL(query, [SCHEMA]);
141+
const sql = await cubeQueryToSQL({ query, tableSchemas: [SCHEMA] });
142142
console.info('SQL: ', sql);
143143
expect(sql).toBe(
144144
`SELECT person.* FROM (SELECT *, activities AS person__activities FROM (select * from person) AS person) AS person WHERE (NOT ('Running' = ANY(SELECT unnest(person__activities))))`

meerkat-node/src/__tests__/cube-context-params.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ describe('context-param-tests', () => {
5757
],
5858
dimensions: [],
5959
};
60-
const sql = await cubeQueryToSQL(query, [SCHEMA], {
60+
const sql = await cubeQueryToSQL({ query, tableSchemas: [SCHEMA], contextParams: {
6161
TABLE_NAME: 'orders',
62-
});
62+
}});
6363
console.info('SQL: ', sql);
6464
const output: any = await duckdbExec(sql);
6565
expect(output).toHaveLength(1);

meerkat-node/src/__tests__/cube-filter-params.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('filter-param-tests', () => {
5858
],
5959
dimensions: [],
6060
};
61-
const sql = await cubeQueryToSQL(query, [SCHEMA]);
61+
const sql = await cubeQueryToSQL({query, tableSchemas: [SCHEMA]});
6262
console.info('SQL: ', sql);
6363
const output: any = await duckdbExec(sql);
6464
expect(output).toHaveLength(2);
@@ -91,7 +91,7 @@ describe('filter-param-tests', () => {
9191
],
9292
dimensions: [],
9393
};
94-
const sql = await cubeQueryToSQL(query, [SCHEMA]);
94+
const sql = await cubeQueryToSQL({ query, tableSchemas: [SCHEMA] });
9595
console.info('SQL: ', sql);
9696
const output: any = await duckdbExec(sql);
9797
expect(output).toHaveLength(3);
@@ -105,7 +105,7 @@ describe('filter-param-tests', () => {
105105
dimensions: [],
106106
};
107107

108-
const sql = await cubeQueryToSQL(query, [SCHEMA]);
108+
const sql = await cubeQueryToSQL({ query, tableSchemas: [SCHEMA] });
109109
console.info('SQL: ', sql);
110110
const output: any = await duckdbExec(sql);
111111
expect(output).toHaveLength(8);
@@ -137,7 +137,7 @@ describe('filter-param-tests', () => {
137137
dimensions: [],
138138
};
139139

140-
const sql = await cubeQueryToSQL(query, [SCHEMA]);
140+
const sql = await cubeQueryToSQL({ query, tableSchemas: [SCHEMA] });
141141
console.info('SQL: ', sql);
142142
const output: any = await duckdbExec(sql);
143143
expect(output).toHaveLength(5);
@@ -162,7 +162,7 @@ describe('filter-param-tests', () => {
162162
dimensions: [],
163163
};
164164

165-
const sql = await cubeQueryToSQL(query, [SCHEMA]);
165+
const sql = await cubeQueryToSQL({ query, tableSchemas: [SCHEMA] });
166166
console.info('SQL: ', sql);
167167
const output: any = await duckdbExec(sql);
168168
expect(output).toHaveLength(1);

meerkat-node/src/__tests__/cube-projection-filters.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('cube-to-sql', () => {
7474
});
7575

7676
it('Should construct the SQL query and apply filter on projections', async () => {
77-
const sql = await cubeQueryToSQL(QUERY, [SCHEMA]);
77+
const sql = await cubeQueryToSQL({ query: QUERY, tableSchemas: [SCHEMA] });
7878
const expectedSQL = `SELECT COUNT(DISTINCT id) AS person__count_star , person__other_dimension FROM (SELECT *, CASE WHEN primary_part_id LIKE '%enhancement%' THEN 'yes' ELSE 'no' END AS person__ticket_prioritized, 'dashboard_others' AS person__other_dimension FROM (SELECT * FROM person) AS person) AS person WHERE ((person__ticket_prioritized != 'no')) GROUP BY person__other_dimension`;
7979
expect(sql).toBe(expectedSQL);
8080
console.info('SQL: ', sql);
@@ -86,8 +86,8 @@ describe('cube-to-sql', () => {
8686
});
8787

8888
it('Should be able to handle multi level filters', async () => {
89-
const sql = await cubeQueryToSQL(
90-
{
89+
const sql = await cubeQueryToSQL({
90+
query: {
9191
...QUERY,
9292
filters: [
9393
{
@@ -110,8 +110,8 @@ describe('cube-to-sql', () => {
110110
},
111111
],
112112
},
113-
[SCHEMA]
114-
);
113+
tableSchemas: [SCHEMA]
114+
});
115115
const expectedSQL = `SELECT COUNT(DISTINCT id) AS person__count_star , person__other_dimension FROM (SELECT *, id AS person__id, CASE WHEN primary_part_id LIKE '%enhancement%' THEN 'yes' ELSE 'no' END AS person__ticket_prioritized, 'dashboard_others' AS person__other_dimension FROM (SELECT * FROM person) AS person) AS person WHERE (((person__id != '1') OR (person__ticket_prioritized != 'no'))) GROUP BY person__other_dimension`;
116116
expect(sql).toBe(expectedSQL);
117117
console.info('SQL: ', sql);
@@ -123,8 +123,8 @@ describe('cube-to-sql', () => {
123123
});
124124

125125
it('Should be able to handle same projection on multiple levels in filter', async () => {
126-
const sql = await cubeQueryToSQL(
127-
{
126+
const sql = await cubeQueryToSQL({
127+
query: {
128128
...QUERY,
129129
filters: [
130130
{
@@ -152,8 +152,8 @@ describe('cube-to-sql', () => {
152152
},
153153
],
154154
},
155-
[SCHEMA]
156-
);
155+
tableSchemas: [SCHEMA]
156+
});
157157
const expectedSQL = `SELECT COUNT(DISTINCT id) AS person__count_star , person__other_dimension FROM (SELECT *, id AS person__id, CASE WHEN primary_part_id LIKE '%enhancement%' THEN 'yes' ELSE 'no' END AS person__ticket_prioritized, 'dashboard_others' AS person__other_dimension FROM (SELECT * FROM person) AS person) AS person WHERE ((person__id != '2') AND ((person__id != '1') OR (person__ticket_prioritized != 'no'))) GROUP BY person__other_dimension`;
158158
expect(sql).toBe(expectedSQL);
159159
console.info('SQL: ', sql);

meerkat-node/src/__tests__/cube-schema-to-sql.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('Cube Schema to SQL', () => {
6363
],
6464
],
6565
};
66-
const sql = await cubeQueryToSQL(query, [productsSchema, suppliersSchema]);
66+
const sql = await cubeQueryToSQL({ query, tableSchemas: [productsSchema, suppliersSchema] });
6767
console.info(`SQL for Simple Cube Query: `, sql);
6868
const output = await duckdbExec(sql);
6969
console.info('parsedOutput', output);

0 commit comments

Comments
 (0)