テーブルに新しいデータが入った時に品質テストを行い異常があったら通知するアラートを作ってみた
さがらです。
テーブルに新しいデータが入った時に品質テストを行い異常があったら通知するアラートを作ってみたので、その内容をまとめてみます。
前提知識:テーブルに新しいデータが入った時のアラート機能
これは2025年3月にパブリックプレビューでリリースされた新しいアラートの機能になります。
この機能の実例として、先日タスクとDynamic Tableのエラー通知に用いたブログも執筆しております。
事前準備
EメールのNotification Integrationの作成
今回はアラートの条件文に合致するときにEメールを送信して通知したいため、以下のクエリを実行してEメールのNotification Integrationを作成しておきます。
-- メール送信用のNotification Integration作成
use role accountadmin;
create or replace notification integration email_int
type = email
enabled = true;
grant usage on integration email_int to role sysadmin;
必要な権限の付与
今回はSYSADMINロールですべてのクエリを実行するため、事前に必要な権限を付与しておきます。
use role accountadmin;
-- サーバーレスアラート関係
grant execute managed alert on account to role sysadmin;
grant execute alert on account to role sysadmin;
データの準備
以下のクエリを実行して、ベースとなるデータの準備をしておきます。
-- ロールの設定
use role sysadmin;
-- データベースとスキーマの作成
create database if not exists dq_tutorial_db;
create schema if not exists dq_tutorial_db.sch;
-- 従業員テーブルの作成
create or replace table dq_tutorial_db.sch.employeestable (
id number,
name varchar,
last_name varchar,
email varchar,
zip_code number
);
-- 正常なデータを挿入
insert into dq_tutorial_db.sch.employeestable (id, name, last_name, email, zip_code)
values
(1, 'john', 'doe', 'johndoe@example.com', 12345),
(2, 'jane', 'smith', 'janesmith@example.com', 23456),
(3, 'robert', 'johnson', 'rjohnson@example.com', 34567);
-- データの確認
select * from dq_tutorial_db.sch.employeestable;
アラートの作成・有効化
以下のクエリを実行して、アラートを作成します。
-- アラートの作成
use role sysadmin;
create or replace alert dq_tutorial_db.sch.data_quality_alert
if (exists(
select 1
from dq_tutorial_db.sch.employeestable
where name = '' or email is null
))
then
begin
-- エラー詳細を取得
let error_details varchar;
-- 空白値のある行を取得
select 'blank name values found in the following rows:' || chr(10) ||
array_to_string(array_agg('id: ' || id || ', last name: ' || last_name), chr(10))
into :error_details
from dq_tutorial_db.sch.employeestable
where name = '';
-- null値のあるメールアドレスを追加
let null_email_details varchar;
select 'null email values found in the following rows:' || chr(10) ||
array_to_string(array_agg('id: ' || id || ', name: ' || name || ', last name: ' || last_name), chr(10))
into :null_email_details
from dq_tutorial_db.sch.employeestable
where email is null;
-- エラー情報を結合
let full_error_details varchar := :error_details || chr(10) || chr(10) || :null_email_details;
-- メール送信
call system$send_email(
'email_int',
'sagara.satoshi@classmethod.jp',
'data quality alert: issues detected in employee table',
'the following data quality issues were detected:' || chr(10) || chr(10) || :full_error_details ||
chr(10) || chr(10) || 'please review and fix these issues as soon as possible.'
);
end;
この後、以下のクエリを実行してアラートを有効化します。
-- アラートを有効化
use role sysadmin;
alter alert dq_tutorial_db.sch.data_quality_alert resume;
アラートの動作確認
以下のクエリを実行して、アラートが動作するレコードを挿入します。
-- 問題のあるデータを挿入
use role sysadmin;
insert into dq_tutorial_db.sch.employeestable (id, name, last_name, email, zip_code)
values
(4, '', 'williams', 'williams@example.com', 45678), -- 空の名前
(5, 'michael', 'brown', null, 56789); -- nullのメール
-- データの確認
select * from dq_tutorial_db.sch.employeestable;
このあと1分もしない内に、下記のメールが届きました。正しくデータ品質チェックが出来ていることがわかります!
実運用時に気をつけたいこと
今回は検証のためシンプルな構成で試しましたが、実際に運用する際には以下のことに気をつけなければいけないと思いました。
- アラートでの定義時、検出対象のレコードを直近1時間分や直近1日分などに絞り込むことを強く推奨します
- 例えばSnowpipeで1分おきにデータが入ってくるような用途だと、検出時間を絞っておかないと一度エラーを検出したら毎分アラートの通知が届いてくるようになります。
- サーバーレスアラートでもウェアハウス指定のアラートであっても、今回の検証内容では対象のテーブルにデータが入るたびにアラートに指定した条件クエリが動くため、コストを意識しましょう
- alert自体は頻度を指定してのスケジュール実行も可能のため、データ更新頻度や担保すべき品質レベルによってはスケジュール指定することを推奨します。
余談
本当はData Metric Functionsを用いたアラートを行おうと考えたのですが、新しいデータが入ったときのアラートについて2025年4月1日時点ではストアドプロシージャが対応しておらず、DMFsもこれに該当してエラーになってしまいました…
- 2025年4月1日時点の上記ドキュメントの記載
- 実際のクエリエラー