This page documents the syntax of PerfettoSQL, a dialect of SQL used in trace processor and other Perfetto analysis tools to query traces.
PerfettoSQL is a direct descendent of the dialect of SQL implemented by SQLite. Specifically, any SQL valid in SQLite is also valid in PerfettoSQL.
Unfortunately, the SQLite syntax alone is not sufficient for two reasons:
For this reason, PerfettoSQL adds new pieces of syntax which make the experience of writing SQL queries better. All such additons include the keyword PERFETTO
to make it clear that they are PerfettoSQL-only.
INCLUDE PERFETTO MODULE
is used to import all tables/views/functions/macros defined in a PerfettoSQL module (e.g. from the PerfettoSQL standard library).
Note that this statement acts more similar to #include
statements in C++ rather than import
statements from Java/Python. Specifically, all objects in the module become available in the global namespace without being qualified by the module name.
Example:
-- Include all tables/views/functions from the android.startup.startups module -- in the standard library. INCLUDE PERFETTO MODULE android.startup.startups; -- Use the android_startups table defined in the android.startup.startups -- module. SELECT * FROM android_startups;
For interactive development, the key can contain a wildcards:
-- Include all modules under android/. INCLUDE PERFETTO MODULE android.*; -- Or all stdlib modules: INCLUDE PERFETTO MODULE *; -- However, note, that both patterns are not allowed in stdlib.
CREATE PEFETTO FUNCTION
allows functions to be defined in SQL. The syntax is similar to the syntax in PostgreSQL or GoogleSQL.
Example:
-- Create a scalar function with no arguments. CREATE PERFETTO FUNCTION constant_fn() RETURNS INT AS SELECT 1; -- Create a scalar function taking two arguments. CREATE PERFETTO FUNCTION add(x INT, y INT) RETURNS INT AS SELECT $x + $y; -- Create a table function with no arguments CREATE PERFETTO FUNCTION constant_tab_fn() RETURNS TABLE(ts LONG, dur LONG) AS SELECT column1 as ts, column2 as dur FROM ( VALUES (100, 10), (200, 20) ); -- Create a table function with one argument CREATE PERFETTO FUNCTION sched_by_utid(utid INT) RETURNS TABLE(ts LONG, dur LONG, utid INT) AS SELECT ts, dur, utid FROM sched WHERE utid = $utid;
CREATE PERFETTO TABLE
allows defining tables optimized for analytic queries on traces. These tables are both more performant and more memory efficient than SQLite native tables created with CREATE TABLE
.
Note however the full feature set of CREATE TABLE
is not supported:
SELECT
statement. They cannot be defined by column names and types.Example:
-- Create a Perfetto table with constant values. CREATE PERFETTO TABLE constant_table AS SELECT column1 as ts, column2 as dur FROM ( VALUES (100, 10), (200, 20) ); -- Create a Perfetto table with a query on another table. CREATE PERFETTO TABLE slice_sub_table AS SELECT * FROM slice WHERE name = 'foo';
Perfetto tables can have an optional explicit schema. The schema syntax is the same as the function argument or returned-from-a-function table, i.e. a comma-separated list of (column name, colum type) pairs in parenthesis after table or view name.
CREATE PERFETTO TABLE foo(x INT, y STRING) AS SELECT 1 as x, 'test' as y
Views can be created via CREATE PERFETTO VIEW
, taking an optional schema. With the exception of the schema, they behave exactly the same as regular SQLite views.
NOTE: the use of CREATE PERFETTO VIEW
instead of CREATE VIEW
is required in the standard library where each column must be documented.
CREATE PERFETTO VIEW foo(x INT, y STRING) AS SELECT 1 as x, 'test' as y
CREATE PEFETTO MACRO
allows macros to be defined in SQL. The design of macros is inspired by the macros in Rust.
The following are recommended uses of macros:
Macros are powerful but also dangerous if used incorrectly, making debugging extremely difficult. For this reason, it's recommended that they are used sparingly when they are needed and only for the recommended uses described above. If only passing around scalar SQL values, use functions as discussed above.
NOTE: Macros are expanded with a pre-processing step before any execution happens. Expansion is a purely syntatic operation involves replacing the macro invocation with the SQL tokens in the macro definition.
As macros are syntactic, the types of arguments and return types in macros are different to the types used in functions and correspond to parts of the SQL parse tree. The following are the supported types:
Type name | Description |
---|---|
Expr | Corresponds to any SQL scalar expression. |
TableOrSubquery | Corresponds to either an SQL table or a subquery |
ColumnName | Corresponds to a column name of a table |
Example:
-- Create a macro taking no arguments. Note how the returned SQL fragment needs -- to be wrapped in brackets to make it a valid SQL expression. -- -- Note: this is a strongly discouraged use of macros as a simple SQL -- function would also work here. CREATE PERFETTO MACRO constant_macro() RETURNS Expr AS (SELECT 1); -- Using the above macro. Macros are invoked by suffixing their names with !. -- This is similar to how macros are invoked in Rust. SELECT constant_macro!(); -- This causes the following SQL to be actually executed: -- SELECT (SELECT 1); -- A variant of the above. Again, strongly discouraged. CREATE PERFETTO MACRO constant_macro_no_bracket() RETURNS Expr AS 2; -- Using the above macro. SELECT constant_macro_no_bracket!(); -- This causes the following SQL to be actually executed: -- SELECT 2; -- Creating a macro taking a single scalar argument and returning a scalar. -- Note: again this is a strongly discouraged use of macros as functions can -- also do this. CREATE PERFETTO MACRO single_arg_macro(x Expr) RETURNS Expr AS (SELECT $x); SELECT constant_macro!() + single_arg_macro!(100); -- Creating a macro taking both a table and a scalar expression as an argument -- and returning a table. Note again how the returned SQL statement is wrapped -- in brackets to make it a subquery. This allows it to be used anywhere a -- table or subquery is allowed. -- -- Note: if tables are reused multiple times, it's recommended that they be -- "cached" with a common-table expression (CTE) for performance reasons. CREATE PERFETTO MACRO multi_arg_macro(x TableOrSubquery, y Expr) RETURNS TableOrSubquery AS ( SELECT input_tab.input_col + $y FROM $x AS input_tab; )