[ver1.5新機能]CLIから指定したModelや任意のSELECT文の出力結果を確認できる「show」コマンドを試してみた

2023.05.09

さがらです。

先日、dbt-coreのver1.5がリリースされました。

dbt-core ver1.5の新機能として、CLIから指定したModelや任意のSELECT文の出力結果を確認できる「show」コマンドが使えるようになりました。

こちらの機能を試してみたので本記事でまとめてみます。

showコマンドとは

まず、showコマンドについて簡単に説明します。

このドキュメントに書いてある概要を見ると下記のように書いてあり、model、test、analysisフォルダで定義されている.sqlファイルや、コマンドライン上でSQLを直接記述して実行ができるようです。

  • Compile the dbt-SQL definition of a model, test, analysis, or an arbitrary dbt-SQL query passed --inline
  • Run that query against the data warehouse
  • Preview the results in the terminal

dbt CloudだとIDE上にPreviewボタンがあるので使わなくても済む機能かもしれませんが、dbt-coreをお使いの方にはコマンドライン上でdbtで定義したModelがどういった結果を返すのかすぐわかるので、主にdbt-coreをお使いのユーザー向けの機能だと感じました。

試してみた

検証環境

以下の条件でBigQueryと接続して、dbt showコマンドを試してみます。

  • OS:Ubuntu 20.04(Windows10、WSL2上で実行)
  • dbt-bigquery:1.5.0
  • Python:3.11.3

使用する各種Modelは、dbtのチュートリアルでお馴染みのjaffle_shopリポジトリを使用します。

Modelを指定

Modelを指定する場合には、--selectオプションを用いてModelを指定すればOKです。

このModelは100行を返すのですが、デフォルトでは5行だけ返すようです。(この仕様は公式Docにも「By default, dbt show will display the first 5 rows from the query result.」記載されております。)

$ dbt show --select customers.sql
00:05:40  Running with dbt=1.5.0
00:05:40  Found 5 models, 20 tests, 0 snapshots, 0 analyses, 353 macros, 0 operations, 3 seed files, 0 sources, 0 exposures, 0 metrics, 0 groups
00:05:40
00:05:41  Concurrency: 4 threads (target='dev')
00:05:41
00:05:45  Previewing node 'customers':
| customer_id | first_name | last_name | first_order | most_recent_order | number_of_orders | ... |
| ----------- | ---------- | --------- | ----------- | ----------------- | ---------------- | --- |
|          20 | Anna       | A.        |  2018-01-23 |        2018-01-23 |                1 | ... |
|          23 | Mildred    | A.        |             |                   |                  | ... |
|          40 | Maria      | A.        |  2018-01-17 |        2018-01-17 |                1 | ... |
|          59 | Adam       | A.        |  2018-01-15 |        2018-01-15 |                1 | ... |
|          74 | Harry      | A.        |             |                   |                  | ... |

SQLを直接記述

SQLを直接記述する場合には、--inlineオプションを用いて、オプションの後にSELECT文のクエリを直接記述すればOKです。ref()などのJinja関数があっても問題ありません!

$ dbt show --inline "select * from {{ ref('customers') }} where customer_id = 20"
00:11:06  Running with dbt=1.5.0
00:11:06  Found 5 models, 20 tests, 0 snapshots, 0 analyses, 353 macros, 0 operations, 3 seed files, 0 sources, 0 exposures, 0 metrics, 0 groups
00:11:06
00:11:07  Concurrency: 4 threads (target='dev')
00:11:07
00:11:09  Previewing inline node:
| customer_id | first_name | last_name | first_order | most_recent_order | number_of_orders | ... |
| ----------- | ---------- | --------- | ----------- | ----------------- | ---------------- | --- |
|          20 | Anna       | A.        |  2018-01-23 |        2018-01-23 |                1 | ... |

出力する行数を変更

デフォルトでは5行返す仕様ですが、--limit nオプションを使用することで返す行数を変更することが出来ます。

先程Modelを指定したときのコマンドに--limit 10オプションを付けて10行返すようにしてみると、下記の様に返ってきました。

$ dbt show --select customers.sql --limit 10
00:17:07  Running with dbt=1.5.0
00:17:07  Found 5 models, 20 tests, 0 snapshots, 0 analyses, 353 macros, 0 operations, 3 seed files, 0 sources, 0 exposures, 0 metrics, 0 groups
00:17:07
00:17:08  Concurrency: 4 threads (target='dev')
00:17:08
00:17:12  Previewing node 'customers':
| customer_id | first_name | last_name | first_order | most_recent_order | number_of_orders | ... |
| ----------- | ---------- | --------- | ----------- | ----------------- | ---------------- | --- |
|          20 | Anna       | A.        |  2018-01-23 |        2018-01-23 |                1 | ... |
|          23 | Mildred    | A.        |             |                   |                  | ... |
|          40 | Maria      | A.        |  2018-01-17 |        2018-01-17 |                1 | ... |
|          59 | Adam       | A.        |  2018-01-15 |        2018-01-15 |                1 | ... |
|          74 | Harry      | A.        |             |                   |                  | ... |
|          96 | Jacqueline | A.        |             |                   |                  | ... |
|          27 | Benjamin   | B.        |  2018-02-21 |        2018-04-04 |                2 | ... |
|          45 | Scott      | B.        |             |                   |                  | ... |
|          53 | Anne       | B.        |  2018-01-12 |        2018-03-11 |                2 | ... |
|          73 | Alan       | B.        |             |                   |                  | ... |

最後に

簡単ではありますが、CLIから指定したModelや任意のSELECT文の出力結果を確認できる「show」コマンドを試してみました!

dbt-coreをお使いの方で、定義した各SQL文が返す結果をすぐ知りたい場合にはぜひお試しください。