Skip to content

Fix infinite loop in Parser.with_names for malformed WITH statements#605

Merged
macbre merged 2 commits intomasterfrom
fix/parser-tables-hangs
Mar 4, 2026
Merged

Fix infinite loop in Parser.with_names for malformed WITH statements#605
macbre merged 2 commits intomasterfrom
fix/parser-tables-hangs

Conversation

@macbre
Copy link
Owner

@macbre macbre commented Mar 4, 2026

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

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 <noreply@anthropic.com>
@macbre macbre self-assigned this Mar 4, 2026
@macbre macbre requested a review from collerek as a code owner March 4, 2026 20:20
@macbre macbre changed the title Fix infinite loop in Parser.with_names for malformed WITH statements Fix infinite loop in Parser.with_names for malformed WITH statements Mar 4, 2026
@macbre macbre enabled auto-merge (squash) March 4, 2026 20:21
@macbre macbre merged commit 73475ea into master Mar 4, 2026
10 checks passed
@macbre macbre deleted the fix/parser-tables-hangs branch March 4, 2026 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parser(sql).tables hangs for some cases

1 participant