github.com/XiaoMi/Gaea@v1.2.5/parser/ast/ast.go (about) 1 // Copyright 2015 PingCAP, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 // Package ast is the abstract syntax tree parsed from a SQL statement by parser. 15 // It can be analysed and transformed by optimizer. 16 package ast 17 18 import ( 19 "io" 20 21 "github.com/XiaoMi/Gaea/parser/format" 22 "github.com/XiaoMi/Gaea/parser/model" 23 "github.com/XiaoMi/Gaea/parser/types" 24 ) 25 26 // Node is the basic element of the AST. 27 // Interfaces embed Node should have 'Node' name suffix. 28 type Node interface { 29 // Restore returns the sql text from ast tree 30 Restore(ctx *format.RestoreCtx) error 31 // Accept accepts Visitor to visit itself. 32 // The returned node should replace original node. 33 // ok returns false to stop visiting. 34 // 35 // Implementation of this method should first call visitor.Enter, 36 // assign the returned node to its method receiver, if skipChildren returns true, 37 // children should be skipped. Otherwise, call its children in particular order that 38 // later elements depends on former elements. Finally, return visitor.Leave. 39 Accept(v Visitor) (node Node, ok bool) 40 // Text returns the original text of the element. 41 Text() string 42 // SetText sets original text to the Node. 43 SetText(text string) 44 } 45 46 // Flags indicates whether an expression contains certain types of expression. 47 const ( 48 FlagConstant uint64 = 0 49 FlagHasParamMarker uint64 = 1 << iota 50 FlagHasFunc 51 FlagHasReference 52 FlagHasAggregateFunc 53 FlagHasSubquery 54 FlagHasVariable 55 FlagHasDefault 56 FlagPreEvaluated 57 FlagHasWindowFunc 58 ) 59 60 // ExprNode is a node that can be evaluated. 61 // Name of implementations should have 'Expr' suffix. 62 type ExprNode interface { 63 // Node is embedded in ExprNode. 64 Node 65 // SetType sets evaluation type to the expression. 66 SetType(tp *types.FieldType) 67 // GetType gets the evaluation type of the expression. 68 GetType() *types.FieldType 69 // SetFlag sets flag to the expression. 70 // Flag indicates whether the expression contains 71 // parameter marker, reference, aggregate function... 72 SetFlag(flag uint64) 73 // GetFlag returns the flag of the expression. 74 GetFlag() uint64 75 76 // Format formats the AST into a writer. 77 Format(w io.Writer) 78 } 79 80 // OptBinary is used for parser. 81 type OptBinary struct { 82 IsBinary bool 83 Charset string 84 } 85 86 // FuncNode represents function call expression node. 87 type FuncNode interface { 88 ExprNode 89 functionExpression() 90 } 91 92 // StmtNode represents statement node. 93 // Name of implementations should have 'Stmt' suffix. 94 type StmtNode interface { 95 Node 96 statement() 97 } 98 99 // DDLNode represents DDL statement node. 100 type DDLNode interface { 101 StmtNode 102 ddlStatement() 103 } 104 105 // DMLNode represents DML statement node. 106 type DMLNode interface { 107 StmtNode 108 dmlStatement() 109 } 110 111 // ResultField represents a result field which can be a column from a table, 112 // or an expression in select field. It is a generated property during 113 // binding process. ResultField is the key element to evaluate a ColumnNameExpr. 114 // After resolving process, every ColumnNameExpr will be resolved to a ResultField. 115 // During execution, every row retrieved from table will set the row value to 116 // ResultFields of that table, so ColumnNameExpr resolved to that ResultField can be 117 // easily evaluated. 118 type ResultField struct { 119 Column *model.ColumnInfo 120 ColumnAsName model.CIStr 121 Table *model.TableInfo 122 TableAsName model.CIStr 123 DBName model.CIStr 124 125 // Expr represents the expression for the result field. If it is generated from a select field, it would 126 // be the expression of that select field, otherwise the type would be ValueExpr and value 127 // will be set for every retrieved row. 128 Expr ExprNode 129 TableName *TableName 130 // Referenced indicates the result field has been referenced or not. 131 // If not, we don't need to get the values. 132 Referenced bool 133 } 134 135 // ResultSetNode interface has a ResultFields property, represents a Node that returns result set. 136 // Implementations include SelectStmt, SubqueryExpr, TableSource, TableName and Join. 137 type ResultSetNode interface { 138 Node 139 } 140 141 // SensitiveStmtNode overloads StmtNode and provides a SecureText method. 142 type SensitiveStmtNode interface { 143 StmtNode 144 // SecureText is different from Text that it hide password information. 145 SecureText() string 146 } 147 148 // Visitor visits a Node. 149 type Visitor interface { 150 // Enter is called before children nodes are visited. 151 // The returned node must be the same type as the input node n. 152 // skipChildren returns true means children nodes should be skipped, 153 // this is useful when work is done in Enter and there is no need to visit children. 154 Enter(n Node) (node Node, skipChildren bool) 155 // Leave is called after children nodes have been visited. 156 // The returned node's type can be different from the input node if it is a ExprNode, 157 // Non-expression node must be the same type as the input node n. 158 // ok returns false to stop visiting. 159 Leave(n Node) (node Node, ok bool) 160 }