-- CREATE TABLE IF NOT EXISTS single_t.events
-- (
--     session_id                                     UInt64,
--     project_id                                     UInt32,
--     event_type Enum8('CLICK'=0, 'INPUT'=1, 'PAGE'=2,'RESOURCE'=3,'REQUEST'=4,'PERFORMANCE'=5,'LONGTASK'=6,'ERROR'=7,'CUSTOM'=8),
--     tracker_version LowCardinality(String),
--     rev_id Nullable(String),
--     user_uuid                                      UUID,
--     user_os LowCardinality(String),
--     user_os_version LowCardinality(Nullable(String)),
--     user_browser LowCardinality(String),
--     user_browser_version LowCardinality(Nullable(String)),
--     user_device Nullable(String),
--     user_device_type Enum8('other'=0, 'desktop'=1, 'mobile'=2),
--     user_country Enum8('UN'=-128, 'RW'=-127, 'SO'=-126, 'YE'=-125, 'IQ'=-124, 'SA'=-123, 'IR'=-122, 'CY'=-121, 'TZ'=-120, 'SY'=-119, 'AM'=-118, 'KE'=-117, 'CD'=-116, 'DJ'=-115, 'UG'=-114, 'CF'=-113, 'SC'=-112, 'JO'=-111, 'LB'=-110, 'KW'=-109, 'OM'=-108, 'QA'=-107, 'BH'=-106, 'AE'=-105, 'IL'=-104, 'TR'=-103, 'ET'=-102, 'ER'=-101, 'EG'=-100, 'SD'=-99, 'GR'=-98, 'BI'=-97, 'EE'=-96, 'LV'=-95, 'AZ'=-94, 'LT'=-93, 'SJ'=-92, 'GE'=-91, 'MD'=-90, 'BY'=-89, 'FI'=-88, 'AX'=-87, 'UA'=-86, 'MK'=-85, 'HU'=-84, 'BG'=-83, 'AL'=-82, 'PL'=-81, 'RO'=-80, 'XK'=-79, 'ZW'=-78, 'ZM'=-77, 'KM'=-76, 'MW'=-75, 'LS'=-74, 'BW'=-73, 'MU'=-72, 'SZ'=-71, 'RE'=-70, 'ZA'=-69, 'YT'=-68, 'MZ'=-67, 'MG'=-66, 'AF'=-65, 'PK'=-64, 'BD'=-63, 'TM'=-62, 'TJ'=-61, 'LK'=-60, 'BT'=-59, 'IN'=-58, 'MV'=-57, 'IO'=-56, 'NP'=-55, 'MM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'BS'=66, 'BM'=67, 'AI'=68, 'TT'=69, 'KN'=70, 'DM'=71, 'AG'=72, 'LC'=73, 'TC'=74, 'AW'=75, 'VG'=76, 'VC'=77, 'MS'=78, 'MF'=79, 'BL'=80, 'GP'=81, 'GD'=82, 'KY'=83, 'BZ'=84, 'SV'=85, 'GT'=86, 'HN'=87, 'NI'=88, 'CR'=89, 'VE'=90, 'EC'=91, 'CO'=92, 'PA'=93, 'HT'=94, 'AR'=95, 'CL'=96, 'BO'=97, 'PE'=98, 'MX'=99, 'PF'=100, 'PN'=101, 'KI'=102, 'TK'=103, 'TO'=104, 'WF'=105, 'WS'=106, 'NU'=107, 'MP'=108, 'GU'=109, 'PR'=110, 'VI'=111, 'UM'=112, 'AS'=113, 'CA'=114, 'US'=115, 'PS'=116, 'RS'=117, 'AQ'=118, 'SX'=119, 'CW'=120, 'BQ'=121, 'SS'=122),
--     datetime                                       DateTime,
--     label Nullable(String),
--     hesitation_time Nullable(UInt32),
--     name Nullable(String),
--     payload Nullable(String),
--     level Nullable(Enum8('info'=0, 'error'=1)) DEFAULT if(event_type == 'CUSTOM', 'info', null),
--     source Nullable(Enum8('js_exception'=0, 'bugsnag'=1, 'cloudwatch'=2, 'datadog'=3, 'elasticsearch'=4, 'newrelic'=5, 'rollbar'=6, 'sentry'=7, 'stackdriver'=8, 'sumologic'=9)),
--     message Nullable(String),
--     error_id Nullable(String),
--     duration Nullable(UInt16),
--     context Nullable(Enum8('unknown'=0, 'self'=1, 'same-origin-ancestor'=2, 'same-origin-descendant'=3, 'same-origin'=4, 'cross-origin-ancestor'=5, 'cross-origin-descendant'=6, 'cross-origin-unreachable'=7, 'multiple-contexts'=8)),
--     container_type Nullable(Enum8('window'=0, 'iframe'=1, 'embed'=2, 'object'=3)),
--     container_id Nullable(String),
--     container_name Nullable(String),
--     container_src Nullable(String),
--     url Nullable(String),
--     url_host Nullable(String)                      MATERIALIZED lower(domain(url)),
--     url_path Nullable(String)                      MATERIALIZED lower(pathFull(url)),
--     request_start Nullable(UInt16),
--     response_start Nullable(UInt16),
--     response_end Nullable(UInt16),
--     dom_content_loaded_event_start Nullable(UInt16),
--     dom_content_loaded_event_end Nullable(UInt16),
--     load_event_start Nullable(UInt16),
--     load_event_end Nullable(UInt16),
--     first_paint Nullable(UInt16),
--     first_contentful_paint Nullable(UInt16),
--     speed_index Nullable(UInt16),
--     visually_complete Nullable(UInt16),
--     time_to_interactive Nullable(UInt16),
--     ttfb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_start, request_start),
--                                                                    minus(response_start, request_start), Null),
--     ttlb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_end, request_start),
--                                                                    minus(response_end, request_start), Null),
--     response_time Nullable(UInt16)                 MATERIALIZED if(greaterOrEquals(response_end, response_start),
--                                                                    minus(response_end, response_start), Null),
--     dom_building_time Nullable(UInt16)             MATERIALIZED if(
--             greaterOrEquals(dom_content_loaded_event_start, response_end),
--             minus(dom_content_loaded_event_start, response_end), Null),
--     dom_content_loaded_event_time Nullable(UInt16) MATERIALIZED if(
--             greaterOrEquals(dom_content_loaded_event_end, dom_content_loaded_event_start),
--             minus(dom_content_loaded_event_end, dom_content_loaded_event_start), Null),
--     load_event_time Nullable(UInt16)               MATERIALIZED if(greaterOrEquals(load_event_end, load_event_start),
--                                                                    minus(load_event_end, load_event_start), Null),
--     min_fps Nullable(UInt8),
--     avg_fps Nullable(UInt8),
--     max_fps Nullable(UInt8),
--     min_cpu Nullable(UInt8),
--     avg_cpu Nullable(UInt8),
--     max_cpu Nullable(UInt8),
--     min_total_js_heap_size Nullable(UInt64),
--     avg_total_js_heap_size Nullable(UInt64),
--     max_total_js_heap_size Nullable(UInt64),
--     min_used_js_heap_size Nullable(UInt64),
--     avg_used_js_heap_size Nullable(UInt64),
--     max_used_js_heap_size Nullable(UInt64),
--     type Nullable(Enum8('other'=-1, 'script'=0, 'stylesheet'=1, 'fetch'=2, 'img'=3, 'media'=4)),
--     header_size Nullable(UInt16),
--     encoded_body_size Nullable(UInt32),
--     decoded_body_size Nullable(UInt32),
--     compression_ratio Nullable(Float32)            MATERIALIZED divide(decoded_body_size, encoded_body_size),
--     success Nullable(UInt8),
--     method Nullable(Enum8('GET' = 0, 'HEAD' = 1, 'POST' = 2, 'PUT' = 3, 'DELETE' = 4, 'CONNECT' = 5, 'OPTIONS' = 6, 'TRACE' = 7, 'PATCH' = 8)),
--     status Nullable(UInt16)
-- ) ENGINE = MergeTree
--       PARTITION BY toDate(datetime)
--       ORDER BY (project_id, datetime);
-- --       TTL datetime + INTERVAL 1 MONTH;
-- DROP TABLE single_t.events;
--
-- INSERT INTO eng_t.events4(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os,
--                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
--                             user_country, datetime, label, hesitation_time, name, payload, level, source, message,
--                             error_id, duration, context, container_type, container_id, container_name, container_src,
--                             url, request_start, response_start, response_end, dom_content_loaded_event_start,
--                             dom_content_loaded_event_end, load_event_start, load_event_end, first_paint,
--                             first_contentful_paint, speed_index, visually_complete, time_to_interactive, min_fps,
--                             avg_fps, max_fps, min_cpu, avg_cpu, max_cpu, min_total_js_heap_size, avg_total_js_heap_size,
--                             max_total_js_heap_size, min_used_js_heap_size, avg_used_js_heap_size, max_used_js_heap_size,
--                             type, header_size, encoded_body_size, decoded_body_size, success, method, status)
-- SELECT session_id,
--        project_id,
--        event_type,
--        tracker_version,
--        rev_id,
--        user_uuid,
--        user_os,
--        user_os_version,
--        user_browser,
--        user_browser_version,
--        user_device,
--        user_device_type,
--        user_country,
--        datetime,
--        label,
--        hesitation_time,
--        name,
--        payload,
--        level,
--        source,
--        message,
--        error_id,
--        duration,
--        context,
--        container_type,
--        container_id,
--        container_name,
--        container_src,
--        url,
--        request_start,
--        response_start,
--        response_end,
--        dom_content_loaded_event_start,
--        dom_content_loaded_event_end,
--        load_event_start,
--        load_event_end,
--        first_paint,
--        first_contentful_paint,
--        speed_index,
--        visually_complete,
--        time_to_interactive,
--        min_fps,
--        avg_fps,
--        max_fps,
--        min_cpu,
--        avg_cpu,
--        max_cpu,
--        min_total_js_heap_size,
--        avg_total_js_heap_size,
--        max_total_js_heap_size,
--        min_used_js_heap_size,
--        avg_used_js_heap_size,
--        max_used_js_heap_size,
--        type,
--        header_size,
--        encoded_body_size,
--        decoded_body_size,
--        success,
--        method,
--        status
-- FROM (
--          SELECT session_id,
--                 project_id,
--                 'CLICK' AS event_type,
--                 tracker_version,
--                 rev_id,
--                 user_uuid,
--                 user_os,
--                 user_os_version,
--                 user_browser,
--                 user_browser_version,
--                 user_device,
--                 user_device_type,
--                 user_country,
--                 datetime,
--                 label,
--                 hesitation_time,
--                 null    AS name,
--                 null    AS payload,
--                 null    AS level,
--                 null    AS source,
--                 null    AS message,
--                 null    AS error_id,
--                 null    AS duration,
--                 null    AS context,
--                 null    AS container_type,
--                 null    AS container_id,
--                 null    AS container_name,
--                 null    AS container_src,
--                 null    AS url,
--                 null    AS request_start,
--                 null    AS response_start,
--                 null    AS response_end,
--                 null    AS dom_content_loaded_event_start,
--                 null    AS dom_content_loaded_event_end,
--                 null    AS load_event_start,
--                 null    AS load_event_end,
--                 null    AS first_paint,
--                 null    AS first_contentful_paint,
--                 null    AS speed_index,
--                 null    AS visually_complete,
--                 null    AS time_to_interactive,
--                 null    AS min_fps,
--                 null    AS avg_fps,
--                 null    AS max_fps,
--                 null    AS min_cpu,
--                 null    AS avg_cpu,
--                 null    AS max_cpu,
--                 null    AS min_total_js_heap_size,
--                 null    AS avg_total_js_heap_size,
--                 null    AS max_total_js_heap_size,
--                 null    AS min_used_js_heap_size,
--                 null    AS avg_used_js_heap_size,
--                 null    AS max_used_js_heap_size,
--                 null    AS type,
--                 null    AS header_size,
--                 null    AS encoded_body_size,
--                 null    AS decoded_body_size,
--                 null    AS success,
--                 null    AS method,
--                 null    AS status
--          FROM clicks
--          UNION ALL
--          SELECT session_id,
--                 project_id,
--                 'ERROR' AS event_type,
--                 tracker_version,
--                 rev_id,
--                 user_uuid,
--                 user_os,
--                 user_os_version,
--                 user_browser,
--                 user_browser_version,
--                 user_device,
--                 user_device_type,
--                 user_country,
--                 datetime,
--                 null    AS label,
--                 null    AS hesitation_time,
--                 name,
--                 null    AS payload,
--                 null    AS level,
--                 null    AS source,
--                 message,
--                 error_id,
--                 null    AS duration,
--                 null    AS context,
--                 null    AS container_type,
--                 null    AS container_id,
--                 null    AS container_name,
--                 null    AS container_src,
--                 null    AS url,
--                 null    AS request_start,
--                 null    AS response_start,
--                 null    AS response_end,
--                 null    AS dom_content_loaded_event_start,
--                 null    AS dom_content_loaded_event_end,
--                 null    AS load_event_start,
--                 null    AS load_event_end,
--                 null    AS first_paint,
--                 null    AS first_contentful_paint,
--                 null    AS speed_index,
--                 null    AS visually_complete,
--                 null    AS time_to_interactive,
--                 null    AS min_fps,
--                 null    AS avg_fps,
--                 null    AS max_fps,
--                 null    AS min_cpu,
--                 null    AS avg_cpu,
--                 null    AS max_cpu,
--                 null    AS min_total_js_heap_size,
--                 null    AS avg_total_js_heap_size,
--                 null    AS max_total_js_heap_size,
--                 null    AS min_used_js_heap_size,
--                 null    AS avg_used_js_heap_size,
--                 null    AS max_used_js_heap_size,
--                 null    AS type,
--                 null    AS header_size,
--                 null    AS encoded_body_size,
--                 null    AS decoded_body_size,
--                 null    AS success,
--                 null    AS method,
--                 null    AS status
--          FROM errors
--          UNION ALL
--          SELECT session_id,
--                 project_id,
--                 'INPUT' AS event_type,
--                 tracker_version,
--                 rev_id,
--                 user_uuid,
--                 user_os,
--                 user_os_version,
--                 user_browser,
--                 user_browser_version,
--                 user_device,
--                 user_device_type,
--                 user_country,
--                 datetime,
--                 label,
--                 null    AS hesitation_time,
--                 null    AS name,
--                 null    AS payload,
--                 null    AS level,
--                 null    AS source,
--                 null    AS message,
--                 null    AS error_id,
--                 null    AS duration,
--                 null    AS context,
--                 null    AS container_type,
--                 null    AS container_id,
--                 null    AS container_name,
--                 null    AS container_src,
--                 null    AS url,
--                 null    AS request_start,
--                 null    AS response_start,
--                 null    AS response_end,
--                 null    AS dom_content_loaded_event_start,
--                 null    AS dom_content_loaded_event_end,
--                 null    AS load_event_start,
--                 null    AS load_event_end,
--                 null    AS first_paint,
--                 null    AS first_contentful_paint,
--                 null    AS speed_index,
--                 null    AS visually_complete,
--                 null    AS time_to_interactive,
--                 null    AS min_fps,
--                 null    AS avg_fps,
--                 null    AS max_fps,
--                 null    AS min_cpu,
--                 null    AS avg_cpu,
--                 null    AS max_cpu,
--                 null    AS min_total_js_heap_size,
--                 null    AS avg_total_js_heap_size,
--                 null    AS max_total_js_heap_size,
--                 null    AS min_used_js_heap_size,
--                 null    AS avg_used_js_heap_size,
--                 null    AS max_used_js_heap_size,
--                 null    AS type,
--                 null    AS header_size,
--                 null    AS encoded_body_size,
--                 null    AS decoded_body_size,
--                 null    AS success,
--                 null    AS method,
--                 null    AS status
--          FROM inputs
--          UNION ALL
--          SELECT session_id,
--                 project_id,
--                 'LONGTASK' event_type,
--                 tracker_version,
--                 rev_id,
--                 user_uuid,
--                 user_os,
--                 user_os_version,
--                 user_browser,
--                 user_browser_version,
--                 user_device,
--                 user_device_type,
--                 user_country,
--                 datetime,
--                 null AS    label,
--                 null AS    hesitation_time,
--                 null AS    name,
--                 null AS    payload,
--                 null AS    level,
--                 null AS    source,
--                 null AS    message,
--                 null AS    error_id,
--                 duration,
--                 context,
--                 container_type,
--                 container_id,
--                 container_name,
--                 container_src,
--                 null AS    url,
--                 null AS    request_start,
--                 null AS    response_start,
--                 null AS    response_end,
--                 null AS    dom_content_loaded_event_start,
--                 null AS    dom_content_loaded_event_end,
--                 null AS    load_event_start,
--                 null AS    load_event_end,
--                 null AS    first_paint,
--                 null AS    first_contentful_paint,
--                 null AS    speed_index,
--                 null AS    visually_complete,
--                 null AS    time_to_interactive,
--                 null AS    min_fps,
--                 null AS    avg_fps,
--                 null AS    max_fps,
--                 null AS    min_cpu,
--                 null AS    avg_cpu,
--                 null AS    max_cpu,
--                 null AS    min_total_js_heap_size,
--                 null AS    avg_total_js_heap_size,
--                 null AS    max_total_js_heap_size,
--                 null AS    min_used_js_heap_size,
--                 null AS    avg_used_js_heap_size,
--                 null AS    max_used_js_heap_size,
--                 null AS    type,
--                 null AS    header_size,
--                 null AS    encoded_body_size,
--                 null AS    decoded_body_size,
--                 null AS    success,
--                 null AS    method,
--                 null AS    status
--          FROM longtasks
--          UNION ALL
--          SELECT session_id,
--                 project_id,
--                 'PAGE'  event_type,
--                 tracker_version,
--                 rev_id,
--                 user_uuid,
--                 user_os,
--                 user_os_version,
--                 user_browser,
--                 user_browser_version,
--                 user_device,
--                 user_device_type,
--                 user_country,
--                 datetime,
--                 null AS label,
--                 null AS hesitation_time,
--                 null AS name,
--                 null AS payload,
--                 null AS level,
--                 null AS source,
--                 null AS message,
--                 null AS error_id,
--                 null AS duration,
--                 null AS context,
--                 null AS container_type,
--                 null AS container_id,
--                 null AS container_name,
--                 null AS container_src,
--                 url,
--                 request_start,
--                 response_start,
--                 response_end,
--                 dom_content_loaded_event_start,
--                 dom_content_loaded_event_end,
--                 load_event_start,
--                 load_event_end,
--                 first_paint,
--                 first_contentful_paint,
--                 speed_index,
--                 visually_complete,
--                 time_to_interactive,
--                 null AS min_fps,
--                 null AS avg_fps,
--                 null AS max_fps,
--                 null AS min_cpu,
--                 null AS avg_cpu,
--                 null AS max_cpu,
--                 null AS min_total_js_heap_size,
--                 null AS avg_total_js_heap_size,
--                 null AS max_total_js_heap_size,
--                 null AS min_used_js_heap_size,
--                 null AS avg_used_js_heap_size,
--                 null AS max_used_js_heap_size,
--                 null AS type,
--                 null AS header_size,
--                 null AS encoded_body_size,
--                 null AS decoded_body_size,
--                 null AS success,
--                 null AS method,
--                 null AS status
--          FROM pages
--          UNION ALL
--          SELECT session_id,
--                 project_id,
--                 'PERFORMANCE' AS event_type,
--                 tracker_version,
--                 rev_id,
--                 user_uuid,
--                 user_os,
--                 user_os_version,
--                 user_browser,
--                 user_browser_version,
--                 user_device,
--                 user_device_type,
--                 user_country,
--                 datetime,
--                 null AS label    label,
--                 null AS label    hesitation_time,
--                 null AS label    name,
--                 null AS label    payload,
--                 null AS label    level,
--                 null AS label    source,
--                 null AS label    message,
--                 null AS label    error_id,
--                 null AS label    duration,
--                 null AS label    context,
--                 null AS label    container_type,
--                 null AS label    container_id,
--                 null AS label    container_name,
--                 null AS label    container_src,
--                 null          AS labelurl,
--                 null AS label    request_start,
--                 null AS label    response_start,
--                 null AS label    response_end,
--                 null AS label    dom_content_loaded_event_start,
--                 null AS label    dom_content_loaded_event_end,
--                 null AS label    load_event_startnull,
--                 null AS label    load_event_end,
--                 null AS label    first_paint,
--                 null AS label    first_contentful_paint,
--                 null AS label    speed_index,
--                 null AS label    visually_complete,
--                 null AS label    time_to_interactive,
--                 min_fps,
--                 avg_fps,
--                 max_fps,
--                 min_cpu,
--                 avg_cpu,
--                 max_cpu,
--                 min_total_js_heap_size,
--                 avg_total_js_heap_size,
--                 max_total_js_heap_size,
--                 min_used_js_heap_size,
--                 avg_used_js_heap_size,
--                 max_used_js_heap_size,
--                 null AS label    type,
--                 null AS label    header_size,
--                 null AS label    encoded_body_size,
--                 null AS label    decoded_body_size,
--                 null AS label    success,
--                 null AS label    method,
--                 null AS label    status
--          FROM performance
--          UNION ALL
--          SELECT session_id,
--                 project_id,
--                 'RESOURCE' AS event_type,
--                 tracker_version,
--                 rev_id,
--                 user_uuid,
--                 user_os,
--                 user_os_version,
--                 user_browser,
--                 user_browser_version,
--                 user_device,
--                 user_device_type,
--                 user_country,
--                 datetime,
--                 null       AS label,
--                 null       AS hesitation_time,
--                 null       AS name,
--                 null       AS payload,
--                 null       AS level,
--                 null       AS source,
--                 null       AS message,
--                 null       AS error_id,
--                 duration,
--                 null       AS context,
--                 null       AS container_type,
--                 null       AS container_id,
--                 null       AS container_name,
--                 null       AS container_src,
--                 url,
--                 null       AS request_start,
--                 null       AS response_start,
--                 null       AS response_end,
--                 null       AS dom_content_loaded_event_start,
--                 null       AS dom_content_loaded_event_end,
--                 null       AS load_event_start,
--                 null       AS load_event_end,
--                 null       AS first_paint,
--                 null       AS first_contentful_paint,
--                 null       AS speed_index,
--                 null       AS visually_complete,
--                 null       AS time_to_interactive,
--                 null       AS min_fps,
--                 null       AS avg_fps,
--                 null       AS max_fps,
--                 null       AS min_cpu,
--                 null       AS avg_cpu,
--                 null       AS max_cpu,
--                 null       AS min_total_js_heap_size,
--                 null       AS avg_total_js_heap_size,
--                 null       AS max_total_js_heap_size,
--                 null       AS min_used_js_heap_size,
--                 null       AS avg_used_js_heap_size,
--                 null       AS max_used_js_heap_size,
--                 type,
--                 header_size,
--                 encoded_body_size,
--                 decoded_body_size,
--                 success,
--                 method,
--                 status
--          FROM resources);
--
--
-- INSERT INTO eng_t.events4(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os,
--                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
--                             user_country, datetime, label, hesitation_time)
-- SELECT session_id,
--        project_id,
--        'CLICK' AS event_type,
--        tracker_version,
--        rev_id,
--        user_uuid,
--        user_os,
--        user_os_version,
--        user_browser,
--        user_browser_version,
--        user_device,
--        user_device_type,
--        user_country,
--        datetime,
--        label,
--        hesitation_time
-- FROM clicks;
--
--
-- INSERT INTO eng_t.events4(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
--                             user_os_version,
--                             user_browser, user_browser_version, user_device, user_device_type, user_country, datetime,
--                             source, name, message, error_id)
--
-- SELECT 'ERROR' AS event_type,
--        session_id,
--        project_id,
--        tracker_version,
--        rev_id,
--        user_uuid,
--        user_os,
--        user_os_version,
--        user_browser,
--        user_browser_version,
--        user_device,
--        user_device_type,
--        user_country,
--        datetime,
--        source,
--        name,
--        message,
--        error_id
-- FROM errors;
--
--
--
-- INSERT INTO eng_t.events4(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
--                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
--                             user_country, datetime, label)
--
-- SELECT 'INPUT' AS event_type,
--        session_id,
--        project_id,
--        tracker_version,
--        rev_id,
--        user_uuid,
--        user_os,
--        user_os_version,
--        user_browser,
--        user_browser_version,
--        user_device,
--        user_device_type,
--        user_country,
--        datetime,
--        label
-- FROM inputs;
--
-- INSERT INTO eng_t.events4(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
--                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
--                             user_country, datetime, duration, context, container_type, container_id, container_name,
--                             container_src)
-- SELECT 'LONGTASK' AS event_type,
--        session_id,
--        project_id,
--        tracker_version,
--        rev_id,
--        user_uuid,
--        user_os,
--        user_os_version,
--        user_browser,
--        user_browser_version,
--        user_device,
--        user_device_type,
--        user_country,
--        datetime,
--        duration,
--        context,
--        container_type,
--        container_id,
--        container_name,
--        container_src
-- FROM longtasks;
--
--
-- INSERT INTO eng_t.events4(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
--                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
--                             user_country, datetime, url, request_start, response_start, response_end,
--                             dom_content_loaded_event_start, dom_content_loaded_event_end, load_event_start,
--                             load_event_end, first_paint, first_contentful_paint, speed_index, visually_complete,
--                             time_to_interactive)
-- SELECT 'PAGE' AS event_type,
--        session_id,
--        project_id,
--        tracker_version,
--        rev_id,
--        user_uuid,
--        user_os,
--        user_os_version,
--        user_browser,
--        user_browser_version,
--        user_device,
--        user_device_type,
--        user_country,
--        datetime,
--        url,
--        request_start,
--        response_start,
--        response_end,
--        dom_content_loaded_event_start,
--        dom_content_loaded_event_end,
--        load_event_start,
--        load_event_end,
--        first_paint,
--        first_contentful_paint,
--        speed_index,
--        visually_complete,
--        time_to_interactive
-- FROM pages;
--
-- INSERT INTO eng_t.events4(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
--                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
--                             user_country, datetime, min_fps, avg_fps, max_fps, min_cpu, avg_cpu, max_cpu,
--                             min_total_js_heap_size, avg_total_js_heap_size, max_total_js_heap_size,
--                             min_used_js_heap_size, avg_used_js_heap_size, max_used_js_heap_size)
-- SELECT 'PERFORMANCE' AS event_type,
--        session_id,
--        project_id,
--        tracker_version,
--        rev_id,
--        user_uuid,
--        user_os,
--        user_os_version,
--        user_browser,
--        user_browser_version,
--        user_device,
--        user_device_type,
--        user_country,
--        datetime,
--        min_fps,
--        avg_fps,
--        max_fps,
--        min_cpu,
--        avg_cpu,
--        max_cpu,
--        min_total_js_heap_size,
--        avg_total_js_heap_size,
--        max_total_js_heap_size,
--        min_used_js_heap_size,
--        avg_used_js_heap_size,
--        max_used_js_heap_size
-- FROM performance;
--
-- INSERT INTO eng_t.events4(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
--                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
--                             user_country, datetime, url, type, duration, header_size, encoded_body_size,
--                             decoded_body_size, success, method, status)
-- SELECT 'RESOURCE' AS event_type,
--        session_id,
--        project_id,
--        tracker_version,
--        rev_id,
--        user_uuid,
--        user_os,
--        user_os_version,
--        user_browser,
--        user_browser_version,
--        user_device,
--        user_device_type,
--        user_country,
--        datetime,
--        url,
--        type,
--        duration,
--        header_size,
--        encoded_body_size,
--        decoded_body_size,
--        success,
--        method,
--        status
-- FROM resources;
--
--
-- SELECT table, formatReadableSize(size) as size, rows, days, formatReadableSize(avgDaySize) as avgDaySize
-- FROM (
--          SELECT table,
--                 sum(bytes)                   AS size,
--                 sum(rows)                    AS rows,
--                 min(min_date)                AS min_date,
--                 max(max_date)                AS max_date,
--                 (max_date - min_date)        AS days,
--                 size / (max_date - min_date) AS avgDaySize
--          FROM system.parts
--          WHERE active
--          GROUP BY table
--          ORDER BY rows DESC
--          );
--
-- SELECT database,
--        table,
--        formatReadableSize(sum(bytes)) as size,
--        min(min_date)                  as min_date,
--        max(max_date)                  as max_date
-- FROM system.parts
-- WHERE active
-- GROUP BY database, table;
--
-- SELECT count(*)
-- FROM single_t.events;
-- -- 449 484 932
-- -- 449 484 932
--
-- SELECT (SELECT count(*) FROM clicks) + (SELECT count(*) FROM inputs) + (SELECT count(*) FROM longtasks) +
--        (SELECT count(*) FROM errors) + (SELECT count(*) FROM pages) + (SELECT count(*) FROM resources) +
--        (SELECT count(*) FROM performance) AS totl;
--
--
--
-- CREATE TABLE IF NOT EXISTS single_t.events3
-- (
--     session_id                                     UInt64,
--     project_id                                     UInt32,
--     event_type Enum8('CLICK'=0, 'INPUT'=1, 'PAGE'=2,'RESOURCE'=3,'REQUEST'=4,'PERFORMANCE'=5,'LONGTASK'=6,'ERROR'=7,'CUSTOM'=8),
--     tracker_version LowCardinality(String),
--     rev_id Nullable(String),
--     user_uuid                                      UUID,
--     user_os LowCardinality(String),
--     user_os_version LowCardinality(Nullable(String)),
--     user_browser LowCardinality(String),
--     user_browser_version LowCardinality(Nullable(String)),
--     user_device Nullable(String),
--     user_device_type Enum8('other'=0, 'desktop'=1, 'mobile'=2),
--     user_country Enum8('UN'=-128, 'RW'=-127, 'SO'=-126, 'YE'=-125, 'IQ'=-124, 'SA'=-123, 'IR'=-122, 'CY'=-121, 'TZ'=-120, 'SY'=-119, 'AM'=-118, 'KE'=-117, 'CD'=-116, 'DJ'=-115, 'UG'=-114, 'CF'=-113, 'SC'=-112, 'JO'=-111, 'LB'=-110, 'KW'=-109, 'OM'=-108, 'QA'=-107, 'BH'=-106, 'AE'=-105, 'IL'=-104, 'TR'=-103, 'ET'=-102, 'ER'=-101, 'EG'=-100, 'SD'=-99, 'GR'=-98, 'BI'=-97, 'EE'=-96, 'LV'=-95, 'AZ'=-94, 'LT'=-93, 'SJ'=-92, 'GE'=-91, 'MD'=-90, 'BY'=-89, 'FI'=-88, 'AX'=-87, 'UA'=-86, 'MK'=-85, 'HU'=-84, 'BG'=-83, 'AL'=-82, 'PL'=-81, 'RO'=-80, 'XK'=-79, 'ZW'=-78, 'ZM'=-77, 'KM'=-76, 'MW'=-75, 'LS'=-74, 'BW'=-73, 'MU'=-72, 'SZ'=-71, 'RE'=-70, 'ZA'=-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'=49, 'BB'=50, 'CV'=51, 'GY'=52, 'GF'=53, 'SR'=54, 'PM'=55, 'GL'=56, 'PY'=57, 'UY'=58, 'BR'=59, 'FK'=60, 'GS'=61, 'JM'=62, 'DO'=63, 'CU'=64, 'MQ'=65, 'BS'=66, 'BM'=67, 'AI'=68, 'TT'=69, 'KN'=70, 'DM'=71, 'AG'=72, 'LC'=73, 'TC'=74, 'AW'=75, 'VG'=76, 'VC'=77, 'MS'=78, 'MF'=79, 'BL'=80, 'GP'=81, 'GD'=82, 'KY'=83, 'BZ'=84, 'SV'=85, 'GT'=86, 'HN'=87, 'NI'=88, 'CR'=89, 'VE'=90, 'EC'=91, 'CO'=92, 'PA'=93, 'HT'=94, 'AR'=95, 'CL'=96, 'BO'=97, 'PE'=98, 'MX'=99, 'PF'=100, 'PN'=101, 'KI'=102, 'TK'=103, 'TO'=104, 'WF'=105, 'WS'=106, 'NU'=107, 'MP'=108, 'GU'=109, 'PR'=110, 'VI'=111, 'UM'=112, 'AS'=113, 'CA'=114, 'US'=115, 'PS'=116, 'RS'=117, 'AQ'=118, 'SX'=119, 'CW'=120, 'BQ'=121, 'SS'=122),
--     datetime                                       DateTime,
--     label Nullable(String),
--     hesitation_time Nullable(UInt32),
--     name Nullable(String),
--     payload Nullable(String),
--     level Nullable(Enum8('info'=0, 'error'=1)) DEFAULT if(event_type == 'CUSTOM', 'info', null),
--     source Nullable(Enum8('js_exception'=0, 'bugsnag'=1, 'cloudwatch'=2, 'datadog'=3, 'elasticsearch'=4, 'newrelic'=5, 'rollbar'=6, 'sentry'=7, 'stackdriver'=8, 'sumologic'=9)),
--     message Nullable(String),
--     error_id Nullable(String),
--     duration Nullable(UInt16),
--     context Nullable(Enum8('unknown'=0, 'self'=1, 'same-origin-ancestor'=2, 'same-origin-descendant'=3, 'same-origin'=4, 'cross-origin-ancestor'=5, 'cross-origin-descendant'=6, 'cross-origin-unreachable'=7, 'multiple-contexts'=8)),
--     container_type Nullable(Enum8('window'=0, 'iframe'=1, 'embed'=2, 'object'=3)),
--     container_id Nullable(String),
--     container_name Nullable(String),
--     container_src Nullable(String),
--     url Nullable(String),
--     url_host Nullable(String)                      MATERIALIZED lower(domain(url)),
--     url_path Nullable(String)                      MATERIALIZED lower(pathFull(url)),
--     request_start Nullable(UInt16),
--     response_start Nullable(UInt16),
--     response_end Nullable(UInt16),
--     dom_content_loaded_event_start Nullable(UInt16),
--     dom_content_loaded_event_end Nullable(UInt16),
--     load_event_start Nullable(UInt16),
--     load_event_end Nullable(UInt16),
--     first_paint Nullable(UInt16),
--     first_contentful_paint Nullable(UInt16),
--     speed_index Nullable(UInt16),
--     visually_complete Nullable(UInt16),
--     time_to_interactive Nullable(UInt16),
--     ttfb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_start, request_start),
--                                                                    minus(response_start, request_start), Null),
--     ttlb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_end, request_start),
--                                                                    minus(response_end, request_start), Null),
--     response_time Nullable(UInt16)                 MATERIALIZED if(greaterOrEquals(response_end, response_start),
--                                                                    minus(response_end, response_start), Null),
--     dom_building_time Nullable(UInt16)             MATERIALIZED if(
--             greaterOrEquals(dom_content_loaded_event_start, response_end),
--             minus(dom_content_loaded_event_start, response_end), Null),
--     dom_content_loaded_event_time Nullable(UInt16) MATERIALIZED if(
--             greaterOrEquals(dom_content_loaded_event_end, dom_content_loaded_event_start),
--             minus(dom_content_loaded_event_end, dom_content_loaded_event_start), Null),
--     load_event_time Nullable(UInt16)               MATERIALIZED if(greaterOrEquals(load_event_end, load_event_start),
--                                                                    minus(load_event_end, load_event_start), Null),
--     min_fps Nullable(UInt8),
--     avg_fps Nullable(UInt8),
--     max_fps Nullable(UInt8),
--     min_cpu Nullable(UInt8),
--     avg_cpu Nullable(UInt8),
--     max_cpu Nullable(UInt8),
--     min_total_js_heap_size Nullable(UInt64),
--     avg_total_js_heap_size Nullable(UInt64),
--     max_total_js_heap_size Nullable(UInt64),
--     min_used_js_heap_size Nullable(UInt64),
--     avg_used_js_heap_size Nullable(UInt64),
--     max_used_js_heap_size Nullable(UInt64),
--     type Nullable(Enum8('other'=-1, 'script'=0, 'stylesheet'=1, 'fetch'=2, 'img'=3, 'media'=4)),
--     header_size Nullable(UInt16),
--     encoded_body_size Nullable(UInt32),
--     decoded_body_size Nullable(UInt32),
--     compression_ratio Nullable(Float32)            MATERIALIZED divide(decoded_body_size, encoded_body_size),
--     success Nullable(UInt8),
--     method Nullable(Enum8('GET' = 0, 'HEAD' = 1, 'POST' = 2, 'PUT' = 3, 'DELETE' = 4, 'CONNECT' = 5, 'OPTIONS' = 6, 'TRACE' = 7, 'PATCH' = 8)),
--     status Nullable(UInt16)
-- ) ENGINE = MergeTree
--       PARTITION BY toDate(datetime)
--       ORDER BY (project_id, datetime,event_type);
--
-- -- INSERT INTO eng_t.events42(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os, user_os_version, user_browser, user_browser_version, user_device, user_device_type, user_country, datetime, label, hesitation_time, name, payload, level, source, message, error_id, duration, context, container_type, container_id, container_name, container_src, url, request_start, response_start, response_end, dom_content_loaded_event_start, dom_content_loaded_event_end, load_event_start, load_event_end, first_paint, first_contentful_paint, speed_index, visually_complete, time_to_interactive, min_fps, avg_fps, max_fps, min_cpu, avg_cpu, max_cpu, min_total_js_heap_size, avg_total_js_heap_size, max_total_js_heap_size, min_used_js_heap_size, avg_used_js_heap_size, max_used_js_heap_size, type, header_size, encoded_body_size, decoded_body_size, success, method, status)
-- -- SELECT session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os, user_os_version, user_browser, user_browser_version, user_device, user_device_type, user_country, datetime, label, hesitation_time, name, payload, level, source, message, error_id, duration, context, container_type, container_id, container_name, container_src, url, request_start, response_start, response_end, dom_content_loaded_event_start, dom_content_loaded_event_end, load_event_start, load_event_end, first_paint, first_contentful_paint, speed_index, visually_complete, time_to_interactive, min_fps, avg_fps, max_fps, min_cpu, avg_cpu, max_cpu, min_total_js_heap_size, avg_total_js_heap_size, max_total_js_heap_size, min_used_js_heap_size, avg_used_js_heap_size, max_used_js_heap_size, type, header_size, encoded_body_size, decoded_body_size, success, method, status FROM single_t.events;

