github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/pipeline/pipeline.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 pipeline
    16  
    17  import (
    18  	"bytes"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    21  	"github.com/matrixorigin/matrixone/pkg/perfcounter"
    22  	"github.com/matrixorigin/matrixone/pkg/sql/colexec/table_scan"
    23  	"github.com/matrixorigin/matrixone/pkg/sql/colexec/value_scan"
    24  	"github.com/matrixorigin/matrixone/pkg/vm"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    27  )
    28  
    29  func New(tableID uint64, attrs []string, ins vm.Instructions, reg *process.WaitRegister) *Pipeline {
    30  	return &Pipeline{
    31  		reg:          reg,
    32  		instructions: ins,
    33  		attrs:        attrs,
    34  		tableID:      tableID,
    35  	}
    36  }
    37  
    38  func NewMerge(ins vm.Instructions, reg *process.WaitRegister) *Pipeline {
    39  	return &Pipeline{
    40  		reg:          reg,
    41  		instructions: ins,
    42  	}
    43  }
    44  
    45  func (p *Pipeline) String() string {
    46  	var buf bytes.Buffer
    47  
    48  	vm.String(p.instructions, &buf)
    49  	return buf.String()
    50  }
    51  
    52  func (p *Pipeline) Run(r engine.Reader, topValueMsgTag int32, proc *process.Process) (end bool, err error) {
    53  	// performance counter
    54  	perfCounterSet := new(perfcounter.CounterSet)
    55  	proc.Ctx = perfcounter.WithCounterSet(proc.Ctx, perfCounterSet)
    56  	defer func() {
    57  		_ = perfCounterSet //TODO
    58  	}()
    59  
    60  	// var bat *batch.Batch
    61  	// used to handle some push-down request
    62  	if p.reg != nil {
    63  		select {
    64  		case <-p.reg.Ctx.Done():
    65  		case <-p.reg.Ch:
    66  		}
    67  	}
    68  
    69  	tableScanOperator := table_scan.Argument{
    70  		Reader:         r,
    71  		TopValueMsgTag: topValueMsgTag,
    72  		Attrs:          p.attrs,
    73  		TableID:        p.tableID,
    74  	}
    75  	p.instructions = append([]vm.Instruction{
    76  		{
    77  			Op:      vm.TableScan,
    78  			Idx:     -1,
    79  			Arg:     &tableScanOperator,
    80  			IsFirst: true,
    81  			IsLast:  false,
    82  		},
    83  	}, p.instructions...)
    84  
    85  	if err = vm.Prepare(p.instructions, proc); err != nil {
    86  		return false, err
    87  	}
    88  	for {
    89  		end, err = vm.Run(p.instructions, proc)
    90  		if err != nil {
    91  			return end, err
    92  		}
    93  		if end {
    94  			// end is true means pipeline successfully completed
    95  			return end, nil
    96  		}
    97  	}
    98  }
    99  
   100  func (p *Pipeline) ConstRun(bat *batch.Batch, proc *process.Process) (end bool, err error) {
   101  	// used to handle some push-down request
   102  	if p.reg != nil {
   103  		select {
   104  		case <-p.reg.Ctx.Done():
   105  		case <-p.reg.Ch:
   106  		}
   107  	}
   108  	pipelineInputBatches := []*batch.Batch{bat, nil}
   109  
   110  	for _, ins := range p.instructions {
   111  		ins.Idx += 1
   112  		ins.IsFirst = false
   113  	}
   114  
   115  	valueScanOperator := value_scan.Argument{
   116  		Batchs: pipelineInputBatches,
   117  	}
   118  	p.instructions = append([]vm.Instruction{
   119  		{
   120  			Op:      vm.ValueScan,
   121  			Idx:     0,
   122  			Arg:     &valueScanOperator,
   123  			IsFirst: true,
   124  			IsLast:  false,
   125  		},
   126  	}, p.instructions...)
   127  
   128  	if err = vm.Prepare(p.instructions, proc); err != nil {
   129  		return false, err
   130  	}
   131  
   132  	for {
   133  		end, err = vm.Run(p.instructions, proc)
   134  		if err != nil {
   135  			return end, err
   136  		}
   137  		if end {
   138  			return end, nil
   139  		}
   140  	}
   141  }
   142  
   143  func (p *Pipeline) MergeRun(proc *process.Process) (end bool, err error) {
   144  	// used to handle some push-down request
   145  	if p.reg != nil {
   146  		select {
   147  		case <-p.reg.Ctx.Done():
   148  		case <-p.reg.Ch:
   149  		}
   150  	}
   151  
   152  	if err = vm.Prepare(p.instructions, proc); err != nil {
   153  		return false, err
   154  	}
   155  	for {
   156  		end, err = vm.Run(p.instructions, proc)
   157  		if err != nil {
   158  			return end, err
   159  		}
   160  		if end {
   161  			return end, nil
   162  		}
   163  	}
   164  }