Add initial legacy Presto/Athena compatibility udfs (#316)

* Add initial legacy Presto/Athena compatibility udfs

* fixup! Add initial legacy Presto/Athena compatibility udfs
This commit is contained in:
Sunah Suh 2019-08-28 17:16:59 -05:00 коммит произвёл GitHub
Родитель c5b33ee739
Коммит 525b9f05af
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 53 добавлений и 0 удалений

Просмотреть файл

@ -53,6 +53,8 @@ Recommended practices
- Each file must define a single function using `CREATE TEMP FUNCTION` syntax
- SQL UDFs must be defined in the `udf/` directory and JS UDFs must be defined
in the `udf_js` directory
- The `udf_legacy/` directory is an exception which should only contain
compatibility functions for queries migrated from Athena/Presto.
- The function must be named as `<dir_name>_<file_name_without_suffix>`
so `udf/mode_last.sql` must define a function `udf_mode_last`
- Must be defined as temporary UDFs

4
udf_legacy/README.md Normal file
Просмотреть файл

@ -0,0 +1,4 @@
Legacy UDFs
===
This directory contains compatibility functions for query migrations from Athena/Presto, and is named `udf_legacy` to discourage their ongoing use.

16
udf_legacy/contains.sql Normal file
Просмотреть файл

@ -0,0 +1,16 @@
/*
Returns true if the array arr contains the element el
*/
CREATE TEMP FUNCTION
udf_legacy_contains(arr ANY TYPE, el ANY TYPE)
RETURNS BOOLEAN
AS (
el IN UNNEST(arr)
);
-- Tests
assert_true(udf_legacy_contains([1, 2, 3], 1)),
asset_false(udf_legacy_contains([1, 2, 3], 5))

Просмотреть файл

@ -0,0 +1,15 @@
/*
Shim for Presto/Athena's DATE_FORMAT to BigQuery's FORMAT_DATE
Note that the format strings for FORMAT_DATE vs DATE_FORMAT are not
identical, but usages found in existing scheduled queries had format
strings that were portable between the two
*/
CREATE TEMP FUNCTION
udf_legacy_date_format(d DATE, f STRING)
RETURNS STRING
AS (
FORMAT_DATE(f, d)
);

16
udf_legacy/to_iso8601.sql Normal file
Просмотреть файл

@ -0,0 +1,16 @@
/*
Takes in a DATE type and returns a `YYYY-MM-DD` formatted string. This
function does _not_ attempt to replicate the entirety of the
functionality of Presto/Athena's `to_iso8601()` (which takes in a DATE,
DATETIME or TIMESTAMP type and returns either an iso8601-formatted date
string or a date-time string depending on the input type) since all use
cases found in legacy scheduled queries were for the DATE case.
*/
CREATE TEMP FUNCTION
udf_legacy_to_iso8601(x DATE)
RETURNS STRING
AS (
FORMAT_DATE('%F', x)
);