From 845c6eba382d88a0d552daa7fc26fd0bb75bd04f Mon Sep 17 00:00:00 2001 From: NGUYEN HUU BACH Date: Sat, 9 Dec 2023 12:16:39 +0000 Subject: [PATCH] enhance existed macros to support more range of databases --- macros/store/insert_list_to_table.sql | 4 +++- macros/utils/bool_to_string.sql | 6 +++--- models/alerts/re_data_z_score.sql | 2 +- models/meta/re_data_selected.sql | 2 +- .../metrics/for_anomalies/re_data_last_metrics.sql | 2 +- models/metrics/for_anomalies/re_data_last_stats.sql | 12 ++++++------ 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/macros/store/insert_list_to_table.sql b/macros/store/insert_list_to_table.sql index 8230a4fa..25f3bea0 100644 --- a/macros/store/insert_list_to_table.sql +++ b/macros/store/insert_list_to_table.sql @@ -14,7 +14,9 @@ {%- if row[p] is none -%} NULL {%- else -%} - {%- if row[p] is string -%} + {%- if row[p] is boolean -%} + cast ({{-row[p]}} as {{ boolean_type()-}}) + {%- elif row[p] is string -%} {%- if dtype and p in dtype -%} {% set cast_type = dtype[p] %} cast ({{ re_data.quote_string(row[p]) }} as {{ cast_type }}) diff --git a/macros/utils/bool_to_string.sql b/macros/utils/bool_to_string.sql index 897bf4af..0b87cdf7 100644 --- a/macros/utils/bool_to_string.sql +++ b/macros/utils/bool_to_string.sql @@ -1,8 +1,8 @@ {% macro bool_to_string(column) %} ( - case when {{ column }} = true then 'true' - when {{ column }} = false then 'false' + case when cast({{ column }} as boolean) = true then 'true' + when cast({{ column }} as boolean) = false then 'false' end ) as {{ column }} -{% endmacro %} \ No newline at end of file +{% endmacro %} diff --git a/models/alerts/re_data_z_score.sql b/models/alerts/re_data_z_score.sql index a9034736..28b8f7c1 100644 --- a/models/alerts/re_data_z_score.sql +++ b/models/alerts/re_data_z_score.sql @@ -29,7 +29,7 @@ with z_score_without_id as ( stats.last_third_quartile - stats.last_first_quartile as last_iqr, stats.last_first_quartile, stats.last_third_quartile, - {{ time_window_end() }} as time_window_end, + cast( {{ time_window_end() }} as {{ timestamp_type() }}) as time_window_end, cast( {{dbt.current_timestamp_backcompat()}} as {{ timestamp_type() }} ) as computed_on from {{ ref('re_data_last_stats') }} as stats, diff --git a/models/meta/re_data_selected.sql b/models/meta/re_data_selected.sql index 34348afb..df742914 100644 --- a/models/meta/re_data_selected.sql +++ b/models/meta/re_data_selected.sql @@ -3,4 +3,4 @@ select name, schema, database, time_filter, metrics, columns, anomaly_detector, owners from {{ ref('re_data_monitored')}} where - selected = true \ No newline at end of file + cast(selected as boolean) = true diff --git a/models/metrics/for_anomalies/re_data_last_metrics.sql b/models/metrics/for_anomalies/re_data_last_metrics.sql index 38e23b5a..8c4ab583 100644 --- a/models/metrics/for_anomalies/re_data_last_metrics.sql +++ b/models/metrics/for_anomalies/re_data_last_metrics.sql @@ -8,5 +8,5 @@ select from {{ ref('re_data_base_metrics') }} where - time_window_end = {{- time_window_end() -}} + cast(time_window_end as timestamp) = {{- time_window_end() -}} diff --git a/models/metrics/for_anomalies/re_data_last_stats.sql b/models/metrics/for_anomalies/re_data_last_stats.sql index 49383980..a68bcd30 100644 --- a/models/metrics/for_anomalies/re_data_last_stats.sql +++ b/models/metrics/for_anomalies/re_data_last_stats.sql @@ -13,8 +13,8 @@ with median_value as ( from {{ ref('re_data_base_metrics') }} where - time_window_end > {{- anamaly_detection_time_window_start() -}} and - time_window_end <= {{- time_window_end() -}} + cast(time_window_end as timestamp) > {{- anamaly_detection_time_window_start() -}} and + cast(time_window_end as timestamp) <= {{- time_window_end() -}} {% if target.type in postgres_type_db() %} group by {{ columns_to_group_by }} @@ -38,8 +38,8 @@ with median_value as ( s.metric = mv.metric and s.interval_length_sec = mv.interval_length_sec where - s.time_window_end > {{- anamaly_detection_time_window_start() -}} and - s.time_window_end <= {{- time_window_end() -}} + cast(s.time_window_end as timestamp) > {{- anamaly_detection_time_window_start() -}} and + cast(s.time_window_end as timestamp) <= {{- time_window_end() -}} ), median_abs_deviation as ( select distinct table_name, @@ -66,8 +66,8 @@ with median_value as ( from {{ ref('re_data_base_metrics') }} where - time_window_end > {{- anamaly_detection_time_window_start() -}} and - time_window_end <= {{- time_window_end() -}} + cast(time_window_end as timestamp) > {{- anamaly_detection_time_window_start() -}} and + cast(time_window_end as timestamp) <= {{- time_window_end() -}} group by {{ columns_to_group_by }} )