Skip to content

postgresql: infer the type of ARRAY(SELECT ...) subqueries - #4716

Open
takiren wants to merge 2 commits into
sqlc-dev:mainfrom
takiren:array-sublink-type
Open

takiren wants to merge 2 commits into
sqlc-dev:mainfrom
takiren:array-sublink-type

Conversation

@takiren

@takiren takiren commented Nov 30, 2026 •

Copy link
Copy Markdown

closes #4728

ARRAY(SELECT ...) in a PostgreSQL query used to come back as interface{}. It now takes the type of the subquery's column, as an array, the same way the analysis core already types it (internal/core/analyzer/expr.go).

-- name: ListSingerAlbums :many
SELECT id, ARRAY(SELECT album_id FROM albums WHERE albums.singer_id = singers.id) AS titles
FROM singers;

type ListSingerAlbumsRow struct {
        ID     int64
        Titles []int64 // was interface{}
}

Breaking: an unaliased ARRAY(...) column is now named array, as PostgreSQL names it. It was previously named exists, so its field changes from Exists interface{} to Array []T.

The new end-to-end case also exposed a formatter bug: SubLink.Format printed ARRAY(SELECT ...) as (SELECT ...). The second commit fixes it.

Testing

- New case select_array_subquery/postgresql/{pgx/v5,stdlib}, with and without an alias.
- go test --tags=examples ./internal/endtoend passes against PostgreSQL 16, including managed-db and TestFormat. The examples/ module was not run.
ARRAY(SELECT col ...) was typed as interface{}. It now takes the
subquery's single column as its element type, so
ARRAY(SELECT album_id FROM albums ...) becomes []int64.

An unaliased ARRAY subquery is named "array", as PostgreSQL names it,
instead of "exists". This changes the generated field name for
queries that relied on the old name.
@takiren
takiren marked this pull request as ready for review November 30, 2026 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant