github.com/matrixorigin/matrixone@v1.2.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  	plan2 "github.com/matrixorigin/matrixone/pkg/pb/plan"
    21  	"github.com/matrixorigin/matrixone/pkg/util/trace/impl/motrace/statistic"
    22  
    23  	"github.com/google/uuid"
    24  	"github.com/matrixorigin/matrixone/pkg/sql/plan"
    25  )
    26  
    27  type ExplainData struct {
    28  	Steps        []Step `json:"steps"`
    29  	Code         uint16 `json:"code"`
    30  	Message      string `json:"message"`
    31  	Uuid         string `json:"uuid"`
    32  	NewPlanStats statistic.StatsInfo
    33  }
    34  
    35  type Step struct {
    36  	GraphData   GraphData `json:"graphData"`
    37  	Step        int       `json:"step"`
    38  	Description string    `json:"description"`
    39  	State       string    `json:"state"`
    40  	PlanStats   PlanStats `json:"stats"`
    41  }
    42  
    43  type GraphData struct {
    44  	Nodes  []Node  `json:"nodes"`
    45  	Edges  []Edge  `json:"edges"`
    46  	Labels []Label `json:"labels"`
    47  	Global Global  `json:"global"`
    48  }
    49  
    50  type PlanStats struct {
    51  }
    52  
    53  type Stats struct {
    54  	BlockNum    int32   `json:"blocknum"`
    55  	Outcnt      float64 `json:"outcnt"`
    56  	Cost        float64 `json:"cost"`
    57  	HashmapSize float64 `json:"hashmapsize"`
    58  	Rowsize     float64 `json:"rowsize"`
    59  }
    60  
    61  type Node struct {
    62  	NodeId     string     `json:"id"`
    63  	Name       string     `json:"name"`
    64  	Title      string     `json:"title"`
    65  	Labels     []Label    `json:"labels"`
    66  	Statistics Statistics `json:"statistics"`
    67  	Stats      Stats      `json:"stats"`
    68  	TotalStats TotalStats `json:"totalStats"`
    69  }
    70  
    71  type Edge struct {
    72  	Id     string `json:"id"`
    73  	Src    string `json:"src"`
    74  	Dst    string `json:"dst"`
    75  	Output int64  `json:"output"`
    76  	Unit   string `json:"unit"`
    77  }
    78  
    79  type Label struct {
    80  	Name  string      `json:"name"`
    81  	Value interface{} `json:"value"`
    82  }
    83  
    84  type TotalStats struct {
    85  	Name  string `json:"name"`
    86  	Value int64  `json:"value"`
    87  	Unit  string `json:"unit"`
    88  }
    89  
    90  type Global struct {
    91  	Statistics Statistics `json:"statistics"`
    92  	TotalStats TotalStats `json:"totalStats"`
    93  }
    94  
    95  type Statistics struct {
    96  	Time       []StatisticValue `json:"Time"`
    97  	Memory     []StatisticValue `json:"Memory"`
    98  	Throughput []StatisticValue `json:"Throughput"`
    99  	IO         []StatisticValue `json:"IO"`
   100  	Network    []StatisticValue `json:"Network"`
   101  }
   102  
   103  type StatisticValue struct {
   104  	Name  string `json:"name"`
   105  	Value int64  `json:"value"`
   106  	Unit  string `json:"unit"`
   107  }
   108  
   109  func NewStatisticValue(name string, unit string) *StatisticValue {
   110  	return &StatisticValue{
   111  		Name: name,
   112  		Unit: unit,
   113  	}
   114  }
   115  
   116  func NewExplainData(uuid uuid.UUID) *ExplainData {
   117  	return &ExplainData{
   118  		Steps: make([]Step, 0),
   119  		Uuid:  uuid.String(),
   120  	}
   121  }
   122  
   123  func NewExplainDataFail(uuid uuid.UUID, code uint16, msg string) *ExplainData {
   124  	return &ExplainData{
   125  		Code:    code,
   126  		Message: msg,
   127  		Uuid:    uuid.String(),
   128  	}
   129  }
   130  
   131  func NewStep(step int) *Step {
   132  	return &Step{
   133  		Step:        step,
   134  		Description: "",
   135  		State:       "success",
   136  	}
   137  }
   138  
   139  func NewGraphData(nodeSize int) *GraphData {
   140  	return &GraphData{
   141  		Nodes:  make([]Node, 0, nodeSize),
   142  		Edges:  make([]Edge, 0, nodeSize),
   143  		Labels: make([]Label, 0),
   144  		Global: *NewGlobal(),
   145  	}
   146  }
   147  
   148  func NewGlobal() *Global {
   149  	statistics := Statistics{
   150  		Memory:     make([]StatisticValue, 0),
   151  		Throughput: make([]StatisticValue, 0),
   152  		IO:         make([]StatisticValue, 0),
   153  		Network:    make([]StatisticValue, 0),
   154  	}
   155  
   156  	return &Global{
   157  		Statistics: statistics,
   158  		TotalStats: TotalStats{},
   159  	}
   160  }
   161  
   162  func NewLabel(name string, value interface{}) *Label {
   163  	return &Label{
   164  		Name:  name,
   165  		Value: value,
   166  	}
   167  }
   168  
   169  func NewStatistics() *Statistics {
   170  	return &Statistics{
   171  		Memory:     make([]StatisticValue, 0),
   172  		Throughput: make([]StatisticValue, 0),
   173  		IO:         make([]StatisticValue, 0),
   174  		Network:    make([]StatisticValue, 0),
   175  	}
   176  }
   177  
   178  func buildEdge(parentNode *plan.Node, childNode *plan.Node, index int32) *Edge {
   179  	edge := &Edge{
   180  		Id:   "E" + strconv.Itoa(int(index)),
   181  		Src:  strconv.FormatInt(int64(childNode.NodeId), 10),
   182  		Dst:  strconv.FormatInt(int64(parentNode.NodeId), 10),
   183  		Unit: "count",
   184  	}
   185  	if childNode.AnalyzeInfo != nil {
   186  		edge.Output = childNode.AnalyzeInfo.OutputRows
   187  	}
   188  	return edge
   189  }
   190  
   191  var nodeTypeToNameMap = map[plan2.Node_NodeType]string{
   192  	plan2.Node_UNKNOWN: "UnKnown Node",
   193  
   194  	plan2.Node_VALUE_SCAN:    "Values Scan",
   195  	plan2.Node_TABLE_SCAN:    "Table Scan",
   196  	plan2.Node_FUNCTION_SCAN: "Function Scan",
   197  	plan2.Node_EXTERNAL_SCAN: "External Scan",
   198  	plan2.Node_MATERIAL_SCAN: "Material Scan",
   199  	plan2.Node_SOURCE_SCAN:   "Source Scan",
   200  
   201  	plan2.Node_PROJECT: "Project",
   202  
   203  	plan2.Node_EXTERNAL_FUNCTION: "External Function",
   204  
   205  	plan2.Node_MATERIAL:       "Material",
   206  	plan2.Node_SINK:           "Sink",
   207  	plan2.Node_SINK_SCAN:      "Sink Scan",
   208  	plan2.Node_RECURSIVE_SCAN: "Recursive Scan",
   209  	plan2.Node_RECURSIVE_CTE:  "CTE Scan",
   210  
   211  	plan2.Node_AGG:       "Aggregate",
   212  	plan2.Node_DISTINCT:  "Distinct",
   213  	plan2.Node_FILTER:    "Filter",
   214  	plan2.Node_JOIN:      "Join",
   215  	plan2.Node_SAMPLE:    "Sample",
   216  	plan2.Node_SORT:      "Sort",
   217  	plan2.Node_UNION:     "Union",
   218  	plan2.Node_UNION_ALL: "Union All",
   219  	plan2.Node_UNIQUE:    "Unique",
   220  	plan2.Node_WINDOW:    "Window",
   221  
   222  	plan2.Node_BROADCAST: "Broadcast",
   223  	plan2.Node_SPLIT:     "Split",
   224  	plan2.Node_GATHER:    "Gather",
   225  
   226  	plan2.Node_ASSERT: "Assert",
   227  
   228  	plan2.Node_INSERT:  "Insert",
   229  	plan2.Node_DELETE:  "Delete",
   230  	plan2.Node_REPLACE: "Replace",
   231  
   232  	plan2.Node_LOCK_OP: "Lock Operator",
   233  
   234  	plan2.Node_INTERSECT:     "Intersect",
   235  	plan2.Node_INTERSECT_ALL: "Intersect All",
   236  	plan2.Node_MINUS:         "Minus",
   237  	plan2.Node_MINUS_ALL:     "Minus All",
   238  
   239  	plan2.Node_ON_DUPLICATE_KEY: "On Duplicate Key",
   240  	plan2.Node_PRE_INSERT:       "Pre Insert",
   241  	plan2.Node_PRE_DELETE:       "Pre Delete",
   242  	plan2.Node_PRE_INSERT_UK:    "Pre Insert Unique",
   243  	plan2.Node_PRE_INSERT_SK:    "Pre Insert 2nd Key",
   244  
   245  	plan2.Node_TIME_WINDOW:  "Time window",
   246  	plan2.Node_FILL:         "Fill",
   247  	plan2.Node_PARTITION:    "Partition",
   248  	plan2.Node_FUZZY_FILTER: "Fuzzy filter",
   249  }
   250  
   251  const (
   252  	Label_Table_Name                = "Full table name"
   253  	Label_Table_Columns             = "Columns"
   254  	Label_Total_Columns             = "Total columns"
   255  	Label_Scan_Columns              = "Scan columns"
   256  	Label_List_Expression           = "List of expressions"
   257  	Label_Grouping_Keys             = "Grouping keys"
   258  	Label_Agg_Functions             = "Aggregate functions"
   259  	Label_Filter_Conditions         = "Filter conditions"
   260  	Label_Block_Filter_Conditions   = "Block Filter conditions"
   261  	Label_Join_Type                 = "Join type"
   262  	Label_Join_Conditions           = "Join conditions"
   263  	Label_Left_NodeId               = "Left node id"
   264  	Label_Right_NodeId              = "Right node id"
   265  	Label_Sort_Keys                 = "Sort keys"
   266  	Label_List_Values               = "List of values"
   267  	Label_Union_Expressions         = "Union expressions"
   268  	Label_Union_All_Expressions     = "Union all expressions"
   269  	Label_Intersect_Expressions     = "Intersect expressions"
   270  	Label_Intersect_All_Expressions = "Intersect all expressions"
   271  	Label_Minus_Expressions         = "Minus expressions"
   272  	Label_Pre_Insert                = "Pre insert"
   273  	Label_Pre_InsertUk              = "Pre insert uk"
   274  	Label_Pre_InsertSk              = "Pre insert sk"
   275  	Label_Pre_Delete                = "Pre delete"
   276  	Label_Sink                      = "Sink"
   277  	Label_Sink_Scan                 = "Sink scan"
   278  	Label_Recursive_SCAN            = "Recursive scan"
   279  	Label_Recursive_CTE             = "CTE scan"
   280  	Label_Lock_Op                   = "Lock op"
   281  	Label_Row_Number                = "Number of rows"
   282  	Label_Offset                    = "Offset"
   283  
   284  	Label_Time_Window       = "Time window"
   285  	Label_Partition         = "Partition"
   286  	Label_Fill              = "Fill"
   287  	Label_Boardcast         = "Boardcast"
   288  	Label_Split             = "Split"
   289  	Label_Gather            = "Gather"
   290  	Label_Assert            = "Assert"
   291  	Label_On_Duplicate_Key  = "On duplicate key"
   292  	Label_Fuzzy_Filter      = "Fuzzy filter"
   293  	Label_External_Function = "External Function"
   294  	Label_Distinct          = "Distinct"
   295  	Label_Sample            = "Sample"
   296  	Label_Window            = "Window"
   297  	Label_Minus_All         = "Minus All"
   298  	Label_Unique            = "Unique"
   299  	Label_Replace           = "Replace"
   300  	Label_Unknown           = "Unknown"
   301  	Label_Meterial          = "Meterial"
   302  )
   303  
   304  const (
   305  	Statistic_Unit_ns    = "ns"
   306  	Statistic_Unit_count = "count"
   307  	Statistic_Unit_byte  = "byte"
   308  )