0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

dbt-osmosisで遊んでみる

Last updated at Posted at 2025-02-14

dbt-osmosisとは

dbt-osmosisとは、上流テーブルのカラムのdescriptionやデータ型を下流へ継承(※)してくれるツールです。
本記事では、dbt-osmosisのインストールから挙動の検証を行います。

継承

ドキュメントより抜粋

dbt-osmosisの最も強力な機能の 1 つは、複数レベルの列ドキュメントの継承です。つまり、列に関連するすべてのタグ、description、メタフィールドを、任意の上流ノードから現在のモデルにカスケードできます。

動作環境

DBはpostgresqlを利用しています。

$ psql -V
psql (PostgreSQL) 14.15 (Ubuntu 14.15-0ubuntu0.22.04.1)

$ dbt -V
Core:
  - installed: 1.9.2

Plugins:
  - postgres: 1.9.0 - Up to date!

$ dbt-osmosis --version
dbt-osmosis, version 1.1.13

ディレクトリ構造

過去の記事で作成したプロジェクトをもとにdbt-osmosisの検証を行っていきます。

.
├── dbt_project.yml
├── models
│   ├── intermediates
│   │   └── marketing
│   │       ├── _int_marketing__models.yml
│   │       ├── int_dim_customers.sql
│   │       ├── int_dim_dates.sql
│   │       ├── int_dim_products.sql
│   │       └── int_fact_sales.sql
│   ├── marts
│   │   └── marketing
│   │       ├── _marketing__models.yml
│   │       └── obt_sales.sql
│   └── staging
│       └── adventureworks
│           ├── _adventureworks__models.yml
│           ├── _adventureworks__sources.yml
│           ├── stg_adventureworks__stg_addresses.sql
│           ├── stg_adventureworks__stg_customers.sql
│           ├── stg_adventureworks__stg_dates.sql
│           ├── stg_adventureworks__stg_order_details.sql
│           ├── stg_adventureworks__stg_order_headers.sql
│           └── stg_adventureworks__stg_products.sql
├── seeds
│   └── adventureworks
│       ├── _adventureworks__seed.yml
│       ├── raw_addresses.csv
│       ├── raw_customers.csv
│       ├── raw_dates.csv
│       ├── raw_order_details.csv
│       ├── raw_order_headers.csv
│       └── raw_products.csv

パッケージのインストールと設定

コマンド1つでインストールができます!

pip install dbt-osmosis

続いて、dbt_project.ymlにdbt-osmosisの設定を追加します。

dbt_project.yml
models:
  dbt_modeling: # プロジェクト名
    staging: # サブディレクトリ名
      adventureworks:
        +materialized: view
        +dbt-osmosis: "_adventureworks__models.yml" # "_{model}.yml"
        +schema: stg_schema
    intermediates:
      marketing:
        +materialized: view
        +dbt-osmosis: "_int_marketing__models.yml"
        +schema: int_schema
    marts:
      marketing:
        +materialized: table
        +dbt-osmosis: "_marketing__models.yml"
        +schema: mart_schema

seedsも同様の記法で設定できます。

seeds:
  dbt_modeling:
    adventureworks: # サブディレクトリ名
      +dbt-osmosis: "_adventureworks__seed.yml" 

sources

オプションでsourcesdbt-osmosisの配下に加えられます。

vars:
  dbt-osmosis:
    sources:
      src_adventureworks: # sources.name属性と一致させる
        path: "staging/adventureworks/_adventureworks__sources.yml"

sourcesdbt-osmosis管理配下に追加する方法に関するはこちらです。

dbt-osmosisの挙動を確認する

挙動を確認してみます!

$ dbt-osmosis yaml refactor

before

_int_marketing__models.yaml
version: 2
models:
  - name: int_dim_dates
    columns:
      - name: date_key
      - name: date
      - name: day_of_week
      - name: month
      - name: year

after

sourcesで定義した、data_typedescriptionが伝播していることがわかります!

# _int_marketing__models.yaml
version: 2
models:
  - name: int_dim_dates
    columns:
      - name: date_key
        # models.sql内でcastしたデータ型が反映されている
+       data_type: bigint
      - name: date
+       data_type: date
        # sourcesレイヤ(中間層よりも上流)で定義したdescriptionが継承されている
+       description: add_description_from_sources:The date of the order 
      - name: day_of_week
+       data_type: character varying
      - name: month
+       data_type: integer
      - name: year
+       data_type: integer

動かしながらわかってきたこと

  • モデル内で明示的にCASTすることで、そのデータ型がschema.ymlに反映されます
    • この際、dbt runを事前に実行し、モデルを実体化しておく必要があります
with stg_dates as (
    select 
        date
        , day_of_week
        , month::smallint
        , year
    from {{ source('public','raw_dates') }}
)
select * from stg_dates
version: 2
models:
  - name: stg_jaffle_shop__stg_dates
    columns:
      - name: month
-        data_type: integer
+        data_type: smallint
    ...
  • sedds.yml内でカラムのメタデータとしてdescriptionを定義しても、下流のsources.ymlへ伝播されない??
    • これはどうやってもうまくいきませんでした。検証の仕方がダメなのか。。。

  • 下記ドキュメントの通りDB側でdescriptionを追加してdbt-osmosisを実行してみましたが、sources.yml内で自動で生成されることはありませんでした。
    • Postgresql以外のDBエンジンではうまくいかも?

ソース yaml ファイルはデータベースから生成されます。スキーマ yaml ファイルは、継承されたメタデータを使用して dbt モデルから生成されます。

  • sedds.ymlsources.yml内ではcolumnsの属性としてdata_typeは存在しないが、推測されたデータ型が付与される
    • 存在しないプロパティが自動で付与されるのは挙動的に気持ち悪い...
version: 2
sources:
  - name: public
    tables:
      - name: raw_dates
        columns:
          - name: date
            description: add_description_from_sources:The date of the order
+           data_type: date
          - name: day_of_week
+           data_type: character
          - name: month
+           data_type: integer
          - name: year
+           data_type: integer
  • model contractsの補完と伝播
    • typeを入力しておくだけで、それ以外のプロパティを勝手に補完してくれる
    • ただし下流のモデルにconstraintsは伝播しない
# _jaffle_shop__models.yml
version: 2
models:
  - name: stg_jaffle_shop__stg_dates
    config:
      constract:
        enforced: true
    columns:
      - name: date
        description: add_description_from_sources:The date of the order
        data_type: date
        constraints:
          - type: not_null
           # typeを入力しておくだけで、それ以外のプロパティを勝手に補完してくれる
+           name:
+           expression:
+           warn_unenforced: true
+           warn_unsupported: true
+           to:
+           to_columns: []
# _int_marketing__models.yml
version: 2
models:
  - name: int_dim_dates
    config:
      constract:
        enforced: true
    columns:
      - name: date_key
        data_type: smallint
      - name: date
        description: add_description_from_sources:The date of the order
        data_type: date
        # 下流のモデルにconstraintsは伝播しない

最後に

まだまだ勉強途中のため内容に誤りがあれば申し訳ありません。ご指摘いただけますと幸いです。

扱いが難しい挙動はあるものの、メタデータ管理を楽にする強力なツールだと思いました!

検証しきれていないコマンドやオプションがまだまだあるので、それらについても調べて記事を更新していきたいと思います。

ドキュメント

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?