Skip to content

API Reference parrot.semantic.accounting.compiler

github-actions[bot] edited this page Dec 2, 2026 · 1 revision

parrot.semantic.accounting.compiler

View source

Accounting compiler (FEAT-713 M5): planning, SQL rendering, self-check, hashing and lineage.

Imports nothing from parrot.semantic outside the accounting package.

Alias AccountingError

Re-exported from parrot.semantic.accounting.errors.AccountingError.

Alias AccountingModel

Re-exported from parrot.semantic.accounting.models.AccountingModel.

Alias AccountingRequest

Re-exported from parrot.semantic.accounting.models.AccountingRequest.

Alias AssertionRequest

Re-exported from parrot.semantic.accounting.models.AssertionRequest.

Alias BinOp

Re-exported from parrot.semantic.accounting.grammar.BinOp.

Alias CompiledMetricQuery

Re-exported from parrot.semantic.accounting.models.CompiledMetricQuery.

Alias Expr

Re-exported from parrot.semantic.accounting.grammar.Expr.

Attribute Interval

Alias Lineage

Re-exported from parrot.semantic.accounting.models.Lineage.

Alias MetricPlan

Re-exported from parrot.semantic.accounting.models.MetricPlan.

Alias MetricRequest

Re-exported from parrot.semantic.accounting.models.MetricRequest.

Alias Neg

Re-exported from parrot.semantic.accounting.grammar.Neg.

Alias Num

Re-exported from parrot.semantic.accounting.grammar.Num.

Alias PeriodSpansMembershipChange

Re-exported from parrot.semantic.accounting.errors.PeriodSpansMembershipChange.

Alias PlanExpr

Re-exported from parrot.semantic.accounting.models.PlanExpr.

Alias PlanSet

Re-exported from parrot.semantic.accounting.models.PlanSet.

Alias Ref

Re-exported from parrot.semantic.accounting.grammar.Ref.

Alias RequestValidationError

Re-exported from parrot.semantic.accounting.errors.RequestValidationError.

Alias Sum

Re-exported from parrot.semantic.accounting.grammar.Sum.

Function canonical_request

Sorted-key compact JSON of the request, by alias — the cache-key and audit form.

canonical_request(request: MetricRequest | AssertionRequest) -> str

Parameter Type Default Description
request MetricRequest | AssertionRequest A metric or assertion request.

Returns: str — The exact <request JSON> string embedded in CompiledMetricQuery.cache_key.

Function compile_assertion

plan_assertion + render_plan; lineage carries metric=None and the assertion index.

compile_assertion(model: AccountingModel, request: AssertionRequest, dialect: str) -> CompiledMetricQuery

Parameter Type Default Description
model AccountingModel The loaded model.
request AssertionRequest The assertion request.
dialect str Target dialect.

Returns: CompiledMetricQuery — The compiled assertion query.

Function compile_metric

plan_metric + render_plan. Same (model_hash, canonical request, dialect) ⇒ byte-identical sql.

compile_metric(model: AccountingModel, request: MetricRequest, dialect: str) -> CompiledMetricQuery

Parameter Type Default Description
model AccountingModel The loaded model.
request MetricRequest The metric request.
dialect str Target dialect.

Returns: CompiledMetricQuery — The compiled query with plan, hashes and lineage.

Alias metric_order

Re-exported from parrot.semantic.accounting.validation.metric_order.

Alias parse_assertion

Re-exported from parrot.semantic.accounting.grammar.parse_assertion.

Alias parse_expr

Re-exported from parrot.semantic.accounting.grammar.parse_expr.

Function plan_assertion

Plan both sides of model.spec.assertions[request.assertion_index] over request.period (no grain).

plan_assertion(model: AccountingModel, request: AssertionRequest) -> MetricPlan

Parameter Type Default Description
model AccountingModel The loaded, validated model.
request AssertionRequest The assertion request (never cumulative).

Returns: MetricPlan — A MetricPlan whose columns are ("lhs", …) and ("rhs", …).

Raises:

  • RequestValidationError — Index out of range or an inactive set.
  • PeriodSpansMembershipChange — The period crosses a membership change.

Function plan_metric

Dialect-independent, JSON-serialisable plan. Raises RequestValidationError / PeriodSpansMembershipChange.

plan_metric(model: AccountingModel, request: MetricRequest) -> MetricPlan

Parameter Type Default Description
model AccountingModel The loaded, validated model.
request MetricRequest The metric request (cumulative/grain combinations already checked by the model).

Returns: MetricPlan — The portable MetricPlan.

Raises:

  • RequestValidationError — Unknown metric or dimension, undeclared filter, inactive set.
  • PeriodSpansMembershipChange — The period, or the widened cumulative window, crosses a membership change.

Alias references

Re-exported from parrot.semantic.accounting.grammar.references.

Function render_plan

Render plan to SQL for dialect; deterministic; never returns SQL that fails the self-check.

The SQL returns exact per-set aggregates (cumulative sums for period-to-date requests) and never evaluates metric arithmetic; plan.columns is applied afterwards by the shared evaluator.

render_plan(plan: MetricPlan, dialect: str) -> str

Parameter Type Default Description
plan MetricPlan The portable plan; nothing else is consulted.
dialect str "duckdb", "postgres" or "bigquery".

Returns: str — The rendered SQL text.

Raises:

  • AccountingError — Unknown dialect, inconsistent plan, or SQL failing the self-check.

Function self_check

Re-parse and assert referenced tables/columns ⊆ declared; raise AccountingError otherwise.

Output aliases are accepted only for the names the renderer produces (bucket and set_<name>) and only when the SQL itself defines them; * projections, table functions and any other table are rejected.

self_check(sql: str, dialect: str, *, table: str, columns: frozenset[str]) -> None

Parameter Type Default Description
sql str Rendered SQL.
dialect str Dialect to parse with.
table str The declared journal table ([catalog.][db.]table).
columns frozenset[str] Declared journal columns.

Raises:

  • AccountingError — Unparseable SQL, more than one statement, or a reference outside the allowlist.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally