PerfettoSQL Pipe Syntax Reference

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:


Pragma and Statement Forms

PERFETTO PRAGMA pipelines

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

Top-level forms

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.
  • An alias always needs 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('TREE KEY PATHS'), '#tree-key-paths'),
  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')))

Shared rules

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.

Limits

  • A pipeline outputs at most 256 columns.
  • Every column of a source must have a distinct name made of letters, digits and _, not starting with a digit. This holds even for columns a later stage drops:
    • A column with no name, such as SELECT 1 + 1, fails with expected every column to have a name, but column <n> has none: give it one with AS.
    • A name which is not a valid identifier fails with expected every column to have a valid name, but '<name>' is not one: give it one with AS.
    • Two columns with one name fail with expected distinct column names, but there are two named '<name>'.

Sources

FROM

Diagram(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.
  • Qualifiers: 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 OF

Finds 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')))

Requirements

  • At least two sources.
  • Each source has its own alias. A table name is its own alias; a subquery needs AS <alias>.
  • Every source has ts and dur, and every PER column.

Output

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.

ColumnFound by bare nameFound through alias
Region ts, durYesNo
Each source's own ts, durNo<alias>.ts
PER columns of the first sourceYesYes
PER columns of later sourcesNoYes
Every other source columnYes, if only one source has itYes

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.

Interval rules

  • Half-open: [0, 10) and [10, 20) do not overlap.
  • Points: A row with 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.
  • Negative bounds: A negative ts or dur fails with ts or dur is below zero.
  • Overflow: 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 ... TREE

Reduces 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

Requirements

  • NODES has an id column, EDGES has src and dst columns, and FROM has an id column. All of them hold integers.
  • No two nodes have the same id, and every node has one.

Output

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.

KindRootsA node's parent
BFSEvery seedThe first node searched with an edge to it
DFSEach seed not reached from an earlier oneThe node it was reached from
DOMINATOREvery seedIts immediate dominator

Graph rules

  • Order: Seeds are searched, and each node's edges followed, in the order their rows arrive. Where a node can be reached from several equally good parents, that order picks one, so sort FROM or EDGES to choose.
  • Missing nodes: An edge or seed whose id is not a node, or is NULL, is ignored. NODES can be a subset of the graph EDGES describes.
  • Dominators: A node's dominators are the nodes every path to it from the seeds passes through, as though one more node had an edge to every seed.

Aggregates

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(')'))))
  • Forms: Each aggregate needs an 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.
  • Results are LONG columns. Overflow is an error.
  • Scope: Every aggregate reads the row from before the stage, so one aggregate cannot read another.
  • Names: An aggregate can take a name another column has. That name is then ambiguous, and an error when a later stage refers to it.

Expressions

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.


Stages

WHERE

Keeps 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.
  • A string is never converted to a number or a number to a string. Comparing a numeric column with a string, or a string column with a number, is an error: 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.
  • Keeps all columns and aliases.
  • Not supported yet: OR, NOT, NOT IN, NOT GLOB, NOT REGEXP, LIKE, parentheses, functions, arithmetic, and a literal on the left. Filter in the source subquery instead.

DISTINCT

Removes repeated rows:

Diagram(Terminal('DISTINCT'))
  • Rows are equal when they agree on every column found by bare name. NULLs are equal.
  • Columns only an alias finds, such as an intersection's 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.
  • The first of each set of equal rows is kept, and kept rows stay in order.
  • A row can have at most 64 columns.

ORDER BY

Sorts the rows:

Diagram(
  Terminal('ORDER'), Terminal('BY'),
  OneOrMore(
    Sequence(NonTerminal('column'),
      Optional(Choice(0, Terminal('ASC'), Terminal('DESC')))),
    Terminal(',')))
  • The first key decides the order and each later key breaks ties. Each defaults to ASC.
  • A key is a column, which can be qualified. Expressions are not supported yet.
  • Strings sort by their bytes, as SQLite's default BINARY collation does.
  • NULLs come first when ascending and last when descending, as in SQLite.
  • Rows equal on every key keep their order.
  • Keeps all columns and aliases.
  • INTERVAL FLATTEN, INTERVAL FILL GAPS, TREE ACCUMULATE and AGGREGATE arrange rows their own way, so put ORDER BY after them.

LIMIT

Keeps the first rows, after skipping some:

Diagram(
  Terminal('LIMIT'), NonTerminal('count'),
  Optional(Sequence(Terminal('OFFSET'), NonTerminal('skipped'))))
  • Skips <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.
  • Keeps all columns and aliases.

INTERVAL FLATTEN

Splits 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'))
  • Intervals: Bare ts and dur, which must be integer columns. Input does not need to be sorted.
  • Groups: Each combination of PER values is flattened on its own. Without PER, all rows are one group. NULL keys group together.
  • Aggregates: At least one; see Aggregates. Each is taken over the rows covering a segment.
  • Output: 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:

  • Segments: Output splits at every input start and end. Gaps with no rows give no output. Neighbouring segments are not merged even when their values match.
  • Points: Rows with 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.
  • Negative bounds are an error. Filter out unfinished slices, which have dur = -1, first.
  • Overflow is an error.

For an example, see Flattening Overlapping Intervals.

