Skip to content

HQL Book

A small question can require a surprising amount of machinery. The orders are in a database, the corrections arrive as events, and the explanation for an exception is in somebody's notes. Before we can ask what happened, we write a query, a script, and a little program to reconcile their answers. The little program tends to become permanent.

HQL begins with this everyday inconvenience. It is a strongly typed language for querying and transforming data in records, tables, files, streams, and vaults of linked Markdown documents. Its central device is a pipeline: each step receives a value and produces another, and the types describe how the steps fit together. The program is checked before it runs; data arriving from outside is checked at the boundary. These checks cannot tell us whether we asked a sensible question. They can save us from debugging an answer to a question the program could never ask correctly.

Documents deserve a place in this arrangement. A note has text, but it may also have a date, an author, links, and claims about other notes. We can query that structure, follow the links, and put the results back into a document. When a language model enters the picture, the same concern for explicit inputs and inspectable results remains. The later chapters explore a design in which context, prompts, and runs become values that programs can work with. Generating a program cheaply makes understanding what it does more valuable.

This book develops these ideas through programs small enough to examine. Read it in order on a first visit: expressions lead to types, types to pipelines, and pipelines to systems whose parts need not live in the same place. Run the examples, change an input, and try to predict the result before asking the machine. A type error is a useful result of this exercise.

The five parts follow that progression. The language introduces values, functions, queries, types, and errors. Data flows connects checked pipelines to files, tables, topics, and other systems. Knowledge and AI turns to documents, graphs, retrieval, and the design of agents and connected vaults. The standard library supplies statistical, random, encoding, and cryptographic operations. Extending HQL shows how functions implemented in Rust become libraries that HQL programs can import.

Following along

Install hql, clone the repository for the reading-club vault, and run each program with hql eval:

git clone https://github.com/ewiger/hql && cd hql
hql --vault examples/reading-club eval 'cards | size'

The reading-club vault, examples/reading-club/, is a small reading club: seven books, five authors, the links between them, one claim about which book inspired which, and a librarian.

The programs that reach a server — Kafka, PostgreSQL, NATS, a lakehouse — run in the hql-demo repository. Each of its cases is a vault with the Docker Compose file that starts what it needs and a run.sh that compares the output with the expected one. With Docker and hql on the PATH:

git clone https://github.com/ewiger/hql-demo && cd hql-demo
./run-all.sh

The book distinguishes the implemented language from its design direction. Chapter 18 and chapters 25 to 27 describe APIs and object stores, the ai module, and the direction beyond one vault; these are design, not features of the current release. The marked sections of chapters 14, 15, 19 and 37 also describe design. Chapter 14's companion on planning separates the local backend from future execution strategies. Runnable examples show their expected output; the programs in chapters 16, 17, 19 and 20 that reach a server are checked in the book and executed in the hql-demo repository.

Part I: The language

  1. An AI-oriented language — What an AI-oriented programming language could look like? What properties it should have? And where one goes with it when it comes to mixing knowledge, natural language, with both structured and unstructured data. As well as evidence of what is true and false, and how to reason about things in an almost precise logical way.
  2. Hello world — the first program, how to run it, and type validation.
  3. Data types — the basic types, Data for JSON-shaped trees, and Option for a missing value.
  4. Collections — lists, sets and maps, and the steps they share.
  5. Functions — lambdas, declared functions, the two body forms = expr and { … }, generic functions, aliases, and partial application.
  6. Modules — modules of your own, imports, receiver modules, public and private declarations, how a name resolves, and the packages modules come in.
  7. SQL-like Queries — where, order by, group by and the aggregates.
  8. Streams that do not end — generators, laziness, and batches.
  9. Writing results out — the Format type: an output format, such as Json or Markdown, is a value.
  10. I/O — endpoints, sources and sinks, formats, files, the network, and hql as a command-line tool.
  11. The type system — records, unions and match, results, subtypes and narrowing, generics, receiver calls, operators and traits.
  12. Advanced data typing — turning loose data into typed data, TypeContract and DataContract, and validation.
  13. Errors — failures kept as data, diagnostics and the run's diagnostic channel, strict and collect.
  14. Working with — the commands, the reports, and what to do when something is wrong.

Part II: Data flows

  1. ETL pipelines — the wiring as a program: | and ., the three phases, logical steps and effects, hql in a shell pipe, the shop whose orders the rest of Part II follows, and partitioning, schedules and backends. Its companion, Logical steps and physical plans, follows a checked program into local execution and the design direction for parallel workers, SQL translation and distributed execution.
  2. Endpoints — one model for every system: a file by path or by id, source, tap, target and sink, the kind that states the type, secrets in the environment, and transports.
  3. Tables and topics — the shop's orders in a SQLite table, a PostgreSQL table and a Kafka topic, what a write promises, and a topic whose messages are not all orders.
  4. Subjects and the lakehouse — core NATS notifications, a JetStream log and a durable consumer, and bronze, silver and gold Delta tables.
  5. APIs, webhooks and stores — a paginated REST API, a GraphQL catalogue, a webhook, an archive in object storage, and the systems the transport families reserve.
  6. OLTP — the shop's transactional side: its whole day in one checked file, where its mistakes are found, a replay as the same file, and streams against tables.
  7. OLAP — the history those transactions leave: bronze, silver and gold, the grain of a row and its keys, deduplication, a table kept up to date by key, and incremental loads.

Part III: Knowledge and AI

  1. A vault of documents — cards, their metadata, a reading list nobody maintains, and the vault as a namespace.
  2. Links and graphs — following links, building graphs, drawing them.
  3. Finding cards by meaning — rankings, hits, and the evidence behind a score.
  4. Queries inside documents — cells, documents built from them, and cells that maintain a vault.
  5. Agents and runs — agents as cards, context and prompts, tools, and runs kept as values.
  6. Agents inside documents — a document whose cells retrieve evidence, run an agent over the draft they sit in, and remember the answer.
  7. The hyper web — namespaces beyond one tree, providers, retrieval and agents across them, and a reader who asks.

Part IV: The standard library

  1. Statistics — means, spread, quantiles, the mode and correlation, over a list and over a query's column.
  2. Random values — sampling, shuffling, choice, numbers and UUIDs, and a seed that makes them reproducible.
  3. Digests and encodings — digests of text and bytes, the fingerprint of a Data tree, and hex, base64 and the other text encodings.
  4. Keys and cryptography — keys and their formats, secrets that print redacted, signatures, MACs and password hashes, and the encryption and key vault that are design.

Part V: Extending HQL

  1. Functions and declarations — a function with a body in HQL, a declaration whose body is Rust, and how the standard library is built from the second.
  2. Packages and extensions — an import followed from the module it names to the package, the extension and the file that runs.
  3. Writing an extension — a package from an empty directory: the crate, the contract, the Rust functions, values, errors, and functions passed in.
  4. How a library is loaded — static and dynamic libraries, the one symbol hql calls, and why a library fits one hql and one compiler.
  5. Installing packages — hql package, what an installed package is, a package under development, and the settings of .hql/config.toml.
  6. The registry and publishing — official and known packages, version ranges and exact versions, and publishing a crate.
  7. The ml package — datasets, models as values, prediction and metrics, from the first official package.