MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / typeCheckConstant

Function typeCheckConstant

pkg/sql/sem/tree/constant.go:67–97  ·  view source on GitHub ↗
(
	ctx context.Context, semaCtx *SemaContext, c Constant, desired *types.T,
)

Source from the content-addressed store, hash-verified

65}
66
67func typeCheckConstant(
68 ctx context.Context, semaCtx *SemaContext, c Constant, desired *types.T,
69) (ret TypedExpr, err error) {
70 avail := c.AvailableTypes()
71 if !desired.IsAmbiguous() {
72 for _, typ := range avail {
73 if desired.Equivalent(typ) {
74 return c.ResolveAsType(ctx, semaCtx, desired)
75 }
76 }
77 }
78
79 // If a numeric constant will be promoted to a DECIMAL because it was out
80 // of range of an INT, but an INT is desired, throw an error here so that
81 // the error message specifically mentions the overflow.
82 if desired.Family() == types.IntFamily {
83 if n, ok := c.(*NumVal); ok {
84 _, err := n.AsInt64()
85 switch {
86 case errors.Is(err, errConstOutOfRange64):
87 return nil, err
88 case errors.Is(err, errConstNotInt):
89 default:
90 return nil, errors.NewAssertionErrorWithWrappedErrf(err, "unexpected error")
91 }
92 }
93 }
94
95 natural := avail[0]
96 return c.ResolveAsType(ctx, semaCtx, natural)
97}
98
99func naturalConstantType(c Constant) *types.T {
100 return c.AvailableTypes()[0]

Callers 2

TypeCheckMethod · 0.85
TypeCheckMethod · 0.85

Calls 6

IsAmbiguousMethod · 0.80
EquivalentMethod · 0.80
FamilyMethod · 0.80
AvailableTypesMethod · 0.65
ResolveAsTypeMethod · 0.65
AsInt64Method · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…