Skip to content

Commit b2fc73e

Browse files
authored
internal/catalog: Parse unnamed function arguments (#166)
Arguments to SQL functions are not required to have a name
1 parent 58dfc7e commit b2fc73e

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

internal/catalog/build.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,12 @@ func Update(c *pg.Catalog, stmt nodes.Node) error {
353353
args := make([]pg.Argument, arity)
354354
for i, item := range n.Parameters.Items {
355355
arg := item.(nodes.FunctionParameter)
356+
var name string
357+
if arg.Name != nil {
358+
name = *arg.Name
359+
}
356360
args[i] = pg.Argument{
357-
Name: *arg.Name,
361+
Name: name,
358362
DataType: join(arg.ArgType.Names, "."),
359363
HasDefault: arg.Defexpr != nil,
360364
}

internal/catalog/build_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,13 @@ func TestUpdate(t *testing.T) {
322322
`,
323323
pg.NewCatalog(),
324324
},
325+
{
326+
`
327+
DROP FUNCTION IF EXISTS bar(text);
328+
DROP FUNCTION IF EXISTS bar(text) CASCADE;
329+
`,
330+
pg.NewCatalog(),
331+
},
325332
{
326333
`
327334
CREATE TABLE venues (id SERIAL PRIMARY KEY);
@@ -343,6 +350,31 @@ func TestUpdate(t *testing.T) {
343350
},
344351
},
345352
},
353+
{ // first argument has no name
354+
`
355+
CREATE FUNCTION foo(TEXT) RETURNS bool AS $$ SELECT true $$ LANGUAGE sql;
356+
`,
357+
pg.Catalog{
358+
Schemas: map[string]pg.Schema{
359+
"public": {
360+
Funcs: map[string][]pg.Function{
361+
"foo": []pg.Function{
362+
{
363+
Name: "foo",
364+
Arguments: []pg.Argument{
365+
{
366+
Name: "",
367+
DataType: "text",
368+
},
369+
},
370+
ReturnType: "bool",
371+
},
372+
},
373+
},
374+
},
375+
},
376+
},
377+
},
346378
{ // same name, different arity
347379
`
348380
CREATE FUNCTION foo(bar TEXT) RETURNS bool AS $$ SELECT true $$ LANGUAGE sql;

0 commit comments

Comments
 (0)