SQL transforms
The statements, names, types, operators and functions a pipeline's transform.sql can use, and what the engine refuses.
| Look up | Reference |
|---|---|
| Statement syntax and names | Statements |
| Time windows and aggregates | SQL group windows |
| Running totals and the rows before a row, and one row for each key | SQL OVER |
| Operators, data types and casts | SQL expressions and types |
| Built-in functions | SQL functions |
| Unsupported operations and error messages | SQL restrictions and errors |
A SQL transform is the transform.sql field of a Pipeline. 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 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. These aggregate a stream's rows by windows of event time. It runs OVER, which gives each row values over the rows before it, such as a running total, and 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 lists the messages.
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 INTOtakes no list of columns: theSELECTnames the output's columns. It takes noON CONFLICT.CREATE VIEWtakes a name andAS SELECT, and nothing else: notOR REPLACE,TEMPORARY,MATERIALIZEDor 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
SELECTas a view, and read the view.
What FROM reads
A SELECT reads one relation, which its FROM names:
- A source's stream, by its whole name. See 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
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.Ordersreadspublic.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.ordersreadspublic.orders. If the source has a stream whose name starts withsource., thensource.is the qualifier, andFROM "source.orders"reads that stream. The plan has a note when both readings exist.
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 itsrequire. Name the stream asFROMdoes, 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). destination.before an output's name is optional:INSERT INTO destination.customerswritescustomers.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
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
- 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
ASis folded to lower case, unless it is quoted. - A column can be qualified by the alias that
FROMgives, or, without one, by the name thatFROMreads:orders.idorpublic.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 asopenetl_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). - A column that is not a plain column needs a name: write
lower(email) AS email, notlower(email).
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 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 |
unnest, unnest_outer | In the SELECT list, a row for each element of a list: see 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 | 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 |
| 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 |
| Values over the rows before each row | sum(amount) OVER (PARTITION BY region ORDER BY created_at) AS total | See 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 |
| Read a view or a subquery | FROM paid_orders, FROM (SELECT …) AS t |
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
unnestcalls in oneSELECTgive rows side by side: the first elements together, then the second ones. unnestflattens one level. Useflatten()first for a list of lists.unnesttakes a list, not a struct. It goes in theSELECTlist, not inFROM.- Each row that
unnestgives has an_openetl_idof its own, derived from its input row's.
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
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
WHEREif the stream has no primary key. Such a statement is refused, unless planning simplifies itsWHEREto true, as withWHERE TRUE, which leaves no filter. - Passes each delete without its
WHERE. The statement'sSELECTlist 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 theSELECTlist runs, so an expression such as1 AS versionstill 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
WHEREinto a delete of its key alone. The delete's other columns are null.
The Pipeline reference says how the destination applies these deletes, and what happens to an update that does not send every column.
Changelog streams
See Changelog streams.
Group windows
See Group windows.
Windows
See Windows.
Bounds and keys
See Bounds and keys.
Aggregates
See Aggregates.
When a window's row is written
See When a window's row is written.
Reads
See Reads.
State
See State.
Changing a query
See Changing a query.
Expressions and operators
See Expressions and operators.
Time zones
See Time zones.
Types
See Types.
Casts
See Casts.
Functions
See Functions.
Conditionals
See Conditionals.
Strings
See Strings.
Regular expressions
See Regular expressions.
Numbers
See Numbers.
Dates and times
See Dates and times.
Hashes and encodings
See Hashes and encodings.
Lists
See Lists.
Structs
See Structs.
JSON
See JSON.
What's refused
See What is refused.
Statements of other kinds
See Statements of other kinds.
Clauses and statements
Names and columns
See Names and columns.
Group windows
See Group windows.
Changelog streams
See Changelog streams.
When rows run
See When rows run.