github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/parsers/tree/format.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tree 16 17 import ( 18 "fmt" 19 "strings" 20 21 "github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect" 22 ) 23 24 // FmtCtx contains formatted text of the node. 25 type FmtCtx struct { 26 *strings.Builder 27 dialectType dialect.DialectType 28 // quoteString string 29 quoteString bool 30 singleQuoteString bool 31 } 32 33 func NewFmtCtx(dialectType dialect.DialectType, opts ...FmtCtxOption) *FmtCtx { 34 ctx := &FmtCtx{ 35 Builder: new(strings.Builder), 36 dialectType: dialectType, 37 } 38 for _, opt := range opts { 39 opt.Apply(ctx) 40 } 41 return ctx 42 } 43 44 type FmtCtxOption func(*FmtCtx) 45 46 func (f FmtCtxOption) Apply(ctx *FmtCtx) { 47 f(ctx) 48 } 49 50 func WithQuoteString(quote bool) FmtCtxOption { 51 return FmtCtxOption(func(ctx *FmtCtx) { 52 ctx.quoteString = quote 53 }) 54 } 55 func WithSingleQuoteString() FmtCtxOption { 56 return FmtCtxOption(func(ctx *FmtCtx) { 57 ctx.singleQuoteString = true 58 }) 59 } 60 61 // NodeFormatter for formatted output of the node. 62 type NodeFormatter interface { 63 Format(ctx *FmtCtx) 64 } 65 66 func String(node NodeFormatter, dialectType dialect.DialectType) string { 67 if node == nil { 68 return "<nil>" 69 } 70 71 ctx := NewFmtCtx(dialectType) 72 node.Format(ctx) 73 return ctx.String() 74 } 75 76 func (ctx *FmtCtx) PrintExpr(currentExpr Expr, expr Expr, left bool) { 77 if precedenceFor(currentExpr) == Syntactic { 78 expr.Format(ctx) 79 } else { 80 needParens := needParens(currentExpr, expr, left) 81 if needParens { 82 ctx.WriteByte('(') 83 } 84 expr.Format(ctx) 85 if needParens { 86 ctx.WriteByte(')') 87 } 88 } 89 } 90 91 func (ctx *FmtCtx) WriteValue(t P_TYPE, v string) (int, error) { 92 if ctx.quoteString { 93 switch t { 94 case P_char: 95 return ctx.WriteString(fmt.Sprintf("%q", v)) 96 default: 97 return ctx.WriteString(v) 98 } 99 } 100 if ctx.singleQuoteString && t == P_char { 101 return ctx.WriteString(fmt.Sprintf("'%s'", v)) 102 } 103 return ctx.WriteString(v) 104 } 105 106 func (ctx *FmtCtx) WriteStringQuote(v string) (int, error) { 107 if ctx.quoteString { 108 return ctx.WriteString(fmt.Sprintf("%q", v)) 109 } else { 110 return ctx.WriteString(v) 111 } 112 } 113 114 // needParens says if we need a parenthesis 115 // op is the operator we are printing 116 // val is the value we are checking if we need parens around or not 117 // left let's us know if the value is on the lhs or rhs of the operator 118 func needParens(op, val Expr, left bool) bool { 119 // Values are atomic and never need parens 120 if IsValue(val) { 121 return false 122 } 123 124 if areBothISExpr(op, val) { 125 return true 126 } 127 128 opBinding := precedenceFor(op) 129 valBinding := precedenceFor(val) 130 131 if opBinding == Syntactic || valBinding == Syntactic { 132 return false 133 } 134 135 if left { 136 // for left associative operators, if the value is to the left of the operator, 137 // we only need parens if the order is higher for the value expression 138 return valBinding > opBinding 139 } 140 141 return valBinding >= opBinding 142 } 143 144 // IsValue returns true if the Expr is a string, integral or value arg. 145 // NULL is not considered to be a value. 146 func IsValue(node Expr) bool { 147 switch node.(type) { 148 case *NumVal, *StrVal: 149 return true 150 } 151 return false 152 } 153 154 func areBothISExpr(op Expr, val Expr) bool { 155 // when using IS on an IS op, we need special handling 156 _, isOpIs := op.(*IsNullExpr) 157 if isOpIs { 158 _, isValIs := val.(*IsNullExpr) 159 if isValIs { 160 return true 161 } 162 } 163 _, isOpIsNot := op.(*IsNullExpr) 164 if isOpIsNot { 165 _, isValIsNot := val.(*IsNullExpr) 166 if isValIsNot { 167 return true 168 } 169 } 170 return false 171 }