Warning: Pipe syntax is experimental and unstable. Do not rely on it in dashboards, scripts, or production queries. Anything can and will break without notice. Outside the PerfettoSQL standard library, enable it on a connection with
PERFETTO PRAGMA pipelines = 1;.
Related pages:
PERFETTO PRAGMA pipelinesTurns pipe syntax on or off for the connection. The standard library can always use it.
PERFETTO PRAGMA pipelines = 1; -- On PERFETTO PRAGMA pipelines = 0; -- Off
The value must be a non-negative integer literal; any value other than 0 turns pipelines on. The pragma name is case-sensitive.
A pipeline can be a statement on its own, or the body of a PERFETTO TABLE:
<pipeline>; CREATE [OR REPLACE] PERFETTO TABLE <table_name> [(<schema>)] AS <pipeline>;
It cannot yet be the body of a view, function or WITH clause, or appear inside a subquery.
A <pipeline> is a source followed by any number of stages:
Diagram( NonTerminal('source-clause'), ZeroOrMore(Sequence(Terminal('|>'), NonTerminal('stage'))))
|> must be written with no space between | and >.TREE, ACCUMULATE, UP, DOWN, INTERVAL, INTERSECTION, FLATTEN, FILL, GAPS, AGGREGATE, PER and EXTEND are not reserved, so they can still be used as names in SQL.AS: FROM slice AS s, not FROM slice s.A <source-clause> is FROM or INTERVAL INTERSECTION OF. A <stage> is one of:
Diagram(Choice(0, Link(Terminal('WHERE'), '#where'), Link(Terminal('DISTINCT'), '#distinct'), Link(Terminal('ORDER BY'), '#order-by'), Link(Terminal('INTERVAL FLATTEN'), '#interval-flatten'), Link(Terminal('INTERVAL FILL GAPS'), '#interval-fill-gaps'), Link(Terminal('TREE ACCUMULATE'), '#tree-accumulate'), Link(Terminal('AGGREGATE'), '#aggregate'), Link(Terminal('SELECT'), '#select'), Link(Terminal('EXTEND'), '#extend'), Link(Terminal('DROP'), '#drop'), Link(Terminal('RENAME'), '#rename'), Link(Terminal('SET'), '#set'), Link(Terminal('AS'), '#as')))
A <source> is a table or a subquery, with an optional alias:
Diagram( Choice(0, NonTerminal('table-name'), Sequence(Terminal('('), NonTerminal('select-stmt'), Terminal(')'))), Optional(Sequence(Terminal('AS'), NonTerminal('alias'))))
A <column> can be qualified by an alias:
Diagram(Optional(Sequence(NonTerminal('alias'), Terminal('.'))), NonTerminal('name'))
A <per-clause> lists the columns that split rows into groups:
Diagram(Terminal('PER'), OneOrMore(NonTerminal('column'), Terminal(',')))
A column listed again groups nothing new.
_, not starting with a digit. This holds even for columns a later stage drops:SELECT 1 + 1, fails with expected every column to have a name, but column <n> has none: give it one with AS.expected every column to have a valid name, but '<name>' is not one: give it one with AS.expected distinct column names, but there are two named '<name>'.FROMDiagram(Terminal('FROM'), NonTerminal('source'))
<table-name> is a table, a view, or a macro which expands to one. A schema-qualified name such as main.t is not supported.<select-stmt> is any SQL SELECT.FROM slice lets columns be written slice.<col>. FROM slice AS s makes that s.<col> instead. An unaliased subquery has no qualifier until a later |> AS <alias>.INTERVAL INTERSECTION OFFinds where intervals from two or more sources overlap:
Diagram( Terminal('INTERVAL'), Terminal('INTERSECTION'), Terminal('OF'), Terminal('('), OneOrMore(NonTerminal('source'), Terminal(',')), Terminal(')'), Optional(NonTerminal('per-clause')))
AS <alias>.ts and dur, and every PER column.One row is produced for each region [ts, ts + dur) where a row of every source overlaps, among rows that agree on the PER columns.
| Column | Found by bare name | Found through alias |
|---|---|---|
Region ts, dur | Yes | No |
Each source's own ts, dur | No | <alias>.ts |
PER columns of the first source | Yes | Yes |
PER columns of later sources | No | Yes |
| Every other source column | Yes, if only one source has it | Yes |
A bare name two sources share is ambiguous, and an error when something refers to it.
*, |> AS <alias> and the result of a pipeline which ends at the intersection all hold the columns found by bare name: the region‘s ts and dur, then each source’s columns in order. When two sources share a column name, both copies are in the result, which then cannot be saved as a table. Add a |> SELECT naming them through their aliases.
[0, 10) and [10, 20) do not overlap.dur = 0 is the instant ts. It overlaps [a, b) when a <= ts < b, and another point at the same instant. The region has dur = 0.NULL bounds: A row with a NULL ts or dur is skipped.ts or dur fails with ts or dur is below zero.ts + dur beyond the 64-bit range fails with integer overflow.NULL in PER columns: Rows holding NULL in a PER column agree on it, as in GROUP BY.INTERVAL FLATTEN, TREE ACCUMULATE and AGGREGATE take an <aggregate-list>, and treat every aggregate the same way:
Diagram(OneOrMore( Sequence(NonTerminal('aggregate'), Terminal('AS'), NonTerminal('name')), Terminal(',')))
Diagram(Choice(0, Sequence(Terminal('COUNT'), Terminal('('), Terminal('*'), Terminal(')')), Sequence( Choice(0, Terminal('SUM'), Terminal('MIN'), Terminal('MAX')), Terminal('('), NonTerminal('column'), Terminal(')'))))
AS name. Expressions, DISTINCT, FILTER, OVER and other functions are rejected.COUNT(*) counts rows.SUM, MIN and MAX take an integer column. They skip NULLs and return NULL if every value is NULL. A column from a SQL source whose type is not known in advance is checked when the query runs.LONG columns. Overflow is an error.WHEREKeeps the rows that match a condition:
Diagram( Terminal('WHERE'), OneOrMore( Sequence( NonTerminal('column'), NonTerminal('op'), NonTerminal('number')), Terminal('AND')))
<op> is one of =, !=, <, <=, > and >=.<number> is an integer or floating-point literal.NULL never matches. Integers compare with fractions as in SQL.OR, NOT, IS NULL, parentheses, strings, functions, arithmetic, and a literal on the left. Filter in the source subquery instead.DISTINCTRemoves repeated rows:
Diagram(Terminal('DISTINCT'))
NULLs are equal.x.ts, are not compared, and are dropped since they have no one value per row. Aliases are kept, holding only the columns that were compared.ORDER BYSorts the rows:
Diagram( Terminal('ORDER'), Terminal('BY'), OneOrMore( Sequence(NonTerminal('column'), Optional(Choice(0, Terminal('ASC'), Terminal('DESC')))), Terminal(',')))
ASC.NULLs come first when ascending and last when descending, as in SQLite.INTERVAL FLATTEN, INTERVAL FILL GAPS, TREE ACCUMULATE and AGGREGATE arrange rows their own way, so put ORDER BY after them.INTERVAL FLATTENSplits overlapping intervals into segments and aggregates the rows active in each:
Diagram( Terminal('INTERVAL'), Terminal('FLATTEN'), Optional(NonTerminal('per-clause')), Terminal('AGGREGATE'), NonTerminal('aggregate-list'))
ts and dur, which must be integer columns. Input does not need to be sorted.PER values is flattened on its own. Without PER, all rows are one group. NULL keys group together.ts, dur, the PER columns, then the aggregates. Every other column and all aliases are dropped. A PER column listed twice is output twice, so its name is then ambiguous.Interval rules:
dur = 0 at one instant give one extra segment with dur = 0. Its aggregates cover those points and the intervals active at that instant: intervals starting there count, intervals ending there do not. Points do not count towards other segments.NULL bounds: Rows with a NULL ts or dur are skipped.dur = -1, first.For an example, see Flattening Overlapping Intervals.
INTERVAL FILL GAPSKeeps every row and adds a row for each stretch of a background interval which no row covers:
Diagram( Terminal('INTERVAL'), Terminal('FILL'), Terminal('GAPS'), Terminal('WITH'), NonTerminal('source'), Optional(NonTerminal('per-clause')))
<source> holds the background intervals.
ts and dur, in the input and the background.PER values is filled on its own. The background has either every PER column or none of them:PER values, and NULL for every other column. Every background column must also be in the input.Interval rules:
dur = -1 covers everything after its ts.NULL ts covers nothing, but is still output.ts is an error.For an example, see Filling Gaps.
TREE ACCUMULATEAggregates up or down a tree:
Diagram( Terminal('TREE'), Terminal('ACCUMULATE'), Choice(0, Terminal('UP'), Terminal('DOWN')), NonTerminal('aggregate-list'))
id and parent_id. Roots have parent_id IS NULL.UP: each node's aggregate covers itself and all its descendants. Rows come out children first.DOWN: each node's aggregate covers itself and all its ancestors. Rows come out parents first.COUNT(*) going DOWN is a node's depth, counting from 1.id, a row which is its own parent, and a parent_id with no row of that id. These are only checked if a later stage reads one of the aggregates; otherwise the stage is skipped.AGGREGATECollapses each group of rows into one:
Diagram( Terminal('AGGREGATE'), NonTerminal('aggregate-list'), Optional(NonTerminal('group-by')))
Diagram( Terminal('GROUP'), Terminal('BY'), OneOrMore( Sequence(NonTerminal('column'), Optional(Sequence(Terminal('AS'), NonTerminal('name')))), Terminal(',')))
GROUP BY column. A key can have any type and can be qualified. NULL keys group together. Without GROUP BY, all rows are one group, which gives a row even when there are no rows.AS name, or else its column name without qualifier. Every other column and all aliases are dropped. A key listed twice, even as cpu and t.cpu, groups once but is output twice.For example, CPU time per slice:
INCLUDE PERFETTO MODULE slices.with_context; INTERVAL INTERSECTION OF ( (SELECT id, ts, dur, utid FROM thread_slice WHERE dur > 0) AS s, (SELECT ts, dur, utid FROM sched WHERE dur > 0) AS running ) PER utid |> AGGREGATE SUM(dur) AS cpu_time GROUP BY s.id |> ORDER BY id;
SELECTReplaces the row with the listed items, and clears all aliases:
Diagram(Terminal('SELECT'), OneOrMore(NonTerminal('item'), Terminal(',')))
Each <item> is one of:
Diagram(Choice(0, Sequence(NonTerminal('column'), Optional(Sequence(Terminal('AS'), NonTerminal('name')))), NonTerminal('star')))
AS, it keeps its name. Other expressions parse, but fail with a computed column is not supported yet.<star>:Diagram( Optional(Sequence(NonTerminal('alias'), Terminal('.'))), Terminal('*'), Optional(NonTerminal('except')), Optional(NonTerminal('replace')))
Diagram( Terminal('EXCEPT'), Terminal('('), OneOrMore(NonTerminal('name'), Terminal(',')), Terminal(')'))
Diagram( Terminal('REPLACE'), Terminal('('), OneOrMore( Sequence(NonTerminal('column'), Terminal('AS'), NonTerminal('target')), Terminal(',')), Terminal(')'))
A star works as follows:
* is the columns found by bare name. After INTERVAL INTERSECTION OF, that leaves out each source's own ts and dur. <alias>.* is the columns the alias had when it was set.EXCEPT removes every column with a listed name. Each name must match at least one column and be listed once, and at least one column must remain.REPLACE (<column> AS <target>) puts <column>'s values in <target>, keeping <target>'s name and position. <column> is looked up in the row before the stage; <target> must be one column left after EXCEPT.EXTENDAdds columns at the end of the row, keeping every column and alias:
Diagram(Terminal('EXTEND'), OneOrMore(NonTerminal('item'), Terminal(',')))
SELECT, except that a bare * is not allowed. Use <alias>.* to add an alias's columns.DROPRemoves columns by name:
Diagram(Terminal('DROP'), OneOrMore(NonTerminal('name'), Terminal(',')))
RENAMERenames columns in place:
Diagram( Terminal('RENAME'), OneOrMore( Sequence(NonTerminal('old'), Terminal('AS'), NonTerminal('new')), Terminal(',')))
<old> must be one column found by bare name, listed once.|> RENAME x AS y, y AS x swaps x and y.SETReplaces the values of columns in place:
Diagram( Terminal('SET'), OneOrMore( Sequence(NonTerminal('target'), Terminal('='), NonTerminal('column')), Terminal(',')))
<target> must be one column found by bare name, listed once.<column> is looked up in the row before the stage. So |> SET x = y, y = x swaps the values of x and y. Other expressions fail with a computed column is not supported yet.ASReplaces all aliases with one alias for the current row:
Diagram(Terminal('AS'), NonTerminal('alias'))
After |> AS u, earlier aliases are gone, and u.<col> and u.* are the columns found by bare name at this point. After INTERVAL INTERSECTION OF, u.ts is the region‘s ts, and the sources’ own ts and dur can no longer be reached.
|> RENAME x AS y does when y exists. This is only an error when a later stage refers to that name without a qualifier. DROP y or * EXCEPT (y) removes every copy."...", `...` or [...]. Inside "..." and `...`, a doubled quote is a literal quote.