Skip to content

Commit

Permalink
SNOW-733718 Fix remove_comments when comments are at end of line (#1474)
Browse files Browse the repository at this point in the history
Description

Before the fix, the remove_comments operation will swallow the final
line break, which breaks user query if there is no spaces between
two lines.

Testing

Added a new unit test
  • Loading branch information
sfc-gh-sfan authored Mar 14, 2023
1 parent f0a38d9 commit 277d744
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/snowflake/connector/util_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ def split_statements(
if not remove_comments:
# keep the comment
statement.append((line[col:], False))
else:
statement.append(("\n", True))
col = len_line + 1
col0 = col
elif line[col:].startswith("/*") and not line[col0:].startswith(
Expand Down
26 changes: 26 additions & 0 deletions test/unit/test_split_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from __future__ import annotations

from io import StringIO
from textwrap import dedent

import pytest

Expand Down Expand Up @@ -499,6 +500,31 @@ def test_comments_with_semicolon():
next(itr)


@pytest.mark.skipif(split_statements is None, reason="No split_statements is available")
def test_remove_comments_end_of_line():
with StringIO(
dedent(
"""\
select L_ORDERKEY,
L_SHIPINSTRUCT-- this is a comment
from lineitem
where L_ORDERKEY=5795896006;
"""
)
) as f:
itr = split_statements(f, remove_comments=True)
expected_sql = dedent(
"""\
select L_ORDERKEY,
L_SHIPINSTRUCT
from lineitem
where L_ORDERKEY=5795896006;"""
)
assert next(itr) == (expected_sql, False)
with pytest.raises(StopIteration):
next(itr)


@pytest.mark.skipif(split_statements is None, reason="No split_statements is available")
def test_comment_in_values():
"""SNOW-51297: SnowSQL -o remove_comments=True breaks the query."""
Expand Down

0 comments on commit 277d744

Please sign in to comment.