HQL¶
A strongly typed expression and query language.
In HQL a type is a data contract. The checker tests a program against its contracts before it reads any data, and every read tests each record against them again. One language reads and writes files, PostgreSQL tables, Kafka topics, NATS subjects, lakehouse tables and HyperMarkDown documents.
cargo install hql
The current release is 0.8.0: child processes, scripts and HyperMarkDown 0.1.0.
type Book { title : String, year : Int, rating : Int }
books = [
Book(title = "Dune", year = 1965, rating = 5),
Book(title = "Emma", year = 1815, rating = 3),
Book(title = "Beloved", year = 1987, rating = 4),
]
books
| filter(b => b.rating >= 4)
| sort(b => b.title)
| map(b => b.title)
["Beloved", "Dune"]
type Book { title : String, year : Int, rating : Int }
books = [
Book(title = "Dune", year = 1965, rating = 5),
Book(title = "Emma", year = "1815", rating = 3),
]
books | map(b => b.title)
check error[type-mismatch]: `year` of `Book` is Int, and this is String
--> playground.hql:5:33
|
5 | Book(title = "Emma", year = "1815", rating = 3),
| ^^^^^^
type EventData : DataContract = {
id : Int
kind : String
customer : String
amount : Decimal
at : String
require amount >= 0 as non_negative
require ["order_placed", "order_shipped", "payment_failed"]
| contains(kind) as known_kind
}
type Event { id : Int, kind : String, customer : String,
amount : Decimal, at : String }
type Rejected { id : Int, message : Data, rules : List<String> }
tap("order_events_raw")
| validate(EventData)
| fork {
clean =
oks()
| map(e => Event(id = e.id, kind = e.kind, customer = e.customer,
amount = e.amount, at = e.at))
| sink("order_clean"),
quarantine =
errs()
| map(e => Rejected(
id = match e.value.id { some(Int n) => n, _ => 0 },
message = e.value,
rules = e.violations | map(v => v.rule)))
| sink("order_quarantine"),
}
order_clean ids 1, 2, 4, 6
order_quarantine id 3 EventData.amount.type
id 5 EventData.customer.missing
id 7 EventData.non_negative
id 8 EventData.known_kind
cards
| filter(c => c.metadata.status == "todo")
| sort(by = c => c.title)
| map(c => c.title)
["Leaves of Grass", "The Common Reader", "The Hobbit"]
One program, end to end¶
A source gives values of a type. Each step is checked against that type before the run. A target receives what the last step gives.
Sources
- Kafka topic
- NATS subject
- PostgreSQL table
- JSON file
- HMD vault
Contract
type EventData : DataContract
checked before the run, enforced on every read
Pipeline
validateforkmapsink
Targets
- PostgreSQL table
- Delta table
- Kafka topic
- file
Checked before it runs¶
hql check reports a wrong field, type or step with its line and column.
Nothing is read until the program checks.
Batch and stream¶
source reads a table or file whole, as a Seq<T>. tap reads a topic as
a Stream<T>. The same steps apply to both.
Data and documents¶
A HyperMarkDown vault is a source too: its cards, their metadata and the links between them are typed values.
Endpoints¶
An endpoint is named in .hql/config.toml and used by name in a program.
- SQLite table
- PostgreSQL table
- Kafka topic
- NATS subject
- JetStream stream
- Delta / Spark lakehouse
- Files JSON, CSV, text
- HMD vault documents
- REST, webhooks design
How HQL compares¶
| What it is | Types over data | Unbounded streams | Sources and targets | |
|---|---|---|---|---|
| HQL | Expression and query language, one binary | Record types are contracts, checked before the run and on every read | Stream<T> beside Seq<T> |
Files, SQLite, PostgreSQL, Kafka, NATS, Delta, Markdown vaults |
| SQL | Query language of a relational database | Column types of the database's tables | Not in standard SQL | The database it runs in |
| PRQL | Pipelined query language that compiles to SQL | Those of the target database | No | The database the SQL runs in |
| dbt | SQL models with Jinja templates, built in a warehouse | Model contracts checked at build; data tests run after | No | One warehouse per target |
| Spark SQL | SQL and DataFrames on a Spark cluster | Schemas resolved when a query is analysed on the cluster | Structured Streaming | Many connectors |
| fquery | Graph query engine, a Python library | Python objects of the host program | Not documented | Maps queries to SQL and Malloy |
| Bend | General-purpose functional language | Not a data language | — | — |
Why a language for this, and where it is going: An AI-oriented language.
The book¶
- I The language Chapters 0–13 · types, collections, functions, modules, queries, streams, I/O, errors
- II Data flows Chapters 14–20 · pipelines, endpoints, tables and topics, OLTP, OLAP
- III Knowledge and AI Chapters 21–27 · vaults, graphs, retrieval, documents, agents
- IV Standard library Chapters 28–31 · statistics, random, digests, cryptography
- V Extending HQL Chapters 32–38 · extensions, packages, the registry
From the blog¶
A card in full colour: HyperMarkDown for VS Code 0.7.0¶
HyperMarkDown for VS Code 0.7.0 colours HQL, colours a cell in any language, accepts the 770 language names of GitHub Linguist on a fence, and carries a language server for links. This post is a card itself, and shows its own source.
Run the server programs¶
The Kafka, PostgreSQL, NATS and lakehouse programs run in the hql-demo repository. Each case starts its servers with Docker Compose and compares the output with the expected one.
git clone https://github.com/ewiger/hql-demo && cd hql-demo
./run-all.sh
Community¶
Ask questions, share programs and follow releases on r/hqlang. Bugs and feature requests go to GitHub issues.