Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dimension validator #118

Merged
merged 8 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions meerkat-browser/src/ast/dimension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { validateDimension } from '@devrev/meerkat-core';
import { AsyncDuckDBConnection } from '@duckdb/duckdb-wasm';
import { parseQueryToAST } from './query-to-ast';
import { getAvailableFunctions, isParseError } from './utils';

/**
* Validates the query can be used as a dimension by parsing it to an AST and checking its structure
* @param connection - DuckDB connection instance
* @param query - The query string to validate
* @returns Promise<boolean> - Whether the dimension is valid
*/
export const validateDimensionQuery = async ({
connection,
query,
validFunctions,
}: {
connection: AsyncDuckDBConnection;
query: string;
validFunctions?: string[];
}): Promise<boolean> => {
const parsedSerialization = await parseQueryToAST(query, connection);

if (isParseError(parsedSerialization)) {
throw new Error(parsedSerialization.error_message ?? 'Unknown error');
}

// Only fetch valid functions if not provided
const availableFunctions =
validFunctions ?? (await getAvailableFunctions(connection, 'scalar'));

return validateDimension(parsedSerialization, availableFunctions);
};
2 changes: 2 additions & 0 deletions meerkat-browser/src/ast/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './dimension';
export * from './query-to-ast';
29 changes: 29 additions & 0 deletions meerkat-browser/src/ast/query-to-ast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
astSerializerQuery,
deserializeQuery,
ParsedSerialization,
} from '@devrev/meerkat-core';
import { AsyncDuckDBConnection } from '@duckdb/duckdb-wasm';

/**
* Converts a query to an AST
* @param query - The query string to convert
* @param connection - The DuckDB connection instance
* @returns The AST as a JSON object
*/
export const parseQueryToAST = async (
query: string,
connection: AsyncDuckDBConnection
): Promise<ParsedSerialization> => {
try {
const serializedQuery = astSerializerQuery(query);
const arrowResult = await connection.query(serializedQuery);

const parsedOutputQuery = arrowResult.toArray().map((row) => row.toJSON());
const deserializedQuery = deserializeQuery(parsedOutputQuery);

return JSON.parse(deserializedQuery);
} catch (error) {
throw new Error('Failed to parse query to AST');
}
};
vpbs2 marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 18 additions & 0 deletions meerkat-browser/src/ast/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ParsedSerialization } from '@devrev/meerkat-core';
import { AsyncDuckDBConnection } from '@duckdb/duckdb-wasm';

export const isParseError = (data: ParsedSerialization): boolean => {
return !!data.error;
};

// Helper function to get available functions from DuckDB based on function type
export const getAvailableFunctions = async (
connection: AsyncDuckDBConnection,
function_type: 'scalar' | 'aggregate'
): Promise<string[]> => {
const result = await connection.query(
`SELECT distinct function_name FROM duckdb_functions() WHERE function_type = '${function_type}'`
);

return result.toArray().map((row) => row.toJSON().function_name);
vpbs2 marked this conversation as resolved.
Show resolved Hide resolved
};
1 change: 1 addition & 0 deletions meerkat-browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './browser-cube-to-sql/browser-cube-to-sql';
export { convertCubeStringToTableSchema };
import { convertCubeStringToTableSchema } from '@devrev/meerkat-core';
export { validateDimensionQuery } from './ast';
3 changes: 3 additions & 0 deletions meerkat-core/src/ast-serializer/ast-serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const astSerializerQuery = (query: string) => {
return `SELECT json_serialize_sql('${query}')`;
};
vpbs2 marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading