Skip to content

Commit 996f7d1

Browse files
authored
Revert "feat(compiler): Support subqueries in the FROM clause (second coming)…" (#3314)
This reverts commit 3a3e387.
1 parent 3a3e387 commit 996f7d1

File tree

11 files changed

+5
-290
lines changed

11 files changed

+5
-290
lines changed

internal/compiler/query_catalog.go

+4-45
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,26 @@ import (
99
)
1010

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

1817
func (comp *Compiler) buildQueryCatalog(c *catalog.Catalog, node ast.Node, embeds rewrite.EmbedSet) (*QueryCatalog, error) {
1918
var with *ast.WithClause
20-
var from *ast.List
2119
switch n := node.(type) {
2220
case *ast.DeleteStmt:
2321
with = n.WithClause
2422
case *ast.InsertStmt:
2523
with = n.WithClause
2624
case *ast.UpdateStmt:
2725
with = n.WithClause
28-
from = n.FromClause
2926
case *ast.SelectStmt:
3027
with = n.WithClause
31-
from = n.FromClause
3228
default:
3329
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,
4130
}
31+
qc := &QueryCatalog{catalog: c, ctes: map[string]*Table{}, embeds: embeds}
4232
if with != nil {
4333
for _, item := range with.Ctes.Items {
4434
if cte, ok := item.(*ast.CommonTableExpr); ok {
@@ -70,37 +60,6 @@ func (comp *Compiler) buildQueryCatalog(c *catalog.Catalog, node ast.Node, embed
7060
}
7161
}
7262
}
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-
}
10463
return qc, nil
10564
}
10665

internal/compiler/resolve.go

+1-54
Original file line numberDiff line numberDiff line change
@@ -67,38 +67,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
6767
continue
6868
}
6969
// If the table name doesn't exist, first check if it's a CTE
70-
catTable, qcerr := qc.GetTable(fqn)
71-
if qcerr != nil {
72-
return nil, err
73-
}
74-
75-
// If it's a CTE, add it to the alias map and add its columns to
76-
// the type map. This is to allow us to resolve references to the
77-
// CTE's columns in a query.
78-
aliasMap[fqn.Name] = fqn
79-
if rv.Alias != nil {
80-
aliasMap[*rv.Alias.Aliasname] = fqn
81-
}
82-
83-
catCols := make([]*catalog.Column, 0, len(catTable.Columns))
84-
for _, col := range catTable.Columns {
85-
catCols = append(catCols, &catalog.Column{
86-
Name: col.Name,
87-
Type: ast.TypeName{Name: col.DataType},
88-
IsNotNull: col.NotNull,
89-
IsUnsigned: col.Unsigned,
90-
IsArray: col.IsArray,
91-
ArrayDims: col.ArrayDims,
92-
Comment: col.Comment,
93-
Length: col.Length,
94-
})
95-
}
96-
97-
err = indexTable(catalog.Table{
98-
Rel: catTable.Rel,
99-
Columns: catCols,
100-
})
101-
if err != nil {
70+
if _, qcerr := qc.GetTable(fqn); qcerr != nil {
10271
return nil, err
10372
}
10473
continue
@@ -111,28 +80,6 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
11180
aliasMap[*rv.Alias.Aliasname] = fqn
11281
}
11382
}
114-
for _, f := range qc.fromClauses {
115-
catCols := make([]*catalog.Column, 0, len(f.Columns))
116-
for _, col := range f.Columns {
117-
catCols = append(catCols, &catalog.Column{
118-
Name: col.Name,
119-
Type: ast.TypeName{Name: col.DataType},
120-
IsNotNull: col.NotNull,
121-
IsUnsigned: col.Unsigned,
122-
IsArray: col.IsArray,
123-
ArrayDims: col.ArrayDims,
124-
Comment: col.Comment,
125-
Length: col.Length,
126-
})
127-
}
128-
129-
if err := indexTable(catalog.Table{
130-
Rel: f.Rel,
131-
Columns: catCols,
132-
}); err != nil {
133-
return nil, err
134-
}
135-
}
13683

13784
// resolve a table for an embed
13885
for _, embed := range embeds {

internal/endtoend/testdata/cte_resolve_ref/issue.md

-2
This file was deleted.

internal/endtoend/testdata/cte_resolve_ref/postgresql/pgx/go/db.go

-32
This file was deleted.

internal/endtoend/testdata/cte_resolve_ref/postgresql/pgx/go/models.go

-13
This file was deleted.

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

-44
This file was deleted.

internal/endtoend/testdata/cte_resolve_ref/postgresql/pgx/query.sql

-21
This file was deleted.

internal/endtoend/testdata/cte_resolve_ref/postgresql/pgx/schema.sql

-9
This file was deleted.

internal/endtoend/testdata/cte_resolve_ref/postgresql/pgx/sqlc.yaml

-10
This file was deleted.

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

-54
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

-6
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,3 @@ 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 <= ?;

0 commit comments

Comments
 (0)