diff --git a/internal/compiler/output_columns.go b/internal/compiler/output_columns.go index 8330f3f269..badec377b2 100644 --- a/internal/compiler/output_columns.go +++ b/internal/compiler/output_columns.go @@ -3,6 +3,7 @@ package compiler import ( "errors" "fmt" + "github.com/kyleconroy/sqlc/internal/sql/catalog" "github.com/kyleconroy/sqlc/internal/sql/ast" @@ -202,6 +203,16 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) { switch n.SubLinkType { case ast.EXISTS_SUBLINK: cols = append(cols, &Column{Name: name, DataType: "bool", NotNull: true}) + case ast.EXPR_SUBLINK: + subcols, err := outputColumns(qc, n.Subselect) + if err != nil { + return nil, err + } + first := subcols[0] + if res.Name != nil { + first.Name = *res.Name + } + cols = append(cols, first) default: cols = append(cols, &Column{Name: name, DataType: "any", NotNull: false}) } diff --git a/internal/endtoend/testdata/select_nested_count/postgresql/go/db.go b/internal/endtoend/testdata/select_nested_count/postgresql/go/db.go new file mode 100644 index 0000000000..6a99519302 --- /dev/null +++ b/internal/endtoend/testdata/select_nested_count/postgresql/go/db.go @@ -0,0 +1,29 @@ +// Code generated by sqlc. DO NOT EDIT. + +package querytest + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/select_nested_count/postgresql/go/models.go b/internal/endtoend/testdata/select_nested_count/postgresql/go/models.go new file mode 100644 index 0000000000..c1f8631d13 --- /dev/null +++ b/internal/endtoend/testdata/select_nested_count/postgresql/go/models.go @@ -0,0 +1,19 @@ +// Code generated by sqlc. DO NOT EDIT. + +package querytest + +import ( + "database/sql" +) + +type Author struct { + ID int64 + Name string + Bio sql.NullString +} + +type Book struct { + ID int64 + AuthorID int64 + Title string +} diff --git a/internal/endtoend/testdata/select_nested_count/postgresql/go/query.sql.go b/internal/endtoend/testdata/select_nested_count/postgresql/go/query.sql.go new file mode 100644 index 0000000000..4159cdd5ba --- /dev/null +++ b/internal/endtoend/testdata/select_nested_count/postgresql/go/query.sql.go @@ -0,0 +1,52 @@ +// Code generated by sqlc. DO NOT EDIT. +// source: query.sql + +package querytest + +import ( + "context" + "database/sql" +) + +const getAuthorsWithBooksCount = `-- name: GetAuthorsWithBooksCount :many +SELECT id, name, bio, ( + SELECT COUNT(id) FROM books + WHERE books.author_id = id +) AS books_count +FROM authors +` + +type GetAuthorsWithBooksCountRow struct { + ID int64 + Name string + Bio sql.NullString + BooksCount int64 +} + +func (q *Queries) GetAuthorsWithBooksCount(ctx context.Context) ([]GetAuthorsWithBooksCountRow, error) { + rows, err := q.db.QueryContext(ctx, getAuthorsWithBooksCount) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetAuthorsWithBooksCountRow + for rows.Next() { + var i GetAuthorsWithBooksCountRow + if err := rows.Scan( + &i.ID, + &i.Name, + &i.Bio, + &i.BooksCount, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/select_nested_count/postgresql/query.sql b/internal/endtoend/testdata/select_nested_count/postgresql/query.sql new file mode 100644 index 0000000000..11079ed4f5 --- /dev/null +++ b/internal/endtoend/testdata/select_nested_count/postgresql/query.sql @@ -0,0 +1,19 @@ +CREATE TABLE authors ( + id BIGSERIAL PRIMARY KEY, + name text NOT NULL, + bio text +); + +CREATE TABLE books ( + id BIGSERIAL PRIMARY KEY, + author_id BIGSERIAL NOT NULL + REFERENCES authors(id), + title text NOT NULL +); + +-- name: GetAuthorsWithBooksCount :many +SELECT *, ( + SELECT COUNT(id) FROM books + WHERE books.author_id = id +) AS books_count +FROM authors; diff --git a/internal/endtoend/testdata/select_nested_count/postgresql/sqlc.json b/internal/endtoend/testdata/select_nested_count/postgresql/sqlc.json new file mode 100644 index 0000000000..ac7c2ed829 --- /dev/null +++ b/internal/endtoend/testdata/select_nested_count/postgresql/sqlc.json @@ -0,0 +1,11 @@ +{ + "version": "1", + "packages": [ + { + "path": "go", + "name": "querytest", + "schema": "query.sql", + "queries": "query.sql" + } + ] +} diff --git a/internal/engine/postgresql/convert.go b/internal/engine/postgresql/convert.go index 73f2631826..6270f7e4d2 100644 --- a/internal/engine/postgresql/convert.go +++ b/internal/engine/postgresql/convert.go @@ -38,7 +38,7 @@ func convertSubLinkType(t pg.SubLinkType) (ast.SubLinkType, error) { case pg.SubLinkType_ROWCOMPARE_SUBLINK: return ast.ROWCOMPARE_SUBLINK, nil case pg.SubLinkType_EXPR_SUBLINK: - return ast.EXISTS_SUBLINK, nil + return ast.EXPR_SUBLINK, nil case pg.SubLinkType_MULTIEXPR_SUBLINK: return ast.MULTIEXPR_SUBLINK, nil case pg.SubLinkType_ARRAY_SUBLINK: @@ -2775,10 +2775,10 @@ func convertVacuumStmt(n *pg.VacuumStmt) *ast.VacuumStmt { return nil } return &ast.VacuumStmt{ - // FIXME: The VacuumStmt node has changed quite a bit - // Options: n.Options - // Relation: convertRangeVar(n.Relation), - // VaCols: convertSlice(n.VaCols), + // FIXME: The VacuumStmt node has changed quite a bit + // Options: n.Options + // Relation: convertRangeVar(n.Relation), + // VaCols: convertSlice(n.VaCols), } }