Skip to content

Commit e68bdad

Browse files
authored
Suggest columns after GROUP BY (#1599)
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 <tab>` 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.
1 parent 0797904 commit e68bdad

4 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎AUTHORS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ Contributors:
148148
* Charalampos Stratakis
149149
* Laszlo Bimba (bimlas)
150150
* Anjanna
151+
* Tommi Kyntölä (kynde)
151152

152153
Creator:
153154
--------

‎changelog.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Bug fixes:
2020
* Fix trailing SQL comments preventing query submission and execution.
2121
* ``SELECT 1; -- note`` now submits correctly in multiline mode
2222
* ``rstrip(";")`` in ``pgexecute.py`` now handles comments after the semicolon
23+
* Suggest columns after `GROUP BY`, like `ORDER BY` already does.
2324

2425
4.4.0 (2025-12-24)
2526
==================

‎pgcli/packages/sqlcompletion.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ def suggest_based_on_last_token(token, stmt):
383383
# E.g. 'UPDATE foo SET'
384384
return (Column(table_refs=stmt.get_tables(), local_tables=stmt.local_tables),)
385385

386-
elif token_v in ("select", "where", "having", "order by", "distinct"):
386+
elif token_v in ("select", "where", "having", "group by", "order by", "distinct"):
387387
return _suggest_expression(token_v, stmt)
388388
elif token_v == "as":
389389
# Don't suggest anything for aliases

‎tests/test_sqlcompletion.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ def test_distinct_suggests_cols(text):
221221
"SELECT * FROM tbl x JOIN tbl1 y ORDER BY ",
222222
"ORDER BY",
223223
),
224+
(
225+
"SELECT * FROM tbl x JOIN tbl1 y GROUP BY ",
226+
"SELECT * FROM tbl x JOIN tbl1 y GROUP BY ",
227+
"GROUP BY",
228+
),
224229
],
225230
)
226231
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_
247252
"SELECT * FROM tbl x JOIN tbl1 y ORDER BY x.",
248253
"SELECT * FROM tbl x JOIN tbl1 y ORDER BY x.",
249254
),
255+
(
256+
"SELECT * FROM tbl x JOIN tbl1 y GROUP BY x.",
257+
"SELECT * FROM tbl x JOIN tbl1 y GROUP BY x.",
258+
),
250259
],
251260
)
252261
def test_distinct_and_order_by_suggestions_with_alias_given(text, text_before):

0 commit comments

Comments
 (0)