github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/parsers/tree/explain.go (about)

     1  // Copyright 2021 Matrix Origin
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tree
    16  
    17  import (
    18  	"strconv"
    19  	"strings"
    20  )
    21  
    22  type Explain interface {
    23  	Statement
    24  }
    25  
    26  type explainImpl struct {
    27  	Explain
    28  	Statement Statement
    29  	Format    string
    30  	Options   []OptionElem
    31  }
    32  
    33  // EXPLAIN stmt statement
    34  type ExplainStmt struct {
    35  	explainImpl
    36  }
    37  
    38  func (node *ExplainStmt) Format(ctx *FmtCtx) {
    39  	ctx.WriteString("explain")
    40  	if node.Options != nil && len(node.Options) > 0 {
    41  		ctx.WriteString(" (")
    42  		var temp string
    43  		for _, v := range node.Options {
    44  			temp += v.Name
    45  			if v.Value != "NULL" {
    46  				temp += " " + v.Value
    47  			}
    48  			temp += ","
    49  		}
    50  		ctx.WriteString(temp[:len(temp)-1] + ")")
    51  	}
    52  
    53  	stmt := node.explainImpl.Statement
    54  	switch st := stmt.(type) {
    55  	case *ShowColumns:
    56  		if st.Table != nil {
    57  			ctx.WriteByte(' ')
    58  			st.Table.ToTableName().Format(ctx)
    59  		}
    60  		if st.ColName != nil {
    61  			ctx.WriteByte(' ')
    62  			st.ColName.Format(ctx)
    63  		}
    64  	default:
    65  		if stmt != nil {
    66  			ctx.WriteByte(' ')
    67  			stmt.Format(ctx)
    68  		}
    69  	}
    70  }
    71  
    72  func (node *ExplainStmt) GetStatementType() string { return "Explain" }
    73  func (node *ExplainStmt) GetQueryType() string     { return QueryTypeOth }
    74  
    75  func NewExplainStmt(stmt Statement, f string) *ExplainStmt {
    76  	return &ExplainStmt{explainImpl{Statement: stmt, Format: f}}
    77  }
    78  
    79  // EXPLAIN ANALYZE statement
    80  type ExplainAnalyze struct {
    81  	explainImpl
    82  }
    83  
    84  func (node *ExplainAnalyze) Format(ctx *FmtCtx) {
    85  	ctx.WriteString("explain")
    86  	if node.Options != nil && len(node.Options) > 0 {
    87  		ctx.WriteString(" (")
    88  		var temp string
    89  		for _, v := range node.Options {
    90  			temp += v.Name
    91  			if v.Value != "NULL" {
    92  				temp += " " + v.Value
    93  			}
    94  			temp += ","
    95  		}
    96  		ctx.WriteString(temp[:len(temp)-1] + ")")
    97  	}
    98  
    99  	stmt := node.explainImpl.Statement
   100  	switch st := stmt.(type) {
   101  	case *ShowColumns:
   102  		if st.Table != nil {
   103  			ctx.WriteByte(' ')
   104  			st.Table.ToTableName().Format(ctx)
   105  		}
   106  		if st.ColName != nil {
   107  			ctx.WriteByte(' ')
   108  			st.ColName.Format(ctx)
   109  		}
   110  	default:
   111  		if stmt != nil {
   112  			ctx.WriteByte(' ')
   113  			stmt.Format(ctx)
   114  		}
   115  	}
   116  }
   117  
   118  func (node *ExplainAnalyze) GetStatementType() string { return "Explain Analyze" }
   119  func (node *ExplainAnalyze) GetQueryType() string     { return QueryTypeOth }
   120  
   121  func NewExplainAnalyze(stmt Statement, f string) *ExplainAnalyze {
   122  	return &ExplainAnalyze{explainImpl{Statement: stmt, Format: f}}
   123  }
   124  
   125  // EXPLAIN FOR CONNECTION statement
   126  type ExplainFor struct {
   127  	explainImpl
   128  	ID uint64
   129  }
   130  
   131  func (node *ExplainFor) Format(ctx *FmtCtx) {
   132  	ctx.WriteString("explain format = ")
   133  	ctx.WriteString(node.explainImpl.Format)
   134  	ctx.WriteString(" for connection ")
   135  	ctx.WriteString(strconv.FormatInt(int64(node.ID), 10))
   136  }
   137  
   138  func (node *ExplainFor) GetStatementType() string { return "Explain Format" }
   139  func (node *ExplainFor) GetQueryType() string     { return QueryTypeOth }
   140  
   141  func NewExplainFor(f string, id uint64) *ExplainFor {
   142  	return &ExplainFor{
   143  		explainImpl: explainImpl{Statement: nil, Format: f},
   144  		ID:          id,
   145  	}
   146  }
   147  
   148  type OptionElem struct {
   149  	Name  string
   150  	Value string
   151  }
   152  
   153  func MakeOptionElem(name string, value string) OptionElem {
   154  	return OptionElem{
   155  		Name:  name,
   156  		Value: value,
   157  	}
   158  }
   159  
   160  func MakeOptions(elem OptionElem) []OptionElem {
   161  	var options = make([]OptionElem, 1)
   162  	options[0] = elem
   163  	return options
   164  }
   165  
   166  func IsContainAnalyze(options []OptionElem) bool {
   167  	if len(options) > 0 {
   168  		for _, option := range options {
   169  			if strings.EqualFold(option.Name, "analyze") && strings.EqualFold(option.Value, "true") {
   170  				return true
   171  			}
   172  		}
   173  	}
   174  	return false
   175  }