You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
create or replace function `fn.ts_conv`(ts timestamp)
as (
struct(
ts as raw
, struct(
string(ts, 'UTC') as pretty
, struct(
timestamp_trunc(ts, hour, 'UTC') as hour
, timestamp_trunc(ts, day, 'UTC') as day
, timestamp_trunc(ts, week, 'UTC') as week
, timestamp_trunc(ts, month, 'UTC') as month
) as trunc
, struct(
extract(hour from ts at time zone 'UTC') as hour
, extract(day from ts at time zone 'UTC') as day
, extract(week from ts at time zone 'UTC') as week
, extract(month from ts at time zone 'UTC') as month
) as part
) as utc
, struct(
string(ts, 'Asia/Tokyo') as pretty
struct(
timestamp_trunc(ts, hour, 'Asia/Tokyo') as hour
, timestamp_trunc(ts, day, 'Asia/Tokyo') as day
, timestamp_trunc(ts, week, 'Asia/Tokyo') as week
, timestamp_trunc(ts, month, 'Asia/Tokyo') as month
) as trunc
, struct(
extract(hour from ts at time zone 'Asia/Tokyo') as hour
, extract(day from ts at time zone 'Asia/Tokyo') as day
, extract(week from ts at time zone 'Asia/Tokyo' ) as week
, extract(month from ts at time zone 'Asia/Tokyo') as month
) as part
) as jst
)
);
-- 構造体のアクセス宣言のみで時間の変換が可能になるselect ts_conv(current_timestamp()).jst.trunc.hour
;
set @@time_zone ='Asia/Tokyo';
create or replace table `sandbox.test_partition`-- DATE(d) の日付はUTCを基準に計算される
partition by DATE(d)
asselect d from unnest(
generate_timestamp_array(
timestamp('2022-08-24', 'UTC')
, timestamp('2022-08-25', 'UTC')
, interval 3 hour
)
) as d
BigQueryにおけるタイムゾーンの取り扱い
BigQueryにおけるタイムゾーンの取り扱いは問題を起こしやすい。
特にDATE型やdatetime型に丸め込まれた場合に問題が発生しやすい。
CURRENT_DATE()
やCURRENT_DATETIME()
で得られる日付はデフォルトではUTC
である*1。また 日付パーティションもtimestampからの変換を行っている場合はデフォルトでUTCとなる。
特定のタイムゾーンでの日付を得るためには明示的にタイムゾーンを与えなければならない
BigQuery コンソール上の timestampの表記の注意点
注意点としてBigQueryのコンソール画面における timestamp型の表記は
デフォルトではUTCタイムゾーンとして表示される.
JSTといったタイムゾーンの変換を行ったとしてもコンソール上はUTCタイムゾーンを基準に表示される。
先ほどのクエリ結果は、コンソール上では次のように表示される。
BigQuery でのタイムゾーンを含む取り扱いのプラクティス
DWHとのレイヤは timestamp型を保持する
BigQueryでタイムゾーンが型レベルで取り扱えるのは timestamp型 のみである。
データマートの作成などデータソースとしての役割を担うDWHレイヤにおいてはtimestamp型で保持することが望ましい
利用者向けに ts変換用のUDFを用意しておく
時刻の変換用はミスが出やすいため関数を用意しておくと、利用に便利である
@@time_zone によるデフォルトのタイムゾーンの変換
BigQuery Scriptの機能を利用し、 デフォルトのタイムゾーンの変換が可能である (Ref. https://cloud.google.com/bigquery/docs/reference/system-variables)
ただし、上記の設定はテーブル作成の際のパーティション計算には及ばない点は注意である
@@time_zoneを変更したとしても日付パーティションの作成の際に、timestampをDATEに変換したとしても
DATEはUTCを基準に計算される.
Orgnization/Projectごとのデフォルトタイムゾーンの設定 (from twitter:@yutah_3)
BigQueryではタイムゾーン情報は、プロジェクトもしくは組織レベルでのデフォルト値を設定することができる。
この場合ユーザによるBigQuery Script毎の設定は不要となる。
この現在の設定は INFORMATION_SCHEMAから確認できる
詳細は公式ドキュメントを参照するとよい。
https://cloud.google.com/bigquery/docs/default-configuration#default_configurations
References
*1. Timestamp Function | How time zones work with timestamp functions
The text was updated successfully, but these errors were encountered: