Skip to content

Commit

Permalink
Snapshot time data type (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank authored Sep 4, 2024
1 parent 4fd2d9a commit c310bc5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20240621-143024.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Compare 'snapshot_get_time' and snapshot 'updated_at' data types
time: 2024-06-21T14:30:24.336219-04:00
custom:
Author: gshank
Issue: "242"
8 changes: 8 additions & 0 deletions dbt/include/global_project/macros/adapters/timestamps.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
{{ current_timestamp() }}
{% endmacro %}

{% macro get_snapshot_get_time_data_type() %}
{% set snapshot_time = adapter.dispatch('snapshot_get_time', 'dbt')() %}
{% set time_data_type_sql = 'select ' ~ snapshot_time ~ ' as dbt_snapshot_time' %}
{% set snapshot_time_column_schema = get_column_schema_from_query(time_data_type_sql) %}
{% set time_data_type = snapshot_time_column_schema[0].dtype %}
{{ return(time_data_type or none) }}
{% endmacro %}

---------------------------------------------

/* {#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,26 @@

{% do return(temp_relation) %}
{% endmacro %}


{% macro get_updated_at_column_data_type(snapshot_sql) %}
{% set snapshot_sql_column_schema = get_column_schema_from_query(snapshot_sql) %}
{% set dbt_updated_at_data_type = null %}
{% set ns = namespace() -%} {#-- handle for-loop scoping with a namespace --#}
{% set ns.dbt_updated_at_data_type = null -%}
{% for column in snapshot_sql_column_schema %}
{% if ((column.column == 'dbt_updated_at') or (column.column == 'DBT_UPDATED_AT')) %}
{% set ns.dbt_updated_at_data_type = column.dtype %}
{% endif %}
{% endfor %}
{{ return(ns.dbt_updated_at_data_type or none) }}
{% endmacro %}


{% macro check_time_data_types(sql) %}
{% set dbt_updated_at_data_type = get_updated_at_column_data_type(sql) %}
{% set snapshot_get_time_data_type = get_snapshot_get_time_data_type() %}
{% if snapshot_get_time_data_type is not none and dbt_updated_at_data_type is not none and snapshot_get_time_data_type != dbt_updated_at_data_type %}
{{ exceptions.warn_snapshot_timestamp_data_types(snapshot_get_time_data_type, dbt_updated_at_data_type) }}
{% endif %}
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
{% if not target_relation_exists %}

{% set build_sql = build_snapshot_table(strategy, model['compiled_code']) %}
{% set build_or_select_sql = build_sql %}
{% set final_sql = create_table_as(False, target_relation, build_sql) %}

{% else %}

{{ adapter.valid_snapshot_target(target_relation) }}

{% set build_or_select_sql = snapshot_staging_table(strategy, sql, target_relation) %}
{% set staging_table = build_snapshot_staging_table(strategy, sql, target_relation) %}

-- this may no-op if the database does not require column expansion
Expand Down Expand Up @@ -71,6 +73,9 @@

{% endif %}


{{ check_time_data_types(build_or_select_sql) }}

{% call statement('main') %}
{{ final_sql }}
{% endcall %}
Expand Down

0 comments on commit c310bc5

Please sign in to comment.