Skip to Content
OpenETL
Reference

SQL transforms

The statements, names, types, operators and functions a pipeline's transform.sql can use, and what the engine refuses.

Edit on GitHub
Look upReference
Statement syntax and namesStatements
Time windows and aggregatesSQL group windows
Running totals and the rows before a row, and one row for each keySQL OVER
Operators, data types and castsSQL expressions and types
Built-in functionsSQL functions
Unsupported operations and error messagesSQL 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 /* … */.

StatementWhat 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

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.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

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).
  • 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

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 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).
  • A column that is not a plain column needs a name: write lower(email) AS email, not lower(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:

NameMeaning without quotes
sourceBefore a stream's name in FROM, the source: FROM source.orders reads orders. FROM "source.orders" reads a stream of that name.
destinationBefore an output's name in INSERT INTO, the destination: INSERT INTO destination.customers writes customers.
window_start, window_endIn 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, sessionIn GROUP BY, a group window
unnest, unnest_outerIn the SELECT list, a row for each element of a list: see UNNEST
__openetl_window_start, __openetl_window_endThe 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

WhatHowNotes
Choose and rename columnsSELECT id AS order_id, status
Every columnSELECT *, or SELECT o.*A column that the stream gains later reaches the output only through *.
Compute a columnSELECT amount * 100 AS centsExpressions, operators and functions
Literals'eu' AS region, 1 AS versionA NULL literal needs a cast: CAST(NULL AS VARCHAR) AS note
Filter rowsWHERE status = 'paid'
One row for each element of a listSELECT id, unnest(tags) AS tagSee UNNEST
Values over the rows before each rowsum(amount) OVER (PARTITION BY region ORDER BY created_at) AS totalSee OVER
One row for each key: its first, or its latestQUALIFY row_number() OVER (PARTITION BY id ORDER BY created_at) = 1See Dedupe
Read a view or a subqueryFROM 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 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

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 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 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

See 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.

On this page