github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/offset/offset.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 offset
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  func String(arg any, buf *bytes.Buffer) {
    26  	n := arg.(*Argument)
    27  	buf.WriteString(fmt.Sprintf("offset(%v)", n.Offset))
    28  }
    29  
    30  func Prepare(_ *process.Process, _ any) error {
    31  	return nil
    32  }
    33  
    34  func Call(idx int, proc *process.Process, arg any, isFirst bool, isLast bool) (bool, error) {
    35  	bat := proc.InputBatch()
    36  	if bat == nil {
    37  		return true, nil
    38  	}
    39  	if bat.Length() == 0 {
    40  		return false, nil
    41  	}
    42  	ap := arg.(*Argument)
    43  	anal := proc.GetAnalyze(idx)
    44  	anal.Start()
    45  	defer anal.Stop()
    46  	anal.Input(bat, isFirst)
    47  
    48  	if ap.Seen > ap.Offset {
    49  		return false, nil
    50  	}
    51  	length := bat.Length()
    52  	if ap.Seen+uint64(length) > ap.Offset {
    53  		sels := newSels(int64(ap.Offset-ap.Seen), int64(length)-int64(ap.Offset-ap.Seen), proc)
    54  		ap.Seen += uint64(length)
    55  		bat.Shrink(sels)
    56  		proc.Mp().PutSels(sels)
    57  		proc.SetInputBatch(bat)
    58  		return false, nil
    59  	}
    60  	ap.Seen += uint64(length)
    61  	bat.Clean(proc.Mp())
    62  	proc.SetInputBatch(&batch.Batch{})
    63  	return false, nil
    64  }
    65  
    66  func newSels(start, count int64, proc *process.Process) []int64 {
    67  	sels := proc.Mp().GetSels()
    68  	for i := int64(0); i < count; i++ {
    69  		sels = append(sels, start+i)
    70  	}
    71  	return sels[:count]
    72  }