CREATE TABLE IF NOT EXISTS single_t.events3
(
    session_id                                     UInt64,
    project_id                                     UInt32,
    event_type Enum8('CLICK'=0, 'INPUT'=1, 'PAGE'=2,'RESOURCE'=3,'REQUEST'=4,'PERFORMANCE'=5,'LONGTASK'=6,'ERROR'=7,'CUSTOM'=8),
    tracker_version LowCardinality(String),
    rev_id Nullable(String),
    user_uuid                                      UUID,
    user_os LowCardinality(String),
    user_os_version LowCardinality(Nullable(String)),
    user_browser LowCardinality(String),
    user_browser_version LowCardinality(Nullable(String)),
    user_device Nullable(String),
    user_device_type Enum8('other'=0, 'desktop'=1, 'mobile'=2),
    user_country Enum8('UN'=-128, 'RW'=-127, 'SO'=-126, 'YE'=-125, 'IQ'=-124, 'SA'=-123, 'IR'=-122, 'CY'=-121, 'TZ'=-120, 'SY'=-119, 'AM'=-118, 'KE'=-117, 'CD'=-116, 'DJ'=-115, 'UG'=-114, 'CF'=-113, 'SC'=-112, 'JO'=-111, 'LB'=-110, 'KW'=-109, 'OM'=-108, 'QA'=-107, 'BH'=-106, 'AE'=-105, 'IL'=-104, 'TR'=-103, 'ET'=-102, 'ER'=-101, 'EG'=-100, 'SD'=-99, 'GR'=-98, 'BI'=-97, 'EE'=-96, 'LV'=-95, 'AZ'=-94, 'LT'=-93, 'SJ'=-92, 'GE'=-91, 'MD'=-90, 'BY'=-89, 'FI'=-88, 'AX'=-87, 'UA'=-86, 'MK'=-85, 'HU'=-84, 'BG'=-83, 'AL'=-82, 'PL'=-81, 'RO'=-80, 'XK'=-79, 'ZW'=-78, 'ZM'=-77, 'KM'=-76, 'MW'=-75, 'LS'=-74, 'BW'=-73, 'MU'=-72, 'SZ'=-71, 'RE'=-70, 'ZA'=-69, 'YT'=-68, 'MZ'=-67, 'MG'=-66, 'AF'=-65, 'PK'=-64, 'BD'=-63, 'TM'=-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'=57, 'UY'=58, 'BR'=59, 'FK'=60, 'GS'=61, 'JM'=62, 'DO'=63, 'CU'=64, 'MQ'=65, 'BS'=66, 'BM'=67, 'AI'=68, 'TT'=69, 'KN'=70, 'DM'=71, 'AG'=72, 'LC'=73, 'TC'=74, 'AW'=75, 'VG'=76, 'VC'=77, 'MS'=78, 'MF'=79, 'BL'=80, 'GP'=81, 'GD'=82, 'KY'=83, 'BZ'=84, 'SV'=85, 'GT'=86, 'HN'=87, 'NI'=88, 'CR'=89, 'VE'=90, 'EC'=91, 'CO'=92, 'PA'=93, 'HT'=94, 'AR'=95, 'CL'=96, 'BO'=97, 'PE'=98, 'MX'=99, 'PF'=100, 'PN'=101, 'KI'=102, 'TK'=103, 'TO'=104, 'WF'=105, 'WS'=106, 'NU'=107, 'MP'=108, 'GU'=109, 'PR'=110, 'VI'=111, 'UM'=112, 'AS'=113, 'CA'=114, 'US'=115, 'PS'=116, 'RS'=117, 'AQ'=118, 'SX'=119, 'CW'=120, 'BQ'=121, 'SS'=122),
    datetime                                       DateTime,
    label Nullable(String),
    hesitation_time Nullable(UInt32),
    name Nullable(String),
    payload Nullable(String),
    level Nullable(Enum8('info'=0, 'error'=1)) DEFAULT if(event_type == 'CUSTOM', 'info', null),
    source Nullable(Enum8('js_exception'=0, 'bugsnag'=1, 'cloudwatch'=2, 'datadog'=3, 'elasticsearch'=4, 'newrelic'=5, 'rollbar'=6, 'sentry'=7, 'stackdriver'=8, 'sumologic'=9)),
    message Nullable(String),
    error_id Nullable(String),
    duration Nullable(UInt16),
    context Nullable(Enum8('unknown'=0, 'self'=1, 'same-origin-ancestor'=2, 'same-origin-descendant'=3, 'same-origin'=4, 'cross-origin-ancestor'=5, 'cross-origin-descendant'=6, 'cross-origin-unreachable'=7, 'multiple-contexts'=8)),
    container_type Nullable(Enum8('window'=0, 'iframe'=1, 'embed'=2, 'object'=3)),
    container_id Nullable(String),
    container_name Nullable(String),
    container_src Nullable(String),
    url Nullable(String),
    url_host Nullable(String)                      MATERIALIZED lower(domain(url)),
    url_path Nullable(String)                      MATERIALIZED lower(pathFull(url)),
    request_start Nullable(UInt16),
    response_start Nullable(UInt16),
    response_end Nullable(UInt16),
    dom_content_loaded_event_start Nullable(UInt16),
    dom_content_loaded_event_end Nullable(UInt16),
    load_event_start Nullable(UInt16),
    load_event_end Nullable(UInt16),
    first_paint Nullable(UInt16),
    first_contentful_paint Nullable(UInt16),
    speed_index Nullable(UInt16),
    visually_complete Nullable(UInt16),
    time_to_interactive Nullable(UInt16),
    ttfb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_start, request_start),
                                                                   minus(response_start, request_start), Null),
    ttlb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_end, request_start),
                                                                   minus(response_end, request_start), Null),
    response_time Nullable(UInt16)                 MATERIALIZED if(greaterOrEquals(response_end, response_start),
                                                                   minus(response_end, response_start), Null),
    dom_building_time Nullable(UInt16)             MATERIALIZED if(
            greaterOrEquals(dom_content_loaded_event_start, response_end),
            minus(dom_content_loaded_event_start, response_end), Null),
    dom_content_loaded_event_time Nullable(UInt16) MATERIALIZED if(
            greaterOrEquals(dom_content_loaded_event_end, dom_content_loaded_event_start),
            minus(dom_content_loaded_event_end, dom_content_loaded_event_start), Null),
    load_event_time Nullable(UInt16)               MATERIALIZED if(greaterOrEquals(load_event_end, load_event_start),
                                                                   minus(load_event_end, load_event_start), Null),
    min_fps Nullable(UInt8),
    avg_fps Nullable(UInt8),
    max_fps Nullable(UInt8),
    min_cpu Nullable(UInt8),
    avg_cpu Nullable(UInt8),
    max_cpu Nullable(UInt8),
    min_total_js_heap_size Nullable(UInt64),
    avg_total_js_heap_size Nullable(UInt64),
    max_total_js_heap_size Nullable(UInt64),
    min_used_js_heap_size Nullable(UInt64),
    avg_used_js_heap_size Nullable(UInt64),
    max_used_js_heap_size Nullable(UInt64),
    type Nullable(Enum8('other'=-1, 'script'=0, 'stylesheet'=1, 'fetch'=2, 'img'=3, 'media'=4)),
    header_size Nullable(UInt16),
    encoded_body_size Nullable(UInt32),
    decoded_body_size Nullable(UInt32),
    compression_ratio Nullable(Float32)            MATERIALIZED divide(decoded_body_size, encoded_body_size),
    success Nullable(BOOLEAN),
    method Nullable(Enum8('GET' = 0, 'HEAD' = 1, 'POST' = 2, 'PUT' = 3, 'DELETE' = 4, 'CONNECT' = 5, 'OPTIONS' = 6, 'TRACE' = 7, 'PATCH' = 8)),
    status Nullable(UInt16)
) ENGINE = MergeTree
      PARTITION BY toDate(datetime)
      ORDER BY (project_id, datetime, event_type);

INSERT INTO eng_t.events4(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os,
                          user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                          user_country, datetime, label, hesitation_time)
SELECT session_id,
       project_id,
       'CLICK' AS event_type,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       label,
       hesitation_time
FROM clicks
WHERE mod(session_id, 2) = 1;


INSERT INTO eng_t.events4(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                          user_os_version,
                          user_browser, user_browser_version, user_device, user_device_type, user_country, datetime,
                          source, name, message, error_id)

SELECT 'ERROR' AS event_type,
       session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       source,
       name,
       message,
       error_id
FROM errors
WHERE mod(session_id, 2) = 0;
TRUNCATE TABLE eng_t.events4;
INSERT INTO eng_t.events4(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                          user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                          user_country, datetime, label)

SELECT 'INPUT' AS event_type,
       session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       label
FROM inputs
WHERE mod(session_id, 2) = 0;

INSERT INTO eng_t.events4(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                          user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                          user_country, datetime, duration, context, container_type, container_id, container_name,
                          container_src)
SELECT 'LONGTASK' AS event_type,
       session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       duration,
       context,
       container_type,
       container_id,
       container_name,
       container_src
FROM longtasks
WHERE mod(session_id, 2) = 0;


INSERT INTO eng_t.events4(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                          user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                          user_country, datetime, url, request_start, response_start, response_end,
                          dom_content_loaded_event_start, dom_content_loaded_event_end, load_event_start,
                          load_event_end, first_paint, first_contentful_paint, speed_index, visually_complete,
                          time_to_interactive)
