Skip to content

Conversation

@fivetran-felixhuang
Copy link
Collaborator

https://docs.snowflake.com/en/sql-reference/functions/to_binary

In Snowflake, TO_BINARY takes an input string with an optional format argument (like 'BASE64'), reads this string according to the format (default is HEX), and converts it to binary. But in DuckDB, the TO_BINARY function takes a UTF-8 string coverts it to binary but returns it as string (like '0101').

So we need to take care of the input format and the return type.

if the return_type is BINARY (like snowflake):
(Snowflake -> DuckDB)
- TO_BINARY('48454C50', 'HEX') → UNHEX('48454C50')
- TO_BINARY('TEST', 'UTF-8') → ENCODE('TEST')
- TO_BINARY('SEVMUA==', 'BASE64') → FROM_BASE64('SEVMUA==')

If the return_type is VARCHAR (like DuckDB), we can use DuckDB's native TO_BINARY.

For this PR only transpilation from Snowflake to DuckDB is supported.

Copy link
Collaborator

@georgesittas georgesittas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@georgesittas georgesittas merged commit b45d9a3 into main Dec 10, 2025
8 checks passed
@georgesittas georgesittas deleted the transpilation_to_binary_snowflake_duckdb branch December 10, 2025 12:49
georgesittas added a commit that referenced this pull request Dec 10, 2025
…#6504)

* support TO_BINARY transpilation from snowflake to duckdb

* add default format for snowflake

* fix tests

* address comments

* small fix

* use type inference for transpilation

* small update

* Remove unnecessary `FUNCTIONS` entry.

---------

Co-authored-by: Jo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants