Skip to content

Commit f0b0733

Browse files
Sanjays2402j-bennet
authored andcommitted
Suggest columns after a column named type (dbcli#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 dbcli#1412 Co-authored-by: Irina Truong <[email protected]>
1 parent fa67580 commit f0b0733

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎changelog.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Bug fixes:
3434
``.pgpass`` lookup now use the connstring's user instead of falling back to
3535
the OS login name (no more "Password for daf" when the connstring says
3636
``user=test2``).
37+
* Suggest columns, not datatypes, after a column literally named ``type`` in a
38+
``SELECT`` list (upstream #1611, cherry-picked).
3739
* Suggest columns after ``GROUP BY``, like ``ORDER BY`` already does
3840
(upstream #1599, cherry-picked).
3941

‎pgcli/packages/sqlcompletion.py‎

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

333333

334+
def _is_datatype_keyword(token, stmt):
335+
"""Decide whether a `type` token is the SQL TYPE keyword or a column name.
336+
337+
`type` is a non-reserved word in postgres, so sqlparse tags a column
338+
literally named "type" as a keyword. The TYPE keyword only introduces a
339+
datatype in DDL contexts (`ALTER COLUMN bar TYPE`, `SET DATA TYPE`,
340+
`CREATE TYPE`), never directly inside a DML select/insert/update list.
341+
"""
342+
if isinstance(token, str):
343+
# An explicit internal request for datatype suggestions, e.g. the
344+
# parenthesized column list in `CREATE TABLE foo (bar <CURSOR>`.
345+
return True
346+
347+
if not token.is_keyword:
348+
return False
349+
350+
prev_keyword, _ = find_prev_keyword(stmt.text_before_cursor.rstrip(), n_skip=1)
351+
if prev_keyword is None:
352+
return False
353+
354+
return prev_keyword.ttype is not sqlparse.tokens.Keyword.DML
355+
356+
334357
def suggest_based_on_last_token(token, stmt):
335358
if isinstance(token, str):
336359
token_v = token.lower()
@@ -553,7 +576,7 @@ def suggest_based_on_last_token(token, stmt):
553576
return suggest_based_on_last_token(prev_keyword, stmt)
554577
else:
555578
return ()
556-
elif token_v in ("type", "::"):
579+
elif token_v == "::" or (token_v == "type" and _is_datatype_keyword(token, stmt)):
557580
# ALTER TABLE foo SET DATA TYPE bar
558581
# SELECT foo::bar
559582
# 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
@@ -801,6 +801,23 @@ def test_alter_column_type_suggests_types():
801801
}
802802

803803

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

0 commit comments

Comments
 (0)