SELECT 'PAGE' AS event_type,
       session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       url,
       request_start,
       response_start,
       response_end,
       dom_content_loaded_event_start,
       dom_content_loaded_event_end,
       load_event_start,
       load_event_end,
       first_paint,
       first_contentful_paint,
       speed_index,
       visually_complete,
       time_to_interactive
FROM pages
WHERE mod(session_id, 2) = 0;

INSERT INTO eng_t.events4(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                          user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                          user_country, datetime, min_fps, avg_fps, max_fps, min_cpu, avg_cpu, max_cpu,
                          min_total_js_heap_size, avg_total_js_heap_size, max_total_js_heap_size,
                          min_used_js_heap_size, avg_used_js_heap_size, max_used_js_heap_size)
SELECT 'PERFORMANCE' AS event_type,
       session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       min_fps,
       avg_fps,
       max_fps,
       min_cpu,
       avg_cpu,
       max_cpu,
       min_total_js_heap_size,
       avg_total_js_heap_size,
       max_total_js_heap_size,
       min_used_js_heap_size,
       avg_used_js_heap_size,
       max_used_js_heap_size
FROM performance
WHERE mod(session_id, 2) = 0;

INSERT INTO eng_t.events4(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                          user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                          user_country, datetime, url, type, duration, header_size, encoded_body_size,
                          decoded_body_size, success, method, status)
SELECT 'RESOURCE'                               AS event_type,
       session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       url,
       type,
       duration,
       header_size,
       encoded_body_size,
       decoded_body_size,
       success,
       method,
       if(status IS NOT NULL, status = 1, null) AS status
FROM resources
WHERE type != 'fetch'
  AND mod(session_id, 2) = 0;

INSERT INTO eng_t.events4(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                          user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                          user_country, datetime, url, type, duration, header_size, encoded_body_size,
                          decoded_body_size, success, method, status)
SELECT 'REQUEST'                                AS event_type,
       session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       url,
       type,
       duration,
       header_size,
       encoded_body_size,
       decoded_body_size,
       success,
       method,
       if(status IS NOT NULL, status = 1, null) AS status
FROM resources
WHERE type = 'fetch'
  AND mod(session_id, 2) = 0;

CREATE TABLE IF NOT EXISTS eng_t.events4
(
    session_id                                     UInt64,
    project_id                                     UInt32,
    event_type Enum8('CLICK'=0, 'INPUT'=1, 'PAGE'=2,'RESOURCE'=3,'REQUEST'=4,'PERFORMANCE'=5,'LONGTASK'=6,'ERROR'=7,'CUSTOM'=8),
    tracker_version LowCardinality(String),
    rev_id Nullable(String),
    user_uuid                                      UUID,
    user_os LowCardinality(String),
    user_os_version LowCardinality(Nullable(String)),
    user_browser LowCardinality(String),
    user_browser_version LowCardinality(Nullable(String)),
    user_device Nullable(String),
    user_device_type Enum8('other'=0, 'desktop'=1, 'mobile'=2),
    user_country Enum8('UN'=-128, 'RW'=-127, 'SO'=-126, 'YE'=-125, 'IQ'=-124, 'SA'=-123, 'IR'=-122, 'CY'=-121, 'TZ'=-120, 'SY'=-119, 'AM'=-118, 'KE'=-117, 'CD'=-116, 'DJ'=-115, 'UG'=-114, 'CF'=-113, 'SC'=-112, 'JO'=-111, 'LB'=-110, 'KW'=-109, 'OM'=-108, 'QA'=-107, 'BH'=-106, 'AE'=-105, 'IL'=-104, 'TR'=-103, 'ET'=-102, 'ER'=-101, 'EG'=-100, 'SD'=-99, 'GR'=-98, 'BI'=-97, 'EE'=-96, 'LV'=-95, 'AZ'=-94, 'LT'=-93, 'SJ'=-92, 'GE'=-91, 'MD'=-90, 'BY'=-89, 'FI'=-88, 'AX'=-87, 'UA'=-86, 'MK'=-85, 'HU'=-84, 'BG'=-83, 'AL'=-82, 'PL'=-81, 'RO'=-80, 'XK'=-79, 'ZW'=-78, 'ZM'=-77, 'KM'=-76, 'MW'=-75, 'LS'=-74, 'BW'=-73, 'MU'=-72, 'SZ'=-71, 'RE'=-70, 'ZA'=-69, 'YT'=-68, 'MZ'=-67, 'MG'=-66, 'AF'=-65, 'PK'=-64, 'BD'=-63, 'TM'=-62, 'TJ'=-61, 'LK'=-60, 'BT'=-59, 'IN'=-58, 'MV'=-57, 'IO'=-56, 'NP'=-55, 'MM'=-54, 'UZ'=-53, 'KZ'=-52, 'KG'=-51, 'TF'=-50, 'HM'=-49, 'CC'=-48, 'PW'=[REDACTED] 'VN'=-46, 'TH'=-45, 'ID'=-44, 'LA'=-43, 'TW'=-42, 'PH'=-41, 'MY'=-40, 'CN'=-39, 'HK'=-38, 'BN'=-37, 'MO'=-36, 'KH'=-35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=88, 'CR'=89, 'VE'=90, 'EC'=91, 'CO'=92, 'PA'=93, 'HT'=94, 'AR'=95, 'CL'=96, 'BO'=97, 'PE'=98, 'MX'=99, 'PF'=100, 'PN'=101, 'KI'=102, 'TK'=103, 'TO'=104, 'WF'=105, 'WS'=106, 'NU'=107, 'MP'=108, 'GU'=109, 'PR'=110, 'VI'=111, 'UM'=112, 'AS'=113, 'CA'=114, 'US'=115, 'PS'=116, 'RS'=117, 'AQ'=118, 'SX'=119, 'CW'=120, 'BQ'=121, 'SS'=122),
    datetime                                       DateTime,
    label Nullable(String),
    hesitation_time Nullable(UInt32),
    name Nullable(String),
    payload Nullable(String),
    level Nullable(Enum8('info'=0, 'error'=1)) DEFAULT if(event_type == 'CUSTOM', 'info', null),
    source Nullable(Enum8('js_exception'=0, 'bugsnag'=1, 'cloudwatch'=2, 'datadog'=3, 'elasticsearch'=4, 'newrelic'=5, 'rollbar'=6, 'sentry'=7, 'stackdriver'=8, 'sumologic'=9)),
    message Nullable(String),
    error_id Nullable(String),
    duration Nullable(UInt16),
    context Nullable(Enum8('unknown'=0, 'self'=1, 'same-origin-ancestor'=2, 'same-origin-descendant'=3, 'same-origin'=4, 'cross-origin-ancestor'=5, 'cross-origin-descendant'=6, 'cross-origin-unreachable'=7, 'multiple-contexts'=8)),
    container_type Nullable(Enum8('window'=0, 'iframe'=1, 'embed'=2, 'object'=3)),
    container_id Nullable(String),
    container_name Nullable(String),
    container_src Nullable(String),
    url Nullable(String),
    url_host Nullable(String)                      MATERIALIZED lower(domain(url)),
    url_path Nullable(String)                      MATERIALIZED lower(pathFull(url)),
    request_start Nullable(UInt16),
    response_start Nullable(UInt16),
    response_end Nullable(UInt16),
    dom_content_loaded_event_start Nullable(UInt16),
    dom_content_loaded_event_end Nullable(UInt16),
    load_event_start Nullable(UInt16),
    load_event_end Nullable(UInt16),
    first_paint Nullable(UInt16),
    first_contentful_paint Nullable(UInt16),
    speed_index Nullable(UInt16),
    visually_complete Nullable(UInt16),
    time_to_interactive Nullable(UInt16),
    ttfb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_start, request_start),
                                                                   minus(response_start, request_start), Null),
    ttlb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_end, request_start),
                                                                   minus(response_end, request_start), Null),
    response_time Nullable(UInt16)                 MATERIALIZED if(greaterOrEquals(response_end, response_start),
                                                                   minus(response_end, response_start), Null),
    dom_building_time Nullable(UInt16)             MATERIALIZED if(
            greaterOrEquals(dom_content_loaded_event_start, response_end),
            minus(dom_content_loaded_event_start, response_end), Null),
    dom_content_loaded_event_time Nullable(UInt16) MATERIALIZED if(
            greaterOrEquals(dom_content_loaded_event_end, dom_content_loaded_event_start),
            minus(dom_content_loaded_event_end, dom_content_loaded_event_start), Null),
    load_event_time Nullable(UInt16)               MATERIALIZED if(greaterOrEquals(load_event_end, load_event_start),
                                                                   minus(load_event_end, load_event_start), Null),
    min_fps Nullable(UInt8),
    avg_fps Nullable(UInt8),
    max_fps Nullable(UInt8),
    min_cpu Nullable(UInt8),
    avg_cpu Nullable(UInt8),
    max_cpu Nullable(UInt8),
    min_total_js_heap_size Nullable(UInt64),
    avg_total_js_heap_size Nullable(UInt64),
    max_total_js_heap_size Nullable(UInt64),
    min_used_js_heap_size Nullable(UInt64),
    avg_used_js_heap_size Nullable(UInt64),
    max_used_js_heap_size Nullable(UInt64),
    type Nullable(Enum8('other'=-1, 'script'=0, 'stylesheet'=1, 'fetch'=2, 'img'=3, 'media'=4)),
    header_size Nullable(UInt16),
    encoded_body_size Nullable(UInt32),
    decoded_body_size Nullable(UInt32),
    compression_ratio Nullable(Float32)            MATERIALIZED divide(decoded_body_size, encoded_body_size),
    success Nullable(BOOLEAN),
    method Nullable(Enum8('GET' = 0, 'HEAD' = 1, 'POST' = 2, 'PUT' = 3, 'DELETE' = 4, 'CONNECT' = 5, 'OPTIONS' = 6, 'TRACE' = 7, 'PATCH' = 8)),
    status Nullable(UInt16)
) ENGINE = Join(ALL, INNER, session_id);

TRUNCATE TABLE eng_t.events4;


-- merge metadata with events
CREATE DATABASE full_meerge;

