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

Method ResolveAsType

postgres/parser/sem/tree/constant.go:570–630  ·  view source on GitHub ↗

ResolveAsType implements the Constant interface.

(
	ctx context.Context, semaCtx *SemaContext, typ *types.T,
)

Source from the content-addressed store, hash-verified

568
569// ResolveAsType implements the Constant interface.
570func (expr *StrVal) ResolveAsType(
571 ctx context.Context, semaCtx *SemaContext, typ *types.T,
572) (TypedExpr, error) {
573 if expr.scannedAsBytes {
574 // We're looking at typing a byte literal constant into some value type.
575 switch typ.Family() {
576 case types.BytesFamily:
577 expr.resBytes = DBytes(expr.s)
578 return &expr.resBytes, nil
579 case types.EnumFamily:
580 return MakeDEnumFromPhysicalRepresentation(typ, []byte(expr.s))
581 case types.UuidFamily:
582 return ParseDUuidFromBytes([]byte(expr.s))
583 case types.StringFamily:
584 // bpchar types truncate trailing whitespace.
585 if typ.Oid() == oid.T_bpchar {
586 expr.resString = DString(strings.TrimRight(expr.s, " "))
587 return &expr.resString, nil
588 }
589 expr.resString = DString(expr.s)
590 return &expr.resString, nil
591 }
592 return nil, errors.AssertionFailedf("attempt to type byte array literal to %T", typ)
593 }
594
595 // Typing a string literal constant into some value type.
596 switch typ.Family() {
597 case types.StringFamily:
598 if typ.Oid() == oid.T_name {
599 expr.resString = DString(expr.s)
600 return NewDNameFromDString(&expr.resString), nil
601 }
602 // bpchar types truncate trailing whitespace.
603 if typ.Oid() == oid.T_bpchar {
604 expr.resString = DString(strings.TrimRight(expr.s, " "))
605 return &expr.resString, nil
606 }
607 expr.resString = DString(expr.s)
608 return &expr.resString, nil
609
610 case types.BytesFamily:
611 return ParseDByte(expr.s)
612
613 default:
614 val, dependsOnContext, err := ParseAndRequireString(typ, expr.s, dummyParseTimeContext{})
615 if err != nil {
616 return nil, err
617 }
618 if !dependsOnContext {
619 return val, nil
620 }
621 // Interpreting a string as one of these types may depend on the timezone or
622 // the current time; the value won't be safe to reuse later. So in this case
623 // we return a CastExpr and let the conversion happen at evaluation time. We
624 // still want to error out if the conversion is not possible though (this is
625 // used when resolving overloads).
626 expr.resString = DString(expr.s)
627 c := NewTypedCastExpr(&expr.resString, typ)

Callers

nothing calls this directly

Calls 11

DBytesTypeAlias · 0.85
ParseDUuidFromBytesFunction · 0.85
DStringTypeAlias · 0.85
NewDNameFromDStringFunction · 0.85
ParseDByteFunction · 0.85
ParseAndRequireStringFunction · 0.85
NewTypedCastExprFunction · 0.85
FamilyMethod · 0.80
OidMethod · 0.80
TypeCheckMethod · 0.65

Tested by

no test coverage detected