Skip to content

Commit ea6c30d

Browse files
Suggest columns after a column named type (#1611)
sqlparse tags a column literally named "type" as the TYPE keyword because type is non-reserved in postgres. suggest_based_on_last_token matched that token unconditionally, so the SELECT list switched to datatype/table/schema suggestions and offered no columns for the rest of the query. The TYPE keyword only introduces a datatype in DDL contexts, so only treat it as such when the preceding keyword is not a DML statement keyword. Cast operators and parenthesized column lists are unaffected. Fixes #1412 Co-authored-by: Irina Truong <[email protected]>
1 parent 4beae47 commit ea6c30d

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎changelog.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Bug fixes:
66
* Restore cursor shape behaviour for Emacs mode
77
* Fix ``TypeError: cannot use a string pattern on a bytes-like object`` when
88
completion metadata comes back as bytes (e.g. ``SQL_ASCII`` client encoding).
9+
* Suggest columns, not datatypes, after a column literally named ``type`` in a ``SELECT`` list.
910

1011
Features:
1112
---------

‎pgcli/packages/sqlcompletion.py‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,29 @@ def suggest_special(text):
278278
return (Keyword(), Special())
279279

280280

281+
def _is_datatype_keyword(token, stmt):
282+
"""Decide whether a `type` token is the SQL TYPE keyword or a column name.
283+
284+
`type` is a non-reserved word in postgres, so sqlparse tags a column
285+
literally named "type" as a keyword. The TYPE keyword only introduces a
286+
datatype in DDL contexts (`ALTER COLUMN bar TYPE`, `SET DATA TYPE`,
287+
`CREATE TYPE`), never directly inside a DML select/insert/update list.
288+
"""
289+
if isinstance(token, str):
290+
# An explicit internal request for datatype suggestions, e.g. the
291+
# parenthesized column list in `CREATE TABLE foo (bar <CURSOR>`.
292+
return True
293+
294+
if not token.is_keyword:
295+
return False
296+
297+
prev_keyword, _ = find_prev_keyword(stmt.text_before_cursor.rstrip(), n_skip=1)
298+
if prev_keyword is None:
299+
return False
300+
301+
return prev_keyword.ttype is not sqlparse.tokens.Keyword.DML
302+
303+
281304
def suggest_based_on_last_token(token, stmt):
282305
if isinstance(token, str):
283306
token_v = token.lower()
@@ -492,7 +515,7 @@ def suggest_based_on_last_token(token, stmt):
492515
return suggest_based_on_last_token(prev_keyword, stmt)
493516
else:
494517
return ()
495-
elif token_v in ("type", "::"):
518+
elif token_v == "::" or (token_v == "type" and _is_datatype_keyword(token, stmt)):
496519
# ALTER TABLE foo SET DATA TYPE bar
497520
# SELECT foo::bar
498521
# Note that tables are a form of composite type in postgresql, so

‎tests/test_sqlcompletion.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,23 @@ def test_alter_column_type_suggests_types():
798798
}
799799

800800

801+
@pytest.mark.parametrize(
802+
"text",
803+
[
804+
"SELECT type ",
805+
"SELECT type, ",
806+
"SELECT id, type, ",
807+
],
808+
)
809+
def test_column_named_type_still_suggests_columns(text):
810+
# `type` is a non-reserved word, so sqlparse tags a column literally
811+
# named "type" as the TYPE keyword; it must not switch the SELECT list
812+
# over to datatype suggestions.
813+
suggestions = suggest_type(text, text)
814+
assert Column in {type(s) for s in suggestions}
815+
assert Datatype(schema=None) not in suggestions
816+
817+
801818
@pytest.mark.parametrize(
802819
"text",
803820
[

0 commit comments

Comments
 (0)