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

Method doc

pkg/sql/sem/tree/pretty.go:1027–1095  ·  view source on GitHub ↗
(p *PrettyCfg)

Source from the content-addressed store, hash-verified

1025}
1026
1027func (node *CastExpr) doc(p *PrettyCfg) pretty.Doc {
1028 typ := p.formatType(node.Type)
1029
1030 switch node.SyntaxMode {
1031 case CastPrepend:
1032 // This is a special case for things like INTERVAL '1s'. These only work
1033 // with string constats; if the underlying expression was changed, we fall
1034 // back to the short syntax.
1035 if _, ok := node.Expr.(*StrVal); ok {
1036 return pretty.Fold(pretty.Concat,
1037 typ,
1038 pretty.Text(" "),
1039 p.Doc(node.Expr),
1040 )
1041 }
1042 fallthrough
1043 case CastShort:
1044 if typ, ok := GetStaticallyKnownType(node.Type); ok {
1045 switch typ.Family() {
1046 case types.JsonFamily:
1047 if sv, ok := node.Expr.(*StrVal); ok && p.JSONFmt {
1048 return p.jsonCast(sv, "::", typ)
1049 }
1050 }
1051 }
1052 return pretty.Fold(pretty.Concat,
1053 p.exprDocWithParen(node.Expr),
1054 pretty.Text("::"),
1055 typ,
1056 )
1057 default:
1058 if nTyp, ok := GetStaticallyKnownType(node.Type); ok && nTyp.Family() == types.CollatedStringFamily {
1059 // COLLATE clause needs to go after CAST expression, so create
1060 // equivalent string type without the locale to get name of string
1061 // type without the COLLATE.
1062 strTyp := types.MakeScalar(
1063 types.StringFamily,
1064 nTyp.Oid(),
1065 nTyp.Precision(),
1066 nTyp.Width(),
1067 "", /* locale */
1068 )
1069 typ = pretty.Text(strTyp.SQLString())
1070 }
1071
1072 ret := pretty.Fold(pretty.Concat,
1073 pretty.Keyword("CAST"),
1074 p.bracket(
1075 "(",
1076 p.nestUnder(
1077 p.Doc(node.Expr),
1078 pretty.Concat(
1079 p.keywordWithText("", "AS", " "),
1080 typ,
1081 ),
1082 ),
1083 ")",
1084 ),

Callers

nothing calls this directly

Calls 15

FoldFunction · 0.92
TextFunction · 0.92
MakeScalarFunction · 0.92
KeywordFunction · 0.92
ConcatFunction · 0.92
GetStaticallyKnownTypeFunction · 0.85
formatTypeMethod · 0.80
DocMethod · 0.80
FamilyMethod · 0.80
jsonCastMethod · 0.80
exprDocWithParenMethod · 0.80
OidMethod · 0.80

Tested by

no test coverage detected