# Querying

Every table of the lake is a view in DuckDB, so any question about a repository is a SQL
query. This page lists the query commands, the macros that save the common joins, the
commands that answer without SQL and the tables.

## The query commands

```sh
ahto query tables                            # every table with its files, rows and bytes
ahto query schema commits                    # the columns of a table, with what each one means
ahto query sql "describe commits"            # the columns of a table, as DuckDB sees them
ahto query sql "select author_name, count(*) as commits
                from commits group by 1 order by 2 desc limit 10"
ahto query sql -o csv "select commit_oid, message from commits" > commits.csv
ahto query file report.sql                   # run every statement of a file, print the last result
ahto query explain "select ..."              # the plan of a query, with rows and time per step
ahto query export --format parquet --out authors.parquet "select ..."   # json, csv, parquet or arrow
```

`ahto query schema` with no table prints every table and column with its description.

## Macros

Four macros save the joins most questions need:

| Macro | Gives | Columns |
|---|---|---|
| `files_at(tree_oid)` | the files under a root tree, as `git ls-tree -r` | `path`, `mode`, `kind`, `oid` |
| `head_tree(repo_id)` | the root tree of the default branch in the last crawl (one value) | |
| `head_files(repo_id)` | `files_at(head_tree(repo_id))` | `path`, `mode`, `kind`, `oid` |
| `commits_of(repo_id)` | the commits a repository reaches | the columns of `commits` |

`kind` is `file`, `exec`, `symlink`, `gitlink` or `other`; `oid` is the blob, or the commit
of a gitlink; `path` is NULL when it is not valid UTF-8. For example:

```sh
ahto query sql "select path from head_files((select repo_id from repositories limit 1))
                where path like '%.go' order by path"
```

SQL sent to the gateway (the SQL workbench, functions, Arrow) refuses `files_at` and
`WITH RECURSIVE`: DuckDB does not hold a recursive walk to the memory limit, and one old
Linux tree took more than 5 GB in 5 seconds. Use `head_files`, or the files API at a
commit, which walks one level per query. The local `ahto query` commands still run them.

## Without SQL

```sh
ahto files                        # the files on the default branch: path, mode, object id, language
ahto files --at v1.2.0 cmd/       # under cmd/ at a tag, branch or full commit or tree id
ahto history README.md            # the commits that changed a file or directory, newest first
ahto inspect HEAD                 # one commit, tree, blob or tag, like git cat-file -p
ahto inspect --ast HEAD:main.go   # the syntax tree of a source file
```

`history` follows first parents, like `git log --first-parent --no-renames -- <path>`.
`files` and `history` pick the only repository in the lake, or take `--repo`.

Every command prints a table by default; `-o json`, `-o jsonl` and `-o csv` give machine
output.

## Functions

`ahto function list` shows the functions over the lake: built-ins such as `hot_files`,
`lone_maintainer`, `largest_functions`, `todo_comments`, `top_imports` and
`security_fix_commits`, and the WebAssembly functions in the lake's `functions` folder.
`ahto function call <name> [key=value...]` runs one and prints its rows:

```sh
ahto function list --category quality
ahto function call hot_files path=src/ limit=20
```

A wasm function is a folder with a `function.yaml` and its module; `examples/functions`
has one that counts TODO and FIXME per blob. `ahto function new <name>` writes a new one that
builds without an ahto checkout, and `ahto function test <folder>` runs it once without adding
it, keeping a run or filling the cache (`run` is another name for `call`):

```sh
ahto function new line_count && cd line_count && go mod tidy && go test ./...
GOOS=wasip1 GOARCH=wasm go build -buildmode=c-shared -o line_count.wasm .
ahto function test . --rows 'SELECT DISTINCT blob_oid FROM file_versions LIMIT 10'
```

Over the gateway the same try is `function_test`, and `function_add` also takes a module by
URL (`fetch`: the manifest, an https `module_url` and its `module_sha256`), so a remote client
does not send the module in base64. The design is in
the functions design document.

## The tables

| Table | One row per |
|---|---|
| `repositories`, `remotes` | repository, and each URL it was crawled from |
| `crawl_runs`, `snapshots` | crawl, and each distinct state of a repository's refs |
| `refs`, `ref_updates` | ref in a snapshot, and each time a ref moved |
| `commits`, `commit_parents` | commit (author, committer, dates, message), and each parent link |
| `repo_commits` | commit a repository reaches |
| `tags` | annotated tag |
| `trees`, `tree_entries` | directory, and each file or directory in it |
| `blobs` | file content (size and line count; the bytes stay in the mirror) |
| `blob_contents` | text of a file that is valid UTF-8, has no NUL byte and is at most 1 MiB |
| `changes` | file a commit added, changed or deleted, against its first parent |
| `file_versions` | file at a version: language and vendor, generated, test and docs flags |
| `submodules`, `languages` | submodule in `.gitmodules`, and each known language |
| `tree_files` | file under the root tree of a crawled ref tip (a read model) |
| `path_changes` | row of `changes`, sorted by path, with its author, subject and blob facts (a read model) |

The read models are derived from the tables above, so the code browser reads one table for
a tree or a history. A crawl brings them up to date after it publishes, and
`ahto storage derive` does the same on its own.

The parsing tables (symbols, imports, calls and the syntax trees) are in
[parsing](/docs/parsing), and the advisory tables in [security](/docs/security).