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

Fix bug #2731 on stripping query comments for snowflake #2974

Merged
merged 5 commits into from
Jan 21, 2021
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

### Features
- Add optional configs for `require_partition_filter` and `partition_expiration_days` in BigQuery ([#1843](https://github.com/fishtown-analytics/dbt/issues/1843), [#2928](https://github.com/fishtown-analytics/dbt/pull/2928))
- Fix for EOL SQL comments prevent entire line execution ([#2731](https://github.com/fishtown-analytics/dbt/issues/2731), [#2974](https://github.com/fishtown-analytics/dbt/pull/2974))

Contributors:
- [@yu-iskw](https://github.com/yu-iskw) ([#2928](https://github.com/fishtown-analytics/dbt/pull/2928))
- [@rvacaru](https://github.com/rvacaru) ([#2974](https://github.com/fishtown-analytics/dbt/pull/2974))

## dbt 0.19.0 (Release TBD)

Expand Down Expand Up @@ -936,7 +938,6 @@ Thanks for your contributions to dbt!
- [@bastienboutonnet](https://github.com/bastienboutonnet) ([#1591](https://github.com/fishtown-analytics/dbt/pull/1591), [#1689](https://github.com/fishtown-analytics/dbt/pull/1689))



## dbt 0.14.0 - Wilt Chamberlain (July 10, 2019)

### Overview
Expand Down
4 changes: 3 additions & 1 deletion plugins/snowflake/dbt/adapters/snowflake/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,9 @@ def add_query(self, sql, auto_begin=True,
# empty queries. this avoids using exceptions as flow control,
# and also allows us to return the status of the last cursor
without_comments = re.sub(
re.compile('^.*(--.*)$', re.MULTILINE),
re.compile(
r'(\".*?\"|\'.*?\')|(/\*.*?\*/|--[^\r\n]*$)', re.MULTILINE
),
'', individual_query).strip()

if without_comments == "":
Expand Down
46 changes: 46 additions & 0 deletions test/unit/test_snowflake_adapter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import agate
import re
import unittest
from contextlib import contextmanager
from unittest import mock
Expand Down Expand Up @@ -523,3 +524,48 @@ def test_float_from_description(self):
assert col.is_numeric() is False
assert col.is_string() is False
assert col.is_integer() is False


class SnowflakeConnectionsTest(unittest.TestCase):
def test_comment_stripping_regex(self):
pattern = r'(\".*?\"|\'.*?\')|(/\*.*?\*/|--[^\r\n]*$)'
comment1 = '-- just comment'
comment2 = '/* just comment */'
query1 = 'select 1; -- comment'
query2 = 'select 1; /* comment */'
query3 = 'select 1; -- comment\nselect 2; /* comment */ '
query4 = 'select \n1; -- comment\nselect \n2; /* comment */ '
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add one more unit test that represents the original error case reported in #2731:

query5 = 'select 1; -- comment \nselect 2; -- comment \nselect 3; -- comment'
expected_query_5 = 'select 1; \nselect 2; \nselect 3;'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the extra test. Let me know if there's anything more

query5 = 'select 1; -- comment \nselect 2; -- comment \nselect 3; -- comment'

stripped_comment1 = re.sub(re.compile(pattern, re.MULTILINE),
'', comment1).strip()

stripped_comment2 = re.sub(re.compile(pattern, re.MULTILINE),
'', comment2).strip()

stripped_query1 = re.sub(re.compile(pattern, re.MULTILINE),
'', query1).strip()

stripped_query2 = re.sub(re.compile(pattern, re.MULTILINE),
'', query2).strip()

stripped_query3 = re.sub(re.compile(pattern, re.MULTILINE),
'', query3).strip()

stripped_query4 = re.sub(re.compile(pattern, re.MULTILINE),
'', query4).strip()

stripped_query5 = re.sub(re.compile(pattern, re.MULTILINE),
'', query5).strip()

expected_query_3 = 'select 1; \nselect 2;'
expected_query_4 = 'select \n1; \nselect \n2;'
expected_query_5 = 'select 1; \nselect 2; \nselect 3;'

self.assertEqual('', stripped_comment1)
self.assertEqual('', stripped_comment2)
self.assertEqual('select 1;', stripped_query1)
self.assertEqual('select 1;', stripped_query2)
self.assertEqual(expected_query_3, stripped_query3)
self.assertEqual(expected_query_4, stripped_query4)
self.assertEqual(expected_query_5, stripped_query5)