-
Notifications
You must be signed in to change notification settings - Fork 16.3k
Discard semicolon stripping in SQL hook #25855
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
Discard semicolon stripping in SQL hook #25855
Conversation
| splits = sqlparse.split(sqlparse.format(sql, strip_comments=True)) | ||
| statements: List[str] = list( | ||
| filter(None, [s.rstrip(';').strip() if s.endswith(';') else s.strip() for s in splits]) | ||
| ) |
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.
The sqlparse.split already has .strip:
return [str(stmt).strip() for stmt in stack.run(sql, encoding)]
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.
But this filtration is necessary I think:
splits = ["SELECT ...", None, ""]
list(filter(None, splits))
Out[3]: ['SELECT ...']
|
Fixing tests. There're some issues |
| (' SELECT * FROM table ; # comment;', ['SELECT * FROM table']), | ||
| ('SELECT * FROM table;;;;;', ['SELECT * FROM table']), | ||
| ('SELECT * FROM table;;# comment;;;', ['SELECT * FROM table']), | ||
| ('SELECT * FROM table;;# comment;; ;', ['SELECT * FROM table']), |
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.
Why did I remove these test cases?
In the previous PR, some functionality was added that tried to make SQL query clearer.
But also it caused a few bugs. I think I tried to do some jobs for a developer and it was a bad idea.
I think we could add some functionality like split_statements but the developer should decide either to use it or not. But with semicolon stripping it wasn't that.
|
Cool. Thanks!. A lot of learnings from the common.sql exercise :) |
Motivation:
Some databases (at least Oracle) need mandatory
;at the end of the SQL query.Also, possibly semicolon stripping might affect T-SQL statements.
How to fix it?
strip_semicolonand set it toFalseby default in OracleI think the second solution is better because basically it returns previous behavior for the hook and does not add additional complexity.
closes: #25851