Skip to content

Commit

Permalink
UDFs for generating deep links to BigQuery console (#359)
Browse files Browse the repository at this point in the history
Co-authored-by: ddeleo <danieldeleo@google.com>
  • Loading branch information
danieldeleo and ddeleo committed May 18, 2023
1 parent 9a9a2c8 commit 4fcd597
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
33 changes: 33 additions & 0 deletions udfs/community/job_url.sqlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
config { hasOutput: true }
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* Generates a deep link to the BigQuery console for a given job_id in the form:
* project:location.job_id.
*/
CREATE OR REPLACE FUNCTION ${self()}(job_id STRING)
RETURNS STRING
OPTIONS(description="""Generates a deep link to the BigQuery console
for a given job_id in the form: "project:location.job_id".
""") AS (
"https://console.cloud.google.com/bigquery?project="
|| SPLIT(job_id, ":")[SAFE_OFFSET(0)]
|| "&j=bq:"
|| SPLIT(SPLIT(job_id, ":")[SAFE_OFFSET(1)], ".")[SAFE_OFFSET(0)]
|| ":"
|| SPLIT(job_id, ".")[SAFE_OFFSET(1)]
);
33 changes: 33 additions & 0 deletions udfs/community/table_url.sqlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
config { hasOutput: true }
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* Generates a deep link to the BigQuery console for a table or view.
*/
CREATE OR REPLACE FUNCTION ${self()}(table_id STRING)
RETURNS STRING
OPTIONS(description="""Generates a deep link to the BigQuery console
for a table or view in the form: "project.dataset.table".
""") AS (
"https://console.cloud.google.com/bigquery?p="
|| SPLIT(table_id, ".")[SAFE_OFFSET(0)]
|| "&d="
|| SPLIT(table_id, ".")[SAFE_OFFSET(1)]
|| "&t="
|| SPLIT(table_id, ".")[SAFE_OFFSET(2)]
|| "&page=table"
);
49 changes: 49 additions & 0 deletions udfs/community/test_cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -3086,3 +3086,52 @@ generate_udf_test("sure_values", [
expected_output: `NULL`
}
]);

generate_udf_test("job_url", [
{
inputs: [
`"my_project:us.my_job_id"`
],
expected_output: `"https://console.cloud.google.com/bigquery?project=my_project&j=bq:us:my_job_id"`
},
{
inputs: [
`"my_job_id"`
],
expected_output: `NULL`
},
{
inputs: [
`NULL`
],
expected_output: `NULL`
}
]);

generate_udf_test("table_url", [
{
inputs: [
`"my_project.my_dataset.my_table"`
],
expected_output: `"https://console.cloud.google.com/bigquery?p=my_project&d=my_dataset&t=my_table&page=table"`
},
{
inputs: [
`"my_dataset.my_table"`
],
expected_output: `NULL`
},
{
inputs: [
`"my_table"`
],
expected_output: `NULL`
},
{
inputs: [
`NULL`
],
expected_output: `NULL`
}

]);

0 comments on commit 4fcd597

Please sign in to comment.