Skip to content

Instantly share code, notes, and snippets.

@isaac-ped
Last active April 15, 2025 18:14
Show Gist options
  • Save isaac-ped/dde84c1fc300326b89488d2f1fe7d4cf to your computer and use it in GitHub Desktop.
Save isaac-ped/dde84c1fc300326b89488d2f1fe7d4cf to your computer and use it in GitHub Desktop.
scheduled snapshot query creation
def _snapshot_query(
snapshot_dataset: str,
source_dataset: str,
source_table: str,
retention_days: int | None,
) -> str:
"""Contsruct the query for snapshotting a table.
Taken from: https://cloud.google.com/bigquery/docs/table-snapshots-scheduled#schedule_the_monthly_query
See the docstring for _create_scheduled_snapshot for details on parameters.
"""
if retention_days is not None:
expiration = f"DATE_ADD(current_timestamp(), INTERVAL {retention_days} DAY);"
clone_opts = (
r'" OPTIONS(expiration_timestamp = TIMESTAMP \"", expiration, "\")"'
)
else:
expiration = '""'
clone_opts = '""'
return f"""
-- Declare variables
DECLARE snapshot_name STRING;
DECLARE expiration TIMESTAMP;
DECLARE query STRING;
-- Set variables
SET expiration = {expiration}
SET snapshot_name = CONCAT(
"{snapshot_dataset}.{source_dataset}-{source_table}",
FORMAT_DATETIME('%Y%m%d', current_date())
);
-- Construct the query to create the snapshot
SET query = CONCAT(
"CREATE SNAPSHOT TABLE ", snapshot_name,
" CLONE {source_dataset}.{source_table}", {clone_opts}, ";"
");"
);
-- Run the query
EXECUTE IMMEDIATE query;"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment