-
Notifications
You must be signed in to change notification settings - Fork 885
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Also, I don't think there needs to be a |
||
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)$' | ||
|
@@ -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 | ||
|
@@ -86,6 +88,7 @@ func {{.Name}}() *catalog.Schema { | |
{{end}} | ||
}, | ||
ReturnType: &ast.TypeName{Name: "{{.ReturnType.Name}}"}, | ||
ReturnTypeNullable: {{.ReturnTypeNullable}}, | ||
}, | ||
{{- end}} | ||
} | ||
|
@@ -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 { | ||
|
@@ -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, | ||
} | ||
} | ||
|
||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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.