Skip to content

Commit 1b4c47b

Browse files
mysql: allow some builtin functions to be nullable (#616)
Co-authored-by: Jessica Bellon <10819486+JessTheBell@users.noreply.github.com> Co-authored-by: Kyle Conroy <kyle@conroy.org>
1 parent ae36ce2 commit 1b4c47b

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

internal/mysql/functions.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,15 @@ func functionReturnType(f string) string {
1818
panic(fmt.Sprintf("unknown mysql function type \"%v\"", f))
1919
}
2020
}
21+
22+
// returns true if MySQL function can return null.
23+
// See: https://dev.mysql.com/doc/refman/8.0/en/function-reference.html
24+
func functionIsNullable(f string) bool {
25+
switch f {
26+
case "avg", "sum", "min", "max", "mod",
27+
"concat", "left", "find_in_set", "group_concat":
28+
return true
29+
default:
30+
return false
31+
}
32+
}

internal/mysql/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ func (pGen PackageGenerator) parseSelectAliasExpr(exprs sqlparser.SelectExprs, t
450450
Name: returnVal,
451451
Type: sqlparser.ColumnType{
452452
Type: funcType,
453-
NotNull: true,
453+
NotNull: sqlparser.BoolVal(!functionIsNullable(funcName)),
454454
},
455455
}
456456
cols = append(cols, Column{colDfn, ""}) // func returns types don't originate from a table schema

0 commit comments

Comments
 (0)