Skip to content

What is dbt_utils.star macro


อ่านบทความ Building a Kimball dimensional model with dbt แล้วเจอ macro ที่ไม่เคยใช้คือ dbt_utils.star เลยอยากเขียนแชร์ไว้

with f_sales as (
    select * from {{ ref('fct_sales') }}
),

d_customer as (
    select * from {{ ref('dim_customer') }}
),

d_credit_card as (
    select * from {{ ref('dim_credit_card') }}
),

d_address as (
    select * from {{ ref('dim_address') }}
),

d_order_status as (
    select * from {{ ref('dim_order_status') }}
),

d_product as (
    select * from {{ ref('dim_product') }}
),

d_date as (
    select * from {{ ref('dim_date') }}
)

select
    {{ dbt_utils.star(from=ref('fct_sales'), relation_alias='f_sales', except=[
        "product_key", "customer_key", "creditcard_key", "ship_address_key", "order_status_key", "order_date_key"
    ]) }},
    {{ dbt_utils.star(from=ref('dim_product'), relation_alias='d_product', except=["product_key"]) }},
    {{ dbt_utils.star(from=ref('dim_customer'), relation_alias='d_customer', except=["customer_key"]) }},
    {{ dbt_utils.star(from=ref('dim_credit_card'), relation_alias='d_credit_card', except=["creditcard_key"]) }},
    {{ dbt_utils.star(from=ref('dim_address'), relation_alias='d_address', except=["address_key"]) }},
    {{ dbt_utils.star(from=ref('dim_order_status'), relation_alias='d_order_status', except=["order_status_key"]) }},
    {{ dbt_utils.star(from=ref('dim_date'), relation_alias='d_date', except=["date_key"]) }}
from f_sales
left join d_product on f_sales.product_key = d_product.product_key
left join d_customer on f_sales.customer_key = d_customer.customer_key
left join d_credit_card on f_sales.creditcard_key = d_credit_card.creditcard_key
left join d_address on f_sales.ship_address_key = d_address.address_key
left join d_order_status on f_sales.order_status_key = d_order_status.order_status_key
left join d_date on f_sales.order_date_key = d_date.date_key

SQL ข้างบนเป็นตัวอย่างจากในบทความที่อ่าน ซึ่งผมยังไม่เคยใช้ macro นี้ เลยลองหาดูว่ามันคืออะไร และเกิดมาแก้ปัญหาอะไร

ในสถานการณ์ที่เราต้องเขียน SQL ในการ select column จาก table ที่มีหลาย column เช่น Table A มี 57 columns แล้วเราต้องการทุก columns ยกเว้น column_56 ปกติวิธีที่ผมทำก็คือเขียนในท่านี้ (เอาจริงๆ ตอนนี้ก็ใช้ AI ช่วยเขียนแหละ ไม่ได้เขียนเอง 55555)

select
	column_1,
	column_2,
	column_3,
	please_save_me…
from {{ ref('table_a') }}

ซึ่งเราสามารถเขียนใหม่ ให้ query เราอ่านง่ายขึ้นด้วยการใช้ dbt_utils.star macro

select
	{{ dbt_utils.star(from=ref('table_a'), except=['column_56']) }}
from {{ ref('table_a') }}

ซึ่งทำให้อ่านง่ายขึ้นเยอะมาก

แล้ว macro นี้มี arguments อะไรให้ใช้อีกบ้าง?

  • from: The relation (model, seed, or source) to inspect.

  • except: A list of column names to omit from the generated list.

  • prefix: Text added to the front of every column name.

  • suffix: Text added to the back of every column name.

  • relation_alias: Table alias prefix applied to the columns.

  • quote_identifiers: Boolean to determine whether to quote column names (default True).

  • unquote_aliases: Boolean to leave the prefix/suffix alias unquoted (default False).

  • rename: A dict mapping specific column names to a new name.


References