Merge "tp: Implement deprecation policy for stdlib" into main
diff --git a/Android.bp b/Android.bp
index 55e3c23..6f5ab9a 100644
--- a/Android.bp
+++ b/Android.bp
@@ -11990,6 +11990,14 @@
         "src/trace_processor/perfetto_sql/stdlib/common/thread_states.sql",
         "src/trace_processor/perfetto_sql/stdlib/common/timestamps.sql",
         "src/trace_processor/perfetto_sql/stdlib/counters/intervals.sql",
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/args.sql",
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/counters.sql",
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/cpus.sql",
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/metadata.sql",
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/percentiles.sql",
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/slices.sql",
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/thread_states.sql",
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/timestamps.sql",
         "src/trace_processor/perfetto_sql/stdlib/graphs/dominator_tree.sql",
         "src/trace_processor/perfetto_sql/stdlib/graphs/search.sql",
         "src/trace_processor/perfetto_sql/stdlib/intervals/overlap.sql",
diff --git a/BUILD b/BUILD
index f7f2aa6..88b3138 100644
--- a/BUILD
+++ b/BUILD
@@ -2311,6 +2311,21 @@
     ],
 )
 
+# GN target: //src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common:common
+perfetto_filegroup(
+    name = "src_trace_processor_perfetto_sql_stdlib_deprecated_v42_common_common",
+    srcs = [
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/args.sql",
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/counters.sql",
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/cpus.sql",
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/metadata.sql",
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/percentiles.sql",
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/slices.sql",
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/thread_states.sql",
+        "src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/timestamps.sql",
+    ],
+)
+
 # GN target: //src/trace_processor/perfetto_sql/stdlib/graphs:graphs
 perfetto_filegroup(
     name = "src_trace_processor_perfetto_sql_stdlib_graphs_graphs",
@@ -2380,6 +2395,7 @@
         ":src_trace_processor_perfetto_sql_stdlib_chrome_chrome_sql",
         ":src_trace_processor_perfetto_sql_stdlib_common_common",
         ":src_trace_processor_perfetto_sql_stdlib_counters_counters",
+        ":src_trace_processor_perfetto_sql_stdlib_deprecated_v42_common_common",
         ":src_trace_processor_perfetto_sql_stdlib_graphs_graphs",
         ":src_trace_processor_perfetto_sql_stdlib_intervals_intervals",
         ":src_trace_processor_perfetto_sql_stdlib_linux_linux",
diff --git a/infra/perfetto.dev/src/gen_stdlib_docs_md.py b/infra/perfetto.dev/src/gen_stdlib_docs_md.py
index 47007a7..6fc5283 100644
--- a/infra/perfetto.dev/src/gen_stdlib_docs_md.py
+++ b/infra/perfetto.dev/src/gen_stdlib_docs_md.py
@@ -224,9 +224,10 @@
   modules_dict: Dict[str, ModuleMd] = {}
 
   for module_name, module_files in modules_json_dict.items():
-    modules_dict[module_name] = ModuleMd(module_name, module_files)
+    # Remove 'common' when it has been removed from the code.
+    if module_name not in ['deprecated', 'common']:
+      modules_dict[module_name] = ModuleMd(module_name, module_files)
 
-  common_module = modules_dict.pop('common')
   prelude_module = modules_dict.pop('prelude')
 
   with open(args.output, 'w') as f:
@@ -274,8 +275,6 @@
 
     summary_objs = [prelude_module.summary_objs
                    ] if prelude_module.summary_objs else []
-    summary_objs += [common_module.summary_objs
-                    ] if common_module.summary_objs else []
     summary_objs += [
         module.summary_objs
         for name, module in modules_dict.items()
@@ -284,8 +283,6 @@
 
     summary_funs = [prelude_module.summary_funs
                    ] if prelude_module.summary_funs else []
-    summary_funs += [common_module.summary_funs
-                    ] if common_module.summary_funs else []
     summary_funs += [
         module.summary_funs
         for name, module in modules_dict.items()
@@ -293,8 +290,6 @@
     ]
     summary_view_funs = [prelude_module.summary_view_funs
                         ] if prelude_module.summary_view_funs else []
-    summary_view_funs += [common_module.summary_view_funs
-                         ] if common_module.summary_view_funs else []
     summary_view_funs += [
         module.summary_view_funs
         for name, module in modules_dict.items()
@@ -302,8 +297,6 @@
     ]
     summary_macros = [prelude_module.summary_macros
                      ] if prelude_module.summary_macros else []
-    summary_macros += [common_module.summary_macros
-                      ] if common_module.summary_macros else []
     summary_macros += [
         module.summary_macros
         for name, module in modules_dict.items()
@@ -341,8 +334,6 @@
     f.write('\n\n')
     f.write(prelude_module.print_description())
     f.write('\n')
-    f.write(common_module.print_description())
-    f.write('\n')
     f.write('\n'.join(
         module.print_description() for module in modules_dict.values()))
 
diff --git a/python/generators/sql_processing/docs_parse.py b/python/generators/sql_processing/docs_parse.py
index 31cea3c..46a6308 100644
--- a/python/generators/sql_processing/docs_parse.py
+++ b/python/generators/sql_processing/docs_parse.py
@@ -50,7 +50,7 @@
 # Returns: error message if the name is not correct, None otherwise.
 def get_module_prefix_error(name: str, path: str, module: str) -> Optional[str]:
   prefix = name.lower().split('_')[0]
-  if module == "common" or module == "prelude":
+  if module in ["common", "prelude", "deprecated"]:
     if prefix == module:
       return (f'Names of tables/views/functions in the "{module}" module '
               f'should not start with {module}')
diff --git a/src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.cc b/src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.cc
index a864b6d..a246c6a 100644
--- a/src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.cc
+++ b/src/trace_processor/perfetto_sql/engine/perfetto_sql_parser.cc
@@ -230,7 +230,7 @@
 
   if (!ValidateModuleName(key)) {
     base::StackString<1024> err(
-        "Include key should be a dot-separated list of module names, with the"
+        "Include key should be a dot-separated list of module names, with the "
         "last name optionally being a wildcard: '%s'",
         key.c_str());
     return ErrorAtToken(tok, err.c_str());
diff --git a/src/trace_processor/perfetto_sql/stdlib/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/BUILD.gn
index eab54f3..da40ef5 100644
--- a/src/trace_processor/perfetto_sql/stdlib/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/BUILD.gn
@@ -23,6 +23,7 @@
     "chrome:chrome_sql",
     "common",
     "counters",
+    "deprecated/v42/common",
     "graphs",
     "intervals",
     "linux",
diff --git a/src/trace_processor/perfetto_sql/stdlib/common/args.sql b/src/trace_processor/perfetto_sql/stdlib/common/args.sql
index df0615a..3d1e793 100644
--- a/src/trace_processor/perfetto_sql/stdlib/common/args.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/common/args.sql
@@ -13,19 +13,8 @@
 -- See the License for the specific language governing permissions and
 -- limitations under the License.
 
--- Returns the formatted value of a given argument.
--- Similar to EXTRACT_ARG, but instead of returning the raw value, it returns
--- the value formatted according to the 'value_type' column (e.g. for booleans,
--- EXTRACT_ARG will return 0 or 1, while FORMATTED_ARG will return 'true' or
--- 'false').
-CREATE PERFETTO FUNCTION formatted_arg(
-  -- Id of the arg set.
-  arg_set_id INT,
-  -- Key of the argument.
-  arg_key STRING
-)
--- Formatted value of the argument.
-RETURNS STRING AS
-SELECT display_value
-FROM args
-WHERE arg_set_id = $arg_set_id AND key = $arg_key;
\ No newline at end of file
+-- No new changes allowed. Will be removed after v45 of Perfetto.
+--
+-- We decided to move away from the generalised `common` module and migrate the
+-- most useful functionality into specialised modules.
+INCLUDE PERFETTO MODULE deprecated.v42.common.args;
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/common/counters.sql b/src/trace_processor/perfetto_sql/stdlib/common/counters.sql
index 57429a2..f0c1ce6 100644
--- a/src/trace_processor/perfetto_sql/stdlib/common/counters.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/common/counters.sql
@@ -13,89 +13,9 @@
 -- See the License for the specific language governing permissions and
 -- limitations under the License.
 
-INCLUDE PERFETTO MODULE common.timestamps;
-
--- Timestamp of first counter value in a counter.
-CREATE PERFETTO FUNCTION earliest_timestamp_for_counter_track(
-  -- Id of a counter track with a counter.
-  counter_track_id INT)
--- Timestamp of first counter value. Null if doesn't exist.
-RETURNS LONG AS
-SELECT MIN(ts) FROM counter WHERE counter.track_id = $counter_track_id;
-
--- Counter values with details of counter track with calculated duration of each counter value.
--- Duration is calculated as time from counter to the next counter.
-CREATE PERFETTO FUNCTION counter_with_dur_for_track(
-  -- Id of track counter track.
-  counter_track_id INT)
-RETURNS TABLE(
-    -- Timestamp of the counter value.
-    ts LONG,
-    -- Duration of the counter value.
-    dur LONG,
-    -- Counter value.
-    value DOUBLE,
-    -- Id of the counter track.
-    track_id INT,
-    -- Name of the counter track.
-    track_name STRING,
-    -- Counter track set id.
-    track_arg_set_id INT,
-    -- Counter arg set id.
-    arg_set_id INT
-) AS
-SELECT
-  ts,
-  LEAD(ts, 1, trace_end()) OVER(ORDER BY ts) - ts AS dur,
-  value,
-  track.id AS track_id,
-  track.name AS track_name,
-  track.source_arg_set_id AS track_arg_set_id,
-  counter.arg_set_id AS arg_set_id
-FROM counter
-JOIN counter_track track ON track.id = counter.track_id
-WHERE track.id = $counter_track_id;
-
--- COUNTER_WITH_DUR_FOR_TRACK but in a specified time.
--- Does calculation over the table ends - creates an artificial counter value at
--- the start if needed and chops the duration of the last timestamps in range.
-CREATE PERFETTO FUNCTION counter_for_time_range(
-  -- Id of track counter track.
-  counter_track_id INT,
-  -- Timestamp of the timerange start.
-  -- Can be earlier than the first counter value.
-  start_ts LONG,
-  -- Timestamp of the timerange end.
-  end_ts LONG)
-RETURNS TABLE(
-  -- Timestamp of the counter value.
-  ts LONG,
-  -- Duration of the counter value.
-  dur LONG,
-  -- Counter value.
-  value DOUBLE,
-  -- If of the counter track.
-  track_id INT,
-  -- Name of the counter track.
-  track_name STRING,
-  -- Counter track set id.
-  track_arg_set_id INT,
-  -- Counter arg set id.
-  arg_set_id INT
-) AS
-SELECT
-  IIF(ts < $start_ts, $start_ts, ts) AS ts,
-  IIF(
-    ts < $start_ts,
-    dur - ($start_ts - ts),
-    IIF(ts + dur > $end_ts, $end_ts - ts, dur)) AS dur,
-  value,
-  track_id,
-  track_name,
-  track_arg_set_id,
-  arg_set_id
-FROM counter_with_dur_for_track($counter_track_id)
-WHERE TRUE
-  AND ts + dur >= $start_ts
-  AND ts < $end_ts
-ORDER BY ts ASC;
+-- No new changes allowed. Will be removed after v45 of Perfetto.
+--
+-- We decided to move away from the generalised `common` module and migrate the
+-- most useful functionality into specialised modules.
+INCLUDE PERFETTO MODULE deprecated.v42.common.args;
+INCLUDE PERFETTO MODULE deprecated.v42.common.counters;
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/common/cpus.sql b/src/trace_processor/perfetto_sql/stdlib/common/cpus.sql
index d897503..7923e86 100644
--- a/src/trace_processor/perfetto_sql/stdlib/common/cpus.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/common/cpus.sql
@@ -13,57 +13,9 @@
 -- See the License for the specific language governing permissions and
 -- limitations under the License.
 
-
-CREATE PERFETTO TABLE _cpu_sizes AS
-SELECT 0 AS n, 'little' AS size
-UNION
-SELECT 1 AS n, 'mid' AS size
-UNION
-SELECT 2 AS n, 'big' AS size;
-
-CREATE PERFETTO TABLE _ranked_cpus AS
-SELECT
- (DENSE_RANK() OVER win) - 1 AS n,
- cpu
-FROM (
-  SELECT
-    track.cpu AS cpu,
-    MAX(counter.value) AS maxfreq
-  FROM counter
-  JOIN cpu_counter_track AS track
-  ON (counter.track_id = track.id)
-  WHERE track.name = "cpufreq"
-  GROUP BY track.cpu
-)
-WINDOW win AS (ORDER BY maxfreq);
-
--- Guess size of CPU.
--- On some multicore devices the cores are heterogeneous and divided
--- into two or more 'sizes'. In a typical case a device might have 8
--- cores of which 4 are 'little' (low power & low performance) and 4
--- are 'big' (high power & high performance). This functions attempts
--- to map a given CPU index onto the relevant descriptor. For
--- homogeneous systems this returns NULL.
-CREATE PERFETTO FUNCTION guess_cpu_size(
-  -- Index of the CPU whose size we will guess.
-  cpu_index INT)
--- A descriptive size ('little', 'mid', 'big', etc) or NULL if we have insufficient information.
-RETURNS STRING AS
-SELECT
-  IIF((SELECT COUNT(DISTINCT n) FROM _ranked_cpus) >= 2, size, null) as size
-FROM _ranked_cpus
-LEFT JOIN _cpu_sizes USING(n)
-WHERE cpu = $cpu_index;
-
-
--- A list of CPUs with their sizes.
-CREATE PERFETTO TABLE cpus(
-  -- Index of the CPU.
-  cpu_index INT,
-  -- A descriptive size ('little', 'mid', 'big', etc) or NULL if we have insufficient information.
-  size STRING
-) AS
-SELECT
-  cpu as cpu_index,
-  guess_cpu_size(cpu) AS size
-FROM _ranked_cpus;
\ No newline at end of file
+-- No new changes allowed. Will be removed after v45 of Perfetto.
+--
+-- We decided to move away from the generalised `common` module and migrate the
+-- most useful functionality into specialised modules.
+INCLUDE PERFETTO MODULE deprecated.v42.common.args;
+INCLUDE PERFETTO MODULE deprecated.v42.common.cpus;
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/common/metadata.sql b/src/trace_processor/perfetto_sql/stdlib/common/metadata.sql
index e667477..bd1a0fd 100644
--- a/src/trace_processor/perfetto_sql/stdlib/common/metadata.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/common/metadata.sql
@@ -13,10 +13,9 @@
 -- See the License for the specific language governing permissions and
 -- limitations under the License.
 
--- Extracts an int value with the given name from the metadata table.
-CREATE PERFETTO FUNCTION extract_int_metadata(
-  -- The name of the metadata entry.
-  name STRING)
--- int_value for the given name. NULL if there's no such entry.
-RETURNS LONG AS
-SELECT int_value FROM metadata WHERE name = ($name);
\ No newline at end of file
+-- No new changes allowed. Will be removed after v45 of Perfetto.
+--
+-- We decided to move away from the generalised `common` module and migrate the
+-- most useful functionality into specialised modules.
+INCLUDE PERFETTO MODULE deprecated.v42.common.args;
+INCLUDE PERFETTO MODULE deprecated.v42.common.metadata;
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/common/percentiles.sql b/src/trace_processor/perfetto_sql/stdlib/common/percentiles.sql
index 47ecde0..525c95c 100644
--- a/src/trace_processor/perfetto_sql/stdlib/common/percentiles.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/common/percentiles.sql
@@ -13,106 +13,9 @@
 -- See the License for the specific language governing permissions and
 -- limitations under the License.
 
-INCLUDE PERFETTO MODULE common.counters;
-INCLUDE PERFETTO MODULE common.timestamps;
-
-CREATE PERFETTO FUNCTION _number_generator(upper_limit INT)
-RETURNS TABLE(num INT) AS
-WITH nums AS
-    (SELECT 1 num UNION SELECT num + 1
-    from NUMS
-    WHERE num < $upper_limit)
-SELECT num FROM nums;
-
+-- No new changes allowed. Will be removed after v45 of Perfetto.
 --
--- Get durations for percentile
---
-
--- All percentiles (range 1-100) for counter track ID in a given time range.
---
--- Percentiles are calculated by:
--- 1. Dividing the sum of duration in time range for each value in the counter
--- by duration of the counter in range. This gives us `percentile_for)value` (DOUBLE).
--- 2. Fetching each percentile by taking floor of each `percentile_for_value`, grouping by
--- resulting `percentile` and MIN from value for each grouping. As we are rounding down,
--- taking MIN assures most reliable data.
--- 3. Filling the possible gaps in percentiles by getting the minimal value from higher
--- percentiles for each gap.
-CREATE PERFETTO FUNCTION counter_percentiles_for_time_range(
-  -- Id of the counter track.
-  counter_track_id INT,
-  -- Timestamp of start of time range.
-  start_ts LONG,
-  -- Timestamp of end of time range.
-  end_ts LONG)
-RETURNS TABLE(
-  -- All of the numbers from 1 to 100.
-  percentile INT,
-  -- Value for the percentile.
-  value DOUBLE
-) AS
-WITH percentiles_for_value AS (
-    SELECT
-        value,
-        (CAST(SUM(dur) OVER(ORDER BY value ASC) AS DOUBLE) /
-            ($end_ts - MAX($start_ts, earliest_timestamp_for_counter_track($counter_track_id)))) * 100
-        AS percentile_for_value
-    FROM COUNTER_FOR_TIME_RANGE($counter_track_id, $start_ts, $end_ts)
-    ORDER BY value ASC
-),
-with_gaps AS (
-    SELECT
-        CAST(percentile_for_value AS INT) AS percentile,
-        MIN(value) AS value
-    FROM percentiles_for_value
-    GROUP BY percentile
-    ORDER BY percentile ASC)
-SELECT
-    num AS percentile,
-    IFNULL(value, MIN(value) OVER (ORDER BY percentile DESC)) AS value
-FROM _NUMBER_GENERATOR(100) AS nums
-LEFT JOIN with_gaps ON with_gaps.percentile = nums.num
-ORDER BY percentile DESC;
-
--- All percentiles (range 1-100) for counter track ID.
-CREATE PERFETTO FUNCTION counter_percentiles_for_track(
-  -- Id of the counter track.
-  counter_track_id INT)
-RETURNS TABLE(
-  -- All of the numbers from 1 to 100.
-  percentile INT,
-  -- Value for the percentile.
-  value DOUBLE
-) AS
-SELECT *
-FROM counter_percentiles_for_time_range(
-  $counter_track_id, trace_start(), trace_end());
-
--- Value for specific percentile (range 1-100) for counter track ID in time range.
-CREATE PERFETTO FUNCTION counter_track_percentile_for_time(
-  -- Id of the counter track.
-  counter_track_id INT,
-  -- Any of the numbers from 1 to 100.
-  percentile INT,
-  -- Timestamp of start of time range.
-  start_ts LONG,
-  -- Timestamp of end of time range.
-  end_ts LONG)
--- Value for the percentile.
-RETURNS DOUBLE AS
-SELECT value
-FROM counter_percentiles_for_time_range($counter_track_id, $start_ts, $end_ts)
-WHERE percentile = $percentile;
-
--- Value for specific percentile (range 1-100) for counter track ID.
-CREATE PERFETTO FUNCTION counter_track_percentile(
-  -- Id of the counter track.
-  counter_track_id INT,
-  -- Any of the numbers from 1 to 100.
-  percentile INT)
--- Value for the percentile.
-RETURNS DOUBLE AS
-SELECT counter_track_percentile_for_time($counter_track_id,
-                                         $percentile,
-                                         trace_start(),
-                                         trace_end());
+-- We decided to move away from the generalised `common` module and migrate the
+-- most useful functionality into specialised modules.
+INCLUDE PERFETTO MODULE deprecated.v42.common.args;
+INCLUDE PERFETTO MODULE deprecated.v42.common.percentiles;
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/common/slices.sql b/src/trace_processor/perfetto_sql/stdlib/common/slices.sql
index 84badb6..d5d70c9 100644
--- a/src/trace_processor/perfetto_sql/stdlib/common/slices.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/common/slices.sql
@@ -13,244 +13,9 @@
 -- See the License for the specific language governing permissions and
 -- limitations under the License.
 
--- All thread slices with data about thread, thread track and process.
--- Where possible, use available view functions which filter this view.
-CREATE PERFETTO VIEW thread_slice(
-  -- Alias for `slice.id`.
-  id INT,
-  -- Alias for `slice.type`.
-  type STRING,
-  -- Alias for `slice.ts`.
-  ts INT,
-  -- Alias for `slice.dur`.
-  dur INT,
-  -- Alias for `slice.category`.
-  category STRING,
-  -- Alias for `slice.name`.
-  name STRING,
-  -- Alias for `slice.track_id`.
-  track_id INT,
-  -- Alias for `thread_track.name`.
-  track_name STRING,
-  -- Alias for `thread.name`.
-  thread_name STRING,
-  -- Alias for `thread.utid`.
-  utid INT,
-  -- Alias for `thread.tid`
-  tid INT,
-  -- Alias for `process.name`.
-  process_name STRING,
-  -- Alias for `process.upid`.
-  upid INT,
-  -- Alias for `process.pid`.
-  pid INT,
-  -- Alias for `slice.depth`.
-  depth INT,
-  -- Alias for `slice.parent_id`.
-  parent_id INT,
-  -- Alias for `slice.arg_set_id`.
-  arg_set_id INT,
-  -- Alias for `slice.thread_ts`.
-  thread_ts INT,
-  -- Alias for `slice.thread_dur`.
-  thread_dur INT
-) AS
-SELECT
-  slice.id,
-  slice.type,
-  slice.ts,
-  slice.dur,
-  slice.category,
-  slice.name,
-  slice.track_id,
-  thread_track.name AS track_name,
-  thread.name AS thread_name,
-  thread.utid,
-  thread.tid,
-  process.name AS process_name,
-  process.upid,
-  process.pid,
-  slice.depth,
-  slice.parent_id,
-  slice.arg_set_id,
-  slice.thread_ts,
-  slice.thread_dur
-FROM slice
-JOIN thread_track ON slice.track_id = thread_track.id
-JOIN thread USING (utid)
-LEFT JOIN process USING (upid);
-
--- All process slices with data about process track and process.
--- Where possible, use available view functions which filter this view.
-CREATE PERFETTO VIEW process_slice(
-  -- Alias for `slice.id`.
-  id INT,
-  -- Alias for `slice.type`.
-  type STRING,
-  -- Alias for `slice.ts`.
-  ts INT,
-  -- Alias for `slice.dur`.
-  dur INT,
-  -- Alias for `slice.category`.
-  category STRING,
-  -- Alias for `slice.name`.
-  name STRING,
-  -- Alias for `slice.track_id`.
-  track_id INT,
-  -- Alias for `process_track.name`.
-  track_name STRING,
-  -- Alias for `process.name`.
-  process_name STRING,
-  -- Alias for `process.upid`.
-  upid INT,
-  -- Alias for `process.pid`.
-  pid INT,
-  -- Alias for `slice.depth`.
-  depth INT,
-  -- Alias for `slice.parent_id`.
-  parent_id INT,
-  -- Alias for `slice.arg_set_id`.
-  arg_set_id INT,
-  -- Alias for `slice.thread_ts`.
-  thread_ts INT,
-  -- Alias for `slice.thread_dur`.
-  thread_dur INT
-) AS
-SELECT
-  slice.id,
-  slice.type,
-  slice.ts,
-  slice.dur,
-  slice.category,
-  slice.name,
-  slice.track_id,
-  process_track.name AS track_name,
-  process.name AS process_name,
-  process.upid,
-  process.pid,
-  slice.depth,
-  slice.parent_id,
-  slice.arg_set_id,
-  slice.thread_ts,
-  slice.thread_dur
-FROM slice
-JOIN process_track ON slice.track_id = process_track.id
-JOIN process USING (upid);
-
--- Checks if slice has an ancestor with provided name.
-CREATE PERFETTO FUNCTION has_parent_slice_with_name(
-  -- Id of the slice to check parents of.
-  id INT,
-  -- Name of potential ancestor slice.
-  parent_name STRING)
--- Whether `parent_name` is a name of an ancestor slice.
-RETURNS BOOL AS
-SELECT EXISTS(
-  SELECT 1
-  FROM ancestor_slice($id)
-  WHERE name = $parent_name
-  LIMIT 1
-);
-
--- Checks if slice has a descendant with provided name.
-CREATE PERFETTO FUNCTION has_descendant_slice_with_name(
-  -- Id of the slice to check descendants of.
-  id INT,
-  -- Name of potential descendant slice.
-  descendant_name STRING
-)
--- Whether `descendant_name` is a name of an descendant slice.
-RETURNS BOOL AS
-SELECT EXISTS(
-  SELECT 1
-  FROM descendant_slice($id)
-  WHERE name = $descendant_name
-  LIMIT 1
-);
-
--- Count slices with specified name.
-CREATE PERFETTO FUNCTION slice_count(
-  -- Name of the slices to counted.
-  slice_glob STRING)
--- Number of slices with the name.
-RETURNS INT AS
-SELECT COUNT(1) FROM slice WHERE name GLOB $slice_glob;;
-
--- Finds the end timestamp for a given slice's descendant with a given name.
--- If there are multiple descendants with a given name, the function will return the
--- first one, so it's most useful when working with a timeline broken down into phases,
--- where each subphase can happen only once.
-CREATE PERFETTO FUNCTION descendant_slice_end(
-  -- Id of the parent slice.
-  parent_id INT,
-  -- Name of the child with the desired end TS.
-  child_name STRING
-)
--- End timestamp of the child or NULL if it doesn't exist.
-RETURNS INT AS
-SELECT
-  CASE WHEN s.dur
-    IS NOT -1 THEN s.ts + s.dur
-    ELSE NULL
-  END
-FROM descendant_slice($parent_id) s
-WHERE s.name = $child_name
-LIMIT 1;
-
--- Finds all slices with a direct parent with the given parent_id.
-CREATE PERFETTO FUNCTION direct_children_slice(
-  -- Id of the parent slice.
-  parent_id LONG)
-RETURNS TABLE(
-  -- Alias for `slice.id`.
-  id LONG,
-  -- Alias for `slice.type`.
-  type STRING,
-  -- Alias for `slice.ts`.
-  ts LONG,
-  -- Alias for `slice.dur`.
-  dur LONG,
-  -- Alias for `slice.category`.
-  category LONG,
-  -- Alias for `slice.name`.
-  name STRING,
-  -- Alias for `slice.track_id`.
-  track_id LONG,
-  -- Alias for `slice.depth`.
-  depth LONG,
-  -- Alias for `slice.parent_id`.
-  parent_id LONG,
-  -- Alias for `slice.arg_set_id`.
-  arg_set_id LONG,
-  -- Alias for `slice.thread_ts`.
-  thread_ts LONG,
-  -- Alias for `slice.thread_dur`.
-  thread_dur LONG
-) AS
-SELECT
-  slice.id,
-  slice.type,
-  slice.ts,
-  slice.dur,
-  slice.category,
-  slice.name,
-  slice.track_id,
-  slice.depth,
-  slice.parent_id,
-  slice.arg_set_id,
-  slice.thread_ts,
-  slice.thread_dur
-FROM slice
-WHERE parent_id = $parent_id;
-
--- Given a slice id, returns the name of the slice.
-CREATE PERFETTO FUNCTION slice_name_from_id(
-  -- The slice id which we need the name for.
-  id LONG
-)
--- The name of slice with the given id.
-RETURNS STRING AS
-SELECT
-  name
-FROM slice
-WHERE $id = id;
+-- No new changes allowed. Will be removed after v45 of Perfetto.
+--
+-- We decided to move away from the generalised `common` module and migrate the
+-- most useful functionality into specialised modules.
+INCLUDE PERFETTO MODULE deprecated.v42.common.args;
+INCLUDE PERFETTO MODULE deprecated.v42.common.slices;
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/common/thread_states.sql b/src/trace_processor/perfetto_sql/stdlib/common/thread_states.sql
index d43d6a1..a21e9da 100644
--- a/src/trace_processor/perfetto_sql/stdlib/common/thread_states.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/common/thread_states.sql
@@ -13,107 +13,9 @@
 -- See the License for the specific language governing permissions and
 -- limitations under the License.
 
-INCLUDE PERFETTO MODULE common.timestamps;
-INCLUDE PERFETTO MODULE common.cpus;
-
--- TODO(altimin): this doesn't handle some corner cases which thread_state.ts
--- handles (as complex strings manipulations in SQL are pretty painful),
--- but they are pretty niche.
--- Translates the thread state name from a single-letter shorthard to
--- a human-readable name.
-CREATE PERFETTO FUNCTION _translate_thread_state_name(name STRING)
-RETURNS STRING AS
-SELECT CASE $name
-WHEN 'Running' THEN 'Running'
-WHEN 'R' THEN 'Runnable'
-WHEN 'R+' THEN 'Runnable (Preempted)'
-WHEN 'S' THEN 'Sleeping'
-WHEN 'D' THEN 'Uninterruptible Sleep'
-WHEN 'T' THEN 'Stopped'
-WHEN 't' THEN 'Traced'
-WHEN 'X' THEN 'Exit (Dead)'
-WHEN 'Z' THEN 'Exit (Zombie)'
-WHEN 'x' THEN 'Task Dead'
-WHEN 'I' THEN 'Idle'
-WHEN 'K' THEN 'Wakekill'
-WHEN 'W' THEN 'Waking'
-WHEN 'P' THEN 'Parked'
-WHEN 'N' THEN 'No Load'
-ELSE $name
-END;
-
--- Returns a human-readable name for a thread state.
-CREATE PERFETTO FUNCTION human_readable_thread_state_name(
-  -- Thread state id.
-  id INT)
--- Human-readable name for the thread state.
-RETURNS STRING AS
-WITH data AS (
-  SELECT
-    _translate_thread_state_name(state) AS state,
-    (CASE io_wait
-      WHEN 1 THEN ' (IO)'
-      WHEN 0 THEN ' (non-IO)'
-      ELSE ''
-    END) AS io_wait
-  FROM thread_state
-  WHERE id = $id
-)
-SELECT
-  printf('%s%s', state, io_wait)
-FROM data;
-
--- Returns an aggregation of thread states (by state and cpu) for a given
--- interval of time for a given thread.
-CREATE PERFETTO FUNCTION thread_state_summary_for_interval(
-  -- The start of the interval.
-  ts INT,
-  -- The duration of the interval.
-  dur INT,
-  -- The utid of the thread.
-  utid INT)
-RETURNS TABLE(
-  -- Human-readable thread state name.
-  state STRING,
-  -- Raw thread state name, alias of `thread_state.state`.
-  raw_state STRING,
-  -- The type of CPU if available (e.g. "big" / "mid" / "little").
-  cpu_type STRING,
-  -- The CPU index.
-  cpu INT,
-  -- The name of the kernel function execution is blocked in.
-  blocked_function STRING,
-  -- The total duration.
-  dur INT
-) AS
-WITH
-states_starting_inside AS (
-  SELECT id
-  FROM thread_state
-  WHERE $ts <= ts
-    AND ts <= $ts + $dur
-    AND utid = $utid
-),
-first_state_starting_before AS (
-  SELECT id
-  FROM thread_state
-  WHERE ts < $ts AND utid = $utid
-  ORDER BY ts DESC
-  LIMIT 1
-),
-relevant_states AS (
-  SELECT * FROM states_starting_inside
-  UNION ALL
-  SELECT * FROM first_state_starting_before
-)
-SELECT
-  human_readable_thread_state_name(id) as state,
-  state as raw_state,
-  guess_cpu_size(cpu) as cpu_type,
-  cpu,
-  blocked_function,
-  sum(spans_overlapping_dur($ts, $dur, ts, dur)) as dur
-FROM thread_state
-JOIN relevant_states USING (id)
-GROUP BY state, raw_state, cpu_type, cpu, blocked_function
-ORDER BY dur desc;
\ No newline at end of file
+-- No new changes allowed. Will be removed after v45 of Perfetto.
+--
+-- We decided to move away from the generalised `common` module and migrate the
+-- most useful functionality into specialised modules.
+INCLUDE PERFETTO MODULE deprecated.v42.common.args;
+INCLUDE PERFETTO MODULE deprecated.v42.common.thread_states;
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/common/timestamps.sql b/src/trace_processor/perfetto_sql/stdlib/common/timestamps.sql
index 81b89a4..8f91d3b 100644
--- a/src/trace_processor/perfetto_sql/stdlib/common/timestamps.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/common/timestamps.sql
@@ -13,137 +13,9 @@
 -- See the License for the specific language governing permissions and
 -- limitations under the License.
 
+-- No new changes allowed. Will be removed after v45 of Perfetto.
 --
--- Trace bounds
---
-
--- Fetch start of the trace.
-CREATE PERFETTO FUNCTION trace_start()
--- Start of the trace in nanoseconds.
-RETURNS LONG AS
-SELECT start_ts FROM trace_bounds;
-
--- Fetch end of the trace.
-CREATE PERFETTO FUNCTION trace_end()
--- End of the trace in nanoseconds.
-RETURNS LONG AS
-SELECT end_ts FROM trace_bounds;
-
--- Fetch duration of the trace.
-CREATE PERFETTO FUNCTION trace_dur()
--- Duration of the trace in nanoseconds.
-RETURNS LONG AS
-SELECT trace_end() - trace_start();
-
--- Checks whether two spans are overlapping.
-CREATE PERFETTO FUNCTION is_spans_overlapping(
-  -- Start of first span.
-  ts1 LONG,
-  -- End of first span.
-  ts_end1 LONG,
-  -- Start of second span.
-  ts2 LONG,
-  -- End of second span.
-  ts_end2 LONG)
--- Whether two spans are overlapping.
-RETURNS BOOL AS
-SELECT (IIF($ts1 < $ts2, $ts2, $ts1)
-      < IIF($ts_end1 < $ts_end2, $ts_end1, $ts_end2));
-
---Return the overlapping duration between two spans.
---If either duration is less than 0 or there's no intersection, 0 is returned
-CREATE PERFETTO FUNCTION spans_overlapping_dur(
-  -- Timestamp of first slice start.
-  ts1 LONG,
-  -- Duration of first slice.
-  dur1 LONG,
-  -- Timestamp of second slice start.
-  ts2 LONG,
-  -- Duration of second slice.
-  dur2 LONG
-)
--- Overlapping duration
-RETURNS INT AS
-SELECT
-  CASE
-    WHEN $dur1 = -1 OR $dur2 = -1 THEN 0
-    WHEN $ts1 + $dur1 < $ts2 OR $ts2 + $dur2 < $ts1 THEN 0
-    WHEN ($ts1 >= $ts2) AND ($ts1 + $dur1 <= $ts2 + $dur2) THEN $dur1
-    WHEN ($ts1 < $ts2) AND ($ts1 + $dur1 < $ts2 + $dur2) THEN $ts1 + $dur1 - $ts2
-    WHEN ($ts1 > $ts2) AND ($ts1 + $dur1 > $ts2 + $dur2) THEN $ts2 + $dur2 - $ts1
-    ELSE $dur2
-  END;
-
---
--- Helpers for defining time durations.
---
-
--- Converts a duration in seconds to nanoseconds, which is the default representation
--- of time durations in trace processor. Provided for consisensy with other functions.
-CREATE PERFETTO FUNCTION ns(
-  -- Time duration in nanoseconds.
-  nanos INT
-)
--- Time duration in nanoseconds.
-RETURNS INT AS
-SELECT $nanos;
-
--- Converts a duration in microseconds to nanoseconds, which is the default
--- representation of time durations in trace processor.
-CREATE PERFETTO FUNCTION us(
-  -- Time duration in microseconds.
-  micros INT
-)
--- Time duration in nanoseconds.
-RETURNS INT AS
-SELECT $micros * 1000;
-
--- Converts a duration in millseconds to nanoseconds, which is the default
--- representation of time durations in trace processor.
-CREATE PERFETTO FUNCTION ms(
-  -- Time duration in milliseconds.
-  millis INT
-)
--- Time duration in nanoseconds.
-RETURNS INT AS
-SELECT $millis * 1000 * 1000;
-
--- Converts a duration in seconds to nanoseconds, which is the default
--- representation of time durations in trace processor.
-CREATE PERFETTO FUNCTION seconds(
-  -- Time duration in seconds.
-  seconds INT
-)
--- Time duration in nanoseconds.
-RETURNS INT AS
-SELECT $seconds * 1000 * 1000 * 1000;
-
--- Converts a duration in minutes to nanoseconds, which is the default
--- representation of time durations in trace processor.
-CREATE PERFETTO FUNCTION minutes(
-  -- Time duration in minutes.
-  minutes INT
-)
--- Time duration in nanoseconds.
-RETURNS INT AS
-SELECT $minutes * 60 * 1000 * 1000 * 1000;
-
--- Converts a duration in hours to nanoseconds, which is the default
--- representation of time durations in trace processor.
-CREATE PERFETTO FUNCTION hours(
-  -- Time duration in hours.
-  hours INT
-)
--- Time duration in nanoseconds.
-RETURNS INT AS
-SELECT $hours * 60 * 60 * 1000 * 1000 * 1000;
-
--- Converts a duration in days to nanoseconds, which is the default
--- representation of time durations in trace processor.
-CREATE PERFETTO FUNCTION days(
-  -- Time duration in days.
-  days INT
-)
--- Time duration in nanoseconds.
-RETURNS INT AS
-SELECT $days * 24 * 60 * 60 * 1000 * 1000 * 1000;
+-- We decided to move away from the generalised `common` module and migrate the
+-- most useful functionality into specialised modules.
+INCLUDE PERFETTO MODULE deprecated.v42.common.args;
+INCLUDE PERFETTO MODULE deprecated.v42.common.timestamps;
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/deprecated/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/deprecated/BUILD.gn
new file mode 100644
index 0000000..1e92f31
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/deprecated/BUILD.gn
@@ -0,0 +1,19 @@
+# Copyright (C) 2024 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import("../../../../../gn/perfetto_sql.gni")
+
+perfetto_sql_source_set("deprecated") {
+  sources = [ "v42/common" ]
+}
diff --git a/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/BUILD.gn
new file mode 100644
index 0000000..08597bb
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/BUILD.gn
@@ -0,0 +1,28 @@
+# Copyright (C) 2022 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import("../../../../../../../gn/perfetto_sql.gni")
+
+perfetto_sql_source_set("common") {
+  sources = [
+    "args.sql",
+    "counters.sql",
+    "cpus.sql",
+    "metadata.sql",
+    "percentiles.sql",
+    "slices.sql",
+    "thread_states.sql",
+    "timestamps.sql",
+  ]
+}
diff --git a/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/args.sql b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/args.sql
new file mode 100644
index 0000000..df0615a
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/args.sql
@@ -0,0 +1,31 @@
+--
+-- Copyright 2023 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+-- Returns the formatted value of a given argument.
+-- Similar to EXTRACT_ARG, but instead of returning the raw value, it returns
+-- the value formatted according to the 'value_type' column (e.g. for booleans,
+-- EXTRACT_ARG will return 0 or 1, while FORMATTED_ARG will return 'true' or
+-- 'false').
+CREATE PERFETTO FUNCTION formatted_arg(
+  -- Id of the arg set.
+  arg_set_id INT,
+  -- Key of the argument.
+  arg_key STRING
+)
+-- Formatted value of the argument.
+RETURNS STRING AS
+SELECT display_value
+FROM args
+WHERE arg_set_id = $arg_set_id AND key = $arg_key;
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/counters.sql b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/counters.sql
new file mode 100644
index 0000000..7923c52
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/counters.sql
@@ -0,0 +1,101 @@
+--
+-- Copyright 2023 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+INCLUDE PERFETTO MODULE deprecated.v42.common.timestamps;
+
+-- Timestamp of first counter value in a counter.
+CREATE PERFETTO FUNCTION earliest_timestamp_for_counter_track(
+  -- Id of a counter track with a counter.
+  counter_track_id INT)
+-- Timestamp of first counter value. Null if doesn't exist.
+RETURNS LONG AS
+SELECT MIN(ts) FROM counter WHERE counter.track_id = $counter_track_id;
+
+-- Counter values with details of counter track with calculated duration of each counter value.
+-- Duration is calculated as time from counter to the next counter.
+CREATE PERFETTO FUNCTION counter_with_dur_for_track(
+  -- Id of track counter track.
+  counter_track_id INT)
+RETURNS TABLE(
+    -- Timestamp of the counter value.
+    ts LONG,
+    -- Duration of the counter value.
+    dur LONG,
+    -- Counter value.
+    value DOUBLE,
+    -- Id of the counter track.
+    track_id INT,
+    -- Name of the counter track.
+    track_name STRING,
+    -- Counter track set id.
+    track_arg_set_id INT,
+    -- Counter arg set id.
+    arg_set_id INT
+) AS
+SELECT
+  ts,
+  LEAD(ts, 1, trace_end()) OVER(ORDER BY ts) - ts AS dur,
+  value,
+  track.id AS track_id,
+  track.name AS track_name,
+  track.source_arg_set_id AS track_arg_set_id,
+  counter.arg_set_id AS arg_set_id
+FROM counter
+JOIN counter_track track ON track.id = counter.track_id
+WHERE track.id = $counter_track_id;
+
+-- COUNTER_WITH_DUR_FOR_TRACK but in a specified time.
+-- Does calculation over the table ends - creates an artificial counter value at
+-- the start if needed and chops the duration of the last timestamps in range.
+CREATE PERFETTO FUNCTION counter_for_time_range(
+  -- Id of track counter track.
+  counter_track_id INT,
+  -- Timestamp of the timerange start.
+  -- Can be earlier than the first counter value.
+  start_ts LONG,
+  -- Timestamp of the timerange end.
+  end_ts LONG)
+RETURNS TABLE(
+  -- Timestamp of the counter value.
+  ts LONG,
+  -- Duration of the counter value.
+  dur LONG,
+  -- Counter value.
+  value DOUBLE,
+  -- If of the counter track.
+  track_id INT,
+  -- Name of the counter track.
+  track_name STRING,
+  -- Counter track set id.
+  track_arg_set_id INT,
+  -- Counter arg set id.
+  arg_set_id INT
+) AS
+SELECT
+  IIF(ts < $start_ts, $start_ts, ts) AS ts,
+  IIF(
+    ts < $start_ts,
+    dur - ($start_ts - ts),
+    IIF(ts + dur > $end_ts, $end_ts - ts, dur)) AS dur,
+  value,
+  track_id,
+  track_name,
+  track_arg_set_id,
+  arg_set_id
+FROM counter_with_dur_for_track($counter_track_id)
+WHERE TRUE
+  AND ts + dur >= $start_ts
+  AND ts < $end_ts
+ORDER BY ts ASC;
diff --git a/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/cpus.sql b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/cpus.sql
new file mode 100644
index 0000000..d897503
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/cpus.sql
@@ -0,0 +1,69 @@
+--
+-- Copyright 2023 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+
+CREATE PERFETTO TABLE _cpu_sizes AS
+SELECT 0 AS n, 'little' AS size
+UNION
+SELECT 1 AS n, 'mid' AS size
+UNION
+SELECT 2 AS n, 'big' AS size;
+
+CREATE PERFETTO TABLE _ranked_cpus AS
+SELECT
+ (DENSE_RANK() OVER win) - 1 AS n,
+ cpu
+FROM (
+  SELECT
+    track.cpu AS cpu,
+    MAX(counter.value) AS maxfreq
+  FROM counter
+  JOIN cpu_counter_track AS track
+  ON (counter.track_id = track.id)
+  WHERE track.name = "cpufreq"
+  GROUP BY track.cpu
+)
+WINDOW win AS (ORDER BY maxfreq);
+
+-- Guess size of CPU.
+-- On some multicore devices the cores are heterogeneous and divided
+-- into two or more 'sizes'. In a typical case a device might have 8
+-- cores of which 4 are 'little' (low power & low performance) and 4
+-- are 'big' (high power & high performance). This functions attempts
+-- to map a given CPU index onto the relevant descriptor. For
+-- homogeneous systems this returns NULL.
+CREATE PERFETTO FUNCTION guess_cpu_size(
+  -- Index of the CPU whose size we will guess.
+  cpu_index INT)
+-- A descriptive size ('little', 'mid', 'big', etc) or NULL if we have insufficient information.
+RETURNS STRING AS
+SELECT
+  IIF((SELECT COUNT(DISTINCT n) FROM _ranked_cpus) >= 2, size, null) as size
+FROM _ranked_cpus
+LEFT JOIN _cpu_sizes USING(n)
+WHERE cpu = $cpu_index;
+
+
+-- A list of CPUs with their sizes.
+CREATE PERFETTO TABLE cpus(
+  -- Index of the CPU.
+  cpu_index INT,
+  -- A descriptive size ('little', 'mid', 'big', etc) or NULL if we have insufficient information.
+  size STRING
+) AS
+SELECT
+  cpu as cpu_index,
+  guess_cpu_size(cpu) AS size
+FROM _ranked_cpus;
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/metadata.sql b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/metadata.sql
new file mode 100644
index 0000000..e667477
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/metadata.sql
@@ -0,0 +1,22 @@
+--
+-- Copyright 2022 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+-- Extracts an int value with the given name from the metadata table.
+CREATE PERFETTO FUNCTION extract_int_metadata(
+  -- The name of the metadata entry.
+  name STRING)
+-- int_value for the given name. NULL if there's no such entry.
+RETURNS LONG AS
+SELECT int_value FROM metadata WHERE name = ($name);
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/percentiles.sql b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/percentiles.sql
new file mode 100644
index 0000000..78e1ed1
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/percentiles.sql
@@ -0,0 +1,118 @@
+--
+-- Copyright 2023 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+INCLUDE PERFETTO MODULE deprecated.v42.common.counters;
+INCLUDE PERFETTO MODULE deprecated.v42.common.timestamps;
+
+CREATE PERFETTO FUNCTION _number_generator(upper_limit INT)
+RETURNS TABLE(num INT) AS
+WITH nums AS
+    (SELECT 1 num UNION SELECT num + 1
+    from NUMS
+    WHERE num < $upper_limit)
+SELECT num FROM nums;
+
+--
+-- Get durations for percentile
+--
+
+-- All percentiles (range 1-100) for counter track ID in a given time range.
+--
+-- Percentiles are calculated by:
+-- 1. Dividing the sum of duration in time range for each value in the counter
+-- by duration of the counter in range. This gives us `percentile_for)value` (DOUBLE).
+-- 2. Fetching each percentile by taking floor of each `percentile_for_value`, grouping by
+-- resulting `percentile` and MIN from value for each grouping. As we are rounding down,
+-- taking MIN assures most reliable data.
+-- 3. Filling the possible gaps in percentiles by getting the minimal value from higher
+-- percentiles for each gap.
+CREATE PERFETTO FUNCTION counter_percentiles_for_time_range(
+  -- Id of the counter track.
+  counter_track_id INT,
+  -- Timestamp of start of time range.
+  start_ts LONG,
+  -- Timestamp of end of time range.
+  end_ts LONG)
+RETURNS TABLE(
+  -- All of the numbers from 1 to 100.
+  percentile INT,
+  -- Value for the percentile.
+  value DOUBLE
+) AS
+WITH percentiles_for_value AS (
+    SELECT
+        value,
+        (CAST(SUM(dur) OVER(ORDER BY value ASC) AS DOUBLE) /
+            ($end_ts - MAX($start_ts, earliest_timestamp_for_counter_track($counter_track_id)))) * 100
+        AS percentile_for_value
+    FROM COUNTER_FOR_TIME_RANGE($counter_track_id, $start_ts, $end_ts)
+    ORDER BY value ASC
+),
+with_gaps AS (
+    SELECT
+        CAST(percentile_for_value AS INT) AS percentile,
+        MIN(value) AS value
+    FROM percentiles_for_value
+    GROUP BY percentile
+    ORDER BY percentile ASC)
+SELECT
+    num AS percentile,
+    IFNULL(value, MIN(value) OVER (ORDER BY percentile DESC)) AS value
+FROM _NUMBER_GENERATOR(100) AS nums
+LEFT JOIN with_gaps ON with_gaps.percentile = nums.num
+ORDER BY percentile DESC;
+
+-- All percentiles (range 1-100) for counter track ID.
+CREATE PERFETTO FUNCTION counter_percentiles_for_track(
+  -- Id of the counter track.
+  counter_track_id INT)
+RETURNS TABLE(
+  -- All of the numbers from 1 to 100.
+  percentile INT,
+  -- Value for the percentile.
+  value DOUBLE
+) AS
+SELECT *
+FROM counter_percentiles_for_time_range(
+  $counter_track_id, trace_start(), trace_end());
+
+-- Value for specific percentile (range 1-100) for counter track ID in time range.
+CREATE PERFETTO FUNCTION counter_track_percentile_for_time(
+  -- Id of the counter track.
+  counter_track_id INT,
+  -- Any of the numbers from 1 to 100.
+  percentile INT,
+  -- Timestamp of start of time range.
+  start_ts LONG,
+  -- Timestamp of end of time range.
+  end_ts LONG)
+-- Value for the percentile.
+RETURNS DOUBLE AS
+SELECT value
+FROM counter_percentiles_for_time_range($counter_track_id, $start_ts, $end_ts)
+WHERE percentile = $percentile;
+
+-- Value for specific percentile (range 1-100) for counter track ID.
+CREATE PERFETTO FUNCTION counter_track_percentile(
+  -- Id of the counter track.
+  counter_track_id INT,
+  -- Any of the numbers from 1 to 100.
+  percentile INT)
+-- Value for the percentile.
+RETURNS DOUBLE AS
+SELECT counter_track_percentile_for_time($counter_track_id,
+                                         $percentile,
+                                         trace_start(),
+                                         trace_end());
diff --git a/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/slices.sql b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/slices.sql
new file mode 100644
index 0000000..84badb6
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/slices.sql
@@ -0,0 +1,256 @@
+--
+-- Copyright 2022 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+-- All thread slices with data about thread, thread track and process.
+-- Where possible, use available view functions which filter this view.
+CREATE PERFETTO VIEW thread_slice(
+  -- Alias for `slice.id`.
+  id INT,
+  -- Alias for `slice.type`.
+  type STRING,
+  -- Alias for `slice.ts`.
+  ts INT,
+  -- Alias for `slice.dur`.
+  dur INT,
+  -- Alias for `slice.category`.
+  category STRING,
+  -- Alias for `slice.name`.
+  name STRING,
+  -- Alias for `slice.track_id`.
+  track_id INT,
+  -- Alias for `thread_track.name`.
+  track_name STRING,
+  -- Alias for `thread.name`.
+  thread_name STRING,
+  -- Alias for `thread.utid`.
+  utid INT,
+  -- Alias for `thread.tid`
+  tid INT,
+  -- Alias for `process.name`.
+  process_name STRING,
+  -- Alias for `process.upid`.
+  upid INT,
+  -- Alias for `process.pid`.
+  pid INT,
+  -- Alias for `slice.depth`.
+  depth INT,
+  -- Alias for `slice.parent_id`.
+  parent_id INT,
+  -- Alias for `slice.arg_set_id`.
+  arg_set_id INT,
+  -- Alias for `slice.thread_ts`.
+  thread_ts INT,
+  -- Alias for `slice.thread_dur`.
+  thread_dur INT
+) AS
+SELECT
+  slice.id,
+  slice.type,
+  slice.ts,
+  slice.dur,
+  slice.category,
+  slice.name,
+  slice.track_id,
+  thread_track.name AS track_name,
+  thread.name AS thread_name,
+  thread.utid,
+  thread.tid,
+  process.name AS process_name,
+  process.upid,
+  process.pid,
+  slice.depth,
+  slice.parent_id,
+  slice.arg_set_id,
+  slice.thread_ts,
+  slice.thread_dur
+FROM slice
+JOIN thread_track ON slice.track_id = thread_track.id
+JOIN thread USING (utid)
+LEFT JOIN process USING (upid);
+
+-- All process slices with data about process track and process.
+-- Where possible, use available view functions which filter this view.
+CREATE PERFETTO VIEW process_slice(
+  -- Alias for `slice.id`.
+  id INT,
+  -- Alias for `slice.type`.
+  type STRING,
+  -- Alias for `slice.ts`.
+  ts INT,
+  -- Alias for `slice.dur`.
+  dur INT,
+  -- Alias for `slice.category`.
+  category STRING,
+  -- Alias for `slice.name`.
+  name STRING,
+  -- Alias for `slice.track_id`.
+  track_id INT,
+  -- Alias for `process_track.name`.
+  track_name STRING,
+  -- Alias for `process.name`.
+  process_name STRING,
+  -- Alias for `process.upid`.
+  upid INT,
+  -- Alias for `process.pid`.
+  pid INT,
+  -- Alias for `slice.depth`.
+  depth INT,
+  -- Alias for `slice.parent_id`.
+  parent_id INT,
+  -- Alias for `slice.arg_set_id`.
+  arg_set_id INT,
+  -- Alias for `slice.thread_ts`.
+  thread_ts INT,
+  -- Alias for `slice.thread_dur`.
+  thread_dur INT
+) AS
+SELECT
+  slice.id,
+  slice.type,
+  slice.ts,
+  slice.dur,
+  slice.category,
+  slice.name,
+  slice.track_id,
+  process_track.name AS track_name,
+  process.name AS process_name,
+  process.upid,
+  process.pid,
+  slice.depth,
+  slice.parent_id,
+  slice.arg_set_id,
+  slice.thread_ts,
+  slice.thread_dur
+FROM slice
+JOIN process_track ON slice.track_id = process_track.id
+JOIN process USING (upid);
+
+-- Checks if slice has an ancestor with provided name.
+CREATE PERFETTO FUNCTION has_parent_slice_with_name(
+  -- Id of the slice to check parents of.
+  id INT,
+  -- Name of potential ancestor slice.
+  parent_name STRING)
+-- Whether `parent_name` is a name of an ancestor slice.
+RETURNS BOOL AS
+SELECT EXISTS(
+  SELECT 1
+  FROM ancestor_slice($id)
+  WHERE name = $parent_name
+  LIMIT 1
+);
+
+-- Checks if slice has a descendant with provided name.
+CREATE PERFETTO FUNCTION has_descendant_slice_with_name(
+  -- Id of the slice to check descendants of.
+  id INT,
+  -- Name of potential descendant slice.
+  descendant_name STRING
+)
+-- Whether `descendant_name` is a name of an descendant slice.
+RETURNS BOOL AS
+SELECT EXISTS(
+  SELECT 1
+  FROM descendant_slice($id)
+  WHERE name = $descendant_name
+  LIMIT 1
+);
+
+-- Count slices with specified name.
+CREATE PERFETTO FUNCTION slice_count(
+  -- Name of the slices to counted.
+  slice_glob STRING)
+-- Number of slices with the name.
+RETURNS INT AS
+SELECT COUNT(1) FROM slice WHERE name GLOB $slice_glob;;
+
+-- Finds the end timestamp for a given slice's descendant with a given name.
+-- If there are multiple descendants with a given name, the function will return the
+-- first one, so it's most useful when working with a timeline broken down into phases,
+-- where each subphase can happen only once.
+CREATE PERFETTO FUNCTION descendant_slice_end(
+  -- Id of the parent slice.
+  parent_id INT,
+  -- Name of the child with the desired end TS.
+  child_name STRING
+)
+-- End timestamp of the child or NULL if it doesn't exist.
+RETURNS INT AS
+SELECT
+  CASE WHEN s.dur
+    IS NOT -1 THEN s.ts + s.dur
+    ELSE NULL
+  END
+FROM descendant_slice($parent_id) s
+WHERE s.name = $child_name
+LIMIT 1;
+
+-- Finds all slices with a direct parent with the given parent_id.
+CREATE PERFETTO FUNCTION direct_children_slice(
+  -- Id of the parent slice.
+  parent_id LONG)
+RETURNS TABLE(
+  -- Alias for `slice.id`.
+  id LONG,
+  -- Alias for `slice.type`.
+  type STRING,
+  -- Alias for `slice.ts`.
+  ts LONG,
+  -- Alias for `slice.dur`.
+  dur LONG,
+  -- Alias for `slice.category`.
+  category LONG,
+  -- Alias for `slice.name`.
+  name STRING,
+  -- Alias for `slice.track_id`.
+  track_id LONG,
+  -- Alias for `slice.depth`.
+  depth LONG,
+  -- Alias for `slice.parent_id`.
+  parent_id LONG,
+  -- Alias for `slice.arg_set_id`.
+  arg_set_id LONG,
+  -- Alias for `slice.thread_ts`.
+  thread_ts LONG,
+  -- Alias for `slice.thread_dur`.
+  thread_dur LONG
+) AS
+SELECT
+  slice.id,
+  slice.type,
+  slice.ts,
+  slice.dur,
+  slice.category,
+  slice.name,
+  slice.track_id,
+  slice.depth,
+  slice.parent_id,
+  slice.arg_set_id,
+  slice.thread_ts,
+  slice.thread_dur
+FROM slice
+WHERE parent_id = $parent_id;
+
+-- Given a slice id, returns the name of the slice.
+CREATE PERFETTO FUNCTION slice_name_from_id(
+  -- The slice id which we need the name for.
+  id LONG
+)
+-- The name of slice with the given id.
+RETURNS STRING AS
+SELECT
+  name
+FROM slice
+WHERE $id = id;
diff --git a/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/thread_states.sql b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/thread_states.sql
new file mode 100644
index 0000000..a0c63e9
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/thread_states.sql
@@ -0,0 +1,119 @@
+--
+-- Copyright 2022 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+INCLUDE PERFETTO MODULE deprecated.v42.common.timestamps;
+INCLUDE PERFETTO MODULE deprecated.v42.common.cpus;
+
+-- TODO(altimin): this doesn't handle some corner cases which thread_state.ts
+-- handles (as complex strings manipulations in SQL are pretty painful),
+-- but they are pretty niche.
+-- Translates the thread state name from a single-letter shorthard to
+-- a human-readable name.
+CREATE PERFETTO FUNCTION _translate_thread_state_name(name STRING)
+RETURNS STRING AS
+SELECT CASE $name
+WHEN 'Running' THEN 'Running'
+WHEN 'R' THEN 'Runnable'
+WHEN 'R+' THEN 'Runnable (Preempted)'
+WHEN 'S' THEN 'Sleeping'
+WHEN 'D' THEN 'Uninterruptible Sleep'
+WHEN 'T' THEN 'Stopped'
+WHEN 't' THEN 'Traced'
+WHEN 'X' THEN 'Exit (Dead)'
+WHEN 'Z' THEN 'Exit (Zombie)'
+WHEN 'x' THEN 'Task Dead'
+WHEN 'I' THEN 'Idle'
+WHEN 'K' THEN 'Wakekill'
+WHEN 'W' THEN 'Waking'
+WHEN 'P' THEN 'Parked'
+WHEN 'N' THEN 'No Load'
+ELSE $name
+END;
+
+-- Returns a human-readable name for a thread state.
+CREATE PERFETTO FUNCTION human_readable_thread_state_name(
+  -- Thread state id.
+  id INT)
+-- Human-readable name for the thread state.
+RETURNS STRING AS
+WITH data AS (
+  SELECT
+    _translate_thread_state_name(state) AS state,
+    (CASE io_wait
+      WHEN 1 THEN ' (IO)'
+      WHEN 0 THEN ' (non-IO)'
+      ELSE ''
+    END) AS io_wait
+  FROM thread_state
+  WHERE id = $id
+)
+SELECT
+  printf('%s%s', state, io_wait)
+FROM data;
+
+-- Returns an aggregation of thread states (by state and cpu) for a given
+-- interval of time for a given thread.
+CREATE PERFETTO FUNCTION thread_state_summary_for_interval(
+  -- The start of the interval.
+  ts INT,
+  -- The duration of the interval.
+  dur INT,
+  -- The utid of the thread.
+  utid INT)
+RETURNS TABLE(
+  -- Human-readable thread state name.
+  state STRING,
+  -- Raw thread state name, alias of `thread_state.state`.
+  raw_state STRING,
+  -- The type of CPU if available (e.g. "big" / "mid" / "little").
+  cpu_type STRING,
+  -- The CPU index.
+  cpu INT,
+  -- The name of the kernel function execution is blocked in.
+  blocked_function STRING,
+  -- The total duration.
+  dur INT
+) AS
+WITH
+states_starting_inside AS (
+  SELECT id
+  FROM thread_state
+  WHERE $ts <= ts
+    AND ts <= $ts + $dur
+    AND utid = $utid
+),
+first_state_starting_before AS (
+  SELECT id
+  FROM thread_state
+  WHERE ts < $ts AND utid = $utid
+  ORDER BY ts DESC
+  LIMIT 1
+),
+relevant_states AS (
+  SELECT * FROM states_starting_inside
+  UNION ALL
+  SELECT * FROM first_state_starting_before
+)
+SELECT
+  human_readable_thread_state_name(id) as state,
+  state as raw_state,
+  guess_cpu_size(cpu) as cpu_type,
+  cpu,
+  blocked_function,
+  sum(spans_overlapping_dur($ts, $dur, ts, dur)) as dur
+FROM thread_state
+JOIN relevant_states USING (id)
+GROUP BY state, raw_state, cpu_type, cpu, blocked_function
+ORDER BY dur desc;
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/timestamps.sql b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/timestamps.sql
new file mode 100644
index 0000000..81b89a4
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/deprecated/v42/common/timestamps.sql
@@ -0,0 +1,149 @@
+--
+-- Copyright 2022 The Android Open Source Project
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     https://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+--
+-- Trace bounds
+--
+
+-- Fetch start of the trace.
+CREATE PERFETTO FUNCTION trace_start()
+-- Start of the trace in nanoseconds.
+RETURNS LONG AS
+SELECT start_ts FROM trace_bounds;
+
+-- Fetch end of the trace.
+CREATE PERFETTO FUNCTION trace_end()
+-- End of the trace in nanoseconds.
+RETURNS LONG AS
+SELECT end_ts FROM trace_bounds;
+
+-- Fetch duration of the trace.
+CREATE PERFETTO FUNCTION trace_dur()
+-- Duration of the trace in nanoseconds.
+RETURNS LONG AS
+SELECT trace_end() - trace_start();
+
+-- Checks whether two spans are overlapping.
+CREATE PERFETTO FUNCTION is_spans_overlapping(
+  -- Start of first span.
+  ts1 LONG,
+  -- End of first span.
+  ts_end1 LONG,
+  -- Start of second span.
+  ts2 LONG,
+  -- End of second span.
+  ts_end2 LONG)
+-- Whether two spans are overlapping.
+RETURNS BOOL AS
+SELECT (IIF($ts1 < $ts2, $ts2, $ts1)
+      < IIF($ts_end1 < $ts_end2, $ts_end1, $ts_end2));
+
+--Return the overlapping duration between two spans.
+--If either duration is less than 0 or there's no intersection, 0 is returned
+CREATE PERFETTO FUNCTION spans_overlapping_dur(
+  -- Timestamp of first slice start.
+  ts1 LONG,
+  -- Duration of first slice.
+  dur1 LONG,
+  -- Timestamp of second slice start.
+  ts2 LONG,
+  -- Duration of second slice.
+  dur2 LONG
+)
+-- Overlapping duration
+RETURNS INT AS
+SELECT
+  CASE
+    WHEN $dur1 = -1 OR $dur2 = -1 THEN 0
+    WHEN $ts1 + $dur1 < $ts2 OR $ts2 + $dur2 < $ts1 THEN 0
+    WHEN ($ts1 >= $ts2) AND ($ts1 + $dur1 <= $ts2 + $dur2) THEN $dur1
+    WHEN ($ts1 < $ts2) AND ($ts1 + $dur1 < $ts2 + $dur2) THEN $ts1 + $dur1 - $ts2
+    WHEN ($ts1 > $ts2) AND ($ts1 + $dur1 > $ts2 + $dur2) THEN $ts2 + $dur2 - $ts1
+    ELSE $dur2
+  END;
+
+--
+-- Helpers for defining time durations.
+--
+
+-- Converts a duration in seconds to nanoseconds, which is the default representation
+-- of time durations in trace processor. Provided for consisensy with other functions.
+CREATE PERFETTO FUNCTION ns(
+  -- Time duration in nanoseconds.
+  nanos INT
+)
+-- Time duration in nanoseconds.
+RETURNS INT AS
+SELECT $nanos;
+
+-- Converts a duration in microseconds to nanoseconds, which is the default
+-- representation of time durations in trace processor.
+CREATE PERFETTO FUNCTION us(
+  -- Time duration in microseconds.
+  micros INT
+)
+-- Time duration in nanoseconds.
+RETURNS INT AS
+SELECT $micros * 1000;
+
+-- Converts a duration in millseconds to nanoseconds, which is the default
+-- representation of time durations in trace processor.
+CREATE PERFETTO FUNCTION ms(
+  -- Time duration in milliseconds.
+  millis INT
+)
+-- Time duration in nanoseconds.
+RETURNS INT AS
+SELECT $millis * 1000 * 1000;
+
+-- Converts a duration in seconds to nanoseconds, which is the default
+-- representation of time durations in trace processor.
+CREATE PERFETTO FUNCTION seconds(
+  -- Time duration in seconds.
+  seconds INT
+)
+-- Time duration in nanoseconds.
+RETURNS INT AS
+SELECT $seconds * 1000 * 1000 * 1000;
+
+-- Converts a duration in minutes to nanoseconds, which is the default
+-- representation of time durations in trace processor.
+CREATE PERFETTO FUNCTION minutes(
+  -- Time duration in minutes.
+  minutes INT
+)
+-- Time duration in nanoseconds.
+RETURNS INT AS
+SELECT $minutes * 60 * 1000 * 1000 * 1000;
+
+-- Converts a duration in hours to nanoseconds, which is the default
+-- representation of time durations in trace processor.
+CREATE PERFETTO FUNCTION hours(
+  -- Time duration in hours.
+  hours INT
+)
+-- Time duration in nanoseconds.
+RETURNS INT AS
+SELECT $hours * 60 * 60 * 1000 * 1000 * 1000;
+
+-- Converts a duration in days to nanoseconds, which is the default
+-- representation of time durations in trace processor.
+CREATE PERFETTO FUNCTION days(
+  -- Time duration in days.
+  days INT
+)
+-- Time duration in nanoseconds.
+RETURNS INT AS
+SELECT $days * 24 * 60 * 60 * 1000 * 1000 * 1000;