-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support render for versioned dbt models. (#516)
Add support to model versioning available since dbt 1.6. [Model version](https://docs.getdbt.com/docs/collaborate/govern/model-versions) is supported by dbt-core >= 1.5. As cosmos v1.x, it does not support rendering tasks for the model have versions. Use `node_dict["alias"]` as `DbtNode.name` instead of `node_dict["name"]` if `node_dict["resource_type"]` == `model`. This PR also helps cosmos render the names of nodes to be correctly the same as `identifier` if users config alias or custom the `generate_alias_name` macro. Refer: [alias document](https://docs.getdbt.com/reference/resource-configs/alias) and [the `generate_alias_name` implementation](https://github.com/dbt-labs/dbt-core/blob/ada8860e48b32ac712d92e8b0977b2c3c9749981/core/dbt/include/global_project/macros/get_custom_name/get_custom_alias.sql#L26-L30). Closes: #260 ## Breaking Change? When the model has a version config, each model version will create a database relation with alias `<model_name>_v<v>`, for examples: `dim_customers_v1`, `dim_customers_v2`... Cosmos will create run/test tasks for each version, for example: dim_customers_v1_run, dim_customers_v2_run, dim_customers_v1_test, dim_customers_v2_test... The second expectation is the dag will render the right dependencies, for example: stg_customers_v1 >> customers_v1, stg_customers_v2 >> customer_v2...
- Loading branch information
Showing
26 changed files
with
15,653 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
## `jaffle_shop` | ||
|
||
`jaffle_shop` is a fictional ecommerce store. This dbt project transforms raw data from an app database into a customers and orders model ready for analytics. | ||
|
||
See [dbt's documentation](https://github.com/dbt-labs/jaffle_shop) for more info. | ||
|
||
### Modifications | ||
|
||
This project has been modified from the original to highlight some of the features of Cosmos. Namely: | ||
|
||
- tags have been added to the models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: 'jaffle_shop' | ||
|
||
config-version: 2 | ||
version: '0.1' | ||
|
||
profile: 'jaffle_shop' | ||
|
||
model-paths: ["models"] | ||
seed-paths: ["seeds"] | ||
test-paths: ["tests"] | ||
analysis-paths: ["analysis"] | ||
macro-paths: ["macros"] | ||
|
||
target-path: "target" | ||
clean-targets: | ||
- "target" | ||
- "dbt_modules" | ||
- "logs" | ||
|
||
require-dbt-version: [">=1.0.0", "<2.0.0"] | ||
|
||
models: | ||
jaffle_shop: | ||
materialized: table | ||
staging: | ||
materialized: view |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
with customers as ( | ||
|
||
select * from {{ ref('stg_customers') }} | ||
|
||
), | ||
|
||
orders as ( | ||
|
||
select * from {{ ref('stg_orders') }} | ||
|
||
), | ||
|
||
payments as ( | ||
|
||
select * from {{ ref('stg_payments') }} | ||
|
||
), | ||
|
||
customer_orders as ( | ||
|
||
select | ||
customer_id, | ||
|
||
min(order_date) as first_order, | ||
max(order_date) as most_recent_order, | ||
count(order_id) as number_of_orders | ||
from orders | ||
|
||
group by customer_id | ||
|
||
), | ||
|
||
customer_payments as ( | ||
|
||
select | ||
orders.customer_id, | ||
sum(amount) as total_amount | ||
|
||
from payments | ||
|
||
left join orders on | ||
payments.order_id = orders.order_id | ||
|
||
group by orders.customer_id | ||
|
||
), | ||
|
||
final as ( | ||
|
||
select | ||
customers.customer_id, | ||
customers.first_name, | ||
customers.last_name, | ||
customers.full_name, | ||
customer_orders.first_order, | ||
customer_orders.most_recent_order, | ||
customer_orders.number_of_orders, | ||
customer_payments.total_amount as customer_lifetime_value | ||
|
||
from customers | ||
|
||
left join customer_orders | ||
on customers.customer_id = customer_orders.customer_id | ||
|
||
left join customer_payments | ||
on customers.customer_id = customer_payments.customer_id | ||
|
||
) | ||
|
||
select * from final |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
with customers as ( | ||
|
||
select * from {{ ref('stg_customers', v=1) }} | ||
|
||
), | ||
|
||
orders as ( | ||
|
||
select * from {{ ref('stg_orders') }} | ||
|
||
), | ||
|
||
payments as ( | ||
|
||
select * from {{ ref('stg_payments') }} | ||
|
||
), | ||
|
||
customer_orders as ( | ||
|
||
select | ||
customer_id, | ||
|
||
min(order_date) as first_order, | ||
max(order_date) as most_recent_order, | ||
count(order_id) as number_of_orders | ||
from orders | ||
|
||
group by customer_id | ||
|
||
), | ||
|
||
customer_payments as ( | ||
|
||
select | ||
orders.customer_id, | ||
sum(amount) as total_amount | ||
|
||
from payments | ||
|
||
left join orders on | ||
payments.order_id = orders.order_id | ||
|
||
group by orders.customer_id | ||
|
||
), | ||
|
||
final as ( | ||
|
||
select | ||
customers.customer_id, | ||
customers.first_name, | ||
customers.last_name, | ||
customer_orders.first_order, | ||
customer_orders.most_recent_order, | ||
customer_orders.number_of_orders, | ||
customer_payments.total_amount as customer_lifetime_value | ||
|
||
from customers | ||
|
||
left join customer_orders | ||
on customers.customer_id = customer_orders.customer_id | ||
|
||
left join customer_payments | ||
on customers.customer_id = customer_payments.customer_id | ||
|
||
) | ||
|
||
select * from final |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{% docs orders_status %} | ||
|
||
Orders can be one of the following statuses: | ||
|
||
| status | description | | ||
|----------------|------------------------------------------------------------------------------------------------------------------------| | ||
| placed | The order has been placed but has not yet left the warehouse | | ||
| shipped | The order has ben shipped to the customer and is currently in transit | | ||
| completed | The order has been received by the customer | | ||
| return_pending | The customer has indicated that they would like to return the order, but it has not yet been received at the warehouse | | ||
| returned | The order has been returned by the customer and received at the warehouse | | ||
|
||
|
||
{% enddocs %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{% set payment_methods = ['credit_card', 'coupon', 'bank_transfer', 'gift_card'] %} | ||
|
||
with orders as ( | ||
|
||
select * from {{ ref('stg_orders') }} | ||
|
||
), | ||
|
||
payments as ( | ||
|
||
select * from {{ ref('stg_payments') }} | ||
|
||
), | ||
|
||
order_payments as ( | ||
|
||
select | ||
order_id, | ||
|
||
{% for payment_method in payment_methods -%} | ||
sum(case when payment_method = '{{ payment_method }}' then amount else 0 end) as {{ payment_method }}_amount, | ||
{% endfor -%} | ||
|
||
sum(amount) as total_amount | ||
|
||
from payments | ||
|
||
group by order_id | ||
|
||
), | ||
|
||
final as ( | ||
|
||
select | ||
orders.order_id, | ||
orders.customer_id, | ||
orders.order_date, | ||
orders.status, | ||
|
||
{% for payment_method in payment_methods -%} | ||
|
||
order_payments.{{ payment_method }}_amount, | ||
|
||
{% endfor -%} | ||
|
||
order_payments.total_amount as amount | ||
|
||
from orders | ||
|
||
|
||
left join order_payments | ||
on orders.order_id = order_payments.order_id | ||
|
||
) | ||
|
||
select * from final |
Oops, something went wrong.