CREATE TABLE IF NOT EXISTS massive.events6
(
    session_id                                     UInt64,
    project_id                                     UInt32,
    event_type Enum8('CLICK'=0, 'INPUT'=1, 'PAGE'=2,'RESOURCE'=3,'REQUEST'=4,'PERFORMANCE'=5,'LONGTASK'=6,'ERROR'=7,'CUSTOM'=8),
    tracker_version LowCardinality(String),
    rev_id Nullable(String),
    user_uuid                                      UUID,
    user_os LowCardinality(String),
    user_os_version LowCardinality(Nullable(String)),
    user_browser LowCardinality(String),
    user_browser_version LowCardinality(Nullable(String)),
    user_device Nullable(String),
    user_device_type Enum8('other'=0, 'desktop'=1, 'mobile'=2),
    user_country Enum8('UN'=-128, 'RW'=-127, 'SO'=-126, 'YE'=-125, 'IQ'=-124, 'SA'=-123, 'IR'=-122, 'CY'=-121, 'TZ'=-120, 'SY'=-119, 'AM'=-118, 'KE'=-117, 'CD'=-116, 'DJ'=-115, 'UG'=-114, 'CF'=-113, 'SC'=-112, 'JO'=-111, 'LB'=-110, 'KW'=-109, 'OM'=-108, 'QA'=-107, 'BH'=-106, 'AE'=-105, 'IL'=-104, 'TR'=-103, 'ET'=-102, 'ER'=-101, 'EG'=-100, 'SD'=-99, 'GR'=-98, 'BI'=-97, 'EE'=-96, 'LV'=-95, 'AZ'=-94, 'LT'=-93, 'SJ'=-92, 'GE'=-91, 'MD'=-90, 'BY'=-89, 'FI'=-88, 'AX'=-87, 'UA'=-86, 'MK'=-85, 'HU'=-84, 'BG'=-83, 'AL'=-82, 'PL'=-81, 'RO'=-80, 'XK'=-79, 'ZW'=-78, 'ZM'=-77, 'KM'=-76, 'MW'=-75, 'LS'=-74, 'BW'=-73, 'MU'=-72, 'SZ'=-71, 'RE'=-70, 'ZA'=-69, 'YT'=-68, 'MZ'=-67, 'MG'=-66, 'AF'=-65, 'PK'=-64, 'BD'=-63, 'TM'=-62, 'TJ'=-61, 'LK'=-60, 'BT'=-59, 'IN'=-58,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M'=62, 'DO'=63, 'CU'=64, 'MQ'=65, 'BS'=66, 'BM'=67, 'AI'=68, 'TT'=69, 'KN'=70, 'DM'=71, 'AG'=72, 'LC'=73, 'TC'=74, 'AW'=75, 'VG'=76, 'VC'=77, 'MS'=78, 'MF'=79, 'BL'=80, 'GP'=81, 'GD'=82, 'KY'=83, 'BZ'=84, 'SV'=85, 'GT'=86, 'HN'=87, 'NI'=88, 'CR'=89, 'VE'=90, 'EC'=91, 'CO'=92, 'PA'=93, 'HT'=94, 'AR'=95, 'CL'=96, 'BO'=97, 'PE'=98, 'MX'=99, 'PF'=100, 'PN'=101, 'KI'=102, 'TK'=103, 'TO'=104, 'WF'=105, 'WS'=106, 'NU'=107, 'MP'=108, 'GU'=109, 'PR'=110, 'VI'=111, 'UM'=112, 'AS'=113, 'CA'=114, 'US'=115, 'PS'=116, 'RS'=117, 'AQ'=118, 'SX'=119, 'CW'=120, 'BQ'=121, 'SS'=122),
    datetime                                       DateTime,
    label Nullable(String),
    hesitation_time Nullable(UInt32),
    name Nullable(String),
    payload Nullable(String),
    level Nullable(Enum8('info'=0, 'error'=1)) DEFAULT if(event_type == 'CUSTOM', 'info', null),
    source Nullable(Enum8('js_exception'=0, 'bugsnag'=1, 'cloudwatch'=2, 'datadog'=3, 'elasticsearch'=4, 'newrelic'=5, 'rollbar'=6, 'sentry'=7, 'stackdriver'=8, 'sumologic'=9)),
    message Nullable(String),
    error_id Nullable(String),
    duration Nullable(UInt16),
    context Nullable(Enum8('unknown'=0, 'self'=1, 'same-origin-ancestor'=2, 'same-origin-descendant'=3, 'same-origin'=4, 'cross-origin-ancestor'=5, 'cross-origin-descendant'=6, 'cross-origin-unreachable'=7, 'multiple-contexts'=8)),
    container_type Nullable(Enum8('window'=0, 'iframe'=1, 'embed'=2, 'object'=3)),
    container_id Nullable(String),
    container_name Nullable(String),
    container_src Nullable(String),
    url Nullable(String),
    url_host Nullable(String)                      MATERIALIZED lower(domain(url)),
    url_path Nullable(String)                      MATERIALIZED lower(pathFull(url)),
    request_start Nullable(UInt16),
    response_start Nullable(UInt16),
    response_end Nullable(UInt16),
    dom_content_loaded_event_start Nullable(UInt16),
    dom_content_loaded_event_end Nullable(UInt16),
    load_event_start Nullable(UInt16),
    load_event_end Nullable(UInt16),
    first_paint Nullable(UInt16),
    first_contentful_paint Nullable(UInt16),
    speed_index Nullable(UInt16),
    visually_complete Nullable(UInt16),
    time_to_interactive Nullable(UInt16),
    ttfb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_start, request_start),
                                                                   minus(response_start, request_start), Null),
    ttlb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_end, request_start),
                                                                   minus(response_end, request_start), Null),
    response_time Nullable(UInt16)                 MATERIALIZED if(greaterOrEquals(response_end, response_start),
                                                                   minus(response_end, response_start), Null),
    dom_building_time Nullable(UInt16)             MATERIALIZED if(
            greaterOrEquals(dom_content_loaded_event_start, response_end),
            minus(dom_content_loaded_event_start, response_end), Null),
    dom_content_loaded_event_time Nullable(UInt16) MATERIALIZED if(
            greaterOrEquals(dom_content_loaded_event_end, dom_content_loaded_event_start),
            minus(dom_content_loaded_event_end, dom_content_loaded_event_start), Null),
    load_event_time Nullable(UInt16)               MATERIALIZED if(greaterOrEquals(load_event_end, load_event_start),
                                                                   minus(load_event_end, load_event_start), Null),
    min_fps Nullable(UInt8),
    avg_fps Nullable(UInt8),
    max_fps Nullable(UInt8),
    min_cpu Nullable(UInt8),
    avg_cpu Nullable(UInt8),
    max_cpu Nullable(UInt8),
    min_total_js_heap_size Nullable(UInt64),
    avg_total_js_heap_size Nullable(UInt64),
    max_total_js_heap_size Nullable(UInt64),
    min_used_js_heap_size Nullable(UInt64),
    avg_used_js_heap_size Nullable(UInt64),
    max_used_js_heap_size Nullable(UInt64),
    type Nullable(Enum8('other'=-1, 'script'=0, 'stylesheet'=1, 'fetch'=2, 'img'=3, 'media'=4)),
    header_size Nullable(UInt16),
    encoded_body_size Nullable(UInt32),
    decoded_body_size Nullable(UInt32),
    compression_ratio Nullable(Float32)            MATERIALIZED divide(decoded_body_size, encoded_body_size),
    success Nullable(UInt8),
    method Nullable(Enum8('GET' = 0, 'HEAD' = 1, 'POST' = 2, 'PUT' = 3, 'DELETE' = 4, 'CONNECT' = 5, 'OPTIONS' = 6, 'TRACE' = 7, 'PATCH' = 8)),
    status Nullable(UInt16),
    user_id Nullable(String),
    user_anonymous_id Nullable(String),
    metadata_1 Nullable(String),
    metadata_2 Nullable(String),
    metadata_3 Nullable(String),
    metadata_4 Nullable(String),
    metadata_5 Nullable(String),
    metadata_6 Nullable(String),
    metadata_7 Nullable(String),
    metadata_8 Nullable(String),
    metadata_9 Nullable(String),
    metadata_10 Nullable(String)
) ENGINE = MergeTree
      PARTITION BY toDate(datetime)
      ORDER BY (project_id, datetime);
--       TTL datetime + INTERVAL 1 MONTH;
INSERT INTO massive.events6(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os,
                            user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                            user_country, datetime, label, hesitation_time, user_id, user_anonymous_id, metadata_1,
                            metadata_2, metadata_3, metadata_4, metadata_5, metadata_6, metadata_7, metadata_8,
                            metadata_9, metadata_10)
SELECT session_id + 6651141467121565 * 3 AS session_id,
       project_id,
       'CLICK'                           AS event_type,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       label,
       hesitation_time,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10
FROM default.clicks
         LEFT JOIN default.sessions_metadata USING (session_id);


INSERT INTO massive.events6(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                            user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                            user_country,
                            datetime, source, name, message, error_id, user_id,
                            user_anonymous_id, metadata_1, metadata_2, metadata_3, metadata_4, metadata_5,
                            metadata_6, metadata_7, metadata_8, metadata_9, metadata_10)

SELECT 'ERROR'                           AS event_type,
       session_id + 6651141467121565 * 3 AS session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       source,
       name,
       message,
       error_id,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10
FROM default.errors
         LEFT JOIN default.sessions_metadata USING (session_id);


INSERT INTO massive.events6(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                            user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                            user_country, datetime, label, user_id,
                            user_anonymous_id, metadata_1, metadata_2, metadata_3, metadata_4, metadata_5,
                            metadata_6, metadata_7, metadata_8, metadata_9, metadata_10)

SELECT 'INPUT'                           AS event_type,
       session_id + 6651141467121565 * 3 AS session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       label,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10
FROM default.inputs
         LEFT JOIN default.sessions_metadata USING (session_id);

INSERT INTO massive.events6(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                            user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                            user_country, datetime, duration, context, container_type, container_id, container_name,
                            container_src, user_id,
                            user_anonymous_id, metadata_1, metadata_2, metadata_3, metadata_4, metadata_5,
                            metadata_6, metadata_7, metadata_8, metadata_9, metadata_10)
SELECT 'LONGTASK'                        AS event_type,
       session_id + 6651141467121565 * 3 AS session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       duration,
       context,
       container_type,
       container_id,
       container_name,
       container_src,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10
FROM default.longtasks
         LEFT JOIN default.sessions_metadata USING (session_id);


INSERT INTO massive.events6(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                            user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                            user_country, datetime, url, request_start, response_start, response_end,
                            dom_content_loaded_event_start, dom_content_loaded_event_end, load_event_start,
                            load_event_end, first_paint, first_contentful_paint, speed_index, visually_complete,
                            time_to_interactive, user_id,
                            user_anonymous_id, metadata_1, metadata_2, metadata_3, metadata_4, metadata_5,
                            metadata_6, metadata_7, metadata_8, metadata_9, metadata_10)
SELECT 'PAGE'                            AS event_type,
       session_id + 6651141467121565 * 3 AS session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       url,
       request_start,
       response_start,
       response_end,
       dom_content_loaded_event_start,
       dom_content_loaded_event_end,
       load_event_start,
       load_event_end,
       first_paint,
       first_contentful_paint,
       speed_index,
       visually_complete,
       time_to_interactive,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10
FROM default.pages
         LEFT JOIN default.sessions_metadata USING (session_id);

INSERT INTO massive.events6(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                            user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                            user_country, datetime, min_fps, avg_fps, max_fps, min_cpu, avg_cpu, max_cpu,
                            min_total_js_heap_size, avg_total_js_heap_size, max_total_js_heap_size,
                            min_used_js_heap_size, avg_used_js_heap_size, max_used_js_heap_size, user_id,
                            user_anonymous_id, metadata_1, metadata_2, metadata_3, metadata_4, metadata_5,
                            metadata_6, metadata_7, metadata_8, metadata_9, metadata_10)
SELECT 'PERFORMANCE'                     AS event_type,
       session_id + 6651141467121565 * 3 AS session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       min_fps,
       avg_fps,
       max_fps,
       min_cpu,
       avg_cpu,
       max_cpu,
       min_total_js_heap_size,
       avg_total_js_heap_size,
       max_total_js_heap_size,
       min_used_js_heap_size,
       avg_used_js_heap_size,
       max_used_js_heap_size,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10
FROM default.performance
         LEFT JOIN default.sessions_metadata USING (session_id);

INSERT INTO massive.events6(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                            user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                            user_country, datetime, url, type, duration, header_size, encoded_body_size,
                            decoded_body_size, success, method, status, user_id,
                            user_anonymous_id, metadata_1, metadata_2, metadata_3, metadata_4, metadata_5,
                            metadata_6, metadata_7, metadata_8, metadata_9, metadata_10)
SELECT 'RESOURCE'                               AS event_type,
       session_id + 6651141467121565 * 3        AS session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       url,
       type,
       duration,
       header_size,
       encoded_body_size,
       decoded_body_size,
       success,
       method,
       if(status IS NOT NULL, status = 1, null) AS status,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10
FROM default.resources
         LEFT JOIN default.sessions_metadata USING (session_id)
WHERE type != 'fetch';

INSERT INTO massive2.events7(event_type, session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                             user_country, datetime, url, type, duration, header_size, encoded_body_size,
                             decoded_body_size, success, method, status, user_id,
                             user_anonymous_id, metadata_1, metadata_2, metadata_3, metadata_4, metadata_5,
                             metadata_6, metadata_7, metadata_8, metadata_9, metadata_10)
SELECT 'REQUEST'                                AS event_type,
       session_id + 6651141467121565 * 4        AS session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       url,
       type,
       duration,
       header_size,
       encoded_body_size,
       decoded_body_size,
       success,
       method,
       if(status IS NOT NULL, status = 1, null) AS status,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10
FROM default.resources
         LEFT JOIN default.sessions_metadata USING (session_id)
WHERE type = 'fetch'
  AND mod(project_id, 2) = 0;

-- -- TO GENERATE RANDOM USER IDS
-- INSERT INTO sessions_metadata(session_id, user_id, datetime, project_id, user_device_type)
-- SELECT session_id,
--        arrayElement(
--                array('Ze2wc7lvYi', 'NYd7m0Ytg8', 'qgNpvEkXap', 'wvWqM4Ow2G', 'n5Y6DK7ZdP', 'uW4SEYjXxI', 't4EfJiNxk9',
--                      'qWQ8WuIRLS', 'fnRWCwkFyB', '8wf298MFWR', 'G3A3DL0Fdd', 'cQcZHNNiAJ', 'MKcW2adQ38', 'OBzk9EFxVe',
--                      '8SBiqoFail', '3Wh9Ur0eOr', 'z6KuuxiPXX', '7j4HaReEsF', 'Ros0kDOVeV', 'PvHi3cBkgV', 'HLjUo6oBlJ',
--                      '4Tmi34faA0', 'O9ZATbPjaB', '7ATvuWQCIH', 'kXW4LHnW5X', 'HIHc9TTyTc', 'i5p9jRe7I0', '7dRnUEFoZO',
--                      'u3PDLkI5uG', 'HTYjxmDJCG', '6hKHjcKniO', 'qmPNUWgDIx', 'RfoN9oeYZD', 'HHXpBaYm3k', 'VdpZDfnL9J',
--                      'Qfwa1dPrrF', 'cgdD2GfFVT', 'iRvT6l7qj3', 'QokprB2GMV', 'umqISqbncX', '7bvRdQ4al3', 'VGKZAUIRjy',
--                      'SNTEGLKbCD', 'zfUaVSD8Jn', 'De7zUojKNt', 'lXiotVRkil', 'bQaDX5kESw', 'tngESCaH6I', 'uucUZvTpPd',
--                      'BFJpni8D3I'), mod(session_id, 50)) AS user_id,
--        datetime,
--        project_id,
--        user_device_type
-- FROM sessions
-- WHERE project_id = 2460;

INSERT INTO massive2.sessions2(session_id, project_id, tracker_version, rev_id, user_uuid, user_os, user_os_version,
                               user_browser, user_browser_version, user_device, user_device_type, user_country,
                               datetime,
                               duration, events_count, errors_count, utm_source, utm_medium, utm_campaign)
SELECT session_id + 6651141467121565 * 4 AS session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       duration,
       events_count,
       errors_count,
       utm_source,
       utm_medium,
       utm_campaign
FROM default.sessions;


