chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
// Copyright 2026 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 (
|
||||
"github.com/cockroachdb/errors"
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/postgres/parser/sem/tree"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initBit handles all casts that are built-in. This comprises only the source types.
|
||||
func initBit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
bitExplicit(builtInCasts)
|
||||
bitImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// bitExplicit registers all explicit casts. This comprises only the source types.
|
||||
func bitExplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
bitToInt32 := id.NewCast(pgtypes.Bit.ID, pgtypes.Int32.ID)
|
||||
builtInCasts[bitToInt32] = casts.Cast{
|
||||
ID: bitToInt32,
|
||||
CastType: casts.CastType_Explicit,
|
||||
Function: id.NullFunction,
|
||||
BuiltIn: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
array, err := tree.ParseDBitArray(val.(string))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if array.BitLen() > 32 {
|
||||
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "integer out of range")
|
||||
}
|
||||
return int32(array.AsInt64(32)), nil
|
||||
},
|
||||
UseInOut: false,
|
||||
}
|
||||
bitToInt64 := id.NewCast(pgtypes.Bit.ID, pgtypes.Int64.ID)
|
||||
builtInCasts[bitToInt64] = casts.Cast{
|
||||
ID: bitToInt64,
|
||||
CastType: casts.CastType_Explicit,
|
||||
Function: id.NullFunction,
|
||||
BuiltIn: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
array, err := tree.ParseDBitArray(val.(string))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if array.BitLen() > 64 {
|
||||
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "bigint out of range")
|
||||
}
|
||||
return array.AsInt64(64), nil
|
||||
},
|
||||
UseInOut: false,
|
||||
}
|
||||
}
|
||||
|
||||
// bitImplicit registers all implicit casts. This comprises only the source types.
|
||||
func bitImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Bit,
|
||||
ToType: pgtypes.Bit,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
input := val.(string)
|
||||
array, err := tree.ParseDBitArray(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
expectedLength := targetType.GetAttTypMod()
|
||||
if array.BitLen() != uint(expectedLength) {
|
||||
return nil, pgtypes.ErrWrongLengthBit.New(len(input), expectedLength)
|
||||
}
|
||||
return tree.AsStringWithFlags(array, tree.FmtPgwireText), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Bit,
|
||||
ToType: pgtypes.VarBit,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
input := val.(string)
|
||||
array, err := tree.ParseDBitArray(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
atttypmod := targetType.GetAttTypMod()
|
||||
if atttypmod != -1 {
|
||||
if int32(array.BitLen()) > atttypmod {
|
||||
return nil, pgtypes.ErrVarBitLengthExceeded.New(atttypmod)
|
||||
}
|
||||
}
|
||||
return tree.AsStringWithFlags(array, tree.FmtPgwireText), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
// 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 (
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initBool handles all casts that are built-in. This comprises only the source types.
|
||||
func initBool(builtInCasts map[id.Cast]casts.Cast) {
|
||||
boolExplicit(builtInCasts)
|
||||
boolAssignment(builtInCasts)
|
||||
}
|
||||
|
||||
// boolExplicit registers all explicit casts. This comprises only the source types.
|
||||
func boolExplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddExplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Bool,
|
||||
ToType: pgtypes.Int32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if val.(bool) {
|
||||
return int32(1), nil
|
||||
} else {
|
||||
return int32(0), nil
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// boolAssignment registers all assignment casts. This comprises only the source types.
|
||||
func boolAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Bool,
|
||||
ToType: pgtypes.BpChar,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
str := "false"
|
||||
if val.(bool) {
|
||||
str = "true"
|
||||
}
|
||||
return handleStringCast(str, targetType)
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Bool,
|
||||
ToType: pgtypes.Name,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
str := "f"
|
||||
if val.(bool) {
|
||||
str = "t"
|
||||
}
|
||||
return handleStringCast(str, targetType)
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Bool,
|
||||
ToType: pgtypes.Text,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if val.(bool) {
|
||||
return "true", nil
|
||||
} else {
|
||||
return "false", nil
|
||||
}
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Bool,
|
||||
ToType: pgtypes.VarChar,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
str := "false"
|
||||
if val.(bool) {
|
||||
str = "true"
|
||||
}
|
||||
return handleStringCast(str, targetType)
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
// 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"
|
||||
"strings"
|
||||
|
||||
"github.com/cockroachdb/errors"
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initChar handles all casts that are built-in. This comprises only the source types.
|
||||
func initChar(builtInCasts map[id.Cast]casts.Cast) {
|
||||
charAssignment(builtInCasts)
|
||||
charExplicit(builtInCasts)
|
||||
charImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// charAssignment registers all assignment casts. This comprises only the source types.
|
||||
func charAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.BpChar,
|
||||
ToType: pgtypes.InternalChar,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return targetType.IoInput(ctx, val.(string))
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// charExplicit registers all explicit casts. This comprises only the source types.
|
||||
func charExplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddExplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.BpChar,
|
||||
ToType: pgtypes.Int32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
out, err := strconv.ParseInt(strings.TrimSpace(val.(string)), 10, 32)
|
||||
if err != nil {
|
||||
return nil, errors.Errorf("invalid input syntax for type %s: %q", targetType.String(), val.(string))
|
||||
}
|
||||
if out > 2147483647 || out < -2147483648 {
|
||||
return nil, errors.Errorf("value %q is out of range for type %s", val.(string), targetType.String())
|
||||
}
|
||||
return int32(out), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// charImplicit registers all implicit casts. This comprises only the source types.
|
||||
func charImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.BpChar,
|
||||
ToType: pgtypes.BpChar,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return targetType.IoInput(ctx, val.(string))
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.BpChar,
|
||||
ToType: pgtypes.Name,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return handleStringCast(val.(string), targetType)
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.BpChar,
|
||||
ToType: pgtypes.Text,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val, nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.BpChar,
|
||||
ToType: pgtypes.VarChar,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return handleStringCast(val.(string), targetType)
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// 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 (
|
||||
"time"
|
||||
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initDate handles all casts that are built-in. This comprises only the source types.
|
||||
func initDate(builtInCasts map[id.Cast]casts.Cast) {
|
||||
dateImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// dateImplicit registers all implicit casts. This comprises only the source types.
|
||||
func dateImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Date,
|
||||
ToType: pgtypes.Timestamp,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val.(time.Time), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Date,
|
||||
ToType: pgtypes.TimestampTZ,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val.(time.Time), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
// 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 (
|
||||
"math"
|
||||
|
||||
"github.com/cockroachdb/apd/v3"
|
||||
"github.com/cockroachdb/errors"
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initFloat32 handles all casts that are built-in. This comprises only the source types.
|
||||
func initFloat32(builtInCasts map[id.Cast]casts.Cast) {
|
||||
float32Assignment(builtInCasts)
|
||||
float32Implicit(builtInCasts)
|
||||
}
|
||||
|
||||
// float32Assignment registers all assignment casts. This comprises only the source types.
|
||||
func float32Assignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Float32,
|
||||
ToType: pgtypes.Int16,
|
||||
Function: func(ctx *sql.Context, valInterface any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
val := float32(math.RoundToEven(float64(valInterface.(float32))))
|
||||
if val > 32767 || val < -32768 {
|
||||
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "smallint out of range")
|
||||
}
|
||||
return int16(val), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Float32,
|
||||
ToType: pgtypes.Int32,
|
||||
Function: func(ctx *sql.Context, valInterface any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
val := float32(math.RoundToEven(float64(valInterface.(float32))))
|
||||
if val > 2147483647 || val < -2147483648 {
|
||||
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "integer out of range")
|
||||
}
|
||||
return int32(val), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Float32,
|
||||
ToType: pgtypes.Int64,
|
||||
Function: func(ctx *sql.Context, valInterface any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
val := float32(math.RoundToEven(float64(valInterface.(float32))))
|
||||
if val > 9223372036854775807 || val < -9223372036854775808 {
|
||||
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "bigint out of range")
|
||||
}
|
||||
return int64(val), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Float32,
|
||||
ToType: pgtypes.Numeric,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
d := new(apd.Decimal)
|
||||
err := d.Scan(float64(val.(float32)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pgtypes.GetNumericValueWithTypmod(d, targetType.GetAttTypMod())
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// float32Implicit registers all implicit casts. This comprises only the source types.
|
||||
func float32Implicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Float32,
|
||||
ToType: pgtypes.Float64,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return float64(val.(float32)), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
// 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 (
|
||||
"math"
|
||||
|
||||
"github.com/cockroachdb/apd/v3"
|
||||
"github.com/cockroachdb/errors"
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initFloat64 handles all casts that are built-in. This comprises only the source types.
|
||||
func initFloat64(builtInCasts map[id.Cast]casts.Cast) {
|
||||
float64Assignment(builtInCasts)
|
||||
}
|
||||
|
||||
// float64Assignment registers all assignment casts. This comprises only the source types.
|
||||
func float64Assignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Float64,
|
||||
ToType: pgtypes.Float32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return float32(val.(float64)), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Float64,
|
||||
ToType: pgtypes.Int16,
|
||||
Function: func(ctx *sql.Context, valInterface any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
val := math.RoundToEven(valInterface.(float64))
|
||||
if val > 32767 || val < -32768 {
|
||||
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "smallint out of range")
|
||||
}
|
||||
return int16(val), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Float64,
|
||||
ToType: pgtypes.Int32,
|
||||
Function: func(ctx *sql.Context, valInterface any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
val := math.RoundToEven(valInterface.(float64))
|
||||
if val > 2147483647 || val < -2147483648 {
|
||||
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "integer out of range")
|
||||
}
|
||||
return int32(val), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Float64,
|
||||
ToType: pgtypes.Int64,
|
||||
Function: func(ctx *sql.Context, valInterface any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
val := math.RoundToEven(valInterface.(float64))
|
||||
if val > 9223372036854775807 || val < -9223372036854775808 {
|
||||
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "bigint out of range")
|
||||
}
|
||||
return int64(val), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Float64,
|
||||
ToType: pgtypes.Numeric,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
d := new(apd.Decimal)
|
||||
err := d.Scan(val.(float64))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pgtypes.GetNumericValueWithTypmod(d, targetType.GetAttTypMod())
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// 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 (
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
)
|
||||
|
||||
// Init initializes all casts in this package.
|
||||
func Init(builtInCasts map[id.Cast]casts.Cast) {
|
||||
initBit(builtInCasts)
|
||||
initBool(builtInCasts)
|
||||
initChar(builtInCasts)
|
||||
initDate(builtInCasts)
|
||||
initFloat32(builtInCasts)
|
||||
initFloat64(builtInCasts)
|
||||
initInt16(builtInCasts)
|
||||
initInt32(builtInCasts)
|
||||
initInt64(builtInCasts)
|
||||
initInternalChar(builtInCasts)
|
||||
initInterval(builtInCasts)
|
||||
initJson(builtInCasts)
|
||||
initJsonB(builtInCasts)
|
||||
initName(builtInCasts)
|
||||
initNumeric(builtInCasts)
|
||||
initOid(builtInCasts)
|
||||
initRegclass(builtInCasts)
|
||||
initRegproc(builtInCasts)
|
||||
initRegtype(builtInCasts)
|
||||
initText(builtInCasts)
|
||||
initTime(builtInCasts)
|
||||
initTimestamp(builtInCasts)
|
||||
initTimestampTZ(builtInCasts)
|
||||
initTimeTZ(builtInCasts)
|
||||
initVarBit(builtInCasts)
|
||||
initVarChar(builtInCasts)
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
// 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 (
|
||||
"github.com/cockroachdb/apd/v3"
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initInt16 handles all casts that are built-in. This comprises only the source types.
|
||||
func initInt16(builtInCasts map[id.Cast]casts.Cast) {
|
||||
int16Implicit(builtInCasts)
|
||||
}
|
||||
|
||||
// int16Implicit registers all implicit casts. This comprises only the source types.
|
||||
func int16Implicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int16,
|
||||
ToType: pgtypes.Float32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return float32(val.(int16)), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int16,
|
||||
ToType: pgtypes.Float64,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return float64(val.(int16)), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int16,
|
||||
ToType: pgtypes.Int32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return int32(val.(int16)), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int16,
|
||||
ToType: pgtypes.Int64,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return int64(val.(int16)), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int16,
|
||||
ToType: pgtypes.Numeric,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return pgtypes.GetNumericValueWithTypmod(apd.New(int64(val.(int16)), 0), targetType.GetAttTypMod())
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int16,
|
||||
ToType: pgtypes.Oid,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if internalID := id.Cache().ToInternal(uint32(val.(int16))); internalID.IsValid() {
|
||||
return internalID, nil
|
||||
}
|
||||
return id.NewOID(uint32(val.(int16))).AsId(), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int16,
|
||||
ToType: pgtypes.Regclass,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if internalID := id.Cache().ToInternal(uint32(val.(int16))); internalID.IsValid() {
|
||||
return internalID, nil
|
||||
}
|
||||
return id.NewOID(uint32(val.(int16))).AsId(), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int16,
|
||||
ToType: pgtypes.Regproc,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if internalID := id.Cache().ToInternal(uint32(val.(int16))); internalID.IsValid() {
|
||||
return internalID, nil
|
||||
}
|
||||
return id.NewOID(uint32(val.(int16))).AsId(), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int16,
|
||||
ToType: pgtypes.Regtype,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if internalID := id.Cache().ToInternal(uint32(val.(int16))); internalID.IsValid() {
|
||||
return internalID, nil
|
||||
}
|
||||
return id.NewOID(uint32(val.(int16))).AsId(), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
// 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"
|
||||
"strings"
|
||||
|
||||
"github.com/cockroachdb/apd/v3"
|
||||
"github.com/cockroachdb/errors"
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initInt32 handles all casts that are built-in. This comprises only the source types.
|
||||
func initInt32(builtInCasts map[id.Cast]casts.Cast) {
|
||||
int32Explicit(builtInCasts)
|
||||
int32Assignment(builtInCasts)
|
||||
int32Implicit(builtInCasts)
|
||||
}
|
||||
|
||||
// int32Explicit registers all explicit casts. This comprises only the source types.
|
||||
func int32Explicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddExplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int32,
|
||||
ToType: pgtypes.Bit,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
width := 1
|
||||
if attTypMod := targetType.GetAttTypMod(); attTypMod != -1 {
|
||||
width = int(attTypMod)
|
||||
}
|
||||
bitStr := strconv.FormatInt(int64(val.(int32)), 2)
|
||||
if len(bitStr) > width {
|
||||
return bitStr[len(bitStr)-width:], nil
|
||||
} else if len(bitStr) < width {
|
||||
return strings.Repeat("0", width-len(bitStr)) + bitStr, nil
|
||||
} else {
|
||||
return bitStr, nil
|
||||
}
|
||||
},
|
||||
})
|
||||
framework.MustAddExplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int32,
|
||||
ToType: pgtypes.Bool,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val.(int32) != 0, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// int32Assignment registers all assignment casts. This comprises only the source types.
|
||||
func int32Assignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int32,
|
||||
ToType: pgtypes.Int16,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if val.(int32) > 32767 || val.(int32) < -32768 {
|
||||
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "smallint out of range")
|
||||
}
|
||||
return int16(val.(int32)), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// int32Implicit registers all implicit casts. This comprises only the source types.
|
||||
func int32Implicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int32,
|
||||
ToType: pgtypes.Float32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return float32(val.(int32)), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int32,
|
||||
ToType: pgtypes.Float64,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return float64(val.(int32)), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int32,
|
||||
ToType: pgtypes.Int64,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return int64(val.(int32)), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int32,
|
||||
ToType: pgtypes.Numeric,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return pgtypes.GetNumericValueWithTypmod(apd.New(int64(val.(int32)), 0), targetType.GetAttTypMod())
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int32,
|
||||
ToType: pgtypes.Oid,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if internalID := id.Cache().ToInternal(uint32(val.(int32))); internalID.IsValid() {
|
||||
return internalID, nil
|
||||
}
|
||||
return id.NewOID(uint32(val.(int32))).AsId(), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int32,
|
||||
ToType: pgtypes.Regclass,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if internalID := id.Cache().ToInternal(uint32(val.(int32))); internalID.IsValid() {
|
||||
return internalID, nil
|
||||
}
|
||||
return id.NewOID(uint32(val.(int32))).AsId(), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int32,
|
||||
ToType: pgtypes.Regproc,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if internalID := id.Cache().ToInternal(uint32(val.(int32))); internalID.IsValid() {
|
||||
return internalID, nil
|
||||
}
|
||||
return id.NewOID(uint32(val.(int32))).AsId(), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int32,
|
||||
ToType: pgtypes.Regtype,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if internalID := id.Cache().ToInternal(uint32(val.(int32))); internalID.IsValid() {
|
||||
return internalID, nil
|
||||
}
|
||||
return id.NewOID(uint32(val.(int32))).AsId(), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
// 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 (
|
||||
"math"
|
||||
|
||||
"github.com/cockroachdb/apd/v3"
|
||||
"github.com/cockroachdb/errors"
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initInt64 handles all casts that are built-in. This comprises only the source types.
|
||||
func initInt64(builtInCasts map[id.Cast]casts.Cast) {
|
||||
int64Assignment(builtInCasts)
|
||||
int64Implicit(builtInCasts)
|
||||
}
|
||||
|
||||
// int64Assignment registers all assignment casts. This comprises only the source types.
|
||||
func int64Assignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int64,
|
||||
ToType: pgtypes.Int16,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if val.(int64) > 32767 || val.(int64) < -32768 {
|
||||
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "smallint out of range")
|
||||
}
|
||||
return int16(val.(int64)), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int64,
|
||||
ToType: pgtypes.Int32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if val.(int64) > 2147483647 || val.(int64) < -2147483648 {
|
||||
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "integer out of range")
|
||||
}
|
||||
return int32(val.(int64)), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// int64Implicit registers all implicit casts. This comprises only the source types.
|
||||
func int64Implicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int64,
|
||||
ToType: pgtypes.Float32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return float32(val.(int64)), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int64,
|
||||
ToType: pgtypes.Float64,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return float64(val.(int64)), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int64,
|
||||
ToType: pgtypes.Numeric,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return pgtypes.GetNumericValueWithTypmod(apd.New(val.(int64), 0), targetType.GetAttTypMod())
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int64,
|
||||
ToType: pgtypes.Oid,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if val.(int64) > int64(math.MaxUint32) || val.(int64) < 0 {
|
||||
return nil, errOutOfRange.New(targetType.String())
|
||||
}
|
||||
if internalID := id.Cache().ToInternal(uint32(val.(int64))); internalID.IsValid() {
|
||||
return internalID, nil
|
||||
}
|
||||
return id.NewOID(uint32(val.(int64))).AsId(), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int64,
|
||||
ToType: pgtypes.Regclass,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if val.(int64) > int64(math.MaxUint32) || val.(int64) < 0 {
|
||||
return nil, errOutOfRange.New(targetType.String())
|
||||
}
|
||||
if internalID := id.Cache().ToInternal(uint32(val.(int64))); internalID.IsValid() {
|
||||
return internalID, nil
|
||||
}
|
||||
return id.NewOID(uint32(val.(int64))).AsId(), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int64,
|
||||
ToType: pgtypes.Regproc,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if val.(int64) > int64(math.MaxUint32) || val.(int64) < 0 {
|
||||
return nil, errOutOfRange.New(targetType.String())
|
||||
}
|
||||
if internalID := id.Cache().ToInternal(uint32(val.(int64))); internalID.IsValid() {
|
||||
return internalID, nil
|
||||
}
|
||||
return id.NewOID(uint32(val.(int64))).AsId(), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Int64,
|
||||
ToType: pgtypes.Regtype,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
if val.(int64) > int64(math.MaxUint32) || val.(int64) < 0 {
|
||||
return nil, errOutOfRange.New(targetType.String())
|
||||
}
|
||||
if internalID := id.Cache().ToInternal(uint32(val.(int64))); internalID.IsValid() {
|
||||
return internalID, nil
|
||||
}
|
||||
return id.NewOID(uint32(val.(int64))).AsId(), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
// 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"
|
||||
"unicode"
|
||||
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initInternalChar handles all casts that are built-in. This comprises only the source types.
|
||||
func initInternalChar(builtInCasts map[id.Cast]casts.Cast) {
|
||||
internalCharAssignment(builtInCasts)
|
||||
internalCharExplicit(builtInCasts)
|
||||
internalCharImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// internalCharAssignment registers all assignment casts. This comprises only the source types.
|
||||
func internalCharAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.InternalChar,
|
||||
ToType: pgtypes.BpChar,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return targetType.IoInput(ctx, val.(string))
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.InternalChar,
|
||||
ToType: pgtypes.VarChar,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return handleStringCast(val.(string), targetType)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// internalCharExplicit registers all explicit casts. This comprises only the source types.
|
||||
func internalCharExplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddExplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.InternalChar,
|
||||
ToType: pgtypes.Int32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
s := val.(string)
|
||||
if len(s) == 0 {
|
||||
return int32(0), nil
|
||||
}
|
||||
if unicode.IsLetter(rune(s[0])) {
|
||||
return int32(s[0]), nil
|
||||
}
|
||||
i, err := strconv.ParseInt(s, 10, 32)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int32(i), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// internalCharImplicit registers all implicit casts. This comprises only the source types.
|
||||
func internalCharImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.InternalChar,
|
||||
ToType: pgtypes.Text,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// 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 (
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/postgres/parser/duration"
|
||||
"github.com/dolthub/doltgresql/postgres/parser/timeofday"
|
||||
"github.com/dolthub/doltgresql/server/functions"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initInterval handles all casts that are built-in. This comprises only the source types.
|
||||
func initInterval(builtInCasts map[id.Cast]casts.Cast) {
|
||||
intervalAssignment(builtInCasts)
|
||||
intervalImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// intervalAssignment registers all assignment casts. This comprises only the source types.
|
||||
func intervalAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Interval,
|
||||
ToType: pgtypes.Time,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
dur := val.(duration.Duration)
|
||||
// the month and day of the duration are excluded
|
||||
return timeofday.FromInt(dur.Nanos() / functions.NanosPerMicro), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// intervalImplicit registers all implicit casts. This comprises only the source types.
|
||||
func intervalImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Interval,
|
||||
ToType: pgtypes.Interval,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val.(duration.Duration), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// 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 (
|
||||
"github.com/cockroachdb/errors"
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initJson handles all casts that are built-in. This comprises only the source types.
|
||||
func initJson(builtInCasts map[id.Cast]casts.Cast) {
|
||||
jsonAssignment(builtInCasts)
|
||||
}
|
||||
|
||||
// jsonAssignment registers all assignment casts. This comprises only the source types.
|
||||
func jsonAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Json,
|
||||
ToType: pgtypes.JsonB,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
switch v := val.(type) {
|
||||
case sql.StringWrapper:
|
||||
result, err := v.Unwrap(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return targetType.IoInput(ctx, result)
|
||||
case string:
|
||||
return targetType.IoInput(ctx, v)
|
||||
case sql.JSONWrapper:
|
||||
return v, nil
|
||||
default:
|
||||
return nil, errors.Errorf("unexpected JSON value type: %T", val)
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
// 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 (
|
||||
"encoding/json"
|
||||
"math"
|
||||
|
||||
"github.com/cockroachdb/apd/v3"
|
||||
"github.com/cockroachdb/errors"
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initJsonB handles all casts that are built-in. This comprises only the source types.
|
||||
func initJsonB(builtInCasts map[id.Cast]casts.Cast) {
|
||||
jsonbExplicit(builtInCasts)
|
||||
jsonbAssignment(builtInCasts)
|
||||
}
|
||||
|
||||
// jsonbGetInterface extracts the native Go value from a JSONB value (sql.JSONWrapper or string).
|
||||
func jsonbGetInterface(ctx *sql.Context, val any) (any, error) {
|
||||
switch v := val.(type) {
|
||||
case sql.JSONWrapper:
|
||||
return v.ToInterface(ctx)
|
||||
case sql.StringWrapper:
|
||||
var result any
|
||||
s, err := v.Unwrap(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := json.Unmarshal([]byte(s), &result); err != nil {
|
||||
return nil, errors.Errorf("invalid JSON: %v", err)
|
||||
}
|
||||
return result, nil
|
||||
case string:
|
||||
var result any
|
||||
if err := json.Unmarshal([]byte(v), &result); err != nil {
|
||||
return nil, errors.Errorf("invalid JSON: %v", err)
|
||||
}
|
||||
return result, nil
|
||||
default:
|
||||
return nil, errors.Errorf("unexpected JSONB value type: %T", val)
|
||||
}
|
||||
}
|
||||
|
||||
// jsonbNumberToDecimal converts various numeric types from JSON deserialization to decimal.Decimal.
|
||||
func jsonbNumberToDecimal(v any) (*apd.Decimal, bool) {
|
||||
switch n := v.(type) {
|
||||
case float64:
|
||||
d, _ := apd.New(0, 0).SetFloat64(n)
|
||||
return d, true
|
||||
case float32:
|
||||
d, _ := apd.New(0, 0).SetFloat64(float64(n))
|
||||
return d, true
|
||||
case json.Number:
|
||||
d, _, err := apd.NewFromString(n.String())
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
return d, true
|
||||
case int64:
|
||||
return apd.New(n, 0), true
|
||||
case int32:
|
||||
return apd.New(int64(n), 0), true
|
||||
case *apd.Decimal:
|
||||
return n, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// jsonbDecimalToInt rounds the decimal half-to-even (matching Postgres'
|
||||
// rules for numeric → integer casts) and then verifies that the rounded
|
||||
// value lies within [min, max]. Rounding happens before the bounds check so
|
||||
// values like 32767.4 round down to 32767 and still fit in int16, while
|
||||
// 32767.5 rounds up to 32768 and is rejected.
|
||||
func jsonbDecimalToInt(d *apd.Decimal, min, max *apd.Decimal, rangeMsg string) (int64, error) {
|
||||
if d.Form != apd.Finite {
|
||||
return 0, errors.Wrap(pgtypes.ErrCastOutOfRange, rangeMsg)
|
||||
}
|
||||
// Round half-to-even ("banker's rounding"), matching Postgres' numeric →
|
||||
// integer semantics. The default rounding mode on apd.BaseContext is
|
||||
// RoundHalfUp, so we have to construct a context explicitly.
|
||||
rounded := new(apd.Decimal)
|
||||
p := d.NumDigits() + int64(math.Abs(float64(d.Exponent))) + 1
|
||||
ctx := sql.DecimalCtx.WithPrecision(uint32(p))
|
||||
ctx.Rounding = apd.RoundHalfEven
|
||||
if _, err := ctx.Quantize(rounded, d, 0); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if rounded.Cmp(min) < 0 || rounded.Cmp(max) > 0 {
|
||||
return 0, errors.Wrap(pgtypes.ErrCastOutOfRange, rangeMsg)
|
||||
}
|
||||
i, err := rounded.Int64()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
// jsonbDecimalToFloat64 converts the decimal to a float64, returning an
|
||||
// out-of-range error when the magnitude exceeds what float64 can represent
|
||||
// as a finite value.
|
||||
func jsonbDecimalToFloat64(d *apd.Decimal, rangeMsg string) (float64, error) {
|
||||
if d.Form != apd.Finite {
|
||||
return 0, errors.Wrap(pgtypes.ErrCastOutOfRange, rangeMsg)
|
||||
}
|
||||
f, err := d.Float64()
|
||||
if err != nil || math.IsInf(f, 0) {
|
||||
return 0, errors.Wrap(pgtypes.ErrCastOutOfRange, rangeMsg)
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// jsonbDecimalToFloat32 converts the decimal to a float32, returning an
|
||||
// out-of-range error when the magnitude exceeds what float32 can represent
|
||||
// as a finite value.
|
||||
func jsonbDecimalToFloat32(d *apd.Decimal, rangeMsg string) (float32, error) {
|
||||
f, err := jsonbDecimalToFloat64(d, rangeMsg)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
f32 := float32(f)
|
||||
if math.IsInf(float64(f32), 0) {
|
||||
return 0, errors.Wrap(pgtypes.ErrCastOutOfRange, rangeMsg)
|
||||
}
|
||||
return f32, nil
|
||||
}
|
||||
|
||||
// jsonbExplicit registers all explicit casts. This comprises only the source types.
|
||||
func jsonbExplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddExplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.JsonB,
|
||||
ToType: pgtypes.Bool,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
v, err := jsonbGetInterface(ctx, val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch value := v.(type) {
|
||||
case map[string]interface{}:
|
||||
return nil, errors.Errorf("cannot cast jsonb object to type %s", targetType.String())
|
||||
case []interface{}:
|
||||
return nil, errors.Errorf("cannot cast jsonb array to type %s", targetType.String())
|
||||
case string:
|
||||
return nil, errors.Errorf("cannot cast jsonb string to type %s", targetType.String())
|
||||
case bool:
|
||||
return value, nil
|
||||
case nil:
|
||||
return nil, errors.Errorf("cannot cast jsonb null to type %s", targetType.String())
|
||||
default:
|
||||
if _, ok := jsonbNumberToDecimal(v); ok {
|
||||
return nil, errors.Errorf("cannot cast jsonb numeric to type %s", targetType.String())
|
||||
}
|
||||
return nil, errors.Errorf("unexpected jsonb value type: %T", v)
|
||||
}
|
||||
},
|
||||
})
|
||||
framework.MustAddExplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.JsonB,
|
||||
ToType: pgtypes.Float32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
v, err := jsonbGetInterface(ctx, val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch v.(type) {
|
||||
case map[string]interface{}:
|
||||
return nil, errors.Errorf("cannot cast jsonb object to type %s", targetType.String())
|
||||
case []interface{}:
|
||||
return nil, errors.Errorf("cannot cast jsonb array to type %s", targetType.String())
|
||||
case string:
|
||||
return nil, errors.Errorf("cannot cast jsonb string to type %s", targetType.String())
|
||||
case bool:
|
||||
return nil, errors.Errorf("cannot cast jsonb boolean to type %s", targetType.String())
|
||||
case nil:
|
||||
return nil, errors.Errorf("cannot cast jsonb null to type %s", targetType.String())
|
||||
default:
|
||||
d, ok := jsonbNumberToDecimal(v)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("unexpected jsonb value type: %T", v)
|
||||
}
|
||||
return jsonbDecimalToFloat32(d, "real out of range")
|
||||
}
|
||||
},
|
||||
})
|
||||
framework.MustAddExplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.JsonB,
|
||||
ToType: pgtypes.Float64,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
v, err := jsonbGetInterface(ctx, val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch v.(type) {
|
||||
case map[string]interface{}:
|
||||
return nil, errors.Errorf("cannot cast jsonb object to type %s", targetType.String())
|
||||
case []interface{}:
|
||||
return nil, errors.Errorf("cannot cast jsonb array to type %s", targetType.String())
|
||||
case string:
|
||||
return nil, errors.Errorf("cannot cast jsonb string to type %s", targetType.String())
|
||||
case bool:
|
||||
return nil, errors.Errorf("cannot cast jsonb boolean to type %s", targetType.String())
|
||||
case nil:
|
||||
return nil, errors.Errorf("cannot cast jsonb null to type %s", targetType.String())
|
||||
default:
|
||||
d, ok := jsonbNumberToDecimal(v)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("unexpected jsonb value type: %T", v)
|
||||
}
|
||||
return jsonbDecimalToFloat64(d, "double precision out of range")
|
||||
}
|
||||
},
|
||||
})
|
||||
framework.MustAddExplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.JsonB,
|
||||
ToType: pgtypes.Int16,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
v, err := jsonbGetInterface(ctx, val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch v.(type) {
|
||||
case map[string]interface{}:
|
||||
return nil, errors.Errorf("cannot cast jsonb object to type %s", targetType.String())
|
||||
case []interface{}:
|
||||
return nil, errors.Errorf("cannot cast jsonb array to type %s", targetType.String())
|
||||
case string:
|
||||
return nil, errors.Errorf("cannot cast jsonb string to type %s", targetType.String())
|
||||
case bool:
|
||||
return nil, errors.Errorf("cannot cast jsonb boolean to type %s", targetType.String())
|
||||
case nil:
|
||||
return nil, errors.Errorf("cannot cast jsonb null to type %s", targetType.String())
|
||||
default:
|
||||
d, ok := jsonbNumberToDecimal(v)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("unexpected jsonb value type: %T", v)
|
||||
}
|
||||
i, err := jsonbDecimalToInt(d, pgtypes.NumericValueMinInt16, pgtypes.NumericValueMaxInt16, "smallint out of range")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return int16(i), nil
|
||||
}
|
||||
},
|
||||
})
|
||||
framework.MustAddExplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.JsonB,
|
||||
ToType: pgtypes.Int32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
v, err := jsonbGetInterface(ctx, val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch v.(type) {
|
||||
case map[string]interface{}:
|
||||
return nil, errors.Errorf("cannot cast jsonb object to type %s", targetType.String())
|
||||
case []interface{}:
|
||||
return nil, errors.Errorf("cannot cast jsonb array to type %s", targetType.String())
|
||||
case string:
|
||||
return nil, errors.Errorf("cannot cast jsonb string to type %s", targetType.String())
|
||||
case bool:
|
||||
return nil, errors.Errorf("cannot cast jsonb boolean to type %s", targetType.String())
|
||||
case nil:
|
||||
return nil, errors.Errorf("cannot cast jsonb null to type %s", targetType.String())
|
||||
default:
|
||||
d, ok := jsonbNumberToDecimal(v)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("unexpected jsonb value type: %T", v)
|
||||
}
|
||||
i, err := jsonbDecimalToInt(d, pgtypes.NumericValueMinInt32, pgtypes.NumericValueMaxInt32, "integer out of range")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return int32(i), nil
|
||||
}
|
||||
},
|
||||
})
|
||||
framework.MustAddExplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.JsonB,
|
||||
ToType: pgtypes.Int64,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
v, err := jsonbGetInterface(ctx, val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch v.(type) {
|
||||
case map[string]interface{}:
|
||||
return nil, errors.Errorf("cannot cast jsonb object to type %s", targetType.String())
|
||||
case []interface{}:
|
||||
return nil, errors.Errorf("cannot cast jsonb array to type %s", targetType.String())
|
||||
case string:
|
||||
return nil, errors.Errorf("cannot cast jsonb string to type %s", targetType.String())
|
||||
case bool:
|
||||
return nil, errors.Errorf("cannot cast jsonb boolean to type %s", targetType.String())
|
||||
case nil:
|
||||
return nil, errors.Errorf("cannot cast jsonb null to type %s", targetType.String())
|
||||
default:
|
||||
d, ok := jsonbNumberToDecimal(v)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("unexpected jsonb value type: %T", v)
|
||||
}
|
||||
return jsonbDecimalToInt(d, pgtypes.NumericValueMinInt64, pgtypes.NumericValueMaxInt64, "bigint out of range")
|
||||
}
|
||||
},
|
||||
})
|
||||
framework.MustAddExplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.JsonB,
|
||||
ToType: pgtypes.Numeric,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
v, err := jsonbGetInterface(ctx, val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch v.(type) {
|
||||
case map[string]interface{}:
|
||||
return nil, errors.Errorf("cannot cast jsonb object to type %s", targetType.String())
|
||||
case []interface{}:
|
||||
return nil, errors.Errorf("cannot cast jsonb array to type %s", targetType.String())
|
||||
case string:
|
||||
return nil, errors.Errorf("cannot cast jsonb string to type %s", targetType.String())
|
||||
case bool:
|
||||
return nil, errors.Errorf("cannot cast jsonb boolean to type %s", targetType.String())
|
||||
case nil:
|
||||
return nil, errors.Errorf("cannot cast jsonb null to type %s", targetType.String())
|
||||
default:
|
||||
d, ok := jsonbNumberToDecimal(v)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("unexpected jsonb value type: %T", v)
|
||||
}
|
||||
// Apply the target type's precision/scale typmod, matching
|
||||
// what the numeric → numeric cast does.
|
||||
return pgtypes.GetNumericValueWithTypmod(d, targetType.GetAttTypMod())
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// jsonbAssignment registers all assignment casts. This comprises only the source types.
|
||||
func jsonbAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.JsonB,
|
||||
ToType: pgtypes.Json,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return pgtypes.JsonB.IoOutput(ctx, val)
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// 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 (
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initName handles all casts that are built-in. This comprises only the source types.
|
||||
func initName(builtInCasts map[id.Cast]casts.Cast) {
|
||||
nameAssignment(builtInCasts)
|
||||
nameImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// nameAssignment registers all assignment casts. This comprises only the source types.
|
||||
func nameAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Name,
|
||||
ToType: pgtypes.BpChar,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return handleStringCast(val.(string), targetType)
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Name,
|
||||
ToType: pgtypes.VarChar,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return handleStringCast(val.(string), targetType)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// nameImplicit registers all implicit casts. This comprises only the source types.
|
||||
func nameImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Name,
|
||||
ToType: pgtypes.Text,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// 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 (
|
||||
"github.com/cockroachdb/apd/v3"
|
||||
"github.com/cockroachdb/errors"
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
"github.com/dolthub/go-mysql-server/sql/types"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initNumeric handles all casts that are built-in. This comprises only the source types.
|
||||
func initNumeric(builtInCasts map[id.Cast]casts.Cast) {
|
||||
numericAssignment(builtInCasts)
|
||||
numericImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// numericAssignment registers all assignment casts. This comprises only the source types.
|
||||
func numericAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Numeric,
|
||||
ToType: pgtypes.Int16,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
d := val.(*apd.Decimal)
|
||||
if d.Cmp(pgtypes.NumericValueMinInt16) < 0 || d.Cmp(pgtypes.NumericValueMaxInt16) > 0 {
|
||||
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "smallint out of range")
|
||||
}
|
||||
i := types.DecimalRoundedIntPart(d)
|
||||
return int16(i), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Numeric,
|
||||
ToType: pgtypes.Int32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
d := val.(*apd.Decimal)
|
||||
if d.Cmp(pgtypes.NumericValueMinInt32) < 0 || d.Cmp(pgtypes.NumericValueMaxInt32) > 0 {
|
||||
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "integer out of range")
|
||||
}
|
||||
i := types.DecimalRoundedIntPart(d)
|
||||
return int32(i), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Numeric,
|
||||
ToType: pgtypes.Int64,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
d := val.(*apd.Decimal)
|
||||
if d.Cmp(pgtypes.NumericValueMinInt64) < 0 || d.Cmp(pgtypes.NumericValueMaxInt64) > 0 {
|
||||
return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "bigint out of range")
|
||||
}
|
||||
return types.DecimalRoundedIntPart(d), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// numericImplicit registers all implicit casts. This comprises only the source types.
|
||||
func numericImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Numeric,
|
||||
ToType: pgtypes.Float32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
d := val.(*apd.Decimal)
|
||||
f, _ := d.Float64()
|
||||
return float32(f), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Numeric,
|
||||
ToType: pgtypes.Float64,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
d := val.(*apd.Decimal)
|
||||
f, _ := d.Float64()
|
||||
return f, nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Numeric,
|
||||
ToType: pgtypes.Numeric,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return pgtypes.GetNumericValueWithTypmod(val.(*apd.Decimal), targetType.GetAttTypMod())
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
// 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 (
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initOid handles all casts that are built-in. This comprises only the source types.
|
||||
func initOid(builtInCasts map[id.Cast]casts.Cast) {
|
||||
oidAssignment(builtInCasts)
|
||||
oidImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// oidAssignment registers all assignment casts. This comprises only the source types.
|
||||
func oidAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Oid,
|
||||
ToType: pgtypes.Int32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return int32(id.Cache().ToOID(val.(id.Id))), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Oid,
|
||||
ToType: pgtypes.Int64,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return int64(id.Cache().ToOID(val.(id.Id))), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// oidImplicit registers all implicit casts. This comprises only the source types.
|
||||
func oidImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Oid,
|
||||
ToType: pgtypes.Regclass,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val, nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Oid,
|
||||
ToType: pgtypes.Regproc,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val, nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Oid,
|
||||
ToType: pgtypes.Regtype,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// 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 (
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initRegclass handles all casts that are built-in. This comprises only the source types.
|
||||
func initRegclass(builtInCasts map[id.Cast]casts.Cast) {
|
||||
regclassAssignment(builtInCasts)
|
||||
regclassImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// regclassAssignment registers all assignment casts. This comprises only the source types.
|
||||
func regclassAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Regclass,
|
||||
ToType: pgtypes.Int32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return int32(id.Cache().ToOID(val.(id.Id))), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Regclass,
|
||||
ToType: pgtypes.Int64,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return int64(id.Cache().ToOID(val.(id.Id))), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// regclassImplicit registers all implicit casts. This comprises only the source types.
|
||||
func regclassImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Regclass,
|
||||
ToType: pgtypes.Oid,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// 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 (
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initRegproc handles all casts that are built-in. This comprises only the source types.
|
||||
func initRegproc(builtInCasts map[id.Cast]casts.Cast) {
|
||||
regprocAssignment(builtInCasts)
|
||||
regprocImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// regprocAssignment registers all assignment casts. This comprises only the source types.
|
||||
func regprocAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Regproc,
|
||||
ToType: pgtypes.Int32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return int32(id.Cache().ToOID(val.(id.Id))), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Regproc,
|
||||
ToType: pgtypes.Int64,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return int64(id.Cache().ToOID(val.(id.Id))), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// regprocImplicit registers all implicit casts. This comprises only the source types.
|
||||
func regprocImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Regproc,
|
||||
ToType: pgtypes.Oid,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// 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 (
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initRegtype handles all casts that are built-in. This comprises only the source types.
|
||||
func initRegtype(builtInCasts map[id.Cast]casts.Cast) {
|
||||
regtypeAssignment(builtInCasts)
|
||||
regtypeImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// regtypeAssignment registers all assignment casts. This comprises only the source types.
|
||||
func regtypeAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Regtype,
|
||||
ToType: pgtypes.Int32,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return int32(id.Cache().ToOID(val.(id.Id))), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Regtype,
|
||||
ToType: pgtypes.Int64,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return int64(id.Cache().ToOID(val.(id.Id))), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// regtypeImplicit registers all implicit casts. This comprises only the source types.
|
||||
func regtypeImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Regtype,
|
||||
ToType: pgtypes.Oid,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// 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 (
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initText handles all casts that are built-in. This comprises only the source types.
|
||||
func initText(builtInCasts map[id.Cast]casts.Cast) {
|
||||
textAssignment(builtInCasts)
|
||||
textImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// textAssignment registers all assignment casts. This comprises only the source types.
|
||||
func textAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Text,
|
||||
ToType: pgtypes.InternalChar,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return handleStringCast(val.(string), targetType)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// textImplicit registers all implicit casts. This comprises only the source types.
|
||||
func textImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Text,
|
||||
ToType: pgtypes.BpChar,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return handleStringCast(val.(string), targetType)
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Text,
|
||||
ToType: pgtypes.Name,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return handleStringCast(val.(string), targetType)
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Text,
|
||||
ToType: pgtypes.Regclass,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return targetType.IoInput(ctx, val.(string))
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Text,
|
||||
ToType: pgtypes.VarChar,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return handleStringCast(val.(string), targetType)
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// 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 (
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/postgres/parser/timeofday"
|
||||
"github.com/dolthub/doltgresql/server/functions"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initTime handles all casts that are built-in. This comprises only the source types.
|
||||
func initTime(builtInCasts map[id.Cast]casts.Cast) {
|
||||
timeImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// timeImplicit registers all implicit casts. This comprises only the source types.
|
||||
func timeImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Time,
|
||||
ToType: pgtypes.Interval,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
t := val.(timeofday.TimeOfDay)
|
||||
dur := functions.GetIntervalDurationFromTimeComponents(0, 0, 0, int64(t.Hour()), int64(t.Minute()), int64(t.Second()), int64(t.Microsecond())*1000)
|
||||
return dur, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
// 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 (
|
||||
"time"
|
||||
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/postgres/parser/pgdate"
|
||||
"github.com/dolthub/doltgresql/postgres/parser/timeofday"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initTimestamp handles all casts that are built-in. This comprises only the source types.
|
||||
func initTimestamp(builtInCasts map[id.Cast]casts.Cast) {
|
||||
timestampAssignment(builtInCasts)
|
||||
timestampImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// timestampAssignment registers all assignment casts. This comprises only the source types.
|
||||
func timestampAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Timestamp,
|
||||
ToType: pgtypes.Date,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
d, err := pgdate.MakeDateFromTime(val.(time.Time))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return d.ToTime()
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Timestamp,
|
||||
ToType: pgtypes.Time,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return timeofday.FromTime(val.(time.Time)), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// timestampImplicit registers all implicit casts. This comprises only the source types.
|
||||
func timestampImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Timestamp,
|
||||
ToType: pgtypes.Timestamp,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val.(time.Time), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.Timestamp,
|
||||
ToType: pgtypes.TimestampTZ,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
// TODO: check
|
||||
return val.(time.Time), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
// 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 (
|
||||
"time"
|
||||
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/postgres/parser/pgdate"
|
||||
"github.com/dolthub/doltgresql/postgres/parser/timeofday"
|
||||
"github.com/dolthub/doltgresql/postgres/parser/timetz"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initTimestampTZ handles all casts that are built-in. This comprises only the source types.
|
||||
func initTimestampTZ(builtInCasts map[id.Cast]casts.Cast) {
|
||||
timestampTZAssignment(builtInCasts)
|
||||
timestampTZImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// timestampTZAssignment registers all assignment casts. This comprises only the source types.
|
||||
func timestampTZAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.TimestampTZ,
|
||||
ToType: pgtypes.Date,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
d, err := pgdate.MakeDateFromTime(val.(time.Time))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return d.ToTime()
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.TimestampTZ,
|
||||
ToType: pgtypes.Time,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return timeofday.FromTime(val.(time.Time)), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.TimestampTZ,
|
||||
ToType: pgtypes.Timestamp,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
// TODO: check
|
||||
return val.(time.Time), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.TimestampTZ,
|
||||
ToType: pgtypes.TimeTZ,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return timetz.MakeTimeTZFromTime(val.(time.Time)), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// timestampTZImplicit registers all implicit casts. This comprises only the source types.
|
||||
func timestampTZImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.TimestampTZ,
|
||||
ToType: pgtypes.TimestampTZ,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val.(time.Time), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// 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 (
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/postgres/parser/timetz"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initTimeTZ handles all casts that are built-in. This comprises only the source types.
|
||||
func initTimeTZ(builtInCasts map[id.Cast]casts.Cast) {
|
||||
timeTZAssignment(builtInCasts)
|
||||
timeTZImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// timeTZAssignment registers all assignment casts. This comprises only the source types.
|
||||
func timeTZAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.TimeTZ,
|
||||
ToType: pgtypes.Time,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val.(timetz.TimeTZ).TimeOfDay, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// timeTZImplicit registers all implicit casts. This comprises only the source types.
|
||||
func timeTZImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.TimeTZ,
|
||||
ToType: pgtypes.TimeTZ,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val.(timetz.TimeTZ), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
// 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 (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
cerrors "github.com/cockroachdb/errors"
|
||||
"gopkg.in/src-d/go-errors.v1"
|
||||
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// errOutOfRange is returned when a value is out of range for a given type.
|
||||
var errOutOfRange = errors.NewKind("%s out of range")
|
||||
|
||||
// handleStringCast handles casts to the string types that may have length restrictions. Returns an error if other types
|
||||
// are passed in. Will always return the correct string, even on error, as some contexts may ignore the error.
|
||||
func handleStringCast(input string, targetType *pgtypes.DoltgresType) (string, error) {
|
||||
tm := targetType.GetAttTypMod()
|
||||
switch targetType.ID {
|
||||
case pgtypes.BpChar.ID:
|
||||
if tm == -1 {
|
||||
return input, nil
|
||||
}
|
||||
maxChars, err := pgtypes.GetTypModFromCharLength("char", tm)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
length := uint32(maxChars)
|
||||
str, runeLength := truncateString(input, length)
|
||||
if runeLength > length {
|
||||
return input, cerrors.Wrap(pgtypes.ErrCastOutOfRange, fmt.Sprintf("value too long for type %s", targetType.String()))
|
||||
} else if runeLength < length {
|
||||
return str + strings.Repeat(" ", int(length-runeLength)), nil
|
||||
} else {
|
||||
return str, nil
|
||||
}
|
||||
case pgtypes.InternalChar.ID:
|
||||
str, _ := truncateString(input, pgtypes.InternalCharLength)
|
||||
return str, nil
|
||||
case pgtypes.Name.ID:
|
||||
// Name seems to never throw an error, regardless of the context or how long the input is
|
||||
str, _ := truncateString(input, uint32(targetType.TypLength))
|
||||
return str, nil
|
||||
case pgtypes.VarChar.ID:
|
||||
if tm == -1 {
|
||||
return input, nil
|
||||
}
|
||||
length := uint32(pgtypes.GetCharLengthFromTypmod(tm))
|
||||
str, runeLength := truncateString(input, length)
|
||||
if runeLength > length {
|
||||
return input, cerrors.Wrap(pgtypes.ErrCastOutOfRange, fmt.Sprintf("value too long for type %s", targetType.String()))
|
||||
} else {
|
||||
return str, nil
|
||||
}
|
||||
default:
|
||||
return "", cerrors.Errorf("internal cast called to handle non-string type")
|
||||
}
|
||||
}
|
||||
|
||||
// truncateString returns a string that has been truncated to the given length. Uses the rune count rather than the
|
||||
// byte count. Returns the input string if it's smaller than the length. Also returns the rune count of the string.
|
||||
func truncateString(val string, runeLimit uint32) (string, uint32) {
|
||||
runeLength := uint32(utf8.RuneCountInString(val))
|
||||
if runeLength > runeLimit {
|
||||
// TODO: figure out if there's a faster way to truncate based on rune count
|
||||
startString := val
|
||||
for i := uint32(0); i < runeLimit; i++ {
|
||||
_, size := utf8.DecodeRuneInString(val)
|
||||
val = val[size:]
|
||||
}
|
||||
return startString[:len(startString)-len(val)], runeLength
|
||||
}
|
||||
return val, runeLength
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// Copyright 2026 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 (
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
"github.com/dolthub/doltgresql/postgres/parser/sem/tree"
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initVarBit handles all casts that are built-in. This comprises only the source types.
|
||||
func initVarBit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
varBitImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// varBitImplicit registers all implicit casts. This comprises only the source types.
|
||||
func varBitImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.VarBit,
|
||||
ToType: pgtypes.Bit,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
input := val.(string)
|
||||
array, err := tree.ParseDBitArray(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
expectedLength := targetType.GetAttTypMod()
|
||||
if array.BitLen() != uint(expectedLength) {
|
||||
return nil, pgtypes.ErrWrongLengthBit.New(len(input), expectedLength)
|
||||
}
|
||||
return tree.AsStringWithFlags(array, tree.FmtPgwireText), nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.VarBit,
|
||||
ToType: pgtypes.VarBit,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
input := val.(string)
|
||||
array, err := tree.ParseDBitArray(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
atttypmod := targetType.GetAttTypMod()
|
||||
if atttypmod != -1 {
|
||||
if int32(array.BitLen()) > atttypmod {
|
||||
return nil, pgtypes.ErrVarBitLengthExceeded.New(atttypmod)
|
||||
}
|
||||
}
|
||||
return tree.AsStringWithFlags(array, tree.FmtPgwireText), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// 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 (
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
|
||||
"github.com/dolthub/doltgresql/core/casts"
|
||||
"github.com/dolthub/doltgresql/core/id"
|
||||
|
||||
"github.com/dolthub/doltgresql/server/functions/framework"
|
||||
pgtypes "github.com/dolthub/doltgresql/server/types"
|
||||
)
|
||||
|
||||
// initVarChar handles all casts that are built-in. This comprises only the source types.
|
||||
func initVarChar(builtInCasts map[id.Cast]casts.Cast) {
|
||||
varcharAssignment(builtInCasts)
|
||||
varcharImplicit(builtInCasts)
|
||||
}
|
||||
|
||||
// varcharAssignment registers all assignment casts. This comprises only the source types.
|
||||
func varcharAssignment(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.VarChar,
|
||||
ToType: pgtypes.InternalChar,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return handleStringCast(val.(string), targetType)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// varcharImplicit registers all implicit casts. This comprises only the source types.
|
||||
func varcharImplicit(builtInCasts map[id.Cast]casts.Cast) {
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.VarChar,
|
||||
ToType: pgtypes.BpChar,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return handleStringCast(val.(string), targetType)
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.VarChar,
|
||||
ToType: pgtypes.Name,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return handleStringCast(val.(string), targetType)
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.VarChar,
|
||||
ToType: pgtypes.Text,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return val, nil
|
||||
},
|
||||
})
|
||||
framework.MustAddImplicitTypeCast(builtInCasts, framework.TypeCast{
|
||||
FromType: pgtypes.VarChar,
|
||||
ToType: pgtypes.VarChar,
|
||||
Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
|
||||
return handleStringCast(val.(string), targetType)
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user