# SQL transforms (https://docs.openetl.io/docs/next/reference/sql)

| Look up | Reference |
|---|---|
| Statement syntax and names | [Statements](#statements) |
| Time windows and aggregates | [SQL group windows](./sql-windows.md) |
| Running totals and the rows before a row, and one row for each key | [SQL OVER](./sql-over.md) |
| Operators, data types and casts | [SQL expressions and types](./sql-expressions.md) |
| Built-in functions | [SQL functions](./sql-functions.md) |
| Unsupported operations and error messages | [SQL restrictions and errors](./sql-errors.md) |

A SQL transform is the `transform.sql` field of a [Pipeline](./specs/pipeline.md#a-sql-transform). Its statements read the source's streams and write outputs, which the destination gets as streams. The engine runs them on every lane of a sync or a continuous pipeline's job, between typing a stream's rows and writing them. [Write a SQL transform](../pipelines/sql-transforms.md) shows how to write one and check it.

The engine runs row-wise statements. Each output row comes from one input row or one list element expanded with `UNNEST`.

The engine also runs [group windows](./sql-windows.md#group-windows). These aggregate a stream's rows by windows of event time. It runs [`OVER`](./sql-over.md#over), which gives each row values over the rows before it, such as a running total, and [dedupe](./sql-over.md#dedupe) by `ROW_NUMBER() … = 1`, which keeps each key's first or latest row. Unsupported statements fail with their position in the SQL text. [SQL restrictions and errors](./sql-errors.md#whats-refused) lists the messages.

## Statements [#statements]

A transform is one or more statements, each ended by `;`. The last `;` is optional. Comments start with `--` or are written as `/* … */`.

| Statement | What it does |
|---|---|
| `INSERT INTO OUTPUT SELECT …` | Writes the `SELECT`'s rows to the output `OUTPUT`. |
| `CREATE VIEW NAME AS SELECT …` | Names a `SELECT` that later statements and views read as if it were a stream. A view writes nothing. |

- A transform has at least one `INSERT INTO`.
- One statement writes each output.
- `INSERT INTO` takes no list of columns: the `SELECT` names the output's columns. It takes no `ON CONFLICT`.
- `CREATE VIEW` takes a name and `AS SELECT`, and nothing else: not `OR REPLACE`, `TEMPORARY`, `MATERIALIZED` or a list of columns.
- A view is defined before the statements and views that read it.
- Views go at most 32 deep. A view that reads no view is 1 deep, and a view that reads views is one deeper than the deepest of them. A deeper view is a problem in the plan, at its name.
- Statements read the source's streams, never an output. To read what a statement writes, define its `SELECT` as a view, and read the view.

## What FROM reads [#what-from-reads]

A `SELECT` reads one relation, which its `FROM` names:

- A source's stream, by its whole name. See [Stream names](#stream-names).
- A view.
- A subquery in parentheses with an alias, such as `FROM (SELECT …) AS t`, which is read as a view is.

An alias can name every column of what `FROM` reads, in order: `FROM orders AS o (order_id, total)`. The `SELECT` then reads the columns by those names.

A `SELECT` without a `FROM`, one that reads `VALUES`, and one that reads two relations are refused. A statement reads a stream's rows as the source sends them, under the source's column names and with the columns' logical types.

## Names [#names]

### Stream names [#stream-names]

A stream's name is its whole name in the source's catalog, such as `public.orders` or `HR.EMPLOYEES`.

- Write it as a dotted path, `public.orders`, or quoted whole, `"public.orders"`.
- Unquoted parts match without regard to case: `PUBLIC.Orders` reads `public.orders`. Quoted parts match exactly.
- If two streams differ only by case, quote the name to pick one.
- `source.` before a stream's name is optional: `FROM source.public.orders` reads `public.orders`. If the source has a stream whose name starts with `source.`, then `source.` is the qualifier, and `FROM "source.orders"` reads that stream. The plan has a note when both readings exist.

### Output names [#output-names]

`INSERT INTO` names the output.

- If the name is a source stream's, the output replaces that stream. The destination gets the output in its place, under the stream's entry in `destination.tables`, and with its `require`. Name the stream as `FROM` does, at any depth: `INSERT INTO public.orders`.
- Otherwise, the output is new, and its name is one identifier. Quote a name that has dots: `INSERT INTO "eu.orders"`. An unquoted name is folded to lower case. A name that starts with `_openetl`, in any case, is refused, because such names are OpenETL's ([Names that OpenETL keeps](../integrate/read-data.md#names-that-openetl-keeps)).
- `destination.` before an output's name is optional: `INSERT INTO destination.customers` writes `customers`. `source.` is refused there.

A stream that no statement replaces passes through to the destination as it would without a transform, unless `transform.passthrough` is `none`.

### View names [#view-names]

A view's name is one identifier, never qualified. Quote a name that has dots. An unquoted name is folded to lower case. A view cannot have the name of a source's stream, of an output or of another view, or a name that starts with `_openetl`, in any case. Views take no `source.` qualifier.

### Column names [#column-names]

- An unquoted column name matches a column without regard to case, when only one column fits. A quoted name matches exactly.
- If two columns differ only by case, quote the name to pick one.
- A column that passes through keeps the source's spelling. A name given with `AS` is folded to lower case, unless it is quoted.
- A column can be qualified by the alias that `FROM` gives, or, without one, by the name that `FROM` reads: `orders.id` or `public.orders.id`.
- An output's column names are unique without regard to case.
- A statement reads a column by the source's name for it, even where the destination has another: a source's column `_openetl_x`, which the destination gets as `openetl_x`, is read as `_openetl_x`. An output's column whose name starts with `_openetl`, in any case, reaches the destination without its first `_`, as such a source column does ([Names that OpenETL keeps](../integrate/read-data.md#names-that-openetl-keeps)).
- A column that is not a plain column needs a name: write `lower(email) AS email`, not `lower(email)`.

### Reserved words [#reserved-words]

Some words are SQL keywords that the parser does not read as a name where they stand. Write such a word in double quotes where it names a column, an alias, a stream, a view or an output: `SELECT "user", "table" FROM audit`. A quoted name matches exactly, so write it in the case it has. An alias written with `AS` needs quotes less often than one without it.

These words need double quotes as a name: `all`, `any`, `current_catalog`, `current_date`, `current_time`, `current_timestamp`, `current_user`, `directory`, `distinct`, `exists`, `false`, `from`, `interval`, `lateral`, `local`, `localtime`, `localtimestamp`, `not`, `null`, `returning`, `session_user`, `some`, `table`, `top`, `trim`, `true`, `unnest`, `user`.

Without `AS`, these words need double quotes as an alias too: `analyze`, `and`, `anti`, `array`, `as`, `asof`, `between`, `cluster`, `collate`, `connect`, `cross`, `distribute`, `div`, `end`, `except`, `exclude`, `explain`, `fetch`, `for`, `format`, `full`, `global`, `group`, `having`, `ilike`, `in`, `inner`, `intersect`, `into`, `is`, `join`, `left`, `like`, `limit`, `match`, `match_condition`, `match_recognize`, `member`, `minus`, `natural`, `notnull`, `offset`, `on`, `open`, `operator`, `or`, `order`, `outer`, `output`, `overlaps`, `partition`, `pivot`, `prewhere`, `qualify`, `regexp`, `right`, `rlike`, `sample`, `select`, `semi`, `set`, `settings`, `similar`, `sort`, `start`, `tablesample`, `union`, `unpivot`, `using`, `values`, `view`, `where`, `window`, `with`, `xor`.

[`crates/sql/reserved.txt`](../../crates/sql/reserved.txt) records where each word needs quotes, and `go test ./test/docs` fails when these lists differ from it.

These names have a meaning of their own where they are written without quotes:

| Name | Meaning without quotes |
|---|---|
| `source` | Before a stream's name in `FROM`, the source: `FROM source.orders` reads `orders`. `FROM "source.orders"` reads a stream of that name. |
| `destination` | Before an output's name in `INSERT INTO`, the destination: `INSERT INTO destination.customers` writes `customers`. |
| `window_start`, `window_end` | In a group window's `SELECT` list and `HAVING`, the bounds of the row's window. `"window_start"` is a column of that name. |
| `tumble`, `hop`, `session` | In `GROUP BY`, a [group window](./sql-windows.md#windows) |
| `unnest`, `unnest_outer` | In the `SELECT` list, a row for each element of a list: see [UNNEST](#unnest) |
| `__openetl_window_start`, `__openetl_window_end` | The engine's own names for a group window's bounds as it groups rows. A statement that reads a column whose name starts with `__openetl_`, or gives a column such a name with `AS`, is refused, quoted or not. |

## What a statement can do [#what-a-statement-can-do]

| What | How | Notes |
|---|---|---|
| Choose and rename columns | `SELECT id AS order_id, status` | |
| Every column | `SELECT *`, or `SELECT o.*` | A column that the stream gains later reaches the output only through `*`. |
| Compute a column | `SELECT amount * 100 AS cents` | Expressions, operators and [functions](./sql-functions.md#functions) |
| Literals | `'eu' AS region`, `1 AS version` | A `NULL` literal needs a cast: `CAST(NULL AS VARCHAR) AS note` |
| Filter rows | `WHERE status = 'paid'` | |
| One row for each element of a list | `SELECT id, unnest(tags) AS tag` | See [UNNEST](#unnest) |
| Values over the rows before each row | `sum(amount) OVER (PARTITION BY region ORDER BY created_at) AS total` | See [OVER](./sql-over.md#over) |
| One row for each key: its first, or its latest | `QUALIFY row_number() OVER (PARTITION BY id ORDER BY created_at) = 1` | See [Dedupe](./sql-over.md#dedupe) |
| Read a view or a subquery | `FROM paid_orders`, `FROM (SELECT …) AS t` | |

### UNNEST [#unnest]

`unnest(LIST) AS NAME` in the `SELECT` list gives one output row for each element of the list, with the row's other columns repeated.

- A null or empty list gives no row. `unnest_outer(LIST)` gives one row with a null for it.
- Several `unnest` calls in one `SELECT` give rows side by side: the first elements together, then the second ones.
- `unnest` flattens one level. Use `flatten()` first for a list of lists.
- `unnest` takes a list, not a struct. It goes in the `SELECT` list, not in `FROM`.
- Each row that `unnest` gives has an `_openetl_id` of its own, derived from its input row's.

### Primary keys [#primary-keys]

An output's primary key is the primary key of the stream it reads when both of these are true:

- Each key column passes through unchanged, renamed or not.
- The statement does not use `unnest`, which can give several rows for one input row.

Otherwise the output has no primary key, and is appended or overwritten, never merged. An expression over a key column, such as `id + 1`, drops the key. A view passes a key on by the same rule.

### Changelog streams [#changelog-streams]

A changelog stream is one whose rows are inserts, updates and deletes of their key, such as a stream read with `cdc`. A statement that reads one:

- Keeps the stream's primary key, unchanged and renamed or not. A statement that drops or changes a key column is refused.
- Does not use `unnest`.
- Has no `WHERE` if the stream has no primary key. Such a statement is refused, unless planning simplifies its `WHERE` to true, as with `WHERE TRUE`, which leaves no filter.
- Passes each delete without its `WHERE`. The statement's `SELECT` list runs on the values the source sent with the delete, and gives the delete's columns. A column that the source did not send is null before the `SELECT` list runs, so an expression such as `1 AS version` still gives its value.
- Passes a delete with its key alone if the statement cannot evaluate the delete, or if the values it gives do not fit the output's column types. The delete's other columns are then null. A delete is not quarantined. If the output holds cells, a value that does not fit is held instead, as it is on a stream that passes through, and the delete passes with its other values.
- Turns an insert or update that fails its `WHERE` into a delete of its key alone. The delete's other columns are null.

The [Pipeline reference](./specs/pipeline.md#statements-that-run) says how the destination applies these deletes, and what happens to an update that does not send every column.

### Changelog streams [#changelog-streams-1]

See [Changelog streams](./sql-errors.md#changelog-streams).

## Group windows [#group-windows]

See [Group windows](./sql-windows.md#group-windows).

### Windows [#windows]

See [Windows](./sql-windows.md#windows).

### Bounds and keys [#bounds-and-keys]

See [Bounds and keys](./sql-windows.md#bounds-and-keys).

### Aggregates [#aggregates]

See [Aggregates](./sql-windows.md#aggregates).

### When a window's row is written [#when-a-windows-row-is-written]

See [When a window's row is written](./sql-windows.md#when-a-windows-row-is-written).

### Reads [#reads]

See [Reads](./sql-windows.md#reads).

### State [#state]

See [State](./sql-windows.md#state).

### Changing a query [#changing-a-query]

See [Changing a query](./sql-windows.md#changing-a-query).

## Expressions and operators [#expressions-and-operators]

See [Expressions and operators](./sql-expressions.md#expressions-and-operators).

### Time zones [#time-zones]

See [Time zones](./sql-expressions.md#time-zones).

## Types [#types]

See [Types](./sql-expressions.md#types).

### Casts [#casts]

See [Casts](./sql-expressions.md#casts).

## Functions [#functions]

See [Functions](./sql-functions.md#functions).

### Conditionals [#conditionals]

See [Conditionals](./sql-functions.md#conditionals).

### Strings [#strings]

See [Strings](./sql-functions.md#strings).

### Regular expressions [#regular-expressions]

See [Regular expressions](./sql-functions.md#regular-expressions).

### Numbers [#numbers]

See [Numbers](./sql-functions.md#numbers).

### Dates and times [#dates-and-times]

See [Dates and times](./sql-functions.md#dates-and-times).

### Hashes and encodings [#hashes-and-encodings]

See [Hashes and encodings](./sql-functions.md#hashes-and-encodings).

### Lists [#lists]

See [Lists](./sql-functions.md#lists).

### Structs [#structs]

See [Structs](./sql-functions.md#structs).

### JSON [#json]

See [JSON](./sql-functions.md#json).

## What's refused [#whats-refused]

See [What is refused](./sql-errors.md#whats-refused).

### Statements of other kinds [#statements-of-other-kinds]

See [Statements of other kinds](./sql-errors.md#statements-of-other-kinds).

### Clauses and statements [#clauses-and-statements]

See [Clauses and statements](./sql-errors.md#clauses-and-statements).

### Names and columns [#names-and-columns]

See [Names and columns](./sql-errors.md#names-and-columns).

### Group windows [#group-windows-1]

See [Group windows](./sql-errors.md#group-windows).

### Changelog streams [#changelog-streams-2]

See [Changelog streams](./sql-errors.md#changelog-streams).

### When rows run [#when-rows-run]

See [When rows run](./sql-errors.md#when-rows-run).
