I created this logic to generate dates based on a Chron Pattern. Useful for when you have specific date patterns you want to pull events from to process on a continual basis in a pipeline.
CREATE OR REPLACE FUNCTION GENERATE_CHRON_PATTERNED_TIMESTAMPS (CHRON_PATTERN STRING, DURATION NUMBER)
RETURNS TABLE ( CHRON_START_TIMESTAMP TIMESTAMP
, CHRON_END_TIMESTAMP TIMESTAMP
, CHRON_PATTERN STRING
, MIN_PATTERN STRING
, HOUR_PATTERN STRING
, DAY_PATTERN STRING
, MON_PATTERN STRING
, WDAY_PATTERN STRING
, YEAR_PATTERN STRING
, MIN STRING
, HOUR STRING
, DAY STRING
, MON STRING
, WDAY STRING
, YEAR STRING
)
AS
$$
WITH PARSE_CHRON_PATTERN AS (SELECT CHRON_PATTERN as chron_pattern
, '[0-9*-\.\/]+' as parse_rule
, REGEXP_SUBSTR(chron_pattern,parse_rule,1,1) min_pattern
, REGEXP_SUBSTR(chron_pattern,parse_rule,1,2) hour_pattern
, REGEXP_SUBSTR(chron_pattern,parse_rule,1,3) day_pattern
, REGEXP_SUBSTR(chron_pattern,parse_rule,1,4) mon_pattern
, REGEXP_SUBSTR(chron_pattern,parse_rule,1,5) wday_pattern
, COALESCE(REGEXP_SUBSTR(chron_pattern,parse_rule,1,6), '*') year_pattern
, REGEXP_SUBSTR(chron_pattern,parse_rule,1,1) min_pattern
, REGEXP_SUBSTR(chron_pattern,parse_rule,1,2) hour_pattern
, REGEXP_SUBSTR(chron_pattern,parse_rule,1,3) day_pattern
, REGEXP_SUBSTR(chron_pattern,parse_rule,1,4) mon_pattern
, REGEXP_SUBSTR(chron_pattern,parse_rule,1,5) wday_pattern
, COALESCE(REGEXP_SUBSTR(chron_pattern,parse_rule,1,6), '*') year_pattern
No comments:
Post a Comment