// 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()) }, }) }