Skip to content

SNOW-733718 Fix remove_comments when comments are at end of line #1474

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

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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