github.com/matrixorigin/matrixone@v1.2.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 // Visitor Design Pattern 67 // NodeChecker is abstract tree Node 68 type NodeChecker interface { 69 // `Accept` method accepts Visitor to visit itself. Visitor checks the current node 70 // The returned node should replace original node. 71 // The node returned by Accpet should replace the original node. 72 // If OK returns false, it stops accessing other child nodes. 73 74 // The general implementation logic of the `Accept` method is: 75 // First, call the Visitor.`Enter` method, and assign the returned `node` to the receiver of the `Accept` method, 76 // If the returnd `skipChildren` value is true, then it is necessary to stop accessing the receiver's child node 77 // Otherwise, recursively call the` Accept` of its children nodes, 78 // Finally, don't forget to call the Visitor's `Exit` method 79 Accept(v Visitor) (node Expr, ok bool) 80 } 81 82 // Visitor Design Pattern 83 // Visitor visits the ast node or sub ast nodes 84 type Visitor interface { 85 // Call the 'Enter' method before visiting the children nodes. 86 // The node type returned by the `Enter` method must be the same as the input node type 87 // SkipChildren returning true means that access to child nodes should be skipped. 88 Enter(n Expr) (node Expr, skipChildren bool) 89 90 //`Exit` is called after all children nodes are visited. 91 //The returned node of the `Exit` method is `Expr`, which is of the same type as the input node. 92 //if `Exit` method returns OK as false ,means stop visiting. 93 Exit(n Expr) (node Expr, ok bool) 94 } 95 96 func String(node NodeFormatter, dialectType dialect.DialectType) string { 97 if node == nil { 98 return "<nil>" 99 } 100 101 ctx := NewFmtCtx(dialectType) 102 node.Format(ctx) 103 return ctx.String() 104 } 105 106 // StringWithOpts Restore SQL and provide string formatting restore options 107 func StringWithOpts(node NodeFormatter, dialectType dialect.DialectType, opts ...FmtCtxOption) string { 108 if node == nil { 109 return "<nil>" 110 } 111 112 ctx := NewFmtCtx(dialectType, opts...) 113 node.Format(ctx) 114 return ctx.String() 115 } 116 117 func (ctx *FmtCtx) PrintExpr(currentExpr Expr, expr Expr, left bool) { 118 if precedenceFor(currentExpr) == Syntactic { 119 expr.Format(ctx) 120 } else { 121 needParens := needParens(currentExpr, expr, left) 122 if needParens { 123 ctx.WriteByte('(') 124 } 125 expr.Format(ctx) 126 if needParens { 127 ctx.WriteByte(')') 128 } 129 } 130 } 131 132 func (ctx *FmtCtx) WriteValue(t P_TYPE, v string) (int, error) { 133 if ctx.quoteString { 134 switch t { 135 case P_char: 136 return ctx.WriteString(fmt.Sprintf("%q", v)) 137 default: 138 return ctx.WriteString(v) 139 } 140 } 141 if ctx.singleQuoteString && t == P_char { 142 return ctx.WriteString(fmt.Sprintf("'%s'", v)) 143 } 144 return ctx.WriteString(v) 145 } 146 147 func (ctx *FmtCtx) WriteStringQuote(v string) (int, error) { 148 if ctx.quoteString { 149 return ctx.WriteString(fmt.Sprintf("%q", v)) 150 } else { 151 return ctx.WriteString(v) 152 } 153 } 154 155 // needParens says if we need a parenthesis 156 // op is the operator we are printing 157 // val is the value we are checking if we need parens around or not 158 // left let's us know if the value is on the lhs or rhs of the operator 159 func needParens(op, val Expr, left bool) bool { 160 // Values are atomic and never need parens 161 if IsValue(val) { 162 return false 163 } 164 165 if areBothISExpr(op, val) { 166 return true 167 } 168 169 opBinding := precedenceFor(op) 170 valBinding := precedenceFor(val) 171 172 if opBinding == Syntactic || valBinding == Syntactic { 173 return false 174 } 175 176 if left { 177 // for left associative operators, if the value is to the left of the operator, 178 // we only need parens if the order is higher for the value expression 179 return valBinding > opBinding 180 } 181 182 return valBinding >= opBinding 183 } 184 185 // IsValue returns true if the Expr is a string, integral or value arg. 186 // NULL is not considered to be a value. 187 func IsValue(node Expr) bool { 188 switch node.(type) { 189 case *NumVal, *StrVal: 190 return true 191 } 192 return false 193 } 194 195 func areBothISExpr(op Expr, val Expr) bool { 196 // when using IS on an IS op, we need special handling 197 _, isOpIs := op.(*IsNullExpr) 198 if isOpIs { 199 _, isValIs := val.(*IsNullExpr) 200 if isValIs { 201 return true 202 } 203 } 204 _, isOpIsNot := op.(*IsNullExpr) 205 if isOpIsNot { 206 _, isValIsNot := val.(*IsNullExpr) 207 if isValIsNot { 208 return true 209 } 210 } 211 return false 212 }