You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks for raising the issue.
We are able to reproduce the issue. will look into it.
Meanwhile as a workaround, you can either use parameter remove_comments=True or remove the empty lines from the multi SQL statement before processing.
Example: using remove_comments=True
`sql_script = """
-- This is a middle comment;
select count() from mycsvtable;
select count() from mycsvtable1;
-- Comment still comment;
-- Extra comment;
-- Another comment;
"""
try:
sql_stream = StringIO(sql_script)
with conn.cursor() as cur:
for result_cursor in conn.execute_stream(sql_stream,remove_comments=True):
for result in result_cursor:
print(f"Result: {result}")
finally:
conn.close()`
Exmaple : by removing the empty lines
`sql_script = """
-- This is a middle comment;
select count() from mycsvtable;
select count() from mycsvtable1;
-- Comment still comment;
-- Extra comment;
-- Another comment;
"""
try:
cleaned_script = "\n".join(
line for line in sql_script.splitlines()
if line.strip() and not line.strip().startswith("--")
)
sql_stream = StringIO(cleaned_script)
with conn.cursor() as cur:
for result_cursor in conn.execute_stream(sql_stream,remove_comments=False):
for result in result_cursor:
print(f"Result: {result}")
We discussed this internally with the team and concluded that it's not recommended to use the feature; the recommendation is either to parse the SQL string before using it or please use it with the parameter remove_commetns=True. We can only improve this feature, fixing this will result in another example that will break it, so the recommendations if you want to use this feature please use with remove_comments=True or else please parse the SQL string before using it.
Thanks Sujan. If remove_comments has to be True for the function to function properly, I suggest always removing the comments. Otherwise, this could cause issues for other users as well.
Python version
Python 3.10.0 (default, Mar 3 2022, 03:57:21) [Clang 12.0.0 ]
Operating system and processor architecture
macOS-10.16-x86_64-i386-64bit
Installed packages
What did you do?
Try this stream with execute_stream() in connection.py:
Basically anything with a comment at the end of the stream will cause this error:
Empty SQL statement.
If you look closely at the code, it seems that the issue is in split_statements() call, which is resulting in:
It splits the statements correctly, except that the last statement is not really a statement as it includes comments only.
What did you expect to see?
Expected for the code to execute successfully.
Can you set logging to DEBUG and collect the logs?
The text was updated successfully, but these errors were encountered: