Skip to content

Commit 379cc12

Browse files
authored
fix(compiler): Fix validation of GROUP BY on field aliases (#1348)
1 parent a615c2b commit 379cc12

File tree

11 files changed

+478
-2
lines changed

11 files changed

+478
-2
lines changed

internal/compiler/output_columns.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
7272
continue
7373
}
7474

75-
if err := findColumnForRef(ref, tables); err != nil {
75+
if err := findColumnForRef(ref, tables, n); err != nil {
7676
return nil, err
7777
}
7878
}
@@ -485,7 +485,7 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef)
485485
return cols, nil
486486
}
487487

488-
func findColumnForRef(ref *ast.ColumnRef, tables []*Table) error {
488+
func findColumnForRef(ref *ast.ColumnRef, tables []*Table, selectStatement *ast.SelectStmt) error {
489489
parts := stringSlice(ref.Fields)
490490
var alias, name string
491491
if len(parts) == 1 {
@@ -500,9 +500,28 @@ func findColumnForRef(ref *ast.ColumnRef, tables []*Table) error {
500500
if alias != "" && t.Rel.Name != alias {
501501
continue
502502
}
503+
504+
// Find matching column
505+
var foundColumn bool
503506
for _, c := range t.Columns {
504507
if c.Name == name {
505508
found++
509+
foundColumn = true
510+
}
511+
}
512+
513+
if foundColumn {
514+
continue
515+
}
516+
517+
// Find matching alias
518+
for _, c := range selectStatement.TargetList.Items {
519+
resTarget, ok := c.(*ast.ResTarget)
520+
if !ok {
521+
continue
522+
}
523+
if resTarget.Name != nil && *resTarget.Name == name {
524+
found++
506525
}
507526
}
508527
}

internal/endtoend/testdata/valid_group_by_reference/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/valid_group_by_reference/mysql/go/models.go

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

internal/endtoend/testdata/valid_group_by_reference/mysql/go/query.sql.go

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
CREATE TABLE authors (
2+
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
3+
name text NOT NULL,
4+
bio text,
5+
UNIQUE(name)
6+
);
7+
8+
-- name: ListAuthors :many
9+
SELECT id, name as full_name, bio
10+
FROM authors
11+
GROUP BY full_name;
12+
13+
-- name: ListAuthorsIdenticalAlias :many
14+
SELECT id, name as name, bio
15+
FROM authors
16+
GROUP BY name;
17+
18+
19+
-- https://github.com/kyleconroy/sqlc/issues/1315
20+
21+
CREATE TABLE IF NOT EXISTS weather_metrics
22+
(
23+
time TIMESTAMP NOT NULL,
24+
timezone_shift INT NULL,
25+
city_name TEXT NULL,
26+
temp_c FLOAT NULL,
27+
feels_like_c FLOAT NULL,
28+
temp_min_c FLOAT NULL,
29+
temp_max_c FLOAT NULL,
30+
pressure_hpa FLOAT NULL,
31+
humidity_percent FLOAT NULL,
32+
wind_speed_ms FLOAT NULL,
33+
wind_deg INT NULL,
34+
rain_1h_mm FLOAT NULL,
35+
rain_3h_mm FLOAT NULL,
36+
snow_1h_mm FLOAT NULL,
37+
snow_3h_mm FLOAT NULL,
38+
clouds_percent INT NULL,
39+
weather_type_id INT NULL
40+
);
41+
42+
-- name: ListMetrics :many
43+
SELECT time_bucket('15 days', time) AS bucket, city_name, AVG(temp_c)
44+
FROM weather_metrics
45+
WHERE DATE_SUB(NOW(), INTERVAL 6 MONTH)
46+
GROUP BY bucket, city_name
47+
ORDER BY bucket DESC;
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+
"engine": "mysql",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/endtoend/testdata/valid_group_by_reference/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/valid_group_by_reference/postgresql/go/models.go

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

0 commit comments

Comments
 (0)