Skip to content

Commit 495be9d

Browse files
authored
Fix return type for subqueries in MySQL (#1383) (#1404)
1 parent dcb19a1 commit 495be9d

File tree

6 files changed

+142
-0
lines changed

6 files changed

+142
-0
lines changed

internal/compiler/output_columns.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,17 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
268268
}
269269
cols = append(cols, col)
270270

271+
case *ast.SelectStmt:
272+
subcols, err := outputColumns(qc, n)
273+
if err != nil {
274+
return nil, err
275+
}
276+
first := subcols[0]
277+
if res.Name != nil {
278+
first.Name = *res.Name
279+
}
280+
cols = append(cols, first)
281+
271282
default:
272283
name := ""
273284
if res.Name != nil {

internal/endtoend/testdata/select_nested_count/mysql/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/mysql/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/mysql/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 bigint PRIMARY KEY,
3+
name text NOT NULL,
4+
bio text
5+
);
6+
7+
CREATE TABLE books (
8+
id bigint PRIMARY KEY,
9+
author_id bigint 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"name": "querytest",
7+
"schema": "query.sql",
8+
"queries": "query.sql",
9+
"engine": "mysql"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)