Skip to content

Commit

Permalink
fix(compiler): Validate UNION ... ORDER BY (#2446)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy authored Jul 13, 2023
1 parent f5c528f commit a60acbf
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 2 deletions.
5 changes: 3 additions & 2 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
targets = n.ReturningList
case *ast.SelectStmt:
targets = n.TargetList
isUnion := len(targets.Items) == 0 && n.Larg != nil

if n.GroupClause != nil {
for _, item := range n.GroupClause.Items {
Expand All @@ -77,7 +78,7 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
if c.conf.StrictOrderBy != nil {
validateOrderBy = *c.conf.StrictOrderBy
}
if validateOrderBy {
if !isUnion && validateOrderBy {
if n.SortClause != nil {
for _, item := range n.SortClause.Items {
sb, ok := item.(*ast.SortBy)
Expand Down Expand Up @@ -110,7 +111,7 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er

// For UNION queries, targets is empty and we need to look for the
// columns in Largs.
if len(targets.Items) == 0 && n.Larg != nil {
if isUnion {
return c.outputColumns(qc, n.Larg)
}
case *ast.CallStmt:
Expand Down
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/order_by_union/mysql/go/db.go

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

18 changes: 18 additions & 0 deletions internal/endtoend/testdata/order_by_union/mysql/go/models.go

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

40 changes: 40 additions & 0 deletions internal/endtoend/testdata/order_by_union/mysql/go/query.sql.go

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

14 changes: 14 additions & 0 deletions internal/endtoend/testdata/order_by_union/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE authors (
name text NOT NULL,
bio text
);

CREATE TABLE people (
first_name text NOT NULL
);

-- name: ListAuthorsUnion :many
SELECT name as foo FROM authors
UNION
SELECT first_name as foo FROM people
ORDER BY foo;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/order_by_union/mysql/sqlc.json
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"
}
]
}
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/order_by_union/postgresql/go/db.go

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

19 changes: 19 additions & 0 deletions internal/endtoend/testdata/order_by_union/postgresql/go/models.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.

15 changes: 15 additions & 0 deletions internal/endtoend/testdata/order_by_union/postgresql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text
);

CREATE TABLE people (
first_name text NOT NULL
);

-- name: ListAuthorsUnion :many
SELECT name as foo FROM authors
UNION
SELECT first_name as foo FROM people
ORDER BY foo;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/order_by_union/postgresql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}

0 comments on commit a60acbf

Please sign in to comment.