Skip to content

Commit b736cf6

Browse files
authored
Merge pull request #220 from fishtown-analytics/feature/get_relations_by_schema_prefix
feature/get relations by schema prefix
2 parents 1dc5d88 + f824abc commit b736cf6

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,35 @@ handy paired with `union_relations`.
468468
* `database` (optional, default = `target.database`): The database to inspect
469469
for relations.
470470

471+
#### get_relations_by_pattern
472+
> This was built from the get_relations_by_prefix macro.
473+
474+
Returns a list of [Relations](https://docs.getdbt.com/docs/writing-code-in-dbt/class-reference/#relation)
475+
that match a given schema or table pattern and table/view name with an optional exclusion pattern. Like its cousin
476+
get_relations_by_prefix, it's particularly handy paired with `union_relations`.
477+
**Usage:**
478+
```
479+
-- Returns a list of relations that match schema%.table
480+
{% set relations = dbt_utils.get_relations_by_pattern('schema_pattern%', 'table_pattern') %}
481+
482+
-- Returns a list of relations that match schema.table%
483+
{% set relations = dbt_utils.get_relations_by_pattern('schema_pattern', 'table_pattern%') %}
484+
485+
-- Returns a list of relations as above, excluding any that end in `deprecated`
486+
{% set relations = dbt_utils.get_relations_by_pattern('schema_pattern', 'table_pattern%', '%deprecated') %}
487+
488+
-- Example using the union_relations macro
489+
{% set event_relations = dbt_utils.get_relations_by_pattern('venue%', 'clicks') %}
490+
{{ dbt_utils.union_relations(relations = event_relations) }}
491+
```
492+
493+
**Args:**
494+
* `schema_pattern` (required): The schema pattern to inspect for relations.
495+
* `table_pattern` (required): The name of the table/view (case insensitive).
496+
* `exclude` (optional): Exclude any relations that match this table pattern.
497+
* `database` (optional, default = `target.database`): The database to inspect
498+
for relations.
499+
471500
#### group_by ([source](macros/sql/groupby.sql))
472501
This macro build a group by statement for fields 1...N
473502

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{{ config(materialized = 'table') }}
2+
3+
{% set relations = dbt_utils.get_relations_by_pattern(target.schema, 'data_events_') %}
4+
{{ dbt_utils.union_relations(relations) }}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{% macro get_relations_by_pattern(schema_pattern, table_pattern, exclude='', database=target.database) %}
2+
3+
{%- call statement('get_tables', fetch_result=True) %}
4+
5+
{{ dbt_utils.get_tables_by_pattern(schema_pattern, table_pattern, exclude, database) }}
6+
7+
{%- endcall -%}
8+
9+
{%- set table_list = load_result('get_tables') -%}
10+
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) }}
19+
{%- else -%}
20+
{{ return([]) }}
21+
{%- endif -%}
22+
23+
{% endmacro %}

macros/sql/get_tables_by_pattern.sql

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% macro get_tables_by_pattern(schema_pattern, table_pattern, exclude='', database=target.database) %}
2+
3+
select distinct
4+
table_schema as "table_schema", table_name as "table_name"
5+
from {{database}}.information_schema.tables
6+
where table_schema ilike '{{ schema_pattern }}'
7+
and table_name ilike '{{ table_pattern }}'
8+
and table_name not ilike '{{ exclude }}'
9+
10+
{% endmacro %}
11+
12+

0 commit comments

Comments
 (0)