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): Validate UNION ... ORDER BY #2446

Merged
merged 1 commit into from
Jul 13, 2023
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
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"
}
]
}