github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/optimizer/plan/stringer.go (about)

     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  
    14  package plan
    15  
    16  import (
    17  	"fmt"
    18  	"math"
    19  	"strings"
    20  )
    21  
    22  // ToString explains a Plan, returns description string.
    23  func ToString(p Plan) string {
    24  	strs, _ := toString(p, []string{}, []int{})
    25  	return strings.Join(strs, "->")
    26  }
    27  
    28  func toString(in Plan, strs []string, idxs []int) ([]string, []int) {
    29  	switch in.(type) {
    30  	case *JoinOuter, *JoinInner, *Join:
    31  		idxs = append(idxs, len(strs))
    32  	}
    33  
    34  	for _, c := range in.GetChildren() {
    35  		strs, idxs = toString(c, strs, idxs)
    36  	}
    37  
    38  	var str string
    39  	switch x := in.(type) {
    40  	case *CheckTable:
    41  		str = "CheckTable"
    42  	case *IndexScan:
    43  		str = fmt.Sprintf("Index(%s.%s)", x.Table.Name.L, x.Index.Name.L)
    44  		if x.LimitCount != nil {
    45  			str += fmt.Sprintf(" + Limit(%v)", *x.LimitCount)
    46  		}
    47  	case *Limit:
    48  		str = "Limit"
    49  	case *SelectFields:
    50  		str = "Fields"
    51  	case *SelectLock:
    52  		str = "Lock"
    53  	case *ShowDDL:
    54  		str = "ShowDDL"
    55  	case *Filter:
    56  		str = "Filter"
    57  	case *Sort:
    58  		str = "Sort"
    59  		if x.ExecLimit != nil {
    60  			str += fmt.Sprintf(" + Limit(%v) + Offset(%v)", x.ExecLimit.Count, x.ExecLimit.Offset)
    61  		}
    62  	case *TableScan:
    63  		if len(x.Ranges) > 0 {
    64  			ran := x.Ranges[0]
    65  			if ran.LowVal != math.MinInt64 || ran.HighVal != math.MaxInt64 {
    66  				str = fmt.Sprintf("Range(%s)", x.Table.Name.L)
    67  			} else {
    68  				str = fmt.Sprintf("Table(%s)", x.Table.Name.L)
    69  			}
    70  		} else {
    71  			str = fmt.Sprintf("Table(%s)", x.Table.Name.L)
    72  		}
    73  		if x.LimitCount != nil {
    74  			str += fmt.Sprintf(" + Limit(%v)", *x.LimitCount)
    75  		}
    76  	case *JoinOuter:
    77  		last := len(idxs) - 1
    78  		idx := idxs[last]
    79  		children := strs[idx:]
    80  		strs = strs[:idx]
    81  		str = "OuterJoin{" + strings.Join(children, "->") + "}"
    82  		idxs = idxs[:last]
    83  	case *JoinInner:
    84  		last := len(idxs) - 1
    85  		idx := idxs[last]
    86  		children := strs[idx:]
    87  		strs = strs[:idx]
    88  		str = "InnerJoin{" + strings.Join(children, "->") + "}"
    89  		idxs = idxs[:last]
    90  	case *Join:
    91  		last := len(idxs) - 1
    92  		idx := idxs[last]
    93  		children := strs[idx:]
    94  		strs = strs[:idx]
    95  		str = "Join{" + strings.Join(children, "->") + "}"
    96  		idxs = idxs[:last]
    97  	case *Aggregate:
    98  		str = "Aggregate"
    99  	case *Distinct:
   100  		str = "Distinct"
   101  	default:
   102  		str = fmt.Sprintf("%T", in)
   103  	}
   104  	strs = append(strs, str)
   105  	return strs, idxs
   106  }