forked from GoogleCloudPlatform/bigquery-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts_gen_keyed_timestamps.sqlx
63 lines (59 loc) · 2.66 KB
/
ts_gen_keyed_timestamps.sqlx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
config { hasOutput: true }
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Generate an array of key-timestamp structs with the specified min, max and interval timeseries
* Example Usage:
* SELECT *
* FROM UNNEST(bqutil.fn.ts_gen_keyed_timestamp(['abc'], 900, '2020-01-01', '2020-01-02') a
* LEFT JOIN dataset.table ON a.series_key = a.key AND a.tumble_val = b.timestamp
*/
-- ts_gen_keyed_timestamps:
-- Input:
-- keys: strings that are cross joined with the generated timestamps
-- tumble_seconds: the windowing interval for each generated timestamp
-- min_ts: the inclusive lower bound for the generated timestamps, normalized by the tumble_seconds
-- max_ts: the inclusive upper bound for the generated timestamps, normalized by the tumble_seconds
-- Output: An array of generated timestamps for each key - ARRAY<STRUCT<series_key STRING, tumble_seconds TIMESTAMP>>
CREATE OR REPLACE FUNCTION ${self()}(keys ARRAY<STRING>, tumble_seconds INT64, min_ts TIMESTAMP, max_ts Timestamp)
RETURNS ARRAY<STRUCT<series_key STRING, tumble_val TIMESTAMP>>
OPTIONS (
description="""Generate an array of key-timestamp structs with the specified min, max and interval timeseries
Example Usage:
SELECT *
FROM UNNEST(bqutil.fn.ts_gen_keyed_timestamp(['abc'], 900, '2020-01-01', '2020-01-02') a
LEFT JOIN dataset.table ON a.series_key = a.key AND a.tumble_val = b.timestamp
Inputs:
keys: strings that are cross joined with the generated timestamps
tumble_seconds: the windowing interval for each generated timestamp
min_ts: the inclusive lower bound for the generated timestamps, normalized by the tumble_seconds
max_ts: the inclusive upper bound for the generated timestamps, normalized by the tumble_seconds
Output: An array of generated timestamps for each key - ARRAY<STRUCT<series_key STRING, tumble_seconds TIMESTAMP>>"""
)
AS ((
SELECT ARRAY_AGG(x)
FROM (
SELECT series_key, tumble_val
FROM UNNEST(
GENERATE_TIMESTAMP_ARRAY(
${ref("ts_tumble")}(min_ts, tumble_seconds),
${ref("ts_tumble")}(max_ts, tumble_seconds),
INTERVAL tumble_seconds SECOND
)
) AS tumble_val
CROSS JOIN UNNEST(keys) AS series_key
) x
));