korbit-ai[bot] commented on code in PR #33518:
URL: https://github.com/apache/superset/pull/33518#discussion_r2096032377
##########
superset/db_engine_specs/base.py:
##########
@@ -1124,18 +1128,9 @@ def get_cte_query(cls, sql: str) -> str | None:
"""
if not cls.allows_cte_in_subquery:
- stmt = sqlparse.parse(sql)[0]
-
- # The first meaningful token for CTE will be with WITH
- idx, token = stmt.token_next(-1, skip_ws=True, skip_cm=True)
- if not (token and token.ttype == CTE):
- return None
- idx, token = stmt.token_next(idx)
- idx = stmt.token_index(token) + 1
-
- # extract rest of the SQLs after CTE
- remainder = "".join(str(token) for token in
stmt.tokens[idx:]).strip()
- return f"WITH {token.value},\n{cls.cte_alias} AS (\n{remainder}\n)"
+ statement = SQLStatement(sql, engine=cls.engine)
+ if statement.has_cte():
+ return statement.as_cte(cls.cte_alias).format()
Review Comment:
### Missing CTE format validation <sub></sub>
<details>
<summary>Tell me more</summary>
###### What is the issue?
The code assumes the SQLStatement.format() method will handle CTE correctly
without verifying if the formatted SQL is valid.
###### Why this matters
Lack of validation could cause runtime errors if the formatted SQL is
malformed or invalid for the target database engine.
###### Suggested change ∙ *Feature Preview*
Add validation after formatting the CTE:
```python
def get_cte_query(cls, sql: str) -> str | None:
statement = SQLStatement(sql, engine=cls.engine)
if statement.has_cte():
formatted_cte = statement.as_cte(cls.cte_alias).format()
try:
# Add validation logic here
cls.validate_sql(formatted_cte)
return formatted_cte
except Exception as e:
logger.warning(f"CTE formatting validation failed: {e}")
return None
return None
```
###### Provide feedback to improve future suggestions
[](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/5912e455-e884-4c75-b9b9-9f05ff369b7a/upvote)
[](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/5912e455-e884-4c75-b9b9-9f05ff369b7a?what_not_true=true)
[](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/5912e455-e884-4c75-b9b9-9f05ff369b7a?what_out_of_scope=true)
[](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/5912e455-e884-4c75-b9b9-9f05ff369b7a?what_not_in_standard=true)
[](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/5912e455-e884-4c75-b9b9-9f05ff369b7a)
</details>
<sub>
💬 Looking for more details? Reply to this comment to chat with Korbit.
</sub>
<!--- korbi internal id:2b609f44-d075-4eac-9fa7-e13cc8ab9b64 -->
[](2b609f44-d075-4eac-9fa7-e13cc8ab9b64)
--
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]