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, EXTEND, GRAPH, BFS, DFS, DOMINATOR, NODES and EDGES 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, INTERVAL INTERSECTION OF or GRAPH ... TREE. A <stage> is one of:
Diagram(Choice(0, Link(Terminal('WHERE'), '#where'), Link(Terminal('DISTINCT'), '#distinct'), Link(Terminal('ORDER BY'), '#order-by'), Link(Terminal('LIMIT'), '#limit'), 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.GRAPH ... TREEReduces a graph to a tree over the nodes reachable from a set of seeds:
Diagram( Terminal('GRAPH'), Choice(0, Terminal('BFS'), Terminal('DFS'), Terminal('DOMINATOR')), Terminal('TREE'), Terminal('NODES'), NonTerminal('source'), Terminal('EDGES'), NonTerminal('source'), Terminal('FROM'), NonTerminal('source'))
GRAPH DOMINATOR TREE NODES heap_graph_object EDGES (SELECT owner_id AS src, owned_id AS dst FROM heap_graph_reference) FROM (SELECT id FROM heap_graph_object WHERE root_type IS NOT NULL) |> TREE ACCUMULATE UP SUM(self_size) AS retained_size
NODES has an id column, EDGES has src and dst columns, and FROM has an id column. All of them hold integers.id, and every node has one.One row for each node reachable from the seeds, with every NODES column and parent_id, the id of the node's parent in the tree. parent_id is NULL for a root. A NODES column named parent_id is not carried. Parents come before their children. NODES can be given an alias, which qualifies its columns.
| Kind | Roots | A node's parent |
|---|---|---|
BFS | Every seed | The first node searched with an edge to it |
DFS | Each seed not reached from an earlier one | The node it was reached from |
DOMINATOR | Every seed | Its immediate dominator |
FROM or EDGES to choose.id is not a node, or is NULL, is ignored. NODES can be a subset of the graph EDGES describes.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.SELECT, EXTEND and SET can compute values from:
Columns, NULL, and number and string literals.
+, -, *, / and % on numbers. Any null operand gives null, and division or remainder by zero gives null, as in SQLite. An integer overflow fails the query where SQLite would answer with a double.
=, ==, !=, <>, <, <=, > and >=, which give 0 or 1, or null if either side is. Strings compare by their bytes. An integer can be compared with a floating-point literal, as in WHERE. Comparing it with another floating-point value is an error, as is comparing a string with a number.
<expr> GLOB <string>, which gives 0 or 1, or null if <expr> is. The pattern must be a string literal, and <expr> a string.
<expr> IS [NOT] NULL, which gives 0 or 1.
coalesce(<expr>, <expr>, ...), ifnull(<expr>, <expr>) and iif(<condition>, <expr>, <expr>). Their results must have one type: mixing an integer and a double is an error rather than a widening, so write 1.0 rather than 1. NULL goes with any type. Integer arithmetic in an argument which only some rows use is an error, as it could overflow on rows SQLite never computes it for. A condition is true if it is a number other than zero.
Anything else, such as AND or calling another function, is not supported yet.
WHEREKeeps the rows that match a condition:
Diagram( Terminal('WHERE'), OneOrMore( Sequence( NonTerminal('column'), Choice(0, Sequence(NonTerminal('op'), NonTerminal('literal')), Sequence(Terminal('GLOB'), NonTerminal('string')), Sequence(Terminal('REGEXP'), NonTerminal('string')), Sequence(Terminal('IN'), Terminal('('), OneOrMore(NonTerminal('literal'), Terminal(',')), Terminal(')')), Sequence(Terminal('BETWEEN'), NonTerminal('literal'), Terminal('AND'), NonTerminal('literal')), Sequence(Terminal('IS'), Optional(Terminal('NOT')), Terminal('NULL')))), Terminal('AND')))
<op> is one of =, ==, !=, <>, <, <=, > and >=.<literal> is an integer, floating-point or string literal.GLOB and REGEXP match only string columns.REGEXP matches part of a string unless the pattern is anchored, as SQL's REGEXP does. A pattern which does not compile is an error.NULL never matches a comparison, GLOB, REGEXP or IN. Integers compare with fractions as in SQL.dur = '5' fails with expected a number to compare with a numeric column, but got a string. Plain SQL over a Perfetto table does not fail here: it matches nothing.OR, NOT, NOT IN, NOT GLOB, NOT REGEXP, LIKE, parentheses, 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.BINARY collation does.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.LIMITKeeps the first rows, after skipping some:
Diagram( Terminal('LIMIT'), NonTerminal('count'), Optional(Sequence(Terminal('OFFSET'), NonTerminal('skipped'))))
<skipped> rows, then keeps up to <count> rows, in the order they arrive. Put it after ORDER BY to keep the first rows of a sort.<count> and <skipped> are integer literals. As in SQL, a negative count keeps every row and a negative offset skips none.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.AS and a name.<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. It can also be an expression.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.