Skip to content

Commit

Permalink
compiler: support UNION query
Browse files Browse the repository at this point in the history
This commit adds support for UNION query in the compiler. It adds test cases
for UNION in a SELECT and in a recursive CTE.

It fixes sqlc-dev#568, sqlc-dev#723 and sqlc-dev#778.
  • Loading branch information
aitva committed Feb 14, 2021
1 parent 9207cae commit 0c9db97
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
targets = n.ReturningList
case *ast.SelectStmt:
targets = n.TargetList
// For UNION queries, targets is empty and we need to look for the
// columns in Largs.
if len(targets.Items) == 0 && n.Larg != nil {
return outputColumns(qc, n.Larg)
}
case *ast.TruncateStmt:
targets = &ast.List{}
case *ast.UpdateStmt:
Expand Down Expand Up @@ -199,6 +204,7 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {

}
}

return cols, nil
}

Expand Down
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/cte_recursive/go/db.go

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

12 changes: 12 additions & 0 deletions internal/endtoend/testdata/cte_recursive/go/models.go

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

48 changes: 48 additions & 0 deletions internal/endtoend/testdata/cte_recursive/go/query.sql.go

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

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/cte_recursive/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE bar (id INT NOT NULL, parent_id INT);

-- name: CTERecursive :many
WITH RECURSIVE cte AS (
SELECT b.* FROM bar AS b
WHERE b.id = $1
UNION ALL
SELECT b.*
FROM bar AS b, cte AS c
WHERE b.parent_id = c.id
) SELECT * FROM cte;
11 changes: 11 additions & 0 deletions internal/endtoend/testdata/cte_recursive/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/select_union/go/db.go

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

12 changes: 12 additions & 0 deletions internal/endtoend/testdata/select_union/go/models.go

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

37 changes: 37 additions & 0 deletions internal/endtoend/testdata/select_union/go/query.sql.go

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

6 changes: 6 additions & 0 deletions internal/endtoend/testdata/select_union/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE foo (a text, b text);

-- name: SelectUnion :many
SELECT * FROM foo
UNION
SELECT * FROM foo;
11 changes: 11 additions & 0 deletions internal/endtoend/testdata/select_union/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}

0 comments on commit 0c9db97

Please sign in to comment.