Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Added support for embeding custom types
  • Loading branch information
wickwirew committed Sep 12, 2025
commit c11b647f34145970b23bfb1367996d57bf9beefe
8 changes: 6 additions & 2 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,13 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
Name: fn.ReturnType.Name,
})

// Failed to find table, check for type with name.
if err != nil {
table, err = qc.GetCompositeType(fn.ReturnType)
// No table, check to see if there a type
tyTable, tyErr := qc.GetTableForType(fn.ReturnType)
if tyErr == nil {
table = tyTable
err = nil
}
}
}
if table == nil || err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/query_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (qc QueryCatalog) GetFunc(rel *ast.FuncName) (*Function, error) {
}, nil
}

func (qc QueryCatalog) GetCompositeType(rel *ast.TypeName) (*Table, error) {
func (qc QueryCatalog) GetTableForType(rel *ast.TypeName) (*Table, error) {
ty, err := qc.catalog.GetCompostiteType(rel)
if err != nil {
return &Table{}, err
Expand Down
11 changes: 11 additions & 0 deletions internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
continue
}

tyName := &ast.TypeName{
Catalog: embed.Table.Catalog,
Schema: embed.Table.Schema,
Name: embed.Table.Name,
}
ty, err := c.GetCompostiteType(tyName)
if err == nil {
embed.Table = &ast.TableName{Name: ty.Name}
continue
}

return nil, fmt.Errorf("unable to resolve table with %q: %w", embed.Orig(), err)
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
-- name: SelectBars :many
SELECT * FROM bar_fn();

-- name: SelectFoos :many
SELECT * FROM foo_fn();

-- name: SelectFooEmbed :many
SELECT sqlc.embed(foo), 1 AS one FROM foo_fn() AS foo;

-- name: SelectSingleColumn :many
SELECT baz FROM foo_fn();
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
CREATE TYPE foo AS ENUM ('bar', 'baz');
CREATE TYPE foo AS (foo TEXT, baz INTEGER);

CREATE TYPE bar AS (foo foo, baz INTEGER);

CREATE FUNCTION bar_fn()
RETURNS SETOF bar LANGUAGE SQL STABLE AS $$
SELECT * FROM VALUES ('bar', 1);
$$;
CREATE TABLE bar (foo TEXT, baz INTEGER);

CREATE FUNCTION foo_fn()
RETURNS SETOF foo LANGUAGE SQL STABLE AS $$
SELECT * FROM VALUES ('bar');
SELECT foo, baz FROM bar;
$$;
Loading