Skip to content

Commit 1a8c10c

Browse files
committed
fix: count columns aliases only once
Alias column names don't exist in the table definition. When we take it from the select target list, we only need to do this once (and not once for every table in a join statement). Refs: sqlc-dev#2398
1 parent d32d6af commit 1a8c10c

6 files changed

Lines changed: 126 additions & 1 deletion

File tree

‎internal/compiler/output_columns.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ func findColumnForRef(ref *ast.ColumnRef, tables []*Table, selectStatement *ast.
661661
}
662662

663663
var found int
664+
var foundResTarget *ast.ResTarget
664665
for _, t := range tables {
665666
if alias != "" && t.Rel.Name != alias {
666667
continue
@@ -686,7 +687,10 @@ func findColumnForRef(ref *ast.ColumnRef, tables []*Table, selectStatement *ast.
686687
continue
687688
}
688689
if resTarget.Name != nil && *resTarget.Name == name {
689-
found++
690+
if resTarget != foundResTarget {
691+
found++
692+
foundResTarget = resTarget
693+
}
690694
}
691695
}
692696
}

‎internal/endtoend/testdata/join_group_order/postgresql/go/db.go‎

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

‎internal/endtoend/testdata/join_group_order/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/join_group_order/postgresql/go/query.sql.go‎

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- Example queries for sqlc
2+
CREATE TABLE authors (
3+
id INT
4+
);
5+
6+
CREATE TABLE books (
7+
id INT,
8+
author_id INT,
9+
price INT
10+
);
11+
12+
-- name: ListAuthorsByCheapestBook :many
13+
SELECT
14+
author_id, min(b.price) as min_price
15+
From books b inner join authors a on a.id = b.author_id
16+
GROUP BY b.author_id
17+
ORDER BY min_price;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: 1
2+
packages:
3+
- path: "go"
4+
name: "querytest"
5+
engine: "mysql"
6+
schema: "query.sql"
7+
queries: "query.sql"

0 commit comments

Comments
 (0)