Skip to content

jtalmi/dbt-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dbt logo


dbt-utils

Current version: 0.1.0

This package contains macros that can be (re)used across dbt projects.

Macros

Cross-database

current_timestamp (source)

This macro returns the current timestamp.

Usage:

{{ current_timestamp() }}

dateadd (source)

This macro adds a time/day interval to the supplied date/timestamp. Note: The datepart argument is database-specific.

Usage:

{{ dateadd(datepart='day', interval=1, from_date_or_timestamp='2017-01-01') }}

split_part (source)

This macro adds a time/day interval to the supplied date/timestamp. Note: The datepart argument is database-specific.

Usage:

{{ split_part(string_text='1,2,3', delimiter_text=',', part_number=1) }}

Date/Time

date_spine (source)

This macro returns the sql required to build a date spine.

Usage:

{{ date_spine(
    table=ref('organizations'),
    datepart="minute",
    start_date="to_date('01/01/2016', 'mm/dd/yyyy')",
    end_date="dateadd(week, 1, current_date)"
   )
}}

Geo

haversine_distance (source)

This macro calculates the haversine distance between a pair of x/y coordinates.

Usage:

{{ haversine_distance(lat1=<float>,lon1=<float>,lat2=<float>,lon2=<float>) }}

Schema Tests

equality (source)

This schema test asserts the equality of two relations.

Usage:

model_name:
  constraints:
    equality:
      - ref('other_table_name')


SQL helpers

group_by (source)

This macro build a group by statement for fields 1...N

Usage:

{{ group_by(n=3) }} --> group by 1,2,3

star (source)

This macro generates a select statement for each field that exists in the from relation. Fields listed in the except argument will be excluded from this list.

Usage:

{{ macro star(from=ref('my_model'), except=["exclude_field_1", "exclude_field_2"]) }}

union_tables (source)

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.

Usage:

{{ macro union_tables(tables=[ref('table_1', 'table_2')], column_override={"some_field": "varchar(100)"}) }}

Web

get_url_parameter (source)

This macro extracts a url parameter from a column containing a url.

Usage:

{{ macro get_url_parameter(field='page_url', url_parameter='utm_source') }}

Getting started with dbt

Code of Conduct

Everyone interacting in the dbt project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the PyPA Code of Conduct.

Packages

No packages published

Languages

  • TSQL 82.9%
  • PLpgSQL 15.3%
  • Shell 1.1%
  • Makefile 0.7%