Skip to main content

Semantic data layer: SemanticQuery → backend SQL with authorisation, row-level scoping, time-spine fill, and a typed four-role LLM prompt pipeline.

Project description

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 and (via the strategy seam) Snowflake / BigQuery.

semql does no I/O: catalogues are Python data; the compiler returns SQL + bound params; running the SQL is the caller's job. Prompt-fragment rendering for LLM planners ships in the core (semql.prompt). Sibling packages add MCP exposure (semql-mcp) and ER diagrams (semql-erd).

Install

pip install semql

Quick start

from semql import (
    Backend,
    Catalog,
    Cube,
    Dimension,
    Measure,
    SemanticQuery,
)

orders = Cube(
    name="orders",
    backend=Backend.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.backend

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, prompt, 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
Backend strategies + sqlglot dialect adapter semql.backend, semql.dialect
Visualisation decision (chart type, axes, formats) semql.visualize
is_safe_select post-hoc SQL guard semql.safe
Structured error hierarchy semql.errors

Features

  • Compare windowsCompareWindow(mode="previous_period") wraps the inner query in current / prior CTEs joined via FULL OUTER JOIN and emits {m}_current / {m}_prior / {m}_delta / {m}_pct_change columns per measure.
  • Tenancy — per-cube SCHEMA (default; {tenant_schema} substitution) or DISCRIMINATOR (compiler wraps the source in a subquery with WHERE tenancy_column = $tenant).
  • Row-level securityCube.security_sql AND-composes with tenancy inside the isolation subquery; {ctx.X} placeholders bind as parameters, never inline as literals.
  • MCP-readyCatalog.prompt() produces the planner system-prompt fragment; semql-mcp wraps it as a server.
  • Pluggable backendsBackendStrategy Protocol lets out-of-tree Snowflake / BigQuery adapters slot in without forking the compiler.

Philosophy

See PHILOSOPHY.md at the repo root. Highlights:

  • Correct SQL, not optimal. The query planner is the database's job.
  • The emitted SQL must read like something a human could have written.
  • compile() fails at the first problem; validate() collects them all.
  • The catalogue is data; reflection isn't an afterthought.

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

semql-0.2.0.tar.gz (99.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

semql-0.2.0-py3-none-any.whl (111.9 kB view details)

Uploaded Python 3

File details

Details for the file semql-0.2.0.tar.gz.

File metadata

  • Download URL: semql-0.2.0.tar.gz
  • Upload date:
  • Size: 99.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for semql-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f7ae923fb95b8ae5cd7ae0c5b73366fad8bffcbee74e10971b9daa2d24fba2eb
MD5 f7fbe83a7faa2fed7affa8325905e58d
BLAKE2b-256 7f1d6ff973a1473824ecd3d260e6d35b2da7ab0410e36642e72cae7d4bd0bd45

See more details on using hashes here.

File details

Details for the file semql-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: semql-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 111.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for semql-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 05596a32b3c5e01beec0662f0b4aff2a6c7b562132b9bed8408cf850bccdeae2
MD5 7b967d02ac235c898128a131ddc35811
BLAKE2b-256 b6beb9f01f9814f964f3e12be080539677082337377bd213e090b4a1456b6e53

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page