CREATE DATABASE massive2;
CREATE TABLE IF NOT EXISTS massive2.events7
(
    session_id                                     UInt64,
    project_id                                     UInt32,
    event_type Enum8('CLICK'=0, 'INPUT'=1, 'PAGE'=2,'RESOURCE'=3,'REQUEST'=4,'PERFORMANCE'=5,'LONGTASK'=6,'ERROR'=7,'CUSTOM'=8),
    tracker_version LowCardinality(String),
    rev_id Nullable(String),
    user_uuid                                      UUID,
    user_os LowCardinality(String),
    user_os_version LowCardinality(Nullable(String)),
    user_browser LowCardinality(String),
    user_browser_version LowCardinality(Nullable(String)),
    user_device Nullable(String),
    user_device_type Enum8('other'=0, 'desktop'=1, 'mobile'=2),
    user_country Enum8('UN'=-128, 'RW'=-127, 'SO'=-126, 'YE'=-125, 'IQ'=-124, 'SA'=-123, 'IR'=-122, 'CY'=-121, 'TZ'=-120, 'SY'=-119, 'AM'=-118, 'KE'=-117, 'CD'=-116, 'DJ'=-115, 'UG'=-114, 'CF'=-113, 'SC'=-112, 'JO'=-111, 'LB'=-110, 'KW'=-109, 'OM'=-108, 'QA'=-107, 'BH'=-106, 'AE'=-105, 'IL'=-104, 'TR'=-103, 'ET'=-102, 'ER'=-101, 'EG'=-100, 'SD'=-99, 'GR'=-98, 'BI'=-97, 'EE'=-96, 'LV'=-95, 'AZ'=-94, 'LT'=-93, 'SJ'=-92, 'GE'=-91, 'MD'=-90, 'BY'=-89, 'FI'=-88, 'AX'=-87, 'UA'=-86, 'MK'=-85, 'HU'=-84, 'BG'=-83, 'AL'=-82, 'PL'=-81, 'RO'=-80, 'XK'=-79, 'ZW'=-78, 'ZM'=-77, 'KM'=-76, 'MW'=-75, 'LS'=-74, 'BW'=-73, 'MU'=-72, 'SZ'=-71, 'RE'=-70, 'ZA'=-69, 'YT'=-68, 'MZ'=-67, 'MG'=-66, 'AF'=-65, 'PK'=-64, 'BD'=-63, 'TM'=-62, 'TJ'=-61, 'LK'=-60, 'BT'=-59, 'IN'=-58, 'MV'=-57, 'IO'=-56, 'NP'=-55, 'MM'=-54, 'UZ'=-53, 'KZ'=-52, 'KG'=-51, 'TF'=-50, 'HM'=-49, 'CC'=-48, 'PW'=[REDACTED] 'VN'=-46, 'TH'=-45, 'ID'=-44, 'LA'=-43, 'TW'=-42, 'PH'=-41, 'MY'=-40, 'CN'=-39, 'HK'=-38, 'BN'=-37, 'MO'=-36, 'KH'=-35, 'KR'=-34, 'JP'=-33, 'KP'=-32, 'SG'=-31, 'CK'=-30, 'TL'=-29, 'RU'=-28, 'MN'=-27, 'AU'=-26, 'CX'=-25, 'MH'=-24, 'FM'=-23, 'PG'=-22, 'SB'=-21, 'TV'=-20, 'NR'=-19, 'VU'=-18, 'NC'=-17, 'NF'=-16, 'NZ'=-15, 'FJ'=-14, 'LY'=-13, 'CM'=-12, 'SN'=-11, 'CG'=-10, 'PT'=-9, 'LR'=-8, 'CI'=-7, 'GH'=-6, 'GQ'=-5, 'NG'=-4, 'BF'=-3, 'TG'=-2, 'GW'=-1, 'MR'=0, 'BJ'=1, 'GA'=2, 'SL'=3, 'ST'=4, 'GI'=5, 'GM'=6, 'GN'=7, 'TD'=8, 'NE'=9, 'ML'=10, 'EH'=11, 'TN'=12, 'ES'=13, 'MA'=14, 'MT'=15, 'DZ'=16, 'FO'=17, 'DK'=18, 'IS'=19, 'GB'=20, 'CH'=21, 'SE'=22, 'NL'=23, 'AT'=24, 'BE'=25, 'DE'=26, 'LU'=27, 'IE'=28, 'MC'=29, 'FR'=30, 'AD'=31, 'LI'=32, 'JE'=33, 'IM'=34, 'GG'=35, 'SK'=36, 'CZ'=37, 'NO'=38, 'VA'=39, 'SM'=40, 'IT'=41, 'SI'=42, 'ME'=43, 'HR'=44, 'BA'=45, 'AO'=46, 'NA'=47, 'SH'=48, 'BV'=49, 'BB'=50, 'CV'=51, 'GY'=52, 'GF'=53, 'SR'=54, 'PM'=55, 'GL'=56, 'PY'=57, 'UY'=58, 'BR'=59, 'FK'=60, 'GS'=61, 'JM'=62, 'DO'=63, 'CU'=64, 'MQ'=65, 'BS'=66, 'BM'=67, 'AI'=68, 'TT'=69, 'KN'=70, 'DM'=71, 'AG'=72, 'LC'=73, 'TC'=74, 'AW'=75, 'VG'=76, 'VC'=77, 'MS'=78, 'MF'=79, 'BL'=80, 'GP'=81, 'GD'=82, 'KY'=83, 'BZ'=84, 'SV'=85, 'GT'=86, 'HN'=87, 'NI'=88, 'CR'=89, 'VE'=90, 'EC'=91, 'CO'=92, 'PA'=93, 'HT'=94, 'AR'=95, 'CL'=96, 'BO'=97, 'PE'=98, 'MX'=99, 'PF'=100, 'PN'=101, 'KI'=102, 'TK'=103, 'TO'=104, 'WF'=105, 'WS'=106, 'NU'=107, 'MP'=108, 'GU'=109, 'PR'=110, 'VI'=111, 'UM'=112, 'AS'=113, 'CA'=114, 'US'=115, 'PS'=116, 'RS'=117, 'AQ'=118, 'SX'=119, 'CW'=120, 'BQ'=121, 'SS'=122),
    datetime                                       DateTime,
    label Nullable(String),
    hesitation_time Nullable(UInt32),
    name Nullable(String),
    payload Nullable(String),
    level Nullable(Enum8('info'=0, 'error'=1)) DEFAULT if(event_type == 'CUSTOM', 'info', null),
    source Nullable(Enum8('js_exception'=0, 'bugsnag'=1, 'cloudwatch'=2, 'datadog'=3, 'elasticsearch'=4, 'newrelic'=5, 'rollbar'=6, 'sentry'=7, 'stackdriver'=8, 'sumologic'=9)),
    message Nullable(String),
    error_id Nullable(String),
    duration Nullable(UInt16),
    context Nullable(Enum8('unknown'=0, 'self'=1, 'same-origin-ancestor'=2, 'same-origin-descendant'=3, 'same-origin'=4, 'cross-origin-ancestor'=5, 'cross-origin-descendant'=6, 'cross-origin-unreachable'=7, 'multiple-contexts'=8)),
    container_type Nullable(Enum8('window'=0, 'iframe'=1, 'embed'=2, 'object'=3)),
    container_id Nullable(String),
    container_name Nullable(String),
    container_src Nullable(String),
    url Nullable(String),
    url_host Nullable(String)                      MATERIALIZED lower(domain(url)),
    url_path Nullable(String)                      MATERIALIZED lower(pathFull(url)),
    request_start Nullable(UInt16),
    response_start Nullable(UInt16),
    response_end Nullable(UInt16),
    dom_content_loaded_event_start Nullable(UInt16),
    dom_content_loaded_event_end Nullable(UInt16),
    load_event_start Nullable(UInt16),
    load_event_end Nullable(UInt16),
    first_paint Nullable(UInt16),
    first_contentful_paint Nullable(UInt16),
    speed_index Nullable(UInt16),
    visually_complete Nullable(UInt16),
    time_to_interactive Nullable(UInt16),
    ttfb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_start, request_start),
                                                                   minus(response_start, request_start), Null),
    ttlb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_end, request_start),
                                                                   minus(response_end, request_start), Null),
    response_time Nullable(UInt16)                 MATERIALIZED if(greaterOrEquals(response_end, response_start),
                                                                   minus(response_end, response_start), Null),
    dom_building_time Nullable(UInt16)             MATERIALIZED if(
            greaterOrEquals(dom_content_loaded_event_start, response_end),
            minus(dom_content_loaded_event_start, response_end), Null),
    dom_content_loaded_event_time Nullable(UInt16) MATERIALIZED if(
            greaterOrEquals(dom_content_loaded_event_end, dom_content_loaded_event_start),
            minus(dom_content_loaded_event_end, dom_content_loaded_event_start), Null),
    load_event_time Nullable(UInt16)               MATERIALIZED if(greaterOrEquals(load_event_end, load_event_start),
                                                                   minus(load_event_end, load_event_start), Null),
    min_fps Nullable(UInt8),
    avg_fps Nullable(UInt8),
    max_fps Nullable(UInt8),
    min_cpu Nullable(UInt8),
    avg_cpu Nullable(UInt8),
    max_cpu Nullable(UInt8),
    min_total_js_heap_size Nullable(UInt64),
    avg_total_js_heap_size Nullable(UInt64),
    max_total_js_heap_size Nullable(UInt64),
    min_used_js_heap_size Nullable(UInt64),
    avg_used_js_heap_size Nullable(UInt64),
    max_used_js_heap_size Nullable(UInt64),
    type Nullable(Enum8('other'=-1, 'script'=0, 'stylesheet'=1, 'fetch'=2, 'img'=3, 'media'=4)),
    header_size Nullable(UInt16),
    encoded_body_size Nullable(UInt32),
    decoded_body_size Nullable(UInt32),
    compression_ratio Nullable(Float32)            MATERIALIZED divide(decoded_body_size, encoded_body_size),
    success Nullable(UInt8),
    method Nullable(Enum8('GET' = 0, 'HEAD' = 1, 'POST' = 2, 'PUT' = 3, 'DELETE' = 4, 'CONNECT' = 5, 'OPTIONS' = 6, 'TRACE' = 7, 'PATCH' = 8)),
    status Nullable(UInt16),
    user_id Nullable(String),
    user_anonymous_id Nullable(String),
    metadata_1 Nullable(String),
    metadata_2 Nullable(String),
    metadata_3 Nullable(String),
    metadata_4 Nullable(String),
    metadata_5 Nullable(String),
    metadata_6 Nullable(String),
    metadata_7 Nullable(String),
    metadata_8 Nullable(String),
    metadata_9 Nullable(String),
    metadata_10 Nullable(String)
) ENGINE = MergeTree
      PARTITION BY toDate(datetime)
      ORDER BY (project_id, datetime, event_type);



INSERT INTO massive2.events7(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os,
                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                             user_country, datetime, url, type, duration, header_size, encoded_body_size,
                             decoded_body_size, success, method, status, user_id, user_anonymous_id, metadata_1,
                             metadata_2, metadata_3, metadata_4, metadata_5, metadata_6, metadata_7, metadata_8,
                             metadata_9, metadata_10, riteration)
SELECT session_id,
       project_id,
       event_type,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       url,
       type,
       duration,
       header_size,
       encoded_body_size,
       decoded_body_size,
       success,
       method,
       status,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10,
       43
FROM massive.events6
WHERE event_type = 'REQUEST'
  AND mod(project_id, 2) = 0;
INSERT INTO massive2.events7(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os,
                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                             user_country, datetime, url, type, duration, header_size, encoded_body_size,
                             decoded_body_size, success, method, status, user_id, user_anonymous_id, metadata_1,
                             metadata_2, metadata_3, metadata_4, metadata_5, metadata_6, metadata_7, metadata_8,
                             metadata_9, metadata_10, riteration)
SELECT session_id,
       project_id,
       event_type,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       url,
       type,
       duration,
       header_size,
       encoded_body_size,
       decoded_body_size,
       success,
       method,
       status,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10,
       42
FROM massive.events6
WHERE event_type = 'REQUEST'
  AND mod(project_id, 2) = 1;

INSERT INTO massive2.events7(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os,
                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                             user_country, datetime, url, type, duration, header_size, encoded_body_size,
                             decoded_body_size, success, method, status, user_id, user_anonymous_id, metadata_1,
                             metadata_2, metadata_3, metadata_4, metadata_5, metadata_6, metadata_7, metadata_8,
                             metadata_9, metadata_10, riteration)
SELECT session_id,
       project_id,
       event_type,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       url,
       type,
       duration,
       header_size,
       encoded_body_size,
       decoded_body_size,
       success,
       method,
       status,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10,
       41
FROM massive.events6
WHERE event_type = 'RESOURCE'
  AND mod(project_id, 2) = 0;
INSERT INTO massive2.events7(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os,
                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                             user_country, datetime, url, type, duration, header_size, encoded_body_size,
                             decoded_body_size, success, method, status, user_id, user_anonymous_id, metadata_1,
                             metadata_2, metadata_3, metadata_4, metadata_5, metadata_6, metadata_7, metadata_8,
                             metadata_9, metadata_10, riteration)
SELECT session_id,
       project_id,
       event_type,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       url,
       type,
       duration,
       header_size,
       encoded_body_size,
       decoded_body_size,
       success,
       method,
       status,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10,
       40
FROM massive.events6
WHERE event_type = 'RESOURCE'
  AND mod(project_id, 2) = 1;

INSERT INTO massive2.events7(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os,
                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                             user_country, datetime, label, hesitation_time, user_id, user_anonymous_id, metadata_1,
                             metadata_2, metadata_3, metadata_4, metadata_5, metadata_6, metadata_7, metadata_8,
                             metadata_9, metadata_10, riteration)
SELECT session_id,
       project_id,
       event_type,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       label,
       hesitation_time,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10,
       4
FROM massive.events6
WHERE event_type = 'CLICK';
INSERT INTO massive2.events7(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os,
                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                             user_country, datetime, min_fps, avg_fps, max_fps, min_cpu, avg_cpu, max_cpu,
                             min_total_js_heap_size, avg_total_js_heap_size, max_total_js_heap_size,
                             min_used_js_heap_size, avg_used_js_heap_size, max_used_js_heap_size, user_id,
                             user_anonymous_id, metadata_1, metadata_2, metadata_3, metadata_4, metadata_5, metadata_6,
                             metadata_7, metadata_8, metadata_9, metadata_10, riteration)
SELECT session_id,
       project_id,
       event_type,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       min_fps,
       avg_fps,
       max_fps,
       min_cpu,
       avg_cpu,
       max_cpu,
       min_total_js_heap_size,
       avg_total_js_heap_size,
       max_total_js_heap_size,
       min_used_js_heap_size,
       avg_used_js_heap_size,
       max_used_js_heap_size,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10,
       4
FROM massive.events6
WHERE event_type = 'PERFORMANCE';
INSERT INTO massive2.events7(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os,
                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                             user_country, datetime, url, request_start, response_start, response_end,
                             dom_content_loaded_event_start, dom_content_loaded_event_end, load_event_start,
                             load_event_end, first_paint, first_contentful_paint, speed_index, visually_complete,
                             time_to_interactive, user_id, user_anonymous_id, metadata_1, metadata_2, metadata_3,
                             metadata_4, metadata_5, metadata_6, metadata_7, metadata_8, metadata_9, metadata_10,
                             riteration)
SELECT session_id,
       project_id,
       event_type,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       url,
       request_start,
       response_start,
       response_end,
       dom_content_loaded_event_start,
       dom_content_loaded_event_end,
       load_event_start,
       load_event_end,
       first_paint,
       first_contentful_paint,
       speed_index,
       visually_complete,
       time_to_interactive,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10,
       4
FROM massive.events6
WHERE event_type = 'PAGE';
INSERT INTO massive2.events7(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os,
                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                             user_country, datetime, label, user_id, user_anonymous_id, metadata_1, metadata_2,
                             metadata_3, metadata_4, metadata_5, metadata_6, metadata_7, metadata_8, metadata_9,
                             metadata_10, riteration)
SELECT session_id,
       project_id,
       event_type,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       label,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10,
       4
FROM massive.events6
WHERE event_type = 'INPUT';
INSERT INTO massive2.events7(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os,
                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                             user_country, datetime, source, name, message, error_id, user_id, user_anonymous_id,
                             metadata_1, metadata_2, metadata_3, metadata_4, metadata_5, metadata_6, metadata_7,
                             metadata_8, metadata_9, metadata_10, riteration)
SELECT session_id + 6651141467121565 * 4 AS session_id,
       project_id,
       event_type,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       source,
       name,
       message,
       error_id,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10,
       4
FROM massive.events6
WHERE event_type = 'ERROR';
INSERT INTO massive2.events7(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os,
                             user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                             user_country, datetime, duration, context, container_type, container_id, container_name,
                             container_src, user_id, user_anonymous_id, metadata_1, metadata_2, metadata_3, metadata_4,
                             metadata_5, metadata_6, metadata_7, metadata_8, metadata_9, metadata_10, riteration)
SELECT session_id + 6651141467121565 * 4 AS session_id,
       project_id,
       event_type,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       duration,
       context,
       container_type,
       container_id,
       container_name,
       container_src,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10,
       4
FROM massive.events6
WHERE event_type = 'LONGTASK';

ALTER TABLE massive2.events7
    ADD COLUMN riteration UInt8 DEFAULT 0;
ALTER TABLE massive2.sessions2
    ADD COLUMN riteration UInt8 DEFAULT 0;



INSERT INTO massive2.sessions2(session_id, project_id, tracker_version, rev_id, user_uuid, user_os, user_os_version,
                               user_browser, user_browser_version, user_device, user_device_type, user_country,
                               datetime,
                               duration, events_count, errors_count, utm_source, utm_medium, utm_campaign, riteration)
SELECT session_id + 6651141467121565 * 4 AS session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       duration,
       events_count,
       errors_count,
       utm_source,
       utm_medium,
       utm_campaign,
       4
FROM massive.sessions;

SELECT COUNT(*)
FROM massive2.events7;

