# SQL

Source: https://docs.rushdb.com/engine/languages/sql/

<!-- Generated from docs/manual/languages/sql.md in the RushDB engine repository by scripts/sync-engine-manual.mjs. Edit it there. -->

# SQL

RushDB accepts relational SQL in the PostgreSQL dialect. Parsing, planning,
optimization, and execution are done by Apache DataFusion over a consistent
snapshot of one tenant. This page explains how records and relationships appear
as tables, which statements are supported, and where the limits are. Use it if
you want to query RushDB from SQL tools, BI clients, or application code that
already speaks SQL.

| Property | Value |
|---|---|
| Status | Stable |
| Enabled | By default (`languages.sql: true`); a `--languages` list must name `sql` to keep it |
| Wire id | `sql` |
| Profile | PostgreSQL-dialect relational SQL (DataFusion 54.1) |
| Access | read and write |
| Parameters | positional (`$1`) and named (`$name`) |
| Transports | Protocol v1 (HTTP, gRPC) and [PostgreSQL wire](../connectivity/postgresql-wire.md) |

## How the graph maps to tables

```mermaid
flowchart LR
  subgraph Graph
    A["Person record\nname: Alice"] -- "KNOWS {since: 2020}" --> B["Person record\nname: Bob"]
  end
  subgraph SQL
    T["table person\n__id | name"]
    E["table rushdb_edges.knows\n__id | __source_id | __target_id | __type | since"]
  end
  A -.-> T
  B -.-> T
  A -. edge .-> E
```

**Record tables.** Every label is a table in the default schema. The table
name is the label name; lookups are case-insensitive, so the label `Person` is
`person`, `Person`, or `PERSON` in SQL. Each table has:

| Column | Type | Content |
|---|---|---|
| `__id` | text | The record's stable id (UUID). |
| one column per property | from the catalog | Every property any record of that label has. |

