Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): Fix validation of GROUP BY on field aliases #1348

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
continue
}

if err := findColumnForRef(ref, tables); err != nil {
if err := findColumnForRef(ref, tables, n); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -485,7 +485,7 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef)
return cols, nil
}

func findColumnForRef(ref *ast.ColumnRef, tables []*Table) error {
func findColumnForRef(ref *ast.ColumnRef, tables []*Table, selectStatement *ast.SelectStmt) error {
parts := stringSlice(ref.Fields)
var alias, name string
if len(parts) == 1 {
Expand All @@ -500,9 +500,28 @@ func findColumnForRef(ref *ast.ColumnRef, tables []*Table) error {
if alias != "" && t.Rel.Name != alias {
continue
}

// Find matching column
var foundColumn bool
for _, c := range t.Columns {
if c.Name == name {
found++
foundColumn = true
}
}

if foundColumn {
continue
}

// Find matching alias
for _, c := range selectStatement.TargetList.Items {
resTarget, ok := c.(*ast.ResTarget)
if !ok {
continue
}
if resTarget.Name != nil && *resTarget.Name == name {
found++
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/valid_group_by_reference/mysql/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
CREATE TABLE authors (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name text NOT NULL,
bio text,
UNIQUE(name)
);

-- name: ListAuthors :many
SELECT id, name as full_name, bio
FROM authors
GROUP BY full_name;

-- name: ListAuthorsIdenticalAlias :many
SELECT id, name as name, bio
FROM authors
GROUP BY name;


-- https://github.com/kyleconroy/sqlc/issues/1315

CREATE TABLE IF NOT EXISTS weather_metrics
(
time TIMESTAMP NOT NULL,
timezone_shift INT NULL,
city_name TEXT NULL,
temp_c FLOAT NULL,
feels_like_c FLOAT NULL,
temp_min_c FLOAT NULL,
temp_max_c FLOAT NULL,
pressure_hpa FLOAT NULL,
humidity_percent FLOAT NULL,
wind_speed_ms FLOAT NULL,
wind_deg INT NULL,
rain_1h_mm FLOAT NULL,
rain_3h_mm FLOAT NULL,
snow_1h_mm FLOAT NULL,
snow_3h_mm FLOAT NULL,
clouds_percent INT NULL,
weather_type_id INT NULL
);

-- name: ListMetrics :many
SELECT time_bucket('15 days', time) AS bucket, city_name, AVG(temp_c)
FROM weather_metrics
WHERE DATE_SUB(NOW(), INTERVAL 6 MONTH)
GROUP BY bucket, city_name
ORDER BY bucket DESC;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "mysql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading