github.com/matrixorigin/matrixone@v0.7.0/pkg/frontend/types.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 frontend
    16  
    17  import (
    18  	"context"
    19  	"strings"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    25  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    26  	"github.com/matrixorigin/matrixone/pkg/container/types"
    27  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    28  	"github.com/matrixorigin/matrixone/pkg/sql/plan"
    29  	"github.com/matrixorigin/matrixone/pkg/txn/client"
    30  )
    31  
    32  type (
    33  	TxnOperator = client.TxnOperator
    34  	TxnClient   = client.TxnClient
    35  	TxnOption   = client.TxnOption
    36  )
    37  
    38  type ComputationRunner interface {
    39  	Run(ts uint64) (err error)
    40  }
    41  
    42  // ComputationWrapper is the wrapper of the computation
    43  type ComputationWrapper interface {
    44  	ComputationRunner
    45  	GetAst() tree.Statement
    46  
    47  	GetProcess() *process.Process
    48  
    49  	SetDatabaseName(db string) error
    50  
    51  	GetColumns() ([]interface{}, error)
    52  
    53  	GetAffectedRows() uint64
    54  
    55  	Compile(requestCtx context.Context, u interface{}, fill func(interface{}, *batch.Batch) error) (interface{}, error)
    56  
    57  	GetUUID() []byte
    58  
    59  	RecordExecPlan(ctx context.Context) error
    60  
    61  	GetLoadTag() bool
    62  }
    63  
    64  type ColumnInfo interface {
    65  	GetName() string
    66  
    67  	GetType() types.T
    68  }
    69  
    70  var _ ColumnInfo = &engineColumnInfo{}
    71  
    72  type TableInfo interface {
    73  	GetColumns()
    74  }
    75  
    76  type engineColumnInfo struct {
    77  	name string
    78  	typ  types.Type
    79  }
    80  
    81  func (ec *engineColumnInfo) GetName() string {
    82  	return ec.name
    83  }
    84  
    85  func (ec *engineColumnInfo) GetType() types.T {
    86  	return ec.typ.Oid
    87  }
    88  
    89  type PrepareStmt struct {
    90  	Name        string
    91  	PreparePlan *plan.Plan
    92  	PrepareStmt tree.Statement
    93  	ParamTypes  []byte
    94  }
    95  
    96  /*
    97  Disguise the COMMAND CMD_FIELD_LIST as sql query.
    98  */
    99  const (
   100  	cmdFieldListSql = "__++__internal_cmd_field_list"
   101  	intereSql       = "internal_sql"
   102  	cloudUserSql    = "cloud_user_sql"
   103  	cloudNoUserSql  = "cloud_nonuser_sql"
   104  	externSql       = "external_sql"
   105  	cloudUserTag    = "cloud_user"
   106  	cloudNoUserTag  = "cloud_nonuser"
   107  )
   108  
   109  // isCmdFieldListSql checks the sql is the cmdFieldListSql or not.
   110  func isCmdFieldListSql(sql string) bool {
   111  	return strings.HasPrefix(strings.ToLower(sql), cmdFieldListSql)
   112  }
   113  
   114  // makeCmdFieldListSql makes the internal CMD_FIELD_LIST sql
   115  func makeCmdFieldListSql(query string) string {
   116  	return cmdFieldListSql + " " + query
   117  }
   118  
   119  // parseCmdFieldList parses the internal cmd field list
   120  func parseCmdFieldList(ctx context.Context, sql string) (*InternalCmdFieldList, error) {
   121  	if !isCmdFieldListSql(sql) {
   122  		return nil, moerr.NewInternalError(ctx, "it is not the CMD_FIELD_LIST")
   123  	}
   124  	rest := strings.TrimSpace(sql[len(cmdFieldListSql):])
   125  	//find null
   126  	nullIdx := strings.IndexRune(rest, rune(0))
   127  	var tableName string
   128  	if nullIdx < len(rest) {
   129  		tableName = rest[:nullIdx]
   130  		//neglect wildcard
   131  		//wildcard := payload[nullIdx+1:]
   132  		return &InternalCmdFieldList{tableName: tableName}, nil
   133  	} else {
   134  		return nil, moerr.NewInternalError(ctx, "wrong format for COM_FIELD_LIST")
   135  	}
   136  }
   137  
   138  var _ tree.Statement = &InternalCmdFieldList{}
   139  
   140  // InternalCmdFieldList the CMD_FIELD_LIST statement
   141  type InternalCmdFieldList struct {
   142  	tableName string
   143  }
   144  
   145  func (icfl *InternalCmdFieldList) String() string {
   146  	return makeCmdFieldListSql(icfl.tableName)
   147  }
   148  
   149  func (icfl *InternalCmdFieldList) Format(ctx *tree.FmtCtx) {
   150  	ctx.WriteString(makeCmdFieldListSql(icfl.tableName))
   151  }
   152  
   153  func (icfl *InternalCmdFieldList) GetStatementType() string { return "InternalCmd" }
   154  func (icfl *InternalCmdFieldList) GetQueryType() string     { return tree.QueryTypeDQL }
   155  
   156  // ExecResult is the result interface of the execution
   157  type ExecResult interface {
   158  	GetRowCount() uint64
   159  
   160  	GetString(ctx context.Context, rindex, cindex uint64) (string, error)
   161  
   162  	GetUint64(ctx context.Context, rindex, cindex uint64) (uint64, error)
   163  
   164  	GetInt64(ctx context.Context, rindex, cindex uint64) (int64, error)
   165  }
   166  
   167  func execResultArrayHasData(arr []ExecResult) bool {
   168  	return len(arr) != 0 && arr[0].GetRowCount() != 0
   169  }
   170  
   171  // BackgroundExec executes the sql in background session without network output.
   172  type BackgroundExec interface {
   173  	Close()
   174  	Exec(context.Context, string) error
   175  	GetExecResultSet() []interface{}
   176  	ClearExecResultSet()
   177  }
   178  
   179  var _ BackgroundExec = &BackgroundHandler{}
   180  
   181  type dumpTable struct {
   182  	name   string
   183  	ddl    string
   184  	rel    engine.Relation
   185  	attrs  []string
   186  	isView bool
   187  }
   188  
   189  // profile makes the debug info
   190  type profile interface {
   191  	makeProfile(profileTyp profileType)
   192  
   193  	getProfile(profileTyp profileType) string
   194  }
   195  
   196  var _ profile = &Session{}