Skip to content

Commit

Permalink
feat: Add dbt aggregation models
Browse files Browse the repository at this point in the history
  • Loading branch information
1ambda committed Sep 3, 2023
1 parent 2ce344d commit 6e2aeb9
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 44 deletions.
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,17 @@ repos:
# - id: dbt-test
# name: 'dbt test'
# verbose: true


# SQL
- repo: https://github.com/sqlfluff/sqlfluff
rev: 2.3.1
hooks:
- id: sqlfluff-fix
name: 'sqlfluff fix'
verbose: true
files: ^(dags|dbts)/
- id: sqlfluff-lint
name: 'sqlfluff lint'
verbose: true
files: ^(dags|dbts)/
2 changes: 2 additions & 0 deletions dbts/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
__sqlfluff_tmp_*.sql

# Created by https://www.toptal.com/developers/gitignore/api/dbt,python
# Edit at https://www.toptal.com/developers/gitignore?templates=dbt,python

Expand Down
12 changes: 12 additions & 0 deletions dbts/dbt-model/mart/_aggr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2

models:
- name: aggr_location
tags:
- iceberg
- aggregation
columns:
- name: loc
tests:
- not_null
- name: count
10 changes: 10 additions & 0 deletions dbts/dbt-model/mart/aggr_location.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
WITH location AS (
SELECT *
FROM {{ ref('int_location') }}
)

SELECT
concat_ws(' - ', location.region, location.nation) AS loc,
count(*) AS count
FROM location
GROUP BY 1
15 changes: 15 additions & 0 deletions dbts/dbt-model/staging/_int.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 2

models:
- name: location
tags:
- iceberg
- intermediate
columns:
- name: nation_key
tests:
- unique
- not_null
- name: region_key
tests:
- not_null
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
version: 2

models:
- name: staging_nations
- name: stg_nations
description: staging layer for nations data
tags:
- "iceberg"
- "staging"
- iceberg
- staging
columns:
- name: nation_key
tests:
- not_null

- name: staging_regions
- name: stg_regions
description: staging layer for regions data
tags:
- "iceberg"
- "staging"
- iceberg
- staging
columns:
- name: region_key
tests:
Expand Down
23 changes: 23 additions & 0 deletions dbts/dbt-model/staging/int_location.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
WITH nation AS (
SELECT *
FROM {{ ref('stg_nations') }}
),

region AS (
SELECT *
FROM {{ ref('stg_regions') }}
),

final AS (
SELECT
nation.nation_key,
nation.name AS nation,
nation.region_key,
region.name AS region
FROM nation
INNER JOIN region
ON nation.region_key = region.region_key
)

SELECT *
FROM final
23 changes: 0 additions & 23 deletions dbts/dbt-model/staging/staging_nations.sql

This file was deleted.

11 changes: 0 additions & 11 deletions dbts/dbt-model/staging/staging_regions.sql

This file was deleted.

30 changes: 30 additions & 0 deletions dbts/dbt-model/staging/stg_nations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
WITH source AS (
SELECT *
FROM {{ source('tpch', 'nation') }}
),

renamed AS (
SELECT
nationkey AS nation_key,
name AS name,
regionkey AS region_key,
comment AS comment

FROM source

UNION ALL
(

-- make sure to test for dupes!!!
SELECT
nationkey AS nation_key,
NULL AS name,
NULL AS region_key,
NULL AS comment

FROM source LIMIT 1
)
)

SELECT *
FROM renamed
16 changes: 16 additions & 0 deletions dbts/dbt-model/staging/stg_regions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
WITH source AS (
SELECT *
FROM {{ source('tpch', 'region') }}
),

renamed AS (
SELECT
regionkey AS region_key,
name AS name,
comment AS comment

FROM source
)

SELECT *
FROM renamed
9 changes: 5 additions & 4 deletions dbts/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ seed-paths: [ "dbt-seeds" ]
macro-paths: [ "dbt-macro" ]
snapshot-paths: [ "dbt-snapshot" ]
target-path: "target"
clean-targets: # directories to be removed by `dbt clean`
clean-targets:
- "target"
- "logs"
- "dbt_modules"
# - "dbt_packages"

# Configure variables
vars:
Expand All @@ -28,11 +27,13 @@ vars:
# Configure models
models:
lakehouse:
+on_table_exists: drop
staging:
+schema: staging
+materialized: view
mart:
+schema: mart
+materialized: table
+on_table_exists: drop


# Configure dispatches
dispatch:
Expand Down

0 comments on commit 6e2aeb9

Please sign in to comment.