Skip to content

Commit

Permalink
trino: Allow replacing existing SQL UDFs
Browse files Browse the repository at this point in the history
This makes SQL UDF installation idempotent.
  • Loading branch information
emk committed Feb 29, 2024
1 parent 38655a6 commit 9637cae
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions docker/trino/install-udfs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set -euo pipefail

# URL of our Trino server.
trino_url="${1:-"http://localhost:8080"}"
#echo trino_url: "$trino_url"

until curl -s "$trino_url/v1/info" | grep -q '"starting":false'; do sleep 1; done
trino --file "/etc/trino/trino_compat.sql" "$trino_url"
16 changes: 8 additions & 8 deletions sql/trino_compat.sql
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
-- Compatibility functions for Trino, needed to run code generated by Joinery.

CREATE FUNCTION memory.joinery_compat.DATETIME(date VARCHAR)
CREATE OR REPLACE FUNCTION memory.joinery_compat.DATETIME(date VARCHAR)
RETURNS TIMESTAMP
RETURNS NULL ON NULL INPUT
RETURN (
CAST(date AS TIMESTAMP)
);

CREATE FUNCTION memory.joinery_compat.DATETIME(date DATE)
CREATE OR REPLACE FUNCTION memory.joinery_compat.DATETIME(date DATE)
RETURNS TIMESTAMP
RETURNS NULL ON NULL INPUT
RETURN (
CAST(date AS TIMESTAMP)
);

CREATE FUNCTION memory.joinery_compat.GENERATE_UUID()
CREATE OR REPLACE FUNCTION memory.joinery_compat.GENERATE_UUID()
RETURNS VARCHAR
NOT DETERMINISTIC
RETURN (
CAST(UUID() AS VARCHAR)
);

CREATE FUNCTION memory.joinery_compat.SHA256_COMPAT(input VARCHAR)
CREATE OR REPLACE FUNCTION memory.joinery_compat.SHA256_COMPAT(input VARCHAR)
RETURNS VARBINARY
RETURNS NULL ON NULL INPUT
RETURN (
SHA256(TO_UTF8(input))
);

CREATE FUNCTION memory.joinery_compat.SHA256_COMPAT(input VARBINARY)
CREATE OR REPLACE FUNCTION memory.joinery_compat.SHA256_COMPAT(input VARBINARY)
RETURNS VARBINARY
RETURNS NULL ON NULL INPUT
RETURN (
SHA256(input)
);

-- Handle odd BigQuery behaviour around the `pos` argument.
CREATE FUNCTION memory.joinery_compat.SUBSTR_COMPAT(input VARCHAR, pos INT)
CREATE OR REPLACE FUNCTION memory.joinery_compat.SUBSTR_COMPAT(input VARCHAR, pos INT)
RETURNS VARCHAR
RETURNS NULL ON NULL INPUT
RETURN (
Expand All @@ -50,7 +50,7 @@ RETURN (

-- As the two argument case, but also treat a length greater than the string as
-- as "to the end of the string".
CREATE FUNCTION memory.joinery_compat.SUBSTR_COMPAT(input VARCHAR, pos INT, len INT)
CREATE OR REPLACE FUNCTION memory.joinery_compat.SUBSTR_COMPAT(input VARCHAR, pos INT, len INT)
RETURNS VARCHAR
RETURNS NULL ON NULL INPUT
RETURN (
Expand All @@ -72,7 +72,7 @@ RETURN (
END
);

CREATE FUNCTION memory.joinery_compat.TO_HEX_COMPAT(input VARBINARY)
CREATE OR REPLACE FUNCTION memory.joinery_compat.TO_HEX_COMPAT(input VARBINARY)
RETURNS VARCHAR
RETURNS NULL ON NULL INPUT
RETURN (
Expand Down

0 comments on commit 9637cae

Please sign in to comment.