Fix: generate_surrogate_key returns hex strings for SHA256/SHA512 on Presto and Trino#5888
Open
Pawansingh3889 wants to merge 1 commit into
Open
Fix: generate_surrogate_key returns hex strings for SHA256/SHA512 on Presto and Trino#5888Pawansingh3889 wants to merge 1 commit into
Pawansingh3889 wants to merge 1 commit into
Conversation
…Presto and Trino Signed-off-by: Pawan Singh Kapkoti <42340841+Pawansingh3889@users.noreply.github.com>
a6384fe to
f580eb3
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Fixes #5871.
@GENERATE_SURROGATE_KEY(..., hash_function := 'SHA256')produces a key that changes value depending on the engine. With MD5 the macro converts the parsedexp.MD5Digestintoexp.MD5, so Presto and Trino render the hex-string form (LOWER(TO_HEX(MD5(TO_UTF8(...))))). With SHA256/SHA512 there is no equivalent conversion, so Trino gets a bareSHA256(varchar): a type error at execution time, and a binary digest rather than a hex string where it does run.The upstream half merged in sqlglot this week (tobymao/sqlglot#7824): Presto/Trino now map native
SHA256/SHA512toexp.SHA2Digestand renderexp.SHA2in the hex-string form, mirroring their MD5/MD5Digest handling.This PR is the macro half, mirroring the MD5 conversion that already sits one line above:
exp.SHA2Digestresults convert toexp.SHA2(keeping the length), so the surrogate key is always the string-semantics expression.text, which lets generators that wrap an encode around string inputs (TO_UTF8on Presto/Trino) do so without a separate annotation pass.Behavior on the currently pinned sqlglot (30.8.0) is byte-identical for every dialect; I verified the rendered SQL for MD5/SHA256 across trino, duckdb, bigquery and snowflake before and after. The conversion itself is still exercised today because BigQuery's parser already maps
SHA256toSHA2Digest. Once the sqlglot pin moves past #7824, Trino and Presto start renderingLOWER(TO_HEX(SHA256(TO_UTF8(...))))with no further change here; the string assertions for that are noted in the test for whoever does the bump (happy to follow up with them at that point).Test Plan
test_generate_surrogate_key_hash_semanticsintests/core/test_macros.py: asserts the macro returnsexp.SHA2(never a digest node) with a text-typed argument, and pins the rendered SQL for bigquery/duckdb SHA256 and trino MD5, all stable across supported sqlglot versions.tests/core/test_macros.py: 139 passed.Checklist
make styleand fixed any issues (ruff check and ruff format pass on the changed files; happy to fix anything else CI flags)make fast-test) - rantests/core/test_macros.pyin full (139 passed); leaving the broader suite to CIgit commit -s) per the DCO