From e954bce2c80a97e681e633c737479e7caa172c02 Mon Sep 17 00:00:00 2001 From: adityajai119 Date: Tue, 1 Dec 2026 20:15:46 +0530 Subject: [PATCH] Recognize CTEs declared with WITH RECURSIVE --- AUTHORS | 1 + changelog.rst | 2 ++ pgcli/packages/parseutils/ctes.py | 2 ++ tests/parseutils/test_ctes.py | 10 ++++++++++ 4 files changed, 15 insertions(+) diff --git a/AUTHORS b/AUTHORS index d0b4a5cee..c3a5a00f4 100644 --- a/AUTHORS +++ b/AUTHORS @@ -159,6 +159,7 @@ Contributors: * Pieter Ouwerkerk (pouwerkerk) * VXNCXNX * Wei-Lun Li (b84330808) + * adityajai119 * jackwalkerlabs (Codex-assisted) diff --git a/changelog.rst b/changelog.rst index a1ade6bc2..0f5dc0946 100644 --- a/changelog.rst +++ b/changelog.rst @@ -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: --------- diff --git a/pgcli/packages/parseutils/ctes.py b/pgcli/packages/parseutils/ctes.py index 978a3211f..7395c6d3a 100644 --- a/pgcli/packages/parseutils/ctes.py +++ b/pgcli/packages/parseutils/ctes.py @@ -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) diff --git a/tests/parseutils/test_ctes.py b/tests/parseutils/test_ctes.py index 3e89ccafe..0ffbee39e 100644 --- a/tests/parseutils/test_ctes.py +++ b/tests/parseutils/test_ctes.py @@ -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"