You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Complete columns from MATERIALIZED and data-modifying CTEs, and output aliases in ORDER BY #3608
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:
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.
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.
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.
Activity