Skip to content

Commit a60acbf

Browse files
authored
fix(compiler): Validate UNION ... ORDER BY (#2446)
1 parent f5c528f commit a60acbf

File tree

11 files changed

+235
-2
lines changed

11 files changed

+235
-2
lines changed

internal/compiler/output_columns.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
6565
targets = n.ReturningList
6666
case *ast.SelectStmt:
6767
targets = n.TargetList
68+
isUnion := len(targets.Items) == 0 && n.Larg != nil
6869

6970
if n.GroupClause != nil {
7071
for _, item := range n.GroupClause.Items {
@@ -77,7 +78,7 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
7778
if c.conf.StrictOrderBy != nil {
7879
validateOrderBy = *c.conf.StrictOrderBy
7980
}
80-
if validateOrderBy {
81+
if !isUnion && validateOrderBy {
8182
if n.SortClause != nil {
8283
for _, item := range n.SortClause.Items {
8384
sb, ok := item.(*ast.SortBy)
@@ -110,7 +111,7 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
110111

111112
// For UNION queries, targets is empty and we need to look for the
112113
// columns in Largs.
113-
if len(targets.Items) == 0 && n.Larg != nil {
114+
if isUnion {
114115
return c.outputColumns(qc, n.Larg)
115116
}
116117
case *ast.CallStmt:

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

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

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

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CREATE TABLE authors (
2+
name text NOT NULL,
3+
bio text
4+
);
5+
6+
CREATE TABLE people (
7+
first_name text NOT NULL
8+
);
9+
10+
-- name: ListAuthorsUnion :many
11+
SELECT name as foo FROM authors
12+
UNION
13+
SELECT first_name as foo FROM people
14+
ORDER BY foo;
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/order_by_union/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/order_by_union/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/order_by_union/postgresql/go/query.sql.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE TABLE authors (
2+
id BIGSERIAL PRIMARY KEY,
3+
name text NOT NULL,
4+
bio text
5+
);
6+
7+
CREATE TABLE people (
8+
first_name text NOT NULL
9+
);
10+
11+
-- name: ListAuthorsUnion :many
12+
SELECT name as foo FROM authors
13+
UNION
14+
SELECT first_name as foo FROM people
15+
ORDER BY foo;
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": "postgresql",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)