Skip to content

Commit

Permalink
Adjust the query to keep the destination rather than replace. (#1013)
Browse files Browse the repository at this point in the history
* Adjust the query to keep the destination rather than replace.

Signed-off-by: Caleb Brown <[email protected]>

* Update BQ load comments.

Signed-off-by: Caleb Brown <[email protected]>

---------

Signed-off-by: Caleb Brown <[email protected]>
  • Loading branch information
calebbrown authored Feb 14, 2024
1 parent bf4dd8f commit 836a6a2
Showing 1 changed file with 15 additions and 1 deletion.
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\`;
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

0 comments on commit 836a6a2

Please sign in to comment.