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

Method Format

pkg/sql/sem/tree/expr.go:1512–1558  ·  view source on GitHub ↗

Format implements the NodeFormatter interface.

(ctx *FmtCtx)

Source from the content-addressed store, hash-verified

1510
1511// Format implements the NodeFormatter interface.
1512func (node *CastExpr) Format(ctx *FmtCtx) {
1513 switch node.SyntaxMode {
1514 case CastPrepend:
1515 // This is a special case for things like INTERVAL '1s'. These only work
1516 // with string constants; if the underlying expression was changed, we fall
1517 // back to the short syntax.
1518 if _, ok := node.Expr.(*StrVal); ok {
1519 ctx.FormatTypeReference(node.Type)
1520 ctx.WriteByte(' ')
1521 // We need to replace this with a quoted string constants in certain
1522 // cases because the grammar requires a string constant rather than an
1523 // expression for this form of casting in the typed_literal rule
1524 if ctx.HasFlags(FmtHideConstants) {
1525 ctx.WriteString("'_'")
1526 } else {
1527 ctx.FormatNode(node.Expr)
1528 }
1529 break
1530 }
1531 fallthrough
1532 case CastShort:
1533 exprFmtWithParen(ctx, node.Expr)
1534 ctx.WriteString("::")
1535 ctx.FormatTypeReference(node.Type)
1536 default:
1537 ctx.WriteString("CAST(")
1538 ctx.FormatNode(node.Expr)
1539 ctx.WriteString(" AS ")
1540 if typ, ok := GetStaticallyKnownType(node.Type); ok && typ.Family() == types.CollatedStringFamily {
1541 // Need to write closing parentheses before COLLATE clause, so create
1542 // equivalent string type without the locale.
1543 strTyp := types.MakeScalar(
1544 types.StringFamily,
1545 typ.Oid(),
1546 typ.Precision(),
1547 typ.Width(),
1548 "", /* locale */
1549 )
1550 ctx.WriteString(strTyp.SQLString())
1551 ctx.WriteString(") COLLATE ")
1552 lex.EncodeLocaleName(&ctx.Buffer, typ.Locale())
1553 } else {
1554 ctx.FormatTypeReference(node.Type)
1555 ctx.WriteByte(')')
1556 }
1557 }
1558}
1559
1560// NewTypedCastExpr returns a new CastExpr that is verified to be well-typed.
1561func NewTypedCastExpr(expr TypedExpr, typ *types.T) *CastExpr {

Callers

nothing calls this directly

Calls 13

MakeScalarFunction · 0.92
EncodeLocaleNameFunction · 0.92
exprFmtWithParenFunction · 0.85
GetStaticallyKnownTypeFunction · 0.85
FormatTypeReferenceMethod · 0.80
FormatNodeMethod · 0.80
FamilyMethod · 0.80
OidMethod · 0.80
PrecisionMethod · 0.80
LocaleMethod · 0.80
WidthMethod · 0.65
SQLStringMethod · 0.65

Tested by

no test coverage detected