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

Implement oid type #204

Merged
merged 15 commits into from
May 2, 2024
2 changes: 2 additions & 0 deletions server/ast/resolvable_type_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ func nodeResolvableTypeReference(typ tree.ResolvableTypeReference) (*vitess.Conv
Scale: columnType.Scale(),
}
}
case oid.T_oid:
resolvedType = pgtypes.Oid
case oid.T_text:
resolvedType = pgtypes.Text
case oid.T_time:
Expand Down
11 changes: 11 additions & 0 deletions server/cast/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ func boolExplicit() {
return handleCharExplicitCast(str, targetType)
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Bool,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
if val.(bool) {
return uint32(1), nil
} else {
return uint32(0), nil
}
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Bool,
ToType: pgtypes.Text,
Expand Down
30 changes: 30 additions & 0 deletions server/cast/char.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ func charExplicit() {
return d, nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.BpChar,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
out, err := strconv.ParseInt(strings.TrimSpace(val.(string)), 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid input syntax for type %s: %q", targetType.String(), val.(string))
}
// Note: This minimum is different (-4294967295) for Postgres 15.4 compiled by Visual C++
if out > pgtypes.MaxUint32 || out < pgtypes.MinInt32 {
return nil, fmt.Errorf("value %q is out of range for type %s", val.(string), targetType.String())
}
return uint32(out), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.BpChar,
ToType: pgtypes.Text,
Expand Down Expand Up @@ -286,6 +301,21 @@ func charImplicit() {
return d, nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.BpChar,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
out, err := strconv.ParseInt(strings.TrimSpace(val.(string)), 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid input syntax for type %s: %q", targetType.String(), val.(string))
}
// Note: This minimum is different (-4294967295) for Postgres 15.4 compiled by Visual C++
if out > pgtypes.MaxUint32 || out < pgtypes.MinInt32 {
return nil, fmt.Errorf("value %q is out of range for type %s", val.(string), targetType.String())
}
return uint32(out), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.BpChar,
ToType: pgtypes.Text,
Expand Down
1 change: 1 addition & 0 deletions server/cast/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func Init() {
initInt64()
initName()
initNumeric()
initOid()
initText()
initUuid()
initVarChar()
Expand Down
14 changes: 14 additions & 0 deletions server/cast/int16.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ func int16Explicit() {
return decimal.NewFromInt(int64(val.(int16))), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int16,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return uint32(val.(int16)), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int16,
ToType: pgtypes.Text,
Expand Down Expand Up @@ -166,6 +173,13 @@ func int16Implicit() {
return decimal.NewFromInt(int64(val.(int16))), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int16,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return uint32(val.(int16)), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int16,
ToType: pgtypes.Text,
Expand Down
14 changes: 14 additions & 0 deletions server/cast/int32.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ func int32Explicit() {
return decimal.NewFromInt(int64(val.(int32))), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int32,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return uint32(val.(int32)), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int32,
ToType: pgtypes.Text,
Expand Down Expand Up @@ -173,6 +180,13 @@ func int32Implicit() {
return decimal.NewFromInt(int64(val.(int32))), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int32,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return uint32(val.(int32)), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int32,
ToType: pgtypes.Text,
Expand Down
20 changes: 20 additions & 0 deletions server/cast/int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ func int64Explicit() {
return decimal.NewFromInt(val.(int64)), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int64,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
if val.(int64) > pgtypes.MaxUint32 || val.(int64) < 0 {
return nil, errOutOfRange.New(targetType.String())
}
return uint32(val.(int64)), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int64,
ToType: pgtypes.Text,
Expand Down Expand Up @@ -179,6 +189,16 @@ func int64Implicit() {
return decimal.NewFromInt(val.(int64)), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int64,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
if val.(int64) > pgtypes.MaxUint32 || val.(int64) < 0 {
return nil, errOutOfRange.New(targetType.String())
}
return uint32(val.(int64)), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int64,
ToType: pgtypes.Text,
Expand Down
30 changes: 30 additions & 0 deletions server/cast/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ func nameExplicit() {
return d, nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Name,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
out, err := strconv.ParseInt(strings.TrimSpace(val.(string)), 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid input syntax for type %s: %q", targetType.String(), val.(string))
}
// Note: This minimum is different (-4294967295) for Postgres 15.4 compiled by Visual C++
if out > pgtypes.MaxUint32 || out < pgtypes.MinInt32 {
return nil, fmt.Errorf("value %q is out of range for type %s", val.(string), targetType.String())
}
return uint32(out), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Name,
ToType: pgtypes.Text,
Expand Down Expand Up @@ -286,6 +301,21 @@ func nameImplicit() {
return d, nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Name,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
out, err := strconv.ParseInt(strings.TrimSpace(val.(string)), 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid input syntax for type %s: %q", targetType.String(), val.(string))
}
// Note: This minimum is different (-4294967295) for Postgres 15.4 compiled by Visual C++
if out > pgtypes.MaxUint32 || out < pgtypes.MinInt32 {
return nil, fmt.Errorf("value %q is out of range for type %s", val.(string), targetType.String())
}
return uint32(out), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Name,
ToType: pgtypes.Text,
Expand Down
142 changes: 142 additions & 0 deletions server/cast/oid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright 2024 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cast

import (
"strconv"

"github.com/dolthub/doltgresql/server/functions/framework"
pgtypes "github.com/dolthub/doltgresql/server/types"
)

// initOid handles all explicit and implicit casts that are built-in. This comprises only the "From" types.
func initOid() {
oidExplicit()
oidImplicit()
}

// oidExplicit registers all explicit casts. This comprises only the "From" types.
func oidExplicit() {
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Oid,
ToType: pgtypes.BpChar,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatInt(int64(val.(uint32)), 10)
return handleCharExplicitCast(str, targetType)
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Oid,
ToType: pgtypes.Int32,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
// Will return -1 for uint32 values greater than 2147483647
return int32(val.(uint32)), nil
Hydrocharged marked this conversation as resolved.
Show resolved Hide resolved
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Oid,
ToType: pgtypes.Int64,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return int64(val.(uint32)), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Oid,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatInt(int64(val.(uint32)), 10)
return handleCharExplicitCast(str, targetType)
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Oid,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return val, nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Oid,
ToType: pgtypes.Text,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return strconv.FormatInt(int64(val.(uint32)), 10), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Oid,
ToType: pgtypes.VarChar,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatInt(int64(val.(uint32)), 10)
return handleCharExplicitCast(str, targetType)
},
})
}

// oidImplicit registers all implicit casts. This comprises only the "From" types.
func oidImplicit() {
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Oid,
ToType: pgtypes.BpChar,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatInt(int64(val.(uint32)), 10)
return handleCharImplicitCast(str, targetType)
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Oid,
ToType: pgtypes.Int32,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
// Will return -1 for uint32 values greater than 2147483647
return int32(val.(uint32)), nil
tbantle22 marked this conversation as resolved.
Show resolved Hide resolved
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Oid,
ToType: pgtypes.Int64,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return int64(val.(uint32)), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Oid,
ToType: pgtypes.Name,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatInt(int64(val.(uint32)), 10)
return handleCharExplicitCast(str, targetType)
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Oid,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return val, nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Oid,
ToType: pgtypes.Text,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return strconv.FormatInt(int64(val.(uint32)), 10), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Oid,
ToType: pgtypes.VarChar,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
str := strconv.FormatInt(int64(val.(uint32)), 10)
return handleCharImplicitCast(str, targetType)
},
})
}
30 changes: 30 additions & 0 deletions server/cast/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ func textExplicit() {
return d, nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Text,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
out, err := strconv.ParseInt(strings.TrimSpace(val.(string)), 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid input syntax for type %s: %q", targetType.String(), val.(string))
}
// Note: This minimum is different (-4294967295) for Postgres 15.4 compiled by Visual C++
if out > pgtypes.MaxUint32 || out < pgtypes.MinInt32 {
return nil, fmt.Errorf("value %q is out of range for type %s", val.(string), targetType.String())
}
return uint32(out), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Text,
ToType: pgtypes.Text,
Expand Down Expand Up @@ -286,6 +301,21 @@ func textImplicit() {
return d, nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Text,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
out, err := strconv.ParseInt(strings.TrimSpace(val.(string)), 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid input syntax for type %s: %q", targetType.String(), val.(string))
}
// Note: This minimum is different (-4294967295) for Postgres 15.4 compiled by Visual C++
if out > pgtypes.MaxUint32 || out < pgtypes.MinInt32 {
return nil, fmt.Errorf("value %q is out of range for type %s", val.(string), targetType.String())
}
return uint32(out), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Text,
ToType: pgtypes.Text,
Expand Down
Loading