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
28 changes: 28 additions & 0 deletions server/cast/char.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ 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))
}
if out > pgtypes.MaxUint32 {
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 +300,20 @@ 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))
}
if out > pgtypes.MaxUint32 {
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
14 changes: 14 additions & 0 deletions server/cast/float32.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ func float32Explicit() {
return decimal.NewFromFloat(float64(val.(float32))), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float32,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, valInterface any, targetType pgtypes.DoltgresType) (any, error) {
return nil, errCannotCast.New(pgtypes.Float32.String(), targetType.String())
tbantle22 marked this conversation as resolved.
Show resolved Hide resolved
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float32,
ToType: pgtypes.Text,
Expand Down Expand Up @@ -192,6 +199,13 @@ func float32Implicit() {
return decimal.NewFromFloat(float64(val.(float32))), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float32,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, valInterface any, targetType pgtypes.DoltgresType) (any, error) {
return nil, errCannotCast.New(pgtypes.Float32.String(), targetType.String())
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float32,
ToType: pgtypes.Text,
Expand Down
14 changes: 14 additions & 0 deletions server/cast/float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ func float64Explicit() {
return decimal.NewFromFloat(val.(float64)), nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float64,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, valInterface any, targetType pgtypes.DoltgresType) (any, error) {
return nil, errCannotCast.New(pgtypes.Float64.String(), targetType.String())
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float64,
ToType: pgtypes.Text,
Expand Down Expand Up @@ -192,6 +199,13 @@ func float64Implicit() {
return decimal.NewFromFloat(val.(float64)), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float64,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, valInterface any, targetType pgtypes.DoltgresType) (any, error) {
return nil, errCannotCast.New(pgtypes.Float64.String(), targetType.String())
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Float64,
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 {
tbantle22 marked this conversation as resolved.
Show resolved Hide resolved
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 {
return nil, errOutOfRange.New(targetType.String())
}
return uint32(val.(int64)), nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Int64,
ToType: pgtypes.Text,
Expand Down
28 changes: 28 additions & 0 deletions server/cast/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ 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))
}
if out > pgtypes.MaxUint32 {
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 +300,20 @@ 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))
}
if out > pgtypes.MaxUint32 {
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
14 changes: 14 additions & 0 deletions server/cast/numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ func numericExplicit() {
return val, nil
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Numeric,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return nil, errCannotCast.New(pgtypes.Numeric.String(), targetType.String())
},
})
framework.MustAddExplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Numeric,
ToType: pgtypes.Text,
Expand Down Expand Up @@ -189,6 +196,13 @@ func numericImplicit() {
return val, nil
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Numeric,
ToType: pgtypes.Oid,
Function: func(ctx framework.Context, val any, targetType pgtypes.DoltgresType) (any, error) {
return nil, errCannotCast.New(pgtypes.Numeric.String(), targetType.String())
},
})
framework.MustAddImplicitTypeCast(framework.TypeCast{
FromType: pgtypes.Numeric,
ToType: pgtypes.Text,
Expand Down
Loading
Loading