Variableの参照の優先順位ってどうなっているの?
dbtを開発していれば自然とVariableを使うと思います。
しかしこのVariable、便利なのですがdbt_project.yml
で定義したり、コマンドラインの引数として渡せたりで、もし複数の方法で定義して競合したときどうなるのでしょうか?
まあ、公式ドキュメントに書いてあると言われればその通りなのですが、初見ではどこにあるのか見つけるのに少し手間取ったのでこの備忘録を書いた次第です。
上のLinkに以下のようにあります:
The order of precedence for variable declaration is as follows (highest priority first):
- The variables defined on the command line with --vars.
- The package-scoped variable declaration in the root dbt_project.yml file
- The global variable declaration in the root dbt_project.yml file
- If this node is defined in a package: variable declarations in that package's dbt_project.yml file
- The variable's default argument (if one is provided)
profiles.yml
の参照の優先順位ってどうなっているの?
上のLinkに以下のようにあります:
The parent directory for profiles.yml is determined using the following precedence:
- --profiles-dir option
- DBT_PROFILES_DIR environment variable
- current working directory
- ~/.dbt/ directory
model間でref()
で参照していないけれど、強制的に依存関係を定義したい
よかろう:
To address this, you can use a SQL comment along with the ref function — dbt will understand the dependency, and the compiled query will still be valid:
-- depends_on: {{ ref('upstream_parent_model') }} {{ your_macro('variable') }}
なんとコメントでdepends_on: {{ ref('upstream_parent_model') }}
と書いておけば勝手に認識してくれるみたいです、便利!