Skip to content

Commit 5edee98

Browse files
authored
Merge pull request #101 from fishtown-analytics/fix/bq-macros-syntax
WIP fix: updated macros for bq compatibility
2 parents 9b22332 + 99443ea commit 5edee98

28 files changed

+280
-53
lines changed

.circleci/config.yml

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ jobs:
1010
steps:
1111
- checkout
1212

13+
- run:
14+
run: setup_creds
15+
command: |
16+
echo $GCLOUD_SERVICE_KEY | base64 --decode --ignore-garbage > ${HOME}/gcloud-service-key.json
17+
1318
- restore_cache:
1419
key: deps1-{{ .Branch }}
1520

@@ -25,7 +30,7 @@ jobs:
2530
cp integration_tests/ci/sample.profiles.yml ~/.dbt/profiles.yml
2631
2732
- run:
28-
name: "Run tests (postgres)"
33+
name: "Run Tests - Postgres"
2934
environment:
3035
CI_DBT_USER: root
3136
CI_DBT_PASS: ''
@@ -34,10 +39,47 @@ jobs:
3439
command: |
3540
. venv/bin/activate
3641
cd integration_tests
37-
dbt deps
38-
dbt seed --full-refresh
39-
dbt run --full-refresh --exclude test_insert_by_period
40-
dbt test --exclude test_insert_by_period
42+
dbt deps --target postgres
43+
dbt seed --target postgres --full-refresh
44+
dbt run --target postgres
45+
dbt test --target postgres
46+
47+
- run:
48+
name: "Run Tests - Redshift"
49+
command: |
50+
. venv/bin/activate
51+
echo `pwd`
52+
cd integration_tests
53+
dbt deps --target redshift
54+
dbt seed --target redshift --full-refresh
55+
dbt run --target redshift
56+
dbt test --target redshift
57+
58+
- run:
59+
name: "Run Tests - Snowflake"
60+
command: |
61+
. venv/bin/activate
62+
echo `pwd`
63+
cd integration_tests
64+
dbt deps --target snowflake
65+
dbt seed --target snowflake --full-refresh
66+
dbt run --target snowflake --exclude test_unpivot
67+
dbt test --target snowflake --exclude test_unpivot
68+
69+
- run:
70+
name: "Run Tests - BigQuery"
71+
environment:
72+
GCLOUD_SERVICE_KEY_PATH: "/home/circleci/gcloud-service-key.json"
73+
74+
command: |
75+
. venv/bin/activate
76+
echo `pwd`
77+
cd integration_tests
78+
dbt deps --target bigquery
79+
dbt seed --target bigquery --full-refresh
80+
dbt run --target bigquery
81+
dbt test --target bigquery
82+
4183
4284
- save_cache:
4385
key: deps1-{{ .Branch }}

integration_tests/ci/sample.profiles.yml

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,43 @@ config:
77
use_colors: True
88

99
integration_tests:
10-
target: ci
10+
target: postgres
1111
outputs:
12-
ci:
12+
postgres:
1313
type: postgres
1414
host: localhost
1515
user: "{{ env_var('CI_DBT_USER') }}"
1616
pass: "{{ env_var('CI_DBT_PASS') }}"
1717
port: "{{ env_var('CI_DBT_PORT') }}"
1818
dbname: "{{ env_var('CI_DBT_DBNAME') }}"
19-
schema: dbt_utils
19+
schema: dbt_utils_integration_tests_postgres
20+
threads: 1
21+
22+
redshift:
23+
type: redshift
24+
host: "{{ env_var('CI_REDSHIFT_DBT_HOST') }}"
25+
user: "{{ env_var('CI_REDSHIFT_DBT_USER') }}"
26+
pass: "{{ env_var('CI_REDSHIFT_DBT_PASS') }}"
27+
dbname: "{{ env_var('CI_REDSHIFT_DBT_DBNAME') }}"
28+
port: 5439
29+
schema: dbt_utils_integration_tests_redshift
30+
threads: 1
31+
32+
bigquery:
33+
type: bigquery
34+
method: service-account
35+
keyfile: "{{ env_var('GCLOUD_SERVICE_KEY_PATH') }}"
36+
project: 'dbt-integration-tests'
37+
schema: dbt_utils_integration_tests_bigquery
38+
threads: 1
39+
40+
snowflake:
41+
type: snowflake
42+
account: "{{ env_var('CI_SNOWFLAKE_DBT_ACCOUNT') }}"
43+
user: "{{ env_var('CI_SNOWFLAKE_DBT_USER') }}"
44+
password: "{{ env_var('CI_SNOWFLAKE_DBT_PASS') }}"
45+
role: "{{ env_var('CI_SNOWFLAKE_DBT_ROLE') }}"
46+
database: "{{ env_var('CI_SNOWFLAKE_DBT_DATABASE') }}"
47+
warehouse: "{{ env_var('CI_SNOWFLAKE_DBT_WAREHOUSE') }}"
48+
schema: dbt_utils_integration_tests_snowflake
2049
threads: 1
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
parts,split_on,result_1,result_2,result_3
22
a|b|c,|,a,b,c
33
1|2|3,|,1,2,3
4-
,|,,
4+
,|,,,

