drop table if exists r, s;
create table r
(
	a char, 
	b char, 
	ts int, 
	te int
);
create table s
(
	c int, 
	d char, 
	ts int, 
	te int
);
insert into r values
('a','B',1,7), 
('b','B',3,9), 
('c','G',8,10);
insert into s values
(1,'B',2,5), 
(2,'B',3,4), 
(3,'B',7,9);
explain select * from 
(
s01 align r01 on true with (ts,te,ts,te)
) x where c < 3;
                                            QUERY PLAN                                            
--------------------------------------------------------------------------------------------------
 Subquery Scan on x  (cost=26589.43..34996.78 rows=296730 width=20)
   ->  Adjustment(for ALIGN)  (cost=26589.43..32029.48 rows=296730 width=36)
         ->  Sort  (cost=26589.43..26836.70 rows=98910 width=36)
               Sort Key: s01.rn, (GREATEST(s01.ts, r01.ts)), (LEAST(s01.te, r01.te))
               ->  Nested Loop Left Join  (cost=0.00..15677.45 rows=98910 width=36)
                     Join Filter: ((s01.ts < r01.te) AND (s01.te > r01.ts))
                     ->  Subquery Scan on s01  (cost=0.00..69.50 rows=567 width=28)
                           Filter: (s01.c < 3)
                           ->  WindowAgg  (cost=0.00..48.25 rows=1700 width=20)
                                 ->  Seq Scan on s01 s01_1  (cost=0.00..27.00 rows=1700 width=20)
                     ->  Materialize  (cost=0.00..33.55 rows=1570 width=8)
                           ->  Seq Scan on r01  (cost=0.00..25.70 rows=1570 width=8)
(12 rows)

explain select * from 
(
s align r on true with (ts,te,ts,te)
) x where c < 3 and ts > 3;
                                          QUERY PLAN                                          
----------------------------------------------------------------------------------------------
 Subquery Scan on x  (cost=26589.43..35738.60 rows=98910 width=20)
   Filter: (x.ts > 3)
   ->  Adjustment(for ALIGN)  (cost=26589.43..32029.48 rows=296730 width=36)
         ->  Sort  (cost=26589.43..26836.70 rows=98910 width=36)
               Sort Key: s.rn, (GREATEST(s.ts, r.ts)), (LEAST(s.te, r.te))
               ->  Nested Loop Left Join  (cost=0.00..15677.45 rows=98910 width=36)
                     Join Filter: ((s.ts < r.te) AND (s.te > r.ts))
                     ->  Subquery Scan on s  (cost=0.00..69.50 rows=567 width=28)
                           Filter: (s.c < 3)
                           ->  WindowAgg  (cost=0.00..48.25 rows=1700 width=20)
                                 ->  Seq Scan on s s_1  (cost=0.00..27.00 rows=1700 width=20)
                     ->  Materialize  (cost=0.00..33.55 rows=1570 width=8)
                           ->  Seq Scan on r  (cost=0.00..25.70 rows=1570 width=8)
(13 rows)

explain select * from 
(
s align r on true with (ts,te,ts,te)
) x where c < 3 or ts > 3;
                                          QUERY PLAN                                          
----------------------------------------------------------------------------------------------
 Subquery Scan on x  (cost=81865.77..111521.37 rows=494260 width=20)
   Filter: ((x.c < 3) OR (x.ts > 3))
   ->  Adjustment(for ALIGN)  (cost=81865.77..98176.35 rows=889668 width=36)
         ->  Sort  (cost=81865.77..82607.16 rows=296556 width=36)
               Sort Key: (row_number() OVER (?)), (GREATEST(s.ts, r.ts)), (LEAST(s.te, r.te))
               ->  Nested Loop Left Join  (cost=0.00..46802.38 rows=296556 width=36)
                     Join Filter: ((s.ts < r.te) AND (s.te > r.ts))
                     ->  WindowAgg  (cost=0.00..48.25 rows=1700 width=20)
                           ->  Seq Scan on s  (cost=0.00..27.00 rows=1700 width=20)
                     ->  Materialize  (cost=0.00..33.55 rows=1570 width=8)
                           ->  Seq Scan on r  (cost=0.00..25.70 rows=1570 width=8)
(11 rows)

