pdbench/census/Queries/fun-q5.sql

89 lines
2.0 KiB
PL/PgSQL

CREATE OR REPLACE FUNCTION filter5() RETURNS void AS $$
DECLARE
filler RECORD;
i int4;
BEGIN
--CREATE TABLE xc (id int4, hid text, wid text, value int4) WITHOUT OIDS;
--i:=0;
LOOP
SELECT INTO filler
_f1.cid as cleft, _f2.cid as cright, _f1.hid as hleft, _f2.hid as hright
FROM f5 _f1, f5 _f2
WHERE _f1.tid = _f2.tid and _f1.Col IN ('POWSTATE1','POWSTATE2') and
_f2.Col IN ('POWSTATE1','POWSTATE2') and _f1.cid > _f2.cid;
IF NOT FOUND THEN EXIT; END IF;
EXECUTE '
create table k1 without oids as
select _c.* from c5 _c, f5 _f
where ' || quote_literal(filler.cleft) || ' = _f.cid and _f.hid = _c.hid;
';
EXECUTE '
create table k2 without oids as
select _c.* from c5 _c, f5 _f
where ' || quote_literal(filler.cright) || ' = _f.cid and _f.hid = _c.hid;
';
EXECUTE '
update f5
set cid = ' || quote_literal(filler.cleft) || '|| \'x\' || ' || quote_literal(filler.cright) || '
where cid =' || quote_literal(filler.cleft) || ' or cid=' || quote_literal(filler.cright) || ';';
EXECUTE '
create table ctemp5 as
select distinct a.hid, a.wid || \'x\' || b.wid as wid, a.value
from k1 a, k2 b
union all
select distinct b.hid, a.wid || \'x\' || b.wid as wid, b.value
from k1 a, k2 b;
delete from c5 where
hid in (select hid from k1) or hid in (select hid from k2);
';
EXECUTE '
create table toinsert5 as
select _c1.value as value, _c1.hid as hid, _c1.wid as wid, _f2.hid as fhid
from ctemp5 _c1, f5 _f1, f5 _f2
where _f1.tid = _f2.tid and _f1.hid = _c1.hid and _f1.cid = _f2.cid and
_f1.Col in (\'POWSTATE1\',\'POWSTATE2\')
and _f2.Col in (\'POWSTATE1\',\'POWSTATE2\');
insert into c5
select * from ctemp5
where value in (
select value from toinsert5
where toinsert5.wid = ctemp5.wid and toinsert5.fhid = ctemp5.hid
and toinsert5.hid <> ctemp5.hid);
';
--EXECUTE '
--insert into xc
--SELECT '|| quote_literal(i) || ', * FROM c5;';
EXECUTE '
drop table k1;
drop table k2;
drop table ctemp5;
drop table toinsert5;
';
--i := i+1;
END LOOP;
RETURN;
END;
$$ LANGUAGE plpgsql;