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

     1  // Copyright 2021 - 2022 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 explain
    16  
    17  import (
    18  	"strconv"
    19  
    20  	"github.com/google/uuid"
    21  	"github.com/matrixorigin/matrixone/pkg/sql/plan"
    22  )
    23  
    24  type ExplainData struct {
    25  	Steps   []Step `json:"steps"`
    26  	Code    uint16 `json:"code"`
    27  	Message string `json:"message"`
    28  	Success bool   `json:"success"`
    29  	Uuid    string `json:"uuid"`
    30  }
    31  
    32  type Step struct {
    33  	GraphData   GraphData `json:"graphData"`
    34  	Step        int       `json:"step"`
    35  	Description string    `json:"description"`
    36  	State       string    `json:"state"`
    37  	PlanStats   PlanStats `json:"stats"`
    38  }
    39  
    40  type GraphData struct {
    41  	Nodes  []Node  `json:"nodes"`
    42  	Edges  []Edge  `json:"edges"`
    43  	Labels []Label `json:"labels"`
    44  	Global Global  `json:"global"`
    45  }
    46  
    47  type PlanStats struct {
    48  }
    49  
    50  type Stats struct {
    51  	BlockNum    int32   `json:"blocknum"`
    52  	Outcnt      float64 `json:"outcnt"`
    53  	Cost        float64 `json:"cost"`
    54  	HashmapSize float64 `json:"hashmapsize"`
    55  	Rowsize     float64 `json:"rowsize"`
    56  }
    57  
    58  type Node struct {
    59  	NodeId     string     `json:"id"`
    60  	Name       string     `json:"name"`
    61  	Title      string     `json:"title"`
    62  	Labels     []Label    `json:"labels"`
    63  	Statistics Statistics `json:"statistics"`
    64  	Stats      Stats      `json:"stats"`
    65  	TotalStats TotalStats `json:"totalStats"`
    66  }
    67  
    68  type Edge struct {
    69  	Id     string `json:"id"`
    70  	Src    string `json:"src"`
    71  	Dst    string `json:"dst"`
    72  	Output int64  `json:"output"`
    73  	Unit   string `json:"unit"`
    74  }
    75  
    76  type Label struct {
    77  	Name  string      `json:"name"`
    78  	Value interface{} `json:"value"`
    79  }
    80  
    81  type TotalStats struct {
    82  	Name  string `json:"name"`
    83  	Value int64  `json:"value"`
    84  	Unit  string `json:"unit"`
    85  }
    86  
    87  type Global struct {
    88  	Statistics Statistics `json:"statistics"`
    89  	TotalStats TotalStats `json:"totalStats"`
    90  }
    91  
    92  type Statistics struct {
    93  	Time       []StatisticValue `json:"Time"`
    94  	Memory     []StatisticValue `json:"Memory"`
    95  	Throughput []StatisticValue `json:"Throughput"`
    96  	IO         []StatisticValue `json:"IO"`
    97  	Network    []StatisticValue `json:"Network"`
    98  }
    99  
   100  type StatisticValue struct {
   101  	Name  string `json:"name"`
   102  	Value int64  `json:"value"`
   103  	Unit  string `json:"unit"`
   104  }
   105  
   106  func NewStatisticValue(name string, unit string) *StatisticValue {
   107  	return &StatisticValue{
   108  		Name: name,
   109  		Unit: unit,
   110  	}
   111  }
   112  
   113  func NewExplainData(uuid uuid.UUID) *ExplainData {
   114  	return &ExplainData{
   115  		Steps:   make([]Step, 0),
   116  		Success: true,
   117  		Uuid:    uuid.String(),
   118  	}
   119  }
   120  
   121  func NewExplainDataFail(uuid uuid.UUID, code uint16, msg string) *ExplainData {
   122  	return &ExplainData{
   123  		Code:    code,
   124  		Message: msg,
   125  		Success: false,
   126  		Uuid:    uuid.String(),
   127  	}
   128  }
   129  
   130  func NewStep(step int) *Step {
   131  	return &Step{
   132  		Step:        step,
   133  		Description: "",
   134  		State:       "success",
   135  	}
   136  }
   137  
   138  func NewGraphData() *GraphData {
   139  	return &GraphData{
   140  		Nodes:  make([]Node, 0),
   141  		Edges:  make([]Edge, 0),
   142  		Labels: make([]Label, 0),
   143  		Global: *NewGlobal(),
   144  	}
   145  }
   146  
   147  func NewGlobal() *Global {
   148  	statistics := Statistics{
   149  		Memory:     make([]StatisticValue, 0),
   150  		Throughput: make([]StatisticValue, 0),
   151  		IO:         make([]StatisticValue, 0),
   152  		Network:    make([]StatisticValue, 0),
   153  	}
   154  
   155  	return &Global{
   156  		Statistics: statistics,
   157  		TotalStats: TotalStats{},
   158  	}
   159  }
   160  
   161  func NewLabel(name string, value interface{}) *Label {
   162  	return &Label{
   163  		Name:  name,
   164  		Value: value,
   165  	}
   166  }
   167  
   168  func NewStatistics() *Statistics {
   169  	return &Statistics{
   170  		Memory:     make([]StatisticValue, 0),
   171  		Throughput: make([]StatisticValue, 0),
   172  		IO:         make([]StatisticValue, 0),
   173  		Network:    make([]StatisticValue, 0),
   174  	}
   175  }
   176  
   177  func buildEdge(parentNode *plan.Node, childNode *plan.Node, index int32) *Edge {
   178  	edge := &Edge{
   179  		Id:   "E" + strconv.Itoa(int(index)),
   180  		Src:  strconv.FormatInt(int64(childNode.NodeId), 10),
   181  		Dst:  strconv.FormatInt(int64(parentNode.NodeId), 10),
   182  		Unit: "count",
   183  	}
   184  	if childNode.AnalyzeInfo != nil {
   185  		edge.Output = childNode.AnalyzeInfo.OutputRows
   186  	}
   187  	return edge
   188  }