ast.go 5.4 KB

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