Skip to content

Commit 277d744

Browse files
authored
SNOW-733718 Fix remove_comments when comments are at end of line (#1474)
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
1 parent f0a38d9 commit 277d744

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/snowflake/connector/util_text.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ def split_statements(
146146
if not remove_comments:
147147
# keep the comment
148148
statement.append((line[col:], False))
149+
else:
150+
statement.append(("\n", True))
149151
col = len_line + 1
150152
col0 = col
151153
elif line[col:].startswith("/*") and not line[col0:].startswith(

test/unit/test_split_statement.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from __future__ import annotations
77

88
from io import StringIO
9+
from textwrap import dedent
910

1011
import pytest
1112

@@ -499,6 +500,31 @@ def test_comments_with_semicolon():
499500
next(itr)
500501

501502

503+
@pytest.mark.skipif(split_statements is None, reason="No split_statements is available")
504+
def test_remove_comments_end_of_line():
505+
with StringIO(
506+
dedent(
507+
"""\
508+
select L_ORDERKEY,
509+
L_SHIPINSTRUCT-- this is a comment
510+
from lineitem
511+
where L_ORDERKEY=5795896006;
512+
"""
513+
)
514+
) as f:
515+
itr = split_statements(f, remove_comments=True)
516+
expected_sql = dedent(
517+
"""\
518+
select L_ORDERKEY,
519+
L_SHIPINSTRUCT
520+
from lineitem
521+
where L_ORDERKEY=5795896006;"""
522+
)
523+
assert next(itr) == (expected_sql, False)
524+
with pytest.raises(StopIteration):
525+
next(itr)
526+
527+
502528
@pytest.mark.skipif(split_statements is None, reason="No split_statements is available")
503529
def test_comment_in_values():
504530
"""SNOW-51297: SnowSQL -o remove_comments=True breaks the query."""

0 commit comments

Comments
 (0)