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

feat(compiler): Support subqueries in the FROM clause #3227

Merged
merged 2 commits into from
Mar 28, 2024
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
49 changes: 45 additions & 4 deletions internal/compiler/query_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,36 @@ import (
)

type QueryCatalog struct {
catalog *catalog.Catalog
ctes map[string]*Table
embeds rewrite.EmbedSet
catalog *catalog.Catalog
ctes map[string]*Table
fromClauses map[string]*Table
embeds rewrite.EmbedSet
}

func (comp *Compiler) buildQueryCatalog(c *catalog.Catalog, node ast.Node, embeds rewrite.EmbedSet) (*QueryCatalog, error) {
var with *ast.WithClause
var from *ast.List
switch n := node.(type) {
case *ast.DeleteStmt:
with = n.WithClause
case *ast.InsertStmt:
with = n.WithClause
case *ast.UpdateStmt:
with = n.WithClause
from = n.FromClause
case *ast.SelectStmt:
with = n.WithClause
from = n.FromClause
default:
with = nil
from = nil
}
qc := &QueryCatalog{
catalog: c,
ctes: map[string]*Table{},
fromClauses: map[string]*Table{},
embeds: embeds,
}
qc := &QueryCatalog{catalog: c, ctes: map[string]*Table{}, embeds: embeds}
if with != nil {
for _, item := range with.Ctes.Items {
if cte, ok := item.(*ast.CommonTableExpr); ok {
Expand Down Expand Up @@ -60,6 +70,37 @@ func (comp *Compiler) buildQueryCatalog(c *catalog.Catalog, node ast.Node, embed
}
}
}
if from != nil {
for _, item := range from.Items {
if rs, ok := item.(*ast.RangeSubselect); ok {
cols, err := comp.outputColumns(qc, rs.Subquery)
if err != nil {
return nil, err
}
var names []string
if rs.Alias.Colnames != nil {
for _, item := range rs.Alias.Colnames.Items {
if val, ok := item.(*ast.String); ok {
names = append(names, val.Str)
} else {
names = append(names, "")
}
}
}
rel := &ast.TableName{Name: *rs.Alias.Aliasname}
for i := range cols {
cols[i].Table = rel
if len(names) > i {
cols[i].Name = names[i]
}
}
qc.fromClauses[*rs.Alias.Aliasname] = &Table{
Rel: rel,
Columns: cols,
}
}
}
}
return qc, nil
}

Expand Down
55 changes: 54 additions & 1 deletion internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,38 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
continue
}
// If the table name doesn't exist, first check if it's a CTE
if _, qcerr := qc.GetTable(fqn); qcerr != nil {
catTable, qcerr := qc.GetTable(fqn)
if qcerr != nil {
return nil, err
}

// If it's a CTE, add it to the alias map and add its columns to
// the type map. This is to allow us to resolve references to the
// CTE's columns in a query.
aliasMap[fqn.Name] = fqn
if rv.Alias != nil {
aliasMap[*rv.Alias.Aliasname] = fqn
}

catCols := make([]*catalog.Column, 0, len(catTable.Columns))
for _, col := range catTable.Columns {
catCols = append(catCols, &catalog.Column{
Name: col.Name,
Type: ast.TypeName{Name: col.DataType},
IsNotNull: col.NotNull,
IsUnsigned: col.Unsigned,
IsArray: col.IsArray,
ArrayDims: col.ArrayDims,
Comment: col.Comment,
Length: col.Length,
})
}

err = indexTable(catalog.Table{
Rel: catTable.Rel,
Columns: catCols,
})
if err != nil {
return nil, err
}
continue
Expand All @@ -80,6 +111,28 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
aliasMap[*rv.Alias.Aliasname] = fqn
}
}
for _, f := range qc.fromClauses {
catCols := make([]*catalog.Column, 0, len(f.Columns))
for _, col := range f.Columns {
catCols = append(catCols, &catalog.Column{
Name: col.Name,
Type: ast.TypeName{Name: col.DataType},
IsNotNull: col.NotNull,
IsUnsigned: col.Unsigned,
IsArray: col.IsArray,
ArrayDims: col.ArrayDims,
Comment: col.Comment,
Length: col.Length,
})
}

if err := indexTable(catalog.Table{
Rel: f.Rel,
Columns: catCols,
}); err != nil {
return nil, err
}
}

// resolve a table for an embed
for _, embed := range embeds {
Expand Down
2 changes: 2 additions & 0 deletions internal/endtoend/testdata/cte_resolve_ref/issue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://github.com/sqlc-dev/sqlc/issues/3219

32 changes: 32 additions & 0 deletions internal/endtoend/testdata/cte_resolve_ref/postgresql/pgx/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,21 @@
-- name: CTERef :one
WITH t1_ids AS (
SELECT id FROM t1
)
SELECT * FROM t1_ids WHERE t1_ids.id = sqlc.arg('id');

-- name: CTEMultipleRefs :one
WITH t1_ids AS (
SELECT id FROM t1 WHERE t1.id = sqlc.arg('id')
),
t2_ids AS (
SELECT id FROM t2 WHERE t2.id = sqlc.arg('id')
),
all_ids AS (
SELECT * FROM t1_ids
UNION
SELECT * FROM t2_ids
)
SELECT * FROM all_ids AS ids WHERE ids.id = sqlc.arg('id');


Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE t1
(
id SERIAL NOT NULL PRIMARY KEY
);

CREATE TABLE t2
(
id SERIAL NOT NULL PRIMARY KEY
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "2"
sql:
- engine: "postgresql"
schema: "schema.sql"
queries: "query.sql"
gen:
go:
package: "querytest"
out: "go"
sql_package: "pgx/v5"
54 changes: 54 additions & 0 deletions internal/endtoend/testdata/join_alias/mysql/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/join_alias/mysql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ SELECT *
FROM foo f
JOIN bar b ON b.id = f.id
WHERE f.id = ?;

-- name: SubqueryAlias :many
SELECT * FROM (SELECT 1 AS n) AS x WHERE x.n <= ?;

-- name: ColumnAlias :many
SELECT * FROM (SELECT 1 AS n) AS x WHERE n <= ?;
Loading