Open7

dbt

YuichiYuichi

Wizard for dbt Core (TM)で「列、テーブル、参照の自動補完」機能が全く効かない症状

現象

  • analysesのフォルダでは何故か効かないことが特定できた
    • analysesはdbt runの対象外になるフォルダ
  • modelsフォルダでは、問題なく動作する

対応

  • ephemeral(cte)のフォルダーを作成して、そこでanalysesで作業するファイルを格納する
# dbt_project.yml

models:
  dbt_pj:
 
    work_dbt:
      materialized: ephemeral
      +schema: work_dbt

https://marketplace.visualstudio.com/items?itemName=Fivetran.dbt-language-server

YuichiYuichi

not found in dbt project(sqlfluff)のエラー

dbt テンプレートが機能するには、呼び出し元のディレクトリがsqlfluffdbt プロジェクトのルート ディレクトリである必要がある

  • .sqlfluffをルートディレクトリに移動して解決

https://github.com/sqlfluff/sqlfluff/issues/597

YuichiYuichi

ショートカット効率化
クエリ実行→ドキュメントフォーマット→ファイル保存の一括実行
multi-commandを利用

// keybindings.json
[
    {
        "key": "shift+enter",
        "command": "extension.multiCommand.execute",
        "args": { 
            "sequence": [
                "dbtPowerUser.executeSQL",
                "editor.action.formatDocument",
                "workbench.action.files.save",
            ],
        }
    }
]

https://marketplace.visualstudio.com/items?itemName=ryuta46.multi-command
https://qiita.com/mh326/items/6e2c3cf262cc1dfccdbe

YuichiYuichi

dbt test

  • dbtでエラー内容確認
    --store-failures フラグ
 dbt test --select モデル名 --store-failures   

https://zenn.dev/foursue/books/31456a86de5bb4/viewer/5efa91

テスト内容確認クエリ

-- テストしない方をコメントアウトする
{# {% set check_flag = "not_null" %} #} 
{% set check_flag = "unique" %}

{% set table = "table" %}
{% set column = "id" %}

SELECT *
FROM {{ ref(table) }}

{%- if check_flag == "not_null" %}
  WHERE {{ column }} IS NULL
{% elif check_flag == "unique" %}
  WHERE {{ column }} IN (
    SELECT {{ column }}
    FROM {{ ref(table) }}
    GROUP BY {{ column }}
    HAVING COUNT({{ column }}) > 1
  )
{% endif -%}
ORDER BY {{ column }} ASC
YuichiYuichi

dbt-osmosis

インストール

結局pip installした、packages.ymlに入れてインストールしても出来なかった。

dbt-osmosis
pip install -r requirements.txt 

yml作成コマンド

  • 一回、dbt build しないとファイルは作成されるが、カラムリストが生成されない
dbt-osmosis yaml refactor ./models/~フォルダ名
models:
  jaffle_shop:
      +dbt-osmosis: "{model}.yml" # _{model}.ymlとアンダーバー前に入れるよりファイルの並び的に良い
      materialized: table
      staging:
        materialized: view

https://zenn.dev/kyami/articles/d247a3b45f7044

BigQueryのコンソール上でdescriptionを参照できるようにする

models:
  +persist_docs:
    relation: true
    columns: true

relation を true にすると、テーブルのdescriptionが反映され、 columns を true にするとカラムのdescriptionが反映されます。
https://tech.high-link.co.jp/entry/dbt-data-management
https://www.yasuhisay.info/entry/2023/04/08/151748