MCPcopy Create free account
hub / github.com/dolthub/doltgresql / numericAssignment

Function numericAssignment

server/cast/numeric.go:36–72  ·  view source on GitHub ↗

numericAssignment registers all assignment casts. This comprises only the source types.

(builtInCasts map[id.Cast]casts.Cast)

Source from the content-addressed store, hash-verified

34
35// numericAssignment registers all assignment casts. This comprises only the source types.
36func numericAssignment(builtInCasts map[id.Cast]casts.Cast) {
37 framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
38 FromType: pgtypes.Numeric,
39 ToType: pgtypes.Int16,
40 Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
41 d := val.(*apd.Decimal)
42 if d.Cmp(pgtypes.NumericValueMinInt16) < 0 || d.Cmp(pgtypes.NumericValueMaxInt16) > 0 {
43 return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "smallint out of range")
44 }
45 i := types.DecimalRoundedIntPart(d)
46 return int16(i), nil
47 },
48 })
49 framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
50 FromType: pgtypes.Numeric,
51 ToType: pgtypes.Int32,
52 Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
53 d := val.(*apd.Decimal)
54 if d.Cmp(pgtypes.NumericValueMinInt32) < 0 || d.Cmp(pgtypes.NumericValueMaxInt32) > 0 {
55 return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "integer out of range")
56 }
57 i := types.DecimalRoundedIntPart(d)
58 return int32(i), nil
59 },
60 })
61 framework.MustAddAssignmentTypeCast(builtInCasts, framework.TypeCast{
62 FromType: pgtypes.Numeric,
63 ToType: pgtypes.Int64,
64 Function: func(ctx *sql.Context, val any, _, targetType *pgtypes.DoltgresType) (any, error) {
65 d := val.(*apd.Decimal)
66 if d.Cmp(pgtypes.NumericValueMinInt64) < 0 || d.Cmp(pgtypes.NumericValueMaxInt64) > 0 {
67 return nil, errors.Wrap(pgtypes.ErrCastOutOfRange, "bigint out of range")
68 }
69 return types.DecimalRoundedIntPart(d), nil
70 },
71 })
72}
73
74// numericImplicit registers all implicit casts. This comprises only the source types.
75func numericImplicit(builtInCasts map[id.Cast]casts.Cast) {

Callers 1

initNumericFunction · 0.85

Calls 1

Tested by

no test coverage detected