github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/system_view.go (about)

     1  // Copyright 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 plan
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/types"
    19  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    20  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    21  )
    22  
    23  var MoLocksColNames = []string{
    24  	"cn_id",
    25  	//"session_id",
    26  	"txn_id",
    27  	"table_id",
    28  	//"table_name",
    29  	"lock_key",
    30  	"lock_content",
    31  	"lock_mode",
    32  	"lock_status",
    33  	"lock_wait",
    34  }
    35  
    36  var MoLocksColTypes = []types.Type{
    37  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
    38  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
    39  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
    40  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
    41  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
    42  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
    43  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
    44  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
    45  	//types.New(types.T_varchar, types.MaxVarcharLen, 0),
    46  	//types.New(types.T_varchar, types.MaxVarcharLen, 0),
    47  }
    48  
    49  var MoLocksColName2Index = map[string]int32{
    50  	"cn_id":        0,
    51  	"txn_id":       1,
    52  	"table_id":     2,
    53  	"lock_key":     3,
    54  	"lock_content": 4,
    55  	"lock_mode":    5,
    56  	"lock_status":  6,
    57  	"lock_wait":    7,
    58  }
    59  
    60  type MoLocksColType int32
    61  
    62  const (
    63  	MoLocksColTypeCnId = iota
    64  	//MoLocksColTypeSessionId
    65  	MoLocksColTypeTxnId
    66  	MoLocksColTypeTableId
    67  	//MoLocksColTypeTableName
    68  	MoLocksColTypeLockKey
    69  	MoLocksColTypeLockContent
    70  	MoLocksColTypeLockMode
    71  	MoLocksColTypeLockStatus
    72  	MoLocksColTypeLockWait
    73  )
    74  
    75  func (builder *QueryBuilder) buildMoLocks(tbl *tree.TableFunction, ctx *BindContext, exprs []*plan.Expr, childId int32) (int32, error) {
    76  	var err error
    77  
    78  	colDefs := make([]*plan.ColDef, 0, len(MoLocksColNames))
    79  
    80  	for i, name := range MoLocksColNames {
    81  		colDefs = append(colDefs, &plan.ColDef{
    82  			Name: name,
    83  			Typ: plan.Type{
    84  				Id:    int32(MoLocksColTypes[i].Oid),
    85  				Width: MoLocksColTypes[i].Width,
    86  			},
    87  		})
    88  	}
    89  
    90  	node := &plan.Node{
    91  		NodeType: plan.Node_FUNCTION_SCAN,
    92  		Stats:    &plan.Stats{},
    93  		TableDef: &plan.TableDef{
    94  			TableType: "func_table",
    95  			TblFunc: &plan.TableFunction{
    96  				Name: "mo_locks",
    97  			},
    98  			Cols: colDefs,
    99  		},
   100  		BindingTags:     []int32{builder.genNewTag()},
   101  		Children:        []int32{childId},
   102  		TblFuncExprList: exprs,
   103  	}
   104  	return builder.appendNode(node, ctx), err
   105  }
   106  
   107  var MoConfigColNames = []string{
   108  	"node_type",
   109  	"node_id",
   110  	"name",
   111  	"current_value",
   112  	"default_value",
   113  	"internal",
   114  }
   115  
   116  var MoConfigColTypes = []types.Type{
   117  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   118  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   119  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   120  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   121  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   122  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   123  }
   124  
   125  var MoConfigColName2Index = map[string]int32{
   126  	"node_type":     0,
   127  	"node_id":       1,
   128  	"name":          2,
   129  	"current_value": 3,
   130  	"default_value": 4,
   131  	"internal":      5,
   132  }
   133  
   134  type MoConfigColType int32
   135  
   136  const (
   137  	MoConfigColTypeNodeType = iota
   138  	MoConfigColTypeNodeId
   139  	MoConfigColTypeName
   140  	MoConfigColTypeCurrentValue
   141  	MoConfigColTypeDefaultValue
   142  	MoConfigColTypeInternal
   143  )
   144  
   145  func (builder *QueryBuilder) buildMoConfigurations(tbl *tree.TableFunction, ctx *BindContext, exprs []*plan.Expr, childId int32) (int32, error) {
   146  	var err error
   147  
   148  	colDefs := make([]*plan.ColDef, 0, len(MoConfigColNames))
   149  
   150  	for i, name := range MoConfigColNames {
   151  		colDefs = append(colDefs, &plan.ColDef{
   152  			Name: name,
   153  			Typ: plan.Type{
   154  				Id:    int32(MoConfigColTypes[i].Oid),
   155  				Width: MoConfigColTypes[i].Width,
   156  			},
   157  		})
   158  	}
   159  
   160  	node := &plan.Node{
   161  		NodeType: plan.Node_FUNCTION_SCAN,
   162  		Stats:    &plan.Stats{},
   163  		TableDef: &plan.TableDef{
   164  			TableType: "func_table",
   165  			TblFunc: &plan.TableFunction{
   166  				Name: "mo_configurations",
   167  			},
   168  			Cols: colDefs,
   169  		},
   170  		BindingTags:     []int32{builder.genNewTag()},
   171  		Children:        []int32{childId},
   172  		TblFuncExprList: exprs,
   173  	}
   174  	return builder.appendNode(node, ctx), err
   175  }
   176  
   177  var MoTransactionsColNames = []string{
   178  	"cn_id",
   179  	"txn_id",
   180  	"create_ts",
   181  	"snapshot_ts",
   182  	"prepared_ts",
   183  	"commit_ts",
   184  	"txn_mode",
   185  	"isolation",
   186  	"user_txn",
   187  	"txn_status",
   188  	"table_id",
   189  	"lock_key",
   190  	"lock_content",
   191  	"lock_mode",
   192  }
   193  
   194  var MoTransactionsColTypes = []types.Type{
   195  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   196  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   197  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   198  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   199  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   200  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   201  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   202  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   203  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   204  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   205  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   206  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   207  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   208  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   209  }
   210  
   211  var MoTransactionsColName2Index = map[string]int32{
   212  	"cn_id":        0,
   213  	"txn_id":       1,
   214  	"create_ts":    2,
   215  	"snapshot_ts":  3,
   216  	"prepared_ts":  4,
   217  	"commit_ts":    5,
   218  	"txn_mode":     6,
   219  	"isolation":    7,
   220  	"user_txn":     8,
   221  	"txn_status":   9, //based on wait locks list
   222  	"table_id":     10,
   223  	"lock_key":     11,
   224  	"lock_content": 12,
   225  	"lock_mode":    13,
   226  }
   227  
   228  type MoTransactionsColType int32
   229  
   230  const (
   231  	MoTransactionsColTypeCnId = iota
   232  	MoTransactionsColTypeTxnId
   233  	MoTransactionsColTypeCreateTs
   234  	MoTransactionsColTypeSnapshotTs
   235  	MoTransactionsColTypePreparedTs
   236  	MoTransactionsColTypeCommitTs
   237  	MoTransactionsColTypeTxnMode
   238  	MoTransactionsColTypeIsolation
   239  	MoTransactionsColTypeUserTxn
   240  	MoTransactionsColTypeTxnStatus
   241  	MoTransactionsColTypeTableId
   242  	MoTransactionsColTypeLockKey
   243  	MoTransactionsColTypeLockContent
   244  	MoTransactionsColTypeLockMode
   245  )
   246  
   247  func (builder *QueryBuilder) buildMoTransactions(tbl *tree.TableFunction, ctx *BindContext, exprs []*plan.Expr, childId int32) (int32, error) {
   248  	var err error
   249  
   250  	colDefs := make([]*plan.ColDef, 0, len(MoTransactionsColNames))
   251  
   252  	for i, name := range MoTransactionsColNames {
   253  		colDefs = append(colDefs, &plan.ColDef{
   254  			Name: name,
   255  			Typ: plan.Type{
   256  				Id:    int32(MoTransactionsColTypes[i].Oid),
   257  				Width: MoTransactionsColTypes[i].Width,
   258  			},
   259  		})
   260  	}
   261  
   262  	node := &plan.Node{
   263  		NodeType: plan.Node_FUNCTION_SCAN,
   264  		Stats:    &plan.Stats{},
   265  		TableDef: &plan.TableDef{
   266  			TableType: "func_table",
   267  			TblFunc: &plan.TableFunction{
   268  				Name: "mo_transactions",
   269  			},
   270  			Cols: colDefs,
   271  		},
   272  		BindingTags:     []int32{builder.genNewTag()},
   273  		Children:        []int32{childId},
   274  		TblFuncExprList: exprs,
   275  	}
   276  	return builder.appendNode(node, ctx), err
   277  }
   278  
   279  var MoCacheColNames = []string{
   280  	"node_type",
   281  	"node_id",
   282  	"type",
   283  	"used",
   284  	"free",
   285  	"hit_ratio",
   286  }
   287  
   288  var MoCacheColTypes = []types.Type{
   289  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   290  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   291  	types.New(types.T_varchar, types.MaxVarcharLen, 0),
   292  	types.New(types.T_uint64, 0, 0),
   293  	types.New(types.T_uint64, 0, 0),
   294  	types.New(types.T_float32, 0, 0),
   295  }
   296  
   297  var MoCacheColName2Index = map[string]int32{
   298  	"node_type": 0,
   299  	"node_id":   1,
   300  	"type":      2,
   301  	"used":      3,
   302  	"free":      4,
   303  	"hit_ratio": 5,
   304  }
   305  
   306  type MoCacheColType int32
   307  
   308  const (
   309  	MoCacheColTypeNodeType = iota
   310  	MoCacheColTypeNodeId
   311  	MoCacheColTypeType
   312  	MoCacheColTypeUsed
   313  	MoCacheColTypeFree
   314  	MoCacheColTypeHitRatio
   315  )
   316  
   317  func (builder *QueryBuilder) buildMoCache(tbl *tree.TableFunction, ctx *BindContext, exprs []*plan.Expr, childId int32) (int32, error) {
   318  	var err error
   319  
   320  	colDefs := make([]*plan.ColDef, 0, len(MoCacheColNames))
   321  
   322  	for i, name := range MoCacheColNames {
   323  		colDefs = append(colDefs, &plan.ColDef{
   324  			Name: name,
   325  			Typ: plan.Type{
   326  				Id:    int32(MoCacheColTypes[i].Oid),
   327  				Width: MoCacheColTypes[i].Width,
   328  			},
   329  		})
   330  	}
   331  
   332  	node := &plan.Node{
   333  		NodeType: plan.Node_FUNCTION_SCAN,
   334  		Stats:    &plan.Stats{},
   335  		TableDef: &plan.TableDef{
   336  			TableType: "func_table",
   337  			TblFunc: &plan.TableFunction{
   338  				Name: "mo_cache",
   339  			},
   340  			Cols: colDefs,
   341  		},
   342  		BindingTags:     []int32{builder.genNewTag()},
   343  		Children:        []int32{childId},
   344  		TblFuncExprList: exprs,
   345  	}
   346  	return builder.appendNode(node, ctx), err
   347  }