From 0a880a138b1dd0da243beb871abb077b8d58d2ae Mon Sep 17 00:00:00 2001 From: Joaquin Ossa Date: Wed, 2 Oct 2024 14:50:34 +0200 Subject: [PATCH] created macro and fixed naming --- macros/timestamp_to_utc.sql | 22 +++++++++++++++ .../hubspot/stg_hubspot__form_submissions.sql | 28 ++++--------------- 2 files changed, 28 insertions(+), 22 deletions(-) create mode 100644 macros/timestamp_to_utc.sql diff --git a/macros/timestamp_to_utc.sql b/macros/timestamp_to_utc.sql new file mode 100644 index 0000000..20cac93 --- /dev/null +++ b/macros/timestamp_to_utc.sql @@ -0,0 +1,22 @@ +/* +This macro converts a timestamp column from Unix epoch time (in milliseconds) +to a UTC timestamp and a corresponding UTC date. + +It generates two output fields: +1. The timestamp in UTC, with the alias formatted as _at_utc. +2. The date in UTC, with the alias formatted as _date_utc. + +This macro is intended to be used within a SELECT statement +and ensures that the output is properly formatted for further analysis. +*/ +{% macro timestamp_to_utc(column_name) %} + to_timestamp( + {{ adapter.quote(column_name) }}::double precision / 1000 + ) at time zone 'UTC' as {{ column_name | replace("At", "") }}_at_utc, + + cast( + to_timestamp( + {{ adapter.quote(column_name) }}::double precision / 1000 + ) at time zone 'UTC' as date + ) as {{ column_name | replace("At", "") }}_date_utc +{% endmacro %} diff --git a/models/staging/hubspot/stg_hubspot__form_submissions.sql b/models/staging/hubspot/stg_hubspot__form_submissions.sql index 2293dc4..705a952 100644 --- a/models/staging/hubspot/stg_hubspot__form_submissions.sql +++ b/models/staging/hubspot/stg_hubspot__form_submissions.sql @@ -1,29 +1,13 @@ with raw_form_submissions as (select * from {{ source("hubspot", "form_submissions") }}), - stg_core__form_submissions as ( + stg_hubspot__form_submissions as ( select - {{ adapter.quote("formId") }} as id_form, {{ adapter.quote("values") }} as - values - , - {{ adapter.quote("pageUrl") }} as page_url, - to_timestamp( - {{ adapter.quote("submittedAt") }}::double precision / 1000 - ) at time zone 'UTC' as submitted_at_utc, - cast( - to_timestamp( - {{ adapter.quote("submittedAt") }}::double precision / 1000 - ) at time zone 'UTC' as date - ) as submitted_date_utc, - to_timestamp( - {{ adapter.quote("updatedAt") }}::double precision / 1000 - ) at time zone 'UTC' as updated_at_utc, - cast( - to_timestamp( - {{ adapter.quote("updatedAt") }}::double precision / 1000 - ) at time zone 'UTC' as date - ) as updated_date_utc, + {{ adapter.quote("formId") }} as id_form, + {{ adapter.quote("values") }} as "values", + {{ timestamp_to_utc("submittedAt") }}, + {{ timestamp_to_utc("updatedAt") }}, {{ adapter.quote("_airbyte_extracted_at") }} as dwh_extracted_at_utc from raw_form_submissions ) select * -from stg_core__form_submissions +from stg_hubspot__form_submissions