Skip to content

catalog: support functions with table parameters #541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2020
Merged
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
4 changes: 4 additions & 0 deletions internal/endtoend/testdata/func_args/endtoend.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"experimental_parser_only": true
}

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/func_args/go/query.sql.go

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

5 changes: 4 additions & 1 deletion internal/endtoend/testdata/func_args/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ CREATE FUNCTION plus(a integer, b integer) RETURNS integer AS $$
END;
$$ LANGUAGE plpgsql;

CREATE FUNCTION table_args(x INT) RETURNS TABLE (y INT) AS 'SELECT x' LANGUAGE sql;

-- name: Plus :one
SELECT plus(b => $2, a => $1);

Expand All @@ -16,4 +18,5 @@ SELECT make_interval(days => $1::int);
-- name: MakeIntervalMonths :one
SELECT make_interval(months => sqlc.arg('months')::int);


-- name: TableArgs :one
SELECT table_args(x => $1);
22 changes: 22 additions & 0 deletions internal/postgresql/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ func parseFuncName(node nodes.Node) (*ast.FuncName, error) {
}, nil
}

func parseFuncParamMode(m nodes.FunctionParameterMode) (ast.FuncParamMode, error) {
switch m {
case 'i':
return ast.FuncParamIn, nil
case 'o':
return ast.FuncParamOut, nil
case 'b':
return ast.FuncParamInOut, nil
case 'v':
return ast.FuncParamVariadic, nil
case 't':
return ast.FuncParamTable, nil
default:
return -1, fmt.Errorf("parse func param: invalid mode %v", m)
}
}

func parseTypeName(node nodes.Node) (*ast.TypeName, error) {
rel, err := parseRelation(node)
if err != nil {
Expand Down Expand Up @@ -434,9 +451,14 @@ func translate(node nodes.Node) (ast.Node, error) {
if err != nil {
return nil, err
}
mode, err := parseFuncParamMode(arg.Mode)
if err != nil {
return nil, err
}
fp := &ast.FuncParam{
Name: arg.Name,
Type: tn,
Mode: mode,
}
if arg.Defexpr != nil {
fp.DefExpr = &ast.TODO{}
Expand Down
11 changes: 11 additions & 0 deletions internal/sql/ast/func_param.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package ast

type FuncParamMode int

const (
FuncParamIn FuncParamMode = iota
FuncParamOut
FuncParamInOut
FuncParamVariadic
FuncParamTable
)

type FuncParam struct {
Name *string
Type *TypeName
DefExpr Node // Will always be &ast.TODO
Mode FuncParamMode
}

func (n *FuncParam) Pos() int {
Expand Down
20 changes: 18 additions & 2 deletions internal/sql/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,13 @@ func (s *Schema) getFunc(rel *ast.FuncName, tns []*ast.TypeName) (*Function, int
if s.Funcs[i].Name != rel.Name {
continue
}
if len(s.Funcs[i].Args) != len(tns) {

args := s.Funcs[i].InArgs()
if len(args) != len(tns) {
continue
}
found := true
for j := range s.Funcs[i].Args {
for j := range args {
if !sameType(s.Funcs[i].Args[j].Type, tns[j]) {
found = false
break
Expand Down Expand Up @@ -215,10 +217,24 @@ type Function struct {
Desc string
}

func (f *Function) InArgs() []*Argument {
var args []*Argument
for _, a := range f.Args {
switch a.Mode {
case ast.FuncParamTable, ast.FuncParamOut:
continue
default:
args = append(args, a)
}
}
return args
}

type Argument struct {
Name string
Type *ast.TypeName
HasDefault bool
Mode ast.FuncParamMode
}

func New(def string) *Catalog {
Expand Down
1 change: 1 addition & 0 deletions internal/sql/catalog/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (c *Catalog) createFunction(stmt *ast.CreateFunctionStmt) error {
fn.Args[i] = &Argument{
Name: name,
Type: arg.Type,
Mode: arg.Mode,
HasDefault: arg.DefExpr != nil,
}
types[i] = arg.Type
Expand Down
4 changes: 3 additions & 1 deletion internal/sql/catalog/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func (c *Catalog) GetFuncN(rel *ast.FuncName, n int) (Function, error) {
if s.Funcs[i].Name != rel.Name {
continue
}
if len(s.Funcs[i].Args) == n {

args := s.Funcs[i].InArgs()
if len(args) == n {
return *s.Funcs[i], nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/sql/validate/func_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (v *funcCallVisitor) Visit(node ast.Node) astutils.Visitor {
args = len(call.Args.Items)
}
for _, fun := range funs {
if len(fun.Args) == args {
if len(fun.InArgs()) == args {
return v
}
}
Expand Down