Skip to content

Complete columns from MATERIALIZED and data-modifying CTEs, and output aliases in ORDER BY #3608

Closed

Description

@datlechin

Problem

Autocomplete reads a CTE's columns from its SELECT list (features/autocomplete.mdx:84-103). The parser behind it, apple/Packages/TableProMacCore/Sources/TableProAutocomplete/DerivedTableParser.swift, misses several common PostgreSQL shapes, and ORDER BY offers less than the server accepts:

  1. WITH recent AS MATERIALIZED (SELECT id, total FROM orders) SELECT r.| FROM recent r offers no columns. Only a parenthesis right after AS is read as a CTE body (line 88), so AS MATERIALIZED and AS NOT MATERIALIZED, PostgreSQL 12 and later, are skipped.
  2. WITH moved AS (DELETE FROM queue WHERE done RETURNING id, payload) SELECT m.| FROM moved m offers no columns. A body has to begin with SELECT (lines 318-322), so UPDATE, INSERT and DELETE ... RETURNING bodies are ignored, and so is a body that starts with VALUES or a nested WITH.
  3. In SELECT price * qty AS line_total FROM items ORDER BY li|, line_total is not offered. ORDER BY lists the FROM tables' columns and keywords only (apple/TablePro/Core/Autocomplete/SQLCompletionProvider.swift:321-327), though PostgreSQL, MySQL and SQLite all accept an output alias there.

The parser's tests (apple/Packages/TableProMacCore/Tests/TableProAutocompleteTests/DerivedTableParserTests.swift) cover plain, explicit-column and multiple CTEs, and none of the shapes above.

Proposed solution

  • Read AS MATERIALIZED ( and AS NOT MATERIALIZED ( as CTE bodies.
  • Take a data-modifying CTE's columns from its RETURNING list, and resolve RETURNING * to the target table's columns from the schema cache.
  • Use a CTE's declared column list for a VALUES body.
  • In ORDER BY, offer the SELECT list's aliases ahead of the table columns.
  • Add a test for completion inside a CTE's own statement, such as WITH u AS (UPDATE orders SET st| ...) offering the columns of orders, and fix it if it fails.

Done when: each case above has a test in the autocomplete test target, and all of them pass.

Alternatives considered

Type the column names by hand, or draft the CTE as a plain SELECT first and change it afterwards.

Platform

macOS

Related database type

PostgreSQL

Activity

  1. added
    enhancement
    platform-macos
    on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementplatform-macos

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions