# Today I learned about dbt
- dbt `ref` function ကို macro script ထဲမှာ ခေါ်သုံးမရ/မရ။
- မှားပါတယ်။ ခေါ်သုံးလို့ ရတယ်။
- dbt dispatch
- dbt `on-run-end` hook က Error တက်ခဲ့ရင်တောင် ဆက် run လိမ့်မယ်။ `--fail-fast` သုံးလဲ `on-run-end` hook က trigger ဖြစ်ဦးမှာပဲ။
- dbt docs အတွက် markdown ထဲမှာ ``` {% docs %} ``` ရေးတဲ့အခါမှာ code block တွေ ထည့်ရေးရင် သတိထားပါ။ အဆုံးသတ်နားမှာ ထည့်ရေးရင် compile လုပ်တဲ့အခါမှာ error ဖြစ်တတ်တာ ထင်တယ်။ ဒါမှမဟုတ် code block မှာ sql / cmd ဘာညာ မထည့်သင့်တာမျိုး ဖြစ်မယ်။
- `dbt parse` - Parsing step က warehouse နဲ့ မချိတ်ဆက်ဘူး။ ဒါကြောင့် generated လုပ်လိုက်တဲ့ Manifest (`perf_info.json`) file မှာ ဘာ compiled code မှ ပါမှာမဟုတ်ဘူး။
- Default အရ partial parse က ON ထားတယ်။ ဆိုလိုတာက နောက်ဆုံး parse ရလာဒ်နဲ့ နှိုင်းယှဥ်ပြီး အသစ်/ပြောင်းလဲလိုက်တဲ့ အပိုင်း ကိုပဲ check လုပ်မှာ။ အဲဒီ behavior ကို ဖျောက်ချင်ရင် `--no-partial-parse` flag ကို command line မှာ သုံးလို့ ရတယ်။
- dbt `tag` တွေကို project-level `dbt-project.yml` ထဲမှာ ကြေညာလို့ ရသလို model-level `<model_name>.sql` ရဲ့ config block ထဲမှာလဲ ကြေညာလို့ ရတယ်။ အဲဒီလို ၂ နေရာ ကြေညာပေမယ့် dbt က ထပ်နေတဲ့ tag တွေကို merge လုပ်ပေးမှာ မဟုတ်ဘူး။ model-level ကြေညာထားတဲ့ tag က project-level tag ကို override လုပ်သွားမှာ။
- dbt version တွေကို upgrade/downgrade အပြောင်းအလဲ လုပ်တဲ့အခါမှာ သုံးလိုက်စ virtual environment တစ်ခုလုံးကို delete လုပ်ပြီး အသစ် ပြန်ဆောက်ပြီး install လုပ်တာ အကောင်းဆုံးပဲ။ မဟုတ်ရင် dbt version တစ်ခု နဲ့ တခု ကြားမှာ dependency lib တွေကနေ error တမျိုးပြီး တမျိုး တက်တယ်။
```
rm -rf .venv
python -m venv .venv
pip install dbt-core=={version} dbt-duckdb
```
- dbt command line မှာ `--profile` + `--target` ကြေညာခဲ့ရင် ဒီ log မထွက်ပဲနဲ့ မကြေညာခဲ့ရင် ဒီ log ထွက်တယ်။ ဆိုလိုတာက partial parsing နဲ့ အဲဒီ argument ၂ ခု နဲ့ သက်ဆိုင်နေတာ။
![[Pasted image 20251005025735.png]]
- `dbt run + dbt test` command ၂ ခု ကို သုံးရင် - [Ref](https://learn.getdbt.com/learn/course/advanced-deployment/job-structures-45min/understanding-job-structures?page=3)
- models တွေအားလုံးကို အရင် materialize လုပ်တယ်။ ပြီးမှ models တွေကို test တယ်။ snapshots နဲ့ seeds တွေ မပါဘူး။ command ၂ ခါ ရေးရတယ်။ (အကုန်လုံးကို run ပြီးမှ test တာ ဖြစ်တဲ့အတွက် error ဖြစ်ခဲ့ရင်လဲ အားလုံး run ပြီးမှ သိရမယ်။ cost saving မဖြစ်)
- `dbt build` ကို သုံးရင် - (same ref)
- ပိုပြီး smart ဖြစ်တယ်။ DAG order အတိုင်း nodes တွေအားလုံးကို run တယ် test တယ်။ snapshots နဲ့ seed တွေကို ပါ run တယ်။
- test failure တခု ပေါ် မူတည်ပြီး downstream မှာ ရှိနေတဲ့ nodes တွေကို skip လုပ်သွားတယ်။ (မလိုအပ်တဲ့ cost တွေကို save နိုင်တယ်။)
- `dbt build` command နဲ့ ဆိုရင် seed အပါအဝင် dependency အကုန်လုံးကို run တယ်လို့ ယေဘုယျ ပြောလို့ ရတယ်။
- ဒါပေမယ့် အကယ်ရွေ့ seed အတွက် custom schema တစ်ခု သပ်သပ် ပေးထားမယ်၊ အဲဒီ schema ကလဲ BigQuery dataset အနေနဲ့ မရှိသေးဘူးဆိုရင် Runtine Error `Dataset ... was not found in location US` ဆိုပြီးတော့ တက်တယ်။
- US လို့ ဘာကြောင့် ပြောတာလဲ ဆိုတော့ BigQuery က Default US multi-region ထားထားလို့ ။ [Ref](https://docs.getdbt.com/guides/bigquery?step=3#create-bigquery-datasets) Region သတ်မှတ်ချင်ရင်တော့ [[4.dbt_YAML_configs#profiles.yml|profiles.yml]] ထဲက bigquery connection အတွက် `location` config ကြေညာပေးရမယ်။
- `dbt seed` command နဲ့ သီးခြား run ရင်ကျ schema (BQ dataset) ရှိမနေဘူးဆိုရင် auto create လုပ်ပေးနိုင်တယ်။ Error မတက်ဘူး။
- dbt `Analyses` က `one off queries` တွေလို့ ပြောနိုင်တယ်။
- jinja support လုပ်တယ်။ dbt compile နဲ့ လဲ သုံးလို့ ရတယ်။ ဒါပေမယ့် သူ့ကို run လို့ ဘာမှ build လုပ်မှာမဟုတ်ဘူး။
- အသုံးဝင်တဲ့ usage ကတော့ တခါတလေ သုံးရတဲ့ user create လုပ်တဲ့ query script မျိုးတွေ၊ DW test run စမ်းထားတဲ့ query script လိုမျိုးတွေ ၊ query practice လုပ်ကြည့်တာမျိုးတွေ ကို version control နဲ့ တွဲသုံးဖို့အတွက် သုံးလို့ ရတယ်။
- dbt `selector` က `selectors.yml` နဲ့ တွဲသုံးတယ်။ [Best Example](https://gist.github.com/jeremyyeo/1aeca767e2a4f157b07955d58f8078f7) for `--select` vs `--selector`
```yml
# selectors.yml
selectors:
- name: only_my_tag
definition:
tag: my_tag
```
- dbt Set Operators
- space က AND သဘောတရား (Union)
- Eg - Run ALL (snowplow_sessions + all ancestors of snowplow_sessions) AND (fct_orders + all ancestors of fct_orders)
- `dbt run --select "+snowplow_sessions +fct_orders"`
- Another Example - ![[Pasted image 20251005111655.png]]
- comma က Intersections (Common)
- Run all the common descendents of stg_invoices and stg_accounts:
- `dbt run --select "stg_invoices+,stg_accounts+"`
- Another Example : common parent ဖြစ်တဲ့ `stg_stripe_payments` အပိုင်း ကိုပဲ run
- img -![[Pasted image 20251005111859.png]]