[hypequery](https://hypequery.com/) › A Python Semantic Layer for ClickHouse: Analytics as Code | hypequery
Index for agents: https://hypequery.com/llms.txt
Canonical page: https://hypequery.com/python-semantic-layer

> Define measures, dimensions, relationships and tenant rules as Python code in an open-source semantic layer that runs inside your app. No YAML, no extra server.

[Open sourceApache 2.0](https://github.com/hypequery/hypequery)

Python semantic layer

# A semantic layer you write in Python, not YAML

hypequery is an open-source semantic layer for ClickHouse that lives in your codebase. Define what revenue, buyers, and enterprise customers mean as Python code, then query those definitions from any part of your app, with tenant isolation enforced on every query.

[Read the datasets docs ](https://hypequery.com/docs/python/datasets/overview)[ClickHouse with Python](https://hypequery.com/clickhouse-python)

Model format

Python code

Runtime

Inside your app

License

Apache 2.0

## Definitions drift between services

When each team writes its own SQL for the same KPI, dashboards and APIs disagree. A semantic layer only helps if every consumer actually reads from it.

## Config files sit outside your workflow

Models kept in separate YAML files or a separate platform miss the type checks, tests, refactoring, and review that the code using them already gets.

## Another server is another thing to run

A standalone semantic-layer service adds deployment, networking, and auth to keep in sync, before it answers a single query.

Model

## Your model is Python objects

Datasets, dimensions, measures, and relationships are immutable Python objects, validated when you create them. They import like any other module and change through the same pull requests as your application.

- Dimensions and measures with labels and descriptions for catalogs and tools
- Filtered measures and to-one relationships with automatic joins
- Tenant keys enforced from trusted context, never from the query
- A catalog of what each dataset exposes, shared with TypeScript

Model

### Two datasets and a relationship

model.py

```
from hypequery.datasets import Dataset, belongs_to, dimension, filter, measure

customers = Dataset(
    name="customers",
    source="customers",
    dimensions={"id": dimension("string"), "tier": dimension("string")},
)

orders = Dataset(
    name="orders",
    source="orders",
    tenant_key="tenant_id",
    dimensions={
        "country": dimension("string"),
        "status": dimension("string"),
    },
    measures={
        "revenue": measure.sum("amount", label="Revenue"),
        "us_revenue": measure.sum(
            "amount", filters=(filter.eq("country", "US"),)
        ),
        "buyers": measure.count_distinct("customer_id"),
    },
    relationships={
        "customer": belongs_to(
            customers, from_field="customer_id", to_field="id"
        )
    },
)
```

Relationship targets are datasets themselves, so the model is checked when it loads.

Use

## Query by name, wherever the code runs

The same definitions serve a nightly job, a notebook, and a FastAPI endpoint. hypequery plans parameterized ClickHouse SQL, joins related datasets, and scopes every query to one tenant.

There is no separate service to deploy. The semantic layer is a library in your process, so it uses your ClickHouse connection, your auth, and your deployment.

Because Python and TypeScript implement the same specifications, a TypeScript service can serve the same model your Python jobs use.

Query

### Group revenue by a related customer field

use.py

```
from hypequery.datasets import (
    DatasetQuery, create_dataset_client, create_dataset_registry, tenant,
)

analytics = create_dataset_client(
    executor=executor, registry=create_dataset_registry(orders, customers)
)

# In a request handler: bind to the caller's tenant first.
scoped = analytics.for_tenant(tenant(org_id))
scoped.execute(
    "orders",
    DatasetQuery(
        dimensions=("customer.tier",), measures=("revenue", "buyers")
    ),
)
```

A tenant-bound client runs every query as that tenant.

Where teams usually get stuck

## Questions teams ask

### Who this is for

Engineering teams who want shared analytics definitions without adopting a BI platform or running another service.

### Analytics as code

Definitions are versioned, reviewed, and tested like application code, because they are application code.

### Open source

Apache 2.0 licensed and currently in beta. Run it entirely in your own infrastructure against your own ClickHouse.

### Serving is optional

Query datasets in-process, or add the FastAPI router when dashboards, customers, or agents need HTTP access. The same endpoints feed type-safe React hooks and MCP tools.

Further reading

## Go deeper where it actually helps

### [hypequery vs clickhouse-connect](https://hypequery.com/compare/hypequery-vs-clickhouse-connect)

[When to keep the driver for ad hoc SQL, and when to move repeated metrics into shared datasets.](https://hypequery.com/compare/hypequery-vs-clickhouse-connect)

[Open guide](https://hypequery.com/compare/hypequery-vs-clickhouse-connect)

### [Defining datasets](https://hypequery.com/docs/python/datasets/defining-datasets)

[The Python dataset model and its fields.](https://hypequery.com/docs/python/datasets/defining-datasets)

[Open guide](https://hypequery.com/docs/python/datasets/defining-datasets)

### [Relationships](https://hypequery.com/docs/python/datasets/relationships)

[Join related datasets and query their fields by name.](https://hypequery.com/docs/python/datasets/relationships)

[Open guide](https://hypequery.com/docs/python/datasets/relationships)

### [ClickHouse semantic layer](https://hypequery.com/clickhouse-semantic-layer)

[The semantic layer concept and the TypeScript side of hypequery.](https://hypequery.com/clickhouse-semantic-layer)

[Open guide](https://hypequery.com/clickhouse-semantic-layer)

### [ClickHouse FastAPI](https://hypequery.com/clickhouse-fastapi)

[Serve your Python model as authenticated HTTP endpoints.](https://hypequery.com/clickhouse-fastapi)

[Open guide](https://hypequery.com/clickhouse-fastapi)

### [React and MCP clients](https://hypequery.com/docs/python/client-endpoints)

[Generated, type-safe React hooks and agent tools over your Python endpoints.](https://hypequery.com/docs/python/client-endpoints)

[Open guide](https://hypequery.com/docs/python/client-endpoints)

Related pillars

[ClickHouse Flask](https://hypequery.com/clickhouse-flask)[ClickHouse Django](https://hypequery.com/clickhouse-django)[ClickHouse Python](https://hypequery.com/clickhouse-python)[ClickHouse Python analytics](https://hypequery.com/clickhouse-python-analytics)[ClickHouse FastAPI](https://hypequery.com/clickhouse-fastapi)[ClickHouse semantic layer](https://hypequery.com/clickhouse-semantic-layer)[What is a semantic layer?](https://hypequery.com/semantic-layer)[Python multi-tenancy](https://hypequery.com/clickhouse-python-multi-tenant)[ClickHouse to pandas](https://hypequery.com/clickhouse-pandas)[MCP for Python backends](https://hypequery.com/clickhouse-python-mcp)

Next step

## Define your most-used metric as a measure

Start with the number your team argues about most, model it once in Python, and point every consumer at the definition.

Open source, and it runs in your own backend. Want it hosted?

[Start with Python ](https://hypequery.com/docs/quick-start#python)[Measures docs](https://hypequery.com/docs/python/datasets/measures)
