RENAME TABLE graphite TO old_graphite;

CREATE TABLE graphite ENGINE = GraphiteMergeTree (
    'graphite_rollup'
)
PARTITION BY toYYYYMM (Date)
ORDER BY
    (Path, Time) SETTINGS index_granularity = 8192 AS
    SELECT
        multiIf (
            like (Path, '%.policyPassCounter.%'), replaceOne (Path, 'policyPassCounter', 'passCounter'),
            like (Path, '%.policyDropsCounter.%'), replaceOne (Path, 'policyDropsCounter', 'dropCounter'),
            like (Path, '%.ipval.%'), replaceOne (Path, 'ipval', 'vlip'),
            like (Path, '%.tempBlacklist.blacklist.%'), replaceOne (Path, 'tempBlacklist.blacklist', 'tbl'),
            like (Path, '%.tempWhitelist.whitelist.%'), replaceOne (Path, 'tempWhitelist.whitelist', 'twl'),
            like (Path, '%.valid_ip_stats.tempblacklist_%'), replaceOne (Path, 'valid_ip_stats.tempblacklist_', 'tbl.'),
            like (Path, '%.valid_ip_stats.tempwhitelist_%'), replaceOne (Path, 'valid_ip_stats.tempwhitelist_', 'twl.'),
            like (Path, '%.valid_ip_stats.mcr_%'), replaceOne (Path, 'valid_ip_stats.mcr', 'mcr.valid'),
            like (Path, '%.valid_ip_stats.retr_%'), replaceOne (Path, 'valid_ip_stats.retr', 'retr.valid'),
            like (Path, '%.valid_ip_stats.tcpflood_%'), replaceOne (Path, 'valid_ip_stats.tcpflood', 'tcp.valid'),
            like (Path, '%.valid_ip_stats.httpflood_%'), replaceOne (Path, 'valid_ip_stats.httpflood', 'http.valid'),
            like (Path, '%.valid_ip_stats.ssl_%'), replaceOne (Path, 'valid_ip_stats.ssl', 'atls.valid'),
            like (Path, '%.valid_ip_stats.game_%'), replaceOne (Path, 'valid_ip_stats.game', 'game.valid'),
            like (Path, '%.valid_ip_stats.dnsflood_%'), replaceOne (Path, 'valid_ip_stats.dnsflood', 'dns.valid'),
            like (Path, '%.valid_ip_stats.sip_%'), replaceOne (Path, 'valid_ip_stats.sip', 'sip.valid'),
            like (Path, '%.valid_ip_stats.minecraft_%'), replaceOne (Path, 'valid_ip_stats.minecraft', 'mine.valid'),
            like (Path, '%.tempBlacklist%'), replaceOne (Path, '.tempBlacklist', '.tbl'),
            like (Path, '%.tempWhitelist%'), replaceOne (Path, '.tempWhitelist', '.twl'),
            like (Path, '%.blacklist%'), replaceOne (Path, '.blacklist', '.bl'),
            like (Path, '%.whitelist%'), replaceOne (Path, '.whitelist', '.wl'),
            like (Path, '%.ipfrag.%'), replaceOne (Path, '.ipfrag', '.frag'),
            like (Path, '%.regexFilter%'), replaceOne (Path, '.regexFilter', '.rex'),
            like (Path, '%.prefixLimiter%'), replaceOne (Path, '.prefixLimiter', '.dlim'),
            like (Path, '%.tcpProt.%'), replaceOne (Path, '.tcpProt', '.tcp'),
            like (Path, '%.minecraft.%'), replaceOne (Path, '.minecraft', '.mine'),
            like (Path, '%.connRateBlocker.%'), replaceOne (Path, '.connRateBlocker', '.crb'),
            like (Path, '%.tcpSplicer.%'), replaceOne (Path, '.tcpSplicer', '.spli'),
            like (Path, '%.connlimiter.%'), replaceOne (Path, '.connlimiter', '.lcon'),
            like (Path, '%.httpFloodProt.%'), replaceOne (Path, '.httpFloodProt', '.http'),
            like (Path, '%.sourceLimiter.%'), replaceOne (Path, '.sourceLimiter', '.sorb'),
            like (Path, '%.rateLimiter%'), replaceOne (Path, '.rateLimiter', '.lim'),
            like (Path, '%.dnsProt.%'), replaceOne (Path, '.dnsProt', '.dns'),
            like (Path, '%.gameProt.%'), replaceOne (Path, '.gameProt', '.game'),
            like (Path, '%.wargaming.%'), replaceOne (Path, '.wargaming', '.wg'),
            like (Path, '%.valveQueryCacher.%'), replaceOne (Path, '.valveQueryCacher', '.sour'),
            like (Path, '%.tlsProt.%'), replaceOne (Path, '.tlsProt', '.atls'),
            like (Path, '%.tlsFloodProt.%'), replaceOne (Path, '.tlsFloodProt', '.itls'),
            like (Path, '%.sipProt.%'), replaceOne (Path, '.sipProt', '.sip'),
            like (Path, '%.ip6val.%'), replaceOne (Path, '.ip6val', '.vlip6'),
            Path) AS Path, Value, Time, Date, Timestamp
    FROM
        old_graphite;

RENAME TABLE graphite_tagged TO old_graphite_tagged;