Column types come from the label's property catalog at the snapshot: the
types its records store under each property (see
[Value types](#value-types)). Mixed types widen the way
JSON values do: integers and floats read as floats, any other mix as text.
Comparing such a text column with a number casts every value to a number, so
a value that is not one fails the statement with `invalid_request` (`22P02`
over pgwire) rather than being skipped. Compare it with text, or skip those
values explicitly: `WHERE TRY_CAST(price AS BIGINT) > 30`. An
array that has only ever been empty takes the element type its property was
defined with. Property columns come in the order the properties were first
created, then `__id`. Building a table reads no record, whatever the label's
size. A record that lacks a property reads as `NULL` in that column.

**Relationship tables.** Every relationship type is a table in the
`rushdb_edges` schema, named after the type in lowercase (`KNOWS` becomes
`rushdb_edges.knows`). Each table has:

| Column | Content |
|---|---|
| `__id` | The relationship id. |
| `__source_id` | `__id` of the source record. |
| `__target_id` | `__id` of the target record. |
| `__type` | The relationship type. |
| one column per property | Relationship properties. |

Relationship tables are read-only. They stream like label tables: resolving
`rushdb_edges.knows` reads the type's columns and row count from the
relationship catalog, without reading a relationship, and a scan reads the
relationships in batches, so there is no relationship cap. A full scan still
reads past the relationships of other types (there is no per-type index yet),
so its cost grows with the graph, not only with the type; a join on
`__source_id` or `__target_id` does not scan (see [Joins](#joins)).

> **Note:** Labels that differ only in letter case (`Person` and `PERSON`)
> resolve to the same SQL table name. Keep label names case-distinct if you
> plan to query them from SQL.

### Value types

| Stored type | SQL type | PostgreSQL wire type | Value |
|---|---|---|---|
| integer | `BIGINT` | `int8` | |
| float | `DOUBLE` | `float8` | |
| string | `VARCHAR` | `text` | |
| boolean | `BOOLEAN` | `bool` | |
| `datetime` | `TIMESTAMP WITH TIME ZONE` (microseconds, UTC) | `timestamptz` | The instant; over the wire at the session's `TimeZone` |
| `localdatetime` | `TIMESTAMP` (microseconds) | `timestamp` | |
| `date` | `DATE` | `date` | |
| `localtime` | `TIME` | `time` | |
| `duration` | `VARCHAR` holding ISO 8601 (`P1DT2H`) | `interval` (`1 day 02:00:00`) | |
| `point` | `VARCHAR` holding PostgreSQL point text (`(13.4,52.5)`) | `point` | A 3D point has no PostgreSQL type: as text it is GeoJSON, and over the wire it fails the statement (`22P02`) |
| array | `LIST` of the element type | the element type's array | |

Compare temporal columns with literals of their type, or with ISO strings,
which DataFusion casts to the column's type; both are pushed down to the
store like other comparisons:

```sql
SELECT name FROM "Event" WHERE day >= DATE '2024-05-01' AND at < '2024-06-01T00:00:00Z'
```

`INSERT` and `UPDATE` store a `DATE`, `TIME`, `TIMESTAMP` or
`TIMESTAMP WITH TIME ZONE` value as the matching type. Over the PostgreSQL
wire, a date before year 1 is written in text as a signed ISO 8601 year
(`-0043-03-15`) where PostgreSQL writes `0044-03-15 BC`; binary results
carry the exact value.

A read that compares a column with a text no row of that table holds
(`WHERE city = 'berlin'` when the stored value is `Berlin`) still runs, and
the response carries `warnings` with the stored spelling and the columns
that do hold the text; see [Catalog check](../query/search-queries.md#catalog-check).

## Reading data

DataFusion provides the full `SELECT` surface: filtering, projection, joins,
common table expressions, window functions, aggregation, `ORDER BY`, and
`LIMIT`/`OFFSET`.

```sql
SELECT name FROM FACTION ORDER BY name;
```

```sql
SELECT founded_year_bby < 0 AS ancient, COUNT(*) AS n
FROM FACTION
GROUP BY ancient;
```

Window functions and CTEs:

```sql
WITH ranked AS (
  SELECT name, department, salary,
         ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank
  FROM person
)
SELECT name, salary, rank
FROM ranked
WHERE rank = 1
ORDER BY salary DESC;
```

Traverse relationships by joining a record table to a relationship table:

```sql
SELECT p.name, e.since
FROM person p
JOIN rushdb_edges.knows e ON p.__id = e.__source_id
WHERE e.since >= 2020
ORDER BY e.since;
```

### Joins

A join on a key the store can find rows by is answered by lookups instead of
a scan: a label table joined on `__id` fetches each matching record by id,
and a relationship table joined on `__source_id` or `__target_id` fetches
each record's relationships of that type through its adjacency. The join
reads only the rows that match and holds one batch at a time, so its cost
follows the other side of the join, not the table.

A chain of inner joins is ordered from its smallest table outward, using
the store's exact counts: a label's record count, a relationship type's
count, and the planner's count for a filter pushed to a label (an indexed or
dictionary-counted condition). Written in any order,

```sql
SELECT r.stars, b.name
FROM review r
JOIN rushdb_edges.has_business e ON e.__source_id = r.__id
JOIN business b ON b.__id = e.__target_id
WHERE b.is_open = 0
ORDER BY r.stars LIMIT 100;
```

reads the closed businesses, their `has_business` relationships through
adjacency and then those reviews by id, never every review. `EXPLAIN` shows
`LookupJoinExec` for each lookup. Joins on other columns, outer joins and
joins with extra conditions run as `DataFusion`'s hash joins.

A query with `ORDER BY ... LIMIT k` reads first only the columns it filters,
joins, orders and deduplicates by. A label's other columns (a long `text`,
say) are fetched by `__id` for the k rows it returns, since a record's
columns are fixed by its id. `DISTINCT` with `ORDER BY ... LIMIT k` keeps the
k best distinct rows as they stream (`DistinctTopKExec`) instead of every
distinct row. Always give a search a `LIMIT`: without one, every match is read
in full and returned.

Search a text index with the `text_search` table function, which returns
`(id, score)` rows best first; join them to a label table on `__id`:

```sql
SELECT d.title, t.score
FROM text_search('docs', 'quick fox', 20) AS t
JOIN doc d ON d.__id = t.id
ORDER BY t.score DESC;
```

See [Full-text search](../indexes-and-search/full-text-search.md#from-statement-languages).

`EXPLAIN` is accepted and returns DataFusion's plan.

### Large labels

A label table streams its records, so a label of any size can be read.
Filters use the same [query planner](../indexes-and-search/query-planning.md)
as native queries:

- A `column = literal`, `>=`, `<=`, `>` or `<` condition on a property with an
  ordered index (and `__id = '…'`) narrows the records read to the index's
  candidates. Conditions combined with `AND` are intersected; other
  conditions beside them are checked on those candidates.
- When the index answers a filter exactly (`=` on a property that holds one
  kind of value, or `>=`/`<=` on a numeric one), no record is read for it:
  `SELECT count(*) FROM "User" WHERE status = 'active' AND city = 'Oslo'`
  counts index entries.
- A filter with no usable index walks the label, like a PostgreSQL
  sequential scan: on millions of records it takes minutes. The request's
  time and memory bounds stop it, and a session can refuse such filters
  outright with `SET disallow_full_table_scans = on` (or the server with
  `unindexed-walk-max-owners`); the statement then fails with
  `invalid_request` naming the property to index, as native queries do.
  See [request bounds](../architecture/resource-governance.md#request-bounds).
- `SELECT count(*)` with no filter reads the label's record counter, and a
  `LIMIT` without a filter stops after its rows.

### Parameters

Bind values instead of concatenating them into the statement. Positional
placeholders take a JSON array, named placeholders take a JSON object:

```json
{
  "protocolVersion": 1,
  "operation": {
    "kind": "query",
    "query": {
      "kind": "statement",
      "language": "sql",
      "statement": "SELECT name FROM person WHERE salary > $1 ORDER BY name",
      "parameters": [130000]
    }
  }
}
```

```json
{
  "kind": "statement",
  "language": "sql",
  "statement": "SELECT name FROM person WHERE department = $department ORDER BY name",
  "parameters": { "department": "research" }
}
```

Parameter values must be scalars or arrays of a single type. Object-valued
parameters are rejected.

## Writing data

`INSERT`, `UPDATE`, and `DELETE` are supported against record tables. Send them
with `mutate.statement`:

```bash
curl -s http://127.0.0.1:3000/protocol/v1/execute \
  -H "Authorization: Bearer $RUSHDB_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{
    "protocolVersion": 1,
    "operation": {
      "kind": "mutate",
      "mutation": {
        "kind": "statement",
        "language": "sql",
        "statement": "UPDATE person SET salary = salary + $increment WHERE name = $name",
        "parameters": { "increment": 5000, "name": "Alice" }
      }
    }
  }'
```

```sql
INSERT INTO person (name, department, salary) VALUES ('Dora', 'research', 90000);
DELETE FROM person WHERE salary < 100000;
```

How DML is applied:

1. DataFusion evaluates the statement against a private, mutable copy of the
   target table.
2. RushDB compares the result with the original rows by `__id` and builds
   one change set (records created, fields changed, records deleted).
3. The change set is published as one atomic commit, but only if the tenant
   has not changed since the statement's snapshot. Otherwise the statement
   fails and nothing is published. Retry it. Because of this check, DML is
   best suited to moderate write rates; for high-throughput loads, batch
   rows into fewer statements or use the native ingest and write operations.

Rules:

- Exactly one statement per request. Multi-statement batches are rejected.
- The target must be an existing record table, written unqualified
  (`person`, not `rushdb_edges.knows`). A label is created by its first
  record, which you write through the native API, ingest, or a graph
  language.
- Columns must already exist in the table (that is, some record of the label
  already has the property). Add new properties with the native API.
- `__id` is stable. An `INSERT` may supply a UUID in `__id`; if omitted, a
  UUIDv7 is generated. An `UPDATE` that changes `__id` fails.
- `UPDATE` with joins, and `DELETE` over several tables, are rejected.
- Relationship tables cannot be written. Create and delete relationships with
  the native API or a graph language.

## Index DDL

The SQL frontend recognizes index statements before DataFusion sees them. Send
them with `mutate.statement` over Protocol v1:

```sql
-- Ordered index: equality, IN, ranges, and prefix filters on one property
CREATE INDEX item_price ON Item(price);

-- Unique index: a second record of the label with the same value is refused
CREATE UNIQUE INDEX user_email ON "User"(email);

-- Full-text (BM25) index; IMMEDIATE makes searches read their own writes
CREATE INDEX note_body ON Note(body) USING TEXT IMMEDIATE;

-- Vector index (SIMILARITY defaults to cosine; DIMENSIONS is required)
CREATE VECTOR INDEX note_embedding ON Note(embedding) SIMILARITY cosine DIMENSIONS 768;

-- Drops a declared index of that name, otherwise a vector index
DROP INDEX item_price;
```

The label is resolved like a table name: case-insensitively, to the stored
label a `SELECT` on that name reads, so `CREATE INDEX s_region ON supplier
(region)` indexes the label `SUPPLIER`. A label no record has yet is kept as
written.

The response `status` carries the created vector index's name. See
[Indexes](../indexes-and-search/indexes.md) for how indexes are built and used.

> **Note:** Index DDL is a mutation: send it as `mutate.statement` over HTTP
> or gRPC. Over the PostgreSQL wire protocol it is recognised and run as a
> mutation automatically.

## Transactions

Each SQL statement commits atomically on its own. There are no `BEGIN`,
`COMMIT`, or `ROLLBACK` statements in SQL, and SQL statements cannot be staged
in a [multi-request transaction](../query/transactions.md). If you need to
change several records atomically across statements, use the native
transaction operations or a single DML statement that covers all of them.

## Limits

| Limit | Value | Error |
|---|---|---|
| Writes one `INSERT`, `UPDATE` or `DELETE` plans (ids and new values) | the request's response budget | `resource_exhausted` |
| Result rows per statement | 100,000 (`SQL result exceeds the 100000 row safety limit`) | `resource_exhausted` |
| Result bytes | the request's response budget, the body limit (16 MiB) (`response would exceed the response size limit`) | `resource_exhausted` |
| Working memory of one statement (sorts, groups, joins, top-k) | `query-memory-max` (256 MiB; 256 MiB too when set to `0`) or the request's smaller cap (`work_mem` over pgwire), whatever the response budget (`query working set exceeded`) | `resource_exhausted` |
| Working memory of all statements together | the server's SQL share of the phase budget, charged as each statement's memory grows (`the shared SQL memory budget cannot hold …`) | `resource_exhausted`, retryable |
| Time | `request-timeout-ms` (none), or the request's shorter deadline (`statement_timeout` over pgwire) | `cancelled` |
| Records an unindexed filter walks | none, unless `unindexed-walk-max-owners` or the request sets a limit (`disallow_full_table_scans` over pgwire) | `invalid_request` |

Reads of label tables stream and have no record cap (see
[Large labels](#large-labels)). DML statements read the same way: an `UPDATE`
or `DELETE` finds its rows like a `SELECT` with the same `WHERE` (planned on the
indexes), and only the rows it touches are held until the single atomic commit,
so a statement can change any number of records within the request budget.
Relationship tables stream too; a join with them is bounded by working
memory, not a row count. A join that looks relationships up by endpoint
reads them a batch at a time, so an endpoint with millions of relationships
is joined in bounded memory and stops at the deadline. For deep traversals, the
[native query document](../query/search-queries.md) walks relationships
without joins.

## Under a data-access policy

When the tenant's [data-access policy](../operations/security.md) is
enforced, SQL `SELECT` sees only what the policy allows: a table for each
allowed label, a column for each of its visible properties, and a
`rushdb_edges` table for each relationship type that is not denied, holding
only relationships whose source and target labels are both allowed. A hidden
table or column fails exactly like one that does not exist (`not_found`), and
no error or suggestion names it. `text_search` is not available under an
enforced policy, and SQL DML and the other statement languages still fail
with `unauthorized`.

## Errors

| Situation | Protocol error | SQLSTATE over pgwire |
|---|---|---|
| SQL not enabled | `language_disabled` | `0A000` |
| Unknown table (the message suggests close names: `Did you mean 'Patient'?`) | `not_found` | `42P01` |
| Unknown column (the message names the valid or closest columns) | `not_found` | `42703` |
| Unknown function | `not_found` | `42883` |
| A value that does not cast (a mixed-type column compared with a number), division by zero, overflow | `invalid_request` | `22P02`, `22012`, `22003` |
| Unsupported DML form (multi-statement, relationship table, non-DML on the write path, `RETURNING`, `ON CONFLICT`, `ORDER BY`/`LIMIT` in DML) | `unsupported` | `0A000` |
| A statement that is neither a read nor DML/index DDL (`SET`, `PREPARE`, `LISTEN`/`NOTIFY`, cursors, `COPY`, `CREATE TABLE` and other table DDL), multiple statements, or a type or feature DataFusion does not implement (`::jsonb`) | `unsupported` | `0A000` |
| Limit exceeded (the message names the limit: result rows, response size or working set) | `resource_exhausted` | `53200` |
| The SQL working memory shared by concurrent statements is in use (retryable) | `resource_exhausted` | `53200` |
| Cancelled or deadline elapsed | `cancelled` | `57014` |
| Syntax error, a statement that does not plan (ambiguous name, operand types that do not combine), or DML sent through `query.statement` | `invalid_request` | `42601` |
| A filter without a usable index over more records than the server's or the request's unindexed-walk limit | `invalid_request` | `42601` |
| A DML statement lost to another write to the same tenant after its snapshot (retry it) | `write_conflict` | `40001` |
| Other failures | `execution_failed` | `XX000` |

## Not supported

- Explicit SQL transactions, savepoints, and cursors.
- `CREATE TABLE`, `ALTER TABLE`, and other schema DDL. Tables follow labels.
- Writes to relationship tables.
- Vector or full-text search from SQL. Use the protocol's `vector`, `text`,
  and `hybrid` queries ([Vector search](../indexes-and-search/vector-search.md)).
- PostgreSQL system catalogs beyond the listing ones in
  [PostgreSQL wire protocol](../connectivity/postgresql-wire.md#catalogs),
  extensions, and PostgreSQL-specific functions that DataFusion does not
  implement.

## See also

- [PostgreSQL wire protocol](../connectivity/postgresql-wire.md)
- [Records and labels](../data-model/records-and-labels.md)
- [Relationships](../data-model/relationships.md)
- [SQL/PGQ](sql-pgq.md) for graph pattern matching from SQL
- [Query languages overview](README.md)
