-
Notifications
You must be signed in to change notification settings - Fork 3
feat: dimension validator #118
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e834a7e
add: dimension validator
vpbs2 7ae7753
some cleanup
vpbs2 2c6b08a
update: structure
vpbs2 ec62ba1
add: tests
vpbs2 0108671
remove: testing
vpbs2 442a614
update: test
vpbs2 3fbee46
add: more test
vpbs2 53fcbb2
fix: build
vpbs2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './dimension'; | ||
export * from './query-to-ast'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.