advisor.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2019 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/parser/format"
  16. )
  17. var _ StmtNode = &IndexAdviseStmt{}
  18. // IndexAdviseStmt is used to advise indexes
  19. type IndexAdviseStmt struct {
  20. stmtNode
  21. IsLocal bool
  22. Path string
  23. MaxMinutes uint64
  24. MaxIndexNum *MaxIndexNumClause
  25. LinesInfo *LinesClause
  26. }
  27. // Restore implements Node Accept interface.
  28. func (n *IndexAdviseStmt) Restore(ctx *format.RestoreCtx) error {
  29. ctx.WriteKeyWord("INDEX ADVISE ")
  30. if n.IsLocal {
  31. ctx.WriteKeyWord("LOCAL ")
  32. }
  33. ctx.WriteKeyWord("INFILE ")
  34. ctx.WriteString(n.Path)
  35. if n.MaxMinutes != UnspecifiedSize {
  36. ctx.WriteKeyWord(" MAX_MINUTES ")
  37. ctx.WritePlainf("%d", n.MaxMinutes)
  38. }
  39. if n.MaxIndexNum != nil {
  40. n.MaxIndexNum.Restore(ctx)
  41. }
  42. n.LinesInfo.Restore(ctx)
  43. return nil
  44. }
  45. // Accept implements Node Accept interface.
  46. func (n *IndexAdviseStmt) Accept(v Visitor) (Node, bool) {
  47. newNode, skipChildren := v.Enter(n)
  48. if skipChildren {
  49. return v.Leave(newNode)
  50. }
  51. n = newNode.(*IndexAdviseStmt)
  52. return v.Leave(n)
  53. }
  54. // MaxIndexNumClause represents 'maximum number of indexes' clause in index advise statement.
  55. type MaxIndexNumClause struct {
  56. PerTable uint64
  57. PerDB uint64
  58. }
  59. // Restore for max index num clause
  60. func (n *MaxIndexNumClause) Restore(ctx *format.RestoreCtx) error {
  61. ctx.WriteKeyWord(" MAX_IDXNUM")
  62. if n.PerTable != UnspecifiedSize {
  63. ctx.WriteKeyWord(" PER_TABLE ")
  64. ctx.WritePlainf("%d", n.PerTable)
  65. }
  66. if n.PerDB != UnspecifiedSize {
  67. ctx.WriteKeyWord(" PER_DB ")
  68. ctx.WritePlainf("%d", n.PerDB)
  69. }
  70. return nil
  71. }