Skip to content
Open
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Contributors:
* Pieter Ouwerkerk (pouwerkerk)
* VXNCXNX
* Wei-Lun Li (b84330808)
* adityajai119

* jackwalkerlabs (Codex-assisted)

Expand Down
2 changes: 2 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Bug fixes:
created by ``uv venv``.
* Read ``alias_map_file`` as UTF-8 (with or without a BOM) instead of the
locale encoding, which made non-ASCII aliases fail on Windows.
* Suggest the columns of CTEs declared with ``WITH RECURSIVE``, which were
not recognized as CTEs at all.

Internal:
---------
Expand Down
2 changes: 2 additions & 0 deletions pgcli/packages/parseutils/ctes.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def extract_ctes(sql):

# Get the next (meaningful) token, which should be the first CTE
idx, tok = p.token_next(idx)
if tok and tok.match(Keyword, "RECURSIVE"):
idx, tok = p.token_next(idx)
if not tok:
return ([], "")
start_pos = token_start_pos(p.tokens, idx)
Expand Down
10 changes: 10 additions & 0 deletions tests/parseutils/test_ctes.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,13 @@ def test_multiple_cte_extraction():
("x", ("abc", "def"), start1, stop1),
("y", ("ghi", "jkl"), start2, stop2),
)


def test_recursive_cte_extraction():
sql = "WITH RECURSIVE a AS (SELECT abc FROM xxx) SELECT * FROM a"
start_pos = len("WITH RECURSIVE a AS ")
stop_pos = len("WITH RECURSIVE a AS (SELECT abc FROM xxx)")
ctes, remainder = extract_ctes(sql)

assert tuple(ctes) == (("a", ("abc",), start_pos, stop_pos),)
assert remainder.strip() == "SELECT * FROM a"
Loading