rusackas opened a new pull request, #41803:
URL: https://github.com/apache/superset/pull/41803

   ### SUMMARY
   Trino queries that declare inline SQL UDFs (`WITH FUNCTION ... RETURNS ... 
BEGIN ... END SELECT ...`) fail to parse in SQL Lab, even though they run fine 
in Trino itself. Two things go wrong: sqlglot splits statements on every 
semicolon (including the ones inside `BEGIN ... END` routine bodies), and it 
has no grammar for `FUNCTION` specifications in a `WITH` clause. The upstream 
issue (tobymao/sqlglot#5178) was closed as "low priority, PRs welcome", so this 
fixes it on our side with a custom Trino dialect in 
`superset/sql/dialects/trino.py`, following the same pattern as our 
Firebolt/Dremio/Pinot dialects.
   
   The dialect does two things: keeps routine bodies intact when chunking 
statements (tracking `BEGIN`/`CASE`/`IF`/`LOOP`/`REPEAT`/`WHILE` ... `END` 
nesting), and parses inline function specifications into opaque `InlineUDF` 
nodes that regenerate verbatim. Trino doesn't allow queries inside SQL UDF 
bodies, so the opaque representation hides no table references from the 
security checks... table extraction, mutation detection, and limit handling all 
keep working on the main query. The new code paths only activate on syntax that 
fails to parse today (`WITH FUNCTION`, `CREATE [OR REPLACE] FUNCTION`), so 
existing queries are untouched — a CTE literally named `function` still parses 
as a regular CTE, and there's a test pinning that.
   
   One known limitation: a procedural `IF` statement whose condition starts 
with a parenthesis (`IF (a > b) THEN`) is ambiguous with the scalar `IF()` 
function and will still fail to parse — same failure mode as today, so no 
regression.
   
   ### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
   Before (from #26162): `sql parse error: Expecting (. Line 2, Col: 29.` — 
after, the query from the [Trino 
docs](https://trino.io/docs/current/udf/sql/begin.html) parses, splits, and 
round-trips correctly.
   
   ### TESTING INSTRUCTIONS
   Run `pytest tests/unit_tests/sql/dialects/trino_tests.py`. Or with a Trino 
458+ database connected, paste the example from the issue into SQL Lab and run 
it:
   
   ```sql
   WITH FUNCTION meaning_of_life()
     RETURNS tinyint
     BEGIN
       DECLARE a tinyint DEFAULT CAST(6 AS tinyint);
       DECLARE b tinyint DEFAULT CAST(7 AS tinyint);
       RETURN a * b;
     END
   SELECT meaning_of_life()
   ```
   
   ### ADDITIONAL INFORMATION
   - [x] Has associated issue: Fixes #26162
   - [ ] Required feature flags:
   - [ ] Changes UI
   - [ ] Includes DB Migration (follow approval process in 
[SIP-59](https://github.com/apache/superset/issues/13351))
     - [ ] Migration is atomic, supports rollback & is backwards-compatible
     - [ ] Confirm DB migration upgrade and downgrade tested
     - [ ] Runtime estimates and downtime expectations provided
   - [ ] Introduces new feature or API
   - [ ] Removes existing feature or API
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to