Skip to content

Commit ae7c0fb

Browse files
macbreclaude
andcommitted
Fix infinite loop in Parser.with_names for malformed WITH statements
Resolves #556. The parser would hang indefinitely when encountering malformed SQL with consecutive AS keywords in WITH clauses (e.g., "WITH a AS (...) AS g"). The issue was in the with_names property loop: after processing a WITH clause, if the next token was another AS keyword (invalid syntax), the parser would not advance the token, causing an infinite loop. This fix: - Detects malformed SQL with consecutive AS keywords after WITH queries - Raises ValueError("This query is wrong") instead of hanging - Ensures the token always advances to prevent infinite loops - Adds test case to verify the fix Co-Authored-By: Claude <[email protected]>
1 parent 1fbfee4 commit ae7c0fb

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

sql_metadata/parser.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ def tables_aliases(self) -> Dict[str, str]:
447447
return self._table_aliases
448448

449449
@property
450-
def with_names(self) -> List[str]:
450+
def with_names(self) -> List[str]: # noqa: C901
451451
"""
452452
Returns with statements aliases list from a given query
453453
@@ -474,6 +474,12 @@ def with_names(self) -> List[str]:
474474
)
475475
if is_end_of_with_block:
476476
self._is_in_with_block = False
477+
elif token.next_token and token.next_token.is_as_keyword:
478+
# Malformed SQL like "... AS (...) AS ..."
479+
raise ValueError("This query is wrong")
480+
else:
481+
# Advance token to prevent infinite loop
482+
token = token.next_token
477483
else:
478484
token = token.next_token
479485

test/test_with_statements.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,15 @@ def test_as_was_preceded_by_with_query():
526526
parser = Parser(query)
527527
with pytest.raises(ValueError, match="This query is wrong"):
528528
parser.tables
529+
530+
531+
def test_malformed_with_query_hang():
532+
# Test for issue #556 - malformed WITH query causes infinite loop
533+
# https://github.com/macbre/sql-metadata/issues/556
534+
query = """WITH a AS (SELECT MAX(b) AS c
535+
FROM d
536+
WHERE domain =e''$.f') AS g
537+
FROM h;"""
538+
parser = Parser(query)
539+
with pytest.raises(ValueError, match="This query is wrong"):
540+
parser.tables

0 commit comments

Comments
 (0)