-
Notifications
You must be signed in to change notification settings - Fork 13.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: sqlparse
fallback for formatting queries
#30578
Conversation
6d62f0f
to
0588776
Compare
if isinstance(statement, str) | ||
else statement | ||
) | ||
self._sql = statement |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before this PR the Statement
classes didn't store a reference to the original SQL, only the AST. So I had to modify it here.
0588776
to
db7c99a
Compare
@betodealmeida We discussed this PR during the daily PR review meeting, and we think we should aim for removing any dependency of |
@michael-s-molina I agree, I think ideally we want to have a close relationship with I'll add the deprecation notice. For 5.0 we can simply return the query as-is for DB engine specs without a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
SUMMARY
An improvement to #30350.
Today, when a DB engine spec has no corresponding
sqlglot
dialect, we use a generic one to parse queries, which works fine. When converting the AST back to a string to pretty-print SQL, though, we hit a problem: in some databases the query becomes invalid. For example, this Firebolt query:Gets formatted to:
While these two queries are equivalent, the latter is invalid in Firebolt:
The long term solution is to add a Firebolt dialect to
sqlglot
, at least handling this edge case. As a more generic solution, this PR implements logic where, when a DB engine spec doesn't have a corresponding dialect insqlglot
, the query is pretty-printed usingsqlparse.format
. This way we have the strictness and correctness ofsqlglot
when parsing queries, and the leniency ofsqlparse
when formatting them.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A
TESTING INSTRUCTIONS
Added unit tests covering the problem.
ADDITIONAL INFORMATION