1-- 2-- Copyright 2024 The Android Open Source Project 3-- 4-- Licensed under the Apache License, Version 2.0 (the "License"); 5-- you may not use this file except in compliance with the License. 6-- You may obtain a copy of the License at 7-- 8-- https://www.apache.org/licenses/LICENSE-2.0 9-- 10-- Unless required by applicable law or agreed to in writing, software 11-- distributed under the License is distributed on an "AS IS" BASIS, 12-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13-- See the License for the specific language governing permissions and 14-- limitations under the License. 15 16CREATE PERFETTO MACRO _interval_intersect( 17 left_table TableOrSubquery, 18 right_table TableOrSubquery 19) 20RETURNS TableOrSubquery AS 21( 22 WITH 23 __temp_left_table AS (SELECT * FROM $left_table ORDER BY ts), 24 __temp_right_table AS (SELECT * FROM $right_table ORDER BY ts) 25 SELECT ii.ts, ii.dur, ii.left_id, ii.right_id 26 FROM __intrinsic_interval_intersect( 27 (SELECT RepeatedField(id) FROM __temp_left_table), 28 (SELECT RepeatedField(ts) FROM __temp_left_table), 29 (SELECT RepeatedField(dur) FROM __temp_left_table), 30 (SELECT RepeatedField(id) FROM __temp_right_table), 31 (SELECT RepeatedField(ts) FROM __temp_right_table), 32 (SELECT RepeatedField(dur) FROM __temp_right_table) 33 ) ii 34); 35 36CREATE PERFETTO MACRO _interval_intersect_single( 37 ts Expr, 38 dur Expr, 39 intervals_table TableOrSubquery 40) RETURNS TableOrSubquery AS ( 41 SELECT 42 left_id AS id, 43 ts, 44 dur 45 FROM _interval_intersect!( 46 $intervals_table, 47 (SELECT 48 0 AS id, 49 $ts AS ts, 50 $dur AS dur 51 ) 52 ) 53) 54