Skip to content

bug(trino): SQL formatter rewrites Trino-native functions to invalid names (date_diff→datediff, to_hex→hex) via sqlglot dialect mismatch #39100

Closed

Description

@shimonp

Bug description

When "Format SQL" is enabled in SQL Lab, Superset passes Trino queries through
sqlglot for pretty-printing but does not thread the Trino dialect through to
sqlglot's generator. This causes sqlglot to fall back to its generic SQL
generator, which renames several Trino-native functions to names that Trino
does not recognise:

date_diff → datediff (FUNCTION_NOT_FOUND)
to_hex → hex (FUNCTION_NOT_FOUND)
from_hex → unhex (FUNCTION_NOT_FOUND)
date_trunc → (argument order swapped in some paths)

The error from Trino is always the same pattern:
TrinoUserError(type=USER_ERROR, name=FUNCTION_NOT_FOUND,
message="line N:N: Function 'datediff' not registered")

sqlglot itself is NOT at fault. When the dialect is passed correctly, it
round-trips Trino functions perfectly:

sqlglot.transpile("SELECT date_diff('day', a, b)", read="trino", write="trino")
→ ["SELECT date_diff('day', a, b)"] ✅

sqlglot.transpile("SELECT date_diff('day', a, b)") # no dialect
→ ["SELECT DATEDIFF(b, a)"] ❌ ← what Superset produces

Root cause: Superset's SQL formatting path calls sqlglot without specifying
the write dialect for TrinoEngineSpec, so the generic generator is used.
The fix belongs in superset/db_engine_specs/presto.py (PrestoBaseEngineSpec)
or trino.py, adding explicit sqlglot TRANSFORMS overrides for the affected
exp.* AST nodes so they emit the correct Trino function names regardless of
how the generator is invoked.

Screenshots/recordings

  1. Connect Superset 6.0.0 (or 6.1rc1) to a Trino database (any version)
  2. Open SQL Lab, ensure "Format SQL" is ON
  3. Run:
    SELECT date_diff('day', DATE '2023-01-01', current_date)
  4. Observe error:
    TrinoUserError: Function 'datediff' not registered
  5. Disable "Format SQL" — same query runs successfully
  6. Same issue reproducible with: to_hex(), from_hex(), sha256(), md5()

Superset version

master / latest-dev

Python version

3.9

Node version

16

Browser

Chrome

Additional context

Expected result:
Trino-native functions (date_diff, to_hex, from_hex, md5, sha256, date_add)
are passed through to Trino unchanged, regardless of whether "Format SQL"
is enabled.

Checklist

  • I have searched Superset docs and Slack and didn't find a solution to my problem.
  • I have searched the GitHub issue tracker and didn't find a similar bug report.
  • I have checked Superset's logs for errors and if I found a relevant Python stacktrace, I included it here as text in the "additional context" section.

Activity

  1. dosubot commented on Apr 3, 2026

    @dosubot

    Good find — and you're right that sqlglot itself handles Trino functions correctly when the dialect is specified. The root cause is actually in the frontend, not the engine specs or the backend formatting code.

    The backend formatting path in superset/sql/parse.py does accept and correctly use the Trino dialect when provided — both for sqlglot.parse(dialect=...) and Dialect.generate(...). Trino is properly mapped to Dialects.TRINO in the SQLGLOT_DIALECTS dictionary [1].

    The problem is that SQL Lab's frontend never passes the engine parameter when calling the format endpoint. In superset-frontend/src/SqlLab/actions/sqlLab.js, the formatQuery function sends only { sql } — there's even a TODO comment acknowledging this:

    // TODO (betodealmeida): pass engine as a parameter for better formatting
    body: JSON.stringify({ sql }),

    Without the engine parameter, the backend has no dialect to pass to sqlglot, so sqlglot falls back to its generic SQL generator — which is exactly what produces datediff, hex, unhex, etc.

    Interestingly, the ViewQuery component in the Explore view does pass the engine parameter correctly by reading it from dataset metadata.

    So the fix would be to pass the database engine (e.g., "trino") from the SQL Lab editor to the /api/v1/sqllab/format_sql/ endpoint, similar to how ViewQuery.tsx already does it. The SQL Lab editor has access to the selected database — it just needs to include its backend in the format request payload.

    To reply, just mention @dosu.


    Docs are dead. Just use Dosu.

    Leave Feedback Ask Dosu about superset Share Dosu with your team

  2. shimonp commented on Apr 4, 2026

    @shimonp
    Author

    I am new to the bug fixing process in Superset. Am I expected to provide the fix you have described or the core team will review, implement the fix and merge into main branch on its own? It does not work in v6.0.0 or 6.1rc1 and I hope that 6.1 won't be released without implementing and testing this trivial fix. We use Trino engine extensively, so custom syntax of Trino sql dialect is important to us. Best regards.

  3. rusackas commented on Apr 15, 2026

    @rusackas
    Member

    @shimonp if you think the bot is right, and you're willing to test it locally, opening a PR would be much appreciated. If you do, we'll try to review it and merge it, but testing it is tricker since not all of use use Trino, so your validation would be appreciated.

    Tagging @betodealmeida for context here, in case they have input.

  4. shimonp commented on Apr 15, 2026

    @shimonp
    Author
  5. justinpark commented on Apr 15, 2026

    @justinpark
    Member

    @shimonp instead of the ai suggestion, I made the hotfix by using the database_id (which recently added by #36277)

  6. shimonp commented on Apr 15, 2026

    @shimonp
    Author

    @shimonp instead of the ai suggestion, I made the hotfix by using the database_id (which recently added by #36277)

    I agree with this fix. Let me know if you need help to test the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

data:connect:trinoRelated to TrinosqllabNamespace | Anything related to the SQL Lab

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions