From ee59f7401bc658deb917dc3de4ef2f4b550688ca Mon Sep 17 00:00:00 2001 From: Tommi Kyntola Date: Mon, 1 Jun 2026 23:43:31 +0300 Subject: [PATCH] Suggest columns after GROUP BY The completion engine offers columns after SELECT, WHERE, HAVING, ORDER BY and DISTINCT, but GROUP BY was missing from that list, so `SELECT ... FROM tbl GROUP BY ` fell through to FROM-clause handling and suggested tables/schemas instead of columns. Add "group by" to the keyword tuple so it suggests columns like ORDER BY already does, and extend the existing alias tests to cover it. --- AUTHORS | 1 + changelog.rst | 1 + pgcli/packages/sqlcompletion.py | 2 +- tests/test_sqlcompletion.py | 9 +++++++++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index bf9a3b226..1389bcf0b 100644 --- a/AUTHORS +++ b/AUTHORS @@ -147,6 +147,7 @@ Contributors: * Devadathan M B (devadathanmb) * Charalampos Stratakis * Laszlo Bimba (bimlas) + * Tommi Kyntölä (kynde) Creator: -------- diff --git a/changelog.rst b/changelog.rst index 78e8ee394..4e1f699d4 100644 --- a/changelog.rst +++ b/changelog.rst @@ -16,6 +16,7 @@ Bug fixes: * Add `VERSION` to built-in function completion so `SELECT VERSION();` is suggested. * Hide timezone notice at startup when local and server timezones are the same. * Let `sqlparse` accept arbitrarily-large queries. +* Suggest columns after `GROUP BY`, like `ORDER BY` already does. 4.4.0 (2025-12-24) ================== diff --git a/pgcli/packages/sqlcompletion.py b/pgcli/packages/sqlcompletion.py index 88b8c0a64..d8996609b 100644 --- a/pgcli/packages/sqlcompletion.py +++ b/pgcli/packages/sqlcompletion.py @@ -383,7 +383,7 @@ def suggest_based_on_last_token(token, stmt): # E.g. 'UPDATE foo SET' return (Column(table_refs=stmt.get_tables(), local_tables=stmt.local_tables),) - elif token_v in ("select", "where", "having", "order by", "distinct"): + elif token_v in ("select", "where", "having", "group by", "order by", "distinct"): return _suggest_expression(token_v, stmt) elif token_v == "as": # Don't suggest anything for aliases diff --git a/tests/test_sqlcompletion.py b/tests/test_sqlcompletion.py index 028170d58..f9fe9ca1a 100644 --- a/tests/test_sqlcompletion.py +++ b/tests/test_sqlcompletion.py @@ -221,6 +221,11 @@ def test_distinct_suggests_cols(text): "SELECT * FROM tbl x JOIN tbl1 y ORDER BY ", "ORDER BY", ), + ( + "SELECT * FROM tbl x JOIN tbl1 y GROUP BY ", + "SELECT * FROM tbl x JOIN tbl1 y GROUP BY ", + "GROUP BY", + ), ], ) def test_distinct_and_order_by_suggestions_with_aliases(text, text_before, last_keyword): @@ -247,6 +252,10 @@ def test_distinct_and_order_by_suggestions_with_aliases(text, text_before, last_ "SELECT * FROM tbl x JOIN tbl1 y ORDER BY x.", "SELECT * FROM tbl x JOIN tbl1 y ORDER BY x.", ), + ( + "SELECT * FROM tbl x JOIN tbl1 y GROUP BY x.", + "SELECT * FROM tbl x JOIN tbl1 y GROUP BY x.", + ), ], ) def test_distinct_and_order_by_suggestions_with_alias_given(text, text_before):