CREATE DATABASE massive30;
CREATE TABLE IF NOT EXISTS massive30.events30
(
    session_id                                     UInt64,
    project_id                                     UInt32,
    event_type Enum8('CLICK'=0, 'INPUT'=1, 'PAGE'=2,'RESOURCE'=3,'REQUEST'=4,'PERFORMANCE'=5,'LONGTASK'=6,'ERROR'=7,'CUSTOM'=8),
    tracker_version LowCardinality(String),
    rev_id Nullable(String),
    user_uuid                                      UUID,
    user_os LowCardinality(String),
    user_os_version LowCardinality(Nullable(String)),
    user_browser LowCardinality(String),
    user_browser_version LowCardinality(Nullable(String)),
    user_device Nullable(String),
    user_device_type Enum8('other'=0, 'desktop'=1, 'mobile'=2),
    user_country Enum8('UN'=-128, 'RW'=-127, 'SO'=-126, 'YE'=-125, 'IQ'=-124, 'SA'=-123, 'IR'=-122, 'CY'=-121, 'TZ'=-120, 'SY'=-119, 'AM'=-118, 'KE'=-117, 'CD'=-116, 'DJ'=-115, 'UG'=-114, 'CF'=-113, 'SC'=-112, 'JO'=-111, 'LB'=-110, 'KW'=-109, 'OM'=-108, 'QA'=-107, 'BH'=-106, 'AE'=-105, 'IL'=-104, 'TR'=-103, 'ET'=-102, 'ER'=-101, 'EG'=-100, 'SD'=-99, 'GR'=-98, 'BI'=-97, 'EE'=-96, 'LV'=-95, 'AZ'=-94, 'LT'=-93, 'SJ'=-92, 'GE'=-91, 'MD'=-90, 'BY'=-89, 'FI'=-88, 'AX'=-87, 'UA'=-86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'FR'=30, 'AD'=31, 'LI'=32, 'JE'=33, 'IM'=34, 'GG'=35, 'SK'=36, 'CZ'=37, 'NO'=38, 'VA'=39, 'SM'=40, 'IT'=41, 'SI'=42, 'ME'=43, 'HR'=44, 'BA'=45, 'AO'=46, 'NA'=47, 'SH'=48, 'BV'=49, 'BB'=50, 'CV'=51, 'GY'=52, 'GF'=53, 'SR'=54, 'PM'=55, 'GL'=56, 'PY'=57, 'UY'=58, 'BR'=59, 'FK'=60, 'GS'=61, 'JM'=62, 'DO'=63, 'CU'=64, 'MQ'=65, 'BS'=66, 'BM'=67, 'AI'=68, 'TT'=69, 'KN'=70, 'DM'=71, 'AG'=72, 'LC'=73, 'TC'=74, 'AW'=75, 'VG'=76, 'VC'=77, 'MS'=78, 'MF'=79, 'BL'=80, 'GP'=81, 'GD'=82, 'KY'=83, 'BZ'=84, 'SV'=85, 'GT'=86, 'HN'=87, 'NI'=88, 'CR'=89, 'VE'=90, 'EC'=91, 'CO'=92, 'PA'=93, 'HT'=94, 'AR'=95, 'CL'=96, 'BO'=97, 'PE'=98, 'MX'=99, 'PF'=100, 'PN'=101, 'KI'=102, 'TK'=103, 'TO'=104, 'WF'=105, 'WS'=106, 'NU'=107, 'MP'=108, 'GU'=109, 'PR'=110, 'VI'=111, 'UM'=112, 'AS'=113, 'CA'=114, 'US'=115, 'PS'=116, 'RS'=117, 'AQ'=118, 'SX'=119, 'CW'=120, 'BQ'=121, 'SS'=122),
    datetime                                       DateTime,
    label Nullable(String),
    hesitation_time Nullable(UInt32),
    name Nullable(String),
    payload Nullable(String),
    level Nullable(Enum8('info'=0, 'error'=1)) DEFAULT if(event_type == 'CUSTOM', 'info', null),
    source Nullable(Enum8('js_exception'=0, 'bugsnag'=1, 'cloudwatch'=2, 'datadog'=3, 'elasticsearch'=4, 'newrelic'=5, 'rollbar'=6, 'sentry'=7, 'stackdriver'=8, 'sumologic'=9)),
    message Nullable(String),
    error_id Nullable(String),
    duration Nullable(UInt16),
    context Nullable(Enum8('unknown'=0, 'self'=1, 'same-origin-ancestor'=2, 'same-origin-descendant'=3, 'same-origin'=4, 'cross-origin-ancestor'=5, 'cross-origin-descendant'=6, 'cross-origin-unreachable'=7, 'multiple-contexts'=8)),
    container_type Nullable(Enum8('window'=0, 'iframe'=1, 'embed'=2, 'object'=3)),
    container_id Nullable(String),
    container_name Nullable(String),
    container_src Nullable(String),
    url Nullable(String),
    url_host Nullable(String)                      MATERIALIZED lower(domain(url)),
    url_path Nullable(String)                      MATERIALIZED lower(pathFull(url)),
    request_start Nullable(UInt16),
    response_start Nullable(UInt16),
    response_end Nullable(UInt16),
    dom_content_loaded_event_start Nullable(UInt16),
    dom_content_loaded_event_end Nullable(UInt16),
    load_event_start Nullable(UInt16),
    load_event_end Nullable(UInt16),
    first_paint Nullable(UInt16),
    first_contentful_paint Nullable(UInt16),
    speed_index Nullable(UInt16),
    visually_complete Nullable(UInt16),
    time_to_interactive Nullable(UInt16),
    ttfb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_start, request_start),
                                                                   minus(response_start, request_start), Null),
    ttlb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_end, request_start),
                                                                   minus(response_end, request_start), Null),
    response_time Nullable(UInt16)                 MATERIALIZED if(greaterOrEquals(response_end, response_start),
                                                                   minus(response_end, response_start), Null),
    dom_building_time Nullable(UInt16)             MATERIALIZED if(
            greaterOrEquals(dom_content_loaded_event_start, response_end),
            minus(dom_content_loaded_event_start, response_end), Null),
    dom_content_loaded_event_time Nullable(UInt16) MATERIALIZED if(
            greaterOrEquals(dom_content_loaded_event_end, dom_content_loaded_event_start),
            minus(dom_content_loaded_event_end, dom_content_loaded_event_start), Null),
    load_event_time Nullable(UInt16)               MATERIALIZED if(greaterOrEquals(load_event_end, load_event_start),
                                                                   minus(load_event_end, load_event_start), Null),
    min_fps Nullable(UInt8),
    avg_fps Nullable(UInt8),
    max_fps Nullable(UInt8),
    min_cpu Nullable(UInt8),
    avg_cpu Nullable(UInt8),
    max_cpu Nullable(UInt8),
    min_total_js_heap_size Nullable(UInt64),
    avg_total_js_heap_size Nullable(UInt64),
    max_total_js_heap_size Nullable(UInt64),
    min_used_js_heap_size Nullable(UInt64),
    avg_used_js_heap_size Nullable(UInt64),
    max_used_js_heap_size Nullable(UInt64),
    type Nullable(Enum8('other'=-1, 'script'=0, 'stylesheet'=1, 'fetch'=2, 'img'=3, 'media'=4)),
    header_size Nullable(UInt16),
    encoded_body_size Nullable(UInt32),
    decoded_body_size Nullable(UInt32),
    compression_ratio Nullable(Float32)            MATERIALIZED divide(decoded_body_size, encoded_body_size),
    success Nullable(UInt8),
    method Nullable(Enum8('GET' = 0, 'HEAD' = 1, 'POST' = 2, 'PUT' = 3, 'DELETE' = 4, 'CONNECT' = 5, 'OPTIONS' = 6, 'TRACE' = 7, 'PATCH' = 8)),
    status Nullable(UInt16),
    user_id Nullable(String),
    user_anonymous_id Nullable(String),
    metadata_1 Nullable(String),
    metadata_2 Nullable(String),
    metadata_3 Nullable(String),
    metadata_4 Nullable(String),
    metadata_5 Nullable(String),
    metadata_6 Nullable(String),
    metadata_7 Nullable(String),
    metadata_8 Nullable(String),
    metadata_9 Nullable(String),
    metadata_10 Nullable(String)
) ENGINE = MergeTree
      PARTITION BY toYYYYMM(datetime)
      ORDER BY (project_id, datetime, event_type);

ALTER TABLE massive30.events30
    ADD COLUMN riteration UInt8;

INSERT INTO massive30.events30(session_id, project_id, event_type, tracker_version, rev_id, user_uuid, user_os,
                               user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                               user_country, datetime, label, hesitation_time, name, payload, level, source, message,
                               error_id, duration, context, container_type, container_id, container_name, container_src,
                               url, request_start, response_start, response_end, dom_content_loaded_event_start,
                               dom_content_loaded_event_end, load_event_start, load_event_end, first_paint,
                               first_contentful_paint, speed_index, visually_complete, time_to_interactive, min_fps,
                               avg_fps, max_fps, min_cpu, avg_cpu, max_cpu, min_total_js_heap_size,
                               avg_total_js_heap_size, max_total_js_heap_size, min_used_js_heap_size,
                               avg_used_js_heap_size, max_used_js_heap_size, type, header_size, encoded_body_size,
                               decoded_body_size, success, method, status, user_id, user_anonymous_id, metadata_1,
                               metadata_2, metadata_3, metadata_4, metadata_5, metadata_6, metadata_7, metadata_8,
                               metadata_9, metadata_10, riteration)
SELECT session_id,
       project_id,
       event_type,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       label,
       hesitation_time,
       name,
       payload,
       level,
       source,
       message,
       error_id,
       duration,
       context,
       container_type,
       container_id,
       container_name,
       container_src,
       url,
       request_start,
       response_start,
       response_end,
       dom_content_loaded_event_start,
       dom_content_loaded_event_end,
       load_event_start,
       load_event_end,
       first_paint,
       first_contentful_paint,
       speed_index,
       visually_complete,
       time_to_interactive,
       min_fps,
       avg_fps,
       max_fps,
       min_cpu,
       avg_cpu,
       max_cpu,
       min_total_js_heap_size,
       avg_total_js_heap_size,
       max_total_js_heap_size,
       min_used_js_heap_size,
       avg_used_js_heap_size,
       max_used_js_heap_size,
       type,
       header_size,
       encoded_body_size,
       decoded_body_size,
       success,
       method,
       status,
       user_id,
       user_anonymous_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10,
       9 AS riteration
FROM massive2.events7
WHERE mod(project_id, 10) = 9;
-- ORDER BY datetime LIMIT 500000;

DROP TABLE massive30.events30;


DESCRIBE TABLE massive2.events7;


-- -----------------------------------------------------
CREATE DATABASE massive_split;
CREATE TABLE IF NOT EXISTS massive_split.events_s
(
    session_id                                     UInt64,
    project_id                                     UInt32,
    event_type Enum8('CLICK'=0, 'INPUT'=1, 'PAGE'=2,'RESOURCE'=3,'REQUEST'=4,'PERFORMANCE'=5,'LONGTASK'=6,'ERROR'=7,'CUSTOM'=8),
    datetime                                       DateTime,
    label Nullable(String),
    hesitation_time Nullable(UInt32),
    name Nullable(String),
    payload Nullable(String),
    level Nullable(Enum8('info'=0, 'error'=1)) DEFAULT if(event_type == 'CUSTOM', 'info', null),
    source Nullable(Enum8('js_exception'=0, 'bugsnag'=1, 'cloudwatch'=2, 'datadog'=3, 'elasticsearch'=4, 'newrelic'=5, 'rollbar'=6, 'sentry'=7, 'stackdriver'=8, 'sumologic'=9)),
    message Nullable(String),
    error_id Nullable(String),
    duration Nullable(UInt16),
    context Nullable(Enum8('unknown'=0, 'self'=1, 'same-origin-ancestor'=2, 'same-origin-descendant'=3, 'same-origin'=4, 'cross-origin-ancestor'=5, 'cross-origin-descendant'=6, 'cross-origin-unreachable'=7, 'multiple-contexts'=8)),
    container_type Nullable(Enum8('window'=0, 'iframe'=1, 'embed'=2, 'object'=3)),
    container_id Nullable(String),
    container_name Nullable(String),
    container_src Nullable(String),
    url Nullable(String),
    url_host Nullable(String)                      MATERIALIZED lower(domain(url)),
    url_path Nullable(String)                      MATERIALIZED lower(pathFull(url)),
    request_start Nullable(UInt16),
    response_start Nullable(UInt16),
    response_end Nullable(UInt16),
    dom_content_loaded_event_start Nullable(UInt16),
    dom_content_loaded_event_end Nullable(UInt16),
    load_event_start Nullable(UInt16),
    load_event_end Nullable(UInt16),
    first_paint Nullable(UInt16),
    first_contentful_paint Nullable(UInt16),
    speed_index Nullable(UInt16),
    visually_complete Nullable(UInt16),
    time_to_interactive Nullable(UInt16),
    ttfb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_start, request_start),
                                                                   minus(response_start, request_start), Null),
    ttlb Nullable(UInt16)                          MATERIALIZED if(greaterOrEquals(response_end, request_start),
                                                                   minus(response_end, request_start), Null),
    response_time Nullable(UInt16)                 MATERIALIZED if(greaterOrEquals(response_end, response_start),
                                                                   minus(response_end, response_start), Null),
    dom_building_time Nullable(UInt16)             MATERIALIZED if(
            greaterOrEquals(dom_content_loaded_event_start, response_end),
            minus(dom_content_loaded_event_start, response_end), Null),
    dom_content_loaded_event_time Nullable(UInt16) MATERIALIZED if(
            greaterOrEquals(dom_content_loaded_event_end, dom_content_loaded_event_start),
            minus(dom_content_loaded_event_end, dom_content_loaded_event_start), Null),
    load_event_time Nullable(UInt16)               MATERIALIZED if(greaterOrEquals(load_event_end, load_event_start),
                                                                   minus(load_event_end, load_event_start), Null),
    min_fps Nullable(UInt8),
    avg_fps Nullable(UInt8),
    max_fps Nullable(UInt8),
    min_cpu Nullable(UInt8),
    avg_cpu Nullable(UInt8),
    max_cpu Nullable(UInt8),
    min_total_js_heap_size Nullable(UInt64),
    avg_total_js_heap_size Nullable(UInt64),
    max_total_js_heap_size Nullable(UInt64),
    min_used_js_heap_size Nullable(UInt64),
    avg_used_js_heap_size Nullable(UInt64),
    max_used_js_heap_size Nullable(UInt64),
    type Nullable(Enum8('other'=-1, 'script'=0, 'stylesheet'=1, 'fetch'=2, 'img'=3, 'media'=4)),
    header_size Nullable(UInt16),
    encoded_body_size Nullable(UInt32),
    decoded_body_size Nullable(UInt32),
    compression_ratio Nullable(Float32)            MATERIALIZED divide(decoded_body_size, encoded_body_size),
    success Nullable(UInt8),
    method Nullable(Enum8('GET' = 0, 'HEAD' = 1, 'POST' = 2, 'PUT' = 3, 'DELETE' = 4, 'CONNECT' = 5, 'OPTIONS' = 6, 'TRACE' = 7, 'PATCH' = 8)),
    status Nullable(UInt16)
) ENGINE = MergeTree
      PARTITION BY toYYYYMM(datetime)
      ORDER BY (project_id, datetime, event_type, session_id);

