stats.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2017 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
  14. import (
  15. "github.com/pingcap/errors"
  16. "github.com/pingcap/parser/format"
  17. "github.com/pingcap/parser/model"
  18. )
  19. var (
  20. _ StmtNode = &AnalyzeTableStmt{}
  21. _ StmtNode = &DropStatsStmt{}
  22. _ StmtNode = &LoadStatsStmt{}
  23. )
  24. // AnalyzeTableStmt is used to create table statistics.
  25. type AnalyzeTableStmt struct {
  26. stmtNode
  27. TableNames []*TableName
  28. PartitionNames []model.CIStr
  29. IndexNames []model.CIStr
  30. AnalyzeOpts []AnalyzeOpt
  31. // IndexFlag is true when we only analyze indices for a table.
  32. IndexFlag bool
  33. Incremental bool
  34. // HistogramOperation is set in "ANALYZE TABLE ... UPDATE/DROP HISTOGRAM ..." statement.
  35. HistogramOperation HistogramOperationType
  36. ColumnNames []*ColumnName
  37. }
  38. // AnalyzeOptType is the type for analyze options.
  39. type AnalyzeOptionType int
  40. // Analyze option types.
  41. const (
  42. AnalyzeOptNumBuckets = iota
  43. AnalyzeOptNumTopN
  44. AnalyzeOptCMSketchDepth
  45. AnalyzeOptCMSketchWidth
  46. AnalyzeOptNumSamples
  47. )
  48. // AnalyzeOptionString stores the string form of analyze options.
  49. var AnalyzeOptionString = map[AnalyzeOptionType]string{
  50. AnalyzeOptNumBuckets: "BUCKETS",
  51. AnalyzeOptNumTopN: "TOPN",
  52. AnalyzeOptCMSketchWidth: "CMSKETCH WIDTH",
  53. AnalyzeOptCMSketchDepth: "CMSKETCH DEPTH",
  54. AnalyzeOptNumSamples: "SAMPLES",
  55. }
  56. // HistogramOperationType is the type for histogram operation.
  57. type HistogramOperationType int
  58. // Histogram operation types.
  59. const (
  60. // HistogramOperationNop shows no operation in histogram. Default value.
  61. HistogramOperationNop HistogramOperationType = iota
  62. HistogramOperationUpdate
  63. HistogramOperationDrop
  64. )
  65. // String implements fmt.Stringer for HistogramOperationType.
  66. func (hot HistogramOperationType) String() string {
  67. switch hot {
  68. case HistogramOperationUpdate:
  69. return "UPDATE HISTOGRAM"
  70. case HistogramOperationDrop:
  71. return "DROP HISTOGRAM"
  72. }
  73. return ""
  74. }
  75. // AnalyzeOpt stores the analyze option type and value.
  76. type AnalyzeOpt struct {
  77. Type AnalyzeOptionType
  78. Value uint64
  79. }
  80. // Restore implements Node interface.
  81. func (n *AnalyzeTableStmt) Restore(ctx *format.RestoreCtx) error {
  82. if n.Incremental {
  83. ctx.WriteKeyWord("ANALYZE INCREMENTAL TABLE ")
  84. } else {
  85. ctx.WriteKeyWord("ANALYZE TABLE ")
  86. }
  87. for i, table := range n.TableNames {
  88. if i != 0 {
  89. ctx.WritePlain(",")
  90. }
  91. if err := table.Restore(ctx); err != nil {
  92. return errors.Annotatef(err, "An error occurred while restore AnalyzeTableStmt.TableNames[%d]", i)
  93. }
  94. }
  95. if len(n.PartitionNames) != 0 {
  96. ctx.WriteKeyWord(" PARTITION ")
  97. }
  98. for i, partition := range n.PartitionNames {
  99. if i != 0 {
  100. ctx.WritePlain(",")
  101. }
  102. ctx.WriteName(partition.O)
  103. }
  104. if n.HistogramOperation != HistogramOperationNop {
  105. ctx.WritePlain(" ")
  106. ctx.WriteKeyWord(n.HistogramOperation.String())
  107. ctx.WritePlain(" ")
  108. }
  109. if len(n.ColumnNames) > 0 {
  110. ctx.WriteKeyWord("ON ")
  111. for i, columnName := range n.ColumnNames {
  112. if i != 0 {
  113. ctx.WritePlain(",")
  114. }
  115. ctx.WriteName(columnName.Name.O)
  116. }
  117. }
  118. if n.IndexFlag {
  119. ctx.WriteKeyWord(" INDEX")
  120. }
  121. for i, index := range n.IndexNames {
  122. if i != 0 {
  123. ctx.WritePlain(",")
  124. } else {
  125. ctx.WritePlain(" ")
  126. }
  127. ctx.WriteName(index.O)
  128. }
  129. if len(n.AnalyzeOpts) != 0 {
  130. ctx.WriteKeyWord(" WITH")
  131. for i, opt := range n.AnalyzeOpts {
  132. if i != 0 {
  133. ctx.WritePlain(",")
  134. }
  135. ctx.WritePlainf(" %d ", opt.Value)
  136. ctx.WritePlain(AnalyzeOptionString[opt.Type])
  137. }
  138. }
  139. return nil
  140. }
  141. // Accept implements Node Accept interface.
  142. func (n *AnalyzeTableStmt) Accept(v Visitor) (Node, bool) {
  143. newNode, skipChildren := v.Enter(n)
  144. if skipChildren {
  145. return v.Leave(newNode)
  146. }
  147. n = newNode.(*AnalyzeTableStmt)
  148. for i, val := range n.TableNames {
  149. node, ok := val.Accept(v)
  150. if !ok {
  151. return n, false
  152. }
  153. n.TableNames[i] = node.(*TableName)
  154. }
  155. return v.Leave(n)
  156. }
  157. // DropStatsStmt is used to drop table statistics.
  158. type DropStatsStmt struct {
  159. stmtNode
  160. Table *TableName
  161. PartitionNames []model.CIStr
  162. IsGlobalStats bool
  163. }
  164. // Restore implements Node interface.
  165. func (n *DropStatsStmt) Restore(ctx *format.RestoreCtx) error {
  166. ctx.WriteKeyWord("DROP STATS ")
  167. if err := n.Table.Restore(ctx); err != nil {
  168. return errors.Annotate(err, "An error occurred while add table")
  169. }
  170. if n.IsGlobalStats {
  171. ctx.WriteKeyWord(" GLOBAL")
  172. return nil
  173. }
  174. if len(n.PartitionNames) != 0 {
  175. ctx.WriteKeyWord(" PARTITION ")
  176. }
  177. for i, partition := range n.PartitionNames {
  178. if i != 0 {
  179. ctx.WritePlain(",")
  180. }
  181. ctx.WriteName(partition.O)
  182. }
  183. return nil
  184. }
  185. // Accept implements Node Accept interface.
  186. func (n *DropStatsStmt) Accept(v Visitor) (Node, bool) {
  187. newNode, skipChildren := v.Enter(n)
  188. if skipChildren {
  189. return v.Leave(newNode)
  190. }
  191. n = newNode.(*DropStatsStmt)
  192. node, ok := n.Table.Accept(v)
  193. if !ok {
  194. return n, false
  195. }
  196. n.Table = node.(*TableName)
  197. return v.Leave(n)
  198. }
  199. // LoadStatsStmt is the statement node for loading statistic.
  200. type LoadStatsStmt struct {
  201. stmtNode
  202. Path string
  203. }
  204. // Restore implements Node interface.
  205. func (n *LoadStatsStmt) Restore(ctx *format.RestoreCtx) error {
  206. ctx.WriteKeyWord("LOAD STATS ")
  207. ctx.WriteString(n.Path)
  208. return nil
  209. }
  210. // Accept implements Node Accept interface.
  211. func (n *LoadStatsStmt) Accept(v Visitor) (Node, bool) {
  212. newNode, skipChildren := v.Enter(n)
  213. if skipChildren {
  214. return v.Leave(newNode)
  215. }
  216. n = newNode.(*LoadStatsStmt)
  217. return v.Leave(n)
  218. }