Skip to content

Use the declared column list of a CTE for completion - #1671

Open
adityajai119 wants to merge 1 commit into
dbcli:mainfrom
adityajai119:fix-cte-column-list
Open

adityajai119 wants to merge 1 commit into
dbcli:mainfrom
adityajai119:fix-cte-column-list

Conversation

@adityajai119

Copy link
Copy Markdown

Description

A CTE can declare its column names, as in WITH t(a, b) AS (SELECT x, y FROM z) SELECT t. FROM t. Those names replace the ones from the CTE's query, so the columns of t are a and b. pgcli ignored the declared list and suggested x and y, which are not valid columns of t.

get_cte_from_token always read the column names from the select list inside the CTE body. sqlparse groups t(a, b) as a Function inside the CTE identifier, so when that is present the names now come from its parameters; otherwise the select list is used as before.

Added test_cte_extraction_with_column_list to tests/parseutils/test_ctes.py. It fails on main (columns come back as ('x', 'y')) and passes with the fix. tests/parseutils and tests/test_sqlcompletion.py pass, and ruff check/format are clean on the touched files.

No issue found for this; searched open issues and PRs for CTE column lists. AUTHORS already gets my name in #1670, so it is not touched here to avoid a conflict.

Checklist

  • I searched the existing issues and pull requests on this topic and linked them above (see "Before Opening a Pull Request" in CONTRIBUTING.rst).
  • I've added this contribution to the changelog.rst.
  • I've added my name to the AUTHORS file (or it's already there).
  • I installed pre-commit hooks (pip install pre-commit && pre-commit install).
  • I verified that my changes work as expected (this may include manually testing them in your local environment, or in other available environments). Cross this out if not relevant (for example, if you're making a documentation change).
  • Please squash merge this pull request (uncheck if you'd like us to merge as multiple commits)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant