FrancescoCastaldi opened a new pull request, #43565:
URL: https://github.com/apache/superset/pull/43565
### SUMMARY
Fixes #38189
In `SQLStatement._parse`, when a SQL script contained statements terminated
by a semicolon followed by trailing `--` comments (which `sqlglot` parses as a
trailing `exp.Semicolon` statement), the comment relocation logic iterated over
all AST nodes using `.walk()`.
Because `.walk()` traverses AST subtrees in internal dictionary order rather
than reverse SQL document rendering order, `target` was erroneously set to leaf
nodes inside optimizer hint blocks (e.g. `exp.Hint` ->
`Identifier("query_timeout")`), causing trailing comments to be injected
directly inside optimizer hints (e.g. `/*+ SET_VAR(query_timeout /* comment */
= 3000) */`).
This PR fixes this by introducing `_find_last_token_node(node)`, which:
1. Traverses SQL clauses in true reverse generation order (`offset`,
`limit`, `order`, `window`, `where`, `from`, `expressions`, etc.).
2. Explicitly skips `exp.Hint` / non-trailing nodes so that comments are
safely attached to the final token/clause of the statement.
3. Removes the `@pytest.mark.xfail` marker from
`test_sqlscript_format_preserves_optimizer_hint_block_with_semicolon` in
`tests/unit_tests/sql/parse_tests.py` and adds tests covering CTEs and
optimizer hints.
### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A (SQL Parsing & formatting fix)
### TESTING INSTRUCTIONS
1. Execute formatting on a query containing an optimizer hint and trailing
comments with a terminating semicolon (e.g., in SQL Lab with StarRocks/MySQL):
```sql
SELECT /*+ SET_VAR(query_timeout = 3000) */ col1, col2
FROM my_table
LIMIT 100;
-- increase timeout for large scans
```
2. Verify that the query formats cleanly without inserting comments inside
the `/*+ ... */` hint block.
### ADDITIONAL INFORMATION
- [x] Has associated issue: Fixes #38189
- [ ] Required feature flags:
- [ ] Changes UI
- [ ] Includes DB Migration
- [ ] Introduces new feature or API
- [ ] Removes existing feature or API
--
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]