Skip to content
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

Fixed function resolution for NULLs #530

Merged
merged 1 commit into from
Jul 25, 2024
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
26 changes: 8 additions & 18 deletions server/functions/format_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
// initFormatType registers the functions to the catalog.
func initFormatType() {
framework.RegisterFunction(format_type)
framework.RegisterFunction(format_type_null)
}

// format_type represents the PostgreSQL function of the same name, taking the same parameters.
Expand All @@ -35,24 +34,15 @@ var format_type = framework.Function2{
Return: pgtypes.Text,
Parameters: [2]pgtypes.DoltgresType{pgtypes.Oid, pgtypes.Int32},
Callable: func(ctx *sql.Context, _ [3]pgtypes.DoltgresType, val1, val2 any) (any, error) {
toid := val1.(uint32)
typemod := val2.(int32)
if t, ok := types.OidToType[oid.Oid(toid)]; ok {
return t.SQLStandardNameWithTypmod(true, int(typemod)), nil
if val1 == nil {
return nil, nil
}
return "???", nil
},
}

// format_type_null represents the PostgreSQL function format_type, but with a null second parameter.
var format_type_null = framework.Function2{
Name: "format_type",
Return: pgtypes.Text,
Parameters: [2]pgtypes.DoltgresType{pgtypes.Oid, pgtypes.Null},
Callable: func(ctx *sql.Context, _ [3]pgtypes.DoltgresType, val1, val2 any) (any, error) {
toid := val1.(uint32)
if t, ok := types.OidToType[oid.Oid(toid)]; ok {
return t.SQLStandardName(), nil
if t, ok := types.OidToType[oid.Oid(val1.(uint32))]; ok {
if val2 == nil {
return t.SQLStandardName(), nil
} else {
return t.SQLStandardNameWithTypmod(true, int(val2.(int32))), nil
}
}
return "???", nil
},
Expand Down
14 changes: 9 additions & 5 deletions server/functions/framework/compiled_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ func (c *CompiledFunction) resolveFunction(parameters []pgtypes.DoltgresType, so
var polymorphicParameters []pgtypes.DoltgresType
var polymorphicTargets []pgtypes.DoltgresType
for i, overloadParam := range overload {
if polymorphicType, ok := overloadParam.GetRepresentativeType().(pgtypes.DoltgresPolymorphicType); ok && polymorphicType.IsValid(parameters[i]) {
if parameters[i].BaseID() == pgtypes.DoltgresTypeBaseID_Null {
overloadCasts[i] = identityCast
} else if polymorphicType, ok := overloadParam.GetRepresentativeType().(pgtypes.DoltgresPolymorphicType); ok && polymorphicType.IsValid(parameters[i]) {
overloadCasts[i] = identityCast
polymorphicParameters = append(polymorphicParameters, polymorphicType)
polymorphicTargets = append(polymorphicTargets, parameters[i])
Expand Down Expand Up @@ -320,8 +322,10 @@ func (c *CompiledFunction) resolveFunction(parameters []pgtypes.DoltgresType, so
var matchCasts [][]TypeCastFunction
for convertibleIdx, convertible := range convertibles {
currentMatchCount := 0
for paramIdx, param := range convertible {
if parameters[paramIdx].BaseID() == param {
for paramIdx, targetParam := range convertible {
// NULL values count as exact matches, since all types accept NULL as a valid value
paramBaseID := parameters[paramIdx].BaseID()
if paramBaseID == targetParam || paramBaseID == pgtypes.DoltgresTypeBaseID_Null {
currentMatchCount++
}
}
Expand Down Expand Up @@ -350,8 +354,8 @@ func (c *CompiledFunction) resolveFunction(parameters []pgtypes.DoltgresType, so
var preferredCasts [][]TypeCastFunction
for matchIdx, match := range matches {
currentPreferredCount := 0
for paramIdx, param := range match {
if parameters[paramIdx].BaseID() != param && param.GetTypeCategory().IsPreferredType(param) {
for paramIdx, matchParam := range match {
if parameters[paramIdx].BaseID() != matchParam && matchParam.GetTypeCategory().IsPreferredType(matchParam) {
currentPreferredCount++
}
}
Expand Down
Loading