--
-- Test case: TEMPORAL ALIGNER
--
drop table if exists r01, s01;
create table r01
(
	a char, 
	b char, 
	ts int, 
	te int
);
create table s01
(
	c int, 
	d char, 
	ts int, 
	te int
);
insert into r01 values
('a','B',1,7), 
('b','B',3,9), 
('c','G',8,10);
insert into s01 values
(1,'B',2,5), 
(2,'B',3,4), 
(3,'B',7,9);
-- Temporal aligner test: equality qualifiers only, simple query...
select * from 
(
	r01 align s01 on r01.b=s01.d with (ts,te,ts,te)
) x;
 a | b | ts | te 
---+---+----+----
 a | B |  1 |  2
 a | B |  2 |  5
 a | B |  3 |  4
 a | B |  5 |  7
 b | B |  3 |  4
 b | B |  3 |  5
 b | B |  5 |  7
 b | B |  7 |  9
 c | G |  8 | 10
(9 rows)

-- Temporal aligner test: targetlist of outer query *not* A_STAR... 
select a, count(a) from 
(
	r01 align s01 on r01.b=s01.d with (ts,te,ts,te)
) x 
group by a;
 a | count 
---+-------
 c |     1
 b |     4
 a |     4
(3 rows)

--
-- Test column positions where ts and te are not the last two columns
--
drop table if exists r02, s02;
create table r02
(
	a char, 
	ts int, 
	te int, 
	b char
);
create table s02
(
	c int, 
	ts int, 
	d char, 
	te int
);
insert into r02 values
('a',1,7,'B'), 
('b',3,9,'B'), 
('c',8,10,'G');
insert into s02 values
(1,2,'B',5), 
(2,3,'B',4), 
(3,7,'B',9);
-- Temporal aligner test: equality qualifiers only, simple query...
select * from 
(
	r02 align s02 on r02.b=s02.d with (ts,te,ts,te)
) x;
 a | ts | te | b 
---+----+----+---
 a |  1 |  2 | B
 a |  2 |  5 | B
 a |  3 |  4 | B
 a |  5 |  7 | B
 b |  3 |  4 | B
 b |  3 |  5 | B
 b |  5 |  7 | B
 b |  7 |  9 | B
 c |  8 | 10 | G
(9 rows)

-- Temporal aligner test: targetlist of outer query *not* A_STAR... 
select a, count(a) from 
(
	r02 align s02 on r02.b=s02.d with (ts,te,ts,te)
) x 
group by a;
 a | count 
---+-------
 c |     1
 b |     4
 a |     4
(3 rows)

--
-- This is the test case number 03
-- From paper "Temporal alignment", page 438
--
drop table if exists r03,s03;
create table r03(a char, ts int, te int);
create table s03(a char, ts int, te int);
insert into r03 values
('a',1,8), 
('j',2,6), 
('a',8,12);
insert into s03 values
('a',1,8), 
('j',2,6), 
('a',8,12);
select * from 
(
	r03 align s03 on r03.a=s03.a or r03.a!=s03.a with (ts,te,ts,te)
) x;
 a | ts | te 
---+----+----
 a |  1 |  8
 a |  2 |  6
 j |  2 |  6
 a |  8 | 12
(4 rows)
drop table if exists r04,s04;
create table r04(a char, ts int, te int);
create table s04(a char, ts int, te int);
insert into r04 values
('a',1,8);
insert into s04 values
('j',2,6); 
select * from 
(
	r04 align s04 on true with (ts,te,ts,te)
) x;
 a | ts | te 
---+----+----
 a |  1 |  2
 a |  2 |  6
 a |  6 |  8
(3 rows)


