Skip to content

Commit 4c2eb2c

Browse files
authored
Merge pull request #133 from fishtown-analytics/fix/use-relations
Replace deprecated adapter methods with relations
2 parents 083b119 + 358f94d commit 4c2eb2c

File tree

10 files changed

+40
-46
lines changed

10 files changed

+40
-46
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ from {{ref('my_model')}}
258258
```
259259

260260
#### union_tables ([source](macros/sql/union.sql))
261-
This macro implements an "outer union." The list of tables provided to this macro will be unioned together, and any columns exclusive to a subset of these tables will be filled with `null` where not present. The `column_override` argument is used to explicitly assign the column type for a set of columns. The `source_column_name` argument is used to change the name of the`_dbt_source_table` field.
261+
This macro implements an "outer union." The list of relations provided to this macro will be unioned together, and any columns exclusive to a subset of these tables will be filled with `null` where not present. The `column_override` argument is used to explicitly assign the column type for a set of columns. The `source_column_name` argument is used to change the name of the`_dbt_source_table` field.
262262

263263
Usage:
264264
```

integration_tests/models/sql/test_nullcheck_table.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
with nulled as (
99

10-
{{ dbt_utils.nullcheck_table(tbl.schema, tbl.name) }}
10+
{{ dbt_utils.nullcheck_table(tbl) }}
1111

1212
)
1313

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% macro _is_relation(obj, macro) %}
2+
{%- if not (obj is mapping and obj.get('metadata', {}).get('type', '').endswith('Relation')) -%}
3+
{%- do exceptions.raise_compiler_error("Macro " ~ macro ~ " expected a Relation but received the value: " ~ obj) -%}
4+
{%- endif -%}
5+
{% endmacro %}

macros/schema_tests/equality.sql

+2-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99
{% endif %}
1010

1111
-- setup
12-
13-
{% set schema = model.schema %}
14-
{% set model_a_name = model.name %}
15-
16-
{% set dest_columns = adapter.get_columns_in_table(schema, model_a_name) %}
12+
{%- do dbt_utils._is_relation(model, 'test_equality') -%}
13+
{% set dest_columns = adapter.get_columns_in_relation(model) %}
1714
{% set dest_cols_csv = dest_columns | map(attribute='quoted') | join(', ') %}
1815

1916
with a as (

macros/sql/get_tables_by_prefix.sql

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
{% macro get_tables_by_prefix(schema, prefix, exclude='') %}
1+
{% macro get_tables_by_prefix(schema, prefix, exclude='', database=target.database) %}
22

3-
{%- call statement('tables', fetch_result=True) %}
3+
{%- call statement('get_tables', fetch_result=True) %}
44

5-
{{ dbt_utils.get_tables_by_prefix_sql(schema, prefix, exclude) }}
5+
{{ dbt_utils.get_tables_by_prefix_sql(schema, prefix, exclude, database) }}
66

77
{%- endcall -%}
88

9-
{%- set table_list = load_result('tables') -%}
9+
{%- set table_list = load_result('get_tables') -%}
1010

11-
{%- if table_list and table_list['data'] -%}
12-
{%- set tables = table_list['data'] | map(attribute=0) | list %}
13-
{{ return(tables) }}
11+
{%- if table_list and table_list['table'] -%}
12+
{%- set tbl_relations = [] -%}
13+
{%- for row in table_list['table'] -%}
14+
{%- set tbl_relation = api.Relation.create(database, row.table_schema, row.table_name) -%}
15+
{%- do tbl_relations.append(tbl_relation) -%}
16+
{%- endfor -%}
17+
18+
{{ return(tbl_relations) }}
1419
{%- else -%}
1520
{{ return([]) }}
1621
{%- endif -%}

macros/sql/get_tables_by_prefix_sql.sql

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
{% macro get_tables_by_prefix_sql(schema, prefix, exclude='') %}
1+
{% macro get_tables_by_prefix_sql(schema, prefix, exclude='', database=target.database) %}
22
{{ adapter_macro('dbt_utils.get_tables_by_prefix_sql', schema, prefix, exclude) }}
33
{% endmacro %}
44

5-
{% macro default__get_tables_by_prefix_sql(schema, prefix, exclude='') %}
5+
{% macro default__get_tables_by_prefix_sql(schema, prefix, exclude='', database=target.database) %}
66

77
select distinct
8-
table_schema || '.' || table_name as ref
9-
from information_schema.tables
8+
table_schema as "table_schema", table_name as "table_name"
9+
from {{database}}.information_schema.tables
1010
where table_schema = '{{ schema }}'
1111
and table_name ilike '{{ prefix }}%'
1212
and table_name not ilike '{{ exclude }}'
1313

1414
{% endmacro %}
1515

1616

17-
{% macro bigquery__get_tables_by_prefix_sql(schema, prefix, exclude='') %}
17+
{% macro bigquery__get_tables_by_prefix_sql(schema, prefix, exclude='', database=target.database) %}
1818

1919
select distinct
20-
concat(dataset_id, '.', table_id) as ref
20+
dataset_id as table_schema, table_id as table_name
2121

22-
from {{schema}}.__TABLES_SUMMARY__
22+
from {{adapter.quote(database)}}.{{schema}}.__TABLES_SUMMARY__
2323
where dataset_id = '{{schema}}'
2424
and lower(table_id) like lower ('{{prefix}}%')
2525
and lower(table_id) not like lower ('{{exclude}}')

macros/sql/nullcheck_table.sql

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
{% macro nullcheck_table(schema, table) %}
1+
{% macro nullcheck_table(relation) %}
22

3-
{% set cols = adapter.get_columns_in_table(schema, table) %}
3+
{%- do dbt_utils._is_relation(relation, 'nullcheck_table') -%}
4+
{% set cols = adapter.get_columns_in_relation(relation) %}
45

56
select {{ dbt_utils.nullcheck(cols) }}
6-
from {{schema}}.{{table}}
7+
from {{relation}}
78

89
{% endmacro %}

macros/sql/star.sql

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
{% macro star(from, relation_alias=False, except=[]) -%}
2+
3+
{%- do dbt_utils._is_relation(from, 'star') -%}
24

35
{#-- Prevent querying of db in parsing mode. This works because this macro does not create any new refs. #}
46
{%- if not execute -%}
57
{{ return('') }}
68
{% endif %}
79

8-
{%- if from.name -%}
9-
{%- set schema_name, table_name = from.schema, from.name -%}
10-
{%- else -%}
11-
{%- set schema_name, table_name = (from | string).split(".") -%}
12-
{%- endif -%}
13-
1410
{%- set include_cols = [] %}
15-
{%- set cols = adapter.get_columns_in_table(schema_name, table_name) -%}
11+
{%- set cols = adapter.get_columns_in_relation(from) -%}
1612
{%- for col in cols -%}
1713

1814
{%- if col.column not in except -%}

macros/sql/union.sql

+2-7
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@
1616

1717
{%- set _ = table_columns.update({table: []}) %}
1818

19-
{%- if table.name -%}
20-
{%- set schema, table_name = table.schema, table.name -%}
21-
{%- else -%}
22-
{%- set schema, table_name = (table | string).split(".") -%}
23-
{%- endif -%}
24-
25-
{%- set cols = adapter.get_columns_in_table(schema, table_name) %}
19+
{%- do dbt_utils._is_relation(table, 'union_tables') -%}
20+
{%- set cols = adapter.get_columns_in_relation(table) %}
2621
{%- for col in cols -%}
2722

2823
{%- if col.column not in exclude %}

macros/sql/unpivot.sql

+3-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Pivot values from columns to rows.
44
Example Usage: {{ dbt_utils.unpivot(table=ref('users'), cast_to='integer', exclude=['id','created_at']) }}
55

66
Arguments:
7-
table: Table name, required.
7+
table: Relation object, required.
88
cast_to: The datatype to cast all unpivoted columns to. Default is varchar.
99
exclude: A list of columns to exclude from the unpivot operation. Default is none.
1010
#}
@@ -19,13 +19,8 @@ Arguments:
1919

2020
{%- set _ = table_columns.update({table: []}) %}
2121

22-
{%- if table.name -%}
23-
{%- set schema, table_name = table.schema, table.name -%}
24-
{%- else -%}
25-
{%- set schema, table_name = (table | string).split(".") -%}
26-
{%- endif -%}
27-
28-
{%- set cols = adapter.get_columns_in_table(schema, table_name) %}
22+
{%- do dbt_utils._is_relation(table, 'unpivot') -%}
23+
{%- set cols = adapter.get_columns_in_relation(table) %}
2924

3025
{%- for col in cols -%}
3126
{%- if col.column.lower() not in exclude|map('lower') -%}

0 commit comments

Comments
 (0)