Skip to content

sql/catalog: Add support for variadic functions #798

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
Nov 19, 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
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/json_build/go/db.go

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

5 changes: 5 additions & 0 deletions internal/endtoend/testdata/json_build/go/models.go

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

63 changes: 63 additions & 0 deletions internal/endtoend/testdata/json_build/go/query.sql.go

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

13 changes: 13 additions & 0 deletions internal/endtoend/testdata/json_build/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- name: SelectJSONBuildObject :one
SELECT
json_build_object('foo'),
json_build_object('foo', 1),
json_build_object('foo', 1, 2),
json_build_object('foo', 1, 2, 'bar');

-- name: SelectJSONBuildArray :one
SELECT
json_build_array(1),
json_build_array(1, 2),
json_build_array(1, 2, 'foo'),
json_build_array(1, 2, 'foo', 4);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/json_build/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
18 changes: 14 additions & 4 deletions internal/engine/postgresql/pg_catalog.go

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

1 change: 1 addition & 0 deletions internal/sql/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ type Argument struct {
Name string
Type *ast.TypeName
HasDefault bool
Variadic bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add this struct key when there is a mode for Variadic that you can use to check?

Mode ast.FuncParamMode
}

Expand Down
22 changes: 17 additions & 5 deletions internal/sql/catalog/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,32 @@ func (c *Catalog) ResolveFuncCall(call *ast.FuncCall) (*Function, error) {
for _, fun := range funs {
args := fun.InArgs()
var defaults int
var variadic bool
known := map[string]struct{}{}
for _, arg := range args {
if arg.HasDefault {
defaults += 1
}
if arg.Variadic {
variadic = true
}
if arg.Name != "" {
known[arg.Name] = struct{}{}
}
}
if (len(named) + len(positional)) > len(args) {
continue
}
if (len(named) + len(positional)) < (len(args) - defaults) {
continue

if variadic {
// For now, assume variadic fucntions can't also have defaults
if (len(named) + len(positional)) < len(args) {
continue
}
} else {
if (len(named) + len(positional)) > len(args) {
continue
}
if (len(named) + len(positional)) < (len(args) - defaults) {
continue
}
}

// Validate that the provided named arguments exist in the function
Expand Down