integration_tests/data/datetime/data_date_spine.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ date_day
77
2018-01-06
88
2018-01-07
99
2018-01-08
10-
2018-01-09
10+
2018-01-09

integration_tests/dbt_project.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,24 @@ target-path: "target" # directory which will store compiled SQL files
1414
clean-targets: # directories to be removed by `dbt clean`
1515
- "target"
1616
- "dbt_modules"
17+
18+
seeds:
19+
dbt_utils_integration_tests:
20+
enabled: true
21+
22+
cross_db:
23+
data_date_trunc:
24+
column_types:
25+
updated_at: timestamp
26+
day: date
27+
month: date
28+
29+
data_dateadd:
30+
column_types:
31+
from_time: timestamp
32+
result: timestamp
33+
34+
data_datediff:
35+
column_types:
36+
first_date: timestamp
37+
second_date: timestamp

integration_tests/models/cross_db_utils/test_date_trunc.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ with data as (
66
)
77

88
select
9-
{{ dbt_utils.date_trunc('day', 'updated_at') }} as actual,
9+
cast({{dbt_utils.date_trunc('day', 'updated_at') }} as date) as actual,
1010
day as expected
1111

1212
from data
1313

1414
union all
1515

1616
select
17-
{{ dbt_utils.date_trunc('month', 'updated_at') }} as actual,
17+
cast({{ dbt_utils.date_trunc('month', 'updated_at') }} as date) as actual,
1818
month as expected
1919

2020
from data

integration_tests/models/cross_db_utils/test_dateadd.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ with data as (
77

88
select
99
case
10-
when datepart = 'hour' then {{ dbt_utils.dateadd('hour', 'interval_length', 'from_time') }}
11-
when datepart = 'day' then {{ dbt_utils.dateadd('day', 'interval_length', 'from_time') }}
12-
when datepart = 'month' then {{ dbt_utils.dateadd('month', 'interval_length', 'from_time') }}
13-
when datepart = 'year' then {{ dbt_utils.dateadd('year', 'interval_length', 'from_time') }}
10+
when datepart = 'hour' then cast({{ dbt_utils.dateadd('hour', 'interval_length', 'from_time') }} as {{dbt_utils.type_timestamp()}})
11+
when datepart = 'day' then cast({{ dbt_utils.dateadd('day', 'interval_length', 'from_time') }} as {{dbt_utils.type_timestamp()}})
12+
when datepart = 'month' then cast({{ dbt_utils.dateadd('month', 'interval_length', 'from_time') }} as {{dbt_utils.type_timestamp()}})
13+
when datepart = 'year' then cast({{ dbt_utils.dateadd('year', 'interval_length', 'from_time') }} as {{dbt_utils.type_timestamp()}})
1414
else null
1515
end as actual,
1616
result as expected

integration_tests/models/datetime/test_date_spine.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ with date_spine as (
1111
{% if target.type == 'postgres' %}
1212
{{ log("WARNING: Not testing - datediff macro is unsupported on Postgres", info=True) }}
1313
select * from {{ ref('data_date_spine') }}
14+
15+
{% elif target.type == 'bigquery' %}
16+
select cast(date_day as date) as date_day
17+
from ({{ dbt_utils.date_spine("day", "'2018-01-01'", "'2018-01-10'") }})
18+
1419
{% else %}
1520
{{ dbt_utils.date_spine("day", "'2018-01-01'", "'2018-01-10'") }}
1621
{% endif %}

integration_tests/models/materializations/expected_insert_by_period.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{{
22
config(
3-
materialized = 'view')
3+
materialized = 'view',
4+
enabled=(target.type == 'redshift')
5+
)
46
}}
57

68
select *

integration_tests/models/materializations/test_insert_by_period.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
period = 'month',
55
timestamp_field = 'created_at',
66
start_date = '2018-01-01',
7-
stop_date = '2018-06-01')
7+
stop_date = '2018-06-01',
8+
enabled=(target.type == 'redshift')
9+
)
810
}}
911

1012
with events as (
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11

2+
{% if target.type == 'postgres' %}
3+
24
select
35
{{ dbt_utils.date_trunc('day', dbt_utils.current_timestamp()) }} as today
6+
7+
{% else %}
8+
9+
select
10+
cast({{ dbt_utils.date_trunc('day', dbt_utils.current_timestamp()) }} as datetime) as today
11+
12+
{% endif %}

integration_tests/models/sql/test_get_column_values.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
{% set columns = dbt_utils.get_column_values(ref('data_get_column_values'), 'field') %}
33

44

5+
{% if target.type == 'snowflake' %}
6+
57
select
68
{% set columns = columns if columns is iterable else [] %}
79
{% for column in columns -%}
@@ -12,3 +14,18 @@ select
1214
{%- endfor %}
1315

1416
from {{ ref('data_get_column_values') }}
17+
18+
{% else %}
19+
20+
select
21+
{% set columns = columns if columns is iterable else [] %}
22+
{% for column in columns -%}
23+
24+
{{dbt_utils.safe_cast("sum(case when field = '" ~ column ~ "' then 1 else 0 end)", dbt_utils.type_string()) }} as count_{{ column }}
25+
{%- if not loop.last %},{% endif -%}
26+
27+
{%- endfor %}
28+
29+
from {{ ref('data_get_column_values') }}
30+
31+
{% endif %}

integration_tests/models/sql/test_get_tables_by_prefix_and_union.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
{{config( materialized = 'table')}}
2+
13

24
{% if target.type == 'snowflake' %}
35

46
{% set tables = dbt_utils.get_tables_by_prefix((target.schema | upper), 'data_events_') %}
57
{{ dbt_utils.union_tables(tables) }}
6-
8+
79
{% else %}
810

911
{% set tables = dbt_utils.get_tables_by_prefix(target.schema, 'data_events_') %}

integration_tests/models/sql/test_groupby.sql

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1+
with test_data as (
2+
3+
select
4+
5+
{{ dbt_utils.safe_cast("'a'", dbt_utils.type_string() )}} as column_1,
6+
{{ dbt_utils.safe_cast("'b'", dbt_utils.type_string() )}} as column_2
7+
8+
),
9+
10+
grouped as (
11+
12+
select
13+
*,
14+
count(*) as total
15+
16+
from test_data
17+
{{ dbt_utils.group_by(2) }}
18+
19+
)
20+
21+
select * from grouped
122

223

3-
select
4-
'a' as column_1,
5-
'b' as column_2,
6-
count(*) as total
724

8-
{{ dbt_utils.group_by(2) }}
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
1-
1+
{{ config( materialized = "table" ) }}
2+
3+
-- TO DO: remove if-statement
4+
25
{% set tbl = ref('data_nullcheck_table') %}
36

7+
48
with nulled as (
59

610
{{ dbt_utils.nullcheck_table(tbl.schema, tbl.name) }}
711

812
)
913

14+
{% if target.type == 'snowflake' %}
15+
1016
select
1117
field_1::varchar as field_1,
1218
field_2::varchar as field_2,
1319
field_3::varchar as field_3
1420

1521
from nulled
22+
23+
{% else %}
24+
25+
select
26+
27+
{{ dbt_utils.safe_cast('field_1',
28+
dbt_utils.type_string()
29+
)}} as field_1,
30+
31+
{{ dbt_utils.safe_cast('field_2',
32+
dbt_utils.type_string()
33+
)}} as field_2,
34+
35+
{{ dbt_utils.safe_cast('field_3',
36+
dbt_utils.type_string()
37+
)}} as field_3
38+
39+
from nulled
40+
41+
{% endif %}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
{{ dbt_utils.unpivot(table=ref('data_unpivot'), cast_to='varchar', exclude=['customer_id','created_at']) }}
1+
{{ dbt_utils.unpivot(
2+
table=ref('data_unpivot'),
3+
cast_to=dbt_utils.type_string(),
4+
exclude=['customer_id','created_at']
5+
6+
) }}

macros/cross_db_utils/date_trunc.sql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@
77
{% endmacro %}
88

99
{% macro bigquery__date_trunc(datepart, date) %}
10-
date_trunc({{date}}, {{datepart}})
10+
timestamp_trunc(
11+
cast({{date}} as timestamp),
12+
{{datepart}}
13+
)
14+
15+
1116
{% endmacro %}

macros/cross_db_utils/dateadd.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
{% macro bigquery__dateadd(datepart, interval, from_date_or_timestamp) %}
1818

19-
date_add(
20-
cast( {{ from_date_or_timestamp }} as date),
19+
datetime_add(
20+
cast( {{ from_date_or_timestamp }} as datetime),
2121
interval {{ interval }} {{ datepart }}
2222
)
2323

macros/cross_db_utils/datediff.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
{% macro bigquery__datediff(first_date, second_date, datepart) %}
1818

19-
date_diff(
20-
{{second_date}},
21-
{{first_date}},
19+
datetime_diff(
20+
cast({{second_date}} as datetime),
21+
cast({{first_date}} as datetime),
2222
{{datepart}}
23-
)
23+
)
2424

2525
{% endmacro %}
2626

0 commit comments

Comments
 (0)