Last active
September 30, 2020 01:22
-
-
Save dewaldabrie/73809627e9235647a28b1a6f21a7fcbd to your computer and use it in GitHub Desktop.
Bigquery JS UDF Examples
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
-- Example of a persistent function that use libraries stored in Google Cloud Storage | |
-- Taken from https://cloud.google.com/bigquery/docs/reference/standard-sql/user-defined-functions | |
CREATE OR REPLACE FUNCTION dataset.myFunc(a FLOAT64, b STRING) | |
RETURNS STRING | |
LANGUAGE js | |
OPTIONS ( | |
library=["gs://my-bucket/path/to/lib1.js", "gs://my-bucket/path/to/lib2.js"] | |
) | |
AS | |
""" | |
// Assumes 'doInterestingStuff' is defined in one of the library files. | |
return doInterestingStuff(a, b); | |
"""; | |
SELECT myFunc(3.14, 'foo'); |
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
-- Example of a temporary function that does a set difference on two string arrays | |
CREATE TEMPORARY FUNCTION string_set_diff(arr1 ARRAY<STRING>, arr2 ARRAY<STRING>) | |
RETURNS ARRAY<STRING> | |
LANGUAGE js AS """ | |
let difference = arr1.filter(x => !arr2.includes(x)); | |
return difference | |
"""; | |
SELECT string_set_diff(ARRAY ['foo', 'bar'], ARRAY ['baz', 'foo']); | |
-- Results in unity array with value 'bar' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment