github.com/XiaoMi/Gaea@v1.2.5/parser/ast/stats.go (about)

     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  
    14  package ast
    15  
    16  import (
    17  	"github.com/pingcap/errors"
    18  
    19  	"github.com/XiaoMi/Gaea/parser/format"
    20  	"github.com/XiaoMi/Gaea/parser/model"
    21  )
    22  
    23  var (
    24  	_ StmtNode = &AnalyzeTableStmt{}
    25  	_ StmtNode = &DropStatsStmt{}
    26  	_ StmtNode = &LoadStatsStmt{}
    27  )
    28  
    29  // AnalyzeTableStmt is used to create table statistics.
    30  type AnalyzeTableStmt struct {
    31  	stmtNode
    32  
    33  	TableNames     []*TableName
    34  	PartitionNames []model.CIStr
    35  	IndexNames     []model.CIStr
    36  	MaxNumBuckets  uint64
    37  
    38  	// IndexFlag is true when we only analyze indices for a table.
    39  	IndexFlag bool
    40  }
    41  
    42  // Restore implements Node interface.
    43  func (n *AnalyzeTableStmt) Restore(ctx *format.RestoreCtx) error {
    44  	ctx.WriteKeyWord("ANALYZE TABLE ")
    45  	for i, table := range n.TableNames {
    46  		if i != 0 {
    47  			ctx.WritePlain(",")
    48  		}
    49  		if err := table.Restore(ctx); err != nil {
    50  			return errors.Annotatef(err, "An error occurred while restore AnalyzeTableStmt.TableNames[%d]", i)
    51  		}
    52  	}
    53  	if len(n.PartitionNames) != 0 {
    54  		ctx.WriteKeyWord(" PARTITION ")
    55  	}
    56  	for i, partition := range n.PartitionNames {
    57  		if i != 0 {
    58  			ctx.WritePlain(",")
    59  		}
    60  		ctx.WriteName(partition.O)
    61  	}
    62  	if n.IndexFlag {
    63  		ctx.WriteKeyWord(" INDEX")
    64  	}
    65  	for i, index := range n.IndexNames {
    66  		if i != 0 {
    67  			ctx.WritePlain(",")
    68  		} else {
    69  			ctx.WritePlain(" ")
    70  		}
    71  		ctx.WriteName(index.O)
    72  	}
    73  	if n.MaxNumBuckets != 0 {
    74  		ctx.WriteKeyWord(" WITH ")
    75  		ctx.WritePlainf("%d", n.MaxNumBuckets)
    76  		ctx.WriteKeyWord(" BUCKETS")
    77  	}
    78  	return nil
    79  }
    80  
    81  // Accept implements Node Accept interface.
    82  func (n *AnalyzeTableStmt) Accept(v Visitor) (Node, bool) {
    83  	newNode, skipChildren := v.Enter(n)
    84  	if skipChildren {
    85  		return v.Leave(newNode)
    86  	}
    87  	n = newNode.(*AnalyzeTableStmt)
    88  	for i, val := range n.TableNames {
    89  		node, ok := val.Accept(v)
    90  		if !ok {
    91  			return n, false
    92  		}
    93  		n.TableNames[i] = node.(*TableName)
    94  	}
    95  	return v.Leave(n)
    96  }
    97  
    98  // DropStatsStmt is used to drop table statistics.
    99  type DropStatsStmt struct {
   100  	stmtNode
   101  
   102  	Table *TableName
   103  }
   104  
   105  // Restore implements Node interface.
   106  func (n *DropStatsStmt) Restore(ctx *format.RestoreCtx) error {
   107  	ctx.WriteKeyWord("DROP STATS ")
   108  	if err := n.Table.Restore(ctx); err != nil {
   109  		return errors.Annotate(err, "An error occurred while add table")
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  // Accept implements Node Accept interface.
   116  func (n *DropStatsStmt) Accept(v Visitor) (Node, bool) {
   117  	newNode, skipChildren := v.Enter(n)
   118  	if skipChildren {
   119  		return v.Leave(newNode)
   120  	}
   121  	n = newNode.(*DropStatsStmt)
   122  	node, ok := n.Table.Accept(v)
   123  	if !ok {
   124  		return n, false
   125  	}
   126  	n.Table = node.(*TableName)
   127  	return v.Leave(n)
   128  }
   129  
   130  // LoadStatsStmt is the statement node for loading statistic.
   131  type LoadStatsStmt struct {
   132  	stmtNode
   133  
   134  	Path string
   135  }
   136  
   137  // Restore implements Node interface.
   138  func (n *LoadStatsStmt) Restore(ctx *format.RestoreCtx) error {
   139  	ctx.WriteKeyWord("LOAD STATS ")
   140  	ctx.WriteString(n.Path)
   141  	return nil
   142  }
   143  
   144  // Accept implements Node Accept interface.
   145  func (n *LoadStatsStmt) Accept(v Visitor) (Node, bool) {
   146  	newNode, skipChildren := v.Enter(n)
   147  	if skipChildren {
   148  		return v.Leave(newNode)
   149  	}
   150  	n = newNode.(*LoadStatsStmt)
   151  	return v.Leave(n)
   152  }