Skip to content

Commit e541360

Browse files
authored
feat: measure validator (#119)
* add: dimension validator * add: typegiards * update: test * more: test * update: package * fix: build * update: export * remove: log * resolve: comments * update: types * update: export * address: comments
1 parent 7dd5cf5 commit e541360

File tree

17 files changed

+2675
-478
lines changed

17 files changed

+2675
-478
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.84",
3+
"version": "0.0.85",
44
"dependencies": {
55
"@swc/helpers": "~0.5.0",
66
"@devrev/meerkat-core": "*",

meerkat-browser/src/ast/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './dimension';
2+
export * from './measure';
23
export * from './query-to-ast';

meerkat-browser/src/ast/measure.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { validateMeasure } from '@devrev/meerkat-core';
2+
import { AsyncDuckDBConnection } from '@duckdb/duckdb-wasm';
3+
import { parseQueryToAST } from './query-to-ast';
4+
import { getAvailableFunctions, isParseError } from './utils';
5+
6+
/**
7+
* Validates the query can be used as a measure by parsing it to an AST and checking its structure
8+
* @param connection - DuckDB connection instance
9+
* @param query - The query string to validate
10+
* @returns Promise<boolean> - Whether the measure is valid
11+
*/
12+
export const validateMeasureQuery = async ({
13+
connection,
14+
query,
15+
validFunctions,
16+
}: {
17+
connection: AsyncDuckDBConnection;
18+
query: string;
19+
validFunctions?: string[];
20+
}): Promise<boolean> => {
21+
const parsedSerialization = await parseQueryToAST(query, connection);
22+
23+
if (isParseError(parsedSerialization)) {
24+
throw new Error(parsedSerialization.error_message ?? 'Unknown error');
25+
}
26+
27+
// Only fetch valid functions if not provided
28+
const availableFunctions =
29+
validFunctions ?? (await getAvailableFunctions(connection, 'aggregate'));
30+
31+
const validScalarFunctions = await getAvailableFunctions(
32+
connection,
33+
'scalar'
34+
);
35+
36+
return validateMeasure(
37+
parsedSerialization,
38+
availableFunctions,
39+
validScalarFunctions
40+
);
41+
};

meerkat-browser/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
export * from './browser-cube-to-sql/browser-cube-to-sql';
22
export { convertCubeStringToTableSchema };
33
import { convertCubeStringToTableSchema } from '@devrev/meerkat-core';
4-
export { validateDimensionQuery } from './ast';
4+
export {
5+
parseQueryToAST,
6+
validateDimensionQuery,
7+
validateMeasureQuery,
8+
} from './ast';

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

0 commit comments

Comments
 (0)