Skip to content

Commit 12295d5

Browse files
committed
Fix parameter inference for aliased CTE columns
1 parent 5ab441a commit 12295d5

8 files changed

Lines changed: 122 additions & 5 deletions

File tree

internal/compiler/resolve.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
4949
}
5050
return nil
5151
}
52+
indexQueryTable := func(table *Table) error {
53+
columns := make([]*catalog.Column, 0, len(table.Columns))
54+
for _, column := range table.Columns {
55+
columnType := ast.TypeName{Name: column.DataType}
56+
if column.Type != nil {
57+
columnType = *column.Type
58+
}
59+
columns = append(columns, &catalog.Column{
60+
Name: column.Name,
61+
Type: columnType,
62+
IsNotNull: column.NotNull,
63+
IsUnsigned: column.Unsigned,
64+
IsArray: column.IsArray,
65+
ArrayDims: column.ArrayDims,
66+
Comment: column.Comment,
67+
Length: column.Length,
68+
})
69+
}
70+
return indexTable(catalog.Table{Rel: table.Rel, Columns: columns})
71+
}
5272

5373
for _, rv := range rvs {
5474
if rv.Relname == nil {
@@ -66,8 +86,12 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
6686
if qc == nil {
6787
continue
6888
}
69-
// If the table name doesn't exist, first check if it's a CTE
70-
if _, qcerr := qc.GetTable(fqn); qcerr != nil {
89+
// If the table name doesn't exist, first check if it's a CTE.
90+
queryTable, qcerr := qc.GetTable(fqn)
91+
if qcerr != nil {
92+
return nil, err
93+
}
94+
if err := indexQueryTable(queryTable); err != nil {
7195
return nil, err
7296
}
7397
continue

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

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

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

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

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

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- name: CountRecentEvents :one
2+
WITH activity AS (
3+
SELECT happened_at AS activity_at
4+
FROM events
5+
)
6+
SELECT COUNT(*) FILTER (WHERE activity_at >= $1)::bigint AS recent_events
7+
FROM activity;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE TABLE events (
2+
happened_at timestamptz NOT NULL
3+
);
Lines changed: 10 additions & 0 deletions
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"

internal/endtoend/testdata/cte_update/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)