CREATE TABLE graphite_tagged ENGINE = ReplacingMergeTree (
    Version
)
PARTITION BY toYYYYMM (Date)
ORDER BY
    (Tag1, Path, Date) SETTINGS index_granularity = 8192 AS
    SELECT
        Date, Tag1, multiIf (
            like (Path, '%.policyPassCounter.%'), replaceOne (Path, 'policyPassCounter', 'passCounter'),
            like (Path, '%.policyDropsCounter.%'), replaceOne (Path, 'policyDropsCounter', 'dropCounter'),
            like (Path, '%.ipval.%'), replaceOne (Path, 'ipval', 'vlip'),
            like (Path, '%.tempBlacklist.blacklist.%'), replaceOne (Path, 'tempBlacklist.blacklist', 'tbl'),
            like (Path, '%.tempWhitelist.whitelist.%'), replaceOne (Path, 'tempWhitelist.whitelist', 'twl'),
            like (Path, '%.valid_ip_stats.tempblacklist_%'), replaceOne (Path, 'valid_ip_stats.tempblacklist_', 'tbl.'),
            like (Path, '%.valid_ip_stats.tempwhitelist_%'), replaceOne (Path, 'valid_ip_stats.tempwhitelist_', 'twl.'),
            like (Path, '%.valid_ip_stats.mcr_%'), replaceOne (Path, 'valid_ip_stats.mcr', 'mcr.valid'),
            like (Path, '%.valid_ip_stats.retr_%'), replaceOne (Path, 'valid_ip_stats.retr', 'retr.valid'),
            like (Path, '%.valid_ip_stats.tcpflood_%'), replaceOne (Path, 'valid_ip_stats.tcpflood', 'tcp.valid'),
            like (Path, '%.valid_ip_stats.httpflood_%'), replaceOne (Path, 'valid_ip_stats.httpflood', 'http.valid'),
            like (Path, '%.valid_ip_stats.ssl_%'), replaceOne (Path, 'valid_ip_stats.ssl', 'atls.valid'),
            like (Path, '%.valid_ip_stats.game_%'), replaceOne (Path, 'valid_ip_stats.game', 'game.valid'),
            like (Path, '%.valid_ip_stats.dnsflood_%'), replaceOne (Path, 'valid_ip_stats.dnsflood', 'dns.valid'),
            like (Path, '%.valid_ip_stats.sip_%'), replaceOne (Path, 'valid_ip_stats.sip', 'sip.valid'),
            like (Path, '%.valid_ip_stats.minecraft_%'), replaceOne (Path, 'valid_ip_stats.minecraft', 'mine.valid'),
            like (Path, '%.tempBlacklist%'), replaceOne (Path, '.tempBlacklist', '.tbl'),
            like (Path, '%.tempWhitelist%'), replaceOne (Path, '.tempWhitelist', '.twl'),
            like (Path, '%.blacklist%'), replaceOne (Path, '.blacklist', '.bl'),
            like (Path, '%.whitelist%'), replaceOne (Path, '.whitelist', '.wl'),
            like (Path, '%.ipfrag.%'), replaceOne (Path, '.ipfrag', '.frag'),
            like (Path, '%.regexFilter%'), replaceOne (Path, '.regexFilter', '.rex'),
            like (Path, '%.prefixLimiter%'), replaceOne (Path, '.prefixLimiter', '.dlim'),
            like (Path, '%.tcpProt.%'), replaceOne (Path, '.tcpProt', '.tcp'),
            like (Path, '%.minecraft.%'), replaceOne (Path, '.minecraft', '.mine'),
            like (Path, '%.connRateBlocker.%'), replaceOne (Path, '.connRateBlocker', '.crb'),
            like (Path, '%.tcpSplicer.%'), replaceOne (Path, '.tcpSplicer', '.spli'),
            like (Path, '%.connlimiter.%'), replaceOne (Path, '.connlimiter', '.lcon'),
            like (Path, '%.httpFloodProt.%'), replaceOne (Path, '.httpFloodProt', '.http'),
            like (Path, '%.sourceLimiter.%'), replaceOne (Path, '.sourceLimiter', '.sorb'),
            like (Path, '%.rateLimiter%'), replaceOne (Path, '.rateLimiter', '.lim'),
            like (Path, '%.dnsProt.%'), replaceOne (Path, '.dnsProt', '.dns'),
            like (Path, '%.gameProt.%'), replaceOne (Path, '.gameProt', '.game'),
            like (Path, '%.wargaming.%'), replaceOne (Path, '.wargaming', '.wg'),
            like (Path, '%.valveQueryCacher.%'), replaceOne (Path, '.valveQueryCacher', '.sour'),
            like (Path, '%.tlsProt.%'), replaceOne (Path, '.tlsProt', '.atls'),
            like (Path, '%.tlsFloodProt.%'), replaceOne (Path, '.tlsFloodProt', '.itls'),
            like (Path, '%.sipProt.%'), replaceOne (Path, '.sipProt', '.sip'),
            like (Path, '%.ip6val.%'), replaceOne (Path, '.ip6val', '.vlip6'),
            Path) AS Path, Tags, Version
    FROM
        old_graphite_tagged;

TRUNCATE TABLE graphite_index;

DROP TABLE old_graphite;

DROP TABLE old_graphite_tagged;
