Skip to content

Commit

Permalink
Merge pull request #2974 from rvacaru/fix-bug-2731
Browse files Browse the repository at this point in the history
Fix bug #2731 on stripping query comments for snowflake
  • Loading branch information
jtcohen6 authored Jan 21, 2021
2 parents 1f927a3 + 04bd0d8 commit a34a877
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
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 */ '
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)

0 comments on commit a34a877

Please sign in to comment.