-
Notifications
You must be signed in to change notification settings - Fork 175
Fix BigQuery ingestion errors and improve Cloud Build robustness #849
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,57 @@ | ||
| -- 1. Variable declarations must be at the top | ||
| DECLARE alter_stmt STRING; | ||
| BEGIN | ||
| -- 1. Variable declarations must be at the top | ||
| DECLARE alter_stmt STRING; | ||
| DECLARE columns_list STRING; | ||
| DECLARE insert_query STRING; | ||
|
|
||
| -- 2. Ensure the history table exists with metadata columns | ||
| CREATE TABLE IF NOT EXISTS `@PROJECT_ID@.@DATASET_NAME@.history` | ||
| ( | ||
| run_date DATE, | ||
| build_id STRING, | ||
| run_timestamp TIMESTAMP, | ||
| source_uri STRING, | ||
| branch_name STRING | ||
| ) | ||
| PARTITION BY run_date; | ||
| -- 2. Ensure the history table exists with metadata columns | ||
| CREATE TABLE IF NOT EXISTS `@PROJECT_ID@.@DATASET_NAME@.history` | ||
| ( | ||
| run_date DATE, | ||
| build_id STRING, | ||
| run_timestamp TIMESTAMP, | ||
| source_uri STRING, | ||
| branch_name STRING | ||
| ) | ||
| PARTITION BY run_date; | ||
|
|
||
| -- 3. Dynamically find new columns in staging that are missing from history | ||
| SET alter_stmt = ( | ||
| SELECT | ||
| CONCAT("ALTER TABLE `@PROJECT_ID@.@DATASET_NAME@.history` ", | ||
| STRING_AGG(CONCAT("ADD COLUMN `", column_name, "` ", data_type), ", ")) | ||
| FROM `@PROJECT_ID@.@DATASET_NAME@.INFORMATION_SCHEMA.COLUMNS` | ||
| WHERE table_name = 'staging' | ||
| AND column_name NOT IN ( | ||
| SELECT column_name | ||
| FROM `@PROJECT_ID@.@DATASET_NAME@.INFORMATION_SCHEMA.COLUMNS` | ||
| WHERE table_name = 'history' | ||
| ) | ||
| ); | ||
| -- 3. Dynamically find new columns in staging that are missing from history | ||
| SET alter_stmt = ( | ||
| SELECT | ||
| CONCAT("ALTER TABLE `@PROJECT_ID@.@DATASET_NAME@.history` ", | ||
| STRING_AGG(CONCAT("ADD COLUMN `", column_name, "` ", data_type), ", ")) | ||
| FROM `@PROJECT_ID@.@DATASET_NAME@.INFORMATION_SCHEMA.COLUMNS` | ||
| WHERE table_name = 'staging' | ||
| AND column_name NOT IN ( | ||
| SELECT column_name | ||
| FROM `@PROJECT_ID@.@DATASET_NAME@.INFORMATION_SCHEMA.COLUMNS` | ||
| WHERE table_name = 'history' | ||
| ) | ||
| ); | ||
|
|
||
| -- 4. Execute the schema update only if new columns were found | ||
| IF alter_stmt IS NOT NULL THEN | ||
| EXECUTE IMMEDIATE alter_stmt; | ||
| END IF; | ||
| -- 4. Execute the schema update only if new columns were found | ||
| IF alter_stmt IS NOT NULL THEN | ||
| EXECUTE IMMEDIATE alter_stmt; | ||
| END IF; | ||
|
|
||
| -- 5. Perform the idempotent ingestion | ||
| INSERT INTO `@PROJECT_ID@.@DATASET_NAME@.history` | ||
| SELECT | ||
| PARSE_DATE('%d%m%Y', REGEXP_EXTRACT(_FILE_NAME, r'/(\d{8})/')) as run_date, | ||
| REGEXP_EXTRACT(_FILE_NAME, r'/([0-9a-fA-F-]{36})/') as build_id, | ||
| PARSE_TIMESTAMP('%d%m%Y-%H%M%S', REGEXP_EXTRACT(_FILE_NAME, r'/(\d{8}-\d{6})/')) as run_timestamp, | ||
| _FILE_NAME as source_uri, | ||
| REGEXP_EXTRACT(_FILE_NAME, r'/branch=([^/]+)/') as branch_name, | ||
| * | ||
| FROM `@PROJECT_ID@.@DATASET_NAME@.staging` | ||
| WHERE _FILE_NAME NOT IN ( | ||
| SELECT DISTINCT source_uri | ||
| FROM `@PROJECT_ID@.@DATASET_NAME@.history` | ||
| ); | ||
| -- 5. Perform the idempotent ingestion using dynamic SQL to match column counts | ||
| SET columns_list = ( | ||
| SELECT STRING_AGG(CONCAT("`", column_name, "`"), ", " ORDER BY column_name) | ||
| FROM `@PROJECT_ID@.@DATASET_NAME@.INFORMATION_SCHEMA.COLUMNS` | ||
| WHERE table_name = 'staging' | ||
| ); | ||
|
|
||
| SET insert_query = CONCAT( | ||
| "INSERT INTO `@PROJECT_ID@.@DATASET_NAME@.history` (run_date, build_id, run_timestamp, source_uri, branch_name, ", | ||
| columns_list, | ||
| ") SELECT PARSE_DATE('%d%m%Y', REGEXP_EXTRACT(_FILE_NAME, r'/(\\d{8})/')) as run_date, ", | ||
| "REGEXP_EXTRACT(_FILE_NAME, r'/([0-9a-fA-F-]{36})/') as build_id, ", | ||
| "PARSE_TIMESTAMP('%d%m%Y-%H%M%S', REGEXP_EXTRACT(_FILE_NAME, r'/(\\d{8}-\\d{6})/')) as run_timestamp, ", | ||
| "_FILE_NAME as source_uri, ", | ||
| "REGEXP_EXTRACT(_FILE_NAME, r'/branch=([^/]+)/') as branch_name, ", | ||
| columns_list, | ||
| " FROM `@PROJECT_ID@.@DATASET_NAME@.staging` WHERE _FILE_NAME NOT IN (SELECT DISTINCT source_uri FROM `@PROJECT_ID@.@DATASET_NAME@.history`)" | ||
| ); | ||
|
|
||
| EXECUTE IMMEDIATE insert_query; | ||
| END; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.