Skip to content

Commit 6617cff

Browse files
authored
compiler: Handle subqueries in SELECT statements (#1113)
1 parent 6928f2e commit 6617cff

File tree

7 files changed

+146
-5
lines changed

7 files changed

+146
-5
lines changed

internal/compiler/output_columns.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package compiler
33
import (
44
"errors"
55
"fmt"
6+
67
"github.com/kyleconroy/sqlc/internal/sql/catalog"
78

89
"github.com/kyleconroy/sqlc/internal/sql/ast"
@@ -202,6 +203,16 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
202203
switch n.SubLinkType {
203204
case ast.EXISTS_SUBLINK:
204205
cols = append(cols, &Column{Name: name, DataType: "bool", NotNull: true})
206+
case ast.EXPR_SUBLINK:
207+
subcols, err := outputColumns(qc, n.Subselect)
208+
if err != nil {
209+
return nil, err
210+
}
211+
first := subcols[0]
212+
if res.Name != nil {
213+
first.Name = *res.Name
214+
}
215+
cols = append(cols, first)
205216
default:
206217
cols = append(cols, &Column{Name: name, DataType: "any", NotNull: false})
207218
}

internal/endtoend/testdata/select_nested_count/postgresql/go/db.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/select_nested_count/postgresql/go/models.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/select_nested_count/postgresql/go/query.sql.go

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CREATE TABLE authors (
2+
id BIGSERIAL PRIMARY KEY,
3+
name text NOT NULL,
4+
bio text
5+
);
6+
7+
CREATE TABLE books (
8+
id BIGSERIAL PRIMARY KEY,
9+
author_id BIGSERIAL NOT NULL
10+
REFERENCES authors(id),
11+
title text NOT NULL
12+
);
13+
14+
-- name: GetAuthorsWithBooksCount :many
15+
SELECT *, (
16+
SELECT COUNT(id) FROM books
17+
WHERE books.author_id = id
18+
) AS books_count
19+
FROM authors;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"name": "querytest",
7+
"schema": "query.sql",
8+
"queries": "query.sql"
9+
}
10+
]
11+
}

internal/engine/postgresql/convert.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func convertSubLinkType(t pg.SubLinkType) (ast.SubLinkType, error) {
3838
case pg.SubLinkType_ROWCOMPARE_SUBLINK:
3939
return ast.ROWCOMPARE_SUBLINK, nil
4040
case pg.SubLinkType_EXPR_SUBLINK:
41-
return ast.EXISTS_SUBLINK, nil
41+
return ast.EXPR_SUBLINK, nil
4242
case pg.SubLinkType_MULTIEXPR_SUBLINK:
4343
return ast.MULTIEXPR_SUBLINK, nil
4444
case pg.SubLinkType_ARRAY_SUBLINK:
@@ -2775,10 +2775,10 @@ func convertVacuumStmt(n *pg.VacuumStmt) *ast.VacuumStmt {
27752775
return nil
27762776
}
27772777
return &ast.VacuumStmt{
2778-
// FIXME: The VacuumStmt node has changed quite a bit
2779-
// Options: n.Options
2780-
// Relation: convertRangeVar(n.Relation),
2781-
// VaCols: convertSlice(n.VaCols),
2778+
// FIXME: The VacuumStmt node has changed quite a bit
2779+
// Options: n.Options
2780+
// Relation: convertRangeVar(n.Relation),
2781+
// VaCols: convertSlice(n.VaCols),
27822782
}
27832783
}
27842784

0 commit comments

Comments
 (0)