Skip to content

Commit 60fb071

Browse files
committed
Merge branch 'subqueries-split' into combined
2 parents ecddc20 + 7d1b0c6 commit 60fb071

File tree

5 files changed

+132
-7
lines changed

5 files changed

+132
-7
lines changed

internal/compiler/query_catalog.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,36 @@ import (
99
)
1010

1111
type QueryCatalog struct {
12-
catalog *catalog.Catalog
13-
ctes map[string]*Table
14-
embeds rewrite.EmbedSet
12+
catalog *catalog.Catalog
13+
ctes map[string]*Table
14+
fromClauses map[string]*Table
15+
embeds rewrite.EmbedSet
1516
}
1617

1718
func (comp *Compiler) buildQueryCatalog(c *catalog.Catalog, node ast.Node, embeds rewrite.EmbedSet) (*QueryCatalog, error) {
1819
var with *ast.WithClause
20+
var from *ast.List
1921
switch n := node.(type) {
2022
case *ast.DeleteStmt:
2123
with = n.WithClause
2224
case *ast.InsertStmt:
2325
with = n.WithClause
2426
case *ast.UpdateStmt:
2527
with = n.WithClause
28+
from = n.FromClause
2629
case *ast.SelectStmt:
2730
with = n.WithClause
31+
from = n.FromClause
2832
default:
2933
with = nil
34+
from = nil
35+
}
36+
qc := &QueryCatalog{
37+
catalog: c,
38+
ctes: map[string]*Table{},
39+
fromClauses: map[string]*Table{},
40+
embeds: embeds,
3041
}
31-
qc := &QueryCatalog{catalog: c, ctes: map[string]*Table{}, embeds: embeds}
3242
if with != nil {
3343
for _, item := range with.Ctes.Items {
3444
if cte, ok := item.(*ast.CommonTableExpr); ok {
@@ -60,6 +70,37 @@ func (comp *Compiler) buildQueryCatalog(c *catalog.Catalog, node ast.Node, embed
6070
}
6171
}
6272
}
73+
if from != nil {
74+
for _, item := range from.Items {
75+
if rs, ok := item.(*ast.RangeSubselect); ok {
76+
cols, err := comp.outputColumns(qc, rs.Subquery)
77+
if err != nil {
78+
return nil, err
79+
}
80+
var names []string
81+
if rs.Alias.Colnames != nil {
82+
for _, item := range rs.Alias.Colnames.Items {
83+
if val, ok := item.(*ast.String); ok {
84+
names = append(names, val.Str)
85+
} else {
86+
names = append(names, "")
87+
}
88+
}
89+
}
90+
rel := &ast.TableName{Name: *rs.Alias.Aliasname}
91+
for i := range cols {
92+
cols[i].Table = rel
93+
if len(names) > i {
94+
cols[i].Name = names[i]
95+
}
96+
}
97+
qc.fromClauses[*rs.Alias.Aliasname] = &Table{
98+
Rel: rel,
99+
Columns: cols,
100+
}
101+
}
102+
}
103+
}
63104
return qc, nil
64105
}
65106

internal/compiler/resolve.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,30 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
111111
aliasMap[*rv.Alias.Aliasname] = fqn
112112
}
113113
}
114+
if qc != nil {
115+
for _, f := range qc.fromClauses {
116+
catCols := make([]*catalog.Column, 0, len(f.Columns))
117+
for _, col := range f.Columns {
118+
catCols = append(catCols, &catalog.Column{
119+
Name: col.Name,
120+
Type: ast.TypeName{Name: col.DataType},
121+
IsNotNull: col.NotNull,
122+
IsUnsigned: col.Unsigned,
123+
IsArray: col.IsArray,
124+
ArrayDims: col.ArrayDims,
125+
Comment: col.Comment,
126+
Length: col.Length,
127+
})
128+
}
129+
130+
if err := indexTable(catalog.Table{
131+
Rel: f.Rel,
132+
Columns: catCols,
133+
}); err != nil {
134+
return nil, err
135+
}
136+
}
137+
}
114138

115139
// resolve a table for an embed
116140
for _, embed := range embeds {

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

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

internal/endtoend/testdata/join_alias/mysql/query.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ SELECT *
99
FROM foo f
1010
JOIN bar b ON b.id = f.id
1111
WHERE f.id = ?;
12+
13+
-- name: SubqueryAlias :many
14+
SELECT * FROM (SELECT 1 AS n) AS x WHERE x.n <= ?;
15+
16+
-- name: ColumnAlias :many
17+
SELECT * FROM (SELECT 1 AS n) AS x WHERE n <= ?;

internal/endtoend/testdata/select_subquery_alias/postgresql/pgx/go/query.sql.go

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

0 commit comments

Comments
 (0)