feat(snowflake)!: support transpilation of TRY_TO_BINARY from Snowflake to DuckDB #6629
+57
−30
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.
https://docs.snowflake.com/en/sql-reference/functions/try_to_binary
The difference between TRY_TO_BINARY and TO_BINARY is that TRY_TO_BINARY returns NULL when TO_BINARY throws an error. Also TRY_TO_BINARY returns NULL if the format is not HEX, BASE64, or UTF-8.
We can achieve this be using transpilation method tobinary_sql and wrap the transpiled query with TRY(). If the format specified is invalid, we return NULL directly.
source query:
SELECT
TRY_TO_BINARY('48656C6C6F', 'HEX') AS valid_hex,
TRY_TO_BINARY('Hello', 'UTF-8') AS valid_utf8,
TRY_TO_BINARY('SGVsbG8=', 'BASE64') AS valid_base64,
TRY_TO_BINARY('invalid_hex', 'HEX') AS invalid_hex,
TRY_TO_BINARY('48656C6C6F', 'UTF-16') AS invalid_format,
TRY_TO_BINARY('invalid==', 'BASE64') AS invalid_base64;
transpiled query:
SELECT
TRY(UNHEX('48656C6C6F')) AS "VALID_HEX",
TRY(ENCODE('Hello')) AS "VALID_UTF8",
TRY(FROM_BASE64('SGVsbG8=')) AS "VALID_BASE64",
TRY(UNHEX('invalid_hex')) AS "INVALID_HEX",
TRY(NULL) AS "INVALID_FORMAT",
TRY(FROM_BASE64('invalid==')) AS "INVALID_BASE64"
-- (wrapped with HEX so we can see the blob value directly)
SELECT
HEX(TRY(UNHEX('48656C6C6F'))) AS "VALID_HEX",
HEX(TRY(ENCODE('Hello'))) AS "VALID_UTF8",
HEX(TRY(FROM_BASE64('SGVsbG8='))) AS "VALID_BASE64",
HEX(TRY(UNHEX('invalid_hex'))) AS "INVALID_HEX",
HEX(TRY(NULL)) AS "INVALID_FORMAT",
HEX(TRY(FROM_BASE64('invalid=='))) AS "INVALID_BASE64"