This [dbt](https://github.com/fishtown-analytics/dbt) package contains macros that can be (re)used across dbt projects.
## Installation Instructions
Check [dbt Hub](https://hub.getdbt.com/fishtown-analytics/dbt_utils/latest/) for the latest installation instructions, or [read the docs](https://docs.getdbt.com/docs/package-management) for more information on installing packages.
----
## Contents
**[Schema tests](#schema-tests)**
- [equal_rowcount](#equal_rowcount-source)
- [equality](#equality-source)
- [expression_is_true](#expression_is_true-source)
- [recency](#recency-source)
- [at_least_one](#at_least_one-source)
- [not_constant](#not_constant)
- [cardinality_equality](#cardinality_equality-source)
- [unique_where](#unique_where-source)
- [not_null_where](#not_null_where-source)
- [relationships_where](#relationships_where-source)
- [mutually_exclusive_ranges](#mutually_exclusive_ranges-source)
- [unique_combination_of_columns](#unique_combination_of_columns-source)
**[Macros](#macros)**
- [Introspective macros](#introspective-macros):
- [get_column_values](#get_column_values-source)
- [get_relations_by_pattern](#get_relations_by_pattern-source)
- [get_relations_by_prefix](#get_relations_by_prefix-source)
- [get_query_results_as_dict](#get_query_results_as_dict-source)
- [SQL generators](sql-generators)
- [date_spine](#date-spine_source)
- [haversine_distance](haversine_distance-source)]
- [group_by](#group_by-source)
- [star](#star-source)
- [union_relations](#union_relations-source)
- [generate_series](#generate_series-source)
- [surrogate_key](#surrogate_key-source)
- [safe_add](#safe_add-source)
- [pivot](#pivot-source)
- [unpivot](#unpivot-source)
- [Web macros](#web-macros)
- [get_url_parameter](#get_url_parameter-source)
- [get_url_host](#get_url_host-source)
- [get_url_path](#get_url_path-source)
- [Cross-database macros](#cross-database-macros):
- [current_timestamp](#current_timestamp-source)
- [dateadd](#date_add-source)
- [datediff](#datadiff-source)
- [split_part](#split_part-source)
- [last_day](#last_day-source)
- [width_bucket](#width_bucket-source)
- [Logger](#logger)
- [pretty_time](#pretty_time-source)
- [pretty_log_format](#pretty_log_format-source)
- [log_info](#log_info-source)
[Materializations](#materializations):
- [insert_by_period](#insert_by_period-source)
---
### Schema Tests
#### equal_rowcount ([source](macros/schema_tests/equal_rowcount.sql))
This schema test asserts the that two relations have the same number of rows.
**Usage:**
```yaml
version: 2
models:
- name: model_name
tests:
- dbt_utils.equal_rowcount:
compare_model: ref('other_table_name')
```
#### fewer_rows_than ([source](macros/schema_tests/fewer_rows_than.sql))
This schema test asserts that this model has fewer rows than the referenced model.
Usage:
```yaml
version: 2
models:
- name: model_name
tests:
- dbt_utils.fewer_rows_than:
compare_model: ref('other_table_name')
```
#### equality ([source](macros/schema_tests/equality.sql))
This schema test asserts the equality of two relations. Optionally specify a subset of columns to compare.
**Usage:**
```yaml
version: 2
models:
- name: model_name
tests:
- dbt_utils.equality:
compare_model: ref('other_table_name')
compare_columns:
- first_column
- second_column
```
#### expression_is_true ([source](macros/schema_tests/expression_is_true.sql))
This schema test asserts that a valid sql expression is true for all records. This is useful when checking integrity across columns, for example, that a total is equal to the sum of its parts, or that at least one column is true.
**Usage:**
```yaml
version: 2
models:
- name: model_name
tests:
- dbt_utils.expression_is_true:
expression: "col_a + col_b = total"
```
The macro accepts an optional argument `condition` that allows for asserting
the `expression` on a subset of all records.
**Usage:**
```yaml
version: 2
models:
- name: model_name
tests:
- dbt_utils.expression_is_true:
expression: "col_a + col_b = total"
condition: "created_at > '2018-12-31'"
```
This macro can also be used at the column level. When this is done, the `expression` is evaluated against the column.
```yaml
version: 2
models:
- name: model_name
columns:
- name: col_a
tests:
- dbt_utils.expression_is_true:
expression: '>= 1'
- name: col_b
tests:
- dbt_utils.expression_is_true:
expression: '= 1'
condition: col_a = 1
```
#### recency ([source](macros/schema_tests/recency.sql))
This schema test asserts that there is data in the referenced model at least as recent as the defined interval prior to the current timestamp.
**Usage:**
```yaml
version: 2
models:
- name: model_name
tests:
- dbt_utils.recency:
datepart: day
field: created_at
interval: 1
```
#### at_least_one ([source](macros/schema_tests/at_least_one.sql))
This schema test asserts if column has at least one value.
**Usage:**
```yaml
version: 2
models:
- name: model_name
columns:
- name: col_name
tests:
- dbt_utils.at_least_one
```
#### not_constant ([source](macros/schema_tests/not_constant.sql))
This schema test asserts if column does not have same value in all rows.
**Usage:**
```yaml
version: 2
models:
- name: model_name
columns:
- name: column_name
tests:
- dbt_utils.not_constant
```
#### cardinality_equality ([source](macros/schema_tests/cardinality_equality.sql))
This schema test asserts if values in a given column have exactly the same cardinality as values from a different column in a different model.
**Usage:**
```yaml
version: 2
models:
- name: model_name
columns:
- name: from_column
tests:
- dbt_utils.cardinality_equality:
field: other_column_name
to: ref('other_model_name')
```
#### unique_where ([source](macros/schema_tests/unique_where.sql))
This test validates that there are no duplicate values present in a field for a subset of rows by specifying a `where` clause.
**Usage:**
```yaml
version: 2
models:
- name: my_model
columns:
- name: id
tests:
- dbt_utils.unique_where:
where: "_deleted = false"
```
#### not_null_where ([source](macros/schema_tests/not_null_where.sql))
This test validates that there are no null values present in a column for a subset of rows by specifying a `where` clause.
**Usage:**
```yaml
version: 2
models:
- name: my_model
columns:
- name: id
tests:
- dbt_utils.not_null_where:
where: "_deleted = false"
```
#### not_accepted_values ([source](macros/schema_tests/not_accepted_values.sql))
This test validates that there are no rows that match the given values.
Usage:
```yaml
version: 2
models:
- name: my_model
columns:
- name: city
tests:
- dbt_utils.not_accepted_values:
values: ['Barcelona', 'New York']
```
#### relationships_where ([source](macros/schema_tests/relationships_where.sql))
This test validates the referential integrity between two relations (same as the core relationships schema test) with an added predicate to filter out some rows from the test. This is useful to exclude records such as test entities, rows created in the last X minutes/hours to account for temporary gaps due to ETL limitations, etc.
**Usage:**
```yaml
version: 2
models:
- name: model_name
columns:
- name: id
tests:
- dbt_utils.relationships_where:
to: ref('other_model_name')
field: client_id
from_condition: id <> '4ca448b8-24bf-4b8