INTERVAL FILL GAPS

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

  • Intervals: Bare ts and dur, in the input and the background.
  • Lanes: Each combination of PER values is filled on its own. The background has either every PER column or none of them:
    • With them, it says which lanes exist. A lane with no input is filled whole, and input in a lane the background does not have gets no fillers.
    • Without them, the same background applies to every lane in the input.
  • Fillers take each background column‘s value by name, their lane’s PER values, and NULL for every other column. Every background column must also be in the input.
  • Types: A column takes one type across both inputs. A column holding strings in one and numbers in the other is an error.
  • Output: The input's columns. Rows come out in no particular order. Aliases are kept.

Interval rules:

  • Overlapping input rows cover their union.
  • An input row with dur = -1 covers everything after its ts.
  • An input row with a NULL ts covers nothing, but is still output.
  • A point does not split a filler.
  • A negative input ts is an error.
  • Background rows which overlap within a lane are an error.

For an example, see Filling Gaps.

TREE ACCUMULATE

Aggregates up or down a tree:

Diagram(
  Terminal('TREE'), Terminal('ACCUMULATE'),
  Choice(0, Terminal('UP'), Terminal('DOWN')),
  NonTerminal('aggregate-list'))
  • Tree: Bare id and parent_id. Roots have parent_id IS NULL.
  • Directions:
    • 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.
  • Aggregates: See Aggregates. COUNT(*) going DOWN is a node's depth, counting from 1.
  • Output: Every input column, then the aggregates. Aliases are kept but do not include the new columns.
  • Errors: Two rows with one 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.

TREE KEY PATHS

Gives each node a key for its root-to-node path of some columns:

Diagram(
  Terminal('TREE'), Terminal('KEY'), Terminal('PATHS'), Terminal('BY'),
  OneOrMore(NonTerminal('column'), Terminal(',')),
  Terminal('AS'), Terminal('('), NonTerminal('key'), Terminal(','),
  NonTerminal('parent-key'), Terminal(')'))
  • Tree: Bare id and parent_id. Roots have parent_id IS NULL.
  • Keys: Two nodes get the same key when their parents have the same key and they agree on the columns. Roots are keyed by the columns alone. NULLs are equal to each other.
  • Values: Keys are integers counting from 0, in the order paths are first seen. Their values are not stable across queries, so only use them to compare rows of one stage's output.
  • Output: Every input column, then the key and the parent's key, which is NULL for a root. Rows come out parents first. Aliases are kept but do not include the new columns.
  • Errors: The same as TREE ACCUMULATE, also only checked if a later stage reads a key. A column changing type between rows.

To build a tree with one node per distinct path, aggregate by the keys:

FROM callstacks
|> TREE KEY PATHS BY name AS (node_id, parent_node_id)
|> AGGREGATE SUM(self_weight) AS self_weight
   GROUP BY node_id, parent_node_id, name;

AGGREGATE

Collapses 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(',')))
  • Groups: Rows that agree on every 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.
  • Aggregates: At least one; see Aggregates.
  • Output: The keys, then the aggregates. A key is named by its 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.
  • Order: Groups come out in the order their first rows arrived.

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;

SELECT

Replaces 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')))
  • A column. Without AS, it keeps its name.
  • An expression (see Expressions) with AS and a name.
  • A <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:

  1. * 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.
  2. 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.
  3. 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.
  4. Every column a star adds can be referred to by bare name in later stages.

EXTEND

Adds columns at the end of the row, keeping every column and alias:

Diagram(Terminal('EXTEND'), OneOrMore(NonTerminal('item'), Terminal(',')))
  • Items are as in SELECT, except that a bare * is not allowed. Use <alias>.* to add an alias's columns.
  • Items read the row before the stage, so one cannot refer to another.
  • Aliases do not include the new columns.

DROP

Removes columns by name:

Diagram(Terminal('DROP'), OneOrMore(NonTerminal('name'), Terminal(',')))
  • Each name must match a column found by bare name, and removes every column of that name.
  • Each name is listed once, and at least one column must remain.
  • Aliases keep the dropped columns. An alias with the same name as a dropped column is removed.

RENAME

Renames columns in place:

Diagram(
  Terminal('RENAME'),
  OneOrMore(
    Sequence(NonTerminal('old'), Terminal('AS'), NonTerminal('new')),
    Terminal(',')))
  • Each <old> must be one column found by bare name, listed once.
  • All renames happen together, so |> RENAME x AS y, y AS x swaps x and y.
  • Renaming to an existing name is allowed; the name is then ambiguous.
  • Aliases keep the old name.

SET

Replaces the values of columns in place:

Diagram(
  Terminal('SET'),
  OneOrMore(
    Sequence(NonTerminal('target'), Terminal('='), NonTerminal('column')),
    Terminal(',')))
  • Each <target> must be one column found by bare name, listed once.
  • Each <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.
  • Aliases keep the old values. An alias with the same name as a target is removed.

AS

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


Names

  • Case: Column names and aliases match regardless of ASCII case. Output keeps the case they were defined with.
  • Duplicates: A stage can leave two columns with one name, as |> 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.
  • Quoting: Names can be quoted with "...", `...` or [...]. Inside "..." and `...`, a doubled quote is a literal quote.