Skip to content

Commit

Permalink
Merge pull request #202 from dolthub/taylor/name-type
Browse files Browse the repository at this point in the history
Implement `name` type
  • Loading branch information
tbantle22 committed Apr 30, 2024
2 parents 1b37960 + 7db4d34 commit 5df6f24
Show file tree
Hide file tree
Showing 25 changed files with 857 additions and 7 deletions.
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
2 changes: 2 additions & 0 deletions server/ast/resolvable_type_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ func nodeResolvableTypeReference(typ tree.ResolvableTypeReference) (*vitess.Conv
columnTypeName = "JSON"
case oid.T_jsonb:
columnTypeName = "JSON"
case oid.T_name:
resolvedType = pgtypes.Name
case oid.T_numeric:
if columnType.Precision() == 0 && columnType.Scale() == 0 {
resolvedType = pgtypes.Numeric
Expand Down
22 changes: 22 additions & 0 deletions server/cast/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ func boolExplicit() {
}
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Bool,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := "false"
if val.(bool) {
str = "true"
}
return handleCharExplicitCast(str, targetType)
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Bool,
ToType: pgtypes.Text,
Expand Down Expand Up @@ -100,6 +111,17 @@ func boolImplicit() {
return handleCharImplicitCast(str, targetType)
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Bool,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := "false"
if val.(bool) {
str = "true"
}
return handleCharImplicitCast(str, targetType)
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Bool,
ToType: pgtypes.Text,
Expand Down
16 changes: 16 additions & 0 deletions server/cast/bytea.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ func byteaExplicit() {
return val, nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Bytea,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := `\x` + hex.EncodeToString(val.([]byte))
return handleCharExplicitCast(str, targetType)
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Bytea,
ToType: pgtypes.Text,
Expand Down Expand Up @@ -79,6 +87,14 @@ func byteaImplicit() {
return val, nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Bytea,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := `\x` + hex.EncodeToString(val.([]byte))
return handleCharImplicitCast(str, targetType)
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Bytea,
ToType: pgtypes.Text,
Expand Down
14 changes: 14 additions & 0 deletions server/cast/char.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ func charExplicit() {
return out, nil
},
})
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)
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.BpChar,
ToType: pgtypes.Numeric,
Expand Down Expand Up @@ -261,6 +268,13 @@ func charImplicit() {
return out, nil
},
})
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)
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.BpChar,
ToType: pgtypes.Numeric,
Expand Down
16 changes: 16 additions & 0 deletions server/cast/float32.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ func float32Explicit() {
return int64(val), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float32,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatFloat(float64(val.(float32)), 'g', -1, 32)
return handleCharExplicitCast(str, targetType)
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float32,
ToType: pgtypes.Numeric,
Expand Down Expand Up @@ -169,6 +177,14 @@ func float32Implicit() {
return int64(val), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float32,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatFloat(float64(val.(float32)), 'g', -1, 32)
return handleCharImplicitCast(str, targetType)
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float32,
ToType: pgtypes.Numeric,
Expand Down
16 changes: 16 additions & 0 deletions server/cast/float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ func float64Explicit() {
return int64(val), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float64,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatFloat(val.(float64), 'g', -1, 64)
return handleCharExplicitCast(str, targetType)
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float64,
ToType: pgtypes.Numeric,
Expand Down Expand Up @@ -169,6 +177,14 @@ func float64Implicit() {
return int64(val), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float64,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatFloat(val.(float64), 'g', -1, 64)
return handleCharImplicitCast(str, targetType)
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float64,
ToType: pgtypes.Numeric,
Expand Down
1 change: 1 addition & 0 deletions server/cast/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func Init() {
initInt16()
initInt32()
initInt64()
initName()
initNumeric()
initText()
initUuid()
Expand Down
16 changes: 16 additions & 0 deletions server/cast/int16.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ func int16Explicit() {
return int64(val.(int16)), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int16,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatInt(int64(val.(int16)), 10)
return handleCharExplicitCast(str, targetType)
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int16,
ToType: pgtypes.Numeric,
Expand Down Expand Up @@ -143,6 +151,14 @@ func int16Implicit() {
return int64(val.(int16)), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int16,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatInt(int64(val.(int16)), 10)
return handleCharImplicitCast(str, targetType)
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int16,
ToType: pgtypes.Numeric,
Expand Down
16 changes: 16 additions & 0 deletions server/cast/int32.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ func int32Explicit() {
return int64(val.(int32)), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int32,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatInt(int64(val.(int32)), 10)
return handleCharExplicitCast(str, targetType)
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int32,
ToType: pgtypes.Numeric,
Expand Down Expand Up @@ -150,6 +158,14 @@ func int32Implicit() {
return int64(val.(int32)), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int32,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatInt(int64(val.(int32)), 10)
return handleCharImplicitCast(str, targetType)
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int32,
ToType: pgtypes.Numeric,
Expand Down
16 changes: 16 additions & 0 deletions server/cast/int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ func int64Explicit() {
return val, nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int64,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatInt(val.(int64), 10)
return handleCharExplicitCast(str, targetType)
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int64,
ToType: pgtypes.Numeric,
Expand Down Expand Up @@ -156,6 +164,14 @@ func int64Implicit() {
return val, nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int64,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatInt(val.(int64), 10)
return handleCharImplicitCast(str, targetType)
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int64,
ToType: pgtypes.Numeric,
Expand Down
Loading

0 comments on commit 5df6f24

Please sign in to comment.