Skip to content

Commit b9a64d6

Browse files
Byunkmacbre
andauthored
* fix #394 and add test * fix #390 and add test * fix #377 and add test * fix 'NoneType' object has no attribute 'normalized' * formatted by black * remove unnecessaries --------- Co-authored-by: Maciej Brencz <maciej.brencz@gmail.com>
1 parent c2b9188 commit b9a64d6

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

sql_metadata/generalizator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def without_comments(self) -> str:
4747
:rtype: str
4848
"""
4949
sql = sqlparse.format(self._raw_query, strip_comments=True)
50-
sql = re.sub(r"\s{2,}", " ", sql)
50+
sql = sql.replace("\n", " ")
51+
sql = re.sub(r"[ \t]+", " ", sql)
5152
return sql
5253

5354
@property

sql_metadata/parser.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,12 +533,16 @@ def subqueries(self) -> Dict:
533533
):
534534
current_subquery.append(inner_token)
535535
inner_token = inner_token.next_token
536+
537+
query_name = None
536538
if inner_token.next_token.value in self.subqueries_names:
537539
query_name = inner_token.next_token.value
538-
else:
540+
elif inner_token.next_token.is_as_keyword:
539541
query_name = inner_token.next_token.next_token.value
542+
540543
subquery_text = "".join([x.stringified_token for x in current_subquery])
541-
subqueries[query_name] = subquery_text
544+
if query_name is not None:
545+
subqueries[query_name] = subquery_text
542546

543547
token = token.next_token
544548

@@ -622,7 +626,7 @@ def without_comments(self) -> str:
622626
"""
623627
Removes comments from SQL query
624628
"""
625-
return Generalizator(self.query).without_comments
629+
return Generalizator(self._raw_query).without_comments
626630

627631
@property
628632
def generalize(self) -> str:
@@ -865,7 +869,7 @@ def _determine_opening_parenthesis_type(self, token: SQLToken):
865869
# we are in columns and in a column subquery definition
866870
token.is_column_definition_start = True
867871
elif (
868-
token.previous_token.is_as_keyword
872+
token.previous_token_not_comment.is_as_keyword
869873
and token.last_keyword_normalized != "WINDOW"
870874
):
871875
# window clause also contains AS keyword, but it is not a query

sql_metadata/token.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,15 @@ def next_token_not_comment(self):
369369
return self.next_token.next_token_not_comment
370370
return self.next_token
371371

372+
@property
373+
def previous_token_not_comment(self):
374+
"""
375+
Property returning previous non-comment token
376+
"""
377+
if self.previous_token and self.previous_token.is_comment:
378+
return self.previous_token.previous_token_not_comment
379+
return self.previous_token
380+
372381
def is_constraint_definition_inside_create_table_clause(
373382
self, query_type: str
374383
) -> bool:

test/test_comments.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,11 @@ def test_next_token_not_comment_on_non_comments():
205205
select_tok.next_token.next_token
206206
== select_tok.next_token_not_comment.next_token_not_comment
207207
)
208+
209+
210+
def test_without_comments_for_multiline_query():
211+
query = """SELECT * -- comment
212+
FROM table
213+
WHERE table.id = '123'"""
214+
parser = Parser(query)
215+
assert parser.without_comments == """SELECT * FROM table WHERE table.id = '123'"""

test/test_getting_columns.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,14 @@ def test_columns_with_comments():
264264
"order_by": ["cl_sortkey"],
265265
}
266266

267+
parser = Parser(
268+
"""WITH aa AS --sdfsdfsdf
269+
(SELECT C1, C2 FROM T1)
270+
SELECT C1, C2 FROM aa"""
271+
)
272+
assert parser.columns == ["C1", "C2"]
273+
assert parser.columns_dict == {"select": ["C1", "C2"]}
274+
267275

268276
def test_columns_with_keyword_aliases():
269277
parser = Parser(
@@ -477,3 +485,37 @@ def test_having_columns():
477485
"group_by": ["Country"],
478486
"having": ["CustomerID"],
479487
}
488+
489+
490+
def test_nested_queries():
491+
query = """
492+
SELECT max(dt) FROM
493+
(
494+
SELECT max(dt) as dt FROM t
495+
UNION ALL
496+
SELECT max(dt) as dt FROM t2
497+
)
498+
"""
499+
parser = Parser(query)
500+
assert parser.columns == ["dt"]
501+
assert parser.columns_dict == {"select": ["dt"]}
502+
503+
query = """
504+
SELECT max(dt) FROM
505+
(
506+
SELECT max(dt) as dt FROM t
507+
)
508+
"""
509+
parser = Parser(query)
510+
assert parser.columns == ["dt"]
511+
assert parser.columns_dict == {"select": ["dt"]}
512+
513+
query = """
514+
SELECT max(dt) FROM
515+
(
516+
SELECT dt FROM t
517+
)
518+
"""
519+
parser = Parser(query)
520+
assert parser.columns == ["dt"]
521+
assert parser.columns_dict == {"select": ["dt"]}

0 commit comments

Comments
 (0)