Skip to content

fix(e6): transpile the query inside a Databricks EXPLAIN - #328

Merged
tkaunlaky-e6 merged 1 commit into
mainfrom
fix/e6-explain-transpile-inner
Oct 13, 2026
Merged

tkaunlaky-e6 merged 1 commit into
mainfrom
fix/e6-explain-transpile-inner

Conversation

@tkaunlaky-e6

@tkaunlaky-e6 tkaunlaky-e6 commented Oct 13, 2026 •

Copy link
Copy Markdown

Problem

A Databricks EXPLAIN <query> reaches the transpiler but is returned untranspiled. EXPLAIN is a tokenizer COMMAND, so the tokenizer swallows everything after it into one string:

Command(this=EXPLAIN, expression=Literal('SELECT a AS time FROM t'))

The query never becomes AST, so no transform reaches it and it is written back verbatim. The planner then fails on Databricks-only syntax that the plain query would have had transpiled.

Example from the planner logs (Q403866-16-20261013.035120.090):

  • Transpiler returned: EXPLAIN SELECT date_trunc('day', to_timestamp(date, 'MM/dd/yyyy')) AS time, SUM(costInBillingCurrency) AS cost FROM pu_orbis_platform_prod_catalog._maap.costs GROUP BY 1 ORDER BY 1 (unchanged)
  • Planner: Encountered "TIME" at line 1, column 71, then Parsing Failed. The reserved alias time is never quoted.

The same query without EXPLAIN transpiles and runs. Every main build back to Sep 8 returns the EXPLAIN form unchanged, so this is not a regression.

Change

  • databricks.py: the Databricks parser parses the string after EXPLAIN into the statement (dropping the Databricks-only EXTENDED|CODEGEN|COST|FORMATTED style) and returns a Databricks-only Explain. Explain is a Command that also holds the parsed statement:
    Explain(this=EXPLAIN, expression=Literal('FORMATTED SELECT a AS time FROM t'),
            statement=Select(expressions=[Alias(this=Column(a), alias=Identifier(time))], from=From(Table(t))))
    
    Because the query is now part of the AST, the converter's existing AST passes (identifier quoting etc.) reach it with no converter change. A bare or unparseable EXPLAIN stays a plain Command.
  • e6.py: TRANSFORMS[databricks.Explain] writes EXPLAIN <transpiled statement>.
  • Every other dialect still writes it back verbatim. Explain keeps the Command key and raw text, and it is defined in the Databricks module, so no other dialect parses into it.

After: EXPLAIN SELECT DATE_TRUNC('DAY', TO_TIMESTAMP("date", 'MM/dd/yyyy')) "time", SUM(costInBillingCurrency) AS cost FROM ... GROUP BY 1 ORDER BY 1

Verification

  • New test_databricks_explain in tests/dialects/test_e6.py. It fails on main and passes here.
  • Every sqlglot dialect as target (31), main vs this branch, 5 Databricks inputs including bare EXPLAIN and SHOW TABLES: only the e6 EXPLAIN <query> rows change.
  • sqlglot, java and native executors, with and without from_dialect, pretty on and off: the EXPLAIN output equals EXPLAIN + the plain query's output in 72 of 72 cases.
  • /convert-query, java and native: 30 of 30 cases where the EXPLAIN output equals EXPLAIN + the plain output ("time" quoted, GET_JSON_OBJECT, ROLLUP, DATE_DIFF, CTEs). Unchanged: Snowflake EXPLAIN, a column named explain, and the SKIP_E6_TRANSPILATION path.
  • make check passes (ruff, ruff-format, mypy, 1015 tests).
  • Not run on the engine: the native planner in the logs accepts EXPLAIN SELECT ..., but the local engine only accepts EXPLAIN PLAN FOR.

EXPLAIN is a tokenizer COMMAND, so everything after it became one string
literal in an opaque exp.Command and the query was never transpiled; the
planner then failed on Databricks-only syntax (e.g. the reserved alias
AS time left unquoted).

The Databricks parser now parses that string into the statement and returns
a Databricks-only Explain (a Command that also holds the statement), so the
query is part of the AST and AST passes reach it. E6 writes it as
EXPLAIN <query>, dropping the Databricks-only EXTENDED/CODEGEN/COST/FORMATTED
style; every other dialect still writes it back verbatim like any Command.
@tkaunlaky-e6
tkaunlaky-e6 force-pushed the fix/e6-explain-transpile-inner branch from 1116932 to c57e9eb Compare October 13, 2026 12:07
@tkaunlaky-e6 tkaunlaky-e6 changed the title fix(converter): transpile the query inside a Databricks EXPLAIN fix(e6): transpile the query inside a Databricks EXPLAIN Oct 13, 2026
@tkaunlaky-e6
tkaunlaky-e6 merged commit 5aa3f56 into main Oct 13, 2026
6 checks passed
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.

2 participants