Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SNOW-177]Convert teammember_latest to dynamic table #149

Merged
merged 3 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- Introduce the dynamic table
USE SCHEMA {{database_name}}.synapse; --noqa: JJ01,PRS,TMP

CREATE OR REPLACE DYNAMIC TABLE TEAMMEMBER_LATEST
(
CHANGE_TYPE VARCHAR(16777216) COMMENT 'The type of change that occurred to the member of team, e.g., CREATE, UPDATE (Snapshotting does not capture DELETE change).',
CHANGE_TIMESTAMP TIMESTAMP_NTZ(9) COMMENT 'The time when any change to the team was made (e.g. update of the team or a change to its members).',
CHANGE_USER_ID NUMBER(38,0) COMMENT 'The unique identifier of the user who made the change to the team member.',
SNAPSHOT_TIMESTAMP TIMESTAMP_NTZ(9) COMMENT 'The time when the snapshot was taken (It is usually after the change happened).',
TEAM_ID NUMBER(38,0) COMMENT 'The unique identifier of the team.',
MEMBER_ID NUMBER(38,0) COMMENT 'The unique identifier of the member of the team. The member is a Synapse user.',
IS_ADMIN BOOLEAN COMMENT 'If true, then the member is manager of the team.',
SNAPSHOT_DATE DATE COMMENT 'The data is partitioned for fast and cost effective queries. The snapshot_timestamp field is converted into a date and stored in the snapshot_date field for partitioning. The date should be used as a condition (WHERE CLAUSE) in the queries.'
)
TARGET_LAG = '1 day'
WAREHOUSE = compute_xsmall
COMMENT = 'This table contain snapshots of team-members during the past 14 days. Snapshots are captured when a team and/or its members are modified. Note: Snapshots are also taken periodically and independently of the changes. The snapshot_timestamp records when the snapshot was taken.'
AS
WITH dedup_teammembers AS (
SELECT
CHANGE_TYPE,
CHANGE_TIMESTAMP,
CHANGE_USER_ID,
SNAPSHOT_TIMESTAMP,
TEAM_ID,
MEMBER_ID,
IS_ADMIN,
SNAPSHOT_DATE
FROM {{database_name}}.SYNAPSE_RAW.teammembersnapshots --noqa: TMP
WHERE
SNAPSHOT_DATE >= CURRENT_TIMESTAMP - INTERVAL '14 days'
QUALIFY
ROW_NUMBER() OVER (
PARTITION BY TEAM_ID, MEMBER_ID
ORDER BY CHANGE_TIMESTAMP DESC, SNAPSHOT_TIMESTAMP DESC
) = 1
)
SELECT
*
FROM
dedup_teammembers;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Backup the original latest table
USE SCHEMA {{database_name}}.synapse; --noqa: JJ01,PRS,TMP

-- Clone the TEAMMEMBER_LATEST table to ``TEAMMEMBER_LATEST_BACKUP`` for validation purposes
CREATE OR REPLACE TABLE TEAMMEMBER_LATEST_BACKUP CLONE TEAMMEMBER_LATEST;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Drop TEAMMEMBER_LATEST table
USE SCHEMA {{database_name}}.synapse;
DROP TABLE TEAMMEMBER_LATEST;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Drop the snapshot stream
USE SCHEMA {{database_name}}.synapse_raw;
DROP STREAM TEAMMEMBERSNAPSHOTS_STREAM;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Drop any scheduled tasks
USE SCHEMA {{database_name}}.synapse_raw;
-- Suspend ROOT TASK
ALTER TASK REFRESH_SYNAPSE_WAREHOUSE_S3_STAGE_TASK SUSPEND;
-- Drop UPSERT_TO_TEAMMEMBER_LATEST_TASK
DROP TASK UPSERT_TO_TEAMMEMBER_LATEST_TASK;
-- Resume the ROOT task and its child tasks
SELECT SYSTEM$TASK_DEPENDENTS_ENABLE( 'REFRESH_SYNAPSE_WAREHOUSE_S3_STAGE_TASK' );