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