Skip to content

Commit 87aa1a9

Browse files
committed
fix(resolve): fix resolving reference to CTEs
Fixed resolving refs to CTEs by adding CTEs to the aliasMap and indexing its columns when resolving catalog references. Fix #3219
1 parent a0a321b commit 87aa1a9

File tree

8 files changed

+163
-1
lines changed

8 files changed

+163
-1
lines changed

internal/compiler/resolve.go

+32-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,38 @@ 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-
if _, qcerr := qc.GetTable(fqn); qcerr != nil {
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 {
71102
return nil, err
72103
}
73104
continue
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
https://github.com/sqlc-dev/sqlc/issues/3219
2+

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

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- name: CTERef :one
2+
WITH t1_ids AS (
3+
SELECT id FROM t1
4+
)
5+
SELECT * FROM t1_ids WHERE t1_ids.id = sqlc.arg('id');
6+
7+
-- name: CTEMultipleRefs :one
8+
WITH t1_ids AS (
9+
SELECT id FROM t1 WHERE t1.id = sqlc.arg('id')
10+
),
11+
t2_ids AS (
12+
SELECT id FROM t2 WHERE t2.id = sqlc.arg('id')
13+
),
14+
all_ids AS (
15+
SELECT * FROM t1_ids
16+
UNION
17+
SELECT * FROM t2_ids
18+
)
19+
SELECT * FROM all_ids AS ids WHERE ids.id = sqlc.arg('id');
20+
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE t1
2+
(
3+
id SERIAL NOT NULL PRIMARY KEY
4+
);
5+
6+
CREATE TABLE t2
7+
(
8+
id SERIAL NOT NULL PRIMARY KEY
9+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "postgresql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "pgx/v5"

0 commit comments

Comments
 (0)