ふくふくHukuhuku Inc.
EP.09dbt 10分公開: 2026-05-10

packages:dbt_utils と dbt-expectations を使い倒す

dbt_utils は標準装備したい SQL ヘルパー集、dbt-expectations は Great Expectations 風の検証群。よく使う関数を 15 個ピックアップして実例で。

#dbt#packages#dbt_utils#dbt-expectations
シェア

エコシステムには 再利用可能な package が豊富にあり、packages.yml に書いて `dbt deps` で取得します。本記事では現場で確実に使う 2 つ ── dbt_utilsdbt-expectations を中心に。

packages.yml の書き方

packages.yml
YAML
packages:  - package: dbt-labs/dbt_utils    version: [">=1.1.0", "<2.0.0"]
  - package: calogica/dbt_expectations    version: [">=0.10.0", "<1.0.0"]
  - package: dbt-labs/codegen    version: [">=0.12.0", "<1.0.0"]   # YAML 雛形自動生成
  - package: dbt-labs/dbt_external_tables    version: [">=0.9.0"]              # 外部テーブル管理
取得
Bash
dbt deps    # packages.yml に基づき dbt_packages/ にダウンロード

dbt_utils の頻出 15 個

関数用途
`generate_surrogate_key`複数カラムから代替キー生成`dbt_utils.generate_surrogate_key(['id','date'])`
`star`テーブルの全カラムを except で選択`SELECT dbt_utils.star(ref('orders'), except=['_internal'])`
`union_relations`複数テーブルを UNION ALL`dbt_utils.union_relations([ref('a'), ref('b')])`
`pivot`動的 pivotEP.08 参照
`unpivot`wide → long`dbt_utils.unpivot(...)`
`get_column_values`ユニーク値取得`{% set vals = dbt_utils.get_column_values(...) %}`
`group_by`GROUP BY 1,2,3 を簡潔に`dbt_utils.group_by(3)`
`safe_divide`0 除算回避`dbt_utils.safe_divide('a','b')`
`date_spine`日付の連続テーブル`dbt_utils.date_spine(...)`
`type_string`アダプタ非依存の型`dbt.type_string()`
`generate_series`整数の連続`dbt_utils.generate_series(100)`
`deduplicate`重複排除 (keep=latest)`dbt_utils.deduplicate(...)`
`get_relations_by_pattern`テーブル名パターンで取得ワイルドカードunion
`width_bucket`数値ビニングヒストグラム集計
`haversine_distance`緯度経度から距離店舗距離計算

dbt_utils.star ── SELECT * を安全に

SELECT * の代替
SQL
-- 危険: 上流のスキーマ変更が直撃SELECT * FROM {{ ref('stg_orders') }}
-- 安全: 内部カラムを除外SELECT  {{ dbt_utils.star(      ref('stg_orders'),      except=['_loaded_at', '_internal_flag']  ) }}FROM {{ ref('stg_orders') }}
-- prefix 付与SELECT  {{ dbt_utils.star(ref('stg_customers'), relation_alias='c', prefix='cust_') }}FROM {{ ref('stg_customers') }} c

dbt_utils.surrogate_key ── 主キー生成

複合主キーから代替キー
SQL
SELECT  {{ dbt_utils.generate_surrogate_key(['date', 'customer_id', 'product_id']) }} AS sales_id,  date,  customer_id,  product_id,  amountFROM {{ ref('stg_sales') }}-- → sales_id は 32 文字の hash (md5)

dbt-expectations ── 30+ の検証

schema.yml で使える代表 10 個
YAML
tests:  # 範囲  - dbt_expectations.expect_column_values_to_be_between:      min_value: 0      max_value: 100000
  # 行数  - dbt_expectations.expect_table_row_count_to_be_between:      min_value: 1000
  # ユニーク数  - dbt_expectations.expect_column_distinct_count_to_be_between:      min_value: 100      max_value: 1000000
  # 値が他テーブルに存在  - dbt_expectations.expect_column_values_to_be_in_set:      value_set: ['JP', 'US', 'GB']
  # 平均が範囲内  - dbt_expectations.expect_column_mean_to_be_between:      min_value: 1000      max_value: 50000
  # 標準偏差が範囲内  - dbt_expectations.expect_column_stdev_to_be_between:      min_value: 0      max_value: 5000
  # 正規表現マッチ  - dbt_expectations.expect_column_values_to_match_regex:      regex: '^[A-Z]{2}-\d{4}$'
  # 日付が単調増加  - dbt_expectations.expect_column_values_to_be_increasing:      sort_column: ordered_at
  # 別テーブルとの行数比較  - dbt_expectations.expect_table_row_count_to_equal_other_table:      compare_model: ref('fct_orders_legacy')
  # 集計値の比較  - dbt_expectations.expect_column_sum_to_be_between:      min_value: 1000000

codegen ── を自動生成

新ソースの schema.yml 雛形を生成
Bash
# 新しい source テーブルから schema.yml を生成dbt run-operation generate_source --args '{  schema_name: stripe,  database_name: raw}'# → コピペ用の YAML が出力される
# model から schema.yml 雛形dbt run-operation generate_model_yaml --args '{  model_names: [dim_customers, fct_orders]}'

業界別の便利パッケージ

パッケージ用途
`dbt-labs/segment`Segment イベント整形
`fivetran/stripe`Stripe 標準モデル
`fivetran/salesforce`Salesforce ELT 標準モデル
`dbt-labs/snowplow`Snowplow イベント解析
`dbt-audit-helper`model 移行時の差分検証
`dbt-osmosis`schema.yml の自動更新

次の話

EP.10 では seeds (静的データ) と snapshots (SCD Type 2) を扱います。

シェア

この記事の感想を教えてください

あなたの 1 クリックで、本当にこの記事は更新されます。「もっと詳しく」「続編希望」が一定数集まった記事は、 ふくふくが 実際に内容を拡充したり続編記事を公開 します。 送信したリアクションはお使いのブラウザに記録され、再カウントされません。

シリーズの外も探す:

まずは、現状を聞かせてください。

要件が固まっていなくて大丈夫です。現状診断と方針提案までを無料でお手伝いします。

無料相談フォームへ hello [at] hukuhuku [dot] co [dot] jp