Queries and results

A compiled policy (Rego.Policy) evaluates queries. A query is Rego text, for example data.authz.allow or x := data.authz.reasons; count(x) > 0.

Each function parses and compiles the query, and evaluates it with an input and data. The functions give the result in different forms.

The forms of a result

Function The result Use it for
allowed(query, input, data) true only if the query has one solution with no bindings, and its value is true A decision, as ResultSet.Allowed() of OPA's Go API.
eval(query, input, data) The value of the single expression, or null if it is undefined Any other value, for example a set of reasons.
query(query, input, data) One map of the bindings of the vars for each solution A query with vars, for example some x in data.users.
prepare(query) A Prepared query, parsed and compiled one time A query that the application evaluates for each request.
Queries.kt
// true
val allowed = policy.allowed("data.authz.allow", input)
// {"the user is not an admin"}
val reasons = policy.eval("data.authz.reasons", input)
// [{reason="the user is not an admin"}]
val solutions = policy.query("some reason in data.authz.reasons", input)
// parse and compile one time ...
val allow = policy.prepare("data.authz.allow")
// ... and evaluate for each request
val again = allow.allowed(input)
Queries.kt, lines 30 to 39, on GitHub

Do not count the solutions of query for a decision. As in OPA, a query with one expression and no vars has a solution when the value is false: 1 == 2 gives one solution with no bindings.

The result of a prepared query

Prepared.result gives a QueryResult:

It takes the time, the local zone and the runtime of the query too. Thus a query that read the time can be evaluated again with the same time, and gives the same result.

Queries.kt
val rego = Rego(Builtins.Pure.stdlib(Re2Engine) + Builtins.Provided.stdlib)
val hour = rego.compile(RegoFileName("hours.rego") to RegoFile(source))
    .prepare("data.hours.hour")
val result = hour.result(timestamp = time)
// [[9]]: the value of each expression, for each solution
val values = result.expressions
// ProvidedValues(time=2026-01-15T09:30:00Z): what the query read
val provided = result.provided
// true: the same time gives the same result
val reproducible = !result.calledImpureBuiltin
Queries.kt, lines 48 to 57, on GitHub

See A builtin is pure, provided or impure for the provided values, and Signed decisions and inputs for a result that an auditor can reproduce.

Errors

A parse error, a compile error and an error of an evaluation (for example a conflict of two values of a complete rule) are a RegoException.

As in OPA, an error of a builtin makes the call undefined. To stop the query at the error, give strictBuiltinErrors = true.

Queries.kt
// null: the builtin error makes n undefined
val undefined = policy.eval("data.numbers.n", input)
val error = try {
    policy.prepare("data.numbers.n").result(input, strictBuiltinErrors = true)
    throw IllegalStateException("the query must fail")
} catch (e: RegoException) {
    // eval_builtin_error: to_number: ...
    e
}
Queries.kt, lines 67 to 75, on GitHub

The budget of an evaluation

A policy can loop for a long time, or make a very large value, for example numbers.range(1, 1000000000). If the policies or the queries come from people that you do not trust, give each evaluation a budget.

Give the budget to Rego for all the queries, or to Prepared.result for one query. By default, a Rego has Budget.UNLIMITED.

Queries.kt
val rego = Rego(
    Builtins.Pure.stdlib(Re2Engine),
    budget = Budget(time = 2.seconds, steps = 1_000_000),
)
val n = rego.compile(RegoFileName("loops.rego") to RegoFile(source))
    .prepare("data.loops.n")
val error = try {
    n.result(input)
    throw IllegalStateException("the query must fail")
} catch (e: RegoException) {
    // eval_cancel_error: caller cancelled query execution: the evaluation
    // used more than 1000000 steps
    e
}
// a budget for one query replaces the budget of the Rego
val small = Value.of(mapOf("size" to 3))
val three = n.result(small, budget = Budget(steps = 100)).expressions
Queries.kt, lines 85 to 101, on GitHub

A time alone does not limit the memory: a builtin can use all the memory before the time is over. Thus give a server that evaluates the policies of other people both limits.

The trace of a query

query and Prepared.result take a Tracer, which gets each step of the evaluation as a TraceEvent.

Explanation gives the forms of opa eval --explain: FULL, NOTES, FAILS and DEBUG, with OPA's text.

Queries.kt
val events = ArrayList<TraceEvent>()
policy.query("data.authz.allow", input, tracer = events::add)
// the lines of `opa eval --explain full --format pretty`
val lines = Explanation.FULL.lines(events)
Queries.kt, lines 111 to 114, on GitHub

For a viewer who deletes, the explanation shows Index data.authz.allow (matched 0 rules, early exit). The rule index of OPA finds that no rule of allow can match the input, so only the default rule gives a value, false.

The debugger uses the events of rego4k to show the part of the input that each step reads.