github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/metadata_scan.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  	"strings"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    22  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    23  )
    24  
    25  var (
    26  	MetadataScanColDefs   = []*plan.ColDef{}
    27  	MetadataScanColTypes  = []types.Type{}
    28  	Metadata_Rows_Cnt_Pos int32
    29  )
    30  
    31  func init() {
    32  	// MAKE SURE THE TYPE ENUM BEGIN FROM 0 OR YOU WILL GET WRONG
    33  	// WHEN YOU FILL THE SLICE
    34  	mlen := len(plan.MetadataScanInfo_MetadataScanInfoType_name)
    35  	MetadataScanColTypes = make([]types.Type, mlen)
    36  	MetadataScanColDefs = make([]*plan.ColDef, mlen)
    37  	for i := range plan.MetadataScanInfo_MetadataScanInfoType_name {
    38  		var tp types.Type
    39  		switch plan.MetadataScanInfo_MetadataScanInfoType(i) {
    40  		case plan.MetadataScanInfo_COL_NAME:
    41  			tp = types.New(types.T_varchar, types.MaxVarcharLen, 0)
    42  		case plan.MetadataScanInfo_OBJECT_NAME:
    43  			tp = types.New(types.T_varchar, types.MaxVarcharLen, 0)
    44  		case plan.MetadataScanInfo_IS_HIDDEN:
    45  			tp = types.New(types.T_bool, 0, 0)
    46  		case plan.MetadataScanInfo_OBJ_LOC:
    47  			tp = types.New(types.T_varchar, types.MaxVarcharLen, 0)
    48  		case plan.MetadataScanInfo_CREATE_TS:
    49  			tp = types.New(types.T_TS, types.MaxVarcharLen, 0)
    50  		case plan.MetadataScanInfo_DELETE_TS:
    51  			tp = types.New(types.T_TS, types.MaxVarcharLen, 0)
    52  		case plan.MetadataScanInfo_ROWS_CNT:
    53  			tp = types.New(types.T_int64, 0, 0)
    54  			Metadata_Rows_Cnt_Pos = i
    55  		case plan.MetadataScanInfo_NULL_CNT:
    56  			tp = types.New(types.T_int64, 0, 0)
    57  		case plan.MetadataScanInfo_COMPRESS_SIZE:
    58  			tp = types.New(types.T_int64, 0, 0)
    59  		case plan.MetadataScanInfo_ORIGIN_SIZE:
    60  			tp = types.New(types.T_int64, 0, 0)
    61  		case plan.MetadataScanInfo_MIN: // TODO: find a way to show this info
    62  			tp = types.New(types.T_varbinary, types.MaxVarcharLen, 0)
    63  		case plan.MetadataScanInfo_MAX: // TODO: find a way to show this info
    64  			tp = types.New(types.T_varbinary, types.MaxVarcharLen, 0)
    65  		case plan.MetadataScanInfo_SUM: // TODO: find a way to show this info
    66  			tp = types.New(types.T_varbinary, types.MaxVarcharLen, 0)
    67  		default:
    68  			panic("unknown types when gen metadata scan info")
    69  		}
    70  
    71  		colname := plan.MetadataScanInfo_MetadataScanInfoType_name[i]
    72  		coldef := &plan.ColDef{
    73  			Name: strings.ToLower(colname),
    74  			Typ: plan.Type{
    75  				Id:          int32(tp.Oid),
    76  				NotNullable: true,
    77  			},
    78  			Default: &plan.Default{
    79  				NullAbility:  false,
    80  				Expr:         nil,
    81  				OriginString: "",
    82  			},
    83  		}
    84  
    85  		MetadataScanColTypes[i] = tp
    86  		MetadataScanColDefs[i] = coldef
    87  	}
    88  }
    89  
    90  func (builder *QueryBuilder) buildMetadataScan(tbl *tree.TableFunction, ctx *BindContext, exprs []*plan.Expr, childId int32) int32 {
    91  	node := &plan.Node{
    92  		NodeType: plan.Node_FUNCTION_SCAN,
    93  		Stats:    &plan.Stats{},
    94  		TableDef: &plan.TableDef{
    95  			TableType: "func_table",
    96  			TblFunc: &plan.TableFunction{
    97  				Name: "metadata_scan",
    98  			},
    99  			Cols: MetadataScanColDefs,
   100  		},
   101  		BindingTags:     []int32{builder.genNewTag()},
   102  		Children:        []int32{childId},
   103  		TblFuncExprList: exprs,
   104  	}
   105  	return builder.appendNode(node, ctx)
   106  }