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

Adjust the query to keep the destination rather than replace. #1013

Merged
merged 4 commits into from
Feb 14, 2024
Merged
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
16 changes: 15 additions & 1 deletion scripts/bq_load.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,21 @@ for bucket_prefix in `gsutil ls "$RESULT_BUCKET"`; do
union="$union$subquery"
done

query="CREATE OR REPLACE TABLE \`$PROJECT_ID.$DEST_DATASET.$DEST_TABLE\` LIKE \`$PROJECT_ID.$LOAD_DATASET.$table_name\` PARTITION BY TIMESTAMP_TRUNC(CreatedTimestamp, DAY) OPTIONS(expiration_timestamp=NULL) AS $union;"
# Query to populate the destination table.
#
# If the table does not exist it will be created. Keeping the table ensures
# that breaking schema changes are not accidentally propagated to the
# destination table.
#
# A transaction is used to keep the update atomic. It will also rollback the
# TRUNCATE if the INSERT fails, such as when the schema has changed.
query="
CREATE TABLE IF NOT EXISTS \`$PROJECT_ID.$DEST_DATASET.$DEST_TABLE\` LIKE \`$PROJECT_ID.$LOAD_DATASET.$table_name\`
PARTITION BY TIMESTAMP_TRUNC(CreatedTimestamp, DAY) OPTIONS(expiration_timestamp=NULL);
BEGIN TRANSACTION;
TRUNCATE TABLE \`$PROJECT_ID.$DEST_DATASET.$DEST_TABLE\`;
calebbrown marked this conversation as resolved.
Show resolved Hide resolved
INSERT INTO \`$PROJECT_ID.$DEST_DATASET.$DEST_TABLE\` $union;
COMMIT TRANSACTION;"

echo "## Updating \`$PROJECT_ID.$DEST_DATASET.$DEST_TABLE\` from shards."
echo "Executing query: '$query'"
Expand Down
Loading