Skip to content

Commit 2cd271a

Browse files
committed
core: hand views to codegen along with tables
The dump that carries the core catalog to codegen listed tables only, so under SQLCEXPERIMENT=coreanalyzer a view, a materialized view or a table created from a query had no model in models.go, and a query selecting all of one got a row struct of its own instead of the view's model. The listing now covers the relations a query selects rows from the same way, which is what codegen builds a model for. In the core replay context 8 more cases pass, with no case regressing. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XZcKb4GFiZQbeo9oVmyF3m
1 parent d227db3 commit 2cd271a

4 files changed

Lines changed: 30 additions & 27 deletions

File tree

internal/compiler/catalog_core.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
// coreResultCatalog dumps the core catalog into the legacy catalog shape a
1212
// Result carries, so codegen sees the same table models either way a query
1313
// set was analyzed. Relations and enums make the trip: codegen reads tables
14-
// and their columns to build models and enums to build their Go types, and
15-
// none of the functions or operators the core catalog also holds.
14+
// and views with their columns to build models and enums to build their Go
15+
// types, and none of the functions or operators the core catalog also holds.
1616
func coreResultCatalog(c *core.Catalog) (*catalog.Catalog, error) {
1717
cat := catalog.New("public")
1818
namespaces, err := c.Namespaces()
@@ -23,7 +23,7 @@ func coreResultCatalog(c *core.Catalog) (*catalog.Catalog, error) {
2323
for _, ns := range namespaces {
2424
schema := &catalog.Schema{Name: ns.Name}
2525
schemas[ns.Name] = schema
26-
tables, err := c.TablesInNamespace(ns.OID)
26+
tables, err := c.ModelClassesInNamespace(ns.OID)
2727
if err != nil {
2828
return nil, err
2929
}

internal/core/catalogdb/query.sql.go

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

internal/core/catalogdef/query.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ SELECT oid FROM sql_class WHERE namespace_oid = ? AND name = ?;
9898
-- name: ClassOIDByName :one
9999
SELECT oid FROM sql_class WHERE name = ? LIMIT 1;
100100

101-
-- name: ListTablesInNamespace :many
101+
-- name: ListModelClassesInNamespace :many
102102
SELECT oid, name FROM sql_class
103-
WHERE namespace_oid = ? AND kind = 'r'
103+
WHERE namespace_oid = ? AND kind IN ('r', 'v')
104104
ORDER BY oid;
105105

106106
-- name: DeleteClass :exec

internal/core/class.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ type ClassInfo struct {
6565
Name string
6666
}
6767

68-
func (c *Catalog) TablesInNamespace(namespaceOID int64) ([]ClassInfo, error) {
69-
rows, err := c.q.ListTablesInNamespace(context.Background(), namespaceOID)
68+
// ModelClassesInNamespace lists the relations codegen builds a model for, in
69+
// declaration order: the tables, and the views and tables created from a
70+
// query, whose rows a query selects the same way.
71+
func (c *Catalog) ModelClassesInNamespace(namespaceOID int64) ([]ClassInfo, error) {
72+
rows, err := c.q.ListModelClassesInNamespace(context.Background(), namespaceOID)
7073
if err != nil {
71-
return nil, fmt.Errorf("list tables in namespace %d: %w", namespaceOID, err)
74+
return nil, fmt.Errorf("list model classes in namespace %d: %w", namespaceOID, err)
7275
}
7376
out := make([]ClassInfo, 0, len(rows))
7477
for _, r := range rows {

0 commit comments

Comments
 (0)