|
| 1 | +<p align="center"> |
| 2 | + <img src="etc/dbt-logo.png" alt="dbt logo" /> |
| 3 | +</p> |
| 4 | + |
| 5 | +---- |
| 6 | + |
1 | 7 | # dbt-utils
|
2 |
| -Utility functions for dbt projects. |
| 8 | +This package contains macros that can be (re)used across dbt projects. |
| 9 | + |
| 10 | +## Macros |
| 11 | +### Cross-database |
| 12 | +#### current_timestamp ([source](macros/cross_db_utils/current_timestamp.sql)) |
| 13 | +This macro returns the current timestamp. |
| 14 | + |
| 15 | +Usage: |
| 16 | +``` |
| 17 | +{{ current_timestamp() }} |
| 18 | +``` |
| 19 | + |
| 20 | +#### dateadd ([source](macros/cross_db_utils/dateadd.sql)) |
| 21 | +This macro adds a time/day interval to the supplied date/timestamp. Note: The `datepart` argument is database-specific. |
| 22 | + |
| 23 | +Usage: |
| 24 | +``` |
| 25 | +{{ dateadd(datepart='day', interval=1, from_date_or_timestamp='2017-01-01') }} |
| 26 | +``` |
| 27 | + |
| 28 | +#### split_part ([source](macros/cross_db_utils/split_part.sql)) |
| 29 | +This macro adds a time/day interval to the supplied date/timestamp. Note: The `datepart` argument is database-specific. |
| 30 | + |
| 31 | +Usage: |
| 32 | +``` |
| 33 | +{{ split_part(string_text='1,2,3', delimiter_text=',', part_number=1) }} |
| 34 | +``` |
| 35 | +--- |
| 36 | +### Date/Time |
| 37 | +#### date_spine ([source](macros/datetime/date_spine.sql)) |
| 38 | +This macro returns the sql required to build a date spine. |
| 39 | + |
| 40 | +Usage: |
| 41 | +``` |
| 42 | +{{ date_spine( |
| 43 | + table=ref('organizations'), |
| 44 | + datepart="minute", |
| 45 | + start_date="to_date('01/01/2016', 'mm/dd/yyyy')", |
| 46 | + end_date="dateadd(week, 1, current_date)" |
| 47 | + ) |
| 48 | +}} |
| 49 | +``` |
| 50 | +--- |
| 51 | +### Geo |
| 52 | +#### haversine_distance ([source](macros/geo/haversine_distance.sql)) |
| 53 | +This macro calculates the [haversine distance](http://daynebatten.com/2015/09/latitude-longitude-distance-sql/) between a pair of x/y coordinates. |
| 54 | + |
| 55 | +Usage: |
| 56 | +``` |
| 57 | +{{ haversine_distance(lat1=<float>,lon1=<float>,lat2=<float>,lon2=<float>) }} |
| 58 | +``` |
| 59 | +--- |
| 60 | +### Schema Tests |
| 61 | +#### equality ([source](macros/schema_tests/equality.sql)) |
| 62 | +This schema test asserts the equality of two relations. |
| 63 | + |
| 64 | +Usage: |
| 65 | +``` |
| 66 | +model_name: |
| 67 | + constraints: |
| 68 | + equality: |
| 69 | + - ref('other_table_name') |
| 70 | +
|
| 71 | +``` |
| 72 | +--- |
| 73 | +### SQL helpers |
| 74 | +#### group_by ([source](macros/sql/groupby.sql)) |
| 75 | +This macro build a group by statement for fields 1...N |
| 76 | + |
| 77 | +Usage: |
| 78 | +``` |
| 79 | +{{ group_by(n=3) }} --> group by 1,2,3 |
| 80 | +``` |
| 81 | + |
| 82 | +#### star ([source](macros/sql/star.sql)) |
| 83 | +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. |
| 84 | + |
| 85 | +Usage: |
| 86 | +``` |
| 87 | +{{ macro star(from=ref('my_model'), except=["exclude_field_1", "exclude_field_2"]) }} |
| 88 | +``` |
| 89 | + |
| 90 | +#### union_tables ([source](macros/sql/union.sql)) |
| 91 | +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. |
| 92 | + |
| 93 | +Usage: |
| 94 | +``` |
| 95 | +{{ macro union_tables(tables=[ref('table_1', 'table_2')], column_override={"some_field": "varchar(100)"}) }} |
| 96 | +``` |
| 97 | +--- |
| 98 | +### Web |
| 99 | +#### get_url_parameter ([source](macros/web/get_url_parameter.sql)) |
| 100 | +This macro extracts a url parameter from a column containing a url. |
| 101 | + |
| 102 | +Usage: |
| 103 | +``` |
| 104 | +{{ macro get_url_parameter(field='page_url', url_parameter='utm_source') }} |
| 105 | +``` |
| 106 | + |
| 107 | +---- |
| 108 | +### Getting started with dbt |
| 109 | + |
| 110 | +- [What is dbt]? |
| 111 | +- Read the [dbt viewpoint] |
| 112 | +- [Installation] |
| 113 | +- Join the [chat][slack-url] on Slack for live questions and support. |
| 114 | + |
| 115 | + |
| 116 | +## Code of Conduct |
| 117 | + |
| 118 | +Everyone interacting in the dbt project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the [PyPA Code of Conduct]. |
| 119 | + |
| 120 | + |
3 | 121 |
|
4 |
| -### This repo is under active development and breaking changes may be readily introduced at the current time |
| 122 | +[PyPA Code of Conduct]: https://www.pypa.io/en/latest/code-of-conduct/ |
| 123 | +[slack-url]: http://ac-slackin.herokuapp.com/ |
| 124 | +[Installation]: https://dbt.readme.io/docs/installation |
| 125 | +[What is dbt]: https://dbt.readme.io/docs/overview |
| 126 | +[dbt viewpoint]: https://dbt.readme.io/docs/viewpoint |
0 commit comments