Skip to content

fix: #1574 nullable return types for groupby functions #1631

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

Closed
wants to merge 1 commit into from
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
26 changes: 20 additions & 6 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,26 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
}
fun, err := qc.catalog.ResolveFuncCall(n)
if err == nil {
cols = append(cols, &Column{
Name: name,
DataType: dataType(fun.ReturnType),
NotNull: !fun.ReturnTypeNullable,
IsFuncCall: true,
})
if len(fun.Args) > 0 {
ref := n.Args.Items[0].(*ast.ColumnRef)
columns, err := outputColumnRefs(res, tables, ref)
if err != nil {
return nil, err
}
cols = append(cols, &Column{
Name: name,
DataType: columns[0].DataType,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that unfortunately the fix is a lot more complex than this, this is saying that any function with more than 0 args the return type should the type of its first arg?

This is true for most aggregates, (min, max, avg, every) but is not generally true of any function call - in particular count, array_agg and probably any user-defined aggregate.

https://www.postgresql.org/docs/15/functions-aggregate.html

I think special cases for these common aggregates is probably a useful change.

NotNull: !fun.ReturnTypeNullable,
IsFuncCall: true,
})
} else {
cols = append(cols, &Column{
Name: name,
DataType: dataType(fun.ReturnType),
NotNull: !fun.ReturnTypeNullable,
IsFuncCall: true,
})
}
} else {
cols = append(cols, &Column{
Name: name,
Expand Down
26 changes: 16 additions & 10 deletions internal/tools/sqlc-pg-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ SELECT p.proname as name,
format_type(p.prorettype, NULL),
array(select format_type(unnest(p.proargtypes), NULL)),
p.proargnames,
p.proargnames[p.pronargs-p.pronargdefaults+1:p.pronargs]
p.proargnames[p.pronargs-p.pronargdefaults+1:p.pronargs],
CASE WHEN p.prokind = 'a' THEN TRUE ELSE FALSE END
Copy link
Contributor

@ryan-berger ryan-berger Jun 6, 2022

Choose a reason for hiding this comment

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

I mentioned this in the discussion we were having, but I thought it is worth mentioning here.

We probably should do a CASE WHEN p.prokind = 'a' AND p.name != 'COUNT ....' since COUNT is an exception to the nullability rules.

Also, I don't think there needs to be a CASE since you should just be able to use p.prokind = 'a' AND p.name != 'COUNT' as a boolean expression

FROM pg_catalog.pg_proc p
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
WHERE n.nspname OPERATOR(pg_catalog.~) '^(pg_catalog)$'
Expand All @@ -49,7 +50,8 @@ SELECT p.proname as name,
format_type(p.prorettype, NULL),
array(select format_type(unnest(p.proargtypes), NULL)),
p.proargnames,
p.proargnames[p.pronargs-p.pronargdefaults+1:p.pronargs]
p.proargnames[p.pronargs-p.pronargdefaults+1:p.pronargs],
false
FROM pg_catalog.pg_proc p
JOIN extension_funcs ef ON ef.oid = p.oid
WHERE p.proargmodes IS NULL
Expand Down Expand Up @@ -86,6 +88,7 @@ func {{.Name}}() *catalog.Schema {
{{end}}
},
ReturnType: &ast.TypeName{Name: "{{.ReturnType.Name}}"},
ReturnTypeNullable: {{.ReturnTypeNullable}},
},
{{- end}}
}
Expand Down Expand Up @@ -127,11 +130,12 @@ func main() {
}

type Proc struct {
Name string
ReturnType string
ArgTypes []string
ArgNames []string
HasDefault []string
Name string
ReturnType string
ArgTypes []string
ArgNames []string
HasDefault []string
ReturnsNull bool
}

func clean(arg string) string {
Expand All @@ -144,9 +148,10 @@ func clean(arg string) string {

func (p Proc) Func() catalog.Function {
return catalog.Function{
Name: p.Name,
Args: p.Args(),
ReturnType: &ast.TypeName{Name: clean(p.ReturnType)},
Name: p.Name,
Args: p.Args(),
ReturnType: &ast.TypeName{Name: clean(p.ReturnType)},
ReturnTypeNullable: p.ReturnsNull,
}
}

Expand Down Expand Up @@ -185,6 +190,7 @@ func scanFuncs(rows pgx.Rows) ([]catalog.Function, error) {
&p.ArgTypes,
&p.ArgNames,
&p.HasDefault,
&p.ReturnsNull,
)
if err != nil {
return nil, err
Expand Down