-
Notifications
You must be signed in to change notification settings - Fork 17
Add CEL support for Data Quality rules #313
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
+569
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,87 @@ | ||
import {RuleRegistry} from "../../serde/rule-registry"; | ||
import {RuleContext, RuleExecutor} from "../../serde/serde"; | ||
import {ClientConfig} from "../../rest-service"; | ||
import stringify from "json-stringify-deterministic"; | ||
import {LRUCache} from "lru-cache"; | ||
import {createEnv} from "@bufbuild/cel"; | ||
import {createRegistry} from "@bufbuild/protobuf"; | ||
|
||
export class CelExecutor implements RuleExecutor { | ||
config: Map<string, string> | null = null | ||
env = createEnv("", createRegistry()); | ||
cache: LRUCache<string, any> = new LRUCache({max: 1000}) | ||
|
||
static register(): CelExecutor { | ||
const executor = new CelExecutor() | ||
RuleRegistry.registerRuleExecutor(executor) | ||
return executor | ||
} | ||
|
||
configure(clientConfig: ClientConfig, config: Map<string, string>) { | ||
this.config = config | ||
} | ||
|
||
type(): string { | ||
return "CEL" | ||
} | ||
|
||
async transform(ctx: RuleContext, msg: any): Promise<any> { | ||
const args = { | ||
message: msg | ||
} | ||
return await this.execute(ctx, msg, args) | ||
} | ||
|
||
async execute(ctx: RuleContext, msg: any, args: { [key: string]: any }): Promise<any> { | ||
let expr = ctx.rule.expr | ||
if (expr == null) { | ||
return msg | ||
} | ||
const index = expr.indexOf(';') | ||
if (index >= 0) { | ||
const guard = expr.substring(0, index) | ||
if (guard.trim().length != 0) { | ||
const guardResult = await this.executeRule(ctx, guard, msg, args) | ||
if (guardResult === false) { | ||
// skip the expr | ||
if (ctx.rule.kind === 'CONDITION') { | ||
return true | ||
} | ||
return msg | ||
} | ||
} | ||
expr = expr.substring(index + 1) | ||
} | ||
return await this.executeRule(ctx, expr, msg, args) | ||
} | ||
|
||
async executeRule(ctx: RuleContext, expr: string, obj: any, args: { [key: string]: any }): Promise<any> { | ||
const schema = ctx.target.schema | ||
const scriptType = ctx.target.schemaType | ||
const rule: RuleWithArgs = { | ||
rule: expr, | ||
scriptType: scriptType, | ||
schema: schema | ||
} | ||
const ruleJson = stringify(rule) | ||
let program = this.cache.get(ruleJson) | ||
if (program == null) { | ||
const parsedExpr = this.env.parse(expr) | ||
program = this.env.plan(parsedExpr) | ||
this.cache.set(ruleJson, program) | ||
} | ||
for (const [key, value] of Object.entries(args)) { | ||
this.env.set(key, value) | ||
} | ||
return this.env.eval(program) | ||
} | ||
|
||
async close(): Promise<void> { | ||
} | ||
} | ||
|
||
interface RuleWithArgs { | ||
rule?: string | ||
scriptType?: string | ||
schema?: string | ||
} |
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,60 @@ | ||
import {RuleRegistry} from "../../serde/rule-registry"; | ||
import { | ||
FieldContext, | ||
FieldRuleExecutor, | ||
FieldTransform, | ||
RuleContext, | ||
} from "../../serde/serde"; | ||
import {ClientConfig} from "../../rest-service"; | ||
import {CelExecutor} from "./cel-executor"; | ||
|
||
export class CelFieldExecutor extends FieldRuleExecutor { | ||
executor: CelExecutor = new CelExecutor() | ||
|
||
static register(): CelFieldExecutor { | ||
const executor = new CelFieldExecutor() | ||
RuleRegistry.registerRuleExecutor(executor) | ||
return executor | ||
} | ||
|
||
configure(clientConfig: ClientConfig, config: Map<string, string>) { | ||
this.config = config | ||
} | ||
|
||
type(): string { | ||
return "CEL_FIELD" | ||
} | ||
|
||
override newTransform(ctx: RuleContext): FieldTransform { | ||
return new CelFieldExecutorTransform(this.executor) | ||
} | ||
|
||
async close(): Promise<void> { | ||
} | ||
} | ||
|
||
export class CelFieldExecutorTransform implements FieldTransform { | ||
private executor: CelExecutor | ||
|
||
constructor(executor: CelExecutor) { | ||
this.executor = executor | ||
} | ||
|
||
async transform(ctx: RuleContext, fieldCtx: FieldContext, fieldValue: any): Promise<any> { | ||
if (fieldValue == null) { | ||
return null | ||
} | ||
if (!fieldCtx.isPrimitive()) { | ||
return fieldValue | ||
} | ||
const args = { | ||
value: fieldValue, | ||
fullName: fieldCtx.fullName, | ||
name: fieldCtx.name, | ||
typeName: fieldCtx.typeName(), | ||
tags: Array.from(fieldCtx.tags), | ||
message: fieldCtx.containingMessage | ||
} | ||
return await this.executor.execute(ctx, fieldValue, args) | ||
} | ||
} |
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
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.