CREATE TABLE IF NOT EXISTS massive_split.sessions_s
(
    session_id      UInt64,
    project_id      UInt32,
    tracker_version String,
    rev_id Nullable(String),
    user_uuid       UUID,
    user_os         String,
    user_os_version Nullable(String),
    user_browser    String,
    user_browser_version Nullable(String),
    user_device Nullable(String),
    user_device_type Enum8('other'=0, 'desktop'=1, 'mobile'=2),
    user_country Enum8('UN'=-128, 'RW'=-127, 'SO'=-126, 'YE'=-125, 'IQ'=-124, 'SA'=-123, 'IR'=-122, 'CY'=-121, 'TZ'=-120, 'SY'=-119, 'AM'=-118, 'KE'=-117, 'CD'=-116, 'DJ'=-115, 'UG'=-114, 'CF'=-113, 'SC'=-112, 'JO'=-111, 'LB'=-110, 'KW'=-109, 'OM'=-108, 'QA'=-107, 'BH'=-106, 'AE'=-105, 'IL'=-104, 'TR'=-103, 'ET'=-102, 'ER'=-101, 'EG'=-100, 'SD'=-99, 'GR'=-98, 'BI'=-97, 'EE'=-96, 'LV'=-95, 'AZ'=-94, 'LT'=-93, 'SJ'=-92, 'GE'=-91, 'MD'=-90, 'BY'=-89, 'FI'=-88, 'AX'=-87, 'UA'=-86, 'MK'=-85, 'HU'=-84, 'BG'=-83, 'AL'=-82, 'PL'=-81, 'RO'=-80, 'XK'=-79, 'ZW'=-78, 'ZM'=-77, 'KM'=-76, 'MW'=-75, 'LS'=-74, 'BW'=-73, 'MU'=-72, 'SZ'=-71, 'RE'=-70, 'ZA'=-69, 'YT'=-68, 'MZ'=-67, 'MG'=-66, 'AF'=-65, 'PK'=-64, 'BD'=-63, 'TM'=-62, 'TJ'=-61, 'LK'=-60, 'BT'=-59, 'IN'=-58, 'MV'=-57, 'IO'=-56, 'NP'=-55, 'MM'=-54, 'UZ'=-53, 'KZ'=-52, 'KG'=-51, 'TF'=-50, 'HM'=-49, 'CC'=-48, 'PW'=[REDACTED] 'VN'=-46, 'TH'=-45, 'ID'=-44, 'LA'=-43, 'TW'=-42, 'PH'=-41, 'MY'=-40, 'CN'=-39, 'HK'=-38, 'BN'=-37, 'MO'=-36, 'KH'=-35, 'KR'=-34, 'JP'=-33, 'KP'=-32, 'SG'=-31, 'CK'=-30, 'TL'=-29, 'RU'=-28, 'MN'=-27, 'AU'=-26, 'CX'=-25, 'MH'=-24, 'FM'=-23, 'PG'=-22, 'SB'=-21, 'TV'=-20, 'NR'=-19, 'VU'=-18, 'NC'=-17, 'NF'=-16, 'NZ'=-15, 'FJ'=-14, 'LY'=-13, 'CM'=-12, 'SN'=-11, 'CG'=-10, 'PT'=-9, 'LR'=-8, 'CI'=-7, 'GH'=-6, 'GQ'=-5, 'NG'=-4, 'BF'=-3, 'TG'=-2, 'GW'=-1, 'MR'=0, 'BJ'=1, 'GA'=2, 'SL'=3, 'ST'=4, 'GI'=5, 'GM'=6, 'GN'=7, 'TD'=8, 'NE'=9, 'ML'=10, 'EH'=11, 'TN'=12, 'ES'=13, 'MA'=14, 'MT'=15, 'DZ'=16, 'FO'=17, 'DK'=18, 'IS'=19, 'GB'=20, 'CH'=21, 'SE'=22, 'NL'=23, 'AT'=24, 'BE'=25, 'DE'=26, 'LU'=27, 'IE'=28, 'MC'=29, 'FR'=30, 'AD'=31, 'LI'=32, 'JE'=33, 'IM'=34, 'GG'=35, 'SK'=36, 'CZ'=37, 'NO'=38, 'VA'=39, 'SM'=40, 'IT'=41, 'SI'=42, 'ME'=43, 'HR'=44, 'BA'=45, 'AO'=46, 'NA'=47, 'SH'=48, 'BV'=49, 'BB'=50, 'CV'=51, 'GY'=52, 'GF'=53, 'SR'=54, 'PM'=55, 'GL'=56, 'PY'=57, 'UY'=58, 'BR'=59, 'FK'=60, 'GS'=61, 'JM'=62, 'DO'=63, 'CU'=64, 'MQ'=65, 'BS'=66, 'BM'=67, 'AI'=68, 'TT'=69, 'KN'=70, 'DM'=71, 'AG'=72, 'LC'=73, 'TC'=74, 'AW'=75, 'VG'=76, 'VC'=77, 'MS'=78, 'MF'=79, 'BL'=80, 'GP'=81, 'GD'=82, 'KY'=83, 'BZ'=84, 'SV'=85, 'GT'=86, 'HN'=87, 'NI'=88, 'CR'=89, 'VE'=90, 'EC'=91, 'CO'=92, 'PA'=93, 'HT'=94, 'AR'=95, 'CL'=96, 'BO'=97, 'PE'=98, 'MX'=99, 'PF'=100, 'PN'=101, 'KI'=102, 'TK'=103, 'TO'=104, 'WF'=105, 'WS'=106, 'NU'=107, 'MP'=108, 'GU'=109, 'PR'=110, 'VI'=111, 'UM'=112, 'AS'=113, 'CA'=114, 'US'=115, 'PS'=116, 'RS'=117, 'AQ'=118, 'SX'=119, 'CW'=120, 'BQ'=121, 'SS'=122),
    datetime        DateTime,
    duration        UInt32,
    pages_count     UInt16,
    events_count    UInt16,
    errors_count    UInt16,
    utm_source Nullable(String),
    utm_medium Nullable(String),
    utm_campaign Nullable(String),
    _timestamp      DateTime DEFAULT now()
) ENGINE = ReplacingMergeTree(_timestamp)
      PARTITION BY toYYYYMMDD(datetime)
      ORDER BY (project_id, datetime, session_id)
      TTL datetime + INTERVAL 1 MONTH
      SETTINGS index_granularity = 512;


CREATE TABLE IF NOT EXISTS massive_split.metadata_s
(
    session_id UInt64,
    project_id UInt32,
    datetime   DateTime,
    user_id Nullable(String),
    metadata_1 Nullable(String),
    metadata_2 Nullable(String),
    metadata_3 Nullable(String),
    metadata_4 Nullable(String),
    metadata_5 Nullable(String),
    metadata_6 Nullable(String),
    metadata_7 Nullable(String),
    metadata_8 Nullable(String),
    metadata_9 Nullable(String),
    metadata_10 Nullable(String),
    _timestamp DateTime DEFAULT now()
) ENGINE = ReplacingMergeTree(_timestamp)
      PARTITION BY toDate(datetime)
      ORDER BY (project_id, datetime, session_id);

INSERT INTO massive_split.sessions_s(session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                                     user_os_version, user_browser, user_browser_version, user_device, user_device_type,
                                     user_country, datetime, duration, pages_count, events_count, errors_count,
                                     utm_source, utm_medium, utm_campaign, riteration)
SELECT session_id + 6651141467121565 * 4 AS session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       duration,
       mod(rand(), 100)                  AS pages_count,
       events_count,
       errors_count,
       utm_source,
       utm_medium,
       utm_campaign,
       4                                 AS riteration
FROM default.sessions;

ALTER TABLE massive_split.sessions_s
    ADD COLUMN riteration UInt8;

INSERT INTO massive_split.metadata_s(session_id, project_id, datetime, user_id, metadata_1, metadata_2, metadata_3,
                                     metadata_4, metadata_5, metadata_6, metadata_7, metadata_8, metadata_9,
                                     metadata_10)
SELECT session_id,
       project_id,
       datetime,
       user_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10
FROM massive2.events7 AS s
LIMIT 1 BY session_id;

INSERT INTO massive_split.events_s(session_id, project_id, event_type, datetime, label, hesitation_time, name, payload,
                                   level, source, message, error_id, duration, context, container_type, container_id,
                                   container_name, container_src, url, request_start, response_start, response_end,
                                   dom_content_loaded_event_start, dom_content_loaded_event_end, load_event_start,
                                   load_event_end, first_paint, first_contentful_paint, speed_index, visually_complete,
                                   time_to_interactive, min_fps, avg_fps, max_fps, min_cpu, avg_cpu, max_cpu,
                                   min_total_js_heap_size, avg_total_js_heap_size, max_total_js_heap_size,
                                   min_used_js_heap_size, avg_used_js_heap_size, max_used_js_heap_size, type,
                                   header_size, encoded_body_size, decoded_body_size, success, method, status)
SELECT session_id,
       project_id,
       event_type,
       datetime,
       label,
       hesitation_time,
       name,
       payload,
       level,
       source,
       message,
       error_id,
       duration,
       context,
       container_type,
       container_id,
       container_name,
       container_src,
       url,
       request_start,
       response_start,
       response_end,
       dom_content_loaded_event_start,
       dom_content_loaded_event_end,
       load_event_start,
       load_event_end,
       first_paint,
       first_contentful_paint,
       speed_index,
       visually_complete,
       time_to_interactive,
       min_fps,
       avg_fps,
       max_fps,
       min_cpu,
       avg_cpu,
       max_cpu,
       min_total_js_heap_size,
       avg_total_js_heap_size,
       max_total_js_heap_size,
       min_used_js_heap_size,
       avg_used_js_heap_size,
       max_used_js_heap_size,
       type,
       header_size,
       encoded_body_size,
       decoded_body_size,
       success,
       method,
       status
FROM massive2.events7;

SELECT COUNT(*)
FROM massive_split.sessions_s;
SELECT COUNT(*)
FROM massive_split.metadata_s;
SELECT COUNT(*)
FROM massive_split.events_s;
SELECT COUNT(*)
FROM massive2.events7;
-- SELECT COUNT(*) FROM massive2.sessions2;


CREATE TABLE IF NOT EXISTS massive_split.sessions_meta
(
    session_id      UInt64,
    project_id      UInt32,
    tracker_version String,
    rev_id Nullable(String),
    user_uuid       UUID,
    user_os         String,
    user_os_version Nullable(String),
    user_browser    String,
    user_browser_version Nullable(String),
    user_device Nullable(String),
    user_device_type Enum8('other'=0, 'desktop'=1, 'mobile'=2),
    user_country Enum8('UN'=-128, 'RW'=-127, 'SO'=-126, 'YE'=-125, 'IQ'=-124, 'SA'=-123, 'IR'=-122, 'CY'=-121, 'TZ'=-120, 'SY'=-119, 'AM'=-118, 'KE'=-117, 'CD'=-116, 'DJ'=-115, 'UG'=-114, 'CF'=-113, 'SC'=-112, 'JO'=-111, 'LB'=-110, 'KW'=-109, 'OM'=-108, 'QA'=-107, 'BH'=-106, 'AE'=-105, 'IL'=-104, 'TR'=-103, 'ET'=-102, 'ER'=-101, 'EG'=-100, 'SD'=-99, 'GR'=-98, 'BI'=-97, 'EE'=-96, 'LV'=-95, 'AZ'=-94, 'LT'=-93, 'SJ'=-92, 'GE'=-91, 'MD'=-90, 'BY'=-89, 'FI'=-88, 'AX'=-87, 'UA'=-86, 'MK'=-85, 'HU'=-84, 'BG'=-83, 'AL'=-82, 'PL'=-81, 'RO'=-80, 'XK'=-79, 'ZW'=-78, 'ZM'=-77, 'KM'=-76, 'MW'=-75, 'LS'=-74, 'BW'=-73, 'MU'=-72, 'SZ'=-71, 'RE'=-70, 'ZA'=-69, 'YT'=-68, 'MZ'=-67, 'MG'=-66, 'AF'=-65, 'PK'=-64, 'BD'=-63, 'TM'=-62, 'TJ'=-61, 'LK'=-60, 'BT'=-59, 'IN'=-58, 'MV'=-57, 'IO'=-56, 'NP'=-55, 'MM'=-54, 'UZ'=-53, 'KZ'=-52, 'KG'=-51, 'TF'=-50, 'HM'=-49, 'CC'=-48, 'PW'=[REDACTED] 'VN'=-46, 'TH'=-45, 'ID'=-44, 'LA'=-43, 'TW'=-42, 'PH'=-41, 'MY'=-40, 'CN'=-39, 'HK'=-38, 'BN'=-37, 'MO'=-36, 'KH'=-35, 'KR'=-34, 'JP'=-33, 'KP'=-32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 'CO'=92, 'PA'=93, 'HT'=94, 'AR'=95, 'CL'=96, 'BO'=97, 'PE'=98, 'MX'=99, 'PF'=100, 'PN'=101, 'KI'=102, 'TK'=103, 'TO'=104, 'WF'=105, 'WS'=106, 'NU'=107, 'MP'=108, 'GU'=109, 'PR'=110, 'VI'=111, 'UM'=112, 'AS'=113, 'CA'=114, 'US'=115, 'PS'=116, 'RS'=117, 'AQ'=118, 'SX'=119, 'CW'=120, 'BQ'=121, 'SS'=122),
    datetime        DateTime,
    duration        UInt32,
    pages_count     UInt16,
    events_count    UInt16,
    errors_count    UInt16,
    utm_source Nullable(String),
    utm_medium Nullable(String),
    utm_campaign Nullable(String),
    user_id Nullable(String),
    metadata_1 Nullable(String),
    metadata_2 Nullable(String),
    metadata_3 Nullable(String),
    metadata_4 Nullable(String),
    metadata_5 Nullable(String),
    metadata_6 Nullable(String),
    metadata_7 Nullable(String),
    metadata_8 Nullable(String),
    metadata_9 Nullable(String),
    metadata_10 Nullable(String),
    _timestamp      DateTime DEFAULT now()
) ENGINE = ReplacingMergeTree(_timestamp)
      PARTITION BY toYYYYMMDD(datetime)
      ORDER BY (project_id, datetime, session_id)
      TTL datetime + INTERVAL 1 MONTH
      SETTINGS index_granularity = 512;

INSERT INTO massive_split.sessions_meta(session_id, project_id, tracker_version, rev_id, user_uuid, user_os,
                                        user_os_version, user_browser, user_browser_version, user_device,
                                        user_device_type, user_country, datetime, duration, pages_count, events_count,
                                        errors_count, utm_source, utm_medium, utm_campaign, user_id, metadata_1,
                                        metadata_2, metadata_3, metadata_4, metadata_5, metadata_6, metadata_7,
                                        metadata_8, metadata_9, metadata_10)
SELECT session_id,
       project_id,
       tracker_version,
       rev_id,
       user_uuid,
       user_os,
       user_os_version,
       user_browser,
       user_browser_version,
       user_device,
       user_device_type,
       user_country,
       datetime,
       duration,
       pages_count,
       events_count,
       errors_count,
       utm_source,
       utm_medium,
       utm_campaign,
       user_id,
       metadata_1,
       metadata_2,
       metadata_3,
       metadata_4,
       metadata_5,
       metadata_6,
       metadata_7,
       metadata_8,
       metadata_9,
       metadata_10
FROM massive_split.sessions_s AS s
         LEFT JOIN massive_split.metadata_s AS m ON (s.project_id = m.project_id AND s.session_id = m.session_id);
