Open source, MIT

SQL with variables.

ASQL lets you give a query a name, give a value a name, and split a project into files that read each other. It compiles to ordinary SQL and runs on the database you already use.

Get it on GitHub
pip install git+https://github.com/VuongNM/asql

Why it is built this way

Two reasons: to organize SQL code better, and to help you write better SQL. Most of what ASQL does, or refuses to do, comes from one of them.

Reason 1

Organizing the code

SQL has no good way to reuse a piece of a query. A CTE only lives inside one statement. A number like “a big order is 10000 or more” can't be named at all. So the same lines get pasted into every report, and when the rule changes you go looking for every copy.

plain SQLthe rule, pasted twice
-- revenue.sql
SELECT customer_id, sum(amount_cents)
FROM orders
WHERE status IN ('paid', 'shipped')
GROUP BY customer_id;

-- big_spenders.sql
SELECT customer_id, count(*)
FROM orders
WHERE status IN ('paid', 'shipped')
  AND amount_cents >= 10000
GROUP BY customer_id;
shop/Orders.asqlthe rule, once
CONST LIVE = ('paid', 'shipped');
CONST BIG_ORDER = 10000;

LET counted AS
SELECT * FROM orders WHERE status IN LIVE;

LET big AS
SELECT * FROM counted
WHERE amount_cents >= BIG_ORDER;

SELECT * FROM counted;
reports/BigSpenders.asqlreads it by name
SELECT customer_id, count(*) AS big_orders
FROM shop.Orders.big
GROUP BY customer_id;

The marked words are ASQL. The rest is the SQL you already write.

The path of a file is its name

shop.Orders.big is the LET big inside shop/Orders.asql. We did that on purpose. There is no list of models to keep up to date and no ref() to call. When you read a name you already know which file to open.

Why LET, when SQL has WITH?

WITH belongs to one statement. A LET belongs to the file, so the statements after it can read it, and so can other files. You can still write WITH inside any query.

Your database gets plain SQL

None of this reaches the database. asql compile prints the one query it will run, and you can paste that anywhere.

asql compile reports.BigSpenderswhat runs
WITH
  shop__Orders__counted AS (
    SELECT * FROM orders
    WHERE status IN ('paid', 'shipped')
  ),
  shop__Orders__big AS (
    SELECT * FROM shop__Orders__counted AS counted
    WHERE amount_cents >= 10000
  )
SELECT customer_id, count(*) AS big_orders
FROM shop__Orders__big AS big
GROUP BY customer_id;

Reason 2

Writing better code

SQL tells you about mistakes late. A misspelt column fails when the job runs at night. Some mistakes never fail. They give you wrong numbers.

Mistakes are found before anything runs

asql check reads the whole project and reports them with the file and line. It runs no query.

The second error here is the kind that never fails by itself. The query said WHERE status = STATUS, which would have compared 'paid' with 'paid' and returned every row.

Why there are no templates

ASQL can check this much because a .asql file is still SQL. A template, like Jinja, is not SQL until something renders it, so nothing can check it before then. Here an ordinary SQL parser reads each file, and from that ASQL knows the columns and types of every query.

terminal
$ asql check
error: reports/Revenue.asql:1: there is no column
named amount_cent. Did you mean amount_cents?
(counted has id, customer_id, status,
amount_cents, created_at)

error: reports/Paid.asql:2: status is a column
of orders and also the constant
reports.Paid.STATUS. Names ignore case, so the
constant's value would be used for both.

Bad data is stopped the same way

Tests go at the bottom of the file they test. asql build writes the new table next to the old one and runs the tests on the new one. If a test fails, the old table stays where it is.

Two more rules come from the same idea. ASQL will not replace a table it did not build, so a file called orders.asql can't overwrite your raw orders table. And when a name could mean two things, it stops and tells you instead of picking one.

EXPECT UNIQUE (ride_id);
EXPECT minutes > 0;
EXPECT station_id IN (SELECT id FROM stations);
terminal
$ asql build
  FAILED   Rides            EXPECT station_id IN (...):
                            1 row breaks it
  skipped  BusiestStations  needs Rides
  skipped  LongRides        not run
0 built, 1 failed, 2 skipped in bikes.sqlite
Old tables were left as they were.

Where it stands

Version 0.1. About 5,000 lines of Python on top of sqlglot.

Works today

  • SQLite, DuckDB and Postgres, tested end to end
  • Tables, views and incremental tables
  • Daily schedules: PERSIST orders DAILY AT 07:00
  • Column lineage, a formatter, generated docs, an interactive session

Not yet

  • BigQuery has a connector, but it has not finished a build on a live project
  • No Snowflake connector. ASQL can print Snowflake SQL but can't run it
  • Tables build one at a time
  • Nobody has run it in a large production setup

Try it

The tutorial takes about ten minutes. It builds three tables on SQLite and breaks one on purpose.

Start the tutorial