marivo.semantic#
marivo.semantic - Python-native semantic layer (v1.1).
Public surface:
import marivo.datasource as md
import marivo.semantic as ms
catalog = ms.load() # returns SemanticCatalog
catalog = ms.load(domains=['sales']) # filter to specific domains
catalog.domains.show()
catalog.metrics.show() # all metrics across domains
ms.domain(name="sales", owner="Mina Zhang", default=True)
warehouse = md.duckdb("warehouse").ref
orders = ms.entity(name="orders", datasource=warehouse, source=md.table("orders"))
amount = ms.measure_column(
name="amount", entity=orders, column="amount",
additivity="additive", unit="USD",
)
revenue = ms.aggregate(name="revenue", measure=amount, agg="sum")
Declaration decorators#
These public constructors are documented inline because their lowercase names collide with the corresponding catalog-object class filenames on case-insensitive filesystems.
- marivo.semantic.entity(*, name, datasource, source, primary_key=None, versioning=None, domain=None, ai_context=None)[source]#
Declare an entity over a structured physical source.
- Parameters:
name (str) – Entity name.
datasource (Ref[DatasourceKind]) – Datasource ref returned by
ms.ref.datasource(...).source (TableSourceIR | ParquetSourceIR | CsvSourceIR | JsonSourceIR) – Structured physical source, usually
md.table(...),md.parquet(...),md.csv(...), ormd.json(...).primary_key (list[str] | None) – Optional list of column names forming the primary key.
domain (Ref[DomainKind] | None) – Override the active domain namespace with a
Ref[domain]returned byms.domain(...). Defaults to the file’s default domain.ai_context (AiContextValue | None) – Optional
AiContextValuefromms.ai_context(...)with extra agent-facing hints.versioning (SnapshotVersioningIR | ValidityVersioningIR | None)
- Returns:
An
Ref[entity]usable by@ms.dimensionand@ms.metric.- Raises:
SemanticDecoratorError –
datasourceis not a datasource ref,namecollides with another object, orsourceis not an entity source.- Return type:
Ref[EntityKind]
Example
>>> orders = ms.entity( ... name="orders", ... datasource=ms.ref.datasource("warehouse"), ... source=md.table("orders", database="sales_mart"), ... )
- marivo.semantic.dimension(*, name=None, entity, domain=None, ai_context=None)[source]#
Declare a categorical dimension whose body returns an ibis expression over its entity.
The decorated function takes the entity table and returns a single expression (single-return AST). Use this for both raw columns and derived expressions (e.g.
table.region).For quantitative measures, use
@ms.measure(entity=..., additivity=...)instead.- Parameters:
name (str | None) – Dimension name. Defaults to the function name.
entity (Ref[EntityKind]) – Owning entity ref returned by
ms.entity(...).domain (Ref[DomainKind] | None) – Override the active domain namespace with a
Ref[domain]returned byms.domain(...). Defaults to the file’s default domain.ai_context (AiContextValue | None) – Optional
AiContextValuefromms.ai_context(...)with extra agent-facing hints.
- Returns:
A decorator that returns a
Ref[dimension].- Raises:
SemanticDecoratorError –
entityis unknown,namecollides, or the body violates the AST whitelist.- Return type:
Callable[[Callable[[…], Any]], Ref[DimensionKind]]
Example
>>> @ms.dimension(entity=orders) ... def region(orders_table): ... return orders_table.region
- marivo.semantic.measure(*, name=None, entity, additivity, unit=None, domain=None, ai_context=None)[source]#
Declare a row-level quantitative measure whose expression can be aggregated.
Measures represent quantitative facts (e.g. amount, quantity) that can be aggregated using
ms.aggregate(). The decorated function takes the entity table and returns a single ibis expression.- Parameters:
name (str | None) – Measure name. Defaults to the function name.
entity (Ref[EntityKind]) – Owning entity ref returned by
ms.entity(...).additivity (Literal['additive', 'non_additive'] | ~marivo.semantic.ir.SemiAdditive) – Whether the measure is
"additive","non_additive", orms.semi_additive(over=..., fold=...).unit (str | None) – UCUM unit token (e.g.
"USD","CNY","%").domain (Ref[DomainKind] | None) – Override the active domain namespace with a
Ref[domain]returned byms.domain(...). Defaults to the file’s default domain.ai_context (AiContextValue | None) – Optional
AiContextValuefromms.ai_context(...)with extra agent-facing hints.
- Returns:
A decorator that returns a
Ref[measure].- Raises:
SemanticDecoratorError –
entityis unknown,namecollides, or the body violates the AST whitelist.- Return type:
Callable[[Callable[[…], Any]], Ref[MeasureKind]]
Example
>>> @ms.measure(entity=orders, additivity="additive", unit="USD") ... def amount(orders_table): ... return orders_table.amount
- marivo.semantic.metric(*, name=None, entities, additivity, root_entity=None, fanout_policy='block', unit=None, provenance=None, domain=None, ai_context=None)[source]#
Declare a metric from an ibis body. Declares
additivitydirectly.- Parameters:
name (str | None) – Metric name. Defaults to the function name.
entities (list[Ref[EntityKind]]) – List of entity refs.
additivity (Literal['additive', 'non_additive'] | ~marivo.semantic.ir.SemiAdditive) –
"additive","non_additive", orms.semi_additive(over, fold).root_entity (Ref[EntityKind] | None) – Required when more than one entity is provided.
fanout_policy (Literal['block', 'aggregate_then_join']) –
"block"(default) or"aggregate_then_join".unit (str | None) – UCUM unit token.
provenance (SqlProvenance | None) – Optional
SqlProvenancefromms.from_sql(sql=..., dialect=...).domain (Ref[DomainKind] | None) – Override the active domain namespace.
ai_context (AiContextValue | None) – Optional
AiContextValuefromms.ai_context(...)with extra agent-facing hints.
- Returns:
A decorator that returns a
Ref[metric].- Return type:
Callable[[Callable[[…], Any]], Ref[MetricKind]]
Example
>>> @ms.metric(entities=[orders], additivity="additive") ... def gmv(orders): ... return (orders.price * orders.qty).sum()
- marivo.semantic.event(*, name=None, identity, occurred_at, participants, domain=None, ai_context=None)[source]#
Declare immutable business occurrences over one existing Entity.
- Parameters:
name (str | None) – Event name. Defaults to the decorated function name.
identity (tuple[Ref[DimensionKind], ...]) – Ordered non-empty occurrence identity Dimensions.
occurred_at (Ref[TimeDimensionKind]) – Business occurrence-time Dimension.
participants (tuple[Participant, ...]) – One or more participant role declarations.
domain (Ref[DomainKind] | None) – Optional explicit domain override.
ai_context (AiContextValue | None) – Business definition and authoring guidance.
- Returns:
A decorator replacing the function with
Ref[event].- Return type:
Callable[[Callable[[…], Any]], Ref[EventKind]]
Example
>>> @ms.event( ... identity=(event_id,), ... occurred_at=event_time, ... participants=(ms.participant(name="order", cardinality="one"),), ... ) ... def order_created(rows): ... return ms.all_rows()
- Constraints:
The function must return
ms.all_rows()or one restricted boolean expression built from source Dimensions throughms.bind.
- marivo.semantic.relationship(*, name, from_entity, to_entity, keys, domain=None, ai_context=None)[source]#
Declare a join relationship between two entities.
Top-level call (not a decorator). Used by the compiler to plan joins when a metric or dimension references dimensions across related entities.
- Parameters:
name (str) – Required relationship name.
from_entity (Ref[EntityKind]) – Source entity ref.
to_entity (Ref[EntityKind]) – Target entity ref.
keys (list[JoinKey]) – List of
ms.join_on(from_key, to_key)pairs.domain (Ref[DomainKind] | None) – Override the active domain namespace with a
Ref[domain]returned byms.domain(...). Defaults to the file’s default domain.ai_context (AiContextValue | None) – Optional
AiContextValuefromms.ai_context(...)with extra agent-facing hints.
- Returns:
A
Ref[relationship].- Raises:
SemanticDecoratorError –
nameis missing, the entities are unknown, orkeysis empty.- Return type:
Ref[RelationshipKind]
Example
>>> ms.relationship( ... name="orders_to_customers", ... from_entity=orders, to_entity=customers, ... keys=[ms.join_on(customer_id, id)], ... )
- marivo.semantic.time_dimension(*, name=None, entity, granularity, parse=None, is_default=False, domain=None, ai_context=None)[source]#
Declare a time-aware dimension that carries grain and parsing metadata.
Time dimensions are the only dimensions usable as window axes by
session.observe. The body may return any ibis expression that represents the intended time axis. Whenparseis omitted, the parse variant is inferred from the column type at analysis time. Usems.datetime(timezone=...)orms.timestamp(timezone=...)for a native naive source axis so readiness can block an undeclared datasource-timezone fallback. Usems.strptime(...)orms.hour_prefix(...)for string/integer parsing.- Parameters:
name (str | None) – Dimension name. Defaults to the function name.
entity (Ref[EntityKind]) – Owning entity ref returned by
ms.entity(...).granularity (Literal['year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second']) –
year | quarter | month | week | day | hour | minute | second— the finest grain at which queries are meaningful.parse (DateParse | DatetimeParse | TimestampParse | StrptimeParse | HourPrefixParse | None) – Optional parse variant. Omit for native temporal columns (the parse is inferred at analysis time). Use
ms.datetime(timezone=...),ms.timestamp(timezone=...),ms.strptime(format), orms.hour_prefix(prefix)when explicit configuration is needed.is_default (bool) – Mark this dimension as the default time axis when multiple time dimensions exist on the entity. At most one time dimension per entity may carry is_default=True. When observe() is called without time_dimension=, the default dimension is used automatically.
domain (Ref[DomainKind] | None) – Override the active domain namespace with a
Ref[domain]returned byms.domain(...). Defaults to the file’s default domain.ai_context (AiContextValue | None) – Optional
AiContextValuefromms.ai_context(...)with extra agent-facing hints.
- Returns:
A decorator that returns a
Ref[time_dimension].- Raises:
SemanticDecoratorError –
entityis unknown,namecollides, the body violates the AST whitelist, or the parse variant is incompatible with the declared granularity.- Return type:
Callable[[Callable[[…], Any]], Ref[TimeDimensionKind]]
Example
>>> @ms.time_dimension(entity=orders, granularity="day") ... def created_at(orders): ... return orders.created_at
- marivo.semantic.domain(*, name, owner, default=True, ai_context=None)[source]#
Declare a semantic domain namespace inside a project file.
A domain groups entities, dimensions, metrics, and relationships under a single qualified name (
<domain>.<object>). Must be called at module top-level inside amodels/semantic/<model>/*.pyproject file.- Parameters:
name (str) – Domain namespace, e.g.
"sales".owner (str) – Human owner accountable for this domain’s semantic correctness and quality.
default (bool) – If True, subsequent decorators in this file resolve to this domain when no explicit
domain=kwarg is passed.ai_context (AiContextValue | None) – Optional
AiContextValuefromms.ai_context(...)with extra agent-facing hints.
- Returns:
A
Ref[domain]that can be passed as thedomain=kwarg to other decorators to override the default domain context.- Raises:
OutsideLoaderContextError – Called outside a semantic loader pass.
SemanticDecoratorError –
namecollides with another domain in the project.
- Return type:
Ref[DomainKind]
Example
>>> import marivo.semantic as ms >>> sales = ms.domain(name="sales", owner="Mina Zhang", default=True)
Event helpers#
event is the only Event declaration entrypoint. Filtered Event bodies
return a restricted boolean expression; unfiltered bodies explicitly return
all_rows().
- marivo.semantic.participant(*, name, path=None, cardinality)[source]#
Declare one named participant role inside
@ms.event(...).- Parameters:
name (str) – Stable lowercase snake-case role name.
path (tuple[Ref[RelationshipKind], ...] | None) – Directed non-empty relationship path from the Event source. Omit when the source Entity itself plays this role.
cardinality (Literal['one', 'optional_one']) –
"one"or"optional_one".
- Returns:
Immutable participant authoring value.
- Return type:
Participant
Example
>>> buyer = ms.participant( ... name="buyer", ... path=(event_to_buyer,), ... cardinality="one", ... )
- marivo.semantic.participant_role(*, event, name)[source]#
Create the typed handle for one named Event participant role.
- Parameters:
event (Ref[EventKind]) – Exact Event ref returned by
@ms.event(...).name (str) – Declared participant role name.
- Returns:
Immutable role handle resolved against the catalog by consumers.
- Return type:
ParticipantRoleHandle
Example
>>> buyer = ms.participant_role(event=payment_succeeded, name="buyer")
Aggregation & measure helpers#
Declare a tier-1 simple metric: an aggregation over a measure. |
|
Declare a row-count metric for an entity. |
|
Build an AND-joined filter for |
|
Declare a derived linear metric (no body): sum of |
|
Declare a derived ratio metric (no body). |
|
Declare an exact tier-1 weighted mean over two row-level measures. |
|
Declare a semi-additive nature: additive off the |
|
Declare daily snapshot partition versioning for an entity. |
|
Declare SCD2 validity interval versioning for an entity. |
|
Build one relationship key pair for |
|
Declare a cumulative metric over a tier-1 base metric. |
|
Select a grain-to-date cumulative anchor (MTD / QTD / YTD resets). |
|
Select a fixed-size trailing cumulative anchor (rolling N). |
Column helpers#
Declare a categorical dimension directly from one physical column. |
|
Declare a quantitative measure directly from one physical column. |
|
Declare a time dimension directly from one physical column. |
|
Declare one finite governed calendar over an exhaustive civil-date spine. |
|
Declare the baseline-key field for one named calendar correspondence. |
|
Declare one finite governed set of named temporal occurrences. |
|
Declare one finite governed daily final working-status schedule. |
|
Construct one semantic aggregation grain from a period-calendar level. |
Time parsing#
Declare an already-temporal datetime column parse. |
|
Declare an already-temporal timestamp column parse. |
|
Declare a string/integer strptime parse. |
|
Declare an hour-only partition parse using a day prefix column. |
Provenance#
Declare SQL parity provenance for a Python metric body. |
Readiness & verification#
Return a demand-ranked advisory richness report. |
|
Run parity check for a metric against its source SQL. |
Refs, binding & loading#
ref is the immutable exact-kind factory namespace. Ref values themselves
are immutable identities and are never callable; use bind to apply a field
ref to a direct entity alias inside a decorated expression body.
- marivo.semantic.ref#
Exact semantic-ref factories kept separate from immutable identities.
- marivo.semantic.bind(field, entity_alias, /)[source]#
Apply a semantic field ref to an entity alias in an expression body.
Parameters#
- field:
Exact dimension, time-dimension, or measure ref declared in the loaded semantic project.
- entity_alias:
Direct entity parameter of the active decorated expression body.
Returns#
- ibis.expr.types.Value
The referenced field expression evaluated on
entity_alias.
Example#
>>> @ms.metric(entities=[orders], additivity="additive") ... def revenue(orders): ... return ms.bind(amount, orders).sum()
Constraints#
Only valid inside a loaded semantic expression body. The field must belong to the bound entity and must be captured as a direct
ms.bindargument.- Parameters:
field (Ref[DimensionKind | TimeDimensionKind | MeasureKind])
entity_alias (Table)
- Return type:
Value
Focused help#
python -m marivo help only validates the active interpreter, package
version, and environment fingerprint. Use the sole public coordinator,
marivo.help("semantic.<target>"), for bounded constructor, catalog, and
validation contracts rendered from the semantic registry. The ms namespace
executes semantic operations and intentionally has no ms.help() alias.
A legal ms.where(...) declaration that cannot be compared with the resolved
runtime dtype raises filter_value_runtime_incompatible before query
submission. Its authored literal is preserved until the user or business owner
confirms any required code/label mapping.
Details types#
Details for an entity object. |
|
Details for a categorical dimension object. |
|
Details for a row-level quantitative measure object. |
|
Represent a PEP 604 union type |
|
Details for a relationship between entities. |
|
Details for one executable Event definition. |
|
Details for a time dimension object. |
|
Details for a domain object. |
|
Details for a datasource object. |
|
Details for a derived (composed) metric. |
|
Details for a simple (entity-backed) metric. |
|
Static source contract for one governed period calendar. |
|
Certification-derived facts for one owned period-calendar level. |
|
Static source contract for one governed temporal set. |
|
Static source contract for one governed final daily work schedule. |
Catalog & objects#
Typed collections resolve a local name, a full semantic path, or an exact
same-kind Ref within the collection’s current scope. Scoped collections do
not widen to out-of-scope objects. catalog.require(ref) remains the strict,
global, ref-only lookup for configured, persisted, or logged identity.
catalog.verify(...), catalog.preview(...),
catalog.preview_many(...), and catalog-leaf
catalog.readiness(refs=[...]) accept either an exact entry owned by the
current compiled catalog or its exact ref. Entries normalize immediately to
refs; readiness results, preview evidence, persistence, and recovery remain
ref-based.
Read-only object graph over a loaded semantic project. |
|
Read-only typed collection scoped by exact kind and optional owner. |
|
One immutable browsable object in one compiled semantic catalog. |
|
Closed runtime kind registry for every semantic identity. |
|
Loaded period-calendar declaration with direct semantic-grain lookup. |
|
Bounded snapshot-bound page of exact period scopes. |
|
Loaded temporal-set declaration with exact occurrence navigation. |
|
Bounded snapshot-bound page of exact occurrence scopes. |
|
Loaded work-schedule declaration with certified-status navigation. |
Sources & provenance#
SQL parity provenance for a Python-authored metric body. |
Readiness & assessment#
Result of a single metric parity check. |
|
Successful bounded previews for an explicitly requested ref batch. |
Keys & kinds#
One left/right relationship key pair. |
AI context#
Construct a validated AiContext for semantic objects. |
|
Validated AI-facing context for semantic and datasource objects. |
Submodules#
|
Typed semantic errors and warnings raised across the semantic layer. |
|
Shared type aliases for the semantic surface. |