95 lines
3.5 KiB
Go
95 lines
3.5 KiB
Go
// 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
|
|
},
|
|
})
|
|
}
|