create or replace function get_results(p_machine text, p_branch text, p_cc text) returns table (extra_columns int, test1 float, test2 float, test3 float, test4 float, test5 float, test6 float, test7 float, test8 float) as
$$
	select
		m.extra_columns,
		sum(m.ms / p.ms - 1) filter (where m.test_id=1) AS test_1,
		sum(m.ms / p.ms - 1) filter (where m.test_id=2) AS test_2,
		sum(m.ms / p.ms - 1) filter (where m.test_id=3) AS test_3,
		sum(m.ms / p.ms - 1) filter (where m.test_id=4) AS test_4,
		sum(m.ms / p.ms - 1) filter (where m.test_id=5) AS test_5,
		sum(m.ms / p.ms - 1) filter (where m.test_id=6) AS test_6,
		sum(m.ms / p.ms - 1) filter (where m.test_id=7) AS test_7,
		sum(m.ms / p.ms - 1) filter (where m.test_id=8) AS test_8
	FROM
	(select test_id,extra_columns,avg(milliseconds) ms from deform_results where machine = p_machine and branch = 'master' and cc = p_cc group by test_id,extra_columns) AS m
	INNER JOIN
		(select test_id,extra_columns,avg(milliseconds) ms from deform_results where machine = p_machine and branch = p_branch and cc = p_cc group by test_id,extra_columns) AS p
	ON m.test_id = p.test_id AND m.extra_columns = p.extra_columns
	GROUP BY m.extra_columns
	ORDER BY m.extra_columns;
$$ language sql;

select * from get_results('m2', 'speedup_tuple_deform2~2','clang');
select * from get_results('m2', 'speedup_tuple_deform2~2+OPTIMIZE_BYVAL','clang');
select * from get_results('m2', 'speedup_tuple_deform2~1','clang');
select * from get_results('m2', 'speedup_tuple_deform2~1+OPTIMIZE_BYVAL','clang');
select * from get_results('m2', 'speedup_tuple_deform2','clang');
select * from get_results('m2', 'speedup_tuple_deform2+OPTIMIZE_BYVAL','clang');

select * from get_results('amd3990x', 'speedup_tuple_deform2~2','gcc');
select * from get_results('amd3990x', 'speedup_tuple_deform2~2+OPTIMIZE_BYVAL','gcc');
select * from get_results('amd3990x', 'speedup_tuple_deform2~1','gcc');
select * from get_results('amd3990x', 'speedup_tuple_deform2~1+OPTIMIZE_BYVAL','gcc');
select * from get_results('amd3990x', 'speedup_tuple_deform2','gcc');
select * from get_results('amd3990x', 'speedup_tuple_deform2+OPTIMIZE_BYVAL','gcc');


select * from get_results('amd3990x', 'speedup_tuple_deform2~2','clang');
select * from get_results('amd3990x', 'speedup_tuple_deform2~2+OPTIMIZE_BYVAL','clang');
select * from get_results('amd3990x', 'speedup_tuple_deform2~1','clang');
select * from get_results('amd3990x', 'speedup_tuple_deform2~1+OPTIMIZE_BYVAL','clang');
select * from get_results('amd3990x', 'speedup_tuple_deform2','clang');
select * from get_results('amd3990x', 'speedup_tuple_deform2+OPTIMIZE_BYVAL','clang');

select * from get_results('amd7945hx', 'speedup_tuple_deform2~2','gcc');
select * from get_results('amd7945hx', 'speedup_tuple_deform2~2+OPTIMIZE_BYVAL','gcc');
select * from get_results('amd7945hx', 'speedup_tuple_deform2~1','gcc');
select * from get_results('amd7945hx', 'speedup_tuple_deform2~1+OPTIMIZE_BYVAL','gcc');
select * from get_results('amd7945hx', 'speedup_tuple_deform2','gcc');
select * from get_results('amd7945hx', 'speedup_tuple_deform2+OPTIMIZE_BYVAL','gcc');


select * from get_results('amd7945hx', 'speedup_tuple_deform2~2','clang');
select * from get_results('amd7945hx', 'speedup_tuple_deform2~2+OPTIMIZE_BYVAL','clang');
select * from get_results('amd7945hx', 'speedup_tuple_deform2~1','clang');
select * from get_results('amd7945hx', 'speedup_tuple_deform2~1+OPTIMIZE_BYVAL','clang');
select * from get_results('amd7945hx', 'speedup_tuple_deform2','clang');
select * from get_results('amd7945hx', 'speedup_tuple_deform2+OPTIMIZE_BYVAL','clang');


select * from get_results('Xeon_8370C', 'speedup_tuple_deform2~2','gcc');
select * from get_results('Xeon_8370C', 'speedup_tuple_deform2~2+OPTIMIZE_BYVAL','gcc');
select * from get_results('Xeon_8370C', 'speedup_tuple_deform2~1','gcc');
select * from get_results('Xeon_8370C', 'speedup_tuple_deform2~1+OPTIMIZE_BYVAL','gcc');
select * from get_results('Xeon_8370C', 'speedup_tuple_deform2','gcc');
select * from get_results('Xeon_8370C', 'speedup_tuple_deform2+OPTIMIZE_BYVAL','gcc');


select * from get_results('Xeon_8370C', 'speedup_tuple_deform2~2','clang');
select * from get_results('Xeon_8370C', 'speedup_tuple_deform2~2+OPTIMIZE_BYVAL','clang');
select * from get_results('Xeon_8370C', 'speedup_tuple_deform2~1','clang');
select * from get_results('Xeon_8370C', 'speedup_tuple_deform2~1+OPTIMIZE_BYVAL','clang');
select * from get_results('Xeon_8370C', 'speedup_tuple_deform2','clang');
select * from get_results('Xeon_8370C', 'speedup_tuple_deform2+OPTIMIZE_BYVAL','clang');

