Inspiration

The worst data bugs I have seen never threw an error. The query ran, the dashboard loaded, and the number was wrong. A join quietly repeated rows and a total doubled. A comparison against a null dropped half the orders. Someone added a price to a weight. An email column ended up in a public export.

SQL treats all of these as valid. I wanted a language where they are not.

What it does

Quern is a small language for writing data pipelines. You declare your sources once, with a little more than a column type: which columns are keys, which may be null, which hold personal data, and what unit a number is in. The compiler carries those facts through every join, filter and aggregate, and stops when a step would break one.

  • Keys and grain: a join that can multiply rows has to say so, and summing a column whose rows a join has repeated is an error.
  • Nulls: a value that may be missing has to be handled before it is compared, filtered on, joined on or added up.
  • Units: dollars cannot be added to grams, or compared with a bare number.
  • Personal data: a column marked as personal stays marked through every step, and cannot be published to a public file unless it is hashed.

A program that passes compiles to plain DuckDB SQL, one readable step per line of the pipeline. A program that fails gets a diagnostic with a code, the exact position and a hint for the fix. There are 23 diagnostics.

The playground runs the whole compiler in the browser. It has five pipelines that SQL would run without complaint, each with the error Quern gives and a one-click fix. You can edit the code and the data, and add your own CSV file.

How I built it

The compiler is TypeScript with no runtime dependencies: a lexer, a recursive-descent parser that recovers after an error, and a type checker that produces a typed program. Two back ends read that program. One generates SQL. The other is an interpreter that runs it directly.

I built two back ends on purpose. The tests run every test pipeline through both, the SQL on DuckDB and the interpreter in memory, over the example data and 40 random data sets, and the rows have to match. That is how I know the generated SQL means what the language says it means.

The playground is React and Vite, hosted on GitHub Pages. Because the compiler is a pure function from text to diagnostics and SQL, the same code runs in the browser. The page makes no network requests.

Challenges I ran into

  • Knowing when a sum is safe. After a join, some columns are repeated and some are not. Rejecting every sum after a join would make the language useless, so the checker tracks which side of the join each column came from and which columns still identify a row.
  • A false warning. The compiler warned about possible ties in an ordering that could not have any. Fixing it meant tracking which columns are determined by a key, not only which columns are keys.
  • A bug the tests found in my own compiler. The generated SQL read CSV columns by position while the interpreter read them by name. Every hand-written test passed. The comparison between the two back ends caught it.
  • Types are only as good as the declarations. If a file has a duplicate key, everything the checker concluded is wrong. So the interpreter refuses a file that breaks its declaration, and there is a command that generates SQL to test the declarations against real data.

Accomplishments that I'm proud of

  • Every one of the 23 diagnostics has a test, and the suite fails if a new code is added without one.
  • The checks page in the playground does not show canned results. Each verdict is computed by the compiler when the page loads.
  • The limits are written down. The README says what the checker does not catch.

What I learned

Most of the work in a type system is deciding what to reject without being annoying. The four rules are simple to state. Making them precise enough that correct pipelines still compile took most of the time.

I also learned how much a second implementation is worth. An interpreter I wrote as a test oracle found a defect that none of my ordinary tests would have.

What's next for Quern

  • Editor support for .quern files, with errors shown as you type.
  • More SQL targets than DuckDB.
  • Compound units such as dollars per kilogram, and a fixed-point decimal type.
  • Sources other than CSV files.

Built With

Share this project:

Updates

Submission history