Skip to content

Instantly share code, notes, and snippets.

View drewbanin's full-sized avatar
📈
We're hiring!

Drew Banin drewbanin

📈
We're hiring!
View GitHub Profile
with current_data as (
select * from {{ source_table }}
),
archived_data as (
select * from {{ this }}
@drewbanin
drewbanin / indexes.sql
Created April 26, 2017 20:43
table indexes (postgres)
-- Macros to add indexes:
-- macros/indexes.sql
{% macro index(this, column) %}
drop index if exists "{{ this.schema }}"."{{ this.name }}__index_on_{{ column }}" cascade;
create index "{{ this.name }}__index_on_{{ column }}" on {{ this }} ("{{ column }}")
{% endmacro %}
#!/bin/bash
INSANE_PYTHON='/usr/bin/python' # not sane?
VENV_PATH="$HOME/.dbt/dbt-dev-env"
DBT_REPO_REMOTE="https://github.com/fishtown-analytics/dbt.git"
DBT_LOCAL_PATH="${TMPDIR}dbt-development"
DBT_ALIAS="use-dbt-dev"
function install_python() {
echo 'Installing python'
@drewbanin
drewbanin / show_ancestors.py
Created July 11, 2017 15:22
This script shows the ancestors for each node in a dbt graph
import sys
import networkx as nx
if len(sys.argv) != 2:
print("Usage: {} [path/to/graph.gpickle]".format(sys.argv[0]))
sys.exit(1)
filename = sys.argv[1]
graph = nx.read_gpickle(sys.argv[1])
-- pseudocode:
```
{% set table_comment = "here is a comment about my table" %}
{% set column_comments = {
"column_1": "column 1 comment is...",
"column_2": "column 2 comment is...",
}
%}
{%- call statement('states', fetch_result=True) -%}
select distinct state from {{ ref('users') }}
{%- endcall -%}
-- Load the results of our statement and select the first column
{%- set states = load_result('states')['data'] | map(attribute=0)-%}
select
*,
-- Loop over each state
{% for state in states | list -%}
case
select
id,
user_id,
price,
null::timestamp as returned_at -- this column doesn't exist!
from ecom_us.orders
union all
select
select
'analytics.uk_orders'::text as _dbt_source_table,
"id"::integer as "id",
"user_id"::character varying(255) as "user_id",
"price"::character varying(255) as "price",
"returned_at"::timestamp without time zone as "returned_at"
from analytics.uk_orders
{% set orders_tables = [
ref('orders_uk'),
ref('orders_us')]
%}
{{ union_tables(orders_tables) }}