Skip to content

Commit 5252c63

Browse files
committed
Merge remote-tracking branch 'Jille/subqueries-split'
2 parents ae5c797 + 178e449 commit 5252c63

File tree

6 files changed

+175
-15
lines changed

6 files changed

+175
-15
lines changed

internal/compiler/output_columns.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package compiler
33
import (
44
"errors"
55
"fmt"
6+
"math/rand"
67

78
"github.com/sqlc-dev/sqlc/internal/sql/ast"
89
"github.com/sqlc-dev/sqlc/internal/sql/astutils"
@@ -596,16 +597,14 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
596597
if err != nil {
597598
return nil, err
598599
}
599-
600-
var tableName string
601-
if n.Alias != nil {
602-
tableName = *n.Alias.Aliasname
600+
rel := &ast.TableName{}
601+
if n.Alias != nil && n.Alias.Aliasname != nil {
602+
rel.Name = *n.Alias.Aliasname
603+
} else {
604+
rel.Name = fmt.Sprintf("unnamed_subquery_%d", rand.Int63())
603605
}
604-
605606
tables = append(tables, &Table{
606-
Rel: &ast.TableName{
607-
Name: tableName,
608-
},
607+
Rel: rel,
609608
Columns: cols,
610609
})
611610

internal/compiler/query_catalog.go

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,44 @@ package compiler
22

33
import (
44
"fmt"
5+
"math/rand"
56

67
"github.com/sqlc-dev/sqlc/internal/sql/ast"
78
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
89
"github.com/sqlc-dev/sqlc/internal/sql/rewrite"
910
)
1011

1112
type QueryCatalog struct {
12-
catalog *catalog.Catalog
13-
ctes map[string]*Table
14-
embeds rewrite.EmbedSet
13+
catalog *catalog.Catalog
14+
ctes map[string]*Table
15+
fromClauses map[string]*Table
16+
embeds rewrite.EmbedSet
1517
}
1618

1719
func (comp *Compiler) buildQueryCatalog(c *catalog.Catalog, node ast.Node, embeds rewrite.EmbedSet) (*QueryCatalog, error) {
1820
var with *ast.WithClause
21+
var from *ast.List
1922
switch n := node.(type) {
2023
case *ast.DeleteStmt:
2124
with = n.WithClause
2225
case *ast.InsertStmt:
2326
with = n.WithClause
2427
case *ast.UpdateStmt:
2528
with = n.WithClause
29+
from = n.FromClause
2630
case *ast.SelectStmt:
2731
with = n.WithClause
32+
from = n.FromClause
2833
default:
2934
with = nil
35+
from = nil
36+
}
37+
qc := &QueryCatalog{
38+
catalog: c,
39+
ctes: map[string]*Table{},
40+
fromClauses: map[string]*Table{},
41+
embeds: embeds,
3042
}
31-
qc := &QueryCatalog{catalog: c, ctes: map[string]*Table{}, embeds: embeds}
3243
if with != nil {
3344
for _, item := range with.Ctes.Items {
3445
if cte, ok := item.(*ast.CommonTableExpr); ok {
@@ -60,6 +71,42 @@ func (comp *Compiler) buildQueryCatalog(c *catalog.Catalog, node ast.Node, embed
6071
}
6172
}
6273
}
74+
if from != nil {
75+
for _, item := range from.Items {
76+
if rs, ok := item.(*ast.RangeSubselect); ok {
77+
cols, err := comp.outputColumns(qc, rs.Subquery)
78+
if err != nil {
79+
return nil, err
80+
}
81+
var names []string
82+
if rs.Alias != nil && rs.Alias.Colnames != nil {
83+
for _, item := range rs.Alias.Colnames.Items {
84+
if val, ok := item.(*ast.String); ok {
85+
names = append(names, val.Str)
86+
} else {
87+
names = append(names, "")
88+
}
89+
}
90+
}
91+
rel := &ast.TableName{}
92+
if rs.Alias != nil && rs.Alias.Aliasname != nil {
93+
rel.Name = *rs.Alias.Aliasname
94+
} else {
95+
rel.Name = fmt.Sprintf("unaliased_table_%d", rand.Int63())
96+
}
97+
for i := range cols {
98+
cols[i].Table = rel
99+
if len(names) > i {
100+
cols[i].Name = names[i]
101+
}
102+
}
103+
qc.fromClauses[rel.Name] = &Table{
104+
Rel: rel,
105+
Columns: cols,
106+
}
107+
}
108+
}
109+
}
63110
return qc, nil
64111
}
65112

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: 81 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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ 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) WHERE n <= ?;
18+
19+
-- name: ColumnAndQueryAlias :many
20+
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)