From 96a5f01ac8916c99eb4b67f675ee66be37113891 Mon Sep 17 00:00:00 2001 From: Jack Forgash <58153492+forgxyz@users.noreply.github.com> Date: Thu, 1 Sep 2022 18:24:04 -0600 Subject: [PATCH] AN-1988/events handle (#58) * events try 1 * cte * coalesce tweak --- .../silver/silver__event_attributes_https.sql | 61 +++++++++++++++++-- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/models/silver/silver__event_attributes_https.sql b/models/silver/silver__event_attributes_https.sql index dffd5dc..fb6c41e 100644 --- a/models/silver/silver__event_attributes_https.sql +++ b/models/silver/silver__event_attributes_https.sql @@ -23,6 +23,17 @@ AND _inserted_timestamp >= ( ) {% endif %} ), +event_nulls AS ( + SELECT + * + FROM + events + WHERE + COALESCE ( + _event_data_type :Fields, + _event_data_type :fields + ) IS NULL +), events_data AS ( SELECT event_id, @@ -69,13 +80,54 @@ attributes AS ( ) AS attribute_id, INDEX AS attribute_index, _ingested_at, - _inserted_timestamp + _inserted_timestamp, + 'attributes' AS _cte FROM events_data, LATERAL FLATTEN( input => event_data_type_fields ) ), +attributes_2 AS ( + SELECT + event_id, + tx_id, + block_timestamp, + event_index, + event_contract, + event_type, + VALUE :name :: STRING AS attribute_key, + COALESCE( + VALUE :value :value :fields, + VALUE :value :value :staticType, + VALUE :value :value :value :value, + VALUE :value :value :value, + VALUE :value :value + ) AS attribute_value, + concat_ws( + '-', + event_id, + INDEX + ) AS attribute_id, + INDEX AS attribute_index, + _ingested_at, + _inserted_timestamp, + 'attributes_2' AS _cte + FROM + event_nulls, + LATERAL FLATTEN(_event_data_fields) +), +combo AS ( + SELECT + * + FROM + attributes + UNION + SELECT + * + FROM + attributes_2 +), FINAL AS ( SELECT attribute_id, @@ -89,11 +141,10 @@ FINAL AS ( attribute_key, attribute_value, _ingested_at, - _inserted_timestamp + _inserted_timestamp, + _cte FROM - attributes - WHERE - attribute_key IS NOT NULL + combo ) SELECT *