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

     1  // Copyright 2021 - 2023 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  	"strings"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    23  	"github.com/matrixorigin/matrixone/pkg/pb/status"
    24  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    25  )
    26  
    27  var SessionsColTypes []types.Type
    28  
    29  func (builder *QueryBuilder) buildProcesslist(tbl *tree.TableFunction, ctx *BindContext, exprs []*plan.Expr, childId int32) (int32, error) {
    30  	if len(tbl.Func.Exprs) > 0 {
    31  		return 0, moerr.NewInvalidArg(builder.GetContext(), "processlist function has invalid input args length", len(tbl.Func.Exprs))
    32  	}
    33  	SessionsColTypes = make([]types.Type, len(status.SessionField_name))
    34  	sessionsColDefs := make([]*plan.ColDef, len(status.SessionField_name))
    35  
    36  	for i := range status.SessionField_name {
    37  		var typ types.Type
    38  		switch status.SessionField(i) {
    39  		case status.SessionField_CONN_ID:
    40  			typ = types.New(types.T_uint32, 0, 0)
    41  		default:
    42  			typ = types.New(types.T_varchar, types.MaxVarcharLen, 0)
    43  		}
    44  		SessionsColTypes[i] = typ
    45  		sessionsColDefs[i] = &plan.ColDef{
    46  			Name: strings.ToLower(status.SessionField_name[i]),
    47  			Typ: plan.Type{
    48  				Id:    int32(typ.Oid),
    49  				Width: typ.Width,
    50  			},
    51  		}
    52  	}
    53  	node := &plan.Node{
    54  		NodeType: plan.Node_FUNCTION_SCAN,
    55  		Stats:    &plan.Stats{},
    56  		TableDef: &plan.TableDef{
    57  			TableType: "func_table",
    58  			TblFunc: &plan.TableFunction{
    59  				Name: "processlist",
    60  			},
    61  			Cols: sessionsColDefs,
    62  		},
    63  		BindingTags:     []int32{builder.genNewTag()},
    64  		Children:        []int32{childId},
    65  		TblFuncExprList: exprs,
    66  	}
    67  	return builder.appendNode(node, ctx), nil
    68  }