Skip to content

Commit 5958790

Browse files
authored
add sql generation with resolution (#143)
* add sql generation with resolution * revert * better * review comments * refactor * bump package version * fix build * lint * lint --------- Co-authored-by: Eric Gan <[email protected]>
1 parent 9944ebf commit 5958790

File tree

12 files changed

+958
-16
lines changed

12 files changed

+958
-16
lines changed

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.94",
3+
"version": "0.0.95",
44
"dependencies": {
55
"@swc/helpers": "~0.5.0",
66
"@devrev/meerkat-core": "*",
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {
2+
ContextParams,
3+
createBaseTableSchema,
4+
generateResolutionJoinPaths,
5+
generateResolutionSchemas,
6+
generateResolvedDimensions,
7+
Query,
8+
ResolutionConfig,
9+
TableSchema,
10+
} from '@devrev/meerkat-core';
11+
import { AsyncDuckDBConnection } from '@duckdb/duckdb-wasm';
12+
import {
13+
cubeQueryToSQL,
14+
CubeQueryToSQLParams,
15+
} from '../browser-cube-to-sql/browser-cube-to-sql';
16+
17+
export interface CubeQueryToSQLWithResolutionParams {
18+
connection: AsyncDuckDBConnection;
19+
query: Query;
20+
tableSchemas: TableSchema[];
21+
resolutionConfig: ResolutionConfig;
22+
contextParams?: ContextParams;
23+
}
24+
25+
export const cubeQueryToSQLWithResolution = async ({
26+
connection,
27+
query,
28+
tableSchemas,
29+
resolutionConfig,
30+
contextParams,
31+
}: CubeQueryToSQLWithResolutionParams) => {
32+
const baseSql = await cubeQueryToSQL({
33+
connection,
34+
query,
35+
tableSchemas,
36+
contextParams,
37+
});
38+
39+
if (resolutionConfig.columnConfigs.length === 0) {
40+
// If no resolution is needed, return the base SQL.
41+
return baseSql;
42+
}
43+
44+
// Create a table schema for the base query.
45+
const baseTable: TableSchema = createBaseTableSchema(
46+
baseSql,
47+
tableSchemas,
48+
resolutionConfig,
49+
query.measures,
50+
query.dimensions
51+
);
52+
53+
const resolutionSchemas: TableSchema[] =
54+
generateResolutionSchemas(resolutionConfig);
55+
56+
const resolveParams: CubeQueryToSQLParams = {
57+
connection: connection,
58+
query: {
59+
measures: [],
60+
dimensions: generateResolvedDimensions(query, resolutionConfig),
61+
joinPaths: generateResolutionJoinPaths(resolutionConfig),
62+
},
63+
tableSchemas: [baseTable, ...resolutionSchemas],
64+
};
65+
const sql = await cubeQueryToSQL(resolveParams);
66+
67+
return sql;
68+
};

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,38 @@ import {
1111
detectApplyContextParamsToBaseSQL,
1212
getCombinedTableSchema,
1313
getFilterParamsSQL,
14-
getFinalBaseSQL
14+
getFinalBaseSQL,
1515
} from '@devrev/meerkat-core';
1616
import { AsyncDuckDBConnection } from '@duckdb/duckdb-wasm';
1717

18-
19-
const getQueryOutput = async (query: string, connection: AsyncDuckDBConnection) => {
18+
const getQueryOutput = async (
19+
query: string,
20+
connection: AsyncDuckDBConnection
21+
) => {
2022
const queryOutput = await connection.query(query);
2123
const parsedOutputQuery = queryOutput.toArray().map((row) => row.toJSON());
2224
return parsedOutputQuery;
23-
}
24-
25+
};
2526

26-
interface CubeQueryToSQLParams {
27-
connection: AsyncDuckDBConnection,
28-
query: Query,
29-
tableSchemas: TableSchema[],
30-
contextParams?: ContextParams,
27+
export interface CubeQueryToSQLParams {
28+
connection: AsyncDuckDBConnection;
29+
query: Query;
30+
tableSchemas: TableSchema[];
31+
contextParams?: ContextParams;
3132
}
3233

3334
export const cubeQueryToSQL = async ({
3435
connection,
3536
query,
3637
tableSchemas,
37-
contextParams
38+
contextParams,
3839
}: CubeQueryToSQLParams) => {
3940
const updatedTableSchemas: TableSchema[] = await Promise.all(
4041
tableSchemas.map(async (schema: TableSchema) => {
4142
const baseFilterParamsSQL = await getFinalBaseSQL({
4243
query,
4344
tableSchema: schema,
44-
getQueryOutput: (query) => getQueryOutput(query, connection)
45+
getQueryOutput: (query) => getQueryOutput(query, connection),
4546
});
4647
return {
4748
...schema,

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.94",
3+
"version": "0.0.95",
44
"dependencies": {
55
"@swc/helpers": "~0.5.0"
66
},

meerkat-core/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export { getFinalBaseSQL } from './get-final-base-sql/get-final-base-sql';
1515
export { getWrappedBaseQueryWithProjections } from './get-wrapped-base-query-with-projections/get-wrapped-base-query-with-projections';
1616
export * from './joins/joins';
1717
export * from './member-formatters';
18+
export * from './resolution/resolution';
19+
export * from './resolution/types';
1820
export { FilterType } from './types/cube-types';
1921
export * from './types/cube-types/index';
2022
export * from './types/duckdb-serialization-types/index';

0 commit comments

Comments
 (0)