Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,13 +515,30 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
}

fn, err := qc.GetFunc(funcCall.Func)
if err != nil {
if err == nil {
table := &Table{
Rel: &ast.TableName{
Catalog: fn.ReturnTable.Rel.Catalog,
Schema: fn.ReturnTable.Rel.Schema,
//Name: fn.ReturnType.Name,
Name: fn.ReturnTable.Rel.Name,
},
}

for _, c := range fn.Columns {
table.Columns = append(table.Columns, &Column{
Name: c.Name,
DataType: c.DataType,
})
}
tables = append(tables, table)
continue
}
table, err := qc.GetTable(&ast.TableName{
Catalog: fn.ReturnType.Catalog,
Schema: fn.ReturnType.Schema,
Name: fn.ReturnType.Name,
//Name: fn.ReturnType.Name,
Name: fn.Rel.Name,
})
if err != nil {
if n.Alias == nil || len(n.Alias.Colnames.Items) == 0 {
Expand Down
6 changes: 4 additions & 2 deletions internal/compiler/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
)

type Function struct {
Rel *ast.FuncName
ReturnType *ast.TypeName
Rel *ast.FuncName
ReturnType *ast.TypeName
Columns []*Column
ReturnTable *Table
}

type Table struct {
Expand Down
17 changes: 15 additions & 2 deletions internal/compiler/query_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,21 @@ func (qc QueryCatalog) GetFunc(rel *ast.FuncName) (*Function, error) {
if len(funcs) == 0 {
return nil, fmt.Errorf("function not found: %s", rel.Name)
}

var funcRecord *Table
var cols []*Column
rt := funcs[0].ReturnTable
if rt != nil {
for _, c := range rt.Columns {
cols = append(cols, ConvertColumn(rt.Rel, c))
}
funcRecord = &Table{Rel: rt.Rel, Columns: cols}
}

return &Function{
Rel: rel,
ReturnType: funcs[0].ReturnType,
Rel: rel,
ReturnType: funcs[0].ReturnType,
ReturnTable: funcRecord,
Columns: cols,
}, nil
}
25 changes: 25 additions & 0 deletions internal/engine/postgresql/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ func translate(node *nodes.Node) (ast.Node, error) {
}
stmt.Params.Items = append(stmt.Params.Items, fp)
}
if stmt.ReturnType != nil && stmt.ReturnType.Name == "record" {
stmt.ReturnTable = funcReturnTable(stmt)
}
return stmt, nil

case *nodes.Node_CreateSchemaStmt:
Expand Down Expand Up @@ -629,3 +632,25 @@ func translate(node *nodes.Node) (ast.Node, error) {
return convert(node)
}
}

// makes a pseudo table from a function for table return type
func funcReturnTable(stmt *ast.CreateFunctionStmt) *ast.CreateTableStmt {
name := ast.TableName{
Name: stmt.Func.Name,
Catalog: stmt.Func.Catalog,
Schema: stmt.Func.Schema,
}
table := &ast.CreateTableStmt{
Name: &name,
}
for _, param := range stmt.Params.Items {
funcParam := param.(*ast.FuncParam)
if funcParam.Mode == ast.FuncParamTable {
table.Cols = append(table.Cols, &ast.ColumnDef{
Colname: *funcParam.Name,
TypeName: funcParam.Type,
})
}
}
return table
}
5 changes: 3 additions & 2 deletions internal/sql/ast/create_function_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ type CreateFunctionStmt struct {
ReturnType *TypeName
Func *FuncName
// TODO: Undertand these two fields
Options *List
WithClause *List
Options *List
WithClause *List
ReturnTable *CreateTableStmt
}

func (n *CreateFunctionStmt) Pos() int {
Expand Down
31 changes: 31 additions & 0 deletions internal/sql/catalog/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package catalog

import (
"errors"
"fmt"

"github.com/kyleconroy/sqlc/internal/sql/ast"
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
Expand All @@ -17,6 +18,7 @@ type Function struct {
Comment string
Desc string
ReturnTypeNullable bool
ReturnTable *Table
}

type Argument struct {
Expand Down Expand Up @@ -53,6 +55,9 @@ func (c *Catalog) createFunction(stmt *ast.CreateFunctionStmt) error {
Args: make([]*Argument, len(stmt.Params.Items)),
ReturnType: stmt.ReturnType,
}
if stmt.ReturnType.Name == "record" {
fn.ReturnTable = createFuncReturnTable(stmt.ReturnTable)
}
types := make([]*ast.TypeName, len(stmt.Params.Items))
for i, item := range stmt.Params.Items {
arg := item.(*ast.FuncParam)
Expand Down Expand Up @@ -82,6 +87,32 @@ func (c *Catalog) createFunction(stmt *ast.CreateFunctionStmt) error {
return nil
}

func createFuncReturnTable(stmt *ast.CreateTableStmt) *Table {
tbl := Table{
Rel: stmt.Name,
Comment: stmt.Comment,
}
for _, col := range stmt.Cols {
tc := &Column{
Name: col.Colname,
Type: *col.TypeName,
IsNotNull: col.IsNotNull,
IsUnsigned: col.IsUnsigned,
IsArray: col.IsArray,
Comment: col.Comment,
Length: col.Length,
}
if col.Vals != nil {
typeName := ast.TypeName{
Name: fmt.Sprintf("%s_%s", stmt.Name.Name, col.Colname),
}
tc.Type = typeName
}
tbl.Columns = append(tbl.Columns, tc)
}
return &tbl
}

func (c *Catalog) dropFunction(stmt *ast.DropFunctionStmt) error {
for _, spec := range stmt.Funcs {
ns := spec.Name.Schema
Expand Down