Skip to content

Instantly share code, notes, and snippets.

@nad2000
Last active December 30, 2015 13:59
Show Gist options
  • Save nad2000/7839362 to your computer and use it in GitHub Desktop.
Save nad2000/7839362 to your computer and use it in GitHub Desktop.
Create unidirectional sample set (table) from bi-directional samples
-- PostgreSQL
DROP TABLE IF EXISTS uview5_uni;
CREATE TABLE uview5_uni AS
SELECT
start_time,
CASE e WHEN 1 THEN mac_addr_1 ELSE mac_addr_2 END src_mac_addr,
CASE e WHEN 1 THEN mac_addr_2 ELSE mac_addr_1 END des_mac_addr,
CASE e WHEN 1 THEN byte_count_1 ELSE byte_count_2 END byte_count,
CASE e WHEN 1 THEN packet_count_1 ELSE packet_count_2 END packet_count,
last_time,
vlan_id,vlan_id2,mpls_tag,mpls_tag2,
application_id,
CASE e WHEN 1 THEN ip_addr_1 ELSE ip_addr_2 END src_ip_addr,
CASE e WHEN 1 THEN ip_addr_2 ELSE ip_addr_1 END dst_ip_addr,
CASE e WHEN 1 THEN port_1 ELSE port_2 END src_port,
CASE e WHEN 1 THEN port_2 ELSE port_1 END dst_port,
ip_protocol,
zero_window_events,
fragmented_count,
diff_serv,
flags,
flow_id,
ethertype
FROM uview5, (VALUES (1),(2)) AS ep(e)
WHERE
( e=1 AND packet_count_1 > 0 ) OR ( e=2 AND packet_count_2 > 0 );
CREATE OR REPLACE FUNCTION bi2uni(tbl text)
RETURNS VOID LANGUAGE plpgsql AS $$BEGIN
-- PostgreSQL
EXECUTE
'CREATE TABLE '||tbl||'_uni AS
SELECT
start_time,
CASE e WHEN 1 THEN mac_addr_1 ELSE mac_addr_2 END src_mac_addr,
CASE e WHEN 1 THEN mac_addr_2 ELSE mac_addr_1 END des_mac_addr,
CASE e WHEN 1 THEN byte_count_1 ELSE byte_count_2 END byte_count,
CASE e WHEN 1 THEN packet_count_1 ELSE packet_count_2 END packet_count,
last_time,
vlan_id,vlan_id2,mpls_tag,mpls_tag2,
application_id,
CASE e WHEN 1 THEN ip_addr_1 ELSE ip_addr_2 END src_ip_addr,
CASE e WHEN 1 THEN ip_addr_2 ELSE ip_addr_1 END dst_ip_addr,
CASE e WHEN 1 THEN port_1 ELSE port_2 END src_port,
CASE e WHEN 1 THEN port_2 ELSE port_1 END dst_port,
ip_protocol,
zero_window_events,
fragmented_count,
diff_serv,
flags,
flow_id,
ethertype
FROM '||tbl||', (VALUES (1),(2)) AS ep(e)
WHERE
( e=1 AND packet_count_1 > 0 ) OR ( e=2 AND packet_count_2 > 0 );
CREATE INDEX '||tbl||'_uni_idx ON '||tbl||'_uni(last_time);
';
END;$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment