float64Assignment registers all assignment casts. This comprises only the source types.
(builtInCasts map[id.Cast]casts.Cast)
| 34 | |
| 35 | // float64Assignment registers all assignment casts. This comprises only the source types. |
| 36 | func float64Assignment(builtInCasts map[id.Cast]casts.Cast) { |
| 37 | framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{ |
| 38 | FromType: pgtypes.Float64, |
| 39 | ToType: pgtypes.Float32, |
| 40 | Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) { |
| 41 | return float32(val.(float64)), nil |
| 42 | }, |
| 43 | }) |
| 44 | framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{ |
| 45 | FromType: pgtypes.Float64, |
| 46 | ToType: pgtypes.Int16, |
| 47 | Function: func(ctx *sql.Context, valInterface any, _, targetType *pgtypes.DoltgresType) (any, error) { |
| 48 | val := math.RoundToEven(valInterface.(float64)) |
| 49 | if val > 32767 || val < -32768 { |
| 50 | return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "smallint out of range") |
| 51 | } |
| 52 | return int16(val), nil |
| 53 | }, |
| 54 | }) |
| 55 | framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{ |
| 56 | FromType: pgtypes.Float64, |
| 57 | ToType: pgtypes.Int32, |
| 58 | Function: func(ctx *sql.Context, valInterface any, _, targetType *pgtypes.DoltgresType) (any, error) { |
| 59 | val := math.RoundToEven(valInterface.(float64)) |
| 60 | if val > 2147483647 || val < -2147483648 { |
| 61 | return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "integer out of range") |
| 62 | } |
| 63 | return int32(val), nil |
| 64 | }, |
| 65 | }) |
| 66 | framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{ |
| 67 | FromType: pgtypes.Float64, |
| 68 | ToType: pgtypes.Int64, |
| 69 | Function: func(ctx *sql.Context, valInterface any, _, targetType *pgtypes.DoltgresType) (any, error) { |
| 70 | val := math.RoundToEven(valInterface.(float64)) |
| 71 | if val > 9223372036854775807 || val < -9223372036854775808 { |
| 72 | return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "bigint out of range") |
| 73 | } |
| 74 | return int64(val), nil |
| 75 | }, |
| 76 | }) |
| 77 | framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{ |
| 78 | FromType: pgtypes.Float64, |
| 79 | ToType: pgtypes.Numeric, |
| 80 | Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) { |
| 81 | d := new(apd.Decimal) |
| 82 | err := d.Scan(val.(float64)) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | return pgtypes.GetNumericValueWithTypmod(d, targetType.GetAttTypMod()) |
| 87 | }, |
| 88 | }) |
| 89 | } |
no test coverage detected