Skip to content

Commit

Permalink
Add cast
Browse files Browse the repository at this point in the history
  • Loading branch information
tbantle22 committed Apr 29, 2024
1 parent f8225ec commit e8dc356
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ Type structs should not handle PostgreSQL types, even which is different compare
For example `SMALLINT` and `INTEGER` are classified as two different types in DoltgreSQL, while they both fall under a generic `Number` type in [GMS](https://github.com/dolthub/go-mysql-server).
This difference is due to how types are treated in PostgreSQL, with one such example being function overloading, where two different functions sharing the same name are differentiated only by their parameter types.

When adding types, it is usually necessary to also add any relevant casts to `server/functions/framework/cast.go::init()`.
When adding types, it is usually necessary to also add any relevant casts to `server/cast/init.go::Init()`.
You'll need to add casts that go from existing types to the new type, and also from the new type to existing types (casts are not bidirectional).

TODO: adding operator functions
Expand Down
14 changes: 14 additions & 0 deletions server/cast/char.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ func charExplicit() {
return handleCharExplicitCast(val.(string), targetType)
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.BpChar,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return handleCharExplicitCast(val.(string), targetType)
},
})
}

// charImplicit registers all implicit casts. This comprises only the "From" types.
Expand Down Expand Up @@ -297,4 +304,11 @@ func charImplicit() {
return handleCharImplicitCast(val.(string), targetType)
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.BpChar,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return handleCharImplicitCast(val.(string), targetType)
},
})
}
9 changes: 9 additions & 0 deletions server/cast/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func handleCharExplicitCast(str string, targetType pgtypes.DoltgresType) (string
return str + strings.Repeat(" ", int(targetType.Length-runeCount)), nil
}
return str, nil
case pgtypes.NameType:
str, _ = truncateString(str, targetType.Length)
return str, nil
case pgtypes.VarCharType:
if targetType.IsUnbounded() {
return str, nil
Expand Down Expand Up @@ -61,6 +64,12 @@ func handleCharImplicitCast(str string, targetType pgtypes.DoltgresType) (string
return str, nil
}
}
case pgtypes.NameType:
if uint32(utf8.RuneCountInString(str)) > targetType.Length {
return "", fmt.Errorf("value too long for type %s", targetType.String())
} else {
return str, nil
}
case pgtypes.VarCharType:
if !targetType.IsUnbounded() && uint32(utf8.RuneCountInString(str)) > targetType.Length {
return "", fmt.Errorf("value too long for type %s", targetType.String())
Expand Down
14 changes: 14 additions & 0 deletions server/cast/varchar.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ func varcharExplicit() {
return handleCharExplicitCast(val.(string), targetType)
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.VarChar,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return handleCharExplicitCast(val.(string), targetType)
},
})
}

// varcharImplicit registers all implicit casts. This comprises only the "From" types.
Expand Down Expand Up @@ -297,4 +304,11 @@ func varcharImplicit() {
return handleCharImplicitCast(val.(string), targetType)
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.VarChar,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return handleCharImplicitCast(val.(string), targetType)
},
})
}

0 comments on commit e8dc356

Please sign in to comment.