Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GENERATE_SURROGATE_KEY macro data type issue for Trino #2855

Closed
MikeWallis42 opened this issue Jul 3, 2024 · 1 comment · Fixed by tobymao/sqlglot#3732
Closed

GENERATE_SURROGATE_KEY macro data type issue for Trino #2855

MikeWallis42 opened this issue Jul 3, 2024 · 1 comment · Fixed by tobymao/sqlglot#3732

Comments

@MikeWallis42
Copy link

The output SQL for this macro for Trino is:-

LOWER(
  TO_HEX(
    MD5(
      CONCAT(
        CAST(COALESCE(CAST("column_a" AS VARCHAR), '_sqlmesh_surrogate_key_null_') AS VARCHAR),
        CAST('|' AS VARHCAR),
        CAST(COALESCE(CAST("column_b" AS VARCHAR), '_sqlmesh_surrogate_key_null_') AS VARCHAR)
      )
    )
  )
)

But Trino wants it as a varbinary https://trino.io/docs/current/functions/binary.html#md5

io.trino.spi.TrinoException: line 1:2496: Unexpected parameters (varchar) for function md5. Expected: md5(varbinary)

So you need to encode it first with TO_UTF8 https://trino.io/docs/current/functions/string.html#to_utf8

LOWER(
  TO_HEX(
    MD5(
      TO_UTF8(
        CONCAT(
          CAST(COALESCE(CAST("column_a" AS VARCHAR), '_sqlmesh_surrogate_key_null_') AS VARCHAR),
          CAST('|' AS VARHCAR),
          CAST(COALESCE(CAST("column_b" AS VARCHAR), '_sqlmesh_surrogate_key_null_') AS VARCHAR)
        )
      )
    )
  )
)

Many thanks in advance!

@georgesittas
Copy link
Contributor

Hey @MikeWallis42, thank you for reporting this! We'll take a look soon.

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 a pull request may close this issue.

2 participants