Skip to content

Commit

Permalink
Merge pull request #220 from fishtown-analytics/feature/get_relations…
Browse files Browse the repository at this point in the history
…_by_schema_prefix

feature/get relations by schema prefix
  • Loading branch information
JDW818 authored Jul 9, 2020
2 parents 1dc5d88 + f824abc commit b736cf6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,35 @@ handy paired with `union_relations`.
* `database` (optional, default = `target.database`): The database to inspect
for relations.

#### get_relations_by_pattern
> This was built from the get_relations_by_prefix macro.
Returns a list of [Relations](https://docs.getdbt.com/docs/writing-code-in-dbt/class-reference/#relation)
that match a given schema or table pattern and table/view name with an optional exclusion pattern. Like its cousin
get_relations_by_prefix, it's particularly handy paired with `union_relations`.
**Usage:**
```
-- Returns a list of relations that match schema%.table
{% set relations = dbt_utils.get_relations_by_pattern('schema_pattern%', 'table_pattern') %}
-- Returns a list of relations that match schema.table%
{% set relations = dbt_utils.get_relations_by_pattern('schema_pattern', 'table_pattern%') %}
-- Returns a list of relations as above, excluding any that end in `deprecated`
{% set relations = dbt_utils.get_relations_by_pattern('schema_pattern', 'table_pattern%', '%deprecated') %}
-- Example using the union_relations macro
{% set event_relations = dbt_utils.get_relations_by_pattern('venue%', 'clicks') %}
{{ dbt_utils.union_relations(relations = event_relations) }}
```

**Args:**
* `schema_pattern` (required): The schema pattern to inspect for relations.
* `table_pattern` (required): The name of the table/view (case insensitive).
* `exclude` (optional): Exclude any relations that match this table pattern.
* `database` (optional, default = `target.database`): The database to inspect
for relations.

#### group_by ([source](macros/sql/groupby.sql))
This macro build a group by statement for fields 1...N

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ config(materialized = 'table') }}

{% set relations = dbt_utils.get_relations_by_pattern(target.schema, 'data_events_') %}
{{ dbt_utils.union_relations(relations) }}
23 changes: 23 additions & 0 deletions macros/sql/get_relations_by_pattern.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% macro get_relations_by_pattern(schema_pattern, table_pattern, exclude='', database=target.database) %}

{%- call statement('get_tables', fetch_result=True) %}

{{ dbt_utils.get_tables_by_pattern(schema_pattern, table_pattern, exclude, database) }}

{%- endcall -%}

{%- set table_list = load_result('get_tables') -%}

{%- if table_list and table_list['table'] -%}
{%- set tbl_relations = [] -%}
{%- for row in table_list['table'] -%}
{%- set tbl_relation = api.Relation.create(database, row.table_schema, row.table_name) -%}
{%- do tbl_relations.append(tbl_relation) -%}
{%- endfor -%}

{{ return(tbl_relations) }}
{%- else -%}
{{ return([]) }}
{%- endif -%}

{% endmacro %}
12 changes: 12 additions & 0 deletions macros/sql/get_tables_by_pattern.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% macro get_tables_by_pattern(schema_pattern, table_pattern, exclude='', database=target.database) %}

select distinct
table_schema as "table_schema", table_name as "table_name"
from {{database}}.information_schema.tables
where table_schema ilike '{{ schema_pattern }}'
and table_name ilike '{{ table_pattern }}'
and table_name not ilike '{{ exclude }}'

{% endmacro %}


0 comments on commit b736cf6

Please sign in to comment.