github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/types.go (about)

     1  // Copyright 2021 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 vm
    16  
    17  import (
    18  	"bytes"
    19  	"time"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  type OpType int
    26  
    27  const (
    28  	Top OpType = iota
    29  	Limit
    30  	Order
    31  	Group
    32  	Window
    33  	TimeWin
    34  	Fill
    35  	Output
    36  	Offset
    37  	Product
    38  	Restrict
    39  	Dispatch
    40  	Connector
    41  	Projection
    42  
    43  	Join
    44  	LoopJoin
    45  	Left
    46  	LoopLeft
    47  	Single
    48  	LoopSingle
    49  	Semi
    50  	RightSemi
    51  	LoopSemi
    52  	Anti
    53  	RightAnti
    54  	LoopAnti
    55  	Mark
    56  	LoopMark
    57  	IndexJoin
    58  	IndexBuild
    59  
    60  	Merge
    61  	MergeTop
    62  	MergeLimit
    63  	MergeOrder
    64  	MergeGroup
    65  	MergeOffset
    66  	MergeRecursive
    67  	MergeCTE
    68  	Partition
    69  
    70  	Deletion
    71  	Insert
    72  	External
    73  	Source
    74  
    75  	Minus
    76  	Intersect
    77  	IntersectAll
    78  
    79  	HashBuild
    80  
    81  	TableFunction
    82  	TableScan
    83  	ValueScan
    84  	// MergeBlock is used to recieve S3 block metLoc Info, and write
    85  	// them to S3
    86  	MergeBlock
    87  	// MergeDelete is used to recieve S3 Blcok Delete Info from remote Cn
    88  	MergeDelete
    89  	Right
    90  	OnDuplicateKey
    91  	FuzzyFilter
    92  	PreInsert
    93  	PreInsertUnique
    94  	PreInsertSecondaryIndex
    95  	// LastInstructionOp is not a true operator and must set at last.
    96  	// It was used by unit testing to ensure that
    97  	// all functions related to instructions can reach 100% coverage.
    98  	LastInstructionOp
    99  
   100  	// LockOp is used to add locks to lockservice for pessimistic transactions.
   101  	// Operator that encounters a write conflict will block until the previous
   102  	// transaction has released the lock
   103  	LockOp
   104  
   105  	Shuffle
   106  
   107  	Sample
   108  )
   109  
   110  // Instruction contains relational algebra
   111  type Instruction struct {
   112  	// Op specified the operator code of an instruction.
   113  	Op OpType
   114  	// Idx specified the analysis information index.
   115  	Idx int
   116  	// Arg contains the operand of this instruction.
   117  	Arg Operator
   118  
   119  	// flag for analyzeInfo record the row information
   120  	IsFirst bool
   121  	IsLast  bool
   122  
   123  	CnAddr      string
   124  	OperatorID  int32
   125  	ParallelID  int32
   126  	MaxParallel int32
   127  }
   128  
   129  type Operator interface {
   130  	// Free release all the memory allocated from mPool in an operator.
   131  	// pipelineFailed marks the process status of the pipeline when the method is called.
   132  	Free(proc *process.Process, pipelineFailed bool, err error)
   133  
   134  	// String returns the string representation of an operator.
   135  	String(buf *bytes.Buffer)
   136  
   137  	//Prepare prepares an operator for execution.
   138  	Prepare(proc *process.Process) error
   139  
   140  	//Call calls an operator.
   141  	Call(proc *process.Process) (CallResult, error)
   142  
   143  	//Release an operator
   144  	Release()
   145  
   146  	// OperatorBase methods
   147  	SetInfo(info *OperatorInfo)
   148  	AppendChild(child Operator)
   149  
   150  	GetOperatorBase() *OperatorBase
   151  }
   152  
   153  type OperatorBase struct {
   154  	OperatorInfo
   155  	Children []Operator
   156  }
   157  
   158  func (o *OperatorBase) SetInfo(info *OperatorInfo) {
   159  	o.OperatorInfo = *info
   160  }
   161  
   162  func (o *OperatorBase) NumChildren() int {
   163  	return len(o.Children)
   164  }
   165  
   166  func (o *OperatorBase) AppendChild(child Operator) {
   167  	o.Children = append(o.Children, child)
   168  }
   169  
   170  func (o *OperatorBase) SetChildren(children []Operator) {
   171  	o.Children = children
   172  }
   173  
   174  func (o *OperatorBase) GetChildren(idx int) Operator {
   175  	return o.Children[idx]
   176  }
   177  
   178  func (o *OperatorBase) GetCnAddr() string {
   179  	return o.CnAddr
   180  }
   181  
   182  func (o *OperatorBase) GetOperatorID() int32 {
   183  	return o.OperatorID
   184  }
   185  
   186  func (o *OperatorBase) GetParalleID() int32 {
   187  	return o.ParallelID
   188  }
   189  
   190  func (o *OperatorBase) GetMaxParallel() int32 {
   191  	return o.MaxParallel
   192  }
   193  
   194  func (o *OperatorBase) GetIdx() int {
   195  	return o.Idx
   196  }
   197  
   198  func (o *OperatorBase) GetParallelIdx() int {
   199  	return o.ParallelIdx
   200  }
   201  
   202  func (o *OperatorBase) GetParallelMajor() bool {
   203  	return o.ParallelMajor
   204  }
   205  
   206  func (o *OperatorBase) GetIsFirst() bool {
   207  	return o.IsFirst
   208  }
   209  
   210  func (o *OperatorBase) GetIsLast() bool {
   211  	return o.IsLast
   212  }
   213  
   214  var CancelResult = CallResult{
   215  	Status: ExecStop,
   216  }
   217  
   218  func CancelCheck(proc *process.Process) (error, bool) {
   219  	select {
   220  	case <-proc.Ctx.Done():
   221  		return proc.Ctx.Err(), true
   222  	default:
   223  		return nil, false
   224  	}
   225  }
   226  
   227  func ChildrenCall(o Operator, proc *process.Process, anal process.Analyze) (CallResult, error) {
   228  	beforeChildrenCall := time.Now()
   229  	result, err := o.Call(proc)
   230  	anal.ChildrenCallStop(beforeChildrenCall)
   231  	return result, err
   232  }
   233  
   234  type ExecStatus int
   235  
   236  const (
   237  	ExecStop ExecStatus = iota
   238  	ExecNext
   239  	ExecHasMore
   240  )
   241  
   242  type CtrState int
   243  
   244  const (
   245  	Build CtrState = iota
   246  	Eval
   247  	End
   248  )
   249  
   250  type CallResult struct {
   251  	Status ExecStatus
   252  	Batch  *batch.Batch
   253  }
   254  
   255  func NewCallResult() CallResult {
   256  	return CallResult{
   257  		Status: ExecNext,
   258  	}
   259  }
   260  
   261  type OperatorInfo struct {
   262  	Idx           int
   263  	ParallelIdx   int
   264  	ParallelMajor bool
   265  	IsFirst       bool
   266  	IsLast        bool
   267  
   268  	CnAddr      string
   269  	OperatorID  int32
   270  	ParallelID  int32
   271  	MaxParallel int32
   272  }
   273  
   274  func (info OperatorInfo) GetAddress() process.MessageAddress {
   275  	return process.MessageAddress{
   276  		CnAddr:     info.CnAddr,
   277  		OperatorID: info.OperatorID,
   278  		ParallelID: info.ParallelID,
   279  	}
   280  }
   281  
   282  type Instructions []Instruction
   283  
   284  func (ins *Instruction) IsBrokenNode() bool {
   285  	switch ins.Op {
   286  	case Order, MergeOrder, Partition:
   287  		return true
   288  	case Limit, MergeLimit:
   289  		return true
   290  	case Offset, MergeOffset:
   291  		return true
   292  	case Group, MergeGroup:
   293  		return true
   294  	case Sample:
   295  		return true
   296  	case Top, MergeTop:
   297  		return true
   298  	case Window:
   299  		return true
   300  	case TimeWin, Fill:
   301  		return true
   302  	case MergeRecursive:
   303  		return true
   304  	}
   305  	return false
   306  }
   307  
   308  func (ins *Instruction) CannotRemote() bool {
   309  	// todo: I think we should add more operators here.
   310  	return ins.Op == LockOp
   311  }
   312  
   313  type ModificationArgument interface {
   314  	AffectedRows() uint64
   315  }