Skip to content

Commit

Permalink
Merge pull request #40 from calogica/fix/postgres-support
Browse files Browse the repository at this point in the history
Add initial Postgres support
  • Loading branch information
clausherther authored Apr 10, 2021
2 parents 60fa675 + 85fa258 commit 1a9f0fb
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion integration_tests/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require-dbt-version: ">=0.18.1"

vars:
'dbt_date:time_zone': 'America/Los_Angeles'
dbt_utils_dispatch_list: [spark_utils]
dbt_utils_dispatch_list: [spark_utils, dbt_expectations_integration_tests]

quoting:
database: false
Expand Down
10 changes: 5 additions & 5 deletions integration_tests/models/schema_tests/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ models:
- dbt_expectations.expect_column_values_to_be_of_type:
column_type: date
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list: [date, datetime]
column_type_list: [date, "{{ dbt_expectations.type_datetime() | trim }}"]
- dbt_expectations.expect_column_values_to_be_increasing:
sort_column: date_day

Expand All @@ -62,7 +62,7 @@ models:
- dbt_expectations.expect_column_values_to_be_of_type:
column_type: date
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list: [date, datetime]
column_type_list: [date, "{{ dbt_expectations.type_datetime() | trim }}"]

- name: row_value
tests:
Expand All @@ -77,7 +77,7 @@ models:
- name: row_value_log
tests:
- dbt_expectations.expect_column_values_to_be_within_n_moving_stdevs:
date_column_name: cast(date_day as datetime)
date_column_name: cast(date_day as {{ dbt_expectations.type_datetime() }})
sigma_threshold: 6
take_logs: false

Expand All @@ -91,13 +91,13 @@ models:
# - dbt_expectations.expect_column_values_to_be_of_type:
# column_type: datetime
- dbt_expectations.expect_column_values_to_be_in_type_list:
column_type_list: [date, datetime, timestamp_ntz]
column_type_list: ["{{ dbt_expectations.type_datetime() | trim }}"]


- name: row_value_log
tests:
- dbt_expectations.expect_column_values_to_be_within_n_moving_stdevs:
date_column_name: cast(date_hour as datetime)
date_column_name: cast(date_hour as {{ dbt_expectations.type_datetime() }})
period: hour
trend_periods: 48
test_periods: 12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ row_values as (
add_row_values as (

select
cast(d.date_hour as datetime) as date_hour,
cast(d.date_hour as {{ dbt_expectations.type_datetime() }}) as date_hour,
cast(floor(100 * r.generated_number) as {{ dbt_utils.type_int() }}) as row_value
from
dates d
Expand Down
6 changes: 6 additions & 0 deletions macros/math/rand.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@
uniform(0::float, 1::float, random())

{%- endmacro -%}

{% macro postgres__rand() %}

random()

{%- endmacro -%}
6 changes: 6 additions & 0 deletions macros/regex/regexp_instr.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
regexp_instr({{ source_value }}, '{{ regexp }}', {{ position }}, {{ occurrence }})
{% endmacro %}


{% macro postgres__regexp_instr(source_value, regexp, position, occurrence) %}
array_length((select regexp_matches({{ source_value }}, '{{ regexp }}')), 1)
{% endmacro %}


{% macro spark__regexp_instr(source_value, regexp, position, occurrence) %}
case when {{ source_value }} rlike '{{ regexp }}' then 1 else 0 end
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{%- set column_name = column_name | upper -%}
{%- set columns_in_relation = adapter.get_columns_in_relation(model) -%}
{%- set column_type_list = column_type_list| map("upper") -%}
{%- set column_type_list = column_type_list| map("upper") | list -%}

{%- set matching_column_types = [] -%}

Expand Down
21 changes: 21 additions & 0 deletions macros/utils/datatypes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% macro postgres__type_timestamp() -%}
timestamp without time zone
{%- endmacro %}


{%- macro type_datetime() -%}
{{ return(adapter.dispatch('type_datetime', packages = dbt_expectations._get_namespaces())()) }}
{%- endmacro -%}

{% macro default__type_datetime() -%}
datetime
{%- endmacro %}

{% macro snowflake__type_datetime() -%}
{# see: https://docs.snowflake.com/en/sql-reference/data-types-datetime.html#datetime #}
timestamp_ntz
{%- endmacro %}

{% macro postgres__type_datetime() -%}
timestamp without time zone
{%- endmacro %}

0 comments on commit 1a9f0fb

Please sign in to comment.