Skip to content
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

Merged
merged 2 commits into from
Aug 21, 2022

Conversation

kazanzhy
Copy link
Contributor

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?

  1. Add optional strip_semicolon and set it to False by default in Oracle
  2. Remove stripping for all cases.

I think the second solution is better because basically it returns previous behavior for the hook and does not add additional complexity.

closes: #25851

@@ -244,9 +240,7 @@ def split_sql_string(sql: str) -> List[str]:
:return: list of individual expressions
"""
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])
)
Copy link
Contributor Author

@kazanzhy kazanzhy Aug 21, 2022

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)]

Copy link
Contributor Author

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 ...']

@kazanzhy
Copy link
Contributor Author

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']),
Copy link
Contributor Author

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.

@potiuk potiuk merged commit 874a95c into apache:main Aug 21, 2022
@potiuk
Copy link
Member

potiuk commented Aug 21, 2022

Cool. Thanks!. A lot of learnings from the common.sql exercise :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PL/SQL statement stop working after upgrade common-sql to 1.1.0
3 participants