github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/source/source.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 source
    16  
    17  import (
    18  	"bytes"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    22  	mokafka "github.com/matrixorigin/matrixone/pkg/stream/adapter/kafka"
    23  	"github.com/matrixorigin/matrixone/pkg/util/trace"
    24  	"github.com/matrixorigin/matrixone/pkg/vm"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    26  )
    27  
    28  const argName = "source"
    29  
    30  func (arg *Argument) String(buf *bytes.Buffer) {
    31  	buf.WriteString(argName)
    32  	buf.WriteString(": source scan")
    33  }
    34  
    35  func (arg *Argument) Prepare(proc *process.Process) error {
    36  	_, span := trace.Start(proc.Ctx, "SourcePrepare")
    37  	defer span.End()
    38  
    39  	p := arg
    40  	p.attrs = make([]string, len(p.TblDef.Cols))
    41  	p.types = make([]types.Type, len(p.TblDef.Cols))
    42  	p.Configs = make(map[string]interface{})
    43  	for i, col := range p.TblDef.Cols {
    44  		p.attrs[i] = col.Name
    45  		p.types[i] = types.Type{
    46  			Oid:   types.T(col.Typ.Id),
    47  			Scale: col.Typ.Scale,
    48  			Width: col.Typ.Width,
    49  		}
    50  	}
    51  	for _, def := range p.TblDef.Defs {
    52  		switch v := def.Def.(type) {
    53  		case *plan.TableDef_DefType_Properties:
    54  			for _, x := range v.Properties.Properties {
    55  				p.Configs[x.Key] = x.Value
    56  			}
    57  		}
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  func (arg *Argument) Call(proc *process.Process) (vm.CallResult, error) {
    64  	if err, isCancel := vm.CancelCheck(proc); isCancel {
    65  		return vm.CancelResult, err
    66  	}
    67  
    68  	_, span := trace.Start(proc.Ctx, "SourceCall")
    69  	defer span.End()
    70  
    71  	if arg.buf != nil {
    72  		proc.PutBatch(arg.buf)
    73  		arg.buf = nil
    74  	}
    75  	result := vm.NewCallResult()
    76  	var err error
    77  
    78  	switch arg.status {
    79  	case retrieve:
    80  		arg.buf, err = mokafka.RetrieveData(proc.Ctx, proc.SessionInfo.SourceInMemScanBatch, arg.Configs, arg.attrs, arg.types, arg.Offset, arg.Limit, proc.Mp(), mokafka.NewKafkaAdapter)
    81  		if err != nil {
    82  			result.Status = vm.ExecStop
    83  			return result, err
    84  		}
    85  		arg.status = end
    86  		result.Batch = arg.buf
    87  		result.Status = vm.ExecNext
    88  	case end:
    89  		result.Status = vm.ExecStop
    90  	}
    91  
    92  	return result, nil
    93  }