[hypequery](https://hypequery.com/) › ClickHouse with SQLAlchemy: Where a Semantic Layer Fits | hypequery
Index for agents: https://hypequery.com/llms.txt
Canonical page: https://hypequery.com/clickhouse-sqlalchemy

> SQLAlchemy builds queries; hypequery defines what your metrics mean. Use SQLAlchemy for ad hoc ClickHouse SQL and hypequery for shared, tenant-scoped measures.

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

ClickHouse SQLAlchemy

# SQLAlchemy writes queries. hypequery defines your metrics.

Yes, SQLAlchemy works with ClickHouse: through clickhouse-connect's clickhousedb:// dialect, or clickhouse-sqlalchemy's clickhouse+http\:// and clickhouse+native:// drivers. If you use SQLAlchemy with ClickHouse, you already have a good way to build SQL. What you don’t have is one shared definition of revenue, scoped to the right customer. hypequery adds that layer next to SQLAlchemy, not instead of it.

[SQLAlchemy guide ](https://hypequery.com/docs/python/sqlalchemy)[Python semantic layer](https://hypequery.com/python-semantic-layer)

Replaces SQLAlchemy?

No, it sits beside it

Driver

clickhouse-connect

Adds

Measures, validation, tenancy

## Query builders don’t hold meaning

SQLAlchemy can express any aggregation, but it doesn’t know which one is “revenue”. Every caller rebuilds it.

## Each query re-applies the rules

Tenant filters, completed-order conditions, and timezone bucketing are repeated in every statement.

## Callers can ask for anything

An API built on a query builder needs its own allow-list of fields, filters, and limits. That’s a second definition to maintain.

Side by side

## Write SQL when you need it. Name measures when they’re shared.

Keep SQLAlchemy for one-off and operational queries. Put the numbers your product, reports, and agents share into datasets.

- Measures and dimensions are defined once and reviewed as code
- Callers pick fields by name; anything undeclared is rejected
- The tenant filter is added for you and can’t be widened from a query
- Python has no hypequery query builder, so SQLAlchemy stays your tool for raw SQL

compare.py

### The same report, two ways

reports.py

```
from sqlalchemy import func, select

# SQLAlchemy: you write the query, every time
stmt = (
    select(
        orders_table.c.status,
        func.sum(orders_table.c.amount).label("revenue"),
    )
    .where(orders_table.c.org_id == org_id)  # don't forget this one
    .group_by(orders_table.c.status)
)

# hypequery: you name the measure; the definition and tenant filter are shared
from hypequery.datasets import tenant

result = analytics.for_tenant(tenant(org_id)).execute(
    "orders", {"dimensions": ["status"], "measures": ["revenue"]}
)
```

`orders_table` is a SQLAlchemy `Table`; `orders` is a hypequery dataset.

One engine, one config

## Point hypequery at your engine’s server

`executor_from_engine` reads your engine’s URL and connects hypequery to the same ClickHouse server, with its own HTTP clients, so closing one never affects the other. Works with `clickhousedb://` and `clickhouse+http://` engines.

hypequery sends values as ClickHouse server-side parameters, which only the HTTP interface carries, so `clickhouse+native://` engines are refused with an explanation. Use an HTTP-interface URL for hypequery, usually port 8123 or 8443.

hypequery does not read SQLAlchemy `Table` metadata yet. Generate datasets from your live ClickHouse schema with `hypequery generate datasets` instead.

engine.py

### hypequery and SQLAlchemy on one server

analytics/engine.py

```
from sqlalchemy import create_engine
from hypequery.contrib.sqlalchemy import executor_from_engine
from hypequery.datasets import create_dataset_client

# clickhouse-connect's dialect, or clickhouse-sqlalchemy's clickhouse+http://
engine = create_engine("clickhousedb://user:password@host:8123/analytics")

# hypequery opens its own HTTP clients for the engine's server;
# it never borrows a connection from the engine's pool.
analytics = create_dataset_client(
    executor=executor_from_engine(engine), registry=registry
)
```

The same URLs also work directly with `ClickHouseConnection.from_url()`.

Where teams usually get stuck

## Questions teams ask

### Can I use SQLAlchemy with ClickHouse?

Yes. clickhouse-connect ships a `clickhousedb://` dialect, and the clickhouse-sqlalchemy package adds `clickhouse+http://` and `clickhouse+native://`. Either gives you a SQLAlchemy engine for ClickHouse.

### clickhouse-sqlalchemy vs hypequery

clickhouse-sqlalchemy is a dialect for building SQL. hypequery is a semantic layer for shared measures. Many teams want both.

### SQLAlchemy ORM for ClickHouse analytics

ORMs map rows to objects. Analytics wants aggregations by dimension, which is what datasets describe.

### Using one ClickHouse config

`executor_from_engine(engine)` points hypequery at the server your `clickhousedb://` or `clickhouse+http://` engine uses.

### Generate models from SQLAlchemy?

Not today. Datasets are generated from the live ClickHouse schema, or written by hand.

Further reading

## Go deeper where it actually helps

### [SQLAlchemy guide](https://hypequery.com/docs/python/sqlalchemy)

[Engines, supported dialects, and URLs.](https://hypequery.com/docs/python/sqlalchemy)

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

### [Python quick start](https://hypequery.com/docs/quick-start#python)

[Install hypequery and define your first dataset.](https://hypequery.com/docs/quick-start#python)

[Open guide](https://hypequery.com/docs/quick-start#python)

### [Python CLI](https://hypequery.com/docs/python/cli)

[Generate datasets from your ClickHouse schema.](https://hypequery.com/docs/python/cli)

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

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

[Driver or semantic layer: what each one is for.](https://hypequery.com/compare/hypequery-vs-clickhouse-connect)

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

Related pillars

[ClickHouse with TypeScript and Python](https://hypequery.com/clickhouse)[ClickHouse with Python](https://hypequery.com/clickhouse-python)[ClickHouse to pandas](https://hypequery.com/clickhouse-pandas)[ClickHouse with Django](https://hypequery.com/clickhouse-django)[Python semantic layer](https://hypequery.com/python-semantic-layer)

Next step

## Move one shared number into a dataset

Find the aggregation you copy most often, define it as a measure, and query it by name.

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

[Get started ](https://hypequery.com/docs/quick-start#python)[Generate datasets](https://hypequery.com/docs/python/cli)
