semql
Pure-Python compiler from a semantic spec to backend SQL. Define cubes (dimensions, measures, time-dimensions, joins) once; emit correct, parameterised SQL for Postgres, ClickHouse, DuckDB, Snowflake, BigQuery, and the analytics engines Redshift, Trino and Databricks.
SQL Server, MySQL and Oracle ship as experimental / opt-in
dialects: sqlglot transpiles their date_trunc / percentile to
best-effort forms that aren't exercised in CI, so enable them
deliberately by passing experimental_dialects() through the
compiler's dialects= override and verify the SQL on a live
instance. (Gap-filling time-spines — fill_nulls — are not yet
implemented for any of the six new dialects.)
semql does no I/O: catalogs are Python data; the compiler
returns SQL + bound params; running the SQL is the caller's job.
Sibling packages add LLM-planner prompt fragments (semql-prompt),
MCP exposure (semql-mcp) and ER diagrams (semql-erd).
Install
pip install semql
Quick start
from semql import (
Dialect,
Catalog,
Cube,
Dimension,
Measure,
SemanticQuery,
)
orders = Cube(
name="orders",
dialect=Dialect.POSTGRES,
table="orders",
alias="o",
measures=[
Measure(name="revenue", sql="{o}.amount", agg="sum", unit="currency"),
],
dimensions=[
Dimension(name="region", sql="{o}.region", type="string"),
],
)
catalog = Catalog([orders])
compiled = catalog.compile(
SemanticQuery(measures=["orders.revenue"], dimensions=["orders.region"]),
)
# compiled.sql, compiled.params, compiled.columns, compiled.dialect
The {o} placeholder in a cube's sql is its alias; the compiler
resolves it (along with {schema}-style context placeholders and
{ctx.X} row-level-security placeholders) at compile time.
What lives in the box
| Surface | Module |
|---|---|
| Cube / Measure / Dimension / TimeDimension / Join | semql.model |
| SemanticQuery / Filter / TimeWindow / CompareWindow | semql.spec |
| Catalog wrapper (validation, compile entry) | semql.catalog |
| Compiler — sqlglot AST → dialect SQL | semql.compile |
| Collect-all static validator | semql.validate |
| Reflection cubes (catalog_cubes, ...) | semql.introspect |
| Planner / router prompt fragments | semql-prompt (sibling package) |
| Dialect strategies + sqlglot dialect adapter | semql.backend, semql.dialect |
| Visualisation decision (chart type, axes, formats) | semql.visualize |
is_read_only_statement post-hoc SQL guard |
semql.safe |
| Structured error hierarchy | semql.errors |
Features
- Compare windows —
CompareWindow(mode="previous_period")wraps the inner query incurrent/priorCTEs joined viaFULL OUTER JOINand emits{m}_current/{m}_prior/{m}_delta/{m}_pct_changecolumns per measure. - Temporal model — time dimensions group by
second/minute/hour/day/week/month/quarter/year; atype="date"time dimension drops sub-day grain and timezone shifts; per-cubetimezonemakesdate_trunctenant-correct and transpiles per dialect (AT TIME ZONE,CONVERT_TIMEZONE, ClickHouse's native arg, …); per-cubeweek_start(mondaydefault, orsunday) sets theweekbucket boundary consistently across dialects. - Explicit raw SQL — every hand-written fragment (
Measure.sql,Join.on,Cube.base_predicate, …) is wrapped in aRawSQLmarker at validation: when raw SQL is used, the model says so. - Tenancy — per-cube
NONE(default; honestly unscoped),SCHEMA({tenant_schema}substituted from the identity'stenant, required when declared) orDISCRIMINATOR(compiler wraps the source in a subquery with one boundWHEREpredicate pertenancy_columnsentry, so composite tenant keys need no workaround).tenantis first-class onAuthContext;Catalog(strict_tenancy=True)rejects any cube left with no isolation, scope, orrequired_roles. - Row-level security —
Cube.security_sqlAND-composes with tenancy inside the isolation subquery;{ctx.X}placeholders bind as parameters, never inline as literals. - MCP-ready —
build_planner_prompt_fragment(catalog.as_dict())(insemql-prompt) produces the planner system-prompt fragment;semql-mcpwraps it as a server. - Pluggable backends —
DialectStrategyProtocol lets out-of-tree Snowflake / BigQuery adapters slot in without forking the compiler.
Philosophy
See PHILOSOPHY.md at the repo root. Highlights:
- The emitted SQL must be readable by the engineer debugging a production incident at 2am.
- Compile errors beat runtime errors; runtime errors beat wrong results.
compile()fails at the first problem;validate()collects them all.- Catalogs are data; the META cubes expose the catalog through the same compiler path a normal query takes.
Status
Pre-v1. The shape is stable, but minor names / fields may move before the v1 contract locks. Tests pin every public behaviour the README documents.
Release files for semql 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| semql-0.7.0.tar.gz | 259.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| semql-0.7.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 540.7 kB
Release files / semql-0.7.0.tar.gz
| Download URL | semql-0.7.0.tar.gz |
|---|---|
| Size | 259.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
52fd22f6a1fa289ae797d0eb354eaba3871ee3fb816092086e4e989110890674
|
|
BLAKE2b-256 checksum How to use checksums |
0441b81e4bba3eb13be3f6872d9aac630e5ea2890b113e900aa88cc9add1c70e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 21, 2026.
Transparency logRelease files / semql-0.7.0-py3-none-any.whl
| Download URL | semql-0.7.0-py3-none-any.whl |
|---|---|
| Size | 281.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1db3bc54eada1fe5dfffba914c8d947b435496329dda07205712faab11d80eaa
|
|
BLAKE2b-256 checksum How to use checksums |
f5f9cd6b5e70a7ad01f314706b6605c15946e5c4c6ab714746ec52eaa6a3e4bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 21, 